[text] pareto set

Viewer

copydownloadembedprintName: pareto set
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. typedef struct variables {
  7.     float variablesp;
  8.     struct variables* next;
  9. } tvariables;
  10. typedef tvariables* pvariables;
  11. typedef struct province {
  12.     char namep[100];
  13.     pvariables variables;
  14.     struct province* succ;
  15. } tprovince;
  16. typedef tprovince* pprovince;
  17. typedef struct paretoset {
  18.     pprovince selection;
  19.     struct paretoset* next;
  20. } tparetoset;
  21.  
  22. typedef tparetoset* pparetoset;
  23.  
  24. bool checkdata(char* data) {// it search the 2023 string
  25.     char target[] = "2023";
  26.     int a = strlen(data);
  27.     int b = strlen(target);
  28.     int count = 0;
  29.     while (count != a) {
  30.         int check = 0;
  31.         if (target[0] == data[count]) {
  32.             count++;
  33.             check++;
  34.             for (int i = 1; i < b; i++) {
  35.                 if (target[i] == data[count]) {
  36.                     check++;
  37.                 }
  38.                 count++;
  39.             }
  40.         }
  41.         count++;
  42.         if (b == check) {
  43.             return true;
  44.         }
  45.     }
  46.     return false;
  47. }
  48. pprovince checkrepeat(pprovince head, char* name) {// it filter for data(2023)
  49.     while (head != NULL) {
  50.         if (strcmp(head->namep, name) == 0) {
  51.             return head;
  52.         }
  53.         head = head->succ;
  54.     }
  55.     return NULL;
  56. }
  57. void insertinprovince(pprovince* new, float data) {
  58.     pvariables newvariable = (pvariables)malloc(sizeof(tvariables));
  59.     newvariable->variablesp = data;
  60.     newvariable->next = (*new)->variables;
  61.     (*new)->variables = newvariable;
  62. }
  63.  void create(pprovince *head, char* name, float data) {// it create the memories for the list and take the first index and sort it
  64.      pprovince prec = NULL;
  65.      pprovince p = *head;
  66.     pprovince new = (pprovince)malloc(sizeof(tprovince));
  67.     if (new == NULL) {
  68.         printf("nothing works anymore");
  69.         exit(EXIT_FAILURE);
  70.     }
  71.     strcpy(new->namep, name);
  72.     new->variables = NULL;
  73.     insertinprovince(&new, data);
  74.     while(p!=NULL){
  75.         if(new->variables->variablesp<p->variables->variablesp){
  76.             break;
  77.         }
  78.         prec = p;
  79.         p = p->succ;
  80.     }
  81.     if (prec == NULL) {
  82.         new->succ = *head;
  83.         *head = new;
  84.     }
  85.     else {
  86.         new->succ = p;
  87.         prec->succ = new;
  88.     }
  89. }
  90. pprovince allocatememory(pprovince* head, char* name, float support) {// it fill the struct
  91.     pprovince p;
  92.     p = checkrepeat(*head, name);
  93.     if (p != NULL) {
  94.         insertinprovince(&p, support);
  95.     }
  96.     else {
  97.          create(head, name, support);
  98.      
  99.     }
  100.     return *head;
  101. }
  102. pprovince loadfromfile() {// it take the data from the file, it work properly
  103.     pprovince head = NULL;
  104.     FILE* p = fopen("input.txt", "r");
  105.     if (p == NULL) {
  106.         printf("nothing works anymore'");
  107.         return NULL;
  108.     }
  109.     char name[100];
  110.     float support;
  111.     char data[1000];
  112.  
  113.     while (!feof(p)) {
  114.         if (fscanf(p, "%99[^,],%*[^,],%*[^,],%*[^,],%f,%999[^\n]\n", name, &support, data) == 3) {
  115.             if (checkdata(data)) {
  116.                 head = allocatememory(&head, name, support);
  117.             }
  118.         }
  119.         head->variables->variablesp;
  120.     }
  121.     fclose(p);
  122.     return head;
  123. }
  124. pparetoset allocateparetoset(pparetoset head, pprovince p) {
  125.     pparetoset new = (pparetoset)malloc(sizeof(tparetoset));
  126.     new->selection = p;
  127.     new->next = head;
  128.     head = new;
  129.     return head;
  130. }
  131. bool domination(pvariables v1, pvariables v2) {
  132.     while (v1 != NULL && v2 != NULL) {
  133.         if (v1->variablesp - v2->variablesp < 0) {
  134.             return false;//v1 non è niente
  135.         }
  136.         v1 = v1->next;
  137.         v2 = v2->next;
  138.     }
  139.     return true;//v1 domina
  140. }
  141. pparetoset elimination(pparetoset head,pprovince m){//  eliminates 
  142.     pparetoset prec = NULL;
  143.     pparetoset p = head;
  144.     while(p!=NULL && domination(p->selection->variables,m->variables)){
  145.         prec = p;
  146.         p = p->next;
  147.     }
  148.     if (p == NULL)return head;
  149.     if (prec==NULL){
  150.         head = head->next;
  151.         free(p);
  152.     }
  153.     else{
  154.         prec->next = p->next;
  155.         free(p);
  156.     }
  157.  
  158.     return head;
  159. }
  160. pparetoset searchparetoset(pprovince head) {// algorithm with problem that search the pareto set trough data
  161.     pparetoset head = NULL;
  162.     pprovince p = head;
  163.     pparetoset support;
  164.     int a =0 ;
  165.     head = allocateparetoset(head, p);
  166.     p = p->succ;
  167.     for(;p!=NULL;p=p->succ){// il primo ciclo for che va avanti tra le provincie
  168.         support = head;
  169.         for(support;support!=NULL;support=support->next){// secondo ciclo for che controlla se nessuna delle province presenti nella pareto set domina la p in monitoraggio
  170.         if(domination(support->selection->variables,p->variables)){
  171.             a++;
  172.         }
  173.         }
  174.     if(a==0){
  175.         head =elimination(head, p);
  176.         head = allocateparetoset(head, p);
  177.         support = head;
  178.     }
  179.     }
  180.     return head;
  181. }
  182. void printvalues(pvariables x) {
  183.     while (x != NULL) {
  184.         printf("%f ", x->variablesp);
  185.         x = x->next;
  186.     }
  187.     printf("\n");
  188. }
  189. void printparetoset(pparetoset head) {// stampa il risultato
  190.     printf("\n pareto set: \n");
  191.     while (head != NULL) {
  192.         printf("\n***%s***\n", head->selection->namep);
  193.         printf("Values of the variables:\n");
  194.         printvalues(head->selection->variables);
  195.         head = head->next;
  196.     }
  197. }
  198. int main() {
  199.     pprovince head = loadfromfile();
  200.  
  201.     pparetoset head = searchparetoset(head);
  202.     printparetoset(head);
  203.     return 0;
  204. }
  205.  

Editor

You can edit this paste and save as new:


File Description
  • pareto set
  • Paste Code
  • 14 Apr-2024
  • 5.55 Kb
You can Share it: