-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmr_e_h.cpp
executable file
·271 lines (238 loc) · 9.14 KB
/
pmr_e_h.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*******************************************************************************
* MC658 - Projeto e Análise de Algoritmos III - 1s2018
* Prof: Flavio Keidi Miyazawa
* PED: Francisco Jhonatas Melo da Silva
* Usa ideias e código de Mauro Mulati e Flávio Keidi Miyazawa
******************************************************************************/
/*******************************************************************************
* EDITE ESTE ARQUIVO APENAS ONDE INDICADO
* DIGITE SEU RA: 177312
* SUBMETA SOMENTE ESTE ARQUIVO
******************************************************************************/
#include <iostream>
#include <float.h>
#include "pmr_e_h.h"
#include <signal.h>
#include <unistd.h>
#include "gurobi_c++.h"
//typedef vector<vector<double> > matriz;
volatile sig_atomic_t got_interrupt = 0;
static void alarm_handler(int sig) {
got_interrupt = 1;
}
int global_quantItens;
int* global_weigth;
int* global_value;
double** global_relation_array;
int* global_items_array;
int* global_items_array_partial;
int* global_items_array_partial_id;
int global_items_array_partial_count = 0;
int global_max_partial = 0;
void algE_rec(int i, int capacity, int partial_val) {
int id;
if (i == global_quantItens || capacity == 0 || got_interrupt)
return;
// Analise without me
algE_rec(i+1, capacity, partial_val);
// If I fit
capacity -= global_weigth[i];
if (capacity >= 0) {
global_items_array_partial[i] = 1;
global_items_array_partial_id[global_items_array_partial_count++] = i;
// Adds the relation values between items in the partial_items array and `i`, and `i` value itself
for (int j = 0; j < global_items_array_partial_count-1; j++) {
id = global_items_array_partial_id[j];
if (global_items_array_partial[id])
partial_val += global_relation_array[i][id];
}
partial_val += global_value[i];
// save if better
if (partial_val > global_max_partial) {
global_max_partial = partial_val;
for (int j = 0; j < global_quantItens; j++) {
global_items_array[j] = global_items_array_partial[j];
}
}
// Analise with me
algE_rec(i+1, capacity, partial_val);
global_items_array_partial[i] = 0;
global_items_array_partial_count--;
}
}
// get the id for the biggest v[id] that fits
int get_best_id(int capacity) {
double curr_v, max_v = 0;
int max_v_i = -1;
for (int i = 0; i < global_quantItens; i++) {
// skip if already in solution
if (global_items_array_partial[i])
continue;
curr_v = (double)global_value[i] / global_weigth[i];
if (global_weigth[i] <= capacity && curr_v > max_v) {
max_v = curr_v;
max_v_i = i;
}
}
return max_v_i;
}
void algH_array(int &capacity) {
int best_id, j;
while (capacity) {
if (got_interrupt)
return;
// Put the best into solution
best_id = get_best_id(capacity);
if (best_id == -1)
return;
capacity -= global_weigth[best_id];
global_max_partial += global_value[best_id];
global_items_array_partial[best_id] = 1;
// update v[j] for all j outside of the solution with relation[best_id][j] value
for (j=0; j < global_quantItens; j++)
if (!global_items_array_partial[j])
global_value[j] += global_relation_array[best_id][j];
}
}
int algE(int capacity, int quantItens, vector<int> s, vector<int> v, matriz &relation, vector<int>& itensMochila, int maxTime) {
got_interrupt = false;
signal(SIGALRM, alarm_handler);
alarm(maxTime);
global_weigth = &s[0];
global_value = &v[0];
global_items_array = (int*)calloc(quantItens, sizeof(int));
global_items_array_partial = (int*)calloc(quantItens, sizeof(int));
global_items_array_partial_id = (int*)calloc(quantItens, sizeof(int));
global_relation_array = (double**)malloc(quantItens * sizeof(double*));
for (int i = 0; i < quantItens; i++) {
global_relation_array[i] = &relation[i][0];
}
global_max_partial = 0;
global_quantItens = quantItens;
algE_rec(0, capacity, 0);
// copy response
for (int i = 0; i < quantItens; i++)
itensMochila[i] = global_items_array[i];
free(global_items_array);
free(global_items_array_partial);
free(global_items_array_partial_id);
free(global_relation_array);
return global_max_partial;
}
int algH(int capacity, int quantItens, vector<int> s, vector<int> v, matriz &relation, vector<int>& itensMochila, int maxTime) {
global_quantItens = quantItens;
global_max_partial = 0;
global_weigth = &s[0];
global_value = &v[0];
global_items_array_partial = (int*)calloc(quantItens, sizeof(int));
global_relation_array = (double**)malloc(quantItens * sizeof(double*));
for (int i = 0; i < quantItens; i++) {
global_relation_array[i] = &relation[i][0];
}
got_interrupt = false;
signal(SIGALRM, alarm_handler);
alarm(maxTime);
algH_array(capacity);
for (int j = 0; j < global_quantItens; j++) {
itensMochila[j] = global_items_array_partial[j];
}
free(global_items_array_partial);
free(global_relation_array);
return global_max_partial;
}
int algExato(int capacity, int quantItens, vector<int> s, vector<int> v, matriz &relation, vector<int>& itensMochila, int maxTime) {
int verbose = true;
bool with_heuristic = false;
double grb_heuristics_time = 0.05;
int presolve = 0;
for (int i=0 ; i<quantItens ; i++)
itensMochila[i] = 0;
double valor_h;
if (with_heuristic)
valor_h = algH(capacity, quantItens, s, v, relation, itensMochila, maxTime);
vector<GRBVar> x(quantItens);
vector< vector<GRBVar> > y(quantItens);
GRBEnv env = GRBEnv();
GRBModel model = GRBModel(env);
try {
// Quiet gurobi
model.getEnv().set(GRB_IntParam_OutputFlag, verbose);
GRBLinExpr expr;
model.set(GRB_StringAttr_ModelName, "Relational Knapsack Problem"); // gives a name to the problem
model.set(GRB_IntAttr_ModelSense, GRB_MAXIMIZE); // says that lp is a maximization problem
for (int i=0 ; i<quantItens ; i++) {
x[i] = model.addVar(0.0, 1.0, v[i], GRB_BINARY, "");
if (with_heuristic)
x[i].set(GRB_DoubleAttr_Start, itensMochila[i]);
expr += s[i]*x[i];
}
for (int i=0 ; i<quantItens ; i++) {
for (int j=0 ; j<i ; j++) {
y[i].push_back(model.addVar(0.0, 1.0, relation[i][j], GRB_BINARY, ""));
if (with_heuristic)
y[i][j].set(GRB_DoubleAttr_Start, itensMochila[i] && itensMochila[j]);
model.addConstr(x[i]+x[j] <= y[i][j]+1);
model.addConstr(x[i]+x[j] >= 2*y[i][j]);
}
}
model.update(); // run update to use model inserted variables
model.addConstr(expr <= capacity);
model.update(); // Process any pending model modifications.
// bound the execution time
model.getEnv().set(GRB_DoubleParam_TimeLimit, maxTime);
// bound the solution value
if (with_heuristic)
model.getEnv().set(GRB_DoubleParam_Cutoff, valor_h);
// bound heuristics time
model.getEnv().set(GRB_DoubleParam_Heuristics, grb_heuristics_time);
// set MIPFocus
model.getEnv().set(GRB_IntParam_MIPFocus, 1);
// set Presolve approach
model.getEnv().set(GRB_IntParam_Presolve, presolve);
model.update(); // Process any pending model modifications.
model.optimize();
double total_value = 0.0;
for (int i=0 ; i<quantItens ; i++) {
if (x[i].get(GRB_DoubleAttr_X) > 0.999) {
total_value += v[i];
itensMochila[i] = 1;
for (int j=0 ; j<i ; j++) {
if (x[j].get(GRB_DoubleAttr_X) > 0.999) {
total_value += relation[i][j];
}
}
} else {
itensMochila[i] = 0;
}
}
return total_value;
} catch(GRBException e) {
// if it isn't a timelimit exception
if (e.getErrorCode() != 10005) {
cout << "Error code = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
exit(1);
} else {
cout << "Time's up" << endl;
double total_value = 0.0;
double* xvals = model.get(GRB_DoubleAttr_X, model.getVars(), quantItens);
for (int i=0 ; i<quantItens ; i++) {
if (xvals[i] > 0.999) {
total_value += v[i];
itensMochila[i] = 1;
for (int j=0 ; j<i ; j++) {
if (xvals[j] > 0.999) {
total_value += relation[i][j];
}
}
} else {
itensMochila[i] = 0;
}
}
return total_value;
}
} catch (...) {
printf("Exception...\n");
exit(1);
}
}