DotNet Reference

.Net Reference

CpModel.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 System;
17using System.Collections.Generic;
18using Google.OrTools.Util;
19
23public class CpModel
24{
25 public CpModel()
26 {
27 model_ = new CpModelProto();
28 constant_map_ = new Dictionary<long, int>();
29 terms_ = new Queue<Term>();
30 }
31
32 // Getters.
33
40 {
41 get {
42 return model_;
43 }
44 }
45
46 int Negated(int index)
47 {
48 return -index - 1;
49 }
50
51 // Integer variables and constraints.
52
63 public IntVar NewIntVar(long lb, long ub, string name)
64 {
65 return new IntVar(model_, lb, ub, name);
66 }
67
77 public IntVar NewIntVarFromDomain(Domain domain, string name)
78 {
79 return new IntVar(model_, domain, name);
80 }
81
87 public IntVar NewConstant(long value)
88 {
89 return new IntVar(model_, ConvertConstant(value));
90 }
91
97 public BoolVar NewBoolVar(string name)
98 {
99 return new BoolVar(model_, name);
100 }
101
108 {
109 return true_literal_ ??= new BoolVar(model_, ConvertConstant(1));
110 }
111
118 {
119 return TrueLiteral().Not();
120 }
121
122 private long FillLinearConstraint(LinearExpr expr, out LinearConstraintProto linear)
123 {
124 linear = new LinearConstraintProto();
125 Dictionary<IntVar, long> dict = new Dictionary<IntVar, long>();
126 long constant = LinearExpr.GetVarValueMap(expr, 1L, dict, terms_);
127 var count = dict.Count;
128 linear.Vars.Capacity = count;
129 linear.Coeffs.Capacity = count;
130 foreach (KeyValuePair<IntVar, long> term in dict)
131 {
132 linear.Vars.Add(term.Key.Index);
133 linear.Coeffs.Add(term.Value);
134 }
135 return constant;
136 }
142 public Constraint AddLinearConstraint(LinearExpr expr, long lb, long ub)
143 {
144 long constant = FillLinearConstraint(expr, out var linear);
145 linear.Domain.Capacity = 2;
146 linear.Domain.Add(lb is Int64.MinValue or Int64.MaxValue ? lb : lb - constant);
147 linear.Domain.Add(ub is Int64.MinValue or Int64.MaxValue ? ub : ub - constant);
148
149 Constraint ct = new Constraint(model_);
150 ct.Proto.Linear = linear;
151 return ct;
152 }
153
160 {
161 long constant = FillLinearConstraint(expr, out var linear);
162 var array = domain.FlattenedIntervals();
163 linear.Domain.Capacity = array.Length;
164 foreach (long value in array)
165 {
166 linear.Domain.Add(value is Int64.MinValue or Int64.MaxValue ? value : value - constant);
167 }
168
169 Constraint ct = new Constraint(model_);
170 ct.Proto.Linear = linear;
171 return ct;
172 }
173
174 private Constraint AddLinearExpressionNotEqualCst(LinearExpr expr, long value)
175 {
176 long constant = FillLinearConstraint(expr, out var linear);
177 linear.Domain.Capacity = 4;
178 linear.Domain.Add(Int64.MinValue);
179 linear.Domain.Add(value - constant - 1);
180 linear.Domain.Add(value - constant + 1);
181 linear.Domain.Add(Int64.MaxValue);
182
183 Constraint ct = new Constraint(model_);
184 ct.Proto.Linear = linear;
185 return ct;
186 }
187
194 {
195 switch (lin.CtType)
196 {
197 case BoundedLinearExpression.Type.BoundExpression: {
198 return AddLinearConstraint(lin.Left, lin.Lb, lin.Ub);
199 }
200 case BoundedLinearExpression.Type.VarEqVar: {
201 return AddLinearConstraint(lin.Left - lin.Right, 0, 0);
202 }
203 case BoundedLinearExpression.Type.VarDiffVar: {
204 return AddLinearExpressionNotEqualCst(lin.Left - lin.Right, 0);
205 }
206 case BoundedLinearExpression.Type.VarEqCst: {
207 return AddLinearConstraint(lin.Left, lin.Lb, lin.Lb);
208 }
209 case BoundedLinearExpression.Type.VarDiffCst: {
210 return AddLinearExpressionNotEqualCst(lin.Left, lin.Lb);
211 }
212 }
213 return null;
214 }
215
221 public Constraint AddAllDifferent(IEnumerable<LinearExpr> exprs)
222 {
224 alldiff.Exprs.TrySetCapacity(exprs);
225 foreach (LinearExpr expr in exprs)
226 {
227 alldiff.Exprs.Add(GetLinearExpressionProto(expr));
228 }
229
230 Constraint ct = new Constraint(model_);
231 ct.Proto.AllDiff = alldiff;
232 return ct;
233 }
234
240 public Constraint AddElement(IntVar index, IEnumerable<IntVar> vars, IntVar target)
241 {
243 element.Index = index.Index;
244 element.Vars.TrySetCapacity(vars);
245 foreach (IntVar var in vars)
246 {
247 element.Vars.Add(var.Index);
248 }
249 element.Target = target.Index;
250
251 Constraint ct = new Constraint(model_);
252 ct.Proto.Element = element;
253 return ct;
254 }
255
261 public Constraint AddElement(IntVar index, IEnumerable<long> values, IntVar target)
262 {
264 element.Index = index.Index;
265 element.Vars.TrySetCapacity(values);
266 foreach (long value in values)
267 {
268 element.Vars.Add(ConvertConstant(value));
269 }
270 element.Target = target.Index;
271
272 Constraint ct = new Constraint(model_);
273 ct.Proto.Element = element;
274 return ct;
275 }
276
282 public Constraint AddElement(IntVar index, IEnumerable<int> values, IntVar target)
283 {
285 element.Index = index.Index;
286 element.Vars.TrySetCapacity(values);
287 foreach (int value in values)
288 {
289 element.Vars.Add(ConvertConstant(value));
290 }
291 element.Target = target.Index;
292
293 Constraint ct = new Constraint(model_);
294 ct.Proto.Element = element;
295 return ct;
296 }
297
309 {
310 CircuitConstraint ct = new CircuitConstraint(model_);
312 return ct;
313 }
314
327 {
330 return ct;
331 }
332
348 public TableConstraint AddAllowedAssignments(IEnumerable<IntVar> vars)
349 {
351 table.Vars.TrySetCapacity(vars);
352 foreach (IntVar var in vars)
353 {
354 table.Vars.Add(var.Index);
355 }
356
357 TableConstraint ct = new TableConstraint(model_);
358 ct.Proto.Table = table;
359 return ct;
360 }
361
376 public TableConstraint AddForbiddenAssignments(IEnumerable<IntVar> vars)
377 {
379 ct.Proto.Table.Negated = true;
380 return ct;
381 }
382
416 public AutomatonConstraint AddAutomaton(IEnumerable<IntVar> vars, long starting_state,
417 IEnumerable<long> final_states)
418 {
420 aut.Vars.TrySetCapacity(vars);
421 foreach (IntVar var in vars)
422 {
423 aut.Vars.Add(var.Index);
424 }
425
426 aut.StartingState = starting_state;
427 aut.FinalStates.AddRange(final_states);
428
430 ct.Proto.Automaton = aut;
431 return ct;
432 }
433
447 public Constraint AddInverse(IEnumerable<IntVar> direct, IEnumerable<IntVar> reverse)
448 {
450 inverse.FDirect.TrySetCapacity(direct);
451 foreach (IntVar var in direct)
452 {
453 inverse.FDirect.Add(var.Index);
454 }
455
456 inverse.FInverse.TrySetCapacity(reverse);
457 foreach (IntVar var in reverse)
458 {
459 inverse.FInverse.Add(var.Index);
460 }
461
462 Constraint ct = new Constraint(model_);
463 ct.Proto.Inverse = inverse;
464 return ct;
465 }
466
493 public ReservoirConstraint AddReservoirConstraint(long minLevel, long maxLevel)
494 {
496
497 res.MinLevel = minLevel;
498 res.MaxLevel = maxLevel;
499
500 ReservoirConstraint ct = new ReservoirConstraint(this, model_);
501 ct.Proto.Reservoir = res;
502 return ct;
503 }
504
505 public void AddMapDomain(IntVar var, IEnumerable<IntVar> bool_vars, long offset = 0)
506 {
507 int i = 0;
508 int var_index = var.Index;
509 foreach (IntVar bool_var in bool_vars)
510 {
511 int b_index = bool_var.Index;
512
514 lin1.Vars.Capacity = 1;
515 lin1.Vars.Add(var_index);
516 lin1.Coeffs.Capacity = 1;
517 lin1.Coeffs.Add(1L);
518 lin1.Domain.Capacity = 2;
519 lin1.Domain.Add(offset + i);
520 lin1.Domain.Add(offset + i);
522 ct1.Linear = lin1;
523 ct1.EnforcementLiteral.Add(b_index);
524 model_.Constraints.Add(ct1);
525
527 lin1.Vars.Capacity = 1;
528 lin2.Vars.Add(var_index);
529 lin1.Coeffs.Capacity = 1;
530 lin2.Coeffs.Add(1L);
531 lin1.Domain.Capacity = 4;
532 lin2.Domain.Add(Int64.MinValue);
533 lin2.Domain.Add(offset + i - 1);
534 lin2.Domain.Add(offset + i + 1);
535 lin2.Domain.Add(Int64.MaxValue);
537 ct2.Linear = lin2;
538 ct2.EnforcementLiteral.Add(-b_index - 1);
539 model_.Constraints.Add(ct2);
540
541 i++;
542 }
543 }
544
551 {
553 or.Literals.Capacity = 2;
554 or.Literals.Add(a.Not().GetIndex());
555 or.Literals.Add(b.GetIndex());
556
557 Constraint ct = new Constraint(model_);
558 ct.Proto.BoolOr = or;
559 return ct;
560 }
561
567 public Constraint AddBoolOr(IEnumerable<ILiteral> literals)
568 {
569 BoolArgumentProto bool_argument = new BoolArgumentProto();
570 bool_argument.Literals.TrySetCapacity(literals);
571 foreach (ILiteral lit in literals)
572 {
573 bool_argument.Literals.Add(lit.GetIndex());
574 }
575
576 Constraint ct = new Constraint(model_);
577 ct.Proto.BoolOr = bool_argument;
578 return ct;
579 }
580
586 public Constraint AddAtLeastOne(IEnumerable<ILiteral> literals)
587 {
588 return AddBoolOr(literals);
589 }
590
596 public Constraint AddAtMostOne(IEnumerable<ILiteral> literals)
597 {
598 BoolArgumentProto bool_argument = new BoolArgumentProto();
599 bool_argument.Literals.TrySetCapacity(literals);
600 foreach (ILiteral lit in literals)
601 {
602 bool_argument.Literals.Add(lit.GetIndex());
603 }
604
605 Constraint ct = new Constraint(model_);
606 ct.Proto.AtMostOne = bool_argument;
607 return ct;
608 }
609
615 public Constraint AddExactlyOne(IEnumerable<ILiteral> literals)
616 {
617 BoolArgumentProto bool_argument = new BoolArgumentProto();
618 bool_argument.Literals.TrySetCapacity(literals);
619 foreach (ILiteral lit in literals)
620 {
621 bool_argument.Literals.Add(lit.GetIndex());
622 }
623
624 Constraint ct = new Constraint(model_);
625 ct.Proto.ExactlyOne = bool_argument;
626 return ct;
627 }
628
634 public Constraint AddBoolAnd(IEnumerable<ILiteral> literals)
635 {
636 BoolArgumentProto bool_argument = new BoolArgumentProto();
637 bool_argument.Literals.TrySetCapacity(literals);
638 foreach (ILiteral lit in literals)
639 {
640 bool_argument.Literals.Add(lit.GetIndex());
641 }
642
643 Constraint ct = new Constraint(model_);
644 ct.Proto.BoolAnd = bool_argument;
645 return ct;
646 }
647
653 public Constraint AddBoolXor(IEnumerable<ILiteral> literals)
654 {
655 BoolArgumentProto bool_argument = new BoolArgumentProto();
656 bool_argument.Literals.TrySetCapacity(literals);
657 foreach (ILiteral lit in literals)
658 {
659 bool_argument.Literals.Add(lit.GetIndex());
660 }
661
662 Constraint ct = new Constraint(model_);
663 ct.Proto.BoolXor = bool_argument;
664 return ct;
665 }
666
672 public Constraint AddMinEquality(LinearExpr target, IEnumerable<LinearExpr> exprs)
673 {
675 lin.Exprs.TrySetCapacity(exprs);
676 foreach (LinearExpr expr in exprs)
677 {
678 lin.Exprs.Add(GetLinearExpressionProto(expr, /*negate=*/true));
679 }
680 lin.Target = GetLinearExpressionProto(target, /*negate=*/true);
681
682 Constraint ct = new Constraint(model_);
683 ct.Proto.LinMax = lin;
684 return ct;
685 }
686
692 public Constraint AddMaxEquality(LinearExpr target, IEnumerable<LinearExpr> exprs)
693 {
695 lin.Exprs.TrySetCapacity(exprs);
696 foreach (LinearExpr expr in exprs)
697 {
698 lin.Exprs.Add(GetLinearExpressionProto(expr));
699 }
700 lin.Target = GetLinearExpressionProto(target);
701
702 Constraint ct = new Constraint(model_);
703 ct.Proto.LinMax = lin;
704 return ct;
705 }
706
712 public Constraint AddDivisionEquality<T, N, D>(T target, N num, D denom)
713 {
715 div.Exprs.Capacity = 2;
716 div.Exprs.Add(GetLinearExpressionProto(GetLinearExpr(num)));
717 div.Exprs.Add(GetLinearExpressionProto(GetLinearExpr(denom)));
718 div.Target = GetLinearExpressionProto(GetLinearExpr(target));
719
720 Constraint ct = new Constraint(model_);
721 ct.Proto.IntDiv = div;
722 return ct;
723 }
724
731 {
733 abs.Exprs.Capacity = 2;
734 abs.Exprs.Add(GetLinearExpressionProto(expr));
735 abs.Exprs.Add(GetLinearExpressionProto(expr, /*negate=*/true));
736 abs.Target = GetLinearExpressionProto(target);
737
738 Constraint ct = new Constraint(model_);
739 ct.Proto.LinMax = abs;
740 return ct;
741 }
742
748 public Constraint AddModuloEquality<T, V, M>(T target, V v, M m)
749 {
751 mod.Exprs.Capacity = 2;
752 mod.Exprs.Add(GetLinearExpressionProto(GetLinearExpr(v)));
753 mod.Exprs.Add(GetLinearExpressionProto(GetLinearExpr(m)));
754 mod.Target = GetLinearExpressionProto(GetLinearExpr(target));
755
756 Constraint ct = new Constraint(model_);
757 ct.Proto.IntMod = mod;
758 return ct;
759 }
760
766 public Constraint AddMultiplicationEquality(LinearExpr target, IEnumerable<LinearExpr> exprs)
767 {
769 prod.Target = GetLinearExpressionProto(target);
770 prod.Exprs.TrySetCapacity(exprs);
771 foreach (LinearExpr expr in exprs)
772 {
773 prod.Exprs.Add(GetLinearExpressionProto(expr));
774 }
775
776 Constraint ct = new Constraint(model_);
777 ct.Proto.IntProd = prod;
778 return ct;
779 }
780
787 {
789 prod.Target = GetLinearExpressionProto(target);
790 prod.Exprs.Capacity = 2;
791 prod.Exprs.Add(GetLinearExpressionProto(left));
792 prod.Exprs.Add(GetLinearExpressionProto(right));
793
794 Constraint ct = new Constraint(model_);
795 ct.Proto.IntProd = prod;
796 return ct;
797 }
798
799 // Scheduling support
800
816 public IntervalVar NewIntervalVar<S, D, E>(S start, D size, E end, string name)
817 {
818 LinearExpr startExpr = GetLinearExpr(start);
819 LinearExpr sizeExpr = GetLinearExpr(size);
820 LinearExpr endExpr = GetLinearExpr(end);
821 Add(startExpr + sizeExpr == endExpr);
822
823 LinearExpressionProto startProto = GetLinearExpressionProto(startExpr);
826 return new IntervalVar(model_, startProto, sizeProto, endProto, name);
827 }
828
844 public IntervalVar NewFixedSizeIntervalVar<S>(S start, long size, string name)
845 {
846 LinearExpr startExpr = GetLinearExpr(start);
847 LinearExpr sizeExpr = GetLinearExpr(size);
848 LinearExpr endExpr = LinearExpr.Sum(new LinearExpr[] { startExpr, sizeExpr });
849
850 LinearExpressionProto startProto = GetLinearExpressionProto(startExpr);
853 return new IntervalVar(model_, startProto, sizeProto, endProto, name);
854 }
855
874 public IntervalVar NewOptionalIntervalVar<S, D, E>(S start, D size, E end, ILiteral is_present, string name)
875 {
876 LinearExpr startExpr = GetLinearExpr(start);
877 LinearExpr sizeExpr = GetLinearExpr(size);
878 LinearExpr endExpr = GetLinearExpr(end);
879 Add(startExpr + sizeExpr == endExpr).OnlyEnforceIf(is_present);
880
881 LinearExpressionProto startProto = GetLinearExpressionProto(startExpr);
884 return new IntervalVar(model_, startProto, sizeProto, endProto, is_present.GetIndex(), name);
885 }
886
905 public IntervalVar NewOptionalFixedSizeIntervalVar<S>(S start, long size, ILiteral is_present, string name)
906 {
907 LinearExpr startExpr = GetLinearExpr(start);
908 LinearExpr sizeExpr = GetLinearExpr(size);
909 LinearExpr endExpr = LinearExpr.Sum(new LinearExpr[] { startExpr, sizeExpr });
910
911 LinearExpressionProto startProto = GetLinearExpressionProto(startExpr);
914 return new IntervalVar(model_, startProto, sizeProto, endProto, is_present.GetIndex(), name);
915 }
916
926 public Constraint AddNoOverlap(IEnumerable<IntervalVar> intervals)
927 {
929 no_overlap.Intervals.TrySetCapacity(intervals);
930 foreach (IntervalVar var in intervals)
931 {
932 no_overlap.Intervals.Add(var.GetIndex());
933 }
934
935 Constraint ct = new Constraint(model_);
936 ct.Proto.NoOverlap = no_overlap;
937 return ct;
938 }
939
957 {
960 return ct;
961 }
962
983 {
985 LinearExpr capacityExpr = GetLinearExpr(capacity);
986 cumul.Capacity = GetLinearExpressionProto(capacityExpr);
987
988 CumulativeConstraint ct = new CumulativeConstraint(this, model_);
989 ct.Proto.Cumulative = cumul;
990 return ct;
991 }
992
993 // Objective.
994
996 public void Minimize(LinearExpr obj)
997 {
998 SetObjective(obj, true);
999 }
1000
1002 public void Maximize(LinearExpr obj)
1003 {
1004 SetObjective(obj, false);
1005 }
1006
1008 void ClearObjective()
1009 {
1010 model_.Objective = null;
1011 }
1012
1014 bool HasObjective()
1015 {
1016 return model_.Objective is not null;
1017 }
1018
1019 // Search Decision.
1020
1022 public void AddDecisionStrategy(IEnumerable<IntVar> vars,
1025 {
1027 ds.Variables.TrySetCapacity(vars);
1028 foreach (IntVar var in vars)
1029 {
1030 ds.Variables.Add(var.Index);
1031 }
1032 ds.VariableSelectionStrategy = var_str;
1033 ds.DomainReductionStrategy = dom_str;
1034 model_.SearchStrategy.Add(ds);
1035 }
1036
1038 public void AddHint(IntVar var, long value)
1039 {
1040 model_.SolutionHint ??= new PartialVariableAssignment();
1041 model_.SolutionHint.Vars.Add(var.GetIndex());
1042 model_.SolutionHint.Values.Add(value);
1043 }
1044
1046 public void ClearHints()
1047 {
1048 model_.SolutionHint = null;
1049 }
1050
1052 public void AddAssumption(ILiteral lit)
1053 {
1054 model_.Assumptions.Add(lit.GetIndex());
1055 }
1056
1058 public void AddAssumptions(IEnumerable<ILiteral> literals)
1059 {
1060 foreach (ILiteral lit in literals)
1061 {
1062 AddAssumption(lit);
1063 }
1064 }
1065
1067 public void ClearAssumptions()
1068 {
1069 model_.Assumptions.Clear();
1070 }
1071
1072 // Internal methods.
1073
1074 void SetObjective(LinearExpr obj, bool minimize)
1075 {
1076 CpObjectiveProto objective = new CpObjectiveProto();
1077 if (obj is null)
1078 {
1079 objective.Offset = 0L;
1080 objective.ScalingFactor = minimize ? 1L : -1;
1081 }
1082 else if (obj is IntVar intVar)
1083 {
1084 objective.Offset = 0L;
1085 objective.Vars.Capacity = 1;
1086 objective.Vars.Add(intVar.Index);
1087
1088 objective.Coeffs.Capacity = 1;
1089 if (minimize)
1090 {
1091 objective.Coeffs.Add(1L);
1092 objective.ScalingFactor = 1L;
1093 }
1094 else
1095 {
1096 objective.Coeffs.Add(-1L);
1097 objective.ScalingFactor = -1L;
1098 }
1099 }
1100 else
1101 {
1102 Dictionary<IntVar, long> dict = new Dictionary<IntVar, long>();
1103 long constant = LinearExpr.GetVarValueMap(obj, 1L, dict, terms_);
1104 if (minimize)
1105 {
1106 objective.ScalingFactor = 1L;
1107 objective.Offset = constant;
1108 }
1109 else
1110 {
1111 objective.ScalingFactor = -1L;
1112 objective.Offset = -constant;
1113 }
1114
1115 var dictCount = dict.Count;
1116 objective.Vars.Capacity = dictCount;
1117 objective.Coeffs.Capacity = dictCount;
1118 foreach (KeyValuePair<IntVar, long> it in dict)
1119 {
1120 objective.Vars.Add(it.Key.Index);
1121 objective.Coeffs.Add(minimize ? it.Value : -it.Value);
1122 }
1123 }
1124 model_.Objective = objective;
1125 }
1126
1128 public String ModelStats()
1129 {
1130 return CpSatHelper.ModelStats(model_);
1131 }
1132
1143 public Boolean ExportToFile(String file)
1144 {
1145 return CpSatHelper.WriteModelToFile(model_, file);
1146 }
1147
1153 public String Validate()
1154 {
1155 return CpSatHelper.ValidateModel(model_);
1156 }
1157
1158 private int ConvertConstant(long value)
1159 {
1160 if (constant_map_.TryGetValue(value, out var index))
1161 {
1162 return index;
1163 }
1164
1165 index = model_.Variables.Count;
1166 IntegerVariableProto var = new IntegerVariableProto();
1167 var.Domain.Capacity = 2;
1168 var.Domain.Add(value);
1169 var.Domain.Add(value);
1170 constant_map_.Add(value, index);
1171 model_.Variables.Add(var);
1172 return index;
1173 }
1174
1176 {
1177 if (typeof(X) == typeof(IntVar))
1178 {
1179 return (IntVar)(Object)x;
1180 }
1181 if (typeof(X) == typeof(long) || typeof(X) == typeof(int) || typeof(X) == typeof(short))
1182 {
1183 return LinearExpr.Constant(Convert.ToInt64(x));
1184 }
1185 if (typeof(X) == typeof(LinearExpr))
1186 {
1187 return (LinearExpr)(Object)x;
1188 }
1189 throw new ArgumentException("Cannot convert argument to LinearExpr");
1190 }
1191
1193 {
1194 Dictionary<IntVar, long> dict = new Dictionary<IntVar, long>();
1195 long constant = LinearExpr.GetVarValueMap(expr, 1L, dict, terms_);
1196 long mult = negate ? -1 : 1;
1197
1199 var dictCount = dict.Count;
1200 linear.Vars.Capacity = dictCount;
1201 linear.Coeffs.Capacity = dictCount;
1202 foreach (KeyValuePair<IntVar, long> term in dict)
1203 {
1204 linear.Vars.Add(term.Key.Index);
1205 linear.Coeffs.Add(term.Value * mult);
1206 }
1207 linear.Offset = constant * mult;
1208 return linear;
1209 }
1210
1211 private CpModelProto model_;
1212 private Dictionary<long, int> constant_map_;
1213 private BoolVar true_literal_;
1214 private Queue<Term> terms_;
1215}
1216
1217} // namespace Google.OrTools.Sat
All affine expressions must take different values.
Definition: CpModel.pb.cs:1171
pbc::RepeatedField< global::Google.OrTools.Sat.LinearExpressionProto > Exprs
Definition: CpModel.pb.cs:1218
This constraint forces a sequence of variables to be accepted by an automaton.
Definition: CpModel.pb.cs:4243
pbc::RepeatedField< int > Vars
The sequence of variables.
Definition: CpModel.pb.cs:4365
Specialized automaton constraint.
Definition: Constraints.cs:276
Argument of the constraints of the form OP(literals).
Definition: CpModel.pb.cs:514
pbc::RepeatedField< int > Literals
Definition: CpModel.pb.cs:561
Holds a Boolean variable.
Holds a linear constraint: expression ∈ domain
The circuit constraint is defined on a graph where the arc presence are controlled by literals.
Definition: CpModel.pb.cs:3203
Specialized circuit constraint.
Definition: Constraints.cs:85
pbc::RepeatedField< int > EnforcementLiteral
The constraint will be enforced iff all literals listed here are true.
Definition: CpModel.pb.cs:4923
global::Google.OrTools.Sat.LinearArgumentProto IntProd
The int_prod constraint forces the target to equal the product of all variables.
Definition: CpModel.pb.cs:5076
global::Google.OrTools.Sat.NoOverlap2DConstraintProto NoOverlap2D
The no_overlap_2d constraint prevents a set of boxes from overlapping.
Definition: CpModel.pb.cs:5284
global::Google.OrTools.Sat.BoolArgumentProto BoolOr
The bool_or constraint forces at least one literal to be true.
Definition: CpModel.pb.cs:4934
global::Google.OrTools.Sat.BoolArgumentProto BoolAnd
The bool_and constraint forces all of the literals to be true.
Definition: CpModel.pb.cs:4953
global::Google.OrTools.Sat.LinearArgumentProto IntDiv
The int_div constraint forces the target to equal exprs[0] / exprs[1].
Definition: CpModel.pb.cs:5036
global::Google.OrTools.Sat.CumulativeConstraintProto Cumulative
The cumulative constraint ensures that for any integer point, the sum of the demands of the intervals...
Definition: CpModel.pb.cs:5301
global::Google.OrTools.Sat.AutomatonConstraintProto Automaton
The automaton constraint forces a sequence of variables to be accepted by an automaton.
Definition: CpModel.pb.cs:5203
global::Google.OrTools.Sat.TableConstraintProto Table
The table constraint enforces what values a tuple of variables may take.
Definition: CpModel.pb.cs:5187
global::Google.OrTools.Sat.LinearArgumentProto LinMax
The lin_max constraint forces the target to equal the maximum of all linear expressions.
Definition: CpModel.pb.cs:5093
global::Google.OrTools.Sat.LinearConstraintProto Linear
The linear constraint enforces a linear inequality among the variables, such as 0 <= x + 2y <= 10.
Definition: CpModel.pb.cs:5109
global::Google.OrTools.Sat.NoOverlapConstraintProto NoOverlap
The no_overlap constraint prevents a set of intervals from overlapping; in scheduling,...
Definition: CpModel.pb.cs:5269
global::Google.OrTools.Sat.AllDifferentConstraintProto AllDiff
The all_diff constraint forces all variables to take different values.
Definition: CpModel.pb.cs:5124
global::Google.OrTools.Sat.CircuitConstraintProto Circuit
The circuit constraint takes a graph and forces the arcs present (with arc presence indicated by a li...
Definition: CpModel.pb.cs:5156
global::Google.OrTools.Sat.BoolArgumentProto AtMostOne
The at_most_one constraint enforces that no more than one literal is true at the same time.
Definition: CpModel.pb.cs:4978
global::Google.OrTools.Sat.ElementConstraintProto Element
The element constraint forces the variable with the given index to be equal to the target.
Definition: CpModel.pb.cs:5140
global::Google.OrTools.Sat.InverseConstraintProto Inverse
The inverse constraint forces two arrays to be inverses of each other: the values of one are the indi...
Definition: CpModel.pb.cs:5219
global::Google.OrTools.Sat.LinearArgumentProto IntMod
The int_mod constraint forces the target to equal exprs[0] % exprs[1].
Definition: CpModel.pb.cs:5053
global::Google.OrTools.Sat.BoolArgumentProto ExactlyOne
The exactly_one constraint force exactly one literal to true and no more.
Definition: CpModel.pb.cs:5004
global::Google.OrTools.Sat.ReservoirConstraintProto Reservoir
The reservoir constraint forces the sum of a set of active demands to always be between a specified m...
Definition: CpModel.pb.cs:5236
global::Google.OrTools.Sat.BoolArgumentProto BoolXor
The bool_xor constraint forces an odd number of the literals to be true.
Definition: CpModel.pb.cs:5019
global::Google.OrTools.Sat.RoutesConstraintProto Routes
The routes constraint implements the vehicle routing problem.
Definition: CpModel.pb.cs:5171
Wrapper around a ConstraintProto.
Definition: Constraints.cs:29
void OnlyEnforceIf(ILiteral lit)
Adds a literal to the constraint.
Definition: Constraints.cs:38
ConstraintProto Proto
The underlying constraint proto.
Definition: Constraints.cs:62
A constraint programming problem.
Definition: CpModel.pb.cs:8643
pbc::RepeatedField< global::Google.OrTools.Sat.DecisionStrategyProto > SearchStrategy
Defines the strategy that the solver should follow when the search_branching parameter is set to FIXE...
Definition: CpModel.pb.cs:8791
global::Google.OrTools.Sat.PartialVariableAssignment SolutionHint
Solution hint.
Definition: CpModel.pb.cs:8813
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
pbc::RepeatedField< global::Google.OrTools.Sat.ConstraintProto > Constraints
Definition: CpModel.pb.cs:8727
pbc::RepeatedField< int > Assumptions
A list of literals.
Definition: CpModel.pb.cs:8842
global::Google.OrTools.Sat.CpObjectiveProto Objective
The objective to minimize.
Definition: CpModel.pb.cs:8739
Wrapper class around the cp_model proto.
Definition: CpModel.cs:24
void AddAssumption(ILiteral lit)
Adds a literal to the model as assumption.
Definition: CpModel.cs:1052
Constraint AddInverse(IEnumerable< IntVar > direct, IEnumerable< IntVar > reverse)
Adds Inverse(variables, inverseVariables).
Definition: CpModel.cs:447
ILiteral FalseLiteral()
Returns a constant false literal.
Definition: CpModel.cs:117
Constraint AddLinearExpressionInDomain(LinearExpr expr, Domain domain)
Adds expr ∈ domain.
Definition: CpModel.cs:159
IntervalVar NewOptionalFixedSizeIntervalVar< S >(S start, long size, ILiteral is_present, string name)
Creates an optional interval variable from an affine expression start, a fixed size,...
Definition: CpModel.cs:905
IntervalVar NewIntervalVar< S, D, E >(S start, D size, E end, string name)
Creates an interval variable from three affine expressions start, size, and end.
Definition: CpModel.cs:816
TableConstraint AddAllowedAssignments(IEnumerable< IntVar > vars)
Adds AllowedAssignments(variables).
Definition: CpModel.cs:348
void Minimize(LinearExpr obj)
Adds a minimization objective of a linear expression.
Definition: CpModel.cs:996
Constraint AddBoolXor(IEnumerable< ILiteral > literals)
Adds XOr(literals) == true.
Definition: CpModel.cs:653
void AddMapDomain(IntVar var, IEnumerable< IntVar > bool_vars, long offset=0)
Definition: CpModel.cs:505
void ClearAssumptions()
Clears all assumptions from the model.
Definition: CpModel.cs:1067
Constraint AddAtLeastOne(IEnumerable< ILiteral > literals)
Same as AddBoolOr: ∑(literals) ≥ 1.
Definition: CpModel.cs:586
IntervalVar NewFixedSizeIntervalVar< S >(S start, long size, string name)
Creates an interval variable from an affine expression start, and a fixed size.
Definition: CpModel.cs:844
void AddDecisionStrategy(IEnumerable< IntVar > vars, DecisionStrategyProto.Types.VariableSelectionStrategy var_str, DecisionStrategyProto.Types.DomainReductionStrategy dom_str)
Adds DecisionStrategy(variables, var_str, dom_str).
Definition: CpModel.cs:1022
ILiteral TrueLiteral()
Returns a constant true literal.
Definition: CpModel.cs:107
IntervalVar NewOptionalIntervalVar< S, D, E >(S start, D size, E end, ILiteral is_present, string name)
Creates an optional interval variable from three affine expressions start, size, and end,...
Definition: CpModel.cs:874
Boolean ExportToFile(String file)
Write the model as a protocol buffer to file.
Definition: CpModel.cs:1143
AutomatonConstraint AddAutomaton(IEnumerable< IntVar > vars, long starting_state, IEnumerable< long > final_states)
Adds an automaton constraint.
Definition: CpModel.cs:416
Constraint AddMaxEquality(LinearExpr target, IEnumerable< LinearExpr > exprs)
Adds target == Max(exprs).
Definition: CpModel.cs:692
Constraint AddDivisionEquality< T, N, D >(T target, N num, D denom)
Adds target == num / denom (integer division rounded towards 0).
Definition: CpModel.cs:712
void ClearHints()
Clears all hinting from the model.
Definition: CpModel.cs:1046
ReservoirConstraint AddReservoirConstraint(long minLevel, long maxLevel)
Adds a reservoir constraint with optional refill/emptying events.
Definition: CpModel.cs:493
Constraint AddExactlyOne(IEnumerable< ILiteral > literals)
Adds ExactlyOne(literals): ∑(literals) == 1.
Definition: CpModel.cs:615
Constraint AddNoOverlap(IEnumerable< IntervalVar > intervals)
Adds NoOverlap(intervalVars).
Definition: CpModel.cs:926
BoolVar NewBoolVar(string name)
Creates an Boolean variable with given domain.
Definition: CpModel.cs:97
CircuitConstraint AddCircuit()
Adds and returns an empty circuit constraint.
Definition: CpModel.cs:308
LinearExpressionProto GetLinearExpressionProto(LinearExpr expr, bool negate=false)
Definition: CpModel.cs:1192
IntVar NewConstant(long value)
Creates a constant variable.
Definition: CpModel.cs:87
void AddHint(IntVar var, long value)
Adds variable hinting to the model.
Definition: CpModel.cs:1038
Constraint AddElement(IntVar index, IEnumerable< IntVar > vars, IntVar target)
Adds the element constraint: variables[index] == target.
Definition: CpModel.cs:240
Constraint AddMinEquality(LinearExpr target, IEnumerable< LinearExpr > exprs)
Adds target == Min(exprs).
Definition: CpModel.cs:672
Constraint AddAtMostOne(IEnumerable< ILiteral > literals)
Adds AtMostOne(literals): ∑(literals) ≤ 1.
Definition: CpModel.cs:596
Constraint AddMultiplicationEquality(LinearExpr target, IEnumerable< LinearExpr > exprs)
Adds target == ∏(exprs).
Definition: CpModel.cs:766
void Maximize(LinearExpr obj)
Adds a maximization objective of a linear expression.
Definition: CpModel.cs:1002
Constraint AddImplication(ILiteral a, ILiteral b)
Adds a ⇒ b.
Definition: CpModel.cs:550
NoOverlap2dConstraint AddNoOverlap2D()
Adds NoOverlap2D().
Definition: CpModel.cs:956
IntVar NewIntVar(long lb, long ub, string name)
Creates an integer variable with domain [lb, ub].
Definition: CpModel.cs:63
Constraint AddBoolAnd(IEnumerable< ILiteral > literals)
Adds And(literals) == true.
Definition: CpModel.cs:634
Constraint AddBoolOr(IEnumerable< ILiteral > literals)
Adds Or(literals) == true.
Definition: CpModel.cs:567
Constraint AddModuloEquality< T, V, M >(T target, V v, M m)
Adds target == v % m.
Definition: CpModel.cs:748
Constraint AddElement(IntVar index, IEnumerable< long > values, IntVar target)
Adds the element constraint: values[index] == target.
Definition: CpModel.cs:261
CumulativeConstraint AddCumulative< C >(C capacity)
Adds Cumulative(capacity).
Definition: CpModel.cs:982
String ModelStats()
Returns some statistics on model as a string.
Definition: CpModel.cs:1128
MultipleCircuitConstraint AddMultipleCircuit()
Adds and returns an empty multiple circuit constraint.
Definition: CpModel.cs:326
IntVar NewIntVarFromDomain(Domain domain, string name)
Creates an integer variable with given domain.
Definition: CpModel.cs:77
LinearExpr GetLinearExpr< X >(X x)
Definition: CpModel.cs:1175
Constraint AddMultiplicationEquality(LinearExpr target, LinearExpr left, LinearExpr right)
Adds target == left * right.
Definition: CpModel.cs:786
Constraint AddElement(IntVar index, IEnumerable< int > values, IntVar target)
Adds the element constraint: values[index] == target.
Definition: CpModel.cs:282
CpModelProto Model
The underlying CpModelProto.
Definition: CpModel.cs:40
Constraint AddAllDifferent(IEnumerable< LinearExpr > exprs)
Adds the constraint AllDifferent(exprs).
Definition: CpModel.cs:221
String Validate()
Returns a non empty string explaining the issue if the model is invalid.
Definition: CpModel.cs:1153
Constraint Add(BoundedLinearExpression lin)
Adds a linear constraint to the model.
Definition: CpModel.cs:193
Constraint AddAbsEquality(LinearExpr target, LinearExpr expr)
Adds target == abs(expr).
Definition: CpModel.cs:730
Constraint AddLinearConstraint(LinearExpr expr, long lb, long ub)
Adds lb ≤ expr ≤ ub.
Definition: CpModel.cs:142
void AddAssumptions(IEnumerable< ILiteral > literals)
Adds multiple literals to the model as assumptions.
Definition: CpModel.cs:1058
TableConstraint AddForbiddenAssignments(IEnumerable< IntVar > vars)
Adds ForbiddenAssignments(variables).
Definition: CpModel.cs:376
double Offset
The displayed objective is always: scaling_factor * (sum(coefficients[i] * objective_vars[i]) + offse...
Definition: CpModel.pb.cs:6467
pbc::RepeatedField< int > Vars
The linear terms of the objective to minimize.
Definition: CpModel.pb.cs:6438
pbc::RepeatedField< long > Coeffs
Definition: CpModel.pb.cs:6449
static string ValidateModel(Google.OrTools.Sat.CpModelProto model_proto)
Definition: CpSatHelper.cs:59
static bool WriteModelToFile(Google.OrTools.Sat.CpModelProto model_proto, string filename)
Definition: CpSatHelper.cs:69
static string ModelStats(Google.OrTools.Sat.CpModelProto model_proto)
Definition: CpSatHelper.cs:49
The sum of the demands of the intervals at each interval point cannot exceed a capacity.
Definition: CpModel.pb.cs:2606
global::Google.OrTools.Sat.LinearExpressionProto Capacity
Definition: CpModel.pb.cs:2653
Specialized cumulative constraint.
Definition: Constraints.cs:365
Container for nested types declared in the DecisionStrategyProto message type.
Definition: CpModel.pb.cs:7412
VariableSelectionStrategy
The order in which the variables above should be considered.
Definition: CpModel.pb.cs:7419
DomainReductionStrategy
Once a variable has been chosen, this enum describe what decision is taken on its domain.
Definition: CpModel.pb.cs:7433
Define the strategy to follow when the solver needs to take a new decision.
Definition: CpModel.pb.cs:7129
pbc::RepeatedField< int > Variables
The variables to be considered for the next decision.
Definition: CpModel.pb.cs:7184
The constraint target = vars[index].
Definition: CpModel.pb.cs:1600
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.
The two arrays of variable each represent a function, the second is the inverse of the first: f_direc...
Definition: CpModel.pb.cs:4031
pbc::RepeatedField< int > FDirect
Definition: CpModel.pb.cs:4079
pbc::RepeatedField< global::Google.OrTools.Sat.LinearExpressionProto > Exprs
Definition: CpModel.pb.cs:1004
global::Google.OrTools.Sat.LinearExpressionProto Target
Definition: CpModel.pb.cs:990
The linear sum vars[i] * coeffs[i] must fall in the given domain.
Definition: CpModel.pb.cs:1357
pbc::RepeatedField< int > Vars
Definition: CpModel.pb.cs:1406
pbc::RepeatedField< long > Coeffs
Same size as vars.
Definition: CpModel.pb.cs:1420
pbc::RepeatedField< long > Domain
Definition: CpModel.pb.cs:1431
Holds a linear expression: sum (ai * xi) + b.
static LinearExpr Constant(long value)
Creates a constant expression.
static LinearExpr Sum(IEnumerable< LinearExpr > exprs)
Creates Sum(exprs).
Some constraints supports linear expression instead of just using a reference to a variable.
Definition: CpModel.pb.cs:699
pbc::RepeatedField< int > Vars
Definition: CpModel.pb.cs:748
pbc::RepeatedField< long > Coeffs
Definition: CpModel.pb.cs:759
Specialized multiple circuit constraint.
Definition: Constraints.cs:119
The boxes defined by [start_x, end_x) * [start_y, end_y) cannot overlap.
Definition: CpModel.pb.cs:2349
Specialized NoOverlap2D constraint.
Definition: Constraints.cs:407
All the intervals (index of IntervalConstraintProto) must be disjoint.
Definition: CpModel.pb.cs:2164
This message encodes a partial (or full) assignment of the variables of a CpModelProto.
Definition: CpModel.pb.cs:7724
Maintain a reservoir level within bounds.
Definition: CpModel.pb.cs:2878
Specialized reservoir constraint.
Definition: Constraints.cs:307
The "VRP" (Vehicle Routing Problem) constraint.
Definition: CpModel.pb.cs:3461
The values of the n-tuple formed by the given variables can only be one of the listed n-tuples in val...
Definition: CpModel.pb.cs:3778
pbc::RepeatedField< int > Vars
Definition: CpModel.pb.cs:3827
bool Negated
If true, the meaning is "negated", that is we forbid any of the given tuple from a feasible assignmen...
Definition: CpModel.pb.cs:3851
Specialized assignment constraint.
Definition: Constraints.cs:152
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.