-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
216 lines (188 loc) · 5.42 KB
/
config.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include<bits/stdc++.h>
#include<fstream>
#include<sstream>
#define A 34
#define B 12
#define P 33
#define H 12
#define L 24
#define W_ 34
#define S_ 10
#define INPUT_FILE "./parts.txt"
#define OUTPUT_FILE "./possible_configurations.txt"
using namespace std;
struct node{
int value{0};
vector<int> items;
};
int diff(int a = A, int b = B, int p = P, int h = H, int l = L)
{
int v1 = abs(a-b);
int v2 = abs(a-p);
int v3 = abs(a-h);
int v4 = abs(a-l);
int v5 = abs(b-p);
int v6 = abs(b-h);
int v7 = abs(b-l);
int v8 = abs(p-h);
int v9 = abs(p-l);
int v10 = abs(h-l);
return v1+v2+v3+v4+v5+v6+v7+v8+v9+v10;
}
int calculate_value(int index,vector<int>& aero, vector<int> & brakes, vector<int> & power, vector<int> & handling, vector<int> & lightweight, int a = A, int b = B, int p = P, int h = H, int l = L)
{
return a*aero[index]+b*brakes[index]+p*power[index]+h*handling[index]+l*lightweight[index]-diff();
}
node knapsack(int W, int S, int index, vector<int> & weight, vector<int> & value, vector<int> & aero, vector<int> & brakes, vector<int> & power, vector<int> & handling, vector<int> & lightweight, vector<vector<vector<int> > > & dp, vector<vector<vector<vector<int> > > > & items)
{
if(index == weight.size())
{
node ans;
return ans;
}
if(W<=0)
{
node ans;
return ans;
}
if(S<=0)
{
node ans;
return ans;
}
if(dp[W][S][index]!=-1)
{
node ans;
ans.value = dp[W][S][index];
ans.items = items[W][S][index];
return ans;
}
int val1 = 0;
int val2 = 0;
node ans1 = knapsack(W-weight[index],S-1,index+1, weight, value, aero, brakes, power, handling, lightweight, dp, items);
node ans2 = knapsack(W, S, index+1, weight, value,aero, brakes, power, handling, lightweight, dp, items);
if(W-weight[index]>=0 && S-1>=0)
{
val1 = calculate_value(index,aero,brakes,power,handling, lightweight) + ans1.value;
}
val2 = ans2.value;
dp[W][S][index] = max(val1, val2);
if(val1>val2)
{
items[W][S][index].push_back(index);
for(int i=0;i<ans1.items.size();i++)items[W][S][index].push_back(ans1.items[i]);
}
else
{
for(int i=0;i<ans2.items.size();i++)items[W][S][index].push_back(ans2.items[i]);
}
node ans;
ans.value = dp[W][S][index];
ans.items = items[W][S][index];
return ans;
}
int main()
{
//TODO: Optimise by removing memoization in favour of dp
//TODO: Optimise for space in case of item printing
//TODO: Work on the balancing factor and the weighting mechanism
int W = W_;
int S = S_;
vector<int> weight;
vector<int> value;
vector<int> aero;
vector<int> brakes;
vector<int> power;
vector<int> handling;
vector<int> lightweight;
vector<string> names;
ifstream inputfile;
inputfile.open(INPUT_FILE);
if(!inputfile)
{
cout<<"Input file not available, please check for availability"<<endl;
return -1;
}
else
{
string line;
while(getline(inputfile, line))
{
string name_of_part;
int total_value;
int aero_;
int brakes_;
int power_;
int handling_;
int lightweight_;
int weight_of_part;
istringstream mystream(line);
while(mystream>>name_of_part>>total_value>>aero_>>brakes_>>power_>>handling_>>lightweight_>>weight_of_part)
{
names.push_back(name_of_part);
value.push_back(total_value);
weight.push_back(weight_of_part);
aero.push_back(aero_);
brakes.push_back(brakes_);
power.push_back(power_);
handling.push_back(handling_);
lightweight.push_back(lightweight_);
}
}
}
inputfile.close();
vector<vector<vector<int> > > dp(W+1, vector<vector<int> > (S+1,vector<int> (value.size()+1,-1)));
vector<vector<vector<vector<int> > > > items(W+1, vector<vector<vector<int> > > (S+1, vector<vector<int> > (value.size()+1,{})));
node ans = knapsack(W, S, 0, weight, value,aero, brakes, power, handling, lightweight, dp,items);
int final_value = 0;
int final_aero = 0;
int final_brakes = 0;
int final_power = 0;
int final_handling = 0;
int final_lightweight = 0;
for(int i=0;i<ans.items.size();i++)
{
cout<<names[ans.items[i]]<<" ";
cout<<value[ans.items[i]]<<" ";
cout<<aero[ans.items[i]]<<" ";
cout<<brakes[ans.items[i]]<<" ";
cout<<power[ans.items[i]]<<" ";
cout<<handling[ans.items[i]]<<" ";
cout<<lightweight[ans.items[i]]<<endl;
final_value+=value[ans.items[i]];
final_aero+=aero[ans.items[i]];
final_brakes+=brakes[ans.items[i]];
final_power+=power[ans.items[i]];
final_handling+=handling[ans.items[i]];
final_lightweight+=lightweight[ans.items[i]];
}
cout<<endl;
cout<<"Total:"<<final_value<<endl;
cout<<"Aero:"<<final_aero<<endl;
cout<<"Brakes:"<<final_brakes<<endl;
cout<<"Power:"<<final_power<<endl;
cout<<"Handling:"<<final_handling<<endl;
cout<<"Lightweight:"<<final_lightweight<<endl;
ofstream outputfile;
outputfile.open(OUTPUT_FILE, ios::app);
if(!outputfile.is_open())
{
cout<<"Output file could not be successfully opened, please check for possible issues:"<<endl;
return -1;
}
outputfile<<"W:"<<W_<<" S:"<<S_<<" A:"<<A<<" B:"<<B<<" P:"<<P<<" H:"<<H<<" L:"<<L<<endl;
outputfile<<"Total:"<<final_value<<endl;
outputfile<<"Aero:"<<final_aero<<endl;
outputfile<<"Brakes:"<<final_brakes<<endl;
outputfile<<"Power:"<<final_power<<endl;
outputfile<<"Handling:"<<final_handling<<endl;
outputfile<<"Lightweight:"<<final_lightweight<<endl;
outputfile<<"Parts to include : ";
for(int i=0;i<ans.items.size();i++)
{
outputfile<<names[ans.items[i]]<<" ";
}
outputfile<<endl;
outputfile<<"------------------------------------------------------------------------"<<endl;
outputfile.close();
}