-
Notifications
You must be signed in to change notification settings - Fork 2
/
value_heuristic.cc
465 lines (397 loc) · 13.9 KB
/
value_heuristic.cc
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/******************************************************************************
* Copyright (C) 2014 Juan Antonio Garcia Martin , Peter Clote, Ivan Dotu *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
#include <iostream>
#include "value_heuristic.h"
namespace operations_research {
// FirstUnboundSelector functions
IntVar* FirstUnboundSelector::Select(Solver* const s, int64* id) {
for (int i = first_; i < vars_.size(); ++i) {
IntVar* const var = vars_[i];
if (!var->Bound()) {
s->SaveAndSetValue(&first_, i);
*id = i;
return var;
}
}
s->SaveAndSetValue(&first_, static_cast<int>(vars_.size()));
*id = vars_.size();
return nullptr;
}
int64 RandomValueSelector::Select(const IntVar* const v, int64 id){
const uint64 span = v->Max() - v->Min() + 1;
Solver* const s = v->solver();
for (;;) {
const int64 value = v->Min() + s->Rand64(span);
if (v->Contains(value)) {
return value;
}
}
// Never reached, remove warnings
return v->Max();
}
int64 UpValueSelector::Select(const IntVar* const v, int64 id) {
switch(v->Size()){
case 4:
case 2:
return v->Min();
break;
case 3:
case 1:
default:
return v->Max();
break;
}
// Never reached, remove warnings
return v->Max();
}
int64 UpValueSelectorContains::Select(const IntVar* const v, int64 id) {
for(int i=0;i<updomain.size();i++){
if(v->Contains(updomain[i])){
return updomain[i];
}
}
// Never reached, remove warnings
return v->Max();
}
int64 UpValueSelectorContainsRand::Select(const IntVar* const v, int64 id) {
int64 retVal;
int randVal;
for(int i=0;i<updomain.size();i++){
if(v->Contains(updomain[i])){
retVal=updomain[i];
randVal=rand()%100;
if (randVal < threshold_){
return retVal;
}
}
}
return retVal;
// Never reached, remove warnings
return v->Max();
}
int64 UpValueSelectorClosing::Select(const IntVar* const v, int64 id) {
int64 retVal;
int randVal;
for(int i=0;i<updomainClosing.size();i++){
if(v->Contains(updomainClosing[i])){
retVal=updomainClosing[i];
randVal=rand()%100;
if (randVal < threshold_){
return retVal;
}
}
}
return retVal;
// Never reached, remove warnings
return v->Max();
}
int64 BpValueSelector::Select(const IntVar* const v, int64 id) {
for(int i=0;i<bpdomain.size();i++){
if(v->Contains(bpdomain[i])){
return bpdomain[i];
}
}
// Never reached, remove warnings
return v->Max();
}
int64 BpValueSelectorRand::Select(const IntVar* const v, int64 id) {
int64 retVal;
int randVal;
for(int i=0;i<bpdomain.size();i++){
if(v->Contains(bpdomain[i])){
retVal=bpdomain[i];
randVal=((double) rand() / (RAND_MAX));
if (randVal < threshold_){
return retVal;
}
}
}
return retVal;
}
int64 BpValueSelectorMultiRand::Select(const IntVar* const v, int64 id) {
int64 retVal;
int randVal;
for(int i=0;i<bpdomainMulti.at(type_).size();i++){
if(v->Contains(bpdomainMulti.at(type_)[i])){
retVal=bpdomainMulti.at(type_)[i];
randVal=rand()%100;
if (randVal < threshold_){
return retVal;
}
}
}
return retVal;
}
int64 BpValueSelectorBoltz::Select(const IntVar* const v, int64 id) {
int64 retVal;
int randVal;
for(int i=0;i<bpdomain.size();i++){
if(v->Contains(bpdomain[i])){
retVal=bpdomain[i];
randVal=rand()%100;
if(randVal<=stackProbabilities[i]){
return retVal;
}
}
}
return retVal;
}
int64 StackValueSelectorContains::Select(const IntVar* const v, int64 id) {
vector<int>* currentDomain;
if(vars_[id-1]->Bound()){
currentDomain = &stackDomain[vars_[id-1]->Value()];
}
else{
currentDomain = &stackDomain[0];
}
for(int i=0;i<currentDomain->size();i++){
if(v->Contains((*currentDomain)[i])){
return (*currentDomain)[i];
}
}
// Never reached, remove warnings
return v->Max();
}
int64 StackValueSelectorContainsRand::Select(const IntVar* const v, int64 id) {
int64 retVal;
double randVal;
vector<int> currentDomain;
if(vars_[id-1]->Bound()){
currentDomain = stackDomain[vars_[id-1]->Value()];
}
else{
currentDomain = stackDomain[0];
}
for(int i=0;i<currentDomain.size();i++){
if(v->Contains(currentDomain[i])){
retVal=currentDomain[i];
randVal=rand()%100;
if(randVal<threshold_){
return retVal;
}
}
}
return retVal;
}
int64 StackValueSelectorContainsBoltz::Select(const IntVar* const v, int64 id) {
int64 retVal;
double randVal;
vector<int> currentDomain;
vector<double> currentProbs;
if(vars_[id-1]->Bound()){
currentDomain = stackDomain[vars_[id-1]->Value()];
currentProbs = stackProbabilities[vars_[id-1]->Value()];
}
else{
currentDomain = stackDomain[0];
currentProbs = stackProbabilities[0];
}
for(int i=0;i<currentDomain.size();i++){
if(v->Contains(currentDomain[i])){
retVal=currentDomain[i];
randVal=((double) rand() / (RAND_MAX));
if(randVal<=currentProbs[i]){
return retVal;
}
}
}
return retVal;
}
int64 ThreePrimeValueSelector::Select(const IntVar* const v, int64 id) {
int64 retVal;
double randVal;
vector<int> currentDomain;
if(vars_[id-1]->Bound()){
currentDomain = fivePrimeDom[vars_[id-1]->Value()];
}
else{
currentDomain = fivePrimeDom[4];
}
for(int i=0;i<currentDomain.size();i++){
if(v->Contains(currentDomain[i])){
retVal=currentDomain[i];
randVal=rand()%100;
if(randVal<threshold_){
return retVal;
}
}
}
return retVal;
}
std::vector<std::vector<int> > SimplifiedBPenergyHeuristic::orderArray_;
std::vector<std::vector<int> > SimplifiedBPenergyHeuristic::random_;
std::vector<std::vector<int> > SimplifiedBPenergyHeuristic::baseOrder_;
int64 SimplifiedBPenergyHeuristic::Select(const IntVar* const v, int64 id) {
int64 retVal;
double randVal;
if(random_[id][0]==NO_RAND_ASSIGN){
std::vector<int> y;
std::vector<int> tmpOrder;
if(value_selector_type_.at(id)==VH_BP_STACK_INV){
for(int i=0;i<bpdomain.size();i++){
y.push_back(i);
if(vars_[id-1]->Bound()){
random_.at(id)[i] = baseStackInv.at(vars_[id-1]->Value())[i]+(rand()%200);
}
else{
random_.at(id)[i] = baseStackInv.at(0)[i]+rand()%200;
}
}
}
else if (value_selector_type_.at(id)==VH_BP_STACK){
for(int i=0;i<bpdomain.size();i++){
y.push_back(i);
if(vars_[id-1]->Bound()){
random_.at(id)[i] = baseStack.at(vars_[id-1]->Value())[i]+(rand()%200);
}
else{
random_.at(id)[i] = baseStack.at(0)[i]+rand()%200;
}
}
}
else if (value_selector_type_.at(id)==VH_BP){
for(int i=0;i<bpdomain.size();i++){
y.push_back(i);
random_.at(id)[i] = baseStack.at(0)[i]+rand()%200;
}
}
else{
for(int i=0;i<bpdomain.size();i++){
y.push_back(i);
random_.at(id)[i] = baseEnergyMulti[i]+rand()%200;
}
}
std::sort(y.begin(), y.end(), IdxCompare(random_.at(id)));
for(int i=0;i<bpdomain.size();i++){
orderArray_.at(id)[i] = baseOrder_.at(id)[y[i]];
}
}
for(int i=0;i<bpdomain.size();i++){
if(v->Contains(orderArray_.at(id)[i])){
retVal=orderArray_.at(id)[i];
if(v->Size()==2){
orderArray_.at(id)=baseOrder_.at(id);
random_.at(id).assign(bpdomain.size(),NO_RAND_ASSIGN);
}
return retVal;
}
}
return retVal;
}
// VariableAssignmentSelector functions
std::string VariableAssignmentSelector::DebugString() const {
return var_selector_->DebugString() + "_" + var_selector_->VarDebugString() + "\n Value Selectors: " + std::to_string(value_selectors_.size());
//return var_selector_->DebugString() + "_" + value_selectors_[0]->DebugString() + var_selector_->VarDebugString();
}
// AssignOneVariableValue functions
AssignOneVariableValue::AssignOneVariableValue(IntVar* const v, int64 val) : var_(v), value_(val) {}
std::string AssignOneVariableValue::DebugString() const {
// return absl::StrFormat("[%s == %" GG_LL_FORMAT "d]", var_->DebugString().c_str(), value_);
return absl::StrFormat("[%s == %d]", var_->DebugString().c_str(), value_);
}
void AssignOneVariableValue::Apply(Solver* const s) { var_->SetValue(value_); }
void AssignOneVariableValue::Refute(Solver* const s) { var_->RemoveValue(value_);}
// BaseAssignVariables functions
BaseAssignVariables::~BaseAssignVariables() {}
Decision* BaseAssignVariables::Next(Solver* const s) {
int64 id = 0;
// Assign first variables fixed in the restart
if(!fixedVars.empty()){
IntVar* const variable = fixedVars.back();
fixedVars.pop_back();
int64 const val = fixedValues.back();
fixedValues.pop_back();
//printf("Assigning position %d\n",variable->Size());
return s->RevAlloc(new AssignOneVariableValue(variable, val));
}
IntVar* const var = selector_->SelectVariable(s, &id);
//printf("Variable Selected %d\n",id);
if (nullptr != var) {
const int64 value = selector_->SelectValue(var, id);
//printf("Value Selected %d\n\n",value);
return s->RevAlloc(new AssignOneVariableValue(var, value));
}
return nullptr;
}
std::string BaseAssignVariables::DebugString() const {
return selector_->DebugString();
}
// MakePhase
BaseAssignVariables* MakePhase(Solver* s, const std::vector<IntVar*>& vars, vector<int> value_selector_type, int upthreshold, int bpthreshold) {
//VariableSelector
FirstUnboundSelector* var_selector = s->RevAlloc(new FirstUnboundSelector(vars));
if(upthreshold !=100 || bpthreshold !=100){
long seed = time(NULL);
srand(seed);
}
vector<ValueSelector*> value_selectors;
value_selectors.push_back(s->RevAlloc(new MinValueSelector));
value_selectors.push_back(s->RevAlloc(new UpValueSelector));
if(upthreshold ==100){
value_selectors.push_back(s->RevAlloc(new UpValueSelectorContains));
}
else{
value_selectors.push_back(s->RevAlloc(new UpValueSelectorContainsRand(upthreshold)));
}
if(bpthreshold ==100){
value_selectors.push_back(s->RevAlloc(new BpValueSelector));
value_selectors.push_back(s->RevAlloc(new StackValueSelectorContains(vars)));
}
else if(bpthreshold ==0){
value_selectors.push_back(s->RevAlloc(new BpValueSelectorBoltz));
value_selectors.push_back(s->RevAlloc(new StackValueSelectorContainsBoltz(vars)));
}
else {
//value_selectors.push_back(s->RevAlloc(new BpValueSelectorRand(bpthreshold)));
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
}
value_selectors.push_back(s->RevAlloc(new RandomValueSelector));
// Value selector for multiple structures
/* value_selectors.push_back(s->RevAlloc(new BpValueSelectorMultiRand(bpthreshold,0)));
value_selectors.push_back(s->RevAlloc(new BpValueSelector));
value_selectors.push_back(s->RevAlloc(new BpValueSelectorMultiRand(bpthreshold,2)));
value_selectors.push_back(s->RevAlloc(new BpValueSelectorMultiRand(bpthreshold,3)));
value_selectors.push_back(s->RevAlloc(new BpValueSelectorMultiRand(bpthreshold,4)));
value_selectors.push_back(s->RevAlloc(new BpValueSelectorMultiRand(bpthreshold,5)));*/
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
// value_selectors.push_back(s->RevAlloc(new BpValueSelector));
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
value_selectors.push_back(s->RevAlloc(new UpValueSelectorClosing(upthreshold)));
value_selectors.push_back(s->RevAlloc(new ThreePrimeValueSelector(vars, upthreshold)));
value_selectors.push_back(s->RevAlloc(new SimplifiedBPenergyHeuristic(vars, bpthreshold, value_selector_type)));
// BaseVariableAssignmentSelector
VariableAssignmentSelector* selector = s->RevAlloc(new VariableAssignmentSelector(var_selector, value_selectors, value_selector_type));
return s->RevAlloc(new BaseAssignVariables(selector));
}
// OTHER CLASSES (REMOVE)
// AssignOneNtValue
AssignOneNtValue::AssignOneNtValue(IntVar* const v, int64 val)
: var_(v), value_(val) {}
std::string AssignOneNtValue::DebugString() const {
// return absl::StrFormat("[%s == %" GG_LL_FORMAT "d]", var_->DebugString().c_str(), value_);
return absl::StrFormat("[%s == %d]", var_->DebugString().c_str(),value_);
}
void AssignOneNtValue::Apply(Solver* const s) { var_->SetValue(value_); }
void AssignOneNtValue::Refute(Solver* const s) {
var_->RemoveValue(value_);
}
}