-
Notifications
You must be signed in to change notification settings - Fork 6
/
feasibilityjump.hh
789 lines (673 loc) · 26.4 KB
/
feasibilityjump.hh
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <random>
#include <cmath>
#include <cassert>
#include <algorithm>
#define FJ_LOG_PREFIX "Feasibility Jump: "
enum RowType
{
Equal,
Lte,
Gte,
};
enum VarType
{
Continuous,
Integer
};
enum CallbackControlFlow
{
Terminate,
Continue,
};
struct FJStatus
{
int totalEffort;
int effortSinceLastImprovement;
int numVars;
double solutionObjectiveValue;
double *solution;
};
const double violationTolerance = 1.0e-5;
const double equalityTolerance = 1.0e-5;
// Measures if two doubles are equal within a tolerance of 1.0e-5.
bool eq(double a, double b)
{
return fabs(a - b) < equalityTolerance;
}
struct IdxCoeff
{
uint32_t idx;
double coeff;
IdxCoeff(uint32_t idx, double coeff) : idx(idx), coeff(coeff) {}
};
struct Var
{
VarType vartype;
double lb;
double ub;
double objectiveCoeff;
std::vector<IdxCoeff> coeffs;
};
struct Constraint
{
RowType sense;
double rhs;
std::vector<IdxCoeff> coeffs;
double weight;
double incumbentLhs;
int32_t violatedIdx;
// Computes the constraint's contribution to the feasibility score:
// If the constraint is satisfied by the given LHS value, returns 0.
// If the constraint is violated by the given LHS value, returns -|lhs-rhs|.
double score(double lhs)
{
if (sense == RowType::Equal)
return -fabs(lhs - rhs);
else if (sense == RowType::Lte)
return -(std::max(0., lhs - rhs));
else
return -(std::max(0., rhs - lhs));
}
};
// A potential new value for a varaiable, including its score.
struct Move
{
double value;
double score;
static Move undef()
{
Move move;
move.value = NAN;
move.score = -std::numeric_limits<double>::infinity();
return move;
}
};
// Represents a modification of the LHS in a constraint, for a specific
// variable/constraint combination.The `modifyMove` function below is used to
// update the score of a `Move` to reflect the LHS modification.
struct LhsModification
{
uint32_t varIdx;
uint32_t constraintIdx;
double coeff;
double oldLhs;
double newLhs;
};
// Stores the MIP problem, an incumbent assignment, and the set of constraints
// that are violated in the current incumbent assignment. This set is maintained
// when changes are given to the incumbent assignment using `setValue`.
struct Problem
{
std::vector<Var> vars;
std::vector<Constraint> constraints;
std::vector<double> incumbentAssignment;
std::vector<uint32_t> violatedConstraints;
bool usedRelaxContinuous = false;
size_t nNonzeros;
double incumbentObjective = NAN;
int addVar(VarType vartype, double lb, double ub, double objCoeff)
{
auto idx = vars.size();
Var var;
var.vartype = vartype;
var.lb = lb;
var.ub = ub;
var.objectiveCoeff = objCoeff;
vars.push_back(var);
incumbentAssignment.push_back(lb);
return idx;
}
int addConstraint(RowType sense, double rhs, int numCoeffs, int *rowVarIdxs, double *rowCoeffs, int relax_continuous)
{
if (relax_continuous)
usedRelaxContinuous = true;
// If we are relaxing continuous variables, an equality needs to be split into Gte and Lte.
if (relax_continuous > 0 && sense == RowType::Equal)
if (std::any_of(rowVarIdxs, rowVarIdxs + numCoeffs, [&](double varIdx)
{ return vars[varIdx].vartype == VarType::Continuous; }))
{
addConstraint(RowType::Gte, rhs, numCoeffs, rowVarIdxs, rowCoeffs, relax_continuous);
addConstraint(RowType::Lte, rhs, numCoeffs, rowVarIdxs, rowCoeffs, relax_continuous);
return INT_MAX;
}
std::vector<IdxCoeff> coeffs;
for (int i = 0; i < numCoeffs; i += 1)
{
if (relax_continuous > 0 && vars[rowVarIdxs[i]].vartype == VarType::Continuous)
{
if (sense == RowType::Lte)
{
if (rowCoeffs[i] >= 0.)
rhs -= rowCoeffs[i] * vars[rowVarIdxs[i]].lb;
else
rhs -= rowCoeffs[i] * vars[rowVarIdxs[i]].ub;
}
else if (sense == RowType::Gte)
{
if (rowCoeffs[i] >= 0.)
rhs -= rowCoeffs[i] * vars[rowVarIdxs[i]].ub;
else
rhs -= rowCoeffs[i] * vars[rowVarIdxs[i]].lb;
}
else
return INT_MIN;
}
else
coeffs.emplace_back(rowVarIdxs[i], rowCoeffs[i]);
}
if (coeffs.empty())
{
bool ok;
if (sense == RowType::Lte)
ok = 0 <= rhs + equalityTolerance;
else if (sense == RowType::Gte)
ok = 0 + equalityTolerance >= rhs;
else
ok = eq(0, rhs);
return ok ? INT_MAX : INT_MIN;
}
int newConstraintIdx = constraints.size();
for (auto &c : coeffs)
{
vars[c.idx].coeffs.emplace_back(newConstraintIdx, c.coeff);
}
nNonzeros += coeffs.size();
Constraint newConstraint;
newConstraint.coeffs = coeffs;
newConstraint.incumbentLhs = NAN;
newConstraint.violatedIdx = -1;
newConstraint.rhs = rhs;
newConstraint.sense = sense;
newConstraint.weight = 1.0;
constraints.push_back(newConstraint);
return newConstraintIdx;
}
void resetIncumbent(double *initialValues)
{
// Set the initial values, if given.
if (initialValues)
for (size_t i = 0; i < vars.size(); i += 1)
incumbentAssignment[i] = initialValues[i];
// std::copy(initialValues, initialValues + vars.size(), incumbentAssignment);
// Reset the incumbent objective.
incumbentObjective = 0;
for (size_t i = 0; i < vars.size(); i += 1)
incumbentObjective += vars[i].objectiveCoeff * incumbentAssignment[i];
// Reset the constraint LHSs and the violatedConstraints list.
violatedConstraints.clear();
for (size_t cIdx = 0; cIdx < constraints.size(); cIdx += 1)
{
Constraint &cstr = constraints[cIdx];
cstr.incumbentLhs = 0.0;
for (auto &vc : cstr.coeffs)
cstr.incumbentLhs += vc.coeff * incumbentAssignment[vc.idx];
if (cstr.score(cstr.incumbentLhs) < -violationTolerance)
{
cstr.violatedIdx = violatedConstraints.size();
violatedConstraints.push_back(cIdx);
}
else
cstr.violatedIdx = -1;
}
}
// Updates a variable assignment for `varIdx` to `newValue`.
// Takes a function parameter f that receives a LhsModification
// for every variable/constraint combination (except for `varIdx` itself)
// where the LHS of the constraint has changed.
template <typename F>
size_t setValue(uint32_t varIdx, double newValue, F f)
{
size_t dt = 0;
double oldValue = incumbentAssignment[varIdx];
double delta = (newValue - oldValue);
incumbentAssignment[varIdx] = newValue;
incumbentObjective += vars[varIdx].objectiveCoeff * delta;
// printf("Setting v%d to from %g to value %g\n", varIdx, oldValue, newValue);
// Update the LHSs of all involved constraints.
for (auto &cstrCoeff : vars[varIdx].coeffs)
{
double oldLhs = constraints[cstrCoeff.idx].incumbentLhs;
double newLhs = oldLhs + cstrCoeff.coeff * delta;
constraints[cstrCoeff.idx].incumbentLhs = newLhs;
double newCost = constraints[cstrCoeff.idx].score(newLhs);
// Add/remove from the violatedConstraints list.
if (newCost < -violationTolerance && constraints[cstrCoeff.idx].violatedIdx == -1)
{
// Became violated.
constraints[cstrCoeff.idx].violatedIdx = violatedConstraints.size();
violatedConstraints.push_back(cstrCoeff.idx);
}
if (newCost >= -violationTolerance && constraints[cstrCoeff.idx].violatedIdx != -1)
{
// Became satisfied.
auto lastViolatedIdx = violatedConstraints.size() - 1;
auto lastConstraintIdx = violatedConstraints[lastViolatedIdx];
auto thisViolatedIdx = constraints[cstrCoeff.idx].violatedIdx;
std::swap(violatedConstraints[thisViolatedIdx], violatedConstraints[lastViolatedIdx]);
constraints[lastConstraintIdx].violatedIdx = thisViolatedIdx;
constraints[cstrCoeff.idx].violatedIdx = -1;
violatedConstraints.pop_back();
}
// Now, report the changes in LHS for other variables.
dt += constraints[cstrCoeff.idx].coeffs.size();
for (auto &varCoeff : constraints[cstrCoeff.idx].coeffs)
{
if (varCoeff.idx != varIdx)
{
LhsModification m;
m.varIdx = varCoeff.idx;
m.constraintIdx = cstrCoeff.idx;
m.coeff = varCoeff.coeff;
m.oldLhs = oldLhs;
m.newLhs = newLhs;
f(m);
}
}
}
return dt;
}
};
void modifyMove(LhsModification mod, Problem &problem, Move &move)
{
Constraint &c = problem.constraints[mod.constraintIdx];
auto incumbent = problem.incumbentAssignment[mod.varIdx];
double oldModifiedLhs = mod.oldLhs + mod.coeff * (move.value - incumbent);
double oldScoreTerm = c.weight * (c.score(oldModifiedLhs) - c.score(mod.oldLhs));
double newModifiedLhs = mod.newLhs + mod.coeff * (move.value - incumbent);
double newScoreTerm = c.weight * (c.score(newModifiedLhs) - c.score(mod.newLhs));
move.score += newScoreTerm - oldScoreTerm;
}
// Stores current moves and computes updated jump values for
// the "Jump" move type.
class JumpMove
{
std::vector<Move> moves;
std::vector<std::pair<double, double>> bestShiftBuffer;
public:
void init(Problem &problem)
{
moves.resize(problem.vars.size());
}
template <typename F>
void forEachVarMove(int32_t varIdx, F f)
{
f(moves[varIdx]);
}
void updateValue(Problem &problem, uint32_t varIdx)
{
bestShiftBuffer.clear();
auto varIncumbentValue = problem.incumbentAssignment[varIdx];
double currentValue = problem.vars[varIdx].lb;
double currentScore = 0.0;
double currentSlope = 0.0;
// printf(" updatevalue lb %g ub %g numcells %d\n",
// problem.vars[varIdx].lb,
// problem.vars[varIdx].ub, problem.vars[varIdx].coeffs.size());
for (auto &cell : problem.vars[varIdx].coeffs)
{
auto &constraint = problem.constraints[cell.idx];
std::vector<std::pair<double, double>> constraintBounds;
if (constraint.sense == RowType::Lte)
constraintBounds.emplace_back(-std::numeric_limits<double>::infinity(), constraint.rhs);
else if (constraint.sense == RowType::Gte)
constraintBounds.emplace_back(constraint.rhs, std::numeric_limits<double>::infinity());
else
{
constraintBounds.emplace_back(-std::numeric_limits<double>::infinity(), constraint.rhs);
constraintBounds.emplace_back(constraint.rhs, constraint.rhs);
constraintBounds.emplace_back(constraint.rhs, std::numeric_limits<double>::infinity());
}
for (auto &bound : constraintBounds)
{
double residualIncumbent = constraint.incumbentLhs - cell.coeff * varIncumbentValue;
std::pair<double, double> validRange = {
((1.0 / cell.coeff) * (bound.first - residualIncumbent)),
((1.0 / cell.coeff) * (bound.second - residualIncumbent)),
};
if (problem.vars[varIdx].vartype == VarType::Integer)
validRange = {
std::ceil(validRange.first - equalityTolerance),
std::floor(validRange.second + equalityTolerance),
};
if (validRange.first > validRange.second)
continue;
if (validRange.first > currentValue)
{
currentScore += constraint.weight * (validRange.first - currentValue);
currentSlope -= constraint.weight;
if (validRange.first < problem.vars[varIdx].ub)
bestShiftBuffer.emplace_back(validRange.first, constraint.weight);
}
if (validRange.second <= currentValue)
{
currentScore += constraint.weight * (validRange.second - currentValue);
currentSlope += constraint.weight;
}
else if (validRange.second < problem.vars[varIdx].ub)
bestShiftBuffer.emplace_back(validRange.second, constraint.weight);
}
}
bestShiftBuffer.emplace_back(problem.vars[varIdx].lb, 0);
bestShiftBuffer.emplace_back(problem.vars[varIdx].ub, 0);
std::sort(bestShiftBuffer.begin(), bestShiftBuffer.end());
double bestScore = currentScore;
double bestValue = currentValue;
// printf("evaluating best shift buffer size %d \n", bestShiftBuffer.size());
for (auto &item : bestShiftBuffer)
{
currentScore += (item.first - currentValue) * currentSlope;
currentSlope += item.second;
currentValue = item.first;
// printf("bestshift cscore %g cslope %g cval %g bestval %g bestscore %g\n",
// currentScore,currentSlope, currentValue, bestScore, bestValue
// );
if (eq(bestValue, problem.incumbentAssignment[varIdx]) ||
(!eq(currentValue, problem.incumbentAssignment[varIdx]) && currentScore < bestScore))
{
bestScore = currentScore;
bestValue = currentValue;
}
// Slope is always increasing, so if we have a valid value, we can quit
// as soon as the slope turns nonnegative, since we must already have
// visited the minimum.
if (!eq(bestValue, problem.incumbentAssignment[varIdx]) && currentSlope >= 0.)
break;
}
// printf("Setting jump for %d to from %g to %g\n", varIdx, problem.incumbentAssignment[varIdx], moves[varIdx].value);
moves[varIdx].value = bestValue;
}
};
class FeasibilityJumpSolver
{
int verbosity;
Problem problem;
JumpMove jumpMove;
std::vector<uint32_t> goodVarsSet;
std::vector<int32_t> goodVarsSetIdx;
std::mt19937 rng;
double bestObjective = std::numeric_limits<double>::infinity();
double objectiveWeight = 0.0;
size_t bestViolationScore = SIZE_MAX;
size_t effortAtLastCallback = 0;
size_t effortAtLastImprovement = 0;
size_t totalEffort = 0;
double weightUpdateDecay;
double weightUpdateIncrement = 1.0;
size_t nBumps;
// The probability of choosing a random positive-score variable.
const double randomVarProbability = 0.001;
// The probability of choosing a variable using a random constraint's
// non-zero coefficient after updating weights.
const double randomCellProbability = 0.01;
// The number of moves to evaluate, if there are many positive-score
// variables available.
const size_t maxMovesToEvaluate = 25;
public:
FeasibilityJumpSolver(int seed = 0, int _verbosity = 0, double _weightUpdateDecay = 1.0)
{
verbosity = _verbosity;
weightUpdateDecay = _weightUpdateDecay;
rng = std::mt19937(seed);
}
int addVar(VarType vartype, double lb, double ub, double objCoeff)
{
goodVarsSetIdx.push_back(-1);
return problem.addVar(vartype, lb, ub, objCoeff);
}
int addConstraint(RowType sense, double rhs, int numCoeffs, int *rowVarIdxs, double *rowCoeffs, int relax_continuous)
{
return problem.addConstraint(sense, rhs, numCoeffs, rowVarIdxs, rowCoeffs, relax_continuous);
}
int solve(double *initialValues, std::function<CallbackControlFlow(FJStatus)> callback)
{
assert(callback);
if (verbosity >= 1)
printf(FJ_LOG_PREFIX "starting solve. weightUpdateDecay=%g, relaxContinuous=%d \n", weightUpdateDecay, problem.usedRelaxContinuous);
init(initialValues);
for (int step = 0; step < INT_MAX; step += 1)
{
if (user_terminate(callback, nullptr))
break;
if (step % 100000 == 0)
{
if (verbosity >= 1)
printf(FJ_LOG_PREFIX "step %d viol %zd good %zd bumps %zd\n", step, problem.violatedConstraints.size(), goodVarsSet.size(), nBumps);
}
if (problem.violatedConstraints.size() < bestViolationScore)
{
effortAtLastImprovement = totalEffort;
bestViolationScore = problem.violatedConstraints.size();
}
if (problem.violatedConstraints.empty() && problem.incumbentObjective < bestObjective)
{
effortAtLastImprovement = totalEffort;
bestObjective = problem.incumbentObjective;
if (user_terminate(callback, problem.incumbentAssignment.data()))
break;
}
if (problem.vars.size() == 0)
break;
uint32_t var = selectVariable();
doVariableMove(var);
}
return 0;
}
private:
void init(double *initialValues)
{
problem.resetIncumbent(initialValues);
jumpMove.init(problem);
totalEffort += problem.nNonzeros;
// Reset the variable scores.
goodVarsSet.clear();
for (size_t i = 0; i < problem.vars.size(); i += 1)
resetMoves(i);
}
uint32_t selectVariable()
{
if (!goodVarsSet.empty())
{
if (std::uniform_real_distribution<double>(0., 1.)(rng) < randomVarProbability)
return goodVarsSet[rng() % goodVarsSet.size()];
auto sampleSize = std::min(maxMovesToEvaluate, goodVarsSet.size());
totalEffort += sampleSize;
double bestScore = -std::numeric_limits<double>::infinity();
uint32_t bestVar = UINT_MAX;
for (size_t i = 0; i < sampleSize; i++)
{
auto setidx = rng() % goodVarsSet.size();
auto varIdx = goodVarsSet[setidx];
// assert(goodVarsSetIdx[varIdx] >= 0 && goodVarsSetIdx[varIdx] == setidx);
Move move = bestMove(varIdx);
// assert(move.score > equalityTolerance);
if (move.score > bestScore)
{
bestScore = move.score;
bestVar = varIdx;
}
}
assert(bestVar != UINT_MAX);
return bestVar;
}
// Local minimum, update weights.
updateWeights();
if (!problem.violatedConstraints.empty())
{
size_t cstrIdx = problem.violatedConstraints[rng() % problem.violatedConstraints.size()];
auto &constraint = problem.constraints[cstrIdx];
if (std::uniform_real_distribution<double>(0., 1.)(rng) < randomCellProbability)
return constraint.coeffs[rng() % constraint.coeffs.size()].idx;
double bestScore = -std::numeric_limits<double>::infinity();
uint32_t bestVarIdx = UINT_MAX;
for (auto &cell : constraint.coeffs)
{
Move move = bestMove(cell.idx);
if (move.score > bestScore)
{
bestScore = move.score;
bestVarIdx = cell.idx;
}
}
return bestVarIdx;
}
// Fallback to random choice.
return rng() % problem.vars.size();
}
void updateWeights()
{
if (verbosity >= 2)
printf(FJ_LOG_PREFIX "Reached a local minimum.\n");
nBumps += 1;
bool rescaleAllWeights = false;
size_t dt = 0;
if (problem.violatedConstraints.empty())
{
objectiveWeight += weightUpdateIncrement;
if (objectiveWeight > 1.0e20)
rescaleAllWeights = true;
dt += problem.vars.size();
for (size_t varIdx = 0; varIdx < problem.vars.size(); varIdx += 1)
forEachMove(
varIdx, [&](Move &move)
{ move.score += weightUpdateIncrement *
problem.vars[varIdx].objectiveCoeff *
(move.value - problem.incumbentAssignment[varIdx]); });
}
else
{
for (auto &cIdx : problem.violatedConstraints)
{
auto &constraint = problem.constraints[cIdx];
constraint.weight += weightUpdateIncrement;
if (constraint.weight > 1.0e20)
rescaleAllWeights = true;
dt += constraint.coeffs.size();
for (auto &cell : constraint.coeffs)
{
forEachMove(
cell.idx, [&](Move &move)
{
double candidateLhs = constraint.incumbentLhs + cell.coeff * (move.value - problem.incumbentAssignment[cell.idx]);
double diff = weightUpdateIncrement * (constraint.score(candidateLhs) -
constraint.score(constraint.incumbentLhs));
move.score += diff; });
updateGoodMoves(cell.idx);
}
}
}
weightUpdateIncrement /= weightUpdateDecay;
if (rescaleAllWeights)
{
weightUpdateIncrement *= 1.0e-20;
objectiveWeight *= 1.0e-20;
for (auto &c : problem.constraints)
c.weight *= 1.0e-20;
dt += problem.constraints.size();
for (size_t i = 0; i < problem.vars.size(); i += 1)
resetMoves(i);
}
totalEffort += dt;
}
Move bestMove(uint32_t varIdx)
{
Move best = Move::undef();
forEachMove(varIdx, [&](Move &move)
{ if (move.score > best.score)
best = move; });
return best;
}
void doVariableMove(uint32_t varIdx)
{
// First, we get the best move for the variable;
auto m = bestMove(varIdx);
auto newValue = m.value;
// assert(!isnan(newValue));
// Update the incumbent solution.
// printf("Setting var %d from %g to %g for a score of %g\n", varIdx, oldValue, newValue, m.score);
totalEffort += problem.setValue(
varIdx, newValue, [&](LhsModification mod)
{
forEachMove(mod.varIdx, [&](Move &m)
{ modifyMove(mod, problem, m); });
updateGoodMoves(mod.varIdx); });
resetMoves(varIdx);
}
void updateGoodMoves(int32_t varIdx)
{
bool anyGoodMoves = bestMove(varIdx).score > 0.;
if (anyGoodMoves && goodVarsSetIdx[varIdx] == -1)
{
// Became good, add to good set.
goodVarsSetIdx[varIdx] = goodVarsSet.size();
goodVarsSet.push_back(varIdx);
}
else if (!anyGoodMoves && goodVarsSetIdx[varIdx] != -1)
{
// Became bad, remove from good set.
auto lastSetIdx = goodVarsSet.size() - 1;
auto lastVarIdx = goodVarsSet[lastSetIdx];
auto thisSetIdx = goodVarsSetIdx[varIdx];
std::swap(goodVarsSet[thisSetIdx], goodVarsSet[lastSetIdx]);
goodVarsSetIdx[lastVarIdx] = thisSetIdx;
goodVarsSetIdx[varIdx] = -1;
goodVarsSet.pop_back();
}
}
template <typename F>
void forEachMove(int32_t varIdx, F f)
{
jumpMove.forEachVarMove(varIdx, f);
// TODO: here, we can add more move types.
// upDownMove.forEachVarMove(varIdx, f);
}
void resetMoves(uint32_t varIdx)
{
totalEffort += problem.vars[varIdx].coeffs.size();
jumpMove.updateValue(problem, varIdx);
forEachMove(
varIdx, [&](Move &move)
{
move.score = 0.0;
move.score += objectiveWeight *
problem.vars[varIdx].objectiveCoeff *
(move.value - problem.incumbentAssignment[varIdx]);
for (auto &cell : problem.vars[varIdx].coeffs)
{
auto &constraint = problem.constraints[cell.idx];
auto candidateLhs = constraint.incumbentLhs +
cell.coeff *
(move.value - problem.incumbentAssignment[varIdx]);
move.score += constraint.weight *
(constraint.score(candidateLhs) - constraint.score(constraint.incumbentLhs));
} });
updateGoodMoves(varIdx);
}
bool user_terminate(std::function<CallbackControlFlow(FJStatus)> callback, double *solution)
{
const int CALLBACK_EFFORT = 500000;
if (solution != nullptr || totalEffort - effortAtLastCallback > CALLBACK_EFFORT)
{
if (verbosity >= 2)
printf(FJ_LOG_PREFIX "calling user termination.\n");
effortAtLastCallback = totalEffort;
FJStatus status;
status.totalEffort = totalEffort;
status.effortSinceLastImprovement = totalEffort - effortAtLastImprovement;
status.solution = solution;
status.numVars = problem.vars.size();
status.solutionObjectiveValue = problem.incumbentObjective;
auto result = callback(status);
if (result == CallbackControlFlow::Terminate)
{
if (verbosity >= 2)
printf(FJ_LOG_PREFIX "quitting.\n");
return true;
}
}
return false;
}
};