Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
SolverHelper.cs
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15{
16using System;
17using System.Collections.Generic;
18
19public partial class Solver : IDisposable
20{
21 public IntVar[] MakeIntVarArray(int count, long min, long max)
22 {
23 IntVar[] array = new IntVar[count];
24 for (int i = 0; i < count; ++i)
25 {
26 array[i] = MakeIntVar(min, max);
27 }
28 return array;
29 }
30
31 public IntVar[] MakeIntVarArray(int count, long min, long max, string name)
32 {
33 IntVar[] array = new IntVar[count];
34 for (int i = 0; i < count; ++i)
35 {
36 string var_name = name + i;
37 array[i] = MakeIntVar(min, max, var_name);
38 }
39 return array;
40 }
41
42 public IntVar[] MakeIntVarArray(int count, long[] values)
43 {
44 IntVar[] array = new IntVar[count];
45 for (int i = 0; i < count; ++i)
46 {
47 array[i] = MakeIntVar(values);
48 }
49 return array;
50 }
51
52 public IntVar[] MakeIntVarArray(int count, long[] values, string name)
53 {
54 IntVar[] array = new IntVar[count];
55 for (int i = 0; i < count; ++i)
56 {
57 string var_name = name + i;
58 array[i] = MakeIntVar(values, var_name);
59 }
60 return array;
61 }
62
63 public IntVar[] MakeIntVarArray(int count, int[] values)
64 {
65 IntVar[] array = new IntVar[count];
66 for (int i = 0; i < count; ++i)
67 {
68 array[i] = MakeIntVar(values);
69 }
70 return array;
71 }
72
73 public IntVar[] MakeIntVarArray(int count, int[] values, string name)
74 {
75 IntVar[] array = new IntVar[count];
76 for (int i = 0; i < count; ++i)
77 {
78 string var_name = name + i;
79 array[i] = MakeIntVar(values, var_name);
80 }
81 return array;
82 }
83
84 public IntVar[] MakeBoolVarArray(int count)
85 {
86 IntVar[] array = new IntVar[count];
87 for (int i = 0; i < count; ++i)
88 {
89 array[i] = MakeBoolVar();
90 }
91 return array;
92 }
93
94 public IntVar[] MakeBoolVarArray(int count, string name)
95 {
96 IntVar[] array = new IntVar[count];
97 for (int i = 0; i < count; ++i)
98 {
99 string var_name = name + i;
100 array[i] = MakeBoolVar(var_name);
101 }
102 return array;
103 }
104
105 public IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max)
106 {
107 IntVar[,] array = new IntVar[rows, cols];
108 for (int i = 0; i < rows; ++i)
109 {
110 for (int j = 0; j < cols; ++j)
111 {
112 array[i, j] = MakeIntVar(min, max);
113 }
114 }
115 return array;
116 }
117
118 public IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max, string name)
119 {
120 IntVar[,] array = new IntVar[rows, cols];
121 for (int i = 0; i < rows; ++i)
122 {
123 for (int j = 0; j < cols; ++j)
124 {
125 string var_name = name + "[" + i + ", " + j + "]";
126 array[i, j] = MakeIntVar(min, max, var_name);
127 }
128 }
129 return array;
130 }
131
132 public IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values)
133 {
134 IntVar[,] array = new IntVar[rows, cols];
135 for (int i = 0; i < rows; ++i)
136 {
137 for (int j = 0; j < cols; ++j)
138 {
139 array[i, j] = MakeIntVar(values);
140 }
141 }
142 return array;
143 }
144
145 public IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values, string name)
146 {
147 IntVar[,] array = new IntVar[rows, cols];
148 for (int i = 0; i < rows; ++i)
149 {
150 for (int j = 0; j < cols; ++j)
151 {
152 string var_name = name + "[" + i + ", " + j + "]";
153 array[i, j] = MakeIntVar(values, var_name);
154 }
155 }
156 return array;
157 }
158
159 public IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values)
160 {
161 IntVar[,] array = new IntVar[rows, cols];
162 for (int i = 0; i < rows; ++i)
163 {
164 for (int j = 0; j < cols; ++j)
165 {
166 array[i, j] = MakeIntVar(values);
167 }
168 }
169 return array;
170 }
171
172 public IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values, string name)
173 {
174 IntVar[,] array = new IntVar[rows, cols];
175 for (int i = 0; i < rows; ++i)
176 {
177 for (int j = 0; j < cols; ++j)
178 {
179 string var_name = name + "[" + i + ", " + j + "]";
180 array[i, j] = MakeIntVar(values, var_name);
181 }
182 }
183 return array;
184 }
185
186 public IntVar[,] MakeBoolVarMatrix(int rows, int cols)
187 {
188 IntVar[,] array = new IntVar[rows, cols];
189 for (int i = 0; i < rows; ++i)
190 {
191 for (int j = 0; j < cols; ++j)
192 {
193 array[i, j] = MakeBoolVar();
194 }
195 }
196 return array;
197 }
198
199 public IntVar[,] MakeBoolVarMatrix(int rows, int cols, string name)
200 {
201 IntVar[,] array = new IntVar[rows, cols];
202 for (int i = 0; i < rows; ++i)
203 {
204 for (int j = 0; j < cols; ++j)
205 {
206 string var_name = name + "[" + i + ", " + j + "]";
207 array[i, j] = MakeBoolVar(var_name);
208 }
209 }
210 return array;
211 }
212
213 public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration,
214 bool optional)
215 {
216 IntervalVar[] array = new IntervalVar[count];
217 for (int i = 0; i < count; ++i)
218 {
219 array[i] = MakeFixedDurationIntervalVar(start_min, start_max, duration, optional, "");
220 }
221 return array;
222 }
223
224 public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration,
225 bool optional, string name)
226 {
227 IntervalVar[] array = new IntervalVar[count];
228 for (int i = 0; i < count; ++i)
229 {
230 array[i] = MakeFixedDurationIntervalVar(start_min, start_max, duration, optional, name + i);
231 }
232 return array;
233 }
234
235 public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long[] start_min, long[] start_max,
236 long[] duration, bool optional, string name)
237 {
238 IntervalVar[] array = new IntervalVar[count];
239 for (int i = 0; i < count; ++i)
240 {
241 array[i] = MakeFixedDurationIntervalVar(start_min[i], start_max[i], duration[i], optional, name + i);
242 }
243 return array;
244 }
245
246 public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, int[] start_min, int[] start_max, int[] duration,
247 bool optional, string name)
248 {
249 IntervalVar[] array = new IntervalVar[count];
250 for (int i = 0; i < count; ++i)
251 {
252 array[i] = MakeFixedDurationIntervalVar(start_min[i], start_max[i], duration[i], optional, name + i);
253 }
254 return array;
255 }
256 public IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, int[] durations, string name)
257 {
258 int count = starts.Length;
259 IntervalVar[] array = new IntervalVar[count];
260 for (int i = 0; i < count; ++i)
261 {
262 array[i] = MakeFixedDurationIntervalVar(starts[i], durations[i], name + i);
263 }
264 return array;
265 }
266 public IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, long[] durations, string name)
267 {
268 int count = starts.Length;
269 IntervalVar[] array = new IntervalVar[count];
270 for (int i = 0; i < count; ++i)
271 {
272 array[i] = MakeFixedDurationIntervalVar(starts[i], durations[i], name + i);
273 }
274 return array;
275 }
277 {
278 pinned_decision_builder_ = db;
279 pinned_search_monitors_.Clear();
280 NewSearchAux(db);
281 }
282
284 {
285 pinned_decision_builder_ = db;
286 pinned_search_monitors_.Clear();
287 pinned_search_monitors_.Add(sm1);
288 NewSearchAux(db, sm1);
289 }
290
292 {
293 pinned_decision_builder_ = db;
294 pinned_search_monitors_.Clear();
295 pinned_search_monitors_.Add(sm1);
296 pinned_search_monitors_.Add(sm2);
297 NewSearchAux(db, sm1, sm2);
298 }
299
301 {
302 pinned_decision_builder_ = db;
303 pinned_search_monitors_.Clear();
304 pinned_search_monitors_.Add(sm1);
305 pinned_search_monitors_.Add(sm2);
306 pinned_search_monitors_.Add(sm3);
307 NewSearchAux(db, sm1, sm2, sm3);
308 }
309
311 SearchMonitor sm4)
312 {
313 pinned_decision_builder_ = db;
314 pinned_search_monitors_.Clear();
315 pinned_search_monitors_.Add(sm1);
316 pinned_search_monitors_.Add(sm2);
317 pinned_search_monitors_.Add(sm3);
318 pinned_search_monitors_.Add(sm4);
319 NewSearchAux(db, sm1, sm2, sm3, sm4);
320 }
321
322 public void NewSearch(DecisionBuilder db, SearchMonitor[] monitors)
323 {
324 pinned_decision_builder_ = db;
325 pinned_search_monitors_.Clear();
326 pinned_search_monitors_.AddRange(monitors);
327 NewSearchAux(db, monitors);
328 }
329
330 public void EndSearch()
331 {
332 pinned_decision_builder_ = null;
333 pinned_search_monitors_.Clear();
334 EndSearchAux();
335 }
336
337 private System.Collections.Generic.List<SearchMonitor> pinned_search_monitors_ =
338 new System.Collections.Generic.List<SearchMonitor>();
339 private DecisionBuilder pinned_decision_builder_;
340}
341
342public partial class IntExpr : PropagationBaseObject
343{
344 public static IntExpr operator +(IntExpr a, IntExpr b)
345 {
346 return a.solver().MakeSum(a, b);
347 }
348 public static IntExpr operator +(IntExpr a, long v)
349 {
350 return a.solver().MakeSum(a, v);
351 }
352 public static IntExpr operator +(long v, IntExpr a)
353 {
354 return a.solver().MakeSum(a, v);
355 }
356 public static IntExpr operator -(IntExpr a, IntExpr b)
357 {
358 return a.solver().MakeDifference(a, b);
359 }
360 public static IntExpr operator -(IntExpr a, long v)
361 {
362 return a.solver().MakeSum(a, -v);
363 }
364 public static IntExpr operator -(long v, IntExpr a)
365 {
366 return a.solver().MakeDifference(v, a);
367 }
368 public static IntExpr operator *(IntExpr a, IntExpr b)
369 {
370 return a.solver().MakeProd(a, b);
371 }
372 public static IntExpr operator *(IntExpr a, long v)
373 {
374 return a.solver().MakeProd(a, v);
375 }
376 public static IntExpr operator *(long v, IntExpr a)
377 {
378 return a.solver().MakeProd(a, v);
379 }
380 public static IntExpr operator /(IntExpr a, long v)
381 {
382 return a.solver().MakeDiv(a, v);
383 }
384 public static IntExpr operator %(IntExpr a, long v)
385 {
386 return a.solver().MakeModulo(a, v);
387 }
388 public static IntExpr operator -(IntExpr a)
389 {
390 return a.solver().MakeOpposite(a);
391 }
392 public IntExpr Abs()
393 {
394 return this.solver().MakeAbs(this);
395 }
397 {
398 return this.solver().MakeSquare(this);
399 }
401 {
402 return new IntExprEquality(a, b, true);
403 }
405 {
406 return new IntExprEquality(a, b, false);
407 }
408 public static WrappedConstraint operator ==(IntExpr a, long v)
409 {
410 return new WrappedConstraint(a.solver().MakeEquality(a, v));
411 }
412 public static WrappedConstraint operator !=(IntExpr a, long v)
413 {
414 return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v));
415 }
416 public static WrappedConstraint operator >=(IntExpr a, long v)
417 {
418 return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a, v));
419 }
420 public static WrappedConstraint operator>(IntExpr a, long v)
421 {
422 return new WrappedConstraint(a.solver().MakeGreater(a, v));
423 }
424 public static WrappedConstraint operator <=(IntExpr a, long v)
425 {
426 return new WrappedConstraint(a.solver().MakeLessOrEqual(a, v));
427 }
428 public static WrappedConstraint operator<(IntExpr a, long v)
429 {
430 return new WrappedConstraint(a.solver().MakeLess(a, v));
431 }
433 {
434 return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var()));
435 }
437 {
438 return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var()));
439 }
441 {
442 return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var()));
443 }
445 {
446 return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var()));
447 }
448}
449
450public partial class Constraint : PropagationBaseObject, IConstraintWithStatus
451{
452 public static implicit operator IntVar(Constraint eq)
453 {
454 return eq.Var();
455 }
456
457 public static implicit operator IntExpr(Constraint eq)
458 {
459 return eq.Var();
460 }
462 {
463 return a.solver().MakeSum(a.Var(), b.Var());
464 }
465 public static IntExpr operator +(Constraint a, long v)
466 {
467 return a.solver().MakeSum(a.Var(), v);
468 }
469 public static IntExpr operator +(long v, Constraint a)
470 {
471 return a.solver().MakeSum(a.Var(), v);
472 }
474 {
475 return a.solver().MakeDifference(a.Var(), b.Var());
476 }
477 public static IntExpr operator -(Constraint a, long v)
478 {
479 return a.solver().MakeSum(a.Var(), -v);
480 }
481 public static IntExpr operator -(long v, Constraint a)
482 {
483 return a.solver().MakeDifference(v, a.Var());
484 }
486 {
487 return a.solver().MakeProd(a.Var(), b.Var());
488 }
489 public static IntExpr operator *(Constraint a, long v)
490 {
491 return a.solver().MakeProd(a.Var(), v);
492 }
493 public static IntExpr operator *(long v, Constraint a)
494 {
495 return a.solver().MakeProd(a.Var(), v);
496 }
497 public static IntExpr operator /(Constraint a, long v)
498 {
499 return a.solver().MakeDiv(a.Var(), v);
500 }
502 {
503 return a.solver().MakeOpposite(a.Var());
504 }
505 public IntExpr Abs()
506 {
507 return this.solver().MakeAbs(this.Var());
508 }
510 {
511 return this.solver().MakeSquare(this.Var());
512 }
513 public static WrappedConstraint operator ==(Constraint a, long v)
514 {
515 return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v));
516 }
517 public static WrappedConstraint operator ==(long v, Constraint a)
518 {
519 return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v));
520 }
521 public static WrappedConstraint operator !=(Constraint a, long v)
522 {
523 return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v));
524 }
525 public static WrappedConstraint operator !=(long v, Constraint a)
526 {
527 return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v));
528 }
529 public static WrappedConstraint operator >=(Constraint a, long v)
530 {
531 return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v));
532 }
533 public static WrappedConstraint operator >=(long v, Constraint a)
534 {
535 return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v));
536 }
537 public static WrappedConstraint operator>(Constraint a, long v)
538 {
539 return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v));
540 }
541 public static WrappedConstraint operator>(long v, Constraint a)
542 {
543 return new WrappedConstraint(a.solver().MakeLess(a.Var(), v));
544 }
545 public static WrappedConstraint operator <=(Constraint a, long v)
546 {
547 return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v));
548 }
549 public static WrappedConstraint operator <=(long v, Constraint a)
550 {
551 return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v));
552 }
553 public static WrappedConstraint operator<(Constraint a, long v)
554 {
555 return new WrappedConstraint(a.solver().MakeLess(a.Var(), v));
556 }
557 public static WrappedConstraint operator<(long v, Constraint a)
558 {
559 return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v));
560 }
562 {
563 return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var()));
564 }
566 {
567 return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var()));
568 }
570 {
571 return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var()));
572 }
574 {
575 return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var()));
576 }
578 {
579 return new ConstraintEquality(a, b, true);
580 }
582 {
583 return new ConstraintEquality(a, b, false);
584 }
585}
586} // namespace Google.OrTools.ConstraintSolver
static WrappedConstraint operator>=(Constraint a, long v)
static WrappedConstraint operator>(long v, Constraint a)
static WrappedConstraint operator<(Constraint a, Constraint b)
static WrappedConstraint operator==(Constraint a, long v)
static WrappedConstraint operator>(Constraint a, Constraint b)
static IntExpr operator*(Constraint a, Constraint b)
static IntExpr operator+(Constraint a, Constraint b)
static WrappedConstraint operator!=(Constraint a, long v)
static IntExpr operator-(Constraint a, Constraint b)
static WrappedConstraint operator<=(Constraint a, long v)
static IntExpr operator/(Constraint a, long v)
static WrappedConstraint operator<(Constraint a, long v)
static WrappedConstraint operator>(Constraint a, long v)
static WrappedConstraint operator<(long v, Constraint a)
static WrappedConstraint operator>(IntExpr a, long v)
static IntExpr operator*(IntExpr a, IntExpr b)
static IntExpr operator+(IntExpr a, IntExpr b)
static WrappedConstraint operator<(IntExpr a, IntExpr b)
static IntExprEquality operator!=(IntExpr a, IntExpr b)
static IntExpr operator-(IntExpr a, IntExpr b)
static IntExpr operator%(IntExpr a, long v)
static WrappedConstraint operator>(IntExpr a, IntExpr b)
static IntExprEquality operator==(IntExpr a, IntExpr b)
static WrappedConstraint operator<(IntExpr a, long v)
static IntExpr operator/(IntExpr a, long v)
static WrappedConstraint operator>=(IntExpr a, long v)
static WrappedConstraint operator<=(IntExpr a, long v)
IntVar[] MakeIntVarArray(int count, long min, long max, string name)
IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max)
IntVar[] MakeBoolVarArray(int count, string name)
void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2, SearchMonitor sm3, SearchMonitor sm4)
void NewSearchAux(DecisionBuilder db, SearchMonitorVector monitors)
Definition Solver.cs:339
IntVar MakeIntVar(long min, long max, string name)
Definition Solver.cs:510
IntervalVar[] MakeFixedDurationIntervalVarArray(int count, int[] start_min, int[] start_max, int[] duration, bool optional, string name)
IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values)
IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values, string name)
IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values, string name)
IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, int[] durations, string name)
IntervalVar MakeFixedDurationIntervalVar(long start_min, long start_max, long duration, bool optional, string name)
Definition Solver.cs:1623
IntVar[] MakeIntVarArray(int count, int[] values, string name)
IntVar[] MakeIntVarArray(int count, int[] values)
IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration, bool optional)
IntVar[] MakeIntVarArray(int count, long[] values)
IntVar[] MakeIntVarArray(int count, long[] values, string name)
void NewSearch(DecisionBuilder db, SearchMonitor[] monitors)
IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max, string name)
IntVar[,] MakeBoolVarMatrix(int rows, int cols)
void NewSearch(DecisionBuilder db, SearchMonitor sm1)
IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long[] start_min, long[] start_max, long[] duration, bool optional, string name)
void NewSearch(DecisionBuilder db)
void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2)
void NewSearch(DecisionBuilder db, SearchMonitor sm1, SearchMonitor sm2, SearchMonitor sm3)
IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, long[] durations, string name)
IntVar[,] MakeBoolVarMatrix(int rows, int cols, string name)
IntVar[] MakeIntVarArray(int count, long min, long max)
IntExpr MakeSquare(IntExpr expr)
Definition Solver.cs:656
IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values)
IntervalVar[] MakeFixedDurationIntervalVarArray(int count, long start_min, long start_max, long duration, bool optional, string name)
IntExpr MakeAbs(IntExpr expr)
Definition Solver.cs:650