-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrToolsDemoApplicationTest.java
273 lines (220 loc) · 10.6 KB
/
OrToolsDemoApplicationTest.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
package com.ortools;
import com.google.ortools.linearsolver.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static com.google.ortools.linearsolver.MPSolverResponseStatus.MPSOLVER_OPTIMAL;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Slf4j
public class OrToolsDemoApplicationTest {
@BeforeAll
public static void beforeAll() {
log.info("Loading jniortools library...");
System.loadLibrary("jniortools");
log.info("Loading jniortools library...Done.");
}
private MPConstraintProto getFirstConstraint(double ub) {
// x + 7y <= ub
return MPConstraintProto.newBuilder()
.setLowerBound(Double.NEGATIVE_INFINITY)
.setUpperBound(ub)
.addVarIndex(0)
.addCoefficient(1.0d)
.addVarIndex(1)
.addCoefficient(7.0d)
.build();
}
private MPConstraintProto getSecondConstraint(double ub) {
// x <= ub
return MPConstraintProto.newBuilder()
.setLowerBound(Double.NEGATIVE_INFINITY)
.setUpperBound(ub)
.addVarIndex(0)
.addCoefficient(1.0d)
.build();
}
private MPModelProto.Builder getLinearObjective() {
// Objective: Max. x + 10y
final MPVariableProto x = this.getIntVar("x").setObjectiveCoefficient(1.0d).build();
final MPVariableProto y = this.getIntVar("y").setObjectiveCoefficient(10.0d).build();
return this.getModel(
Arrays.asList(x, y),
Arrays.asList(this.getFirstConstraint(17.5d), this.getSecondConstraint(3.5d)),
Collections.emptyList(),
Collections.emptyList()
).setMaximize(true);
}
private MPModelProto.Builder getQuadraticObjective() {
// Objective: Max. xy
final MPVariableProto x = this.getIntVar("x").build();
final MPVariableProto y = this.getIntVar("y").build();
final MPModelProto.Builder mpModelProto = this.getModel(
Arrays.asList(x, y),
Arrays.asList(this.getFirstConstraint(17.5d), this.getSecondConstraint(3.5d)),
Collections.emptyList(),
Collections.emptyList()
).setMaximize(true);
final MPQuadraticObjective quadraticObjective = MPQuadraticObjective.newBuilder()
.addAllQvar1Index(Collections.singletonList(0))
.addAllQvar2Index(Collections.singletonList(1))
.addAllCoefficient(Collections.singletonList(2.0d))
.build();
mpModelProto
.setQuadraticObjective(quadraticObjective)
.setMaximize(true);
return mpModelProto;
}
@Test
public void shouldSolveLP() {
final MPModelProto.Builder mpModelProto = this.getLinearObjective();
final MPSolutionResponse mpSolutionResponse = this.solve(mpModelProto);
assertEquals(MPSOLVER_OPTIMAL, mpSolutionResponse.getStatus());
assertEquals(23.0d, mpSolutionResponse.getObjectiveValue());
assertEquals(3.0d, mpSolutionResponse.getVariableValue(0));
assertEquals(2.0d, mpSolutionResponse.getVariableValue(1));
}
@Test
public void shouldSolveLPWithQuadraticConstraint() {
final MPModelProto.Builder mpModelProto = this.getLinearObjective();
final MPQuadraticConstraint qc0 = MPQuadraticConstraint.newBuilder()
.addQvar1Index(0)
.addQvar2Index(1)
.addQcoefficient(1.0d)
.setUpperBound(5.0d)
.build();
mpModelProto.addGeneralConstraint(MPGeneralConstraintProto.newBuilder().setQuadraticConstraint(qc0).build());
final MPSolutionResponse mpSolutionResponse = this.solve(mpModelProto);
assertEquals(MPSOLVER_OPTIMAL, mpSolutionResponse.getStatus());
assertEquals(22.0d, mpSolutionResponse.getObjectiveValue());
assertEquals(2.0d, mpSolutionResponse.getVariableValue(0));
assertEquals(2.0d, mpSolutionResponse.getVariableValue(1));
}
@Test
public void shouldSolveLPWithIndicatorConstraint() {
final MPModelProto.Builder mpModelProto = this.getLinearObjective();
final MPVariableProto k = this.getIntVar("k").setUpperBound(1.0d).build();
mpModelProto
.addVariable(2, k)
.removeConstraint(0);
// x + 7y <= 17.5
final MPConstraintProto c0_0 = this.getFirstConstraint(17.5d);
// x + 7y <= 24.5
final MPConstraintProto c0_1 = this.getFirstConstraint(24.5d);
final MPIndicatorConstraint ic0 = MPIndicatorConstraint.newBuilder()
.setVarIndex(2)
.setVarValue(0)
.setConstraint(c0_0)
.build();
final MPIndicatorConstraint ic1 = MPIndicatorConstraint.newBuilder()
.setVarIndex(2)
.setVarValue(1)
.setConstraint(c0_1)
.build();
mpModelProto
.addGeneralConstraint(MPGeneralConstraintProto.newBuilder().setIndicatorConstraint(ic0).build())
.addGeneralConstraint(MPGeneralConstraintProto.newBuilder().setIndicatorConstraint(ic1).build());
final MPSolutionResponse mpSolutionResponse = this.solve(mpModelProto);
assertEquals(MPSOLVER_OPTIMAL, mpSolutionResponse.getStatus());
assertEquals(33.0d, mpSolutionResponse.getObjectiveValue());
assertEquals(3.0d, mpSolutionResponse.getVariableValue(0));
assertEquals(3.0d, mpSolutionResponse.getVariableValue(1));
assertEquals(1.0d, mpSolutionResponse.getVariableValue(2));
}
@Test
public void shouldSolveQP() {
final MPModelProto.Builder mpModelProto = this.getQuadraticObjective();
final MPSolutionResponse mpSolutionResponse = this.solve(mpModelProto);
assertEquals(MPSOLVER_OPTIMAL, mpSolutionResponse.getStatus());
assertEquals(12.0d, mpSolutionResponse.getObjectiveValue());
assertEquals(3.0d, mpSolutionResponse.getVariableValue(0));
assertEquals(2.0d, mpSolutionResponse.getVariableValue(1));
}
@Test
public void shouldSolveQPWithQuadraticConstraint() {
final MPModelProto.Builder mpModelProto = this.getQuadraticObjective();
final MPQuadraticConstraint qc0 = MPQuadraticConstraint.newBuilder()
.addQvar1Index(0)
.addQvar2Index(1)
.addQcoefficient(1.0d)
.setUpperBound(5.0d)
.build();
mpModelProto.addGeneralConstraint(MPGeneralConstraintProto.newBuilder().setQuadraticConstraint(qc0).build());
final MPSolutionResponse mpSolutionResponse = this.solve(mpModelProto);
assertEquals(MPSOLVER_OPTIMAL, mpSolutionResponse.getStatus());
assertEquals(8.0d, mpSolutionResponse.getObjectiveValue());
assertEquals(2.0d, mpSolutionResponse.getVariableValue(0));
assertEquals(2.0d, mpSolutionResponse.getVariableValue(1));
}
@Test
public void shouldSolveQPWithIndicatorConstraint() {
final MPModelProto.Builder mpModelProto = this.getQuadraticObjective();
final MPVariableProto k = this.getIntVar("k").setUpperBound(1.0d).build();
mpModelProto
.addVariable(2, k)
.removeConstraint(0);
// x + 7y <= 17.5
final MPConstraintProto c0_0 = this.getFirstConstraint(17.5d);
// x + 7y <= 24.5
final MPConstraintProto c0_1 = this.getFirstConstraint(24.5d);
final MPIndicatorConstraint ic0 = MPIndicatorConstraint.newBuilder()
.setVarIndex(2)
.setVarValue(0)
.setConstraint(c0_0)
.build();
final MPIndicatorConstraint ic1 = MPIndicatorConstraint.newBuilder()
.setVarIndex(2)
.setVarValue(1)
.setConstraint(c0_1)
.build();
mpModelProto
.addGeneralConstraint(MPGeneralConstraintProto.newBuilder().setIndicatorConstraint(ic0).build())
.addGeneralConstraint(MPGeneralConstraintProto.newBuilder().setIndicatorConstraint(ic1).build());
final MPSolutionResponse mpSolutionResponse = this.solve(mpModelProto);
assertEquals(MPSOLVER_OPTIMAL, mpSolutionResponse.getStatus());
assertEquals(18.0d, mpSolutionResponse.getObjectiveValue());
assertEquals(3.0d, mpSolutionResponse.getVariableValue(0));
assertEquals(3.0d, mpSolutionResponse.getVariableValue(1));
assertEquals(1.0d, mpSolutionResponse.getVariableValue(2));
}
private MPVariableProto.Builder getNumVar(String name) {
return MPVariableProto.newBuilder()
.setName(name)
.setLowerBound(0.0d)
.setUpperBound(Double.POSITIVE_INFINITY);
}
private MPVariableProto.Builder getIntVar(String name) {
return this.getNumVar(name).setIsInteger(true);
}
private MPModelProto.Builder getModel(
List<MPVariableProto> mpVariableProtos,
List<MPConstraintProto> mpConstraintProtos,
List<MPIndicatorConstraint> mpIndicatorConstraints,
List<MPQuadraticConstraint> mpQuadraticConstraints
) {
final List<MPGeneralConstraintProto> mpIndicatorConstraintProtos = mpIndicatorConstraints.stream()
.map(MPGeneralConstraintProto.newBuilder()::setIndicatorConstraint)
.map(MPGeneralConstraintProto.Builder::build)
.collect(Collectors.toList());
final List<MPGeneralConstraintProto> mpQuadraticConstraintProtos = mpQuadraticConstraints.stream()
.map(MPGeneralConstraintProto.newBuilder()::setQuadraticConstraint)
.map(MPGeneralConstraintProto.Builder::build)
.collect(Collectors.toList());
return MPModelProto.newBuilder()
.addAllVariable(mpVariableProtos)
.addAllConstraint(mpConstraintProtos)
.addAllGeneralConstraint(mpIndicatorConstraintProtos)
.addAllGeneralConstraint(mpQuadraticConstraintProtos);
}
private MPSolutionResponse solve(MPModelProto.Builder mpModelProto) {
final MPModelRequest mpModelRequest = MPModelRequest.newBuilder()
.setEnableInternalSolverOutput(true)
.setSolverType(MPModelRequest.SolverType.GUROBI_MIXED_INTEGER_PROGRAMMING)
.setModel(mpModelProto)
.build();
return MPSolver.solveWithProto(mpModelRequest);
}
}