forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lp_utils.cc
1227 lines (1097 loc) · 46.4 KB
/
lp_utils.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
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/sat/lp_utils.h"
#include <stdlib.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <limits>
#include <string>
#include <vector>
#include "absl/strings/str_cat.h"
#include "ortools/base/int_type.h"
#include "ortools/base/integral_types.h"
#include "ortools/base/logging.h"
#include "ortools/glop/lp_solver.h"
#include "ortools/glop/parameters.pb.h"
#include "ortools/lp_data/lp_types.h"
#include "ortools/sat/boolean_problem.h"
#include "ortools/sat/cp_model_utils.h"
#include "ortools/sat/integer.h"
#include "ortools/sat/sat_base.h"
#include "ortools/util/fp_utils.h"
namespace operations_research {
namespace sat {
using glop::ColIndex;
using glop::Fractional;
using glop::kInfinity;
using glop::RowIndex;
using operations_research::MPConstraintProto;
using operations_research::MPModelProto;
using operations_research::MPVariableProto;
namespace {
void ScaleConstraint(const std::vector<double>& var_scaling,
MPConstraintProto* mp_constraint) {
const int num_terms = mp_constraint->coefficient_size();
for (int i = 0; i < num_terms; ++i) {
const int var_index = mp_constraint->var_index(i);
mp_constraint->set_coefficient(
i, mp_constraint->coefficient(i) / var_scaling[var_index]);
}
}
void ApplyVarScaling(const std::vector<double> var_scaling,
MPModelProto* mp_model) {
const int num_variables = mp_model->variable_size();
for (int i = 0; i < num_variables; ++i) {
const double scaling = var_scaling[i];
const MPVariableProto& mp_var = mp_model->variable(i);
const double old_lb = mp_var.lower_bound();
const double old_ub = mp_var.upper_bound();
const double old_obj = mp_var.objective_coefficient();
mp_model->mutable_variable(i)->set_lower_bound(old_lb * scaling);
mp_model->mutable_variable(i)->set_upper_bound(old_ub * scaling);
mp_model->mutable_variable(i)->set_objective_coefficient(old_obj / scaling);
}
for (MPConstraintProto& mp_constraint : *mp_model->mutable_constraint()) {
ScaleConstraint(var_scaling, &mp_constraint);
}
for (MPGeneralConstraintProto& general_constraint :
*mp_model->mutable_general_constraint()) {
switch (general_constraint.general_constraint_case()) {
case MPGeneralConstraintProto::kIndicatorConstraint:
ScaleConstraint(var_scaling,
general_constraint.mutable_indicator_constraint()
->mutable_constraint());
break;
case MPGeneralConstraintProto::kAndConstraint:
case MPGeneralConstraintProto::kOrConstraint:
// These constraints have only Boolean variables and no constants. They
// don't need scaling.
break;
default:
LOG(FATAL) << "Scaling unsupported for general constraint of type "
<< general_constraint.general_constraint_case();
}
}
}
} // namespace
std::vector<double> ScaleContinuousVariables(double scaling, double max_bound,
MPModelProto* mp_model) {
const int num_variables = mp_model->variable_size();
std::vector<double> var_scaling(num_variables, 1.0);
for (int i = 0; i < num_variables; ++i) {
if (mp_model->variable(i).is_integer()) continue;
const double lb = mp_model->variable(i).lower_bound();
const double ub = mp_model->variable(i).upper_bound();
const double magnitude = std::max(std::abs(lb), std::abs(ub));
if (magnitude == 0 || magnitude > max_bound) continue;
var_scaling[i] = std::min(scaling, max_bound / magnitude);
}
ApplyVarScaling(var_scaling, mp_model);
return var_scaling;
}
// This uses the best rational approximation of x via continuous fractions. It
// is probably not the best implementation, but according to the unit test, it
// seems to do the job.
int FindRationalFactor(double x, int limit, double tolerance) {
const double initial_x = x;
x = std::abs(x);
x -= std::floor(x);
int q = 1;
int prev_q = 0;
while (q < limit) {
if (std::abs(q * initial_x - std::round(q * initial_x)) < q * tolerance) {
return q;
}
x = 1 / x;
const int new_q = prev_q + static_cast<int>(std::floor(x)) * q;
prev_q = q;
q = new_q;
x -= std::floor(x);
}
return 0;
}
namespace {
// Returns a factor such that factor * var only need to take integer values to
// satisfy the given constraint. Return 0.0 if we didn't find such factor.
//
// Precondition: var must be the only non-integer in the given constraint.
double GetIntegralityMultiplier(const MPModelProto& mp_model,
const std::vector<double>& var_scaling, int var,
int ct_index, double tolerance) {
DCHECK(!mp_model.variable(var).is_integer());
const MPConstraintProto& ct = mp_model.constraint(ct_index);
double multiplier = 1.0;
double var_coeff = 0.0;
const double max_multiplier = 1e4;
for (int i = 0; i < ct.var_index().size(); ++i) {
if (var == ct.var_index(i)) {
var_coeff = ct.coefficient(i);
continue;
}
DCHECK(mp_model.variable(ct.var_index(i)).is_integer());
// This actually compute the smallest multiplier to make all other
// terms in the constraint integer.
const double coeff =
multiplier * ct.coefficient(i) / var_scaling[ct.var_index(i)];
multiplier *=
FindRationalFactor(coeff, /*limit=*/100, multiplier * tolerance);
if (multiplier == 0 || multiplier > max_multiplier) return 0.0;
}
DCHECK_NE(var_coeff, 0.0);
// The constraint bound need to be infinite or integer.
for (const double bound : {ct.lower_bound(), ct.upper_bound()}) {
if (!std::isfinite(bound)) continue;
if (std::abs(std::round(bound * multiplier) - bound * multiplier) >
tolerance * multiplier) {
return 0.0;
}
}
return std::abs(multiplier * var_coeff);
}
} // namespace
void RemoveNearZeroTerms(const SatParameters& params, MPModelProto* mp_model,
SolverLogger* logger) {
const int num_variables = mp_model->variable_size();
// Compute for each variable its current maximum magnitude. Note that we will
// only scale variable with a coefficient >= 1, so it is safe to use this
// bound.
std::vector<double> max_bounds(num_variables);
for (int i = 0; i < num_variables; ++i) {
double value = std::abs(mp_model->variable(i).lower_bound());
value = std::max(value, std::abs(mp_model->variable(i).upper_bound()));
value = std::min(value, params.mip_max_bound());
max_bounds[i] = value;
}
// We want the maximum absolute error while setting coefficients to zero to
// not exceed our mip wanted precision. So for a binary variable we might set
// to zero coefficient around 1e-7. But for large domain, we need lower coeff
// than that, around 1e-12 with the default params.mip_max_bound(). This also
// depends on the size of the constraint.
int64_t num_removed = 0;
double largest_removed = 0.0;
const int num_constraints = mp_model->constraint_size();
for (int c = 0; c < num_constraints; ++c) {
MPConstraintProto* ct = mp_model->mutable_constraint(c);
int new_size = 0;
const int size = ct->var_index().size();
if (size == 0) continue;
const double threshold =
params.mip_wanted_precision() / static_cast<double>(size);
for (int i = 0; i < size; ++i) {
const int var = ct->var_index(i);
const double coeff = ct->coefficient(i);
if (std::abs(coeff) * max_bounds[var] < threshold) {
largest_removed = std::max(largest_removed, std::abs(coeff));
continue;
}
ct->set_var_index(new_size, var);
ct->set_coefficient(new_size, coeff);
++new_size;
}
num_removed += size - new_size;
ct->mutable_var_index()->Truncate(new_size);
ct->mutable_coefficient()->Truncate(new_size);
}
if (num_removed > 0) {
SOLVER_LOG(logger, "Removed ", num_removed,
" near zero terms with largest magnitude of ", largest_removed,
".");
}
}
std::vector<double> DetectImpliedIntegers(MPModelProto* mp_model,
SolverLogger* logger) {
const int num_variables = mp_model->variable_size();
std::vector<double> var_scaling(num_variables, 1.0);
int initial_num_integers = 0;
for (int i = 0; i < num_variables; ++i) {
if (mp_model->variable(i).is_integer()) ++initial_num_integers;
}
VLOG(1) << "Initial num integers: " << initial_num_integers;
// We will process all equality constraints with exactly one non-integer.
const double tolerance = 1e-6;
std::vector<int> constraint_queue;
const int num_constraints = mp_model->constraint_size();
std::vector<int> constraint_to_num_non_integer(num_constraints, 0);
std::vector<std::vector<int>> var_to_constraints(num_variables);
for (int i = 0; i < num_constraints; ++i) {
const MPConstraintProto& mp_constraint = mp_model->constraint(i);
for (const int var : mp_constraint.var_index()) {
if (!mp_model->variable(var).is_integer()) {
var_to_constraints[var].push_back(i);
constraint_to_num_non_integer[i]++;
}
}
if (constraint_to_num_non_integer[i] == 1) {
constraint_queue.push_back(i);
}
}
VLOG(1) << "Initial constraint queue: " << constraint_queue.size() << " / "
<< num_constraints;
int num_detected = 0;
double max_scaling = 0.0;
auto scale_and_mark_as_integer = [&](int var, double scaling) mutable {
CHECK_NE(var, -1);
CHECK(!mp_model->variable(var).is_integer());
CHECK_EQ(var_scaling[var], 1.0);
if (scaling != 1.0) {
VLOG(2) << "Scaled " << var << " by " << scaling;
}
++num_detected;
max_scaling = std::max(max_scaling, scaling);
// Scale the variable right away and mark it as implied integer.
// Note that the constraints will be scaled later.
var_scaling[var] = scaling;
mp_model->mutable_variable(var)->set_is_integer(true);
// Update the queue of constraints with a single non-integer.
for (const int ct_index : var_to_constraints[var]) {
constraint_to_num_non_integer[ct_index]--;
if (constraint_to_num_non_integer[ct_index] == 1) {
constraint_queue.push_back(ct_index);
}
}
};
int num_fail_due_to_rhs = 0;
int num_fail_due_to_large_multiplier = 0;
int num_processed_constraints = 0;
while (!constraint_queue.empty()) {
const int top_ct_index = constraint_queue.back();
constraint_queue.pop_back();
// The non integer variable was already made integer by one other
// constraint.
if (constraint_to_num_non_integer[top_ct_index] == 0) continue;
// Ignore non-equality here.
const MPConstraintProto& ct = mp_model->constraint(top_ct_index);
if (ct.lower_bound() + tolerance < ct.upper_bound()) continue;
++num_processed_constraints;
// This will be set to the unique non-integer term of this constraint.
int var = -1;
double var_coeff;
// We are looking for a "multiplier" so that the unique non-integer term
// in this constraint (i.e. var * var_coeff) times this multiplier is an
// integer.
//
// If this is set to zero or becomes too large, we fail to detect a new
// implied integer and ignore this constraint.
double multiplier = 1.0;
const double max_multiplier = 1e4;
for (int i = 0; i < ct.var_index().size(); ++i) {
if (!mp_model->variable(ct.var_index(i)).is_integer()) {
CHECK_EQ(var, -1);
var = ct.var_index(i);
var_coeff = ct.coefficient(i);
} else {
// This actually compute the smallest multiplier to make all other
// terms in the constraint integer.
const double coeff =
multiplier * ct.coefficient(i) / var_scaling[ct.var_index(i)];
multiplier *=
FindRationalFactor(coeff, /*limit=*/100, multiplier * tolerance);
if (multiplier == 0 || multiplier > max_multiplier) {
break;
}
}
}
if (multiplier == 0 || multiplier > max_multiplier) {
++num_fail_due_to_large_multiplier;
continue;
}
// These "rhs" fail could be handled by shifting the variable.
const double rhs = ct.lower_bound();
if (std::abs(std::round(rhs * multiplier) - rhs * multiplier) >
tolerance * multiplier) {
++num_fail_due_to_rhs;
continue;
}
// We want to multiply the variable so that it is integer. We know that
// coeff * multiplier is an integer, so we just multiply by that.
//
// But if a variable appear in more than one equality, we want to find the
// smallest integrality factor! See diameterc-msts-v40a100d5i.mps
// for an instance of this.
double best_scaling = std::abs(var_coeff * multiplier);
for (const int ct_index : var_to_constraints[var]) {
if (ct_index == top_ct_index) continue;
if (constraint_to_num_non_integer[ct_index] != 1) continue;
// Ignore non-equality here.
const MPConstraintProto& ct = mp_model->constraint(top_ct_index);
if (ct.lower_bound() + tolerance < ct.upper_bound()) continue;
const double multiplier = GetIntegralityMultiplier(
*mp_model, var_scaling, var, ct_index, tolerance);
if (multiplier != 0.0 && multiplier < best_scaling) {
best_scaling = multiplier;
}
}
scale_and_mark_as_integer(var, best_scaling);
}
// Process continuous variables that only appear as the unique non integer
// in a set of non-equality constraints.
//
// Note that turning to integer such variable cannot in turn trigger new
// integer detection, so there is no point doing that in a loop.
int num_in_inequalities = 0;
int num_to_be_handled = 0;
for (int var = 0; var < num_variables; ++var) {
if (mp_model->variable(var).is_integer()) continue;
// This should be presolved and not happen.
if (var_to_constraints[var].empty()) continue;
bool ok = true;
for (const int ct_index : var_to_constraints[var]) {
if (constraint_to_num_non_integer[ct_index] != 1) {
ok = false;
break;
}
}
if (!ok) continue;
std::vector<double> scaled_coeffs;
for (const int ct_index : var_to_constraints[var]) {
const double multiplier = GetIntegralityMultiplier(
*mp_model, var_scaling, var, ct_index, tolerance);
if (multiplier == 0.0) {
ok = false;
break;
}
scaled_coeffs.push_back(multiplier);
}
if (!ok) continue;
// The situation is a bit tricky here, we have a bunch of coeffs c_i, and we
// know that X * c_i can take integer value without changing the constraint
// i meaning.
//
// For now we take the min, and scale only if all c_i / min are integer.
double scaling = scaled_coeffs[0];
for (const double c : scaled_coeffs) {
scaling = std::min(scaling, c);
}
CHECK_GT(scaling, 0.0);
for (const double c : scaled_coeffs) {
const double fraction = c / scaling;
if (std::abs(std::round(fraction) - fraction) > tolerance) {
ok = false;
break;
}
}
if (!ok) {
// TODO(user): be smarter! we should be able to handle these cases.
++num_to_be_handled;
continue;
}
// Tricky, we also need the bound of the scaled variable to be integer.
for (const double bound : {mp_model->variable(var).lower_bound(),
mp_model->variable(var).upper_bound()}) {
if (!std::isfinite(bound)) continue;
if (std::abs(std::round(bound * scaling) - bound * scaling) >
tolerance * scaling) {
ok = false;
break;
}
}
if (!ok) {
// TODO(user): If we scale more we migth be able to turn it into an
// integer.
++num_to_be_handled;
continue;
}
++num_in_inequalities;
scale_and_mark_as_integer(var, scaling);
}
VLOG(1) << "num_new_integer: " << num_detected
<< " num_processed_constraints: " << num_processed_constraints
<< " num_rhs_fail: " << num_fail_due_to_rhs
<< " num_multiplier_fail: " << num_fail_due_to_large_multiplier;
if (num_to_be_handled > 0) {
SOLVER_LOG(logger, "Missed ", num_to_be_handled,
" potential implied integer.");
}
const int num_integers = initial_num_integers + num_detected;
SOLVER_LOG(logger, "Num integers: ", num_integers, "/", num_variables,
" (implied: ", num_detected,
" in_inequalities: ", num_in_inequalities,
" max_scaling: ", max_scaling, ")",
(num_integers == num_variables ? " [IP] " : " [MIP] "));
ApplyVarScaling(var_scaling, mp_model);
return var_scaling;
}
namespace {
// We use a class to reuse the temporay memory.
struct ConstraintScaler {
// Scales an individual constraint.
ConstraintProto* AddConstraint(const MPModelProto& mp_model,
const MPConstraintProto& mp_constraint,
CpModelProto* cp_model);
double max_relative_coeff_error = 0.0;
double max_relative_rhs_error = 0.0;
double max_scaling_factor = 0.0;
double wanted_precision = 1e-6;
int64_t scaling_target = int64_t{1} << 50;
std::vector<int> var_indices;
std::vector<double> coefficients;
std::vector<double> lower_bounds;
std::vector<double> upper_bounds;
};
namespace {
double FindFractionalScaling(const std::vector<double>& coefficients,
double tolerance) {
double multiplier = 1.0;
for (const double coeff : coefficients) {
multiplier *=
FindRationalFactor(coeff, /*limit=*/1e8, multiplier * tolerance);
if (multiplier == 0.0) break;
}
return multiplier;
}
} // namespace
ConstraintProto* ConstraintScaler::AddConstraint(
const MPModelProto& mp_model, const MPConstraintProto& mp_constraint,
CpModelProto* cp_model) {
if (mp_constraint.lower_bound() == -kInfinity &&
mp_constraint.upper_bound() == kInfinity) {
return nullptr;
}
auto* constraint = cp_model->add_constraints();
constraint->set_name(mp_constraint.name());
auto* arg = constraint->mutable_linear();
// First scale the coefficients of the constraints so that the constraint
// sum can always be computed without integer overflow.
var_indices.clear();
coefficients.clear();
lower_bounds.clear();
upper_bounds.clear();
const int num_coeffs = mp_constraint.coefficient_size();
for (int i = 0; i < num_coeffs; ++i) {
const auto& var_proto = cp_model->variables(mp_constraint.var_index(i));
const int64_t lb = var_proto.domain(0);
const int64_t ub = var_proto.domain(var_proto.domain_size() - 1);
if (lb == 0 && ub == 0) continue;
const double coeff = mp_constraint.coefficient(i);
if (coeff == 0.0) continue;
var_indices.push_back(mp_constraint.var_index(i));
coefficients.push_back(coeff);
lower_bounds.push_back(lb);
upper_bounds.push_back(ub);
}
// We compute the worst case error relative to the magnitude of the bounds.
Fractional lb = mp_constraint.lower_bound();
Fractional ub = mp_constraint.upper_bound();
const double ct_norm = std::max(1.0, std::min(std::abs(lb), std::abs(ub)));
double scaling_factor = GetBestScalingOfDoublesToInt64(
coefficients, lower_bounds, upper_bounds, scaling_target);
// Returns the smallest factor of the form 2^i that gives us a relative sum
// error of wanted_precision and still make sure we will have no integer
// overflow.
//
// TODO(user): Make this faster.
double x = std::min(scaling_factor, 1.0);
double relative_coeff_error;
double scaled_sum_error;
for (; x <= scaling_factor; x *= 2) {
ComputeScalingErrors(coefficients, lower_bounds, upper_bounds, x,
&relative_coeff_error, &scaled_sum_error);
if (scaled_sum_error < wanted_precision * x * ct_norm) break;
}
scaling_factor = x;
// Because we deal with an approximate input, scaling with a power of 2 might
// not be the best choice. It is also possible user used rational coeff and
// then converted them to double (1/2, 1/3, 4/5, etc...). This scaling will
// recover such rational input and might result in a smaller overall
// coefficient which is good.
const double integer_factor = FindFractionalScaling(coefficients, 1e-8);
if (integer_factor != 0 && integer_factor < scaling_factor) {
ComputeScalingErrors(coefficients, lower_bounds, upper_bounds, x,
&relative_coeff_error, &scaled_sum_error);
if (scaled_sum_error < wanted_precision * integer_factor * ct_norm) {
scaling_factor = integer_factor;
}
}
const int64_t gcd = ComputeGcdOfRoundedDoubles(coefficients, scaling_factor);
max_relative_coeff_error =
std::max(relative_coeff_error, max_relative_coeff_error);
max_scaling_factor = std::max(scaling_factor / gcd, max_scaling_factor);
// We do not relax the constraint bound if all variables are integer and
// we made no error at all during our scaling.
bool relax_bound = scaled_sum_error > 0;
for (int i = 0; i < coefficients.size(); ++i) {
const double scaled_value = coefficients[i] * scaling_factor;
const int64_t value = static_cast<int64_t>(std::round(scaled_value)) / gcd;
if (value != 0) {
if (!mp_model.variable(var_indices[i]).is_integer()) {
relax_bound = true;
}
arg->add_vars(var_indices[i]);
arg->add_coeffs(value);
}
}
max_relative_rhs_error = std::max(
max_relative_rhs_error, scaled_sum_error / (scaling_factor * ct_norm));
// Add the constraint bounds. Because we are sure the scaled constraint fit
// on an int64_t, if the scaled bounds are too large, the constraint is either
// always true or always false.
if (relax_bound) {
lb -= std::max(std::abs(lb), 1.0) * wanted_precision;
}
const Fractional scaled_lb = std::ceil(lb * scaling_factor);
if (lb == -kInfinity || scaled_lb <= std::numeric_limits<int64_t>::min()) {
arg->add_domain(std::numeric_limits<int64_t>::min());
} else {
arg->add_domain(CeilRatio(IntegerValue(static_cast<int64_t>(scaled_lb)),
IntegerValue(gcd))
.value());
}
if (relax_bound) {
ub += std::max(std::abs(ub), 1.0) * wanted_precision;
}
const Fractional scaled_ub = std::floor(ub * scaling_factor);
if (ub == kInfinity || scaled_ub >= std::numeric_limits<int64_t>::max()) {
arg->add_domain(std::numeric_limits<int64_t>::max());
} else {
arg->add_domain(FloorRatio(IntegerValue(static_cast<int64_t>(scaled_ub)),
IntegerValue(gcd))
.value());
}
return constraint;
}
} // namespace
bool ConvertMPModelProtoToCpModelProto(const SatParameters& params,
const MPModelProto& mp_model,
CpModelProto* cp_model,
SolverLogger* logger) {
CHECK(cp_model != nullptr);
cp_model->Clear();
cp_model->set_name(mp_model.name());
// To make sure we cannot have integer overflow, we use this bound for any
// unbounded variable.
//
// TODO(user): This could be made larger if needed, so be smarter if we have
// MIP problem that we cannot "convert" because of this. Note however than we
// cannot go that much further because we need to make sure we will not run
// into overflow if we add a big linear combination of such variables. It
// should always be possible for a user to scale its problem so that all
// relevant quantities are a couple of millions. A LP/MIP solver have a
// similar condition in disguise because problem with a difference of more
// than 6 magnitudes between the variable values will likely run into numeric
// trouble.
const int64_t kMaxVariableBound =
static_cast<int64_t>(params.mip_max_bound());
int num_truncated_bounds = 0;
int num_small_domains = 0;
const int64_t kSmallDomainSize = 1000;
const double kWantedPrecision = params.mip_wanted_precision();
// Add the variables.
const int num_variables = mp_model.variable_size();
for (int i = 0; i < num_variables; ++i) {
const MPVariableProto& mp_var = mp_model.variable(i);
IntegerVariableProto* cp_var = cp_model->add_variables();
cp_var->set_name(mp_var.name());
// Deal with the corner case of a domain far away from zero.
//
// TODO(user): We should deal with these case by shifting the domain so
// that it includes zero instead of just fixing the variable. But that is a
// bit of work as it requires some postsolve.
if (mp_var.lower_bound() > kMaxVariableBound) {
// Fix var to its lower bound.
++num_truncated_bounds;
const int64_t value =
static_cast<int64_t>(std::round(mp_var.lower_bound()));
cp_var->add_domain(value);
cp_var->add_domain(value);
continue;
} else if (mp_var.upper_bound() < -kMaxVariableBound) {
// Fix var to its upper_bound.
++num_truncated_bounds;
const int64_t value =
static_cast<int64_t>(std::round(mp_var.upper_bound()));
cp_var->add_domain(value);
cp_var->add_domain(value);
continue;
}
// Note that we must process the lower bound first.
for (const bool lower : {true, false}) {
const double bound = lower ? mp_var.lower_bound() : mp_var.upper_bound();
if (std::abs(bound) >= kMaxVariableBound) {
++num_truncated_bounds;
cp_var->add_domain(bound < 0 ? -kMaxVariableBound : kMaxVariableBound);
continue;
}
// Note that the cast is "perfect" because we forbid large values.
cp_var->add_domain(
static_cast<int64_t>(lower ? std::ceil(bound - kWantedPrecision)
: std::floor(bound + kWantedPrecision)));
}
if (cp_var->domain(0) > cp_var->domain(1)) {
LOG(WARNING) << "Variable #" << i << " cannot take integer value. "
<< mp_var.ShortDebugString();
return false;
}
// Notify if a continuous variable has a small domain as this is likely to
// make an all integer solution far from a continuous one.
if (!mp_var.is_integer() && cp_var->domain(0) != cp_var->domain(1) &&
cp_var->domain(1) - cp_var->domain(0) < kSmallDomainSize) {
++num_small_domains;
}
}
LOG_IF(WARNING, num_truncated_bounds > 0)
<< num_truncated_bounds << " bounds were truncated to "
<< kMaxVariableBound << ".";
LOG_IF(WARNING, num_small_domains > 0)
<< num_small_domains << " continuous variable domain with fewer than "
<< kSmallDomainSize << " values.";
ConstraintScaler scaler;
const int64_t kScalingTarget = int64_t{1}
<< params.mip_max_activity_exponent();
scaler.wanted_precision = kWantedPrecision;
scaler.scaling_target = kScalingTarget;
// Add the constraints. We scale each of them individually.
for (const MPConstraintProto& mp_constraint : mp_model.constraint()) {
scaler.AddConstraint(mp_model, mp_constraint, cp_model);
}
for (const MPGeneralConstraintProto& general_constraint :
mp_model.general_constraint()) {
switch (general_constraint.general_constraint_case()) {
case MPGeneralConstraintProto::kIndicatorConstraint: {
const auto& indicator_constraint =
general_constraint.indicator_constraint();
const MPConstraintProto& mp_constraint =
indicator_constraint.constraint();
ConstraintProto* ct =
scaler.AddConstraint(mp_model, mp_constraint, cp_model);
if (ct == nullptr) continue;
// Add the indicator.
const int var = indicator_constraint.var_index();
const int value = indicator_constraint.var_value();
ct->add_enforcement_literal(value == 1 ? var : NegatedRef(var));
break;
}
case MPGeneralConstraintProto::kAndConstraint: {
const auto& and_constraint = general_constraint.and_constraint();
const std::string& name = general_constraint.name();
ConstraintProto* ct_pos = cp_model->add_constraints();
ct_pos->set_name(name.empty() ? "" : absl::StrCat(name, "_pos"));
ct_pos->add_enforcement_literal(and_constraint.resultant_var_index());
*ct_pos->mutable_bool_and()->mutable_literals() =
and_constraint.var_index();
ConstraintProto* ct_neg = cp_model->add_constraints();
ct_neg->set_name(name.empty() ? "" : absl::StrCat(name, "_neg"));
ct_neg->add_enforcement_literal(
NegatedRef(and_constraint.resultant_var_index()));
for (const int var_index : and_constraint.var_index()) {
ct_neg->mutable_bool_or()->add_literals(NegatedRef(var_index));
}
break;
}
case MPGeneralConstraintProto::kOrConstraint: {
const auto& or_constraint = general_constraint.or_constraint();
const std::string& name = general_constraint.name();
ConstraintProto* ct_pos = cp_model->add_constraints();
ct_pos->set_name(name.empty() ? "" : absl::StrCat(name, "_pos"));
ct_pos->add_enforcement_literal(or_constraint.resultant_var_index());
*ct_pos->mutable_bool_or()->mutable_literals() =
or_constraint.var_index();
ConstraintProto* ct_neg = cp_model->add_constraints();
ct_neg->set_name(name.empty() ? "" : absl::StrCat(name, "_neg"));
ct_neg->add_enforcement_literal(
NegatedRef(or_constraint.resultant_var_index()));
for (const int var_index : or_constraint.var_index()) {
ct_neg->mutable_bool_and()->add_literals(NegatedRef(var_index));
}
break;
}
default:
LOG(ERROR) << "Can't convert general constraints of type "
<< general_constraint.general_constraint_case()
<< " to CpModelProto.";
return false;
}
}
// Display the error/scaling on the constraints.
SOLVER_LOG(logger, "Maximum constraint coefficient relative error: ",
scaler.max_relative_coeff_error);
SOLVER_LOG(logger, "Maximum constraint worst-case activity relative error: ",
scaler.max_relative_rhs_error,
(scaler.max_relative_rhs_error > params.mip_check_precision()
? " [Potentially IMPRECISE]"
: ""));
SOLVER_LOG(logger,
"Maximum constraint scaling factor: ", scaler.max_scaling_factor);
// Add the objective.
std::vector<int> var_indices;
std::vector<double> coefficients;
std::vector<double> lower_bounds;
std::vector<double> upper_bounds;
double min_magnitude = std::numeric_limits<double>::infinity();
double max_magnitude = 0.0;
double l1_norm = 0.0;
for (int i = 0; i < num_variables; ++i) {
const MPVariableProto& mp_var = mp_model.variable(i);
if (mp_var.objective_coefficient() == 0.0) continue;
const auto& var_proto = cp_model->variables(i);
const int64_t lb = var_proto.domain(0);
const int64_t ub = var_proto.domain(var_proto.domain_size() - 1);
if (lb == 0 && ub == 0) continue;
var_indices.push_back(i);
coefficients.push_back(mp_var.objective_coefficient());
lower_bounds.push_back(lb);
upper_bounds.push_back(ub);
min_magnitude = std::min(min_magnitude, std::abs(coefficients.back()));
max_magnitude = std::max(max_magnitude, std::abs(coefficients.back()));
l1_norm += std::abs(coefficients.back());
}
if (!coefficients.empty()) {
const double average_magnitude =
l1_norm / static_cast<double>(coefficients.size());
SOLVER_LOG(logger, "Objective magnitude in [", min_magnitude, ", ",
max_magnitude, "] average = ", average_magnitude);
}
if (!coefficients.empty() || mp_model.objective_offset() != 0.0) {
double scaling_factor = GetBestScalingOfDoublesToInt64(
coefficients, lower_bounds, upper_bounds, kScalingTarget);
// Returns the smallest factor of the form 2^i that gives us an absolute
// error of kWantedPrecision and still make sure we will have no integer
// overflow.
//
// TODO(user): Make this faster.
double x = std::min(scaling_factor, 1.0);
double relative_coeff_error;
double scaled_sum_error;
for (; x <= scaling_factor; x *= 2) {
ComputeScalingErrors(coefficients, lower_bounds, upper_bounds, x,
&relative_coeff_error, &scaled_sum_error);
if (scaled_sum_error < kWantedPrecision * x) break;
}
scaling_factor = x;
// Same remark as for the constraint.
// TODO(user): Extract common code.
const double integer_factor = FindFractionalScaling(coefficients, 1e-8);
if (integer_factor != 0 && integer_factor < scaling_factor) {
ComputeScalingErrors(coefficients, lower_bounds, upper_bounds, x,
&relative_coeff_error, &scaled_sum_error);
if (scaled_sum_error < kWantedPrecision * integer_factor) {
scaling_factor = integer_factor;
}
}
const int64_t gcd =
ComputeGcdOfRoundedDoubles(coefficients, scaling_factor);
// Display the objective error/scaling.
SOLVER_LOG(
logger, "Objective coefficient relative error: ", relative_coeff_error,
(relative_coeff_error > params.mip_check_precision() ? " [IMPRECISE]"
: ""));
SOLVER_LOG(logger, "Objective worst-case absolute error: ",
scaled_sum_error / scaling_factor);
SOLVER_LOG(logger, "Objective scaling factor: ", scaling_factor / gcd);
// Note that here we set the scaling factor for the inverse operation of
// getting the "true" objective value from the scaled one. Hence the
// inverse.
auto* objective = cp_model->mutable_objective();
const int mult = mp_model.maximize() ? -1 : 1;
objective->set_offset(mp_model.objective_offset() * scaling_factor / gcd *
mult);
objective->set_scaling_factor(1.0 / scaling_factor * gcd * mult);
for (int i = 0; i < coefficients.size(); ++i) {
const int64_t value =
static_cast<int64_t>(std::round(coefficients[i] * scaling_factor)) /
gcd;
if (value != 0) {
objective->add_vars(var_indices[i]);
objective->add_coeffs(value * mult);
}
}
}
return true;
}
bool ConvertBinaryMPModelProtoToBooleanProblem(const MPModelProto& mp_model,
LinearBooleanProblem* problem) {
CHECK(problem != nullptr);
problem->Clear();
problem->set_name(mp_model.name());
const int num_variables = mp_model.variable_size();
problem->set_num_variables(num_variables);
// Test if the variables are binary variables.
// Add constraints for the fixed variables.
for (int var_id(0); var_id < num_variables; ++var_id) {
const MPVariableProto& mp_var = mp_model.variable(var_id);
problem->add_var_names(mp_var.name());
// This will be changed to false as soon as we detect the variable to be
// non-binary. This is done this way so we can display a nice error message
// before aborting the function and returning false.
bool is_binary = mp_var.is_integer();
const Fractional lb = mp_var.lower_bound();
const Fractional ub = mp_var.upper_bound();
if (lb <= -1.0) is_binary = false;
if (ub >= 2.0) is_binary = false;
if (is_binary) {
// 4 cases.
if (lb <= 0.0 && ub >= 1.0) {
// Binary variable. Ok.
} else if (lb <= 1.0 && ub >= 1.0) {
// Fixed variable at 1.
LinearBooleanConstraint* constraint = problem->add_constraints();
constraint->set_lower_bound(1);
constraint->set_upper_bound(1);
constraint->add_literals(var_id + 1);
constraint->add_coefficients(1);
} else if (lb <= 0.0 && ub >= 0.0) {
// Fixed variable at 0.
LinearBooleanConstraint* constraint = problem->add_constraints();
constraint->set_lower_bound(0);
constraint->set_upper_bound(0);
constraint->add_literals(var_id + 1);
constraint->add_coefficients(1);
} else {
// No possible integer value!
is_binary = false;
}
}
// Abort if the variable is not binary.
if (!is_binary) {
LOG(WARNING) << "The variable #" << var_id << " with name "
<< mp_var.name() << " is not binary. "
<< "lb: " << lb << " ub: " << ub;
return false;
}
}
// Variables needed to scale the double coefficients into int64_t.
const int64_t kInt64Max = std::numeric_limits<int64_t>::max();
double max_relative_error = 0.0;
double max_bound_error = 0.0;
double max_scaling_factor = 0.0;
double relative_error = 0.0;
double scaling_factor = 0.0;
std::vector<double> coefficients;
// Add all constraints.
for (const MPConstraintProto& mp_constraint : mp_model.constraint()) {
LinearBooleanConstraint* constraint = problem->add_constraints();
constraint->set_name(mp_constraint.name());
// First scale the coefficients of the constraints.
coefficients.clear();
const int num_coeffs = mp_constraint.coefficient_size();
for (int i = 0; i < num_coeffs; ++i) {
coefficients.push_back(mp_constraint.coefficient(i));
}
GetBestScalingOfDoublesToInt64(coefficients, kInt64Max, &scaling_factor,
&relative_error);
const int64_t gcd =
ComputeGcdOfRoundedDoubles(coefficients, scaling_factor);
max_relative_error = std::max(relative_error, max_relative_error);
max_scaling_factor = std::max(scaling_factor / gcd, max_scaling_factor);
double bound_error = 0.0;
for (int i = 0; i < num_coeffs; ++i) {