Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ValCstPair.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;
18
19public interface IConstraintWithStatus
20{
23}
24
25public abstract class BaseEquality : IConstraintWithStatus
26{
27 abstract public Solver solver();
28 abstract public IntVar Var();
29
31 {
32 return a.solver().MakeSum(a.Var(), b.Var());
33 }
34 public static IntExpr operator +(BaseEquality a, long v)
35 {
36 return a.solver().MakeSum(a.Var(), v);
37 }
38 public static IntExpr operator +(long v, BaseEquality a)
39 {
40 return a.solver().MakeSum(a.Var(), v);
41 }
43 {
44 return a.solver().MakeDifference(a.Var(), b.Var());
45 }
46 public static IntExpr operator -(BaseEquality a, long v)
47 {
48 return a.solver().MakeSum(a.Var(), -v);
49 }
50 public static IntExpr operator -(long v, BaseEquality a)
51 {
52 return a.solver().MakeDifference(v, a.Var());
53 }
55 {
56 return a.solver().MakeProd(a.Var(), b.Var());
57 }
58 public static IntExpr operator *(BaseEquality a, long v)
59 {
60 return a.solver().MakeProd(a.Var(), v);
61 }
62 public static IntExpr operator *(long v, BaseEquality a)
63 {
64 return a.solver().MakeProd(a.Var(), v);
65 }
66 public static IntExpr operator /(BaseEquality a, long v)
67 {
68 return a.solver().MakeDiv(a.Var(), v);
69 }
71 {
72 return a.solver().MakeOpposite(a.Var());
73 }
74 public IntExpr Abs()
75 {
76 return this.solver().MakeAbs(this.Var());
77 }
78 public IntExpr Square()
79 {
80 return this.solver().MakeSquare(this.Var());
81 }
83 {
84 return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v));
85 }
87 {
88 return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v));
89 }
91 {
92 return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v));
93 }
95 {
96 return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v));
97 }
99 {
100 return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v));
101 }
103 {
104 return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v));
105 }
106 public static WrappedConstraint operator>(BaseEquality a, long v)
107 {
108 return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v));
109 }
110 public static WrappedConstraint operator>(long v, BaseEquality a)
111 {
112 return new WrappedConstraint(a.solver().MakeLess(a.Var(), v));
113 }
115 {
116 return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v));
117 }
119 {
120 return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v));
121 }
122 public static WrappedConstraint operator<(BaseEquality a, long v)
123 {
124 return new WrappedConstraint(a.solver().MakeLess(a.Var(), v));
125 }
126 public static WrappedConstraint operator<(long v, BaseEquality a)
127 {
128 return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v));
129 }
131 {
132 return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var()));
133 }
135 {
136 return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var()));
137 }
139 {
140 return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var()));
141 }
143 {
144 return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var()));
145 }
147 {
148 return new ConstraintEquality(a, b, true);
149 }
151 {
152 return new ConstraintEquality(a, b, false);
153 }
154}
155
157{
158 public bool Val { get; set; }
159
160 public Constraint Cst { get; set; }
161
162 public WrappedConstraint(Constraint cst) : this(true, cst)
163 {
164 }
165
166 public WrappedConstraint(bool val) : this(val, null)
167 {
168 }
169
170 public WrappedConstraint(bool val, Constraint cst)
171 {
172 this.Val = val;
173 this.Cst = cst;
174 }
175
176 public static implicit operator bool(WrappedConstraint valCstPair)
177 {
178 return valCstPair.Val;
179 }
180
181 public static implicit operator Constraint(WrappedConstraint valCstPair)
182 {
183 return valCstPair.Cst;
184 }
185
186 public static implicit operator IntVar(WrappedConstraint eq)
187 {
188 return eq.Var();
189 }
190
191 public static implicit operator IntExpr(WrappedConstraint eq)
192 {
193 return eq.Var();
194 }
195
196 public override Solver solver()
197 {
198 return this.Cst.solver();
199 }
200
201 public override IntVar Var()
202 {
203 return Cst.Var();
204 }
205}
206
208{
209 public IntExprEquality(IntExpr a, IntExpr b, bool equality)
210 {
211 this.left_ = a;
212 this.right_ = b;
213 this.equality_ = equality;
214 }
215
216 bool IsTrue()
217 {
218 return (object)left_ == (object)right_ ? equality_ : !equality_;
219 }
220
221 Constraint ToConstraint()
222 {
223 return equality_ ? left_.solver().MakeEquality(left_.Var(), right_.Var())
224 : left_.solver().MakeNonEquality(left_.Var(), right_.Var());
225 }
226
227 public static bool operator true(IntExprEquality eq)
228 {
229 return eq.IsTrue();
230 }
231
232 public static bool operator false(IntExprEquality eq)
233 {
234 return !eq.IsTrue();
235 }
236
237 public static implicit operator Constraint(IntExprEquality eq)
238 {
239 return eq.ToConstraint();
240 }
241
242 public override IntVar Var()
243 {
244 return equality_ ? left_.solver().MakeIsEqualVar(left_, right_)
245 : left_.solver().MakeIsDifferentVar(left_, right_);
246 }
247
248 public static implicit operator IntVar(IntExprEquality eq)
249 {
250 return eq.Var();
251 }
252
253 public static implicit operator IntExpr(IntExprEquality eq)
254 {
255 return eq.Var();
256 }
257
258 public override Solver solver()
259 {
260 return left_.solver();
261 }
262
263 private IntExpr left_;
264 private IntExpr right_;
265 private bool equality_;
266}
267
269{
271 {
272 this.left_ = a;
273 this.right_ = b;
274 this.equality_ = equality;
275 }
276
277 bool IsTrue()
278 {
279 return (object)left_ == (object)right_ ? equality_ : !equality_;
280 }
281
282 Constraint ToConstraint()
283 {
284 return equality_ ? left_.solver().MakeEquality(left_.Var(), right_.Var())
285 : left_.solver().MakeNonEquality(left_.Var(), right_.Var());
286 }
287
288 public static bool operator true(ConstraintEquality eq)
289 {
290 return eq.IsTrue();
291 }
292
293 public static bool operator false(ConstraintEquality eq)
294 {
295 return !eq.IsTrue();
296 }
297
298 public static implicit operator Constraint(ConstraintEquality eq)
299 {
300 return eq.ToConstraint();
301 }
302
303 public override IntVar Var()
304 {
305 return equality_ ? left_.solver().MakeIsEqualVar(left_.Var(), right_.Var())
306 : left_.solver().MakeIsDifferentVar(left_.Var(), right_.Var());
307 }
308
309 public static implicit operator IntVar(ConstraintEquality eq)
310 {
311 return eq.Var();
312 }
313
314 public static implicit operator IntExpr(ConstraintEquality eq)
315 {
316 return eq.Var();
317 }
318
319 public override Solver solver()
320 {
321 return left_.solver();
322 }
323
324 private IConstraintWithStatus left_;
325 private IConstraintWithStatus right_;
326 private bool equality_;
327}
328} // namespace Google.OrTools.ConstraintSolver
static WrappedConstraint operator>(BaseEquality a, long v)
static WrappedConstraint operator>=(BaseEquality a, long v)
Definition ValCstPair.cs:98
static WrappedConstraint operator==(BaseEquality a, long v)
Definition ValCstPair.cs:82
static IntExpr operator*(BaseEquality a, BaseEquality b)
Definition ValCstPair.cs:54
static WrappedConstraint operator!=(BaseEquality a, long v)
Definition ValCstPair.cs:90
static IntExpr operator/(BaseEquality a, long v)
Definition ValCstPair.cs:66
static WrappedConstraint operator>(BaseEquality a, BaseEquality b)
static WrappedConstraint operator<(BaseEquality a, long v)
static WrappedConstraint operator<(BaseEquality a, BaseEquality b)
static WrappedConstraint operator<=(BaseEquality a, long v)
static IntExpr operator-(BaseEquality a, BaseEquality b)
Definition ValCstPair.cs:42
static WrappedConstraint operator<(long v, BaseEquality a)
static WrappedConstraint operator>(long v, BaseEquality a)
static IntExpr operator+(BaseEquality a, BaseEquality b)
Definition ValCstPair.cs:30
ConstraintEquality(IConstraintWithStatus a, IConstraintWithStatus b, bool equality)
IntExprEquality(IntExpr a, IntExpr b, bool equality)
Constraint MakeLess(IntExpr left, IntExpr right)
Definition Solver.cs:1051
Constraint MakeLessOrEqual(IntExpr left, IntExpr right)
Definition Solver.cs:925
Constraint MakeGreaterOrEqual(IntExpr left, IntExpr right)
Definition Solver.cs:967
Constraint MakeGreater(IntExpr left, IntExpr right)
Definition Solver.cs:1009
IntExpr MakeProd(IntExpr left, IntExpr right)
Definition Solver.cs:626
IntExpr MakeDiv(IntExpr expr, long value)
Definition Solver.cs:638
IntExpr MakeDifference(IntExpr left, IntExpr right)
Definition Solver.cs:608
IntVar MakeIsDifferentVar(IntExpr v1, IntExpr v2)
Definition Solver.cs:871
IntVar MakeIsEqualVar(IntExpr v1, IntExpr v2)
Definition Solver.cs:835
IntExpr MakeSquare(IntExpr expr)
Definition Solver.cs:656
IntExpr MakeOpposite(IntExpr expr)
Definition Solver.cs:620
IntExpr MakeSum(IntExpr left, IntExpr right)
Definition Solver.cs:575
Constraint MakeEquality(IntExpr left, IntExpr right)
Definition Solver.cs:841
IntExpr MakeAbs(IntExpr expr)
Definition Solver.cs:650
Constraint MakeNonEquality(IntExpr left, IntExpr right)
Definition Solver.cs:883