/** * Calculates the value/cost ratio for each object * And stores the ratios in a vector * TODO sort values to improve runtime * @param k the knapsack to use */ void calculateValueCostRatios(knapsack &k) { for (int i = 0; i < k.getNumObjects(); i++) { valueCostRatio.insert(valueCostRatio.begin() + i, (k.getValue(i) / (double)k.getCost(i))); } }
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; }
double greedyKnapsack(knapsack &k){ MaxHeap<float, int>* heap = new MaxHeap<float, int>(&compare); for(int counter = 0; counter < k.getNumObjects(); counter++){ double key = (double) k.getValue(counter) / k.getCost(counter); heap->add(key, counter); } while(! heap->empty()){ int next = heap->extractMaxHeapMaximum(); if(k.getCost(next) + k.getCurrentCost() <= k.getCostBound()){ k.select(next); } } return k.getCurrentValue(); }
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(); }
// sort all items in order of value/weight ratio // and store the ratios in the index at which they exist in knapsack vector<pair<double, int>> sortItems(knapsack sack) { vector<pair<double, int>> ratios; for (int i = 0; i < sack.getNumObjects(); i++) { //cout << "sack.getValue(" << i << ")" << " = " << sack.getValue(i) << endl; //cout << "sack.getValue(" << i << ")" << " = " << sack.getCost(i) << endl; double ratio; double val = sack.getValue(i); double cost = sack.getCost(i); ratio = val / cost; //cout << "Ratio: " << ratio; //cout << "Pushing " << ratio << " to index " << i << endl; pair <double, int> ratInd; // ratio + index ratInd.first = ratio; ratInd.second = i; ratios.push_back(ratInd); //cout << endl << endl << endl; } // this preserves the original ratios vector just in case (may remove it later) vector<pair<double,int>> sortedRatios = ratios; sort(sortedRatios.begin(), sortedRatios.end()); reverse(sortedRatios.begin(), sortedRatios.end()); /* for (int i = 0; i < ratios.size(); i++) { //cout << "Ratio: " << sortedRatios.at(i) << " at index " << i << endl; //cout << "" << sortedRatios.at(i) << endl; }*/ cin.get(); return sortedRatios; }