Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef HILLCLIMBING_H
- #define HILLCLIMBING_H
- #include <ctime>
- #include "container.h"
- #include "buildfunction.h"
- #include <QDebug>
- class HillClimbing {
- private:
- vector<Container> containers;
- double capacity;
- Container *currentContainer;
- vector<double> costFunction;
- bool execute;
- int yy = 0;
- public:
- HillClimbing(vector<double> initialObj, double binCapasity) {
- capacity = binCapasity;
- int index;
- currentContainer = new Container(capacity);
- while (initialObj.size() > 0) {
- srand(time(0));
- if (initialObj.size() > 1)
- index = rand() % (initialObj.size() - 1);
- else index = 0;
- Item item(initialObj[index]);
- if (currentContainer->IsEnoughSpace(item)) {
- currentContainer->PutElementInContainer(item);
- initialObj.erase(initialObj.begin() + index);
- }
- else {
- //save current container
- containers.push_back(*currentContainer);
- //add new container
- currentContainer = new Container(capacity);
- }
- }
- /* for(int i = 0; i < initialObj.size(); i++) {
- Item item(initialObj[i]);
- if(currentContainer->IsEnoughSpace(item)) {
- currentContainer->PutElementInContainer(item);
- }
- else {
- containers.push_back(*currentContainer);
- currentContainer = new Container(capacity);
- currentContainer->PutElementInContainer(item);
- }
- }*/
- if (currentContainer->getWorkload() > 0) {
- containers.push_back(*currentContainer);
- }
- std::sort(containers.begin(), containers.end());
- showContainers("Init data");
- // changeVector(containers[0]);
- setCostFunction(containers);
- execute = true;
- applyHillClimbing();
- showContainers("Hill climbing");
- }
- void changeVector(Container c) {
- cout << "c " << c.getAmountOfFreeSpace() << endl;
- Item item(c.getElementByID(0));
- c.DeleteElementFromContainer(item);
- cout << "c2 " << c.getAmountOfFreeSpace() << endl;
- }
- void setCostFunction(vector<Container> cc) {
- costFunction.clear();
- std::sort(cc.begin(), cc.end());
- for (int i = 0; i < cc.size(); i++) {
- costFunction.push_back(cc[i].getAmountOfFreeSpace());
- }
- }
- bool isNotEqual(double d1, double d2) {
- if (abs(d1 - d2) > 0.1) {
- return true;
- }
- else return false;
- }
- bool compareCostFunction(vector<Container> cc) {
- std::sort(cc.begin(), cc.end());
- bool b;
- if (cc.size() < costFunction.size()) {
- b = true;
- }
- else {
- b = false;
- for (int i = 0; i < containers.size(); i++) {
- if (isNotEqual(costFunction[i], cc[i].getAmountOfFreeSpace())) {
- b = true;
- break;
- }
- }
- }
- setCostFunction(cc);
- return b;
- }
- void showC(vector<Container> vv) {
- std::sort(vv.begin(), vv.end());
- for(int i = 0; i < vv.size(); i++) {
- cout << vv[i].getAmountOfFreeSpace() << " ";
- }
- cout << endl;
- }
- void Swap(int i, int j, Item item1, Item item2) {
- Item temp = containers[j].extractItem(item2);
- containers[j].PutElementInContainer(item1);
- containers[i].DeleteElementFromContainer(item1);
- containers[i].PutElementInContainer(temp);
- }
- bool IsPossileToExchange( Container c1, Container c2, const Item item1,const Item item2) {
- c1.DeleteElementFromContainer(item1);
- c2.DeleteElementFromContainer(item2);
- if (c1.IsEnoughSpace(item2) && c2.IsEnoughSpace(item1)) {
- return true;
- }
- else {
- return false;
- }
- }
- void swapDouble(double &a, double &b) {
- double temp = a;
- a = b;
- b = temp;
- }
- bool IsImprovesCostFunction(Container c1, Container c2, const Item item1, const Item item2) {
- //find init cost function
- double costF1 = c1.getAmountOfFreeSpace();
- double costF2 = c2.getAmountOfFreeSpace();
- if (costF1 < costF2) swapDouble(costF1, costF2);
- Item temp = c2.extractItem(item2);
- cout << "temp in IsImprovesCostFunction " << temp.getWeight() << endl;
- c2.PutElementInContainer(item1);
- c1.DeleteElementFromContainer(item1);
- c1.PutElementInContainer(temp);
- double costF3 = c1.getAmountOfFreeSpace();
- double costF4 = c2.getAmountOfFreeSpace();
- if (costF3 < costF4) swapDouble(costF3, costF4);
- if (costF3 > costF1 && costF4 < costF2) {
- return true;
- }
- else return false;
- }
- void applyHillClimbing() {
- int k = 0;
- while (execute) {
- execute = false;
- cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;
- for (int i = 0; i < containers.size(); i++) {
- for (int j = 0; j < containers.size(); j++) {
- if (i != j && containers[i].getCountOfElements() > 0 && containers[j].getCountOfElements() > 0)
- {
- if(containers[i].getAmountOfFreeSpace() != 0 && containers[j].getAmountOfFreeSpace() != 0)
- SwapElements(i, j);
- }
- }
- }
- cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
- k++;
- //if(compareCostFunction(containers) == false) {
- if(execute == false) {
- cout << "execute = false" << endl;
- std::sort(containers.begin(), containers.end());
- setCostFunction(containers);
- break;
- }
- /* if(k == 10) {
- cout << "k = 1005" << endl;
- std::sort(containers.begin(), containers.end());
- setCostFunction(containers);
- break;
- }*/
- }
- }
- void showCostFunction() {
- for (int i = 0; i < costFunction.size(); i++) {
- cout << costFunction[i] << " ";
- }
- cout << endl;
- }
- //move element from container i to j
- //return true if container has been deleted
- void move(int i, int j, Item item) {
- cout << "in function move " << item.getWeight() << endl;
- containers[j].PutElementInContainer(item);
- containers[i].DeleteElementFromContainer(item);
- if (containers[i].IsContainerEmpty()) {
- containers.erase(containers.begin() + i);
- }
- }
- bool IsMoveImprovesCostFunction(Container c1, Container c2, Item item) {
- double costF1 = c1.getAmountOfFreeSpace();
- double costF2 = c2.getAmountOfFreeSpace();
- if(costF1 < costF2) swapDouble(costF1, costF2);
- c1.DeleteElementFromContainer(item);
- c2.PutElementInContainer(item);
- double costF3 = c1.getAmountOfFreeSpace();
- double costF4 = c2.getAmountOfFreeSpace();
- if(costF3 < costF4) swapDouble(costF3, costF4);
- if ((costF3 > costF1) && (costF4 < costF2)) {
- return true;
- }
- else return false;
- }
- void SwapElements(int i, int j) {
- for (int k = 0; k < containers[i].getCountOfElements(); k++) {
- Item item1(containers[i].getElementByID(k));
- cout << "item1 = " << item1.getWeight() << endl;
- if (containers[j].IsEnoughSpace(item1)) {
- if(IsMoveImprovesCostFunction(containers[i], containers[j], item1) == true) {
- move(i, j, item1);
- execute = true;
- showContainers("debug");
- return;
- }
- }
- else {
- if (containers[j].IsElementLighter(item1) == true) {
- Item item2(containers[j].findElement(item1));
- cout << "before IspossibleToExchange " << containers[i].getAmountOfFreeSpace() << " " << containers[j].getAmountOfFreeSpace() << endl;
- containers[i].showObjects();
- containers[j].showObjects();
- if (IsPossileToExchange(containers[i], containers[j], item1, item2)) {
- cout << "before IsImprovesCostFunction " << containers[i].getAmountOfFreeSpace() << " " << containers[j].getAmountOfFreeSpace() << endl;
- cout << "i = " << i << ": "; containers[i].showObjects();
- cout << "j = " << j << ": "; containers[j].showObjects();
- if (IsImprovesCostFunction(containers[i], containers[j], item1, item2)) {
- Swap(i, j, item1, item2);
- cout << "after IsImprovesCostFunction " << containers[i].getAmountOfFreeSpace() << " " << containers[j].getAmountOfFreeSpace() << endl;
- cout << "i = " << i << ": "; containers[i].showObjects();
- cout << "j = " << j << ": "; containers[j].showObjects();
- execute = true;
- return;
- }
- }
- }
- }
- }
- }
- void showContainers(const char* str) {
- cout << str << endl;
- for (int i = 0; i < containers.size(); i++) {
- cout << "container " << i + 1 << " Fullness: " << containers[i].getWorkload() << " free: " << containers[i].getAmountOfFreeSpace() << endl;
- containers[i].showObjects();
- }
- cout << "Count of containers " << containers.size() << endl;
- // sh();
- }
- void sh(){
- cout << "***************** " << endl;
- for (int i = 0; i < containers.size(); i++) {
- containers[i].showObjects();
- }
- }
- vector<double> getCostFunction() {
- return costFunction;
- }
- ~HillClimbing() {
- delete currentContainer;
- }
- };
- #endif // HILLCLIMBING_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement