-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractionalKnapsackProblem.cpp
More file actions
189 lines (181 loc) · 3.89 KB
/
FractionalKnapsackProblem.cpp
File metadata and controls
189 lines (181 loc) · 3.89 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//40843245 Jay
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <string>
using namespace std;
typedef struct pair
{
int index;
double value;
}PAIR;
class knapsack_bag
{
private:
int max_load;
int *price;
int *weight;
int n;
PAIR *sorted_ratio;
double *selected_load;
double curr_load;
double max_profit;
public:
knapsack_bag(int n)
{
this->n=n;
price=new int[this->n];
weight=new int[this->n];
}
void setPrice(int *arr,int n)
{
int i=0;
for(i=0;i<n;i++)
{
this->price[i]=arr[i];
}
}
void setWeight(int *arr,int n)
{
int i=0;
for(i=0;i<n;i++)
{
this->weight[i]=arr[i];
}
}
void setMaxLoad(int max_load)
{
this->max_load=max_load;
}
void display_sorted_ratio()
{
for(int i=0;i<this->n;i++)
{
cout<<"("<<this->sorted_ratio[i].index<<","<<this->sorted_ratio[i].value<<")"<<endl;
}
cout<<endl;
}
void display_ratio(double *arr,int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
cout<<endl;
}
void display_PriceWeight()
{
cout<<"price and weight is:(price,weight)."<<endl;
for(int i=0;i<this->n;i++)
{
cout<<"("<<this->price[i]<<","<<this->weight[i]<<")"<<endl;
}
}
void display_maxProfit()
{
cout<<"my max profit is "<<this->max_profit<<endl;
for(int i=0;i<this->n;i++)
{
cout<<"selected_load["<<i<<"]="<<this->selected_load[i]<<endl;
}
}
public:
//sort the CP where CP[i]=price[i]/weight[i] with insertion sort.
void sort_ratio(double *arr)
{
sorted_ratio=new PAIR[this->n];
for(int i=0;i<this->n;i++)
{
sorted_ratio[i].index=i;
sorted_ratio[i].value=arr[i];
}
sorted_ratio[0].value=arr[0];
sorted_ratio[0].index=0;
for(int i=1;i<this->n;i++)
{
for(int j=this->n-1;j>=i;j--)
{
if(sorted_ratio[j].value<sorted_ratio[j-1].value)
{
int tempI=sorted_ratio[j].index;
sorted_ratio[j].index=sorted_ratio[j-1].index;
sorted_ratio[j-1].index=tempI;
double temp=sorted_ratio[j].value;
sorted_ratio[j].value=sorted_ratio[j-1].value;
sorted_ratio[j-1].value=temp;
}
}
}
}
public:
//major function
void fractional_knapsack()
{
bool *selected=new bool[this->n];
for(int i=0;i<this->n;i++)
{
selected[i]=false;
}
double *ratio=new double[this->n];
for(int i=0;i<this->n;i++)
{
ratio[i]=double(this->price[i])/this->weight[i];
}
selected_load=new double[this->n];
for(int i=0;i<this->n;i++)
{
selected_load[i]=0;
}
sort_ratio(ratio);
this->curr_load=0;
this->max_profit=0;
int curr_idx=this->n-1;
while(curr_idx>=0 && this->curr_load<this->max_load-0.0000001)
{
int tempI=sorted_ratio[curr_idx].index;
//Cases that can all fetch
if(this->weight[tempI]<=(this->max_load-this->curr_load))
{
this->curr_load+=this->weight[tempI];
this->max_profit+=this->price[tempI];
this->selected_load[tempI]=this->weight[tempI];
}
//Cases that can NOT all fetch
else
{
double val=(this->max_load-this->curr_load)*ratio[tempI];
this->max_profit+=val;
this->selected_load[tempI]=(this->max_load-this->curr_load);
this->curr_load=this->max_load;
}
curr_idx--;
}
this->display_maxProfit();
}
};
int main()
{
int numOfItems=0;
int max_load=0;
int tempPrice=0,tempWeight=0;
int *price,*weight;
cin>>numOfItems;
cin.get(); //discard a new line char.
cin>>max_load;
cin.get();
price=new int[numOfItems];
weight=new int[numOfItems];
for(int i=0;i<numOfItems;i++)
{
cin>>tempPrice>>tempWeight;
price[i]=tempPrice;
weight[i]=tempWeight;
}
knapsack_bag *my_bag=new knapsack_bag(numOfItems);
my_bag->setPrice(price,numOfItems);
my_bag->setWeight(weight,numOfItems);
my_bag->display_PriceWeight();
my_bag->setMaxLoad(max_load);
my_bag->fractional_knapsack();
return 0;
}