Advertisement
Guest User

Untitled

a guest
May 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include "graphics.h"
  5. #include<fstream>
  6. #include<iostream>
  7. #include <string>
  8. #include <iostream>
  9. #include <thread>
  10.  
  11. #define MAX 20
  12.  
  13. using namespace std;
  14.  
  15. string zestaw_1 = "daneBB.txt";
  16. string zestaw_2 = "daneBB1.txt";
  17.  
  18. // Mierzenie czasu
  19. LARGE_INTEGER performanceCountStart, performanceCountEnd;
  20. LARGE_INTEGER startTimer()
  21. {
  22.     LARGE_INTEGER start;
  23.     DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
  24.     QueryPerformanceCounter(&start);
  25.     SetThreadAffinityMask(GetCurrentThread(), oldmask);
  26.     return
  27.         start;
  28. }
  29. LARGE_INTEGER endTimer()
  30. {
  31.     LARGE_INTEGER stop;
  32.     DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
  33.     QueryPerformanceCounter(&stop);
  34.     SetThreadAffinityMask(GetCurrentThread(), oldmask);
  35.     return
  36.         stop;
  37. }
  38.  
  39. // Zmienne algorytmu
  40. float profit[MAX] = { 0 };
  41. float weight[MAX] = { 0 };
  42. float capacityRucksack;
  43. int amountOfElements;
  44.  
  45. // Struktura węzła
  46. struct node
  47. {
  48.     float lowerBound;
  49.     float upperBound;
  50.     int tag;
  51.     int objno;
  52. };
  53.  
  54.  
  55. void load_from_file(string file_name, float (&profit)[MAX], float (&weight)[MAX], float &capacityRucksack, int& amountOfElements)
  56. {
  57.  
  58.     ifstream odczyt(file_name);
  59.     odczyt >> amountOfElements;
  60.     for (int i = 1; i <= amountOfElements; i++)
  61.     {
  62.         odczyt >> weight[i] >> profit[i];
  63.     }
  64.     odczyt >> capacityRucksack;
  65.     odczyt.close();
  66. }
  67.  
  68. // Interfejs - menu
  69. void accept()
  70. {
  71.     int what;
  72.     cout << "1. Manualne wprowadzanie danych\n2. Wczytywanie danych z pliku\n\n\tWybor: ";
  73.     cin >> what;
  74.     if (what == 1)
  75.     {
  76.         cout << "\n\nIlosc elementow: ";
  77.         cin >> amountOfElements;
  78.  
  79.         for (int i = 1; i <= amountOfElements; i++)
  80.         {
  81.             cout << "\nelement " << i;
  82.             cout << "\n\twaga: ";
  83.             cin >> weight[i];
  84.             cout << "\n\tprofit: ";
  85.             cin >> profit[i];
  86.  
  87.         }
  88.         cout << "\nPojemnosc plecaka: ";
  89.         cin >> capacityRucksack;
  90.     }
  91.     else if (what == 2)
  92.     {
  93.         string file_name = "";
  94.         cout << "\nPodaj nazwe pliku: ";
  95.         cin >> file_name;
  96.         load_from_file(file_name,profit,weight,capacityRucksack,amountOfElements);
  97.        
  98.         cout << "\nIlosc elementow: " << amountOfElements;
  99.         cout << "\nelement\twaga\tprofit\n";
  100.         for (int i = 1; i <= amountOfElements; i++)
  101.         {
  102.             cout << "   " << i << "\t" << weight[i] << "\t" << profit[i] << endl;
  103.         }
  104.         cout << "\nPojemnosc plecaka: " << capacityRucksack;
  105.     }
  106.     else
  107.     {
  108.         cout << "Blad w wyborze w menu! Dowidzenia!";
  109.         exit(0);
  110.     }
  111. }
  112.  
  113.  
  114. float ubound(float cProfit, float cWeight, int k, float capacityRucksack)
  115. {
  116.     for (int i = k + 1; i <= amountOfElements; i++)
  117.     {
  118.         if (cWeight + weight[i] <= capacityRucksack)
  119.         {
  120.             cWeight = cWeight + weight[i];
  121.             cProfit = cProfit - profit[i];
  122.         }
  123.     }
  124.     return cProfit;
  125. }
  126.  
  127. float bound(float cProfit, float cWeight, int k)
  128. {
  129.     for (int i = k + 1; i <= amountOfElements; i++)
  130.     {
  131.         if (cWeight + weight[i] <= capacityRucksack)
  132.         {
  133.             cWeight = cWeight + weight[i];
  134.             cProfit = cProfit - profit[i];
  135.         }
  136.         else
  137.         {
  138.             return (cProfit - ((capacityRucksack - cWeight) / weight[i])*profit[i]);
  139.         }
  140.     }
  141.     return cProfit;
  142. }
  143.  
  144. void LCsearch(float profit[MAX], float weight[MAX], float capacityRucksack, int amountOfElements)
  145. {
  146.     int i = 1;
  147.     int k;
  148.     int vector[10] = {};
  149.     float wWeight = 0;
  150.     float pProfit = 0;
  151.     float upper = 999;
  152.     struct node cLeft, cRight, e;
  153.  
  154.     e.upperBound = ubound(0, 0, 0, capacityRucksack);
  155.     e.lowerBound = bound(0, 0, 0);
  156.     e.tag = -1;
  157.     e.objno = 0;
  158.     upper = e.upperBound;
  159.     while (true)
  160.     {
  161.         k = e.objno + 1;
  162.         cRight.upperBound = ubound(pProfit, wWeight, k, capacityRucksack);
  163.         cRight.lowerBound = bound(pProfit, wWeight, k);
  164.         cRight.tag = 0;
  165.         cRight.objno = k;
  166.         if (cRight.upperBound < upper)
  167.         {
  168.             upper = cRight.upperBound;
  169.         }
  170.         cLeft.tag = 1;
  171.         cLeft.objno = k;
  172.         if (wWeight + weight[k] <= capacityRucksack)
  173.         {
  174.             cLeft.upperBound = ubound(pProfit - profit[k], wWeight + weight[k], k, capacityRucksack);
  175.             cLeft.lowerBound = bound(pProfit - profit[k], wWeight + weight[k], k);
  176.         }
  177.         else
  178.         {
  179.  
  180.             e.upperBound = pProfit;
  181.             cLeft.lowerBound = pProfit;// bound(pProfit-profit[k],wWeight+weight[k],k);
  182.         }
  183.  
  184.         if (cLeft.lowerBound <= cRight.lowerBound && cLeft.upperBound <= cRight.upperBound)
  185.         {
  186.             e = cLeft;
  187.         }
  188.         else
  189.         {
  190.             e = cRight;
  191.         }
  192.         vector[i] = e.tag;
  193.         i++;
  194.  
  195.         if (e.tag == 1)
  196.         {
  197.             pProfit = pProfit - profit[(e.objno)];
  198.             wWeight = wWeight + weight[(e.objno)];
  199.         }
  200.  
  201.         if (e.objno == amountOfElements)
  202.         {
  203.             break;
  204.         }
  205.     }
  206.  
  207.     printf("\n\nSolution vector = ");
  208.     for (int i = 1; i <= amountOfElements; i++)
  209.     {
  210.         cout << vector[i]<< "\t";
  211.     }
  212. }
  213.  
  214. void task1(string zestaw)
  215. {
  216.     // Zmienne algorytmu
  217.     float profit[MAX] = { 0 };
  218.     float weight[MAX] = { 0 };
  219.     float capacityRucksack = 0.0;
  220.     int amountOfElements = 0;
  221.  
  222.     load_from_file(zestaw, profit, weight, capacityRucksack, amountOfElements);
  223.     LCsearch(profit, weight, capacityRucksack, amountOfElements);
  224. }
  225.  
  226. void main()
  227. {
  228. /* Na potrzeby testów zakomentowane
  229. TEN KOD DZIAŁA TAK JAK PO STAREMU!
  230.     accept();
  231.     LCsearch(profit,weight,capacityRucksack,amountOfElements);
  232. */
  233.  
  234.     // Watek 1
  235.     thread t1(task1, zestaw_1);
  236.  
  237.     // Wstrzymujemy maina przed zakonczeniem
  238.     t1.join();
  239.  
  240.     cout << endl;
  241.     system("pause");
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.     /*LARGE_INTEGER performanceCountStart, performanceCountEnd;
  262.     ofstream zapis("wyniki1.txt");
  263.     for (int i = 0; i < 100; i++)
  264.     {
  265.     performanceCountStart = startTimer(); //zapami©tujemy czas pocz¥tkowy
  266.     LCsearch();
  267.     performanceCountEnd = endTimer(); //zapami©tujemy koniec czasu
  268.     double tm = performanceCountEnd.QuadPart - performanceCountStart.QuadPart;
  269.     zapis << i+1 << ' ' << tm << endl;
  270.     }
  271.     zapis.close();*/
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement