knapsack::knapsack(const knapsack &k) // Knapsack copy constructor { numObjects = k.getNumObjects(); costBound = k.getCostBound(); value.resize(numObjects); cost.resize(numObjects); index.resize(numObjects); selected.resize(numObjects); for (int i = 0; i < numObjects; i++) { setValue(i,k.getValue(i)); setCost(i,k.getCost(i)); setIndex(i,k.getIndex(i)); if (k.isSelected(i)) select(i); else unSelect(i); } setNum(k.getNum()); currentValue = k.getCurrentValue(); currentCost = k.getCurrentCost(); }
knapsack greedyKnapsack(vector<pair<double,int>> sortedSack, knapsack sack) { int sackIndex; // outer for loop is iterating through sortedSack ratios // look for highest ratio item for (int i = 0; i < sack.getNumObjects(); i++) { // use second value of sorted sack pair as index sackIndex = sortedSack.at(i).second; //get ratio for specific sack(sackIndex) double ratioKnap; double valKnap = sack.getValue(sackIndex); double costKnap = sack.getCost(sackIndex); ratioKnap = valKnap / costKnap; if (sack.getCost() + sack.getCost(sackIndex) <= sack.getCostLimit()) { sack.select(sackIndex); } } for (int k = 0; k < sack.getNumObjects(); k++) { cout<<sack.isSelected(k)<<endl; } return sack; }
void exhaustiveKnapsack(knapsack sack, int timeLimit) { // Get start time to be used in while loop time_t startTime; time(&startTime); // a while loop that expires when time limit is complete by checking difference of // start time and current time at every loop while (true) { // Get current time and stop when greater than input time limit time_t newTime; time(&newTime); if (difftime(newTime, startTime) > timeLimit) { cout << "Time limit expired"; break; } // Put main code here // Update current weight in sack every iteration of while loop int itemsSelected = 0; int currentWeight = 0; for (int i = 0; i < sack.getNumObjects(); i++) { itemsSelected++; currentWeight = sack.getCost(i) + currentWeight; } // Create random index to put into sack srand(time(NULL)); int randNum = rand() % sack.getNumObjects(); // if item isn't selected, and there is enough space, then select item if ((sack.isSelected(randNum) == false) && sack.getCost(randNum) + currentWeight <= sack.getCostLimit()) { sack.select(randNum); } // Need an exit case where there is no more items that can fit in the sack else if (sack.getCost(randNum) + currentWeight > sack.getCostLimit()) { // need to record sack weight and value and items when this happens (and there are // no more items that can legally be added) } } }
void printSolution(knapsack &k, string filename) // Prints out the solution. { ofstream myfile; myfile.open (filename.c_str()); myfile << "------------------------------------------------" << endl; myfile << "Total value: " << k.getCurrentValue() << endl; myfile << "Total cost: " << k.getCurrentCost() << endl << endl; // Print out objects in the solution for (int i = 0; i < k.getNumObjects(); i++) if (k.isSelected(i)) myfile << i << " " << k.getValue(i) << " " << k.getCost(i) << endl; myfile << endl; myfile.close(); }
/** * Finds a local optimum and returns the result * * @param k knapsack to find local optimum for * @param int 2-opt or 3-opt * @return new knapsack with a local opt solution */ knapsack localOptimum(knapsack &k, int opt) { knapsack original(k); deque<knapsack> problem; knapsack bestFound = knapsack(k); try { for (int l = 0; l < k.getNumObjects() - 1; l++) { k = knapsack(original); if (!k.isSelected(l)) continue; k.unSelect(l); for (int m = l; m < k.getNumObjects() - 1; m++) { if (!k.isSelected(m)) continue; k.unSelect(m); if (opt == 3) { for (int n = m; n < k.getNumObjects() - 1; n++) { if (!k.isSelected(n)) continue; k.unSelect(n); problem.push_front(knapsack(k)); } } else problem.push_front(knapsack(k)); } } while(problem.size() > 0) { checkTimeLimit(); knapsack current = problem.front(); problem.pop_front(); deque<knapsack> solution; solution.push_front(knapsack(current)); int currentIndex = 0; while(solution.size() > 0) { knapsack currentSolution = solution.front(); solution.pop_front(); if (currentSolution.getValue() > bestFound.getValue()) { bestFound = knapsack(currentSolution); } if (currentIndex < currentSolution.getNumObjects() - 1) { if (!currentSolution.isSelected(currentIndex) && (currentSolution.getCost() + currentSolution.getCost(currentIndex)) < currentSolution.getCostLimit()) { currentSolution.select(currentIndex); solution.push_front(knapsack(currentSolution)); } currentIndex++; } } } return bestFound; } catch (baseException &ex) { cout << ex.what() << endl; return bestFound; } } //end localOptimum