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 
14 namespace Google.OrTools.Sat
15 {
16 using Google.OrTools.Util;
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Runtime.CompilerServices;
22 using Google.Protobuf.Collections;
23 
25 public interface ILiteral
26 {
28  ILiteral Not();
30  int GetIndex();
33 }
34 
35 internal static class HelperExtensions
36 {
37  [MethodImpl(MethodImplOptions.AggressiveInlining)]
38  public static void AddOrIncrement(this Dictionary<IntVar, long> dict, IntVar 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  public 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  public 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 
74 // Holds a term (expression * coefficient)
75 public struct Term
76 {
77  public LinearExpr expr;
78  public long coefficient;
79 
80  public Term(LinearExpr e, long c)
81  {
82  this.expr = e;
83  this.coefficient = c;
84  }
85 }
86 
92 public class LinearExpr
93 {
95  public static LinearExpr Sum(IEnumerable<LinearExpr> exprs)
96  {
97  return NewBuilder(0).AddSum(exprs);
98  }
99 
101  public static LinearExpr Sum(IEnumerable<ILiteral> literals)
102  {
103  return NewBuilder(0).AddSum(literals);
104  }
105 
107  public static LinearExpr Sum(IEnumerable<BoolVar> vars)
108  {
109  return NewBuilder(0).AddSum(vars);
110  }
111 
113  public static LinearExpr WeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<int> coeffs)
114  {
115  return NewBuilder(0).AddWeightedSum(exprs, coeffs);
116  }
117 
119  public static LinearExpr WeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<long> coeffs)
120  {
121  return NewBuilder(0).AddWeightedSum(exprs, coeffs);
122  }
123 
125  public static LinearExpr WeightedSum(IEnumerable<ILiteral> literals, IEnumerable<int> coeffs)
126  {
127  return NewBuilder(0).AddWeightedSum(literals, coeffs);
128  }
129 
131  public static LinearExpr WeightedSum(IEnumerable<ILiteral> literals, IEnumerable<long> coeffs)
132  {
133  return NewBuilder(0).AddWeightedSum(literals, coeffs);
134  }
135 
137  public static LinearExpr WeightedSum(IEnumerable<BoolVar> vars, IEnumerable<int> coeffs)
138  {
139  return NewBuilder(0).AddWeightedSum(vars, coeffs);
140  }
141 
143  public static LinearExpr WeightedSum(IEnumerable<BoolVar> vars, IEnumerable<long> coeffs)
144  {
145  return NewBuilder(0).AddWeightedSum(vars, coeffs);
146  }
147 
149  public static LinearExpr Term(LinearExpr expr, long coeff)
150  {
151  return Prod(expr, coeff);
152  }
153 
155  public static LinearExpr Term(ILiteral literal, long coeff)
156  {
157  if (literal is BoolVar boolVar)
158  {
159  return Prod(boolVar, coeff);
160  }
161  else
162  {
163  return Affine(literal.NotAsExpr(), -coeff, coeff);
164  }
165  }
166 
168  public static LinearExpr Term(BoolVar var, long coeff)
169  {
170  return Prod(var, coeff);
171  }
172 
174  public static LinearExpr Affine(LinearExpr expr, long coeff, long offset)
175  {
176  if (offset == 0)
177  {
178  return Prod(expr, coeff);
179  }
180  return NewBuilder().AddTerm(expr, coeff).Add(offset);
181  }
182 
184  public static LinearExpr Affine(ILiteral literal, long coeff, long offset)
185  {
186  return NewBuilder().AddTerm(literal, coeff).Add(offset);
187  }
188 
190  public static LinearExpr Affine(BoolVar var, long coeff, long offset)
191  {
192  return NewBuilder().AddTerm(var, coeff).Add(offset);
193  }
194 
196  public static LinearExpr Constant(long value)
197  {
198  return NewBuilder(0).Add(value);
199  }
200 
202  public static LinearExprBuilder NewBuilder(int sizeHint = 2)
203  {
204  return new LinearExprBuilder(sizeHint);
205  }
206 
208  {
209  return NewBuilder().Add(a).Add(b);
210  }
211 
212  public static LinearExpr operator +(LinearExpr a, long v)
213  {
214  return NewBuilder().Add(a).Add(v);
215  }
216 
217  public static LinearExpr operator +(long v, LinearExpr a)
218  {
219  return NewBuilder().Add(a).Add(v);
220  }
221 
223  {
224  return NewBuilder().Add(a).AddTerm(b, -1);
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().AddTerm(a, -1).Add(v);
235  }
236 
237  public static LinearExpr operator *(LinearExpr a, long v)
238  {
239  return Prod(a, v);
240  }
241 
242  public static LinearExpr operator *(long v, LinearExpr a)
243  {
244  return Prod(a, v);
245  }
246 
248  {
249  return Prod(a, -1);
250  }
251 
253  {
254  return new BoundedLinearExpression(a, b, true);
255  }
256 
258  {
259  return new BoundedLinearExpression(a, b, false);
260  }
261 
263  {
264  return new BoundedLinearExpression(a, v, true);
265  }
266 
268  {
269  return new BoundedLinearExpression(a, v, false);
270  }
271 
273  {
274  return new BoundedLinearExpression(v, a, Int64.MaxValue);
275  }
276 
278  {
279  return a <= v;
280  }
281 
283  {
284  return new BoundedLinearExpression(v + 1, a, Int64.MaxValue);
285  }
286 
288  {
289  return a < v;
290  }
291 
293  {
294  return new BoundedLinearExpression(Int64.MinValue, a, v);
295  }
296 
298  {
299  return a >= v;
300  }
301 
303  {
304  return new BoundedLinearExpression(Int64.MinValue, a, v - 1);
305  }
306 
308  {
309  return a > v;
310  }
311 
313  {
314  return new BoundedLinearExpression(0, a - b, Int64.MaxValue);
315  }
316 
318  {
319  return new BoundedLinearExpression(1, a - b, Int64.MaxValue);
320  }
321 
323  {
324  return new BoundedLinearExpression(Int64.MinValue, a - b, 0);
325  }
326 
328  {
329  return new BoundedLinearExpression(Int64.MinValue, a - b, -1);
330  }
331 
332  public static LinearExpr Prod(LinearExpr e, long v)
333  {
334  if (v == 0)
335  {
336  return NewBuilder(0);
337  }
338  else if (v == 1)
339  {
340  return e;
341  }
342  else
343  {
344  return NewBuilder(1).AddTerm(e, v);
345  }
346  }
347 
348  public static long GetVarValueMap(LinearExpr e, long initial_coeff, Dictionary<IntVar, long> dict)
349  {
350  List<Term> terms = new List<Term>();
351  if (e is not null)
352  {
353  terms.Add(new Term(e, initial_coeff));
354  }
355  long constant = 0;
356 
357  while (terms.Count > 0)
358  {
359  Term term = terms[0];
360  terms.RemoveAt(0);
361  if (term.coefficient == 0 || term.expr is null)
362  {
363  continue;
364  }
365 
366  if (term.expr is LinearExprBuilder b)
367  {
368  constant += term.coefficient * b.Offset;
369  foreach (Term sub in b.Terms)
370  {
371  if (sub.expr is IntVar i) // Quick unroll.
372  {
373  dict.AddOrIncrement(i, term.coefficient * sub.coefficient);
374  }
375  else
376  {
377  terms.Add(new Term(sub.expr, sub.coefficient * term.coefficient));
378  }
379  }
380  }
381  else if (term.expr is IntVar i)
382  {
383  dict.AddOrIncrement(i, term.coefficient);
384  }
385  else if (term.expr is NotBoolVar notBoolVar)
386  {
387  dict.AddOrIncrement((IntVar)notBoolVar.Not(), -term.coefficient);
388  constant += term.coefficient;
389  }
390  else
391  {
392  throw new ArgumentException("Cannot interpret '" + term.expr.ToString() + "' in an integer expression");
393  }
394  }
395  return constant;
396  }
397 
399  {
400  int numElements = proto.Vars.Count;
401  long offset = proto.Offset;
402  if (numElements == 0)
403  {
404  return LinearExpr.Constant(offset);
405  }
406  else if (numElements == 1)
407  {
408  IntVar var = new IntVar(model, proto.Vars[0]);
409  long coeff = proto.Coeffs[0];
410  return LinearExpr.Affine(var, coeff, offset);
411  }
412  else
413  {
414  LinearExprBuilder builder = LinearExpr.NewBuilder(numElements);
415  for (int i = 0; i < numElements; ++i)
416  {
417  builder.AddTerm(new IntVar(model, proto.Vars[i]), proto.Coeffs[i]);
418  }
419  builder.Add(offset);
420  return builder;
421  }
422  }
423 }
424 
426 public sealed class LinearExprBuilder : LinearExpr
427 {
428  public LinearExprBuilder(int sizeHint = 2)
429  {
430  terms_ = new List<Term>(sizeHint);
431  offset_ = 0;
432  }
433 
436  {
437  return AddTerm(expr, 1);
438  }
439 
442  {
443  return AddTerm(literal, 1);
444  }
445 
448  {
449  return AddTerm(var, 1);
450  }
451 
453  public LinearExprBuilder Add(long constant)
454  {
455  offset_ += constant;
456  return this;
457  }
458 
460  public LinearExprBuilder AddTerm(LinearExpr expr, long coefficient)
461  {
462  terms_.Add(new Term(expr, coefficient));
463  return this;
464  }
465 
467  public LinearExprBuilder AddTerm(ILiteral literal, long coefficient)
468  {
469  if (literal is BoolVar boolVar)
470  {
471  terms_.Add(new Term(boolVar, coefficient));
472  }
473  else
474  {
475  offset_ += coefficient;
476  terms_.Add(new Term(literal.NotAsExpr(), -coefficient));
477  }
478  return this;
479  }
480 
482  public LinearExprBuilder AddTerm(BoolVar var, long coefficient)
483  {
484  terms_.Add(new Term(var, coefficient));
485  return this;
486  }
487 
489  public LinearExprBuilder AddSum(IEnumerable<LinearExpr> exprs)
490  {
491  terms_.TryEnsureCapacity(exprs);
492  foreach (LinearExpr expr in exprs)
493  {
494  AddTerm(expr, 1);
495  }
496  return this;
497  }
498 
500  public LinearExprBuilder AddSum(IEnumerable<ILiteral> literals)
501  {
502  terms_.TryEnsureCapacity(literals);
503  foreach (ILiteral literal in literals)
504  {
505  AddTerm(literal, 1);
506  }
507  return this;
508  }
509 
511  public LinearExprBuilder AddSum(IEnumerable<BoolVar> vars)
512  {
513  terms_.TryEnsureCapacity(vars);
514  foreach (BoolVar var in vars)
515  {
516  AddTerm(var, 1);
517  }
518  return this;
519  }
520 
522  public LinearExprBuilder AddWeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<long> coefficients)
523  {
524  terms_.TryEnsureCapacity(exprs);
525  foreach (var p in exprs.Zip(coefficients, (e, c) => new { Expr = e, Coeff = c }))
526  {
527  AddTerm(p.Expr, p.Coeff);
528  }
529  return this;
530  }
531 
533  public LinearExprBuilder AddWeightedSum(IEnumerable<LinearExpr> exprs, IEnumerable<int> coefficients)
534  {
535  terms_.TryEnsureCapacity(exprs);
536  foreach (var p in exprs.Zip(coefficients, (e, c) => new { Expr = e, Coeff = c }))
537  {
538  AddTerm(p.Expr, p.Coeff);
539  }
540  return this;
541  }
542 
544  public LinearExprBuilder AddWeightedSum(IEnumerable<ILiteral> literals, IEnumerable<int> coefficients)
545  {
546  terms_.TryEnsureCapacity(literals);
547  foreach (var p in literals.Zip(coefficients, (l, c) => new { Literal = l, Coeff = c }))
548  {
549  AddTerm(p.Literal, p.Coeff);
550  }
551  return this;
552  }
553 
555  public LinearExprBuilder AddWeightedSum(IEnumerable<ILiteral> literals, IEnumerable<long> coefficients)
556  {
557  terms_.TryEnsureCapacity(literals);
558  foreach (var p in literals.Zip(coefficients, (l, c) => new { Literal = l, Coeff = c }))
559  {
560  AddTerm(p.Literal, p.Coeff);
561  }
562  return this;
563  }
564 
566  public LinearExprBuilder AddWeightedSum(IEnumerable<BoolVar> vars, IEnumerable<long> coefficients)
567  {
568  terms_.TryEnsureCapacity(vars);
569  foreach (var p in vars.Zip(coefficients, (v, c) => new { Var = v, Coeff = c }))
570  {
571  AddTerm(p.Var, p.Coeff);
572  }
573  return this;
574  }
575 
577  public LinearExprBuilder AddWeightedSum(IEnumerable<BoolVar> vars, IEnumerable<int> coefficients)
578  {
579  terms_.TryEnsureCapacity(vars);
580  foreach (var p in vars.Zip(coefficients, (v, c) => new { Var = v, Coeff = c }))
581  {
582  AddTerm(p.Var, p.Coeff);
583  }
584  return this;
585  }
586 
587  public override string ToString()
588  {
589  string result = "";
590  foreach (Term term in terms_)
591  {
592  bool first = String.IsNullOrEmpty(result);
593  if (term.expr is null || term.coefficient == 0)
594  {
595  continue;
596  }
597  if (term.coefficient == 1)
598  {
599  if (!first)
600  {
601  result += " + ";
602  }
603 
604  result += term.expr.ToString();
605  }
606  else if (term.coefficient > 0)
607  {
608  if (!first)
609  {
610  result += " + ";
611  }
612 
613  result += String.Format("{0} * {1}", term.coefficient, term.expr.ToString());
614  }
615  else if (term.coefficient == -1)
616  {
617  if (!first)
618  {
619  result += String.Format(" - {0}", term.expr.ToString());
620  }
621  else
622  {
623  result += String.Format("-{0}", term.expr.ToString());
624  }
625  }
626  else
627  {
628  if (!first)
629  {
630  result += String.Format(" - {0} * {1}", -term.coefficient, term.expr.ToString());
631  }
632  else
633  {
634  result += String.Format("{0} * {1}", term.coefficient, term.expr.ToString());
635  }
636  }
637  }
638  if (offset_ > 0)
639  {
640  if (!String.IsNullOrEmpty(result))
641  {
642  result += String.Format(" + {0}", offset_);
643  }
644  else
645  {
646  result += String.Format("{0}", offset_);
647  }
648  }
649  else if (offset_ < 0)
650  {
651  if (!String.IsNullOrEmpty(result))
652  {
653  result += String.Format(" - {0}", -offset_);
654  }
655  else
656  {
657  result += String.Format("{0}", offset_);
658  }
659  }
660  return result;
661  }
662 
663  public long Offset
664  {
665  get {
666  return offset_;
667  }
668  }
669 
670  public List<Term> Terms
671  {
672  get {
673  return terms_;
674  }
675  }
676 
677  private long offset_;
678  private List<Term> terms_;
679 }
680 
689 public class IntVar : LinearExpr
690 {
691  public IntVar(CpModelProto model, Domain domain, string name)
692  {
693  index_ = model.Variables.Count;
694  var_ = new IntegerVariableProto();
695  var_.Name = name;
696  var_.Domain.Add(domain.FlattenedIntervals());
697  model.Variables.Add(var_);
698  }
699 
700  public IntVar(CpModelProto model, long lb, long ub, string name)
701  {
702  index_ = model.Variables.Count;
703  var_ = new IntegerVariableProto();
704  var_.Name = name;
705  var_.Domain.Capacity = 2;
706  var_.Domain.Add(lb);
707  var_.Domain.Add(ub);
708  model.Variables.Add(var_);
709  }
710 
711  public IntVar(CpModelProto model, int index)
712  {
713  index_ = index;
714  var_ = model.Variables[index];
715  }
716 
718  public int GetIndex()
719  {
720  return index_;
721  }
722 
724  public int Index
725  {
726  get {
727  return GetIndex();
728  }
729  }
730 
733  {
734  get {
735  return var_;
736  }
737  set {
738  var_ = value;
739  }
740  }
741 
743  public Domain Domain
744  {
745  get {
747  }
748  }
749 
750  public override string ToString()
751  {
752  return var_.Name ?? var_.ToString();
753  }
754 
756  public string Name()
757  {
758  return var_.Name;
759  }
760 
761  protected readonly int index_;
763 }
764 
773 public sealed class BoolVar : IntVar, ILiteral
774 {
775 
776  public BoolVar(CpModelProto model, String name) : base(model, 0, 1, name)
777  {
778  }
779 
780  public BoolVar(CpModelProto model, int index) : base(model, index)
781  {
782  }
783 
785  public ILiteral Not()
786  {
787  return negation_ ??= new NotBoolVar(this);
788  }
789 
792  {
793  return (LinearExpr)Not();
794  }
795 
796  private NotBoolVar negation_;
797 }
798 
799 public sealed class NotBoolVar : LinearExpr, ILiteral
800 {
801  public NotBoolVar(BoolVar boolvar)
802  {
803  boolvar_ = boolvar;
804  }
805 
806  public int GetIndex()
807  {
808  return -boolvar_.GetIndex() - 1;
809  }
810 
811  public int Index
812  {
813  get {
814  return GetIndex();
815  }
816  }
817 
818  public ILiteral Not()
819  {
820  return boolvar_;
821  }
822 
824  {
825  return (LinearExpr)Not();
826  }
827 
828  public override string ToString()
829  {
830  return String.Format("Not({0})", boolvar_.ToString());
831  }
832 
833  private readonly BoolVar boolvar_;
834 }
835 
844 public sealed class BoundedLinearExpression
845 {
846  public enum Type
847  {
848  BoundExpression,
849  VarEqVar,
850  VarDiffVar,
851  VarEqCst,
852  VarDiffCst,
853  }
854 
855  public BoundedLinearExpression(long lb, LinearExpr expr, long ub)
856  {
857  left_ = expr;
858  right_ = null;
859  lb_ = lb;
860  ub_ = ub;
861  type_ = Type.BoundExpression;
862  }
863 
864  public BoundedLinearExpression(LinearExpr left, LinearExpr right, bool equality)
865  {
866  left_ = left;
867  right_ = right;
868  lb_ = 0;
869  ub_ = 0;
870  type_ = equality ? Type.VarEqVar : Type.VarDiffVar;
871  }
872 
873  public BoundedLinearExpression(LinearExpr left, long v, bool equality)
874  {
875  left_ = left;
876  right_ = null;
877  lb_ = v;
878  ub_ = 0;
879  type_ = equality ? Type.VarEqCst : Type.VarDiffCst;
880  }
881 
882  bool IsTrue()
883  {
884  if (type_ == Type.VarEqVar)
885  {
886  return (object)left_ == (object)right_;
887  }
888  else if (type_ == Type.VarDiffVar)
889  {
890  return (object)left_ != (object)right_;
891  }
892  return false;
893  }
894 
895  public static bool operator true(BoundedLinearExpression bie)
896  {
897  return bie.IsTrue();
898  }
899 
900  public static bool operator false(BoundedLinearExpression bie)
901  {
902  return !bie.IsTrue();
903  }
904 
905  public override string ToString()
906  {
907  switch (type_)
908  {
909  case Type.BoundExpression:
910  return String.Format("{0} <= {1} <= {2}", lb_, left_, ub_);
911  case Type.VarEqVar:
912  return String.Format("{0} == {1}", left_, right_);
913  case Type.VarDiffVar:
914  return String.Format("{0} != {1}", left_, right_);
915  case Type.VarEqCst:
916  return String.Format("{0} == {1}", left_, lb_);
917  case Type.VarDiffCst:
918  return String.Format("{0} != {1}", left_, lb_);
919  default:
920  throw new ArgumentException("Wrong mode in BoundedLinearExpression.");
921  }
922  }
923 
925  {
926  if (a.CtType != Type.BoundExpression || a.Ub != Int64.MaxValue)
927  {
928  throw new ArgumentException("Operator <= not supported for this BoundedLinearExpression");
929  }
930  return new BoundedLinearExpression(a.Lb, a.Left, v);
931  }
932 
934  {
935  if (a.CtType != Type.BoundExpression || a.Ub != Int64.MaxValue)
936  {
937  throw new ArgumentException("Operator < not supported for this BoundedLinearExpression");
938  }
939  return new BoundedLinearExpression(a.Lb, a.Left, v - 1);
940  }
941 
943  {
944  if (a.CtType != Type.BoundExpression || a.Lb != Int64.MinValue)
945  {
946  throw new ArgumentException("Operator >= not supported for this BoundedLinearExpression");
947  }
948  return new BoundedLinearExpression(v, a.Left, a.Ub);
949  }
950 
952  {
953  if (a.CtType != Type.BoundExpression || a.Lb != Int64.MinValue)
954  {
955  throw new ArgumentException("Operator < not supported for this BoundedLinearExpression");
956  }
957  return new BoundedLinearExpression(v + 1, a.Left, a.Ub);
958  }
959 
960  public LinearExpr Left
961  {
962  get {
963  return left_;
964  }
965  }
966 
967  public LinearExpr Right
968  {
969  get {
970  return right_;
971  }
972  }
973 
974  public long Lb
975  {
976  get {
977  return lb_;
978  }
979  }
980 
981  public long Ub
982  {
983  get {
984  return ub_;
985  }
986  }
987 
988  public Type CtType
989  {
990  get {
991  return type_;
992  }
993  }
994 
995  private LinearExpr left_;
996  private LinearExpr right_;
997  private long lb_;
998  private long ub_;
999  private Type type_;
1000 }
1001 
1002 } // namespace Google.OrTools.Sat
static BoundedLinearExpression operator>(long v, LinearExpr a)
static LinearExpr operator -(LinearExpr a)
LinearExprBuilder AddWeightedSum(IEnumerable< BoolVar > vars, IEnumerable< long > coefficients)
Adds sum(vars[i] * coeffs[i]) to the builder.
static long GetVarValueMap(LinearExpr e, long initial_coeff, Dictionary< IntVar, long > dict)
LinearExprBuilder AddWeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< long > coefficients)
Adds sum(exprs[i] * coeffs[i]) to the builder.
static LinearExpr WeightedSum(IEnumerable< ILiteral > literals, IEnumerable< long > coeffs)
Creates Sum(literals[i] * coeffs[i]).
ILiteral Not()
Returns the Boolean negation of the literal.
Some constraints supports linear expression instead of just using a reference to a variable.
Definition: CpModel.pb.cs:695
static LinearExpr Constant(long value)
Creates a constant expression.
static LinearExpr Term(BoolVar var, long coeff)
Creates var * coeff.
LinearExprBuilder Add(long constant)
Adds constant to the builder.
BoundedLinearExpression(long lb, LinearExpr expr, long ub)
static LinearExpr operator -(LinearExpr a, long v)
IntegerVariableProto var_
static BoundedLinearExpression operator>(LinearExpr a, LinearExpr b)
static BoundedLinearExpression operator<(long v, LinearExpr a)
int GetIndex()
Returns the logical index of the literal.
static BoundedLinearExpression operator !=(LinearExpr a, LinearExpr b)
static LinearExpr WeightedSum(IEnumerable< ILiteral > literals, IEnumerable< int > coeffs)
Creates Sum(literals[i] * coeffs[i]).
static LinearExpr Affine(LinearExpr expr, long coeff, long offset)
Creates expr * coeff + offset.
LinearExprBuilder AddTerm(ILiteral literal, long coefficient)
Adds literal * coefficient to the builder.
Holds a linear constraint: expression ∈ domain
IntVar(CpModelProto model, long lb, long ub, string name)
LinearExprBuilder AddWeightedSum(IEnumerable< ILiteral > literals, IEnumerable< long > coefficients)
Adds sum(literals[i] * coeffs[i]) to the builder.
Holds a integer variable with a discrete domain.
static LinearExpr Affine(BoolVar var, long coeff, long offset)
Creates var * coeff + offset.
LinearExprBuilder Add(BoolVar var)
Adds var to the builder.
Holds a Boolean variable or its negation.
ILiteral Not()
Returns the Boolean negation of that variable.
LinearExprBuilder AddWeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< int > coefficients)
Adds sum(exprs[i] * coeffs[i]) to the builder.
static LinearExpr operator *(long v, LinearExpr a)
int GetIndex()
Returns the index of the variable in the underlying CpModelProto.
static BoundedLinearExpression operator >=(long v, LinearExpr a)
static LinearExpr operator -(LinearExpr a, LinearExpr b)
LinearExprBuilder AddSum(IEnumerable< LinearExpr > exprs)
Adds sum(exprs) to the builder.
static LinearExpr Term(ILiteral literal, long coeff)
Creates literal * coeff.
static BoundedLinearExpression operator >=(BoundedLinearExpression a, long v)
static void AddOrIncrement(this Dictionary< IntVar, long > dict, IntVar key, long increment)
static LinearExpr Sum(IEnumerable< BoolVar > vars)
Creates Sum(vars).
static LinearExprBuilder NewBuilder(int sizeHint=2)
Creates a builder class for linear expression.
static BoundedLinearExpression operator !=(LinearExpr a, long v)
LinearExprBuilder AddWeightedSum(IEnumerable< ILiteral > literals, IEnumerable< int > coefficients)
Adds sum(literals[i] * coeffs[i]) to the builder.
static void TrySetCapacity< TField, TValues >(this RepeatedField< TField > field, IEnumerable< TValues > values)
static Domain VariableDomain(Google.OrTools.Sat.IntegerVariableProto variable_proto)
Definition: CpSatHelper.cs:64
static BoundedLinearExpression operator>(BoundedLinearExpression a, long v)
static LinearExpr WeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< int > coeffs)
Creates Sum(exprs[i] * coeffs[i]).
Term(LinearExpr e, long c)
static BoundedLinearExpression operator>(LinearExpr a, long v)
LinearExprBuilder AddSum(IEnumerable< ILiteral > literals)
Adds sum(literals) to the builder.
static void TryEnsureCapacity< TValue, TValues >(this List< TValue > list, IEnumerable< TValues > values)
IntVar(CpModelProto model, Domain domain, string name)
static BoundedLinearExpression operator<(LinearExpr a, long v)
static BoundedLinearExpression operator >=(LinearExpr a, LinearExpr b)
LinearExprBuilder Add(ILiteral literal)
Adds literal to the builder.
int GetIndex()
Returns the logical index of the literal.
LinearExprBuilder AddSum(IEnumerable< BoolVar > vars)
Adds sum(vars) to the builder.
A builder class for linear expressions.
IntVar(CpModelProto model, int index)
static LinearExpr operator *(LinearExpr a, long v)
pbc::RepeatedField< int > Vars
Definition: CpModel.pb.cs:748
A constraint programming problem.
Definition: CpModel.pb.cs:8639
static LinearExpr Term(LinearExpr expr, long coeff)
Creates expr * coeff.
LinearExprBuilder AddTerm(BoolVar var, long coefficient)
Adds var * coefficient to the builder.
static BoundedLinearExpression operator<(LinearExpr a, LinearExpr b)
static BoundedLinearExpression operator<=(BoundedLinearExpression a, long v)
pbc::RepeatedField< global::Google.OrTools.Sat.IntegerVariableProto > Variables
The associated Protos should be referred by their index in these fields.
Definition: CpModel.pb.cs:8716
BoolVar(CpModelProto model, int index)
BoundedLinearExpression(LinearExpr left, long v, bool equality)
pbc::RepeatedField< long > Domain
The variable domain given as a sorted list of n disjoint intervals [min, max] and encoded as [min_0,...
Definition: CpModel.pb.cs:354
Holds a linear expression: sum (ai * xi) + b.
static BoundedLinearExpression operator==(LinearExpr a, LinearExpr b)
LinearExpr NotAsExpr()
Returns the Boolean negation of that variable as a linear expression.
Holds a Boolean variable.
string Name
For debug/logging only.
Definition: CpModel.pb.cs:321
pbc::RepeatedField< long > Coeffs
Definition: CpModel.pb.cs:759
int Index
Returns the index of the variable in the underlying CpModelProto.
string Name()
Returns the name of the variable given upon creation.
static LinearExpr WeightedSum(IEnumerable< BoolVar > vars, IEnumerable< long > coeffs)
Creates Sum(vars[i] * coeffs[i]).
static BoundedLinearExpression operator<(BoundedLinearExpression a, long v)
static LinearExpr Affine(ILiteral literal, long coeff, long offset)
Creates literal * coeff + offset.
static BoundedLinearExpression operator<=(LinearExpr a, long v)
static LinearExpr operator+(LinearExpr a, LinearExpr b)
LinearExpr NotAsExpr()
Returns the Boolean negation of the literal as a linear expression.
static LinearExpr RebuildLinearExprFromLinearExpressionProto(LinearExpressionProto proto, CpModelProto model)
LinearExpr NotAsExpr()
Returns the Boolean negation of the literal as a linear expression.
static LinearExpr WeightedSum(IEnumerable< LinearExpr > exprs, IEnumerable< long > coeffs)
Creates Sum(exprs[i] * coeffs[i]).
static LinearExpr operator -(long v, LinearExpr a)
static LinearExpr WeightedSum(IEnumerable< BoolVar > vars, IEnumerable< int > coeffs)
Creates Sum(vars[i] * coeffs[i]).
BoolVar(CpModelProto model, String name)
static LinearExpr Sum(IEnumerable< ILiteral > literals)
Creates Sum(literals).
static LinearExpr Prod(LinearExpr e, long v)
LinearExprBuilder AddTerm(LinearExpr expr, long coefficient)
Adds expr * coefficient to the builder.
LinearExprBuilder AddWeightedSum(IEnumerable< BoolVar > vars, IEnumerable< int > coefficients)
Adds sum(vars[i] * coeffs[i]) to the builder.
IntegerVariableProto Proto
The underlying IntegerVariableProto.
ILiteral Not()
Returns the Boolean negation of the literal.
LinearExprBuilder Add(LinearExpr expr)
Adds expr to the builder.
static LinearExpr Sum(IEnumerable< LinearExpr > exprs)
Creates Sum(exprs).
static BoundedLinearExpression operator >=(LinearExpr a, long v)
BoundedLinearExpression(LinearExpr left, LinearExpr right, bool equality)