-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapsack.cpp
134 lines (82 loc) · 3.58 KB
/
Knapsack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* File: Knapsack.cpp
* Author: mnouh
*
* Created on March 11, 2012, 6:05 PM
*/
#include "Knapsack.h"
Knapsack::Knapsack() {
}
Knapsack::Knapsack(vector< pair<int, int> > problems, int &knapsack_weight, int &numItems, int &problem_Num, int improve, ofstream & output)
{
this->MAX_WEIGHT = knapsack_weight;
this->num_items = numItems;
this->problem_Num = problem_Num;
this->items = &problems;
this->MAX_VALUE = improve;
//cout << "Improved from Greedy: " << improve << endl;
//cout << "Problem #: " << problem_Num << " Number of Items: " << numItems << " Maximum Amount of Weight: " << this->MAX_WEIGHT << endl;
//cout << "THE SORTED ITEMS" << endl;
/*
for(std::vector< pair<int, int> >::iterator it = this->items->begin(); it != this->items->end(); ++it) {
// cout << "First Item: " << it->first << endl;
cout << "Item " << it->second << endl;
}
**/
//cout << "STARTING TO CALL SOLUTION" << endl;
vector< pair<int, int> > empty;
this->instance_list = new vector< vector < pair <int, int> > * >;
this->sack(0, 0, 0, empty);
/*for(std::vector< pair<int, int> >::iterator it = this->solution->begin(); it != this->solution->end(); ++it) {
cout << "Weight: " << it->first << endl;
cout << "Item Profit " << it->second << endl;
}
**/
//cout << "MAX PROFIT: " << this->MAX_VALUE << endl;
output << num_items << " " << this->MAX_VALUE << " ";
}
void Knapsack::sack(int index, double currentWeight, double currentValue, vector< pair<int, int> > & copiedVector)
{
if(currentWeight <= this->MAX_WEIGHT && currentValue > this->MAX_VALUE) {
this->MAX_VALUE = currentValue;
//cout << "MAX profit" << this->MAX_VALUE << endl;
//this->solution = new vector< pair<int, int> > (copiedVector);
}
if(promising(index, currentWeight, currentValue))
{
vector< pair<int, int> > * leftNodes = new vector< pair<int, int> > (copiedVector);
pair <int, int> an_item;
an_item = make_pair(this->items->at(index+1).first, this->items->at(index+1).second);
leftNodes->push_back(an_item);
this->instance_list->push_back(leftNodes);
sack(index+1, currentWeight+this->items->at(index+1).first, currentValue+this->items->at(index+1).second, *leftNodes);
vector< pair<int, int> > * rightNodes = new vector< pair<int, int> > (copiedVector);
this->instance_list->push_back(rightNodes);
sack(index+1, currentWeight, currentValue, *rightNodes);
}
}
bool Knapsack::promising(int i, double weight, double value)
{
double bound = value;
double size = weight;
int index = i+1;
if(weight >= this->MAX_WEIGHT)
return false;
while(index < this->items->size() && (size + this->items->at(index).first) <= this->MAX_WEIGHT) {
size += this->items->at(index).first;
bound += this->items->at(index).second;
index++;
}
if(index < this->items->size()) {
bound += (this->MAX_WEIGHT - size) * (this->items->at(index).second/this->items->at(index).first);
}
//cout << "Index:" << i << " Bound: " << bound << endl;
return bound > this->MAX_VALUE;
}
Knapsack::~Knapsack()
{
for(std::vector< vector < pair <int, int> > * >::iterator it = this->instance_list->begin(); it != this->instance_list->end(); ++it) {
delete * it;
}
delete this->instance_list;
}