DotNet Reference

.Net Reference

IntegerExpressions.cs
Go to the documentation of this file.
1// Copyright 2010-2021 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
14namespace Google.OrTools.Sat
15{
16using Google.OrTools.Util;
17using System;
18using System.Collections;
19using System.Collections.Generic;
20using System.Linq;
21using System.Runtime.CompilerServices;
22using Google.Protobuf.Collections;
23
25public interface ILiteral
26{
30 int GetIndex();
33}
34
35internal static class HelperExtensions
36{
37 [MethodImpl(MethodImplOptions.AggressiveInlining)]
38 public static void AddOrIncrement(this Dictionary<int, long> dict, int key, long increment)
39 {
40#if NET6_0_OR_GREATER
41 System.Runtime.InteropServices.CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out _) += increment;
42#else
43 if (dict.TryGetValue(key, out var value))
44 {
45 dict[key] = value + increment;
46 }
47 else
48 {
49 dict.Add(key, increment);
50 }
51#endif
52 }
53
54 [MethodImpl(MethodImplOptions.AggressiveInlining)]
55 internal static void TrySetCapacity<TField, TValues>(this RepeatedField<TField> field, IEnumerable<TValues> values)
56 {
57 if (values is ICollection<TValues> collection)
58 {
59 field.Capacity = collection.Count;
60 }
61 }
62
63 [MethodImpl(MethodImplOptions.AggressiveInlining)]
64 internal static void TryEnsureCapacity<TValue, TValues>(this List<TValue> list, IEnumerable<TValues> values)
65 {
66 // Check for ICollection as the generic version is not covariant and TValues could be LinearExpr, IntVar, ...
67 if (values is ICollection collection)
68 {
69 list.Capacity = Math.Max(list.Count + collection.Count, list.Capacity);
70 }
71 }
72
73#if NETFRAMEWORK
74 [MethodImpl(MethodImplOptions.AggressiveInlining)]
75 internal static bool TryDequeue<T>(this Queue<T> queue, out T value)
76 {
77 if (queue.Count > 0)
78 {
79 value = queue.Dequeue();
80 return true;
81 }
82
83 value = default;
84 return false;
85 }
86#endif
87}
88
89// Holds a term (expression * coefficient)
90public struct Term
91{
93 public long coefficient;
94
95 public Term(LinearExpr e, long c)
96 {
97 this.expr = e;
98 this.coefficient = c;
99 }
100}
101
107public class LinearExpr
108{
110 public static LinearExpr Sum(IEnumerable<LinearExpr> exprs)
111 {
112 return NewBuilder(0).AddSum(exprs);
113 }
114
116 public static LinearExpr Sum(IEnumerable<ILiteral> literals)
117 {
118 return NewBuilder(0).AddSum(literals);
119 }
120
122 public static LinearExpr Sum(IEnumerable<BoolVar> vars)
123 {
124 return NewBuilder(0).AddSum(vars);
125 }
126
128 public static LinearExpr WeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<int> coeffs)
129 {
130 return NewBuilder(0).AddWeightedSum(exprs, coeffs);
131 }
132
134 public static LinearExpr WeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<long> coeffs)
135 {
136 return NewBuilder(0).AddWeightedSum(exprs, coeffs);
137 }
138
140 public static LinearExpr WeightedSum(IEnumerable<ILiteral> literals, IEnumerable<int> coeffs)
141 {
142 return NewBuilder(0).AddWeightedSum(literals, coeffs);
143 }
144
146 public static LinearExpr WeightedSum(IEnumerable<ILiteral> literals, IEnumerable<long> coeffs)
147 {
148 return NewBuilder(0).AddWeightedSum(literals, coeffs);
149 }
150
152 public static LinearExpr WeightedSum(IEnumerable<BoolVar> vars, IEnumerable<int> coeffs)
153 {
154 return NewBuilder(0).AddWeightedSum(vars, coeffs);
155 }
156
158 public static LinearExpr WeightedSum(IEnumerable<BoolVar> vars, IEnumerable<long> coeffs)
159 {
160 return NewBuilder(0).AddWeightedSum(vars, coeffs);
161 }
162
164 public static LinearExpr Term(LinearExpr expr, long coeff)
165 {
166 return Prod(expr, coeff);
167 }
168
170 public static LinearExpr Term(ILiteral literal, long coeff)
171 {
172 if (literal is BoolVar boolVar)
173 {
174 return Prod(boolVar, coeff);
175 }
176 else
177 {
178 return Affine(literal.NotAsExpr(), -coeff, coeff);
179 }
180 }
181
183 public static LinearExpr Term(BoolVar var, long coeff)
184 {
185 return Prod(var, coeff);
186 }
187
189 public static LinearExpr Affine(LinearExpr expr, long coeff, long offset)
190 {
191 if (offset == 0)
192 {
193 return Prod(expr, coeff);
194 }
195 return NewBuilder().AddTerm(expr, coeff).Add(offset);
196 }
197
199 public static LinearExpr Affine(ILiteral literal, long coeff, long offset)
200 {
201 return NewBuilder().AddTerm(literal, coeff).Add(offset);
202 }
203
205 public static LinearExpr Affine(BoolVar var, long coeff, long offset)
206 {
207 return NewBuilder().AddTerm(var, coeff).Add(offset);
208 }
209
211 public static LinearExpr Constant(long value)
212 {
213 return NewBuilder(0).Add(value);
214 }
215
217 public static LinearExprBuilder NewBuilder(int sizeHint = 2)
218 {
219 return new LinearExprBuilder(sizeHint);
220 }
221
223 {
224 return NewBuilder().Add(a).Add(b);
225 }
226
227 public static LinearExpr operator +(LinearExpr a, long v)
228 {
229 return NewBuilder().Add(a).Add(v);
230 }
231
232 public static LinearExpr operator +(long v, LinearExpr a)
233 {
234 return NewBuilder().Add(a).Add(v);
235 }
236
238 {
239 return NewBuilder().Add(a).AddTerm(b, -1);
240 }
241
242 public static LinearExpr operator -(LinearExpr a, long v)
243 {
244 return NewBuilder().Add(a).Add(-v);
245 }
246
247 public static LinearExpr operator -(long v, LinearExpr a)
248 {
249 return NewBuilder().AddTerm(a, -1).Add(v);
250 }
251
252 public static LinearExpr operator *(LinearExpr a, long v)
253 {
254 return Prod(a, v);
255 }
256
257 public static LinearExpr operator *(long v, LinearExpr a)
258 {
259 return Prod(a, v);
260 }
261
263 {
264 return Prod(a, -1);
265 }
266
268 {
269 return new BoundedLinearExpression(a, b, true);
270 }
271
273 {
274 return new BoundedLinearExpression(a, b, false);
275 }
276
278 {
279 return new BoundedLinearExpression(a, v, true);
280 }
281
283 {
284 return new BoundedLinearExpression(a, v, false);
285 }
286
288 {
289 return new BoundedLinearExpression(v, a, Int64.MaxValue);
290 }
291
293 {
294 return a <= v;
295 }
296
298 {
299 return new BoundedLinearExpression(v + 1, a, Int64.MaxValue);
300 }
301
303 {
304 return a < v;
305 }
306
308 {
309 return new BoundedLinearExpression(Int64.MinValue, a, v);
310 }
311
313 {
314 return a >= v;
315 }
316
318 {
319 return new BoundedLinearExpression(Int64.MinValue, a, v - 1);
320 }
321
323 {
324 return a > v;
325 }
326
328 {
329 return new BoundedLinearExpression(0, a - b, Int64.MaxValue);
330 }
331
333 {
334 return new BoundedLinearExpression(1, a - b, Int64.MaxValue);
335 }
336
338 {
339 return new BoundedLinearExpression(Int64.MinValue, a - b, 0);
340 }
341
343 {
344 return new BoundedLinearExpression(Int64.MinValue, a - b, -1);
345 }
346
347 internal static LinearExpr Prod(LinearExpr e, long v)
348 {
349 if (v == 0)
350 {
351 return NewBuilder(0);
352 }
353 else if (v == 1)
354 {
355 return e;
356 }
357 else
358 {
359 return NewBuilder(1).AddTerm(e, v);
360 }
361 }
362
363 internal static long GetVarValueMap(LinearExpr e, Dictionary<int, long> dict, Queue<Term> terms)
364 {
365 long constant = 0;
366 long coefficient = 1;
367 LinearExpr expr = e;
368 terms.Clear();
369
370 do
371 {
372 switch (expr)
373 {
374 case LinearExprBuilder builder:
375 constant += coefficient * builder.Offset;
376 if (coefficient == 1)
377 {
378 foreach (Term sub in builder.Terms)
379 {
380 terms.Enqueue(sub);
381 }
382 }
383 else
384 {
385 foreach (Term sub in builder.Terms)
386 {
387 terms.Enqueue(new Term(sub.expr, sub.coefficient * coefficient));
388 }
389 }
390 break;
391 case IntVar intVar:
392 dict.AddOrIncrement(intVar.GetIndex(), coefficient);
393 break;
394 case NotBoolVar notBoolVar:
395 dict.AddOrIncrement(notBoolVar.Not().GetIndex(), -coefficient);
396 constant += coefficient;
397 break;
398 default:
399 throw new ArgumentException("Cannot evaluate '" + expr + "' in an integer expression");
400 }
401
402 if (!terms.TryDequeue(out var term))
403 {
404 break;
405 }
406 expr = term.expr;
407 coefficient = term.coefficient;
408 } while (true);
409
410 return constant;
411 }
412
413 internal static LinearExpr RebuildLinearExprFromLinearExpressionProto(LinearExpressionProto proto,
414 CpModelProto model)
415 {
416 int numElements = proto.Vars.Count;
417 long offset = proto.Offset;
418 if (numElements == 0)
419 {
420 return LinearExpr.Constant(offset);
421 }
422 else if (numElements == 1)
423 {
424 IntVar var = new IntVar(model, proto.Vars[0]);
425 long coeff = proto.Coeffs[0];
426 return LinearExpr.Affine(var, coeff, offset);
427 }
428 else
429 {
430 LinearExprBuilder builder = LinearExpr.NewBuilder(numElements);
431 for (int i = 0; i < numElements; ++i)
432 {
433 builder.AddTerm(new IntVar(model, proto.Vars[i]), proto.Coeffs[i]);
434 }
435 builder.Add(offset);
436 return builder;
437 }
438 }
439}
440
442public sealed class LinearExprBuilder : LinearExpr
443{
444 public LinearExprBuilder(int sizeHint = 2)
445 {
446 terms_ = new List<Term>(sizeHint);
447 offset_ = 0;
448 }
449
452 {
453 return AddTerm(expr, 1);
454 }
455
458 {
459 return AddTerm(literal, 1);
460 }
461
464 {
465 return AddTerm(var, 1);
466 }
467
469 public LinearExprBuilder Add(long constant)
470 {
471 offset_ += constant;
472 return this;
473 }
474
476 public LinearExprBuilder AddTerm(LinearExpr expr, long coefficient)
477 {
478 terms_.Add(new Term(expr, coefficient));
479 return this;
480 }
481
483 public LinearExprBuilder AddTerm(ILiteral literal, long coefficient)
484 {
485 if (literal is BoolVar boolVar)
486 {
487 terms_.Add(new Term(boolVar, coefficient));
488 }
489 else
490 {
491 offset_ += coefficient;
492 terms_.Add(new Term(literal.NotAsExpr(), -coefficient));
493 }
494 return this;
495 }
496
498 public LinearExprBuilder AddTerm(BoolVar var, long coefficient)
499 {
500 terms_.Add(new Term(var, coefficient));
501 return this;
502 }
503
505 public LinearExprBuilder AddSum(IEnumerable<LinearExpr> exprs)
506 {
507 terms_.TryEnsureCapacity(exprs);
508 foreach (LinearExpr expr in exprs)
509 {
510 AddTerm(expr, 1);
511 }
512 return this;
513 }
514
516 public LinearExprBuilder AddSum(IEnumerable<ILiteral> literals)
517 {
518 terms_.TryEnsureCapacity(literals);
519 foreach (ILiteral literal in literals)
520 {
521 AddTerm(literal, 1);
522 }
523 return this;
524 }
525
527 public LinearExprBuilder AddSum(IEnumerable<BoolVar> vars)
528 {
529 terms_.TryEnsureCapacity(vars);
530 foreach (BoolVar var in vars)
531 {
532 AddTerm(var, 1);
533 }
534 return this;
535 }
536
538 public LinearExprBuilder AddWeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<long> coefficients)
539 {
540 terms_.TryEnsureCapacity(exprs);
541 foreach (var p in exprs.Zip(coefficients, (e, c) => new { Expr = e, Coeff = c }))
542 {
543 AddTerm(p.Expr, p.Coeff);
544 }
545 return this;
546 }
547
549 public LinearExprBuilder AddWeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<int> coefficients)
550 {
551 terms_.TryEnsureCapacity(exprs);
552 foreach (var p in exprs.Zip(coefficients, (e, c) => new { Expr = e, Coeff = c }))
553 {
554 AddTerm(p.Expr, p.Coeff);
555 }
556 return this;
557 }
558
560 public LinearExprBuilder AddWeightedSum(IEnumerable<ILiteral> literals, IEnumerable<int> coefficients)
561 {
562 terms_.TryEnsureCapacity(literals);
563 foreach (var p in literals.Zip(coefficients, (l, c) => new { Literal = l, Coeff = c }))
564 {
565 AddTerm(p.Literal, p.Coeff);
566 }
567 return this;
568 }
569
571 public LinearExprBuilder AddWeightedSum(IEnumerable<ILiteral> literals, IEnumerable<long> coefficients)
572 {
573 terms_.TryEnsureCapacity(literals);
574 foreach (var p in literals.Zip(coefficients, (l, c) => new { Literal = l, Coeff = c }))
575 {
576 AddTerm(p.Literal, p.Coeff);
577 }
578 return this;
579 }
580
582 public LinearExprBuilder AddWeightedSum(IEnumerable<BoolVar> vars, IEnumerable<long> coefficients)
583 {
584 terms_.TryEnsureCapacity(vars);
585 foreach (var p in vars.Zip(coefficients, (v, c) => new { Var = v, Coeff = c }))
586 {
587 AddTerm(p.Var, p.Coeff);
588 }
589 return this;
590 }
591
593 public LinearExprBuilder AddWeightedSum(IEnumerable<BoolVar> vars, IEnumerable<int> coefficients)
594 {
595 terms_.TryEnsureCapacity(vars);
596 foreach (var p in vars.Zip(coefficients, (v, c) => new { Var = v, Coeff = c }))
597 {
598 AddTerm(p.Var, p.Coeff);
599 }
600 return this;
601 }
602
603 public override string ToString()
604 {
605 string result = "";
606 foreach (Term term in terms_)
607 {
608 bool first = String.IsNullOrEmpty(result);
609 if (term.expr is null || term.coefficient == 0)
610 {
611 continue;
612 }
613 if (term.coefficient == 1)
614 {
615 if (!first)
616 {
617 result += " + ";
618 }
619
620 result += term.expr.ToString();
621 }
622 else if (term.coefficient > 0)
623 {
624 if (!first)
625 {
626 result += " + ";
627 }
628
629 result += String.Format("{0} * {1}", term.coefficient, term.expr.ToString());
630 }
631 else if (term.coefficient == -1)
632 {
633 if (!first)
634 {
635 result += String.Format(" - {0}", term.expr.ToString());
636 }
637 else
638 {
639 result += String.Format("-{0}", term.expr.ToString());
640 }
641 }
642 else
643 {
644 if (!first)
645 {
646 result += String.Format(" - {0} * {1}", -term.coefficient, term.expr.ToString());
647 }
648 else
649 {
650 result += String.Format("{0} * {1}", term.coefficient, term.expr.ToString());
651 }
652 }
653 }
654 if (offset_ > 0)
655 {
656 if (!String.IsNullOrEmpty(result))
657 {
658 result += String.Format(" + {0}", offset_);
659 }
660 else
661 {
662 result += String.Format("{0}", offset_);
663 }
664 }
665 else if (offset_ < 0)
666 {
667 if (!String.IsNullOrEmpty(result))
668 {
669 result += String.Format(" - {0}", -offset_);
670 }
671 else
672 {
673 result += String.Format("{0}", offset_);
674 }
675 }
676 return result;
677 }
678
679 public long Offset
680 {
681 get {
682 return offset_;
683 }
684 }
685
686 public List<Term> Terms
687 {
688 get {
689 return terms_;
690 }
691 }
692
693 private long offset_;
694 private List<Term> terms_;
695}
696
705public class IntVar : LinearExpr
706{
707 public IntVar(CpModelProto model, Domain domain, string name)
708 {
709 index_ = model.Variables.Count;
710 var_ = new IntegerVariableProto();
711 var_.Name = name;
712 var_.Domain.Add(domain.FlattenedIntervals());
713 model.Variables.Add(var_);
714 }
715
716 public IntVar(CpModelProto model, long lb, long ub, string name)
717 {
718 index_ = model.Variables.Count;
719 var_ = new IntegerVariableProto();
720 var_.Name = name;
721 var_.Domain.Capacity = 2;
722 var_.Domain.Add(lb);
723 var_.Domain.Add(ub);
724 model.Variables.Add(var_);
725 }
726
727 public IntVar(CpModelProto model, int index)
728 {
729 index_ = index;
730 var_ = model.Variables[index];
731 }
732
734 public int GetIndex()
735 {
736 return index_;
737 }
738
740 public int Index
741 {
742 get {
743 return GetIndex();
744 }
745 }
746
748 public IntegerVariableProto Proto
749 {
750 get {
751 return var_;
752 }
753 set {
754 var_ = value;
755 }
756 }
757
760 {
761 get {
762 return CpSatHelper.VariableDomain(var_);
763 }
764 }
765
766 public override string ToString()
767 {
768 return var_.Name ?? var_.ToString();
769 }
770
772 public string Name()
773 {
774 return var_.Name;
775 }
776
777 protected readonly int index_;
778 protected IntegerVariableProto var_;
779}
780
789public sealed class BoolVar : IntVar, ILiteral
790{
791
792 public BoolVar(CpModelProto model, String name) : base(model, 0, 1, name)
793 {
794 }
795
796 public BoolVar(CpModelProto model, int index) : base(model, index)
797 {
798 }
799
801 public ILiteral Not()
802 {
803 return negation_ ??= new NotBoolVar(this);
804 }
805
808 {
809 return (LinearExpr)Not();
810 }
811
812 private NotBoolVar negation_;
813}
814
815public sealed class NotBoolVar : LinearExpr, ILiteral
816{
817 public NotBoolVar(BoolVar boolvar)
818 {
819 boolvar_ = boolvar;
820 }
821
822 public int GetIndex()
823 {
824 return -boolvar_.GetIndex() - 1;
825 }
826
827 public int Index
828 {
829 get {
830 return GetIndex();
831 }
832 }
833
834 public ILiteral Not()
835 {
836 return boolvar_;
837 }
838
840 {
841 return (LinearExpr)Not();
842 }
843
844 public override string ToString()
845 {
846 return String.Format("Not({0})", boolvar_.ToString());
847 }
848
849 private readonly BoolVar boolvar_;
850}
851
860public sealed class BoundedLinearExpression
861{
862 public enum Type
863 {
864 BoundExpression,
865 VarEqVar,
866 VarDiffVar,
867 VarEqCst,
868 VarDiffCst,
869 }
870
871 public BoundedLinearExpression(long lb, LinearExpr expr, long ub)
872 {
873 left_ = expr;
874 right_ = null;
875 lb_ = lb;
876 ub_ = ub;
877 type_ = Type.BoundExpression;
878 }
879
880 public BoundedLinearExpression(LinearExpr left, LinearExpr right, bool equality)
881 {
882 left_ = left;
883 right_ = right;
884 lb_ = 0;
885 ub_ = 0;
886 type_ = equality ? Type.VarEqVar : Type.VarDiffVar;
887 }
888
889 public BoundedLinearExpression(LinearExpr left, long v, bool equality)
890 {
891 left_ = left;
892 right_ = null;
893 lb_ = v;
894 ub_ = 0;
895 type_ = equality ? Type.VarEqCst : Type.VarDiffCst;
896 }
897
898 bool IsTrue()
899 {
900 if (type_ == Type.VarEqVar)
901 {
902 return (object)left_ == (object)right_;
903 }
904 else if (type_ == Type.VarDiffVar)
905 {
906 return (object)left_ != (object)right_;
907 }
908 return false;
909 }
910
911 public static bool operator true(BoundedLinearExpression bie)
912 {
913 return bie.IsTrue();
914 }
915
916 public static bool operator false(BoundedLinearExpression bie)
917 {
918 return !bie.IsTrue();
919 }
920
921 public override string ToString()
922 {
923 switch (type_)
924 {
925 case Type.BoundExpression:
926 return String.Format("{0} <= {1} <= {2}", lb_, left_, ub_);
927 case Type.VarEqVar:
928 return String.Format("{0} == {1}", left_, right_);
929 case Type.VarDiffVar:
930 return String.Format("{0} != {1}", left_, right_);
931 case Type.VarEqCst:
932 return String.Format("{0} == {1}", left_, lb_);
933 case Type.VarDiffCst:
934 return String.Format("{0} != {1}", left_, lb_);
935 default:
936 throw new ArgumentException("Wrong mode in BoundedLinearExpression.");
937 }
938 }
939
941 {
942 if (a.CtType != Type.BoundExpression || a.Ub != Int64.MaxValue)
943 {
944 throw new ArgumentException("Operator <= not supported for this BoundedLinearExpression");
945 }
946 return new BoundedLinearExpression(a.Lb, a.Left, v);
947 }
948
950 {
951 if (a.CtType != Type.BoundExpression || a.Ub != Int64.MaxValue)
952 {
953 throw new ArgumentException("Operator < not supported for this BoundedLinearExpression");
954 }
955 return new BoundedLinearExpression(a.Lb, a.Left, v - 1);
956 }
957
959 {
960 if (a.CtType != Type.BoundExpression || a.Lb != Int64.MinValue)
961 {
962 throw new ArgumentException("Operator >= not supported for this BoundedLinearExpression");
963 }
964 return new BoundedLinearExpression(v, a.Left, a.Ub);
965 }
966
968 {
969 if (a.CtType != Type.BoundExpression || a.Lb != Int64.MinValue)
970 {
971 throw new ArgumentException("Operator < not supported for this BoundedLinearExpression");
972 }
973 return new BoundedLinearExpression(v + 1, a.Left, a.Ub);
974 }
975
977 {
978 get {
979 return left_;
980 }
981 }
982
984 {
985 get {
986 return right_;
987 }
988 }
989
990 public long Lb
991 {
992 get {
993 return lb_;
994 }
995 }
996
997 public long Ub
998 {
999 get {
1000 return ub_;
1001 }
1002 }
1003
1005 {
1006 get {
1007 return type_;
1008 }
1009 }
1010
1011 private LinearExpr left_;
1012 private LinearExpr right_;
1013 private long lb_;
1014 private long ub_;
1015 private Type type_;
1016}
1017
1018} // namespace Google.OrTools.Sat
Holds a Boolean variable.
BoolVar(CpModelProto model, String name)
BoolVar(CpModelProto model, int index)
ILiteral Not()
Returns the Boolean negation of that variable.
LinearExpr NotAsExpr()
Returns the Boolean negation of that variable as a linear expression.
Holds a linear constraint: expression ∈ domain
static BoundedLinearExpression operator<=(BoundedLinearExpression a, long v)
BoundedLinearExpression(long lb, LinearExpr expr, long ub)
BoundedLinearExpression(LinearExpr left, LinearExpr right, bool equality)
BoundedLinearExpression(LinearExpr left, long v, bool equality)
static BoundedLinearExpression operator<(BoundedLinearExpression a, long v)
static BoundedLinearExpression operator>(BoundedLinearExpression a, long v)
static BoundedLinearExpression operator>=(BoundedLinearExpression a, long v)
Holds a integer variable with a discrete domain.
int GetIndex()
Returns the index of the variable in the underlying CpModelProto.
int Index
Returns the index of the variable in the underlying CpModelProto.
string Name()
Returns the name of the variable given upon creation.
IntVar(CpModelProto model, int index)
IntVar(CpModelProto model, Domain domain, string name)
Domain Domain
Returns the domain of the variable.
IntVar(CpModelProto model, long lb, long ub, string name)
IntegerVariableProto Proto
The underlying IntegerVariableProto.
IntegerVariableProto var_
A builder class for linear expressions.
LinearExprBuilder Add(LinearExpr expr)
Adds expr to the builder.
LinearExprBuilder AddSum(IEnumerable< BoolVar > vars)
Adds sum(vars) to the builder.
LinearExprBuilder AddSum(IEnumerable< ILiteral > literals)
Adds sum(literals) to the builder.
LinearExprBuilder AddTerm(ILiteral literal, long coefficient)
Adds literal * coefficient to the builder.
LinearExprBuilder AddSum(IEnumerable< LinearExpr > exprs)
Adds sum(exprs) to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< int > coefficients)
Adds sum(exprs[i] * coeffs[i]) to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< long > coefficients)
Adds sum(exprs[i] * coeffs[i]) to the builder.
LinearExprBuilder Add(ILiteral literal)
Adds literal to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< BoolVar > vars, IEnumerable< int > coefficients)
Adds sum(vars[i] * coeffs[i]) to the builder.
LinearExprBuilder AddTerm(LinearExpr expr, long coefficient)
Adds expr * coefficient to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< ILiteral > literals, IEnumerable< long > coefficients)
Adds sum(literals[i] * coeffs[i]) to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< BoolVar > vars, IEnumerable< long > coefficients)
Adds sum(vars[i] * coeffs[i]) to the builder.
LinearExprBuilder AddTerm(BoolVar var, long coefficient)
Adds var * coefficient to the builder.
LinearExprBuilder Add(long constant)
Adds constant to the builder.
LinearExprBuilder Add(BoolVar var)
Adds var to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< ILiteral > literals, IEnumerable< int > coefficients)
Adds sum(literals[i] * coeffs[i]) to the builder.
Holds a linear expression: sum (ai * xi) + b.
static LinearExpr Affine(LinearExpr expr, long coeff, long offset)
Creates expr * coeff + offset.
static LinearExpr operator*(LinearExpr a, long v)
static LinearExpr Constant(long value)
Creates a constant expression.
static LinearExpr Sum(IEnumerable< LinearExpr > exprs)
Creates Sum(exprs).
static BoundedLinearExpression operator<(LinearExpr a, long v)
static LinearExpr Affine(BoolVar var, long coeff, long offset)
Creates var * coeff + offset.
static BoundedLinearExpression operator==(LinearExpr a, LinearExpr b)
static LinearExpr Term(BoolVar var, long coeff)
Creates var * coeff.
static BoundedLinearExpression operator>(LinearExpr a, long v)
static BoundedLinearExpression operator>=(LinearExpr a, long v)
static BoundedLinearExpression operator<=(LinearExpr a, long v)
static LinearExpr WeightedSum(IEnumerable< BoolVar > vars, IEnumerable< long > coeffs)
Creates Sum(vars[i] * coeffs[i]).
static LinearExpr WeightedSum(IEnumerable< ILiteral > literals, IEnumerable< int > coeffs)
Creates Sum(literals[i] * coeffs[i]).
static LinearExpr Term(ILiteral literal, long coeff)
Creates literal * coeff.
static LinearExpr WeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< long > coeffs)
Creates Sum(exprs[i] * coeffs[i]).
static LinearExpr Sum(IEnumerable< BoolVar > vars)
Creates Sum(vars).
static LinearExpr WeightedSum(IEnumerable< BoolVar > vars, IEnumerable< int > coeffs)
Creates Sum(vars[i] * coeffs[i]).
static LinearExpr operator+(LinearExpr a, LinearExpr b)
static LinearExpr WeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< int > coeffs)
Creates Sum(exprs[i] * coeffs[i]).
static BoundedLinearExpression operator<(LinearExpr a, LinearExpr b)
static BoundedLinearExpression operator>(LinearExpr a, LinearExpr b)
static BoundedLinearExpression operator<(long v, LinearExpr a)
static LinearExpr Affine(ILiteral literal, long coeff, long offset)
Creates literal * coeff + offset.
static BoundedLinearExpression operator>(long v, LinearExpr a)
static LinearExpr Term(LinearExpr expr, long coeff)
Creates expr * coeff.
static BoundedLinearExpression operator!=(LinearExpr a, LinearExpr b)
static LinearExpr Sum(IEnumerable< ILiteral > literals)
Creates Sum(literals).
static LinearExpr operator-(LinearExpr a, LinearExpr b)
static LinearExpr WeightedSum(IEnumerable< ILiteral > literals, IEnumerable< long > coeffs)
Creates Sum(literals[i] * coeffs[i]).
static LinearExprBuilder NewBuilder(int sizeHint=2)
Creates a builder class for linear expression.
int GetIndex()
Returns the logical index of the literal.
ILiteral Not()
Returns the Boolean negation of the literal.
LinearExpr NotAsExpr()
Returns the Boolean negation of the literal as a linear expression.
Holds a Boolean variable or its negation.
int GetIndex()
Returns the logical index of the literal.
ILiteral Not()
Returns the Boolean negation of the literal.
LinearExpr NotAsExpr()
Returns the Boolean negation of the literal as a linear expression.
Term(LinearExpr e, long c)