DotNet Reference

.Net Reference

Constraints.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
15{
16using System;
17using System.Collections.Generic;
18using System.Linq;
19
28public class Constraint
29{
30 public Constraint(CpModelProto model)
31 {
32 index_ = model.Constraints.Count;
33 constraint_ = new ConstraintProto();
34 model.Constraints.Add(constraint_);
35 }
36
38 public void OnlyEnforceIf(ILiteral lit)
39 {
40 constraint_.EnforcementLiteral.Add(lit.GetIndex());
41 }
42
44 public void OnlyEnforceIf(ILiteral[] lits)
45 {
46 foreach (ILiteral lit in lits)
47 {
48 constraint_.EnforcementLiteral.Add(lit.GetIndex());
49 }
50 }
51
53 public int Index
54 {
55 get {
56 return index_;
57 }
58 }
59
61 public ConstraintProto Proto
62 {
63 get {
64 return constraint_;
65 }
66 set {
67 constraint_ = value;
68 }
69 }
70
71 private int index_;
72 private ConstraintProto constraint_;
73}
74
85{
86 public CircuitConstraint(CpModelProto model) : base(model)
87 {
88 }
89
99 public CircuitConstraint AddArc(int tail, int head, ILiteral literal)
100 {
101 CircuitConstraintProto circuit = Proto.Circuit;
102 circuit.Tails.Add(tail);
103 circuit.Heads.Add(head);
104 circuit.Literals.Add(literal.GetIndex());
105 return this;
106 }
107}
108
119{
120 public MultipleCircuitConstraint(CpModelProto model) : base(model)
121 {
122 }
123
133 public MultipleCircuitConstraint AddArc(int tail, int head, ILiteral literal)
134 {
135 RoutesConstraintProto routes = Proto.Routes;
136 routes.Tails.Add(tail);
137 routes.Heads.Add(head);
138 routes.Literals.Add(literal.GetIndex());
139 return this;
140 }
141}
142
152{
153 public TableConstraint(CpModelProto model) : base(model)
154 {
155 }
156
166 public TableConstraint AddTuple(IEnumerable<int> tuple)
167 {
168 TableConstraintProto table = Proto.Table;
169
170 int count = 0;
171 foreach (int value in tuple)
172 {
173 table.Values.Add(value);
174 count++;
175 }
176 if (count != table.Vars.Count)
177 {
178 throw new ArgumentException("addTuple", "tuple does not have the same length as the variables");
179 }
180 return this;
181 }
182
192 public TableConstraint AddTuple(IEnumerable<long> tuple)
193 {
194 TableConstraintProto table = Proto.Table;
195
196 int count = 0;
197 foreach (long value in tuple)
198 {
199 table.Values.Add(value);
200 count++;
201 }
202 if (count != table.Vars.Count)
203 {
204 throw new ArgumentException("addTuple", "tuple does not have the same length as the variables");
205 }
206 return this;
207 }
208
218 public TableConstraint AddTuples(int[,] tuples)
219 {
220 TableConstraintProto table = Proto.Table;
221
222 if (tuples.GetLength(1) != table.Vars.Count)
223 {
224 throw new ArgumentException("addTuples", "tuples does not have the same length as the variables");
225 }
226
227 for (int i = 0; i < tuples.GetLength(0); ++i)
228 {
229 for (int j = 0; j < tuples.GetLength(1); ++j)
230 {
231 table.Values.Add(tuples[i, j]);
232 }
233 }
234 return this;
235 }
236
246 public TableConstraint AddTuples(long[,] tuples)
247 {
248 TableConstraintProto table = Proto.Table;
249
250 if (tuples.GetLength(1) != table.Vars.Count)
251 {
252 throw new ArgumentException("addTuples", "tuples does not have the same length as the variables");
253 }
254
255 for (int i = 0; i < tuples.GetLength(0); ++i)
256 {
257 for (int j = 0; j < tuples.GetLength(1); ++j)
258 {
259 table.Values.Add(tuples[i, j]);
260 }
261 }
262 return this;
263 }
264}
265
276{
277 public AutomatonConstraint(CpModelProto model) : base(model)
278 {
279 }
280
281 /*
282 * <summary>
283 * Adds a transitions to the automaton.
284 * </summary>
285 */
286 public AutomatonConstraint AddTransition(int tail, int head, long label)
287 {
288 AutomatonConstraintProto aut = Proto.Automaton;
289 aut.TransitionTail.Add(tail);
290 aut.TransitionLabel.Add(label);
291 aut.TransitionHead.Add(head);
292 return this;
293 }
294}
295
307{
308 public ReservoirConstraint(CpModel cp_model, CpModelProto model) : base(model)
309 {
310 this.cp_model_ = cp_model;
311 }
312
323 public ReservoirConstraint AddEvent<T, L>(T time, L level_change)
324 {
325 ReservoirConstraintProto res = Proto.Reservoir;
326 res.TimeExprs.Add(cp_model_.GetLinearExpressionProto(cp_model_.GetLinearExpr(time)));
327 res.LevelChanges.Add(Convert.ToInt64(level_change));
328 res.ActiveLiterals.Add(cp_model_.TrueLiteral().GetIndex());
329 return this;
330 }
331
342 public ReservoirConstraint AddOptionalEvent<T, L>(T time, L level_change, ILiteral literal)
343 {
344 ReservoirConstraintProto res = Proto.Reservoir;
345 res.TimeExprs.Add(cp_model_.GetLinearExpressionProto(cp_model_.GetLinearExpr(time)));
346 res.LevelChanges.Add(Convert.ToInt64(level_change));
347 res.ActiveLiterals.Add(literal.GetIndex());
348 return this;
349 }
350
351 private CpModel cp_model_;
352}
353
365{
366 public CumulativeConstraint(CpModel cp_model, CpModelProto model) : base(model)
367 {
368 this.cp_model_ = cp_model;
369 }
370
373 {
374 CumulativeConstraintProto cumul = Proto.Cumulative;
375 cumul.Intervals.Add(interval.GetIndex());
376 LinearExpr demandExpr = cp_model_.GetLinearExpr(demand);
377 cumul.Demands.Add(cp_model_.GetLinearExpressionProto(demandExpr));
378 return this;
379 }
380
382 public CumulativeConstraint AddDemands<D>(IEnumerable<IntervalVar> intervals, IEnumerable<D> demands)
383 {
384 CumulativeConstraintProto cumul = Proto.Cumulative;
385 foreach (var p in intervals.Zip(demands, (i, d) => new { Interval = i, Demand = d }))
386 {
387 cumul.Intervals.Add(p.Interval.GetIndex());
388 LinearExpr demandExpr = cp_model_.GetLinearExpr(p.Demand);
389 cumul.Demands.Add(cp_model_.GetLinearExpressionProto(demandExpr));
390 }
391 return this;
392 }
393
394 private CpModel cp_model_;
395}
396
407{
408 public NoOverlap2dConstraint(CpModelProto model) : base(model)
409 {
410 }
411
414 {
415 Proto.NoOverlap2D.XIntervals.Add(xInterval.GetIndex());
416 Proto.NoOverlap2D.YIntervals.Add(yInterval.GetIndex());
417 return this;
418 }
419}
420
421} // namespace Google.OrTools.Sat
Specialized automaton constraint.
Definition: Constraints.cs:276
AutomatonConstraint(CpModelProto model)
Definition: Constraints.cs:277
AutomatonConstraint AddTransition(int tail, int head, long label)
Definition: Constraints.cs:286
Specialized circuit constraint.
Definition: Constraints.cs:85
CircuitConstraint AddArc(int tail, int head, ILiteral literal)
Add an arc to the graph of the circuit constraint.
Definition: Constraints.cs:99
CircuitConstraint(CpModelProto model)
Definition: Constraints.cs:86
Wrapper around a ConstraintProto.
Definition: Constraints.cs:29
int Index
The index of the constraint in the model.
Definition: Constraints.cs:54
void OnlyEnforceIf(ILiteral[] lits)
Adds a list of literals to the constraint.
Definition: Constraints.cs:44
void OnlyEnforceIf(ILiteral lit)
Adds a literal to the constraint.
Definition: Constraints.cs:38
ConstraintProto Proto
The underlying constraint proto.
Definition: Constraints.cs:62
Constraint(CpModelProto model)
Definition: Constraints.cs:30
Wrapper class around the cp_model proto.
Definition: CpModel.cs:24
ILiteral TrueLiteral()
Returns a constant true literal.
Definition: CpModel.cs:108
Constraint Add(BoundedLinearExpression lin)
Adds a linear constraint to the model.
Definition: CpModel.cs:192
Specialized cumulative constraint.
Definition: Constraints.cs:365
CumulativeConstraint(CpModel cp_model, CpModelProto model)
Definition: Constraints.cs:366
CumulativeConstraint AddDemand< D >(IntervalVar interval, D demand)
Adds a pair (interval, demand) to the constraint.
Definition: Constraints.cs:372
CumulativeConstraint AddDemands< D >(IEnumerable< IntervalVar > intervals, IEnumerable< D > demands)
Adds all pairs (interval, demand) to the constraint.
Definition: Constraints.cs:382
int GetIndex()
The Index of the interval in the model proto
Holds a linear expression: sum (ai * xi) + b.
Specialized multiple circuit constraint.
Definition: Constraints.cs:119
MultipleCircuitConstraint AddArc(int tail, int head, ILiteral literal)
Add an arc to the graph of the multiple circuit constraint.
Definition: Constraints.cs:133
Specialized NoOverlap2D constraint.
Definition: Constraints.cs:407
NoOverlap2dConstraint AddRectangle(IntervalVar xInterval, IntervalVar yInterval)
Adds a rectangle (xInterval, yInterval) to the constraint.
Definition: Constraints.cs:413
Specialized reservoir constraint.
Definition: Constraints.cs:307
ReservoirConstraint(CpModel cp_model, CpModelProto model)
Definition: Constraints.cs:308
ReservoirConstraint AddEvent< T, L >(T time, L level_change)
Adds a mandatory event.
Definition: Constraints.cs:323
ReservoirConstraint AddOptionalEvent< T, L >(T time, L level_change, ILiteral literal)
Adds an optional event.
Definition: Constraints.cs:342
Specialized assignment constraint.
Definition: Constraints.cs:152
TableConstraint AddTuples(int[,] tuples)
Adds a set of tuples of possible/forbidden values to the constraint.
Definition: Constraints.cs:218
TableConstraint AddTuple(IEnumerable< long > tuple)
Adds a tuple of possible/forbidden values to the constraint.
Definition: Constraints.cs:192
TableConstraint(CpModelProto model)
Definition: Constraints.cs:153
TableConstraint AddTuple(IEnumerable< int > tuple)
Adds a tuple of possible/forbidden values to the constraint.
Definition: Constraints.cs:166
TableConstraint AddTuples(long[,] tuples)
Adds a set of tuples of possible/forbidden values to the constraint.
Definition: Constraints.cs:246
Holds a Boolean variable or its negation.
int GetIndex()
Returns the logical index of the literal.