#include #include #include #include using namespace std; #define maxVeci 40 // max veci v batohu #define maxVaha 5999 // max vaha batohu #define constK 4 // k pro kombinovanou heuresticu FILE *src,*dest,*krok; unsigned int stepNr=0; // pocet zkoumanejch kroku int n; // pocet veci int M; // kapacita batohu int V[maxVeci]; // hmotnosti veci int C[maxVeci]; // ceny veci int nr[maxVeci]; // originalni posloupnost prvku int id; // id zadani bool maska[maxVeci]; // bitova maska pro bruteforce bool inKnap[maxVeci]; // je v batohu int knapPrice=0; // cena veci v batohu int knapWeight=0; // vaha v batohu time_t timeStart,timeEnd; // mereni casu int maxComb; // pocet kombinaci pro bruteforce a k-tice int combinations[constK]; // prave pouzivana ktice bool actInKnap[maxVeci]; //aktualni reseni int actPrice,actMaxTeor,actWeight; //aktualni parametry int pole[maxVeci][maxVaha]; //pole cen pro dynamickou verzi (max. 40 polozek pro instanci, maximalni nosnost batohu 999) //--------------------------------------------------------------------------------- void BruteForceComb(int comb=0) { for (int i=0; i=i; j--) { if ((C[j]/V[j]) > (C[i]/V[i])) { tmp=V[j];V[j]=V[i];V[i]=tmp; tmp=C[j];C[j]=C[i];C[i]=tmp; tmp=nr[j];nr[j]=nr[i];nr[i]=tmp; } stepNr++; } } } void Greedy(void) { for (int i=0; iC[maxPrice]) maxPrice=i; stepNr++; } if (knapPrice skoncili jsme rekurzi if (pole[n][space]!=-maxVaha) return (pole[n][space]); // vyresena mista uz neresime a vratime co uz bylo vyreseno if (V[pol]>space) pole[n][space]=ComputePrices(pol,space); // nevejde se tam, vlozim moznost bez prvku else { // vejde -> vlozim lepsi moznost with=C[pol]+ComputePrices(pol,space-V[pol]); // moznost s prvkem without=ComputePrices(pol,space); // moznost bez prvku if (with>without) { pole[n][space]=with; return (with); } else { pole[n][space]=without; return (without); } } return (pole[n][space]); } void Dynamic() { int i, j; for (i=0; i=1; i--) { pol=i-1; // prepocet z poctu na index if (pole[i][vaha]!=pole[pol][vaha]) { inKnap[pol]=true; vaha=vaha-V[pol]; } else inKnap[pol]=false; } } //--------------------------------------------------------------------------------- void DoBest(void) { int actPrice = 0; for (int i=0; iknapPrice) { knapPrice=actPrice; for (int i=0; iknapPrice) { // nejlepsi reseni? knapPrice=actPrice; for (i=0; i2) metoda=atoi(argv[2]); if (metoda<0 || metoda>4) metoda=0; if (argc>1) strcpy(strin,argv[1]); else return EXIT_SUCCESS; if ((src=fopen(strin,"r"))==NULL) { printf(" Error opening %s input file.\n",strin); return EXIT_FAILURE; } sprintf(str,"price%d_%s",metoda,strin); if ((dest=fopen(str,"w"))==NULL) { printf("Error opening %s output file.\n",str); fclose(src); return EXIT_FAILURE; } sprintf(str,"step%d_%s",metoda,strin); if ((krok=fopen(str,"w"))==NULL) { printf("Error opening %s output file.\n",str); fclose(src); fclose(dest); return EXIT_FAILURE; } printf(" %d selected. Working.....\n\n",metoda); for (int rad=0; rad<50; rad++) { knapWeight=0; knapPrice=0; stepNr=0; // nacteni dat ze souboru fscanf(src,"%d ",&id); // id fscanf(src,"%d ",&n); // pocet veci fscanf(src,"%d ",&M); // kapacita batohu if (n>maxVeci) { printf("Warning: maxitems overloaded. Using only %d items.\n",maxVeci); n=maxVeci; } if (M>maxVaha) { printf("Warning: knapsack maxweight overloaded. Using only %d.\n",maxVaha); M=maxVaha; } for (int j=0;j