Advertisement
Guest User

werewr

a guest
Jan 17th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.62 KB | None | 0 0
  1.  #ifndef HILLCLIMBING_H
  2. #define HILLCLIMBING_H
  3. #include <ctime>
  4. #include "container.h"
  5. #include "buildfunction.h"
  6. #include <QDebug>
  7.  
  8. class HillClimbing {
  9. private:
  10.     vector<Container> containers;
  11.     double capacity;
  12.     Container *currentContainer;
  13.     vector<double> costFunction;
  14.     bool execute;
  15.     int yy = 0;
  16. public:
  17.     HillClimbing(vector<double> initialObj, double binCapasity) {
  18.         capacity = binCapasity;
  19.  
  20.         int index;
  21.         currentContainer = new Container(capacity);
  22.        /* while (initialObj.size() > 0) {
  23.             srand(time(0));
  24.             if (initialObj.size() > 1)
  25.  
  26.                 index = rand() % (initialObj.size() - 1);
  27.             else index = 0;
  28.             Item item(initialObj[index]);
  29.             if (currentContainer->IsEnoughSpace(item)) {
  30.                 currentContainer->PutElementInContainer(item);
  31.                 initialObj.erase(initialObj.begin() + index);
  32.             }
  33.             else {
  34.                 //save current container
  35.                 containers.push_back(*currentContainer);
  36.                 //add new container
  37.                 currentContainer = new Container(capacity);
  38.             }
  39.         }*/
  40.        
  41.         if (currentContainer->getWorkload() > 0) {
  42.             containers.push_back(*currentContainer);
  43.         }
  44.         std::sort(containers.begin(), containers.end());
  45.         showContainers("Init data");
  46.         setCostFunction(containers);
  47.         execute = true;
  48.         applyHillClimbing();
  49.         showContainers("Hill climbing");
  50.     }
  51.     void setCostFunction(vector<Container> cc) {
  52.         costFunction.clear();
  53.         std::sort(cc.begin(), cc.end());
  54.         for (int i = 0; i < cc.size(); i++) {
  55.             costFunction.push_back(cc[i].getAmountOfFreeSpace());
  56.         }
  57.     }
  58.     bool isNotEqual(double d1, double d2) {
  59.         if (abs(d1 - d2) > 0.1) {
  60.             return true;
  61.         }
  62.         else return false;
  63.     }
  64.     bool compareCostFunction(vector<Container> cc) {
  65.         std::sort(cc.begin(), cc.end());
  66.         bool b;
  67.         if (cc.size() < costFunction.size()) {
  68.             b = true;
  69.         }
  70.         else {
  71.             b = false;
  72.             for (int i = 0; i < containers.size(); i++) {
  73.               if (isNotEqual(costFunction[i], cc[i].getAmountOfFreeSpace())) {
  74.                   b = true;
  75.                   break;
  76.               }
  77.           }
  78.         }
  79.         setCostFunction(cc);
  80.         return b;
  81.     }
  82.     void showC(vector<Container> vv) {
  83.         std::sort(vv.begin(), vv.end());
  84.         for(int i = 0; i < vv.size(); i++) {
  85.             cout << vv[i].getAmountOfFreeSpace() << " ";
  86.         }
  87.         cout << endl;
  88.     }
  89.  
  90.     void Swap(int i, int j, Item item1, Item item2) {
  91.         Item temp = containers[j].extractItem(item2);
  92.         containers[j].PutElementInContainer(item1);
  93.  
  94.         containers[i].DeleteElementFromContainer(item1);
  95.         containers[i].PutElementInContainer(temp);
  96.     }
  97.     bool IsPossileToExchange(Container c1, Container c2, Item item1, Item item2) {
  98.         c1.DeleteElementFromContainer(item1);
  99.         c2.DeleteElementFromContainer(item2);
  100.  
  101.         if (c1.IsEnoughSpace(item2) && c2.IsEnoughSpace(item1)) {
  102.             return true;
  103.         }
  104.         else {
  105.             return false;
  106.         }
  107.     }
  108.     void swapDouble(double &a, double &b) {
  109.         double temp = a;
  110.         a = b;
  111.         b = temp;
  112.     }
  113.     bool IsImprovesCostFunction(Container c1, Container c2, Item item1, Item item2) {
  114.         //find init cost function
  115.         double costF1 = c1.getAmountOfFreeSpace();
  116.         double costF2 = c2.getAmountOfFreeSpace();
  117.         if (costF1 < costF2) swapDouble(costF1, costF2);
  118.         Item temp = c2.extractItem(item2);
  119.         c2.PutElementInContainer(item1);
  120.  
  121.         c1.DeleteElementFromContainer(item1);
  122.         c1.PutElementInContainer(temp);
  123.  
  124.         double costF3 = c1.getAmountOfFreeSpace();
  125.         double costF4 = c2.getAmountOfFreeSpace();
  126.  
  127.         if (costF3 < costF4) swapDouble(costF3, costF4);
  128.  
  129.         if (costF3 > costF1 && costF4 < costF2) {
  130.             return true;
  131.         }
  132.         else return false;
  133.     }
  134.     void applyHillClimbing() {
  135.         int k = 0;
  136.         while (execute) {
  137.             execute = false;
  138.           //  cout << "before cycle ////////////////////////////////////" << endl;
  139.             for (int i = 0; i < containers.size(); i++) {
  140.                 for (int j = 0; j < containers.size(); j++) {
  141.                     if (i != j && containers[i].getCountOfElements() > 0 && containers[j].getCountOfElements() > 0)
  142.                     {
  143.                         if(containers[i].getAmountOfFreeSpace() != 0 && containers[j].getAmountOfFreeSpace() != 0)
  144.                           SwapElements(i, j);
  145.                     }
  146.                 }
  147.             }
  148.             // cout << "after cycle +++++++++++++++++++++++++++++++++++++" << endl;
  149.             k++;
  150.             cout << "k = " << containers.size() << endl;
  151.             cout << "-------------------------------------------------------------" << endl;
  152.             //setCostFunction(containers);
  153.  
  154.     //if(compareCostFunction(containers) == false) {
  155.             if(k == 105) {
  156.                 setCostFunction(containers);
  157.                 std::sort(containers.begin(), containers.end());
  158.                 break;
  159.             }
  160.         }
  161.     }
  162.     void showCostFunction() {
  163.         for (int i = 0; i < costFunction.size(); i++) {
  164.             cout << costFunction[i] << " ";
  165.         }
  166.         cout << endl;
  167.     }
  168.     //move element from container i to j
  169.     //return true if container has been deleted
  170.     bool move(int i, int j, Item item) {
  171.         containers[j].PutElementInContainer(item);
  172.         containers[i].DeleteElementFromContainer(item);
  173.         if (containers[i].IsContainerEmpty()) {
  174.             containers.erase(containers.begin() + i);
  175.             return true;
  176.         }
  177.         return false;
  178.     }
  179.     void SwapElements(int i, int j) {
  180.  
  181.         for (int k = 0; k < containers[i].getCountOfElements(); k++) {
  182.                 Item item1(containers[i].getElementByID(k));
  183.                 if (containers[j].IsEnoughSpace(item1)) {
  184.                     if (move(i, j, item1) == true)  {
  185.                         execute = true;
  186.                         cout << "move element" << endl;
  187.                         return;
  188.                      }
  189.                 }
  190.                 else {
  191.                     if (containers[j].IsElementLighter(item1) == true) {
  192.                         Item item2(containers[j].findElement(item1));
  193.                         if (IsPossileToExchange(containers[i], containers[j], item1, item2)) {
  194.                             if (IsImprovesCostFunction(containers[i], containers[j], item1, item2)) {
  195.                                /* cout << "--------------------------" << endl;
  196.                                 cout << i << " " << j << endl;
  197.                                 containers[i].showObjects();
  198.                                 containers[j].showObjects();
  199.                                 cout << "item1 " << item1.getWeight() << " item2 " << item2.getWeight() << endl;
  200.                                 cout << "free in first " << containers[i].getAmountOfFreeSpace() << " free in second " << containers[j].getAmountOfFreeSpace();
  201.                                */
  202.                             Swap(i, j, item1, item2);
  203.                                 /* cout << "******************** " << endl;
  204.                                  containers[i].showObjects();
  205.                                  containers[j].showObjects();
  206.                                  cout << "free in first " << containers[i].getAmountOfFreeSpace() << " free in second " << containers[j].getAmountOfFreeSpace();
  207.                                  cout << "--------------------------swap finished" << endl;*/
  208.                                 showC(containers);
  209.                                   execute = true;
  210.                                   return;
  211.                             }
  212.                         }
  213.                     }
  214.                 }
  215.         }
  216.  
  217.     }
  218.     void showContainers(const char* str) {
  219.         cout << str << endl;
  220.         for (int i = 0; i < containers.size(); i++) {
  221.             cout << "container " << i + 1 << " Fullness: " << containers[i].getWorkload() << " free: " << containers[i].getAmountOfFreeSpace() << endl;
  222.             containers[i].showObjects();
  223.         }
  224.         cout << "Count of containers " << containers.size() << endl;
  225.     }
  226.     vector<double> getCostFunction() {
  227.             return costFunction;
  228.         }
  229.     ~HillClimbing() {
  230.         delete currentContainer;
  231.     }
  232. };
  233. #endif // HILLCLIMBING_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement