-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathILS.cpp
174 lines (144 loc) · 4.31 KB
/
ILS.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
#include "ILS.h"
void ILS(vector<vector<int>*> &cgroups,
vector<int> &rlenght, vector<int> &lot_s,
vector<int> &lpiece, vector<int> &dpiece,
vector<int> &leftover,vector<int> &used_rolls,
vector<int> &rollType, vector<int> &variety) {
int maxit = ILS_MAXIT;
vector<vector<int>*> S_star;
vector<vector<int>*> S_pert;
vector<int> leftover_star;
vector<int> leftover_pert;
vector<int> used_rolls_star;
vector<int> used_rolls_pert;
vector<int> variety_star;
vector<int> variety_pert;
int nitems = lpiece.size();
int sum;
int max_pert = MAX_PERTURB;
double T = INIT_TEMP;
localSearchBB(cgroups,rlenght,lot_s,
lpiece,dpiece,leftover,
used_rolls,variety);
S_star = duplicate(cgroups,nitems);//Para este punto, cgroups a sido procesado
S_pert = duplicate(S_star,nitems);
leftover_star = leftover;
used_rolls_star = used_rolls;
variety_star = variety;
srand(time(NULL));
sum = 0;
int tleft1 = 0;
int leftoverS1_size = leftover.size();
for(int i = 0; i < leftoverS1_size; i++)
sum += leftover_star[i];
tleft1 = sum;
sum = 0;
int tleft_best;
int leftover_best_size = leftover.size();
for(int i = 0; i < leftover_best_size; i++)
sum += leftover[i];
tleft_best = sum;
while (maxit > 0) {
//cout << maxit <<"\n";
leftover_pert = leftover_star;
used_rolls_pert = used_rolls_star;
variety_pert = variety_star;
//S_pert = duplicate(S_star,nitems);
overwrite(S_star,S_pert,nitems);
Perturb(S_pert,
leftover_pert,
used_rolls_pert,
variety_pert,
rlenght,
lpiece,
lot_s,
MAX_PERTURB);
localSearchBB(S_pert,
rlenght, lot_s,
lpiece, dpiece,
leftover_pert,
used_rolls_pert,
variety_pert);
sum = 0;
int tleft2;
int leftoverS2_size = leftover_pert.size();
for(int i = 0; i < leftoverS2_size; i++)
sum += leftover_pert[i];
tleft2 = sum;
if (tleft_best > tleft2) {
cout << maxit <<":"<< tleft2 << "<-Mejora\n";
leftover = leftover_pert;
used_rolls = used_rolls_pert;
variety = variety_pert;
tleft_best = tleft2;
overwrite(S_pert,cgroups,nitems);
}
if (tleft1 > tleft2) {
//cout << maxit <<":"<< tleft2 << "<-Mejora*\n";
leftover_star = leftover_pert;
used_rolls_star = used_rolls_pert;
variety_star = variety_pert;
tleft1 = tleft2;
overwrite(S_pert,S_star,nitems);
}
else if (tleft1 < tleft2){ // Se elige con probabilidad Simulated Annealing
double p = exp(((double)(tleft1 - tleft2)) / T);
double r = (double)random()/(double)RAND_MAX;
if (r < p) {
//cout << tleft2 << ":" << tleft1 <<"<-emperorando," << T <<"\n";
leftover_star = leftover_pert;
used_rolls_star = used_rolls_pert;
variety_star = variety_pert;
tleft1 = tleft2;
overwrite(S_pert,S_star,nitems);
}
}
T *= EXP_DESC;
//cout << T << " temp\n";
maxit--;
}
int i;
int totalLO = 0;
int totalRollLenght = 0;
vector<int>* pieceSet;
int minLO;
int minLG;
int minRollType;
cout << "Mejor corte\n";
printCG(cgroups);
for(i = 0; i < cgroups.size(); i++) {
sum = 0;
if (used_rolls[i]) {
// cout << "Tipo " << i << endl;
// cout << "rolls " << used_rolls[i] << endl;
// cout << "leftover " << leftover[i] << endl;
//cout << leftover[i] << " " << i << " origlo\n";
totalLO += leftover[i];
// for(j=0;j<nitems;++j)
// sum += (*cgroups[i])[j];
//cout << (*cgroups[i])[j] << " map\n";
// cout << "n_used_pieces " << sum << endl;
// cout << "------------" << endl;
}
}
//cout << totalLO <<" original\n";
totalLO = 0;
totalRollLenght = 0;
for(i=0; i<cgroups.size(); ++i){
if (used_rolls[i]) {
pieceSet = cgroups[i];
int* bestCut;
bestCut = bestCutting(*pieceSet, rlenght, lpiece);
minLO = bestCut[0];
minLG = bestCut[1];
minRollType = bestCut[2];
delete [] bestCut;
//cout << minLO << " " << i << " minlo\n";
totalLO += minLO;
totalRollLenght += minLG*rlenght[minRollType];
}
}
cout << totalLO << "\n";
cout << totalRollLenght << "\n";
cout << (100.0*(double)totalLO)/(double)totalRollLenght << "\n";
}