Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
IntegerExpressions.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
14namespace Google.OrTools.Sat
15{
17using System;
18using System.Collections;
19using System.Collections.Generic;
20using System.Linq;
21using System.Runtime.CompilerServices;
22using Google.Protobuf.Collections;
23
36
37internal static class HelperExtensions
38{
39 [MethodImpl(MethodImplOptions.AggressiveInlining)]
40 public static void AddOrIncrement(this Dictionary<int, long> dict, int key, long increment)
41 {
42#if NET6_0_OR_GREATER
43 System.Runtime.InteropServices.CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out _) += increment;
44#else
45 if (dict.TryGetValue(key, out var value))
46 {
47 dict[key] = value + increment;
48 }
49 else
50 {
51 dict.Add(key, increment);
52 }
53#endif
54 }
55
56 [MethodImpl(MethodImplOptions.AggressiveInlining)]
57 internal static void TrySetCapacity<TField, TValues>(this RepeatedField<TField> field, IEnumerable<TValues> values)
58 {
59 if (values is ICollection<TValues> collection)
60 {
61 field.Capacity = collection.Count;
62 }
63 }
64
65 [MethodImpl(MethodImplOptions.AggressiveInlining)]
66 internal static void TryEnsureCapacity<TValue, TValues>(this List<TValue> list, IEnumerable<TValues> values)
67 {
68 // Check for ICollection as the generic version is not covariant and TValues could be LinearExpr, IntVar, ...
69 if (values is ICollection collection)
70 {
71 list.Capacity = Math.Max(list.Count + collection.Count, list.Capacity);
72 }
73 }
74
75#if NETFRAMEWORK
76 [MethodImpl(MethodImplOptions.AggressiveInlining)]
77 internal static bool TryDequeue<T>(this Queue<T> queue, out T value)
78 {
79 if (queue.Count > 0)
80 {
81 value = queue.Dequeue();
82 return true;
83 }
84
85 value = default;
86 return false;
87 }
88#endif
89}
90
91// Holds a term (expression * coefficient)
92public struct Term
93{
95 public long coefficient;
96
97 public Term(LinearExpr e, long c)
98 {
99 this.expr = e;
100 this.coefficient = c;
101 }
102}
103
109public class LinearExpr
110{
112 public static LinearExpr Sum(IEnumerable<LinearExpr> exprs)
113 {
114 return NewBuilder(0).AddSum(exprs);
115 }
116
118 public static LinearExpr Sum(IEnumerable<ILiteral> literals)
119 {
120 return NewBuilder(0).AddSum(literals);
121 }
122
124 public static LinearExpr Sum(IEnumerable<BoolVar> vars)
125 {
126 return NewBuilder(0).AddSum(vars);
127 }
128
130 public static LinearExpr WeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<int> coeffs)
131 {
132 return NewBuilder(0).AddWeightedSum(exprs, coeffs);
133 }
134
136 public static LinearExpr WeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<long> coeffs)
137 {
138 return NewBuilder(0).AddWeightedSum(exprs, coeffs);
139 }
140
142 public static LinearExpr WeightedSum(IEnumerable<ILiteral> literals, IEnumerable<int> coeffs)
143 {
144 return NewBuilder(0).AddWeightedSum(literals, coeffs);
145 }
146
148 public static LinearExpr WeightedSum(IEnumerable<ILiteral> literals, IEnumerable<long> coeffs)
149 {
150 return NewBuilder(0).AddWeightedSum(literals, coeffs);
151 }
152
154 public static LinearExpr WeightedSum(IEnumerable<BoolVar> vars, IEnumerable<int> coeffs)
155 {
156 return NewBuilder(0).AddWeightedSum(vars, coeffs);
157 }
158
160 public static LinearExpr WeightedSum(IEnumerable<BoolVar> vars, IEnumerable<long> coeffs)
161 {
162 return NewBuilder(0).AddWeightedSum(vars, coeffs);
163 }
164
166 public static LinearExpr Term(LinearExpr expr, long coeff)
167 {
168 return Prod(expr, coeff);
169 }
170
172 public static LinearExpr Term(ILiteral literal, long coeff)
173 {
174 if (literal is BoolVar boolVar)
175 {
176 return Prod(boolVar, coeff);
177 }
178 else
179 {
180 return Affine(literal.NotAsExpr(), -coeff, coeff);
181 }
182 }
183
185 public static LinearExpr Term(BoolVar var, long coeff)
186 {
187 return Prod(var, coeff);
188 }
189
191 public static LinearExpr Affine(LinearExpr expr, long coeff, long offset)
192 {
193 if (offset == 0)
194 {
195 return Prod(expr, coeff);
196 }
197 return NewBuilder().AddTerm(expr, coeff).Add(offset);
198 }
199
201 public static LinearExpr Affine(ILiteral literal, long coeff, long offset)
202 {
203 return NewBuilder().AddTerm(literal, coeff).Add(offset);
204 }
205
207 public static LinearExpr Affine(BoolVar var, long coeff, long offset)
208 {
209 return NewBuilder().AddTerm(var, coeff).Add(offset);
210 }
211
213 public static LinearExpr Constant(long value)
214 {
215 return NewBuilder(0).Add(value);
216 }
217
219 public static LinearExprBuilder NewBuilder(int sizeHint = 2)
220 {
221 return new LinearExprBuilder(sizeHint);
222 }
223
225 {
226 return NewBuilder().Add(a).Add(b);
227 }
228
229 public static LinearExpr operator +(LinearExpr a, long v)
230 {
231 return NewBuilder().Add(a).Add(v);
232 }
233
234 public static LinearExpr operator +(long v, LinearExpr a)
235 {
236 return NewBuilder().Add(a).Add(v);
237 }
238
240 {
241 return NewBuilder().Add(a).AddTerm(b, -1);
242 }
243
244 public static LinearExpr operator -(LinearExpr a, long v)
245 {
246 return NewBuilder().Add(a).Add(-v);
247 }
248
249 public static LinearExpr operator -(long v, LinearExpr a)
250 {
251 return NewBuilder().AddTerm(a, -1).Add(v);
252 }
253
254 public static LinearExpr operator *(LinearExpr a, long v)
255 {
256 return Prod(a, v);
257 }
258
259 public static LinearExpr operator *(long v, LinearExpr a)
260 {
261 return Prod(a, v);
262 }
263
265 {
266 return Prod(a, -1);
267 }
268
270 {
271 return new BoundedLinearExpression(a, b, true);
272 }
273
275 {
276 return new BoundedLinearExpression(a, b, false);
277 }
278
280 {
281 return new BoundedLinearExpression(a, v, true);
282 }
283
285 {
286 return new BoundedLinearExpression(a, v, false);
287 }
288
290 {
291 return new BoundedLinearExpression(v, a, Int64.MaxValue);
292 }
293
295 {
296 return a <= v;
297 }
298
300 {
301 return new BoundedLinearExpression(v + 1, a, Int64.MaxValue);
302 }
303
305 {
306 return a < v;
307 }
308
310 {
311 return new BoundedLinearExpression(Int64.MinValue, a, v);
312 }
313
315 {
316 return a >= v;
317 }
318
320 {
321 return new BoundedLinearExpression(Int64.MinValue, a, v - 1);
322 }
323
325 {
326 return a > v;
327 }
328
330 {
331 return new BoundedLinearExpression(0, a - b, Int64.MaxValue);
332 }
333
335 {
336 return new BoundedLinearExpression(1, a - b, Int64.MaxValue);
337 }
338
340 {
341 return new BoundedLinearExpression(Int64.MinValue, a - b, 0);
342 }
343
345 {
346 return new BoundedLinearExpression(Int64.MinValue, a - b, -1);
347 }
348
349 internal static LinearExpr Prod(LinearExpr e, long v)
350 {
351 if (v == 0)
352 {
353 return NewBuilder(0);
354 }
355 else if (v == 1)
356 {
357 return e;
358 }
359 else
360 {
361 return NewBuilder(1).AddTerm(e, v);
362 }
363 }
364
365 internal static long GetVarValueMap(LinearExpr e, Dictionary<int, long> dict, Queue<Term> terms)
366 {
367 long constant = 0;
368 long coefficient = 1;
369 LinearExpr expr = e;
370 terms.Clear();
371
372 do
373 {
374 switch (expr)
375 {
376 case LinearExprBuilder builder:
377 constant += coefficient * builder.Offset;
378 if (coefficient == 1)
379 {
380 foreach (Term sub in builder.Terms)
381 {
382 terms.Enqueue(sub);
383 }
384 }
385 else
386 {
387 foreach (Term sub in builder.Terms)
388 {
389 terms.Enqueue(new Term(sub.expr, sub.coefficient * coefficient));
390 }
391 }
392 break;
393 case IntVar intVar:
394 dict.AddOrIncrement(intVar.GetIndex(), coefficient);
395 break;
396 case NotBoolVar notBoolVar:
397 dict.AddOrIncrement(notBoolVar.Not().GetIndex(), -coefficient);
398 constant += coefficient;
399 break;
400 default:
401 throw new ArgumentException("Cannot evaluate '" + expr + "' in an integer expression");
402 }
403
404 if (!terms.TryDequeue(out var term))
405 {
406 break;
407 }
408 expr = term.expr;
409 coefficient = term.coefficient;
410 } while (true);
411
412 return constant;
413 }
414
415 internal static LinearExpr RebuildLinearExprFromLinearExpressionProto(LinearExpressionProto proto,
416 CpModelProto model)
417 {
418 int numElements = proto.Vars.Count;
419 long offset = proto.Offset;
420 if (numElements == 0)
421 {
422 return LinearExpr.Constant(offset);
423 }
424 else if (numElements == 1)
425 {
426 IntVar var = new IntVar(model, proto.Vars[0]);
427 long coeff = proto.Coeffs[0];
428 return LinearExpr.Affine(var, coeff, offset);
429 }
430 else
431 {
432 LinearExprBuilder builder = LinearExpr.NewBuilder(numElements);
433 for (int i = 0; i < numElements; ++i)
434 {
435 builder.AddTerm(new IntVar(model, proto.Vars[i]), proto.Coeffs[i]);
436 }
437 builder.Add(offset);
438 return builder;
439 }
440 }
441}
442
444public sealed class LinearExprBuilder : LinearExpr
445{
446 public LinearExprBuilder(int sizeHint = 2)
447 {
448 terms_ = new List<Term>(sizeHint);
449 offset_ = 0;
450 }
451
454 {
455 return AddTerm(expr, 1);
456 }
457
460 {
461 return AddTerm(literal, 1);
462 }
463
466 {
467 return AddTerm(var, 1);
468 }
469
471 public LinearExprBuilder Add(long constant)
472 {
473 offset_ += constant;
474 return this;
475 }
476
478 public LinearExprBuilder AddTerm(LinearExpr expr, long coefficient)
479 {
480 terms_.Add(new Term(expr, coefficient));
481 return this;
482 }
483
485 public LinearExprBuilder AddTerm(ILiteral literal, long coefficient)
486 {
487 if (literal is BoolVar boolVar)
488 {
489 terms_.Add(new Term(boolVar, coefficient));
490 }
491 else
492 {
493 offset_ += coefficient;
494 terms_.Add(new Term(literal.NotAsExpr(), -coefficient));
495 }
496 return this;
497 }
498
500 public LinearExprBuilder AddTerm(BoolVar var, long coefficient)
501 {
502 terms_.Add(new Term(var, coefficient));
503 return this;
504 }
505
507 public LinearExprBuilder AddSum(IEnumerable<LinearExpr> exprs)
508 {
509 terms_.TryEnsureCapacity(exprs);
510 foreach (LinearExpr expr in exprs)
511 {
512 AddTerm(expr, 1);
513 }
514 return this;
515 }
516
518 public LinearExprBuilder AddSum(IEnumerable<ILiteral> literals)
519 {
520 terms_.TryEnsureCapacity(literals);
521 foreach (ILiteral literal in literals)
522 {
523 AddTerm(literal, 1);
524 }
525 return this;
526 }
527
529 public LinearExprBuilder AddSum(IEnumerable<BoolVar> vars)
530 {
531 terms_.TryEnsureCapacity(vars);
532 foreach (BoolVar var in vars)
533 {
534 AddTerm(var, 1);
535 }
536 return this;
537 }
538
540 public LinearExprBuilder AddWeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<long> coefficients)
541 {
542 terms_.TryEnsureCapacity(exprs);
543 foreach (var p in exprs.Zip(coefficients, (e, c) => new { Expr = e, Coeff = c }))
544 {
545 AddTerm(p.Expr, p.Coeff);
546 }
547 return this;
548 }
549
551 public LinearExprBuilder AddWeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<int> coefficients)
552 {
553 terms_.TryEnsureCapacity(exprs);
554 foreach (var p in exprs.Zip(coefficients, (e, c) => new { Expr = e, Coeff = c }))
555 {
556 AddTerm(p.Expr, p.Coeff);
557 }
558 return this;
559 }
560
562 public LinearExprBuilder AddWeightedSum(IEnumerable<ILiteral> literals, IEnumerable<int> coefficients)
563 {
564 terms_.TryEnsureCapacity(literals);
565 foreach (var p in literals.Zip(coefficients, (l, c) => new { Literal = l, Coeff = c }))
566 {
567 AddTerm(p.Literal, p.Coeff);
568 }
569 return this;
570 }
571
573 public LinearExprBuilder AddWeightedSum(IEnumerable<ILiteral> literals, IEnumerable<long> coefficients)
574 {
575 terms_.TryEnsureCapacity(literals);
576 foreach (var p in literals.Zip(coefficients, (l, c) => new { Literal = l, Coeff = c }))
577 {
578 AddTerm(p.Literal, p.Coeff);
579 }
580 return this;
581 }
582
584 public LinearExprBuilder AddWeightedSum(IEnumerable<BoolVar> vars, IEnumerable<long> coefficients)
585 {
586 terms_.TryEnsureCapacity(vars);
587 foreach (var p in vars.Zip(coefficients, (v, c) => new { Var = v, Coeff = c }))
588 {
589 AddTerm(p.Var, p.Coeff);
590 }
591 return this;
592 }
593
595 public LinearExprBuilder AddWeightedSum(IEnumerable<BoolVar> vars, IEnumerable<int> coefficients)
596 {
597 terms_.TryEnsureCapacity(vars);
598 foreach (var p in vars.Zip(coefficients, (v, c) => new { Var = v, Coeff = c }))
599 {
600 AddTerm(p.Var, p.Coeff);
601 }
602 return this;
603 }
604
605 public override string ToString()
606 {
607 string result = "";
608 bool need_parenthesis = false;
609 foreach (Term term in terms_)
610 {
611 bool first = String.IsNullOrEmpty(result);
612 if (term.expr is null || term.coefficient == 0)
613 {
614 continue;
615 }
616 if (term.coefficient == 1)
617 {
618 if (!first)
619 {
620 result += " + ";
621 need_parenthesis = true;
622 }
623
624 result += term.expr.ToString();
625 }
626 else if (term.coefficient > 0)
627 {
628 if (!first)
629 {
630 need_parenthesis = true;
631 result += " + ";
632 }
633
634 result += String.Format("{0} * {1}", term.coefficient, term.expr.ToString());
635 }
636 else if (term.coefficient == -1)
637 {
638 if (!first)
639 {
640 need_parenthesis = true;
641 result += String.Format(" - {0}", term.expr.ToString());
642 }
643 else
644 {
645 result += String.Format("-{0}", term.expr.ToString());
646 }
647 }
648 else
649 {
650 if (!first)
651 {
652 need_parenthesis = true;
653 result += String.Format(" - {0} * {1}", -term.coefficient, term.expr.ToString());
654 }
655 else
656 {
657 result += String.Format("{0} * {1}", term.coefficient, term.expr.ToString());
658 }
659 }
660 }
661 if (offset_ > 0)
662 {
663 if (!String.IsNullOrEmpty(result))
664 {
665 need_parenthesis = true;
666 result += String.Format(" + {0}", offset_);
667 }
668 else
669 {
670 result += String.Format("{0}", offset_);
671 }
672 }
673 else if (offset_ < 0)
674 {
675 if (!String.IsNullOrEmpty(result))
676 {
677 need_parenthesis = true;
678 result += String.Format(" - {0}", -offset_);
679 }
680 else
681 {
682 result += String.Format("{0}", offset_);
683 }
684 }
685
686 if (need_parenthesis)
687 {
688 return string.Format("({0})", result);
689 }
690 return result;
691 }
692
693 public long Offset
694 {
695 get {
696 return offset_;
697 }
698 }
699
700 public List<Term> Terms
701 {
702 get {
703 return terms_;
704 }
705 }
706
707 private long offset_;
708 private List<Term> terms_;
709}
710
719public class IntVar : LinearExpr
720{
721 public IntVar(CpModelProto model, Domain domain, string name)
722 {
723 index_ = model.Variables.Count;
725 var_.Name = name;
726 var_.Domain.Add(domain.FlattenedIntervals());
727 model.Variables.Add(var_);
728 }
729
730 public IntVar(CpModelProto model, long lb, long ub, string name)
731 {
732 index_ = model.Variables.Count;
734 var_.Name = name;
735 var_.Domain.Capacity = 2;
736 var_.Domain.Add(lb);
737 var_.Domain.Add(ub);
738 model.Variables.Add(var_);
739 }
740
741 public IntVar(CpModelProto model, int index)
742 {
743 index_ = index;
744 var_ = model.Variables[index];
745 }
746
748 public int GetIndex()
749 {
750 return index_;
751 }
752
754 public int Index
755 {
756 get {
757 return GetIndex();
758 }
759 }
760
763 {
764 get {
765 return var_;
766 }
767 set {
768 var_ = value;
769 }
770 }
771
774 {
775 get {
777 }
778 }
779
780 public override string ToString()
781 {
782 return var_.Name ?? var_.ToString();
783 }
784
786 public string Name()
787 {
788 return var_.Name;
789 }
790
791 protected readonly int index_;
793}
794
803public sealed class BoolVar : IntVar, ILiteral
804{
805
806 public BoolVar(CpModelProto model, String name) : base(model, 0, 1, name)
807 {
808 }
809
810 public BoolVar(CpModelProto model, int index) : base(model, index)
811 {
812 }
813
815 public ILiteral Not()
816 {
817 return negation_ ??= new NotBoolVar(this);
818 }
819
822 {
823 return this;
824 }
825
828 {
829 return (LinearExpr)Not();
830 }
831
832 private NotBoolVar negation_;
833}
834
835public sealed class NotBoolVar : LinearExpr, ILiteral
836{
837 public NotBoolVar(BoolVar boolvar)
838 {
839 boolvar_ = boolvar;
840 }
841
842 public int GetIndex()
843 {
844 return -boolvar_.GetIndex() - 1;
845 }
846
847 public int Index
848 {
849 get {
850 return GetIndex();
851 }
852 }
853
854 public ILiteral Not()
855 {
856 return boolvar_;
857 }
858
860 {
861 return LinearExpr.Affine(boolvar_, -1, 1); // 1 - boolvar_.
862 }
863
865 {
866 return boolvar_;
867 }
868
869 public override string ToString()
870 {
871 return String.Format("Not({0})", boolvar_.ToString());
872 }
873
874 private readonly BoolVar boolvar_;
875}
876
885public sealed class BoundedLinearExpression
886{
887 public enum Type
888 {
890 VarEqVar,
892 VarEqCst,
894 }
895
896 public BoundedLinearExpression(long lb, LinearExpr expr, long ub)
897 {
898 left_ = expr;
899 right_ = null;
900 lb_ = lb;
901 ub_ = ub;
902 type_ = Type.BoundExpression;
903 }
904
905 public BoundedLinearExpression(LinearExpr left, LinearExpr right, bool equality)
906 {
907 left_ = left;
908 right_ = right;
909 lb_ = 0;
910 ub_ = 0;
911 type_ = equality ? Type.VarEqVar : Type.VarDiffVar;
912 }
913
914 public BoundedLinearExpression(LinearExpr left, long v, bool equality)
915 {
916 left_ = left;
917 right_ = null;
918 lb_ = v;
919 ub_ = 0;
920 type_ = equality ? Type.VarEqCst : Type.VarDiffCst;
921 }
922
923 bool IsTrue()
924 {
925 if (type_ == Type.VarEqVar)
926 {
927 return (object)left_ == (object)right_;
928 }
929 else if (type_ == Type.VarDiffVar)
930 {
931 return (object)left_ != (object)right_;
932 }
933 return false;
934 }
935
936 public static bool operator true(BoundedLinearExpression bie)
937 {
938 return bie.IsTrue();
939 }
940
941 public static bool operator false(BoundedLinearExpression bie)
942 {
943 return !bie.IsTrue();
944 }
945
946 public override string ToString()
947 {
948 switch (type_)
949 {
950 case Type.BoundExpression:
951 return String.Format("{0} <= {1} <= {2}", lb_, left_, ub_);
952 case Type.VarEqVar:
953 return String.Format("{0} == {1}", left_, right_);
954 case Type.VarDiffVar:
955 return String.Format("{0} != {1}", left_, right_);
956 case Type.VarEqCst:
957 return String.Format("{0} == {1}", left_, lb_);
958 case Type.VarDiffCst:
959 return String.Format("{0} != {1}", left_, lb_);
960 default:
961 throw new ArgumentException("Wrong mode in BoundedLinearExpression.");
962 }
963 }
964
966 {
967 if (a.CtType != Type.BoundExpression || a.Ub != Int64.MaxValue)
968 {
969 throw new ArgumentException("Operator <= not supported for this BoundedLinearExpression");
970 }
971 return new BoundedLinearExpression(a.Lb, a.Left, v);
972 }
973
975 {
976 if (a.CtType != Type.BoundExpression || a.Ub != Int64.MaxValue)
977 {
978 throw new ArgumentException("Operator < not supported for this BoundedLinearExpression");
979 }
980 return new BoundedLinearExpression(a.Lb, a.Left, v - 1);
981 }
982
984 {
985 if (a.CtType != Type.BoundExpression || a.Lb != Int64.MinValue)
986 {
987 throw new ArgumentException("Operator >= not supported for this BoundedLinearExpression");
988 }
989 return new BoundedLinearExpression(v, a.Left, a.Ub);
990 }
991
993 {
994 if (a.CtType != Type.BoundExpression || a.Lb != Int64.MinValue)
995 {
996 throw new ArgumentException("Operator < not supported for this BoundedLinearExpression");
997 }
998 return new BoundedLinearExpression(v + 1, a.Left, a.Ub);
999 }
1000
1002 {
1003 get {
1004 return left_;
1005 }
1006 }
1007
1009 {
1010 get {
1011 return right_;
1012 }
1013 }
1014
1015 public long Lb
1016 {
1017 get {
1018 return lb_;
1019 }
1020 }
1021
1022 public long Ub
1023 {
1024 get {
1025 return ub_;
1026 }
1027 }
1028
1030 {
1031 get {
1032 return type_;
1033 }
1034 }
1035
1036 private LinearExpr left_;
1037 private LinearExpr right_;
1038 private long lb_;
1039 private long ub_;
1040 private Type type_;
1041}
1042
1043} // namespace Google.OrTools.Sat
Holds a Boolean variable.
BoolVar(CpModelProto model, String name)
ILiteral Not()
Returns the Boolean negation of that variable.
LinearExpr NotAsExpr()
Returns the Boolean negation of that variable as a linear expression.
LinearExpr AsExpr()
Returns the literal as a linear expression.
BoolVar(CpModelProto model, int index)
Holds a linear constraint: expression ∈ domain
static BoundedLinearExpression operator<(BoundedLinearExpression a, long v)
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)
A constraint programming problem.
pbc::RepeatedField< global::Google.OrTools.Sat.IntegerVariableProto > Variables
The associated Protos should be referred by their index in these fields.
static Domain VariableDomain(Google.OrTools.Sat.IntegerVariableProto variable_proto)
Holds a integer variable with a discrete domain.
IntVar(CpModelProto model, Domain domain, string name)
IntVar(CpModelProto model, long lb, long ub, string name)
IntVar(CpModelProto model, int index)
pbc::RepeatedField< long > Domain
The variable domain given as a sorted list of n disjoint intervals [min, max] and encoded as [min_0,...
string Name
For debug/logging only. Can be empty.
A builder class for linear expressions.
LinearExprBuilder AddWeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< long > coefficients)
Adds sum(exprs[i] * coeffs[i]) to the builder.
LinearExprBuilder Add(long constant)
Adds constant to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< BoolVar > vars, IEnumerable< long > coefficients)
Adds sum(vars[i] * coeffs[i]) to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< ILiteral > literals, IEnumerable< int > coefficients)
Adds sum(literals[i] * coeffs[i]) to the builder.
LinearExprBuilder AddSum(IEnumerable< LinearExpr > exprs)
Adds sum(exprs) to the builder.
LinearExprBuilder AddTerm(BoolVar var, long coefficient)
Adds var * coefficient to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< BoolVar > vars, IEnumerable< int > coefficients)
Adds sum(vars[i] * coeffs[i]) to the builder.
LinearExprBuilder AddSum(IEnumerable< BoolVar > vars)
Adds sum(vars) to the builder.
LinearExprBuilder Add(LinearExpr expr)
Adds expr to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< int > coefficients)
Adds sum(exprs[i] * coeffs[i]) 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 Add(ILiteral literal)
Adds literal to the builder.
LinearExprBuilder Add(BoolVar var)
Adds var to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< ILiteral > literals, IEnumerable< long > coefficients)
Adds sum(literals[i] * coeffs[i]) to the builder.
LinearExprBuilder AddTerm(LinearExpr expr, long coefficient)
Adds expr * coefficient to the builder.
Holds a linear expression: sum (ai * xi) + b.
static LinearExpr Term(LinearExpr expr, long coeff)
Creates expr * coeff.
static LinearExpr Term(BoolVar var, long coeff)
Creates var * coeff.
static LinearExpr WeightedSum(IEnumerable< BoolVar > vars, IEnumerable< int > coeffs)
Creates Sum(vars[i] * coeffs[i]).
static BoundedLinearExpression operator<=(LinearExpr a, long v)
static LinearExpr Term(ILiteral literal, long coeff)
Creates literal * coeff.
static BoundedLinearExpression operator<(long v, LinearExpr a)
static LinearExpr Constant(long value)
Creates a constant expression.
static LinearExpr operator+(LinearExpr a, LinearExpr b)
static BoundedLinearExpression 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, long v)
static LinearExprBuilder NewBuilder(int sizeHint=2)
Creates a builder class for linear expression.
static BoundedLinearExpression operator<(LinearExpr a, long v)
static LinearExpr operator-(LinearExpr a, LinearExpr b)
static BoundedLinearExpression operator>(long v, LinearExpr a)
static LinearExpr Sum(IEnumerable< BoolVar > vars)
Creates Sum(vars).
static LinearExpr Sum(IEnumerable< ILiteral > literals)
Creates Sum(literals).
static LinearExpr WeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< long > coeffs)
Creates Sum(exprs[i] * coeffs[i]).
static LinearExpr Affine(ILiteral literal, long coeff, long offset)
Creates literal * coeff + offset.
static LinearExpr WeightedSum(IEnumerable< ILiteral > literals, IEnumerable< int > coeffs)
Creates Sum(literals[i] * coeffs[i]).
static LinearExpr Affine(BoolVar var, long coeff, long offset)
Creates var * coeff + offset.
static LinearExpr WeightedSum(IEnumerable< BoolVar > vars, IEnumerable< long > coeffs)
Creates Sum(vars[i] * coeffs[i]).
static LinearExpr Affine(LinearExpr expr, long coeff, long offset)
Creates expr * coeff + offset.
static BoundedLinearExpression operator<(LinearExpr a, LinearExpr b)
static LinearExpr operator*(LinearExpr a, long v)
static LinearExpr WeightedSum(IEnumerable< ILiteral > literals, IEnumerable< long > coeffs)
Creates Sum(literals[i] * coeffs[i]).
static BoundedLinearExpression operator>=(LinearExpr a, long v)
static LinearExpr Sum(IEnumerable< LinearExpr > exprs)
Creates Sum(exprs).
static BoundedLinearExpression operator==(LinearExpr a, LinearExpr b)
static BoundedLinearExpression operator!=(LinearExpr a, LinearExpr b)
LinearExpr AsExpr()
Returns the literal as a linear expression.
LinearExpr NotAsExpr()
Returns the Boolean negation of the literal as a linear expression.
int GetIndex()
Returns the logical index of the literal.
ILiteral Not()
Returns the Boolean negation of the literal.
long[] FlattenedIntervals()
Definition Domain.cs:99
Holds a Boolean variable or its negation.
int GetIndex()
Returns the logical index of the literal.
LinearExpr NotAsExpr()
Returns the Boolean negation of the literal as a linear expression.
LinearExpr AsExpr()
Returns the literal as a linear expression.
ILiteral Not()
Returns the Boolean negation of the literal.
Holds a term (expression * coefficient)
Term(LinearExpr e, long c)