Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
Constraints.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
15{
16using System;
17using System.Collections.Generic;
18using System.Linq;
19
28public class Constraint
29{
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
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 {
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 {
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 {
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 {
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 {
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 {
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 {
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 {
326 res.TimeExprs.Add(cp_model_.GetLinearExpressionProto(cp_model_.GetLinearExpr(time)));
327 res.LevelChanges.Add(cp_model_.GetLinearExpressionProto(cp_model_.GetLinearExpr(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 {
345 res.TimeExprs.Add(cp_model_.GetLinearExpressionProto(cp_model_.GetLinearExpr(time)));
346 res.LevelChanges.Add(cp_model_.GetLinearExpressionProto(cp_model_.GetLinearExpr(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 {
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 {
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
This constraint forces a sequence of variables to be accepted by an automaton.
pbc::RepeatedField< long > TransitionTail
List of transitions (all 3 vectors have the same size). Both tail and head are states,...
pbc::RepeatedField< long > TransitionHead
pbc::RepeatedField< long > TransitionLabel
Specialized automaton constraint.
AutomatonConstraint AddTransition(int tail, int head, long label)
The circuit constraint is defined on a graph where the arc presence are controlled by literals....
Specialized circuit constraint.
CircuitConstraint(CpModelProto model)
CircuitConstraint AddArc(int tail, int head, ILiteral literal)
Add an arc to the graph of the circuit constraint.
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...
global::Google.OrTools.Sat.TableConstraintProto Table
The table constraint enforces what values a tuple of variables may take.
global::Google.OrTools.Sat.AutomatonConstraintProto Automaton
The automaton constraint forces a sequence of variables to be accepted by an automaton.
global::Google.OrTools.Sat.NoOverlap2DConstraintProto NoOverlap2D
The no_overlap_2d constraint prevents a set of boxes from overlapping.
global::Google.OrTools.Sat.CumulativeConstraintProto Cumulative
The cumulative constraint ensures that for any integer point, the sum of the demands of the intervals...
global::Google.OrTools.Sat.RoutesConstraintProto Routes
The routes constraint implements the vehicle routing problem.
pbc::RepeatedField< int > EnforcementLiteral
The constraint will be enforced iff all literals listed here are true. If this is empty,...
global::Google.OrTools.Sat.CircuitConstraintProto Circuit
The circuit constraint takes a graph and forces the arcs present (with arc presence indicated by a li...
Wrapper around a ConstraintProto.
Constraint(CpModelProto model)
int Index
The index of the constraint in the model.
void OnlyEnforceIf(ILiteral lit)
Adds a literal to the constraint.
void OnlyEnforceIf(ILiteral[] lits)
Adds a list of literals to the constraint.
ConstraintProto Proto
The underlying constraint proto.
A constraint programming problem.
pbc::RepeatedField< global::Google.OrTools.Sat.ConstraintProto > Constraints
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
The sum of the demands of the intervals at each interval point cannot exceed a capacity....
pbc::RepeatedField< global::Google.OrTools.Sat.LinearExpressionProto > Demands
Same size as intervals.
Specialized cumulative constraint.
CumulativeConstraint AddDemand< D >(IntervalVar interval, D demand)
Adds a pair (interval, demand) to the constraint.
CumulativeConstraint AddDemands< D >(IEnumerable< IntervalVar > intervals, IEnumerable< D > demands)
Adds all pairs (interval, demand) to the constraint.
CumulativeConstraint(CpModel cp_model, CpModelProto model)
int GetIndex()
The Index of the interval in the model proto.
Holds a linear expression: sum (ai * xi) + b.
Specialized multiple circuit constraint.
MultipleCircuitConstraint AddArc(int tail, int head, ILiteral literal)
Add an arc to the graph of the multiple circuit constraint.
pbc::RepeatedField< int > YIntervals
Same size as x_intervals.
Specialized NoOverlap2D constraint.
NoOverlap2dConstraint AddRectangle(IntervalVar xInterval, IntervalVar yInterval)
Adds a rectangle (xInterval, yInterval) to the constraint.
Maintain a reservoir level within bounds. The water level starts at 0, and at any time,...
pbc::RepeatedField< global::Google.OrTools.Sat.LinearExpressionProto > TimeExprs
affine expressions.
pbc::RepeatedField< global::Google.OrTools.Sat.LinearExpressionProto > LevelChanges
Currently, we only support constant level changes.
pbc::RepeatedField< int > ActiveLiterals
Specialized reservoir constraint.
ReservoirConstraint(CpModel cp_model, CpModelProto model)
ReservoirConstraint AddOptionalEvent< T, L >(T time, L level_change, ILiteral literal)
Adds an optional event.
ReservoirConstraint AddEvent< T, L >(T time, L level_change)
Adds a mandatory event.
The "VRP" (Vehicle Routing Problem) constraint.
pbc::RepeatedField< int > Literals
The values of the n-tuple formed by the given variables can only be one of the listed n-tuples in val...
pbc::RepeatedField< long > Values
Specialized assignment constraint.
TableConstraint AddTuple(IEnumerable< long > tuple)
Adds a tuple of possible/forbidden values to the constraint.
TableConstraint AddTuples(int[,] tuples)
Adds a set of tuples of possible/forbidden values to the constraint.
TableConstraint AddTuples(long[,] tuples)
Adds a set of tuples of possible/forbidden values to the constraint.
TableConstraint AddTuple(IEnumerable< int > tuple)
Adds a tuple of possible/forbidden values to the constraint.
TableConstraint(CpModelProto model)
Holds a Boolean variable or its negation.
int GetIndex()
Returns the logical index of the literal.