forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SatSolverTests.cs
342 lines (298 loc) · 12 KB
/
SatSolverTests.cs
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
using System;
using System.Collections.Generic;
using Xunit;
using Google.OrTools.Sat;
namespace Google.OrTools.Tests {
public class SatSolverTest {
static IntegerVariableProto NewIntegerVariable(long lb, long ub) {
IntegerVariableProto var = new IntegerVariableProto();
var.Domain.Add(lb);
var.Domain.Add(ub);
return var;
}
static ConstraintProto NewLinear2(
int v1, int v2,
long c1, long c2,
long lb, long ub) {
LinearConstraintProto linear = new LinearConstraintProto();
linear.Vars.Add(v1);
linear.Vars.Add(v2);
linear.Coeffs.Add(c1);
linear.Coeffs.Add(c2);
linear.Domain.Add(lb);
linear.Domain.Add(ub);
ConstraintProto ct = new ConstraintProto();
ct.Linear = linear;
return ct;
}
static ConstraintProto NewLinear3(
int v1, int v2, int v3,
long c1, long c2, long c3,
long lb, long ub) {
LinearConstraintProto linear = new LinearConstraintProto();
linear.Vars.Add(v1);
linear.Vars.Add(v2);
linear.Vars.Add(v3);
linear.Coeffs.Add(c1);
linear.Coeffs.Add(c2);
linear.Coeffs.Add(c3);
linear.Domain.Add(lb);
linear.Domain.Add(ub);
ConstraintProto ct = new ConstraintProto();
ct.Linear = linear;
return ct;
}
static CpObjectiveProto NewMinimize1(int v1, long c1) {
CpObjectiveProto obj = new CpObjectiveProto();
obj.Vars.Add(v1);
obj.Coeffs.Add(c1);
return obj;
}
static CpObjectiveProto NewMaximize1(int v1, long c1) {
CpObjectiveProto obj = new CpObjectiveProto();
obj.Vars.Add(-v1 - 1);
obj.Coeffs.Add(c1);
obj.ScalingFactor = -1;
return obj;
}
static CpObjectiveProto NewMaximize2(int v1, int v2, long c1, long c2) {
CpObjectiveProto obj = new CpObjectiveProto();
obj.Vars.Add(-v1 - 1);
obj.Vars.Add(-v2 - 1);
obj.Coeffs.Add(c1);
obj.Coeffs.Add(c2);
obj.ScalingFactor = -1;
return obj;
}
// CpModelProto
[Fact]
public void SimpleLinearModelProto() {
CpModelProto model = new CpModelProto();
model.Variables.Add(NewIntegerVariable(-10, 10));
model.Variables.Add(NewIntegerVariable(-10, 10));
model.Variables.Add(NewIntegerVariable(-1000000, 1000000));
model.Constraints.Add(NewLinear2(0, 1 , 1, 1, -1000000, 100000));
model.Constraints.Add(NewLinear3(0, 1, 2, 1, 2, -1, 0, 100000));
model.Objective = NewMaximize1(2, 1);
//Console.WriteLine("model = " + model.ToString());
CpSolverResponse response = SatHelper.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, response.Status);
Assert.Equal(30, response.ObjectiveValue);
Assert.Equal(new long[] {10, 10, 30}, response.Solution);
//Console.WriteLine("response = " + response.ToString());
}
[Fact]
public void SimpleLinearModelProto2() {
CpModelProto model = new CpModelProto();
model.Variables.Add(NewIntegerVariable(-10, 10));
model.Variables.Add(NewIntegerVariable(-10, 10));
model.Constraints.Add(NewLinear2(0, 1 , 1, 1, -1000000, 100000));
model.Objective = NewMaximize2(0, 1, 1, -2);
//Console.WriteLine("model = " + model.ToString());
CpSolverResponse response = SatHelper.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, response.Status);
Assert.Equal(30, response.ObjectiveValue);
Assert.Equal(new long[] {10, -10}, response.Solution);
//Console.WriteLine("response = " + response.ToString());
}
// CpModel
[Fact]
public void SimpleLinearModel() {
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
IntVar v3 = model.NewIntVar(-100000, 100000, "v3");
model.AddLinearConstraint(v1 + v2, -1000000, 100000);
model.AddLinearConstraint(v1 + 2 * v2 - v3, 0, 100000);
model.Maximize(v3);
Assert.Equal(v1.Domain.FlattenedIntervals(),
new long[] { -10, 10 });
//Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(30, response.ObjectiveValue);
Assert.Equal(new long[] {10, 10, 30}, response.Solution);
//Console.WriteLine("response = " + reponse.ToString());
}
[Fact]
public void SimpleLinearModel2() {
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
model.AddLinearConstraint(v1 + v2, -1000000, 100000);
model.Maximize(v1 - 2 * v2);
//Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(30, response.ObjectiveValue);
Assert.Equal(new long[] {10, -10}, response.Solution);
//Console.WriteLine("response = " + reponse.ToString());
}
[Fact]
public void SimpleLinearModel3() {
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(-10, 10, "v1");
IntVar v2 = model.NewIntVar(-10, 10, "v2");
model.Add(-100000 <= v1 + 2 * v2 <= 100000);
model.Minimize(v1 - 2 * v2);
//Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(-10, solver.Value(v1));
Assert.Equal(10, solver.Value(v2));
Assert.Equal(new long[] {-10, 10}, response.Solution);
Assert.Equal(-30, solver.Value(v1 - 2 * v2));
Assert.Equal(-30, response.ObjectiveValue);
//Console.WriteLine("response = " + reponse.ToString());
}
[Fact]
public void NegativeIntVar() {
CpModel model = new CpModel();
IntVar boolvar = model.NewBoolVar("boolvar");
IntVar x = model.NewIntVar(0,10, "x");
IntVar delta = model.NewIntVar(-5, 5,"delta");
IntVar squaredDelta = model.NewIntVar(0, 25,"squaredDelta");
model.Add(x == boolvar * 4);
model.Add(delta == x - 5 );
model.AddProdEquality(squaredDelta, new IntVar[] {delta, delta});
model.Minimize(squaredDelta);
//Console.WriteLine("model = " + model.Model.ToString());
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
CpSolverResponse response = solver.Response;
Console.WriteLine("response = " + response.ToString());
Assert.Equal(CpSolverStatus.Optimal, status);
Assert.Equal(1, solver.Value(boolvar));
Assert.Equal(4, solver.Value(x));
Assert.Equal(-1, solver.Value(delta));
Assert.Equal(1, solver.Value(squaredDelta));
Assert.Equal(new long[] {1, 4, -1, 1}, response.Solution);
Assert.Equal(1.0, response.ObjectiveValue, 5);
}
[Fact]
public void NegativeSquareVar() {
CpModel model = new CpModel();
IntVar boolvar = model.NewBoolVar("boolvar");
IntVar x = model.NewIntVar(0,10, "x");
IntVar delta = model.NewIntVar(-5, 5,"delta");
IntVar squaredDelta = model.NewIntVar(0, 25,"squaredDelta");
model.Add(x == 4).OnlyEnforceIf(boolvar);
model.Add(x == 0).OnlyEnforceIf(boolvar.Not());
model.Add(delta == x - 5 );
long[,] tuples = { {-5, 25}, {-4, 16}, {-3, 9}, {-2, 4}, {-1, 1}, {0, 0},
{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25} };
model.AddAllowedAssignments(new IntVar[] {delta, squaredDelta}, tuples);
model.Minimize(squaredDelta);
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
CpSolverResponse response = solver.Response;
Assert.Equal(1, solver.Value(boolvar));
Assert.Equal(4, solver.Value(x));
Assert.Equal(-1, solver.Value(delta));
Assert.Equal(1, solver.Value(squaredDelta));
Assert.Equal(new long[] {1, 4, -1, 1}, response.Solution);
Assert.Equal(1.0, response.ObjectiveValue, 6);
}
[Fact]
public void Division() {
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(0, 10, "v1");
IntVar v2 = model.NewIntVar(1, 10, "v2");
model.AddDivisionEquality(3, v1, v2);
//Console.WriteLine(model.Model);
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(3, solver.Value(v1));
Assert.Equal(1, solver.Value(v2));
Assert.Equal(new long[] {3, 1, 3}, response.Solution);
Assert.Equal(0, response.ObjectiveValue);
//Console.WriteLine("response = " + reponse.ToString());
}
[Fact]
public void Modulo() {
CpModel model = new CpModel();
IntVar v1 = model.NewIntVar(1, 10, "v1");
IntVar v2 = model.NewIntVar(1, 10, "v2");
model.AddModuloEquality(3, v1, v2);
//Console.WriteLine(model.Model);
CpSolver solver = new CpSolver();
CpSolverStatus status = solver.Solve(model);
Assert.Equal(CpSolverStatus.Optimal, status);
CpSolverResponse response = solver.Response;
Assert.Equal(3, solver.Value(v1));
Assert.Equal(4, solver.Value(v2));
Assert.Equal(new long[] {3, 4, 3}, response.Solution);
Assert.Equal(0, response.ObjectiveValue);
//Console.WriteLine("response = " + reponse.ToString());
}
[Fact]
public void LargeScalProdLong() {
CpModel model = new CpModel();
List<IntVar> vars = new List<IntVar>();
List<long> coeffs = new List<long>();
for (int i = 0; i < 100000; ++i) {
vars.Add(model.NewBoolVar(""));
coeffs.Add(i + 1);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
model.Minimize(LinearExpr.ScalProd(vars, coeffs));
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"Long: Elapsed time {elapsedMs}");
}
[Fact]
public void LargeScalProdInt() {
CpModel model = new CpModel();
List<IntVar> vars = new List<IntVar>();
List<int> coeffs = new List<int>();
for (int i = 0; i < 100000; ++i) {
vars.Add(model.NewBoolVar(""));
coeffs.Add(i);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
model.Minimize(LinearExpr.ScalProd(vars, coeffs));
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"Int: Elapsed time {elapsedMs}");
}
[Fact]
public void LargeScalProdExpr() {
CpModel model = new CpModel();
List<LinearExpr> exprs = new List<LinearExpr>();
for (int i = 0; i < 100000; ++i) {
exprs.Add(model.NewBoolVar("") * i);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
model.Minimize(LinearExpr.Sum(exprs));
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"Exprs: Elapsed time {elapsedMs}");
}
[Fact]
public void LargeScalProdProto() {
CpModel model = new CpModel();
List<IntVar> vars = new List<IntVar>();
List<long> coeffs = new List<long>();
for (int i = 0; i < 100000; ++i) {
vars.Add(model.NewBoolVar(""));
coeffs.Add(i + 1);
}
var watch = System.Diagnostics.Stopwatch.StartNew();
model.Minimize();
for (int i = 0; i < 100000; ++i) {
model.AddTermToObjective(vars[i], coeffs[i]);
}
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine($"Proto: Elapsed time {elapsedMs}");
}
}
} // namespace Google.OrTools.Tests