forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSolverTest.java
651 lines (571 loc) · 26.2 KB
/
LinearSolverTest.java
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
// 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.
package com.google.ortools.linearsolver;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.ortools.Loader;
import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Test the Linear Solver java interface. */
public final class LinearSolverTest {
// Numerical tolerance for checking primal, dual, objective values
// and other values.
private static final double NUM_TOLERANCE = 1e-5;
@BeforeEach
public void setUp() {
Loader.loadNativeLibraries();
}
private void runBasicCtor(MPSolver.OptimizationProblemType solverType) {
if (!MPSolver.supportsProblemType(solverType)) {
return;
}
final MPSolver solver = new MPSolver("testBasicCtor", solverType);
assertNotNull(solver);
solver.solve();
}
@Test
public void testMPSolver_basicCtor() {
runBasicCtor(MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
runBasicCtor(MPSolver.OptimizationProblemType.GLPK_LINEAR_PROGRAMMING);
runBasicCtor(MPSolver.OptimizationProblemType.GLPK_MIXED_INTEGER_PROGRAMMING);
runBasicCtor(MPSolver.OptimizationProblemType.CLP_LINEAR_PROGRAMMING);
runBasicCtor(MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
}
@Test
public void testMPSolver_destructor() {
final MPSolver solver =
new MPSolver("testDestructor", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
assertNotNull(solver);
solver.delete();
}
private void runLinearSolver(
MPSolver.OptimizationProblemType problemType, boolean integerVariables) {
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("Solver", problemType);
assertNotNull(solver);
final double infinity = MPSolver.infinity();
final MPVariable x1 = solver.makeNumVar(0.0, infinity, "x1");
final MPVariable x2 = solver.makeNumVar(0.0, infinity, "x2");
final MPVariable x3 = solver.makeNumVar(0.0, infinity, "x3");
if (integerVariables) {
x1.setInteger(true);
x2.setInteger(true);
x3.setInteger(true);
}
assertEquals(3, solver.numVariables());
final MPObjective objective = solver.objective();
objective.setCoefficient(x1, 10);
objective.setCoefficient(x2, 6);
objective.setCoefficient(x3, 4);
objective.setMaximization();
assertEquals(6.0, objective.getCoefficient(x2), 1e-6);
assertTrue(objective.maximization());
assertFalse(objective.minimization());
final MPConstraint c0 = solver.makeConstraint(-1000, 100.0);
c0.setCoefficient(x1, 1);
c0.setCoefficient(x2, 1);
c0.setCoefficient(x3, 1);
final MPConstraint c1 = solver.makeConstraint(-1000, 600.0);
c1.setCoefficient(x1, 10);
c1.setCoefficient(x2, 4);
c1.setCoefficient(x3, 5);
assertEquals(4.0, c1.getCoefficient(x2), 1e-6);
final MPConstraint c2 = solver.makeConstraint(-1000, 300.0);
c2.setCoefficient(x1, 2);
c2.setCoefficient(x2, 2);
c2.setCoefficient(x3, 6);
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
if (integerVariables) {
assertEquals(732.0, objective.value(), NUM_TOLERANCE);
assertEquals(33.0, x1.solutionValue(), NUM_TOLERANCE);
assertEquals(67.0, x2.solutionValue(), NUM_TOLERANCE);
assertEquals(0.0, x3.solutionValue(), NUM_TOLERANCE);
} else {
assertEquals(733.333333, objective.value(), NUM_TOLERANCE);
assertEquals(33.333333, x1.solutionValue(), NUM_TOLERANCE);
assertEquals(66.666667, x2.solutionValue(), NUM_TOLERANCE);
assertEquals(0, x3.solutionValue(), NUM_TOLERANCE);
}
}
@Test
public void testMPSolver_linearSolver() {
runLinearSolver(MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING, false);
runLinearSolver(MPSolver.OptimizationProblemType.CLP_LINEAR_PROGRAMMING, false);
runLinearSolver(MPSolver.OptimizationProblemType.GLPK_LINEAR_PROGRAMMING, false);
runLinearSolver(MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING, true);
runLinearSolver(MPSolver.OptimizationProblemType.GLPK_MIXED_INTEGER_PROGRAMMING, true);
runLinearSolver(MPSolver.OptimizationProblemType.SCIP_MIXED_INTEGER_PROGRAMMING, true);
}
private void runFirstLinearExample(MPSolver.OptimizationProblemType problemType) {
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("Solver", problemType);
assertNotNull(solver);
final MPVariable x1 = solver.makeNumVar(0.0, Double.POSITIVE_INFINITY, "x1");
final MPVariable x2 = solver.makeNumVar(0.0, Double.POSITIVE_INFINITY, "x2");
final MPVariable x3 = solver.makeNumVar(0.0, Double.POSITIVE_INFINITY, "x3");
assertEquals(3, solver.numVariables());
final double[] obj = {10.0, 6.0, 4.0};
final MPObjective objective = solver.objective();
objective.setCoefficient(x1, obj[0]);
objective.setCoefficient(x2, obj[1]);
objective.setCoefficient(x3, obj[2]);
objective.setMaximization();
final double rhs0 = 100.0;
final MPConstraint c0 = solver.makeConstraint(-Double.POSITIVE_INFINITY, rhs0, "c0");
final double[] coef0 = {1.0, 1.0, 1.0};
c0.setCoefficient(x1, coef0[0]);
c0.setCoefficient(x2, coef0[1]);
c0.setCoefficient(x3, coef0[2]);
final double rhs1 = 600.0;
final MPConstraint c1 = solver.makeConstraint(-Double.POSITIVE_INFINITY, rhs1, "c1");
final double[] coef1 = {10.0, 4.0, 5.0};
c1.setCoefficient(x1, coef1[0]);
c1.setCoefficient(x2, coef1[1]);
c1.setCoefficient(x3, coef1[2]);
final double rhs2 = 300.0;
final MPConstraint c2 = solver.makeConstraint(-Double.POSITIVE_INFINITY, rhs2);
final double[] coef2 = {2.0, 2.0, 6.0};
c2.setCoefficient(x1, coef2[0]);
c2.setCoefficient(x2, coef2[1]);
c2.setCoefficient(x3, coef2[2]);
assertEquals(3, solver.numConstraints());
assertEquals("c0", c0.name());
assertEquals("c1", c1.name());
assertEquals("auto_c_000000002", c2.name());
// The problem has an optimal solution.;
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
assertEquals(733.333333, objective.value(), NUM_TOLERANCE);
assertEquals(33.333333, x1.solutionValue(), NUM_TOLERANCE);
assertEquals(66.666667, x2.solutionValue(), NUM_TOLERANCE);
assertEquals(0, x3.solutionValue(), NUM_TOLERANCE);
// c0 and c1 are binding;
final double[] activities = solver.computeConstraintActivities();
assertEquals(3, activities.length);
assertEquals(3.333333, c0.dualValue(), NUM_TOLERANCE);
assertEquals(0.666667, c1.dualValue(), NUM_TOLERANCE);
assertEquals(rhs0, activities[c0.index()], NUM_TOLERANCE);
assertEquals(rhs1, activities[c1.index()], NUM_TOLERANCE);
assertEquals(MPSolver.BasisStatus.AT_UPPER_BOUND, c0.basisStatus());
assertEquals(MPSolver.BasisStatus.AT_UPPER_BOUND, c1.basisStatus());
// c2 is not binding;
assertEquals(0.0, c2.dualValue(), NUM_TOLERANCE);
assertEquals(200.0, activities[c2.index()], NUM_TOLERANCE);
assertEquals(MPSolver.BasisStatus.BASIC, c2.basisStatus());
// The optimum of the dual problem is equal to the optimum of the;
// primal problem.;
final double dualObjectiveValue = c0.dualValue() * rhs0 + c1.dualValue() * rhs1;
assertEquals(objective.value(), dualObjectiveValue, NUM_TOLERANCE);
// x1 and x2 are basic;
assertEquals(0.0, x1.reducedCost(), NUM_TOLERANCE);
assertEquals(0.0, x2.reducedCost(), NUM_TOLERANCE);
assertEquals(MPSolver.BasisStatus.BASIC, x1.basisStatus());
assertEquals(MPSolver.BasisStatus.BASIC, x2.basisStatus());
// x3 is non-basic;
final double x3ExpectedReducedCost =
(obj[2] - coef0[2] * c0.dualValue() - coef1[2] * c1.dualValue());
assertEquals(x3ExpectedReducedCost, x3.reducedCost(), NUM_TOLERANCE);
assertEquals(MPSolver.BasisStatus.AT_LOWER_BOUND, x3.basisStatus());
if (solver.problemType() == MPSolver.OptimizationProblemType.GLPK_LINEAR_PROGRAMMING) {
assertEquals(56.333333, solver.computeExactConditionNumber(), NUM_TOLERANCE);
}
}
@Test
public void testMPSolver_firstLinearExample() {
runFirstLinearExample(MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
runFirstLinearExample(MPSolver.OptimizationProblemType.CLP_LINEAR_PROGRAMMING);
runFirstLinearExample(MPSolver.OptimizationProblemType.GLPK_LINEAR_PROGRAMMING);
runFirstLinearExample(MPSolver.OptimizationProblemType.GUROBI_LINEAR_PROGRAMMING);
}
private void runFirstMIPExample(MPSolver.OptimizationProblemType problemType) {
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("Solver", problemType);
assertNotNull(solver);
// Integer variables shouldn't have infinite bounds, nor really large bounds:
// it can make your solver behave erratically. If you have integer variables
// with a truly large dynamic range you should consider making it non-integer.
final double upperBound = 1000;
final MPVariable x1 = solver.makeIntVar(0.0, upperBound, "x1");
final MPVariable x2 = solver.makeIntVar(0.0, upperBound, "x2");
solver.objective().setCoefficient(x1, 1);
solver.objective().setCoefficient(x2, 2);
final MPConstraint ct = solver.makeConstraint(17, Double.POSITIVE_INFINITY);
ct.setCoefficient(x1, 3);
ct.setCoefficient(x2, 2);
// Check the solution.
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
final double optObjValue = 6.0;
assertEquals(optObjValue, solver.objective().value(), 1e-6);
assertEquals(optObjValue, solver.objective().bestBound(), 1e-6);
final double optRowActivity = 18.0;
assertEquals(optRowActivity, solver.computeConstraintActivities()[ct.index()], NUM_TOLERANCE);
// BOP does not support nodes().
if (solver.problemType() != MPSolver.OptimizationProblemType.BOP_INTEGER_PROGRAMMING) {
assertThat(solver.nodes()).isAtLeast(0);
}
}
@Test
public void testMPSolver_firstMIPExample() {
runFirstMIPExample(MPSolver.OptimizationProblemType.BOP_INTEGER_PROGRAMMING);
runFirstMIPExample(MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
runFirstMIPExample(MPSolver.OptimizationProblemType.GLPK_MIXED_INTEGER_PROGRAMMING);
runFirstMIPExample(MPSolver.OptimizationProblemType.SCIP_MIXED_INTEGER_PROGRAMMING);
runFirstMIPExample(MPSolver.OptimizationProblemType.SAT_INTEGER_PROGRAMMING);
runFirstMIPExample(MPSolver.OptimizationProblemType.GUROBI_MIXED_INTEGER_PROGRAMMING);
}
private void runSuccessiveObjectives(MPSolver.OptimizationProblemType problemType) {
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("Solver", problemType);
assertNotNull(solver);
final MPVariable x1 = solver.makeNumVar(0, 10, "var1");
final MPVariable x2 = solver.makeNumVar(0, 10, "var2");
final MPConstraint ct = solver.makeConstraint(0, 10);
ct.setCoefficient(x1, 1);
ct.setCoefficient(x2, 2);
final MPObjective objective = solver.objective();
objective.setCoefficient(x1, 1);
objective.setCoefficient(x2, 0);
objective.setOptimizationDirection(true);
// Check the solution.
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
assertEquals(10.0, x1.solutionValue(), NUM_TOLERANCE);
assertEquals(0.0, x2.solutionValue(), NUM_TOLERANCE);
objective.setCoefficient(x1, 0);
objective.setCoefficient(x2, 1);
objective.setOptimizationDirection(true);
// Check the solution
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
assertEquals(0.0, x1.solutionValue(), NUM_TOLERANCE);
assertEquals(5.0, x2.solutionValue(), NUM_TOLERANCE);
objective.setCoefficient(x1, -1);
objective.setCoefficient(x2, 0);
objective.setOptimizationDirection(false);
// Check the solution.
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
assertEquals(10.0, x1.solutionValue(), NUM_TOLERANCE);
assertEquals(0.0, x2.solutionValue(), NUM_TOLERANCE);
}
@Test
public void testMPSolver_successiveObjectives() {
runSuccessiveObjectives(MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
runSuccessiveObjectives(MPSolver.OptimizationProblemType.CLP_LINEAR_PROGRAMMING);
runSuccessiveObjectives(MPSolver.OptimizationProblemType.GLPK_LINEAR_PROGRAMMING);
runSuccessiveObjectives(MPSolver.OptimizationProblemType.GUROBI_LINEAR_PROGRAMMING);
runSuccessiveObjectives(MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
runSuccessiveObjectives(MPSolver.OptimizationProblemType.GLPK_MIXED_INTEGER_PROGRAMMING);
runSuccessiveObjectives(MPSolver.OptimizationProblemType.SCIP_MIXED_INTEGER_PROGRAMMING);
runSuccessiveObjectives(MPSolver.OptimizationProblemType.SAT_INTEGER_PROGRAMMING);
runSuccessiveObjectives(MPSolver.OptimizationProblemType.GUROBI_MIXED_INTEGER_PROGRAMMING);
}
private void runObjectiveOffset(MPSolver.OptimizationProblemType problemType) {
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("Solver", problemType);
assertNotNull(solver);
final MPVariable x1 = solver.makeNumVar(1.0, 10.0, "x1");
final MPVariable x2 = solver.makeNumVar(1.0, 10.0, "x2");
final MPConstraint ct = solver.makeConstraint(0, 4.0);
ct.setCoefficient(x1, 1);
ct.setCoefficient(x2, 2);
final double objectiveOffset = 10.0;
// Simple minimization.
final MPObjective objective = solver.objective();
objective.setCoefficient(x1, 1.0);
objective.setCoefficient(x2, 1.0);
objective.setOffset(objectiveOffset);
objective.setOptimizationDirection(false);
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
assertEquals(2.0 + objectiveOffset, objective.value(), 1e-6);
// Offset is provided in several separate constants.
objective.setCoefficient(x1, 1.0);
objective.setCoefficient(x2, 1.0);
objective.setOffset(-1.0);
objective.setOffset(objectiveOffset + objective.offset());
objective.setOffset(1.0 + objective.offset());
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
assertEquals(2.0 + objectiveOffset, objective.value(), 1e-6);
// Simple maximization.
objective.setCoefficient(x1, 1.0);
objective.setCoefficient(x2, 1.0);
objective.setOffset(objectiveOffset);
objective.setOptimizationDirection(true);
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
assertEquals(3.0 + objectiveOffset, objective.value(), 1e-6);
}
@Test
public void testMPSolver_objectiveOffset() {
runObjectiveOffset(MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
runObjectiveOffset(MPSolver.OptimizationProblemType.CLP_LINEAR_PROGRAMMING);
runObjectiveOffset(MPSolver.OptimizationProblemType.GLPK_LINEAR_PROGRAMMING);
runObjectiveOffset(MPSolver.OptimizationProblemType.GUROBI_LINEAR_PROGRAMMING);
runObjectiveOffset(MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
runObjectiveOffset(MPSolver.OptimizationProblemType.GLPK_MIXED_INTEGER_PROGRAMMING);
runObjectiveOffset(MPSolver.OptimizationProblemType.SCIP_MIXED_INTEGER_PROGRAMMING);
runObjectiveOffset(MPSolver.OptimizationProblemType.SAT_INTEGER_PROGRAMMING);
runObjectiveOffset(MPSolver.OptimizationProblemType.GUROBI_MIXED_INTEGER_PROGRAMMING);
}
@Test
public void testMPSolver_lazyConstraints() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.SCIP_MIXED_INTEGER_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("testLazyConstraints", problemType);
assertNotNull(solver);
final double infinity = MPSolver.infinity();
final MPVariable x = solver.makeIntVar(0, infinity, "x");
final MPVariable y = solver.makeIntVar(0, infinity, "y");
final MPConstraint ct1 = solver.makeConstraint(0, 10.0);
ct1.setCoefficient(x, 2.0);
ct1.setCoefficient(y, 1.0);
final MPConstraint ct2 = solver.makeConstraint(0, 10.0);
ct2.setCoefficient(x, 1.0);
ct2.setCoefficient(y, 2.0);
ct2.setIsLazy(true);
assertFalse(ct1.isLazy());
assertTrue(ct2.isLazy());
final MPObjective objective = solver.objective();
objective.setCoefficient(x, 1.0);
objective.setCoefficient(y, 1.0);
objective.setOptimizationDirection(true);
assertEquals(MPSolver.ResultStatus.OPTIMAL, solver.solve());
assertEquals(solver.objective().value(), 6.0, NUM_TOLERANCE);
}
@Test
public void testMPSolver_sameConstraintName() {
MPSolver solver = MPSolver.createSolver("GLOP");
assertNotNull(solver);
boolean success = true;
solver.makeConstraint("my_const_name");
try {
solver.makeConstraint("my_const_name");
} catch (Throwable e) {
System.out.println(e);
success = false;
}
assertTrue(success);
}
@Test
public void testMPSolver_exportModelToProto() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("testExportModelToProto", problemType);
assertNotNull(solver);
solver.makeNumVar(0.0, 10.0, "x1");
solver.makeConstraint(0.0, 0.0);
solver.objective().setOptimizationDirection(true);
final MPModelProto model = solver.exportModelToProto();
assertEquals(1, model.getVariableCount());
assertEquals(1, model.getConstraintCount());
assertTrue(model.getMaximize());
}
@Test
public void testMPsolver_createSolutionResponseProto() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("testCreateSolutionResponseProto", problemType);
assertNotNull(solver);
final MPVariable x1 = solver.makeNumVar(0.0, 10.0, "x1");
solver.objective().setCoefficient(x1, 1.0);
solver.objective().setOptimizationDirection(true);
solver.solve();
final MPSolutionResponse response = solver.createSolutionResponseProto();
assertEquals(MPSolverResponseStatus.MPSOLVER_OPTIMAL, response.getStatus());
assertEquals(10.0, response.getObjectiveValue(), 1e-6);
}
@Test
public void testMPSolver_solveWithProto() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPModelProto.Builder modelBuilder = MPModelProto.newBuilder().setMaximize(true);
final MPVariableProto variable = MPVariableProto.newBuilder()
.setLowerBound(0.0)
.setUpperBound(10.0)
.setName("x1")
.setIsInteger(false)
.setObjectiveCoefficient(1.0)
.build();
modelBuilder.addVariable(variable);
final MPModelRequest request = MPModelRequest.newBuilder()
.setModel(modelBuilder.build())
.setSolverType(MPModelRequest.SolverType.GLOP_LINEAR_PROGRAMMING)
.build();
final MPSolutionResponse response = MPSolver.solveWithProto(request);
assertEquals(MPSolverResponseStatus.MPSOLVER_OPTIMAL, response.getStatus());
assertEquals(10.0, response.getObjectiveValue(), 1e-6);
}
@Test
public void testModelExport() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("tesModelExport", problemType);
assertNotNull(solver);
final double infinity = MPSolver.infinity();
// x1, x2 and x3 are continuous non-negative variables.
final MPVariable x1 = solver.makeNumVar(0.0, infinity, "x1");
// Maximize 10 * x1.
solver.objective().setCoefficient(x1, 10);
solver.objective().setMinimization();
// 5 * x1 <= 30.
final MPConstraint c0 = solver.makeConstraint(-infinity, 100.0);
c0.setCoefficient(x1, 5);
final MPModelExportOptions obfuscate = new MPModelExportOptions();
obfuscate.setObfuscate(true);
String out = solver.exportModelAsLpFormat();
assertThat(out).isNotEmpty();
out = solver.exportModelAsLpFormat(obfuscate);
assertThat(out).isNotEmpty();
out = solver.exportModelAsMpsFormat();
assertThat(out).isNotEmpty();
out = solver.exportModelAsMpsFormat(obfuscate);
assertThat(out).isNotEmpty();
}
@Test
public void testMPSolver_wrongModelExport() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("testWrongModelExport", problemType);
assertNotNull(solver);
// Test that forbidden names are renamed.
solver.makeBoolVar("<-%$#!&~-+ ⌂"); // Some illegal name.
String out = solver.exportModelAsLpFormat();
assertThat(out).isNotEmpty();
out = solver.exportModelAsMpsFormat();
assertThat(out).isNotEmpty();
}
@Test
public void testMPSolver_setHint() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("testSetHint", problemType);
assertNotNull(solver);
final MPVariable[] variables = {
solver.makeNumVar(0.0, 10.0, "x1"), solver.makeNumVar(0.0, 10.0, "x2")};
final double[] values = {5.0, 6.0};
solver.setHint(variables, values);
final MPModelProto model = solver.exportModelToProto();
final PartialVariableAssignment hint = model.getSolutionHint();
assertEquals(2, hint.getVarIndexCount());
assertEquals(2, hint.getVarValueCount());
assertEquals(0, hint.getVarIndex(0));
assertEquals(5.0, hint.getVarValue(0), 1e-6);
assertEquals(1, hint.getVarIndex(1));
assertEquals(6.0, hint.getVarValue(1), 1e-6);
}
@Test
public void testMPSolver_issue132() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.CLP_LINEAR_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("CoinError", problemType);
assertNotNull(solver);
final double infinity = MPSolver.infinity();
final MPVariable x0 = solver.makeNumVar(0.0, 1.0, "x0");
final MPVariable x1 = solver.makeNumVar(0.0, 0.3, "x1");
final MPVariable x2 = solver.makeNumVar(0.0, 0.3, "x2");
final MPVariable x3 = solver.makeNumVar(-infinity, infinity, "x3");
final MPObjective obj = solver.objective();
obj.setCoefficient(x1, 2.655523);
obj.setCoefficient(x2, -2.70917);
obj.setCoefficient(x3, 1);
obj.setMaximization();
final MPConstraint c0 = solver.makeConstraint(-infinity, 0.302499);
c0.setCoefficient(x3, 1);
c0.setCoefficient(x0, -3.484345);
final MPConstraint c1 = solver.makeConstraint(-infinity, 0.507194);
c1.setCoefficient(x3, 1);
c1.setCoefficient(x0, -3.074807);
final MPConstraint c2 = solver.makeConstraint(0.594, 0.594);
c2.setCoefficient(x0, 1);
c2.setCoefficient(x1, 1.01);
c2.setCoefficient(x2, -0.99);
System.out.println("Number of variables = " + solver.numVariables());
System.out.println("Number of constraints = " + solver.numConstraints());
solver.enableOutput();
System.out.println(solver.exportModelAsLpFormat());
System.out.println(solver.solve());
}
@Test
public void testMPSolver_setHintAndSolverGetters() {
final MPSolver.OptimizationProblemType problemType =
MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING;
if (!MPSolver.supportsProblemType(problemType)) {
return;
}
final MPSolver solver = new MPSolver("glop", problemType);
assertNotNull(solver);
// x and y are continuous non-negative variables.
final MPVariable x = solver.makeIntVar(0.0, Double.POSITIVE_INFINITY, "x");
final MPVariable y = solver.makeIntVar(0.0, Double.POSITIVE_INFINITY, "y");
// Objectif function: Maximize x + 10 * y.
final MPObjective objective = solver.objective();
objective.setCoefficient(x, 1);
objective.setCoefficient(y, 10);
objective.setMaximization();
// x + 7 * y <= 17.5.
final MPConstraint c0 = solver.makeConstraint(-Double.POSITIVE_INFINITY, 17.5, "c0");
c0.setCoefficient(x, 1);
c0.setCoefficient(y, 7);
// x <= 3.5.
final MPConstraint c1 = solver.makeConstraint(-Double.POSITIVE_INFINITY, 3.5, "c1");
c1.setCoefficient(x, 1);
c1.setCoefficient(y, 0);
// Test solver getters.
final MPVariable[] variables = solver.variables();
assertThat(variables).hasLength(2);
final MPConstraint[] constraints = solver.constraints();
assertThat(constraints).hasLength(2);
// Test API compiles.
solver.setHint(variables, new double[] {2.0, 3.0});
assertEquals("y", variables[1].name());
assertEquals("c0", constraints[0].name());
// TODO(user): Add API to query the hint.
assertFalse(solver.setNumThreads(4));
}
}