Advertisement
Guest User

gfhgfhgfh

a guest
Jan 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.14 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.       /* for(int i = 0; i < initialObj.size(); i++) {
  41.            Item item(initialObj[i]);
  42.            if(currentContainer->IsEnoughSpace(item)) {
  43.                currentContainer->PutElementInContainer(item);
  44.            }
  45.             else {
  46.  
  47.                 containers.push_back(*currentContainer);
  48.                 currentContainer = new Container(capacity);
  49.                   currentContainer->PutElementInContainer(item);
  50.             }
  51.  
  52.        }*/
  53.        if (currentContainer->getWorkload() > 0) {
  54.            containers.push_back(*currentContainer);
  55.        }
  56.        std::sort(containers.begin(), containers.end());
  57.        showContainers("Init data");
  58.       // changeVector(containers[0]);
  59.        setCostFunction(containers);
  60.        execute = true;
  61.        applyHillClimbing();
  62.        showContainers("Hill climbing");
  63.    }
  64.    void changeVector(Container c) {
  65.        cout << "c " << c.getAmountOfFreeSpace() << endl;
  66.        Item item(c.getElementByID(0));
  67.        c.DeleteElementFromContainer(item);
  68.           cout << "c2 " << c.getAmountOfFreeSpace() << endl;
  69.    }
  70.  
  71.    void setCostFunction(vector<Container> cc) {
  72.        costFunction.clear();
  73.        std::sort(cc.begin(), cc.end());
  74.        for (int i = 0; i < cc.size(); i++) {
  75.            costFunction.push_back(cc[i].getAmountOfFreeSpace());
  76.        }
  77.    }
  78.    bool isNotEqual(double d1, double d2) {
  79.        if (abs(d1 - d2) > 0.1) {
  80.            return true;
  81.        }
  82.        else return false;
  83.    }
  84.    bool compareCostFunction(vector<Container> cc) {
  85.        std::sort(cc.begin(), cc.end());
  86.        bool b;
  87.        if (cc.size() < costFunction.size()) {
  88.            b = true;
  89.        }
  90.        else {
  91.            b = false;
  92.            for (int i = 0; i < containers.size(); i++) {
  93.              if (isNotEqual(costFunction[i], cc[i].getAmountOfFreeSpace())) {
  94.                  b = true;
  95.                  break;
  96.              }
  97.          }
  98.        }
  99.        setCostFunction(cc);
  100.        return b;
  101.    }
  102.    void showC(vector<Container> vv) {
  103.        std::sort(vv.begin(), vv.end());
  104.        for(int i = 0; i < vv.size(); i++) {
  105.            cout << vv[i].getAmountOfFreeSpace() << " ";
  106.        }
  107.        cout << endl;
  108.    }
  109.  
  110.    void Swap(int i, int j, Item item1, Item item2) {
  111.        Item temp = containers[j].extractItem(item2);
  112.        containers[j].PutElementInContainer(item1);
  113.  
  114.        containers[i].DeleteElementFromContainer(item1);
  115.        containers[i].PutElementInContainer(temp);
  116.    }
  117.    bool IsPossileToExchange( Container c1,  Container c2, const Item item1,const Item item2) {
  118.        c1.DeleteElementFromContainer(item1);
  119.        c2.DeleteElementFromContainer(item2);
  120.  
  121.        if (c1.IsEnoughSpace(item2) && c2.IsEnoughSpace(item1)) {
  122.            return true;
  123.        }
  124.        else {
  125.            return false;
  126.        }
  127.    }
  128.    void swapDouble(double &a, double &b) {
  129.        double temp = a;
  130.        a = b;
  131.        b = temp;
  132.    }
  133.    bool IsImprovesCostFunction(Container c1, Container c2, const Item item1, const Item item2) {
  134.        //find init cost function
  135.        double costF1 = c1.getAmountOfFreeSpace();
  136.        double costF2 = c2.getAmountOfFreeSpace();
  137.        if (costF1 < costF2) swapDouble(costF1, costF2);
  138.        Item temp = c2.extractItem(item2);
  139.        cout << "temp in IsImprovesCostFunction " << temp.getWeight() << endl;
  140.        c2.PutElementInContainer(item1);
  141.  
  142.        c1.DeleteElementFromContainer(item1);
  143.        c1.PutElementInContainer(temp);
  144.  
  145.        double costF3 = c1.getAmountOfFreeSpace();
  146.        double costF4 = c2.getAmountOfFreeSpace();
  147.  
  148.        if (costF3 < costF4) swapDouble(costF3, costF4);
  149.  
  150.        if (costF3 > costF1 && costF4 < costF2) {
  151.            return true;
  152.        }
  153.        else return false;
  154.    }
  155.    void applyHillClimbing() {
  156.        int k = 0;
  157.        while (execute) {
  158.            execute = false;
  159.            cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;
  160.            for (int i = 0; i < containers.size(); i++) {
  161.                for (int j = 0; j < containers.size(); j++) {
  162.                    if (i != j && containers[i].getCountOfElements() > 0 && containers[j].getCountOfElements() > 0)
  163.                    {
  164.                        if(containers[i].getAmountOfFreeSpace() != 0 && containers[j].getAmountOfFreeSpace() != 0)
  165.                          SwapElements(i, j);
  166.                    }
  167.                }
  168.            }
  169.            cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
  170.            k++;
  171.    //if(compareCostFunction(containers) == false) {
  172.            if(execute == false) {
  173.                cout << "execute = false" << endl;
  174.                std::sort(containers.begin(), containers.end());
  175.                setCostFunction(containers);
  176.  
  177.                break;
  178.            }
  179.           /* if(k == 10) {
  180.                cout << "k  = 1005" << endl;
  181.               std::sort(containers.begin(), containers.end());
  182.                setCostFunction(containers);
  183.  
  184.                break;
  185.            }*/
  186.        }
  187.    }
  188.    void showCostFunction() {
  189.        for (int i = 0; i < costFunction.size(); i++) {
  190.            cout << costFunction[i] << " ";
  191.        }
  192.        cout << endl;
  193.    }
  194.    //move element from container i to j
  195.    //return true if container has been deleted
  196.    void move(int i, int j, Item item) {
  197.        cout << "in function move " << item.getWeight() << endl;
  198.        containers[j].PutElementInContainer(item);
  199.        containers[i].DeleteElementFromContainer(item);
  200.        if (containers[i].IsContainerEmpty()) {
  201.            containers.erase(containers.begin() + i);
  202.        }
  203.    }
  204.    bool IsMoveImprovesCostFunction(Container c1, Container c2, Item item) {
  205.        double costF1 = c1.getAmountOfFreeSpace();
  206.        double costF2 = c2.getAmountOfFreeSpace();
  207.  
  208.        if(costF1 < costF2) swapDouble(costF1, costF2);
  209.  
  210.        c1.DeleteElementFromContainer(item);
  211.        c2.PutElementInContainer(item);
  212.  
  213.        double costF3 = c1.getAmountOfFreeSpace();
  214.        double costF4 = c2.getAmountOfFreeSpace();
  215.  
  216.        if(costF3 < costF4) swapDouble(costF3, costF4);
  217.  
  218.        if ((costF3 > costF1) && (costF4 < costF2)) {
  219.            return true;
  220.        }
  221.        else return false;
  222.    }
  223.  
  224.    void SwapElements(int i, int j) {
  225.  
  226.        for (int k = 0; k < containers[i].getCountOfElements(); k++) {
  227.                Item item1(containers[i].getElementByID(k));
  228.                cout << "item1 = " << item1.getWeight() << endl;
  229.                if (containers[j].IsEnoughSpace(item1)) {
  230.                    if(IsMoveImprovesCostFunction(containers[i], containers[j], item1) == true) {
  231.                            move(i, j, item1);
  232.                           execute = true;
  233.                           showContainers("debug");
  234.                           return;
  235.                      }
  236.                }
  237.                else {
  238.                    if (containers[j].IsElementLighter(item1) == true) {
  239.                        Item item2(containers[j].findElement(item1));
  240.                        cout << "before IspossibleToExchange " << containers[i].getAmountOfFreeSpace() << " " << containers[j].getAmountOfFreeSpace() << endl;
  241.                         containers[i].showObjects();
  242.                         containers[j].showObjects();
  243.                        if (IsPossileToExchange(containers[i], containers[j], item1, item2)) {
  244.                            cout << "before IsImprovesCostFunction " << containers[i].getAmountOfFreeSpace() << " " << containers[j].getAmountOfFreeSpace() << endl;
  245.                            cout << "i = " << i << ": "; containers[i].showObjects();
  246.                            cout << "j = " << j << ": "; containers[j].showObjects();
  247.                            if (IsImprovesCostFunction(containers[i], containers[j], item1, item2)) {
  248.  
  249.                                Swap(i, j, item1, item2);
  250.                                cout << "after IsImprovesCostFunction " << containers[i].getAmountOfFreeSpace() << " " << containers[j].getAmountOfFreeSpace() << endl;
  251.                                cout << "i = " << i << ": "; containers[i].showObjects();
  252.                                cout << "j = " << j << ": "; containers[j].showObjects();
  253.                                  execute = true;
  254.                                  return;
  255.                            }
  256.                        }
  257.                    }
  258.                }
  259.        }
  260.    }
  261.    void showContainers(const char* str) {
  262.        cout << str << endl;
  263.        for (int i = 0; i < containers.size(); i++) {
  264.            cout << "container " << i + 1 << " Fullness: " << containers[i].getWorkload() << " free: " << containers[i].getAmountOfFreeSpace() << endl;
  265.            containers[i].showObjects();
  266.        }
  267.        cout << "Count of containers " << containers.size() << endl;
  268.       // sh();
  269.    }
  270.    void sh(){
  271.        cout << "***************** " << endl;
  272.        for (int i = 0; i < containers.size(); i++) {
  273.            containers[i].showObjects();
  274.        }
  275.    }
  276.  
  277.    vector<double> getCostFunction() {
  278.            return costFunction;
  279.        }
  280.    ~HillClimbing() {
  281.        delete currentContainer;
  282.    }
  283. };
  284. #endif // HILLCLIMBING_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement