forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CpModelTest.java
229 lines (195 loc) · 9.28 KB
/
CpModelTest.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
// 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.sat;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.google.ortools.Loader;
import com.google.ortools.util.Domain;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/** Tests the CpSolver java interface. */
public final class CpModelTest {
@BeforeEach
public void setUp() {
Loader.loadNativeLibraries();
}
@Test
public void testCpModel_newIntVar() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newIntVar(0, 10, "x");
final IntVar y = model.newIntVarFromDomain(Domain.fromValues(new long[] {0, 1, 2, 5}), "y");
final IntVar z =
model.newIntVarFromDomain(Domain.fromIntervals(new long[][] {{0, 2}, {5}}), "");
final IntVar t = model.newBoolVar("t");
final IntVar u = model.newConstant(5);
assertThat(x.getName()).isEqualTo("x");
assertThat(y.getName()).isEqualTo("y");
assertThat(z.getName()).isEmpty();
assertThat(t.getName()).isEqualTo("t");
assertThat(u.getName()).isEmpty();
assertThat(x.getDomain().flattenedIntervals()).isEqualTo(new long[] {0, 10});
assertThat(x.getShortString()).isEqualTo("x(0..10)");
assertThat(y.getShortString()).isEqualTo("y(0..2, 5)");
assertThat(z.getShortString()).isEqualTo("var_2(0..2, 5)");
assertThat(t.getShortString()).isEqualTo("t(0..1)");
assertThat(u.getShortString()).isEqualTo("5");
}
@Test
public void testCpModel_addBoolOr() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
model.addBoolOr(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
assertThat(model.model().getConstraints(0).hasBoolOr()).isTrue();
assertThat(model.model().getConstraints(0).getBoolOr().getLiteralsCount()).isEqualTo(3);
}
@Test
public void testCpModel_addBoolAnd() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
model.addBoolAnd(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
assertThat(model.model().getConstraints(0).hasBoolAnd()).isTrue();
assertThat(model.model().getConstraints(0).getBoolAnd().getLiteralsCount()).isEqualTo(3);
}
@Test
public void testCpModel_addBoolXor() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
model.addBoolXor(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
assertThat(model.model().getConstraints(0).hasBoolXor()).isTrue();
assertThat(model.model().getConstraints(0).getBoolXor().getLiteralsCount()).isEqualTo(3);
}
@Test
public void testCpModel_addImplication() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
model.addImplication(x, y);
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
assertThat(model.model().getConstraints(0).hasBoolOr()).isTrue();
assertThat(model.model().getConstraints(0).getBoolOr().getLiteralsCount()).isEqualTo(2);
}
@Test
public void testCpModel_addLinear() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
model.addEquality(LinearExpr.sum(new IntVar[] {x, y}), 1);
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
assertThat(model.model().getConstraints(0).hasLinear()).isTrue();
assertThat(model.model().getConstraints(0).getLinear().getVarsCount()).isEqualTo(2);
final IntVar b = model.newBoolVar("b");
model.addEquality(LinearExpr.sum(new IntVar[] {x, y}), 2).onlyEnforceIf(b.not());
assertThat(model.model().getConstraintsCount()).isEqualTo(2);
assertThat(model.model().getConstraints(1).hasLinear()).isTrue();
assertThat(model.model().getConstraints(1).getEnforcementLiteralCount()).isEqualTo(1);
assertThat(model.model().getConstraints(1).getEnforcementLiteral(0)).isEqualTo(-3);
final IntVar c = model.newBoolVar("c");
model.addEquality(LinearExpr.sum(new IntVar[] {x, y}), 3).onlyEnforceIf(new Literal[] {b, c});
assertThat(model.model().getConstraintsCount()).isEqualTo(3);
assertThat(model.model().getConstraints(2).hasLinear()).isTrue();
assertThat(model.model().getConstraints(2).getEnforcementLiteralCount()).isEqualTo(2);
assertThat(model.model().getConstraints(2).getEnforcementLiteral(0)).isEqualTo(2);
assertThat(model.model().getConstraints(2).getEnforcementLiteral(1)).isEqualTo(3);
}
@Test
public void testCpModel_addNoOverlap() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final int horizon = 100;
final IntVar startVar1 = model.newIntVar(0, horizon, "start1");
final int duration1 = 10;
final IntervalVar interval1 = model.newFixedSizeIntervalVar(startVar1, duration1, "interval1");
final IntVar startVar2 = model.newIntVar(0, horizon, "start2");
final int duration2 = 15;
final IntervalVar interval2 = model.newFixedSizeIntervalVar(startVar2, duration2, "interval2");
model.addNoOverlap(new IntervalVar[] {interval1, interval2});
assertThat(model.model().getConstraintsCount()).isEqualTo(3);
assertThat(model.model().getConstraints(0).hasInterval()).isTrue();
assertThat(model.model().getConstraints(1).hasInterval()).isTrue();
assertThat(model.model().getConstraints(2).hasNoOverlap()).isTrue();
assertThat(model.model().getConstraints(2).getNoOverlap().getIntervalsCount()).isEqualTo(2);
}
@Test
public void testCpModel_addNoOverlap2D() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final int horizon = 100;
final IntVar startVar1 = model.newIntVar(0, horizon, "start1");
final int duration1 = 10;
final IntervalVar interval1 = model.newFixedSizeIntervalVar(startVar1, duration1, "interval1");
final IntVar startVar2 = model.newIntVar(0, horizon, "start2");
final int duration2 = 15;
final IntervalVar interval2 = model.newFixedSizeIntervalVar(startVar2, duration2, "interval2");
model.addNoOverlap2D(
new IntervalVar[] {interval1, interval2}, new IntervalVar[] {interval1, interval2});
assertThat(model.model().getConstraintsCount()).isEqualTo(3);
assertThat(model.model().getConstraints(0).hasInterval()).isTrue();
assertThat(model.model().getConstraints(1).hasInterval()).isTrue();
assertThat(model.model().getConstraints(2).hasNoOverlap2D()).isTrue();
assertThat(model.model().getConstraints(2).getNoOverlap2D().getXIntervalsCount()).isEqualTo(2);
assertThat(model.model().getConstraints(2).getNoOverlap2D().getYIntervalsCount()).isEqualTo(2);
}
@Test
public void testCpModel_modelStats() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
model.addImplication(x, y);
String stats = model.modelStats();
assertThat(stats).isNotEmpty();
}
@Test
public void testCpModel_validateOk() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
model.addImplication(x, y);
String stats = model.validate();
assertThat(stats).isEmpty();
}
@Test
public void testCpModel_validateNotOk() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newIntVar(0, 9223372036854775807L, "x");
final IntVar y = model.newIntVar(0, 10, "y");
model.addLinearExpressionInDomain(LinearExpr.sum(new IntVar[] {x, y}),
Domain.fromFlatIntervals(new long[] {6, 9223372036854775807L}));
String stats = model.validate();
assertThat(stats).isNotEmpty();
}
@Test
public void testCpModel_exceptionVisibility() throws Exception {
CpModel.MismatchedArrayLengths ex1 = new CpModel.MismatchedArrayLengths("test1", "ar1", "ar2");
CpModel.WrongLength ex2 = new CpModel.WrongLength("test2", "ar");
assertThat(ex1).hasMessageThat().isEqualTo("test1: ar1 and ar2 have mismatched lengths");
assertThat(ex2).hasMessageThat().isEqualTo("test2: ar");
}
}