Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
LinearExpr.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 class LinearExpr
20{
21 public virtual double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
22 {
23 return 0;
24 }
25
26 public virtual double SolutionValue()
27 {
28 return 0;
29 }
30
31 public double Visit(Dictionary<Variable, double> coefficients)
32 {
33 return DoVisit(coefficients, 1.0);
34 }
35
36 public static LinearExpr operator +(LinearExpr a, double v)
37 {
38 return new SumCst(a, v);
39 }
40
41 public static LinearExpr operator +(double v, LinearExpr a)
42 {
43 return new SumCst(a, v);
44 }
45
47 {
48 return new Sum(a, b);
49 }
50
51 public static LinearExpr operator -(LinearExpr a, double v)
52 {
53 return new SumCst(a, -v);
54 }
55
56 public static LinearExpr operator -(double v, LinearExpr a)
57 {
58 return new SumCst(new ProductCst(a, -1.0), v);
59 }
60
62 {
63 return new Sum(a, new ProductCst(b, -1.0));
64 }
65
67 {
68 return new ProductCst(a, -1.0);
69 }
70
71 public static LinearExpr operator *(LinearExpr a, double v)
72 {
73 return new ProductCst(a, v);
74 }
75
76 public static LinearExpr operator /(LinearExpr a, double v)
77 {
78 return new ProductCst(a, 1 / v);
79 }
80
81 public static LinearExpr operator *(double v, LinearExpr a)
82 {
83 return new ProductCst(a, v);
84 }
85
86 public static RangeConstraint operator ==(LinearExpr a, double v)
87 {
88 return new RangeConstraint(a, v, v);
89 }
90
91 public static RangeConstraint operator ==(double v, LinearExpr a)
92 {
93 return new RangeConstraint(a, v, v);
94 }
95
96 public static RangeConstraint operator !=(LinearExpr a, double v)
97 {
98 throw new ArgumentException("Operator != not supported for LinearExpression");
99 }
100
101 public static RangeConstraint operator !=(double v, LinearExpr a)
102 {
103 throw new ArgumentException("Operator != not supported for LinearExpression");
104 }
105
107 {
108 return new Equality(a, b, true);
109 }
110
112 {
113 return new Equality(a, b, false);
114 }
115
116 public static RangeConstraint operator <=(LinearExpr a, double v)
117 {
118 return new RangeConstraint(a, double.NegativeInfinity, v);
119 }
120
121 public static RangeConstraint operator >=(LinearExpr a, double v)
122 {
123 return new RangeConstraint(a, v, double.PositiveInfinity);
124 }
125
127 {
128 return a - b <= 0;
129 }
130
132 {
133 return a - b >= 0;
134 }
135
136 public static implicit operator LinearExpr(Variable a)
137 {
138 return new VarWrapper(a);
139 }
140}
141
142public static class LinearExprArrayHelper
143{
144 public static LinearExpr Sum(this LinearExpr[] exprs)
145 {
146 return new SumArray(exprs);
147 }
148
149 public static LinearExpr Sum(this Variable[] vars)
150 {
151 return new SumVarArray(vars);
152 }
153}
154
155// def __ge__(self, arg):
156// if isinstance(arg, (int, long, float)):
157// return LinearConstraint(self, arg, 1e308)
158// else:
159// return LinearConstraint(Sum(self, ProductCst(arg, -1)), 0.0, 1e308)
160
161// def __le__(self, arg):
162// if isinstance(arg, (int, long, float)):
163// return LinearConstraint(self, -1e308, arg)
164// else:
165// return LinearConstraint(Sum(self, ProductCst(arg, -1)), -1e308, 0.0)
166
168{
169 public ProductCst(LinearExpr expr, double coeff)
170 {
171 this.coeff_ = coeff;
172 this.expr_ = expr;
173 }
174
175 public override String ToString()
176 {
177 return "(" + expr_.ToString() + " * " + coeff_ + ")";
178 }
179
180 public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
181 {
182 double current_multiplier = multiplier * coeff_;
183 if (current_multiplier != 0.0)
184 {
185 return expr_.DoVisit(coefficients, current_multiplier);
186 }
187 else
188 {
189 return 0.0;
190 }
191 }
192
193 public override double SolutionValue()
194 {
195 return expr_.SolutionValue() * coeff_;
196 }
197
198 private LinearExpr expr_;
199 private double coeff_;
200}
201
203{
204 public SumCst(LinearExpr expr, double coeff)
205 {
206 this.coeff_ = coeff;
207 this.expr_ = expr;
208 }
209
210 public override String ToString()
211 {
212 return "(" + expr_.ToString() + " + " + coeff_ + ")";
213 }
214
215 public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
216 {
217 if (multiplier != 0.0)
218 {
219 return coeff_ * multiplier + expr_.DoVisit(coefficients, multiplier);
220 }
221 else
222 {
223 return 0.0;
224 }
225 }
226
227 public override double SolutionValue()
228 {
229 return expr_.SolutionValue() + coeff_;
230 }
231
232 private LinearExpr expr_;
233 private double coeff_;
234}
235
237{
239 {
240 this.var_ = var;
241 }
242
243 public override String ToString()
244 {
245 return var_.Name();
246 }
247
248 public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
249 {
250 if (multiplier != 0.0)
251 {
252 if (coefficients.ContainsKey(var_))
253 {
254 coefficients[var_] += multiplier;
255 }
256 else
257 {
258 coefficients[var_] = multiplier;
259 }
260 }
261 return 0.0;
262 }
263
264 public override double SolutionValue()
265 {
266 return var_.SolutionValue();
267 }
268
269 private Variable var_;
270}
271
273{
274 public Sum(LinearExpr left, LinearExpr right)
275 {
276 this.left_ = left;
277 this.right_ = right;
278 }
279
280 public override String ToString()
281 {
282 return "(" + left_.ToString() + " + " + right_.ToString() + ")";
283 }
284
285 public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
286 {
287 if (multiplier != 0.0)
288 {
289 return left_.DoVisit(coefficients, multiplier) + right_.DoVisit(coefficients, multiplier);
290 }
291 else
292 {
293 return 0.0;
294 }
295 }
296
297 public override double SolutionValue()
298 {
299 return left_.SolutionValue() + right_.SolutionValue();
300 }
301
302 private LinearExpr left_;
303 private LinearExpr right_;
304}
305
306public class SumArray : LinearExpr
307{
308 public SumArray(LinearExpr[] array)
309 {
310 this.array_ = array;
311 }
312
313 public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
314 {
315 if (multiplier != 0.0)
316 {
317 double constant = 0.0;
318 foreach (LinearExpr expr in array_)
319 {
320 constant += expr.DoVisit(coefficients, multiplier);
321 }
322 return constant;
323 }
324 else
325 {
326 return 0.0;
327 }
328 }
329
330 public override double SolutionValue()
331 {
332 double sum = 0.0;
333 foreach (LinearExpr expr in array_)
334 {
335 sum += expr.SolutionValue();
336 }
337 return sum;
338 }
339
340 private LinearExpr[] array_;
341}
342
344{
345 public SumVarArray(Variable[] array)
346 {
347 this.array_ = array;
348 }
349
350 public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
351 {
352 if (multiplier != 0.0)
353 {
354 foreach (Variable var in array_)
355 {
356 if (coefficients.ContainsKey(var))
357 {
358 coefficients[var] += multiplier;
359 }
360 else
361 {
362 coefficients[var] = multiplier;
363 }
364 }
365 }
366 return 0.0;
367 }
368
369 public override double SolutionValue()
370 {
371 double sum = 0.0;
372 foreach (Variable var in array_)
373 {
374 sum += var.SolutionValue();
375 }
376 return sum;
377 }
378
379 private Variable[] array_;
380}
381} // namespace Google.OrTools.LinearSolver
static LinearExpr Sum(this LinearExpr[] exprs)
static LinearExpr Sum(this Variable[] vars)
static LinearExpr operator*(LinearExpr a, double v)
Definition LinearExpr.cs:71
static LinearExpr operator/(LinearExpr a, double v)
Definition LinearExpr.cs:76
static LinearExpr operator-(LinearExpr a, double v)
Definition LinearExpr.cs:51
static RangeConstraint operator!=(LinearExpr a, double v)
Definition LinearExpr.cs:96
static RangeConstraint operator==(LinearExpr a, double v)
Definition LinearExpr.cs:86
static RangeConstraint operator>=(LinearExpr a, double v)
double Visit(Dictionary< Variable, double > coefficients)
Definition LinearExpr.cs:31
static RangeConstraint operator<=(LinearExpr a, double v)
virtual double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition LinearExpr.cs:21
static LinearExpr operator+(LinearExpr a, double v)
Definition LinearExpr.cs:36
ProductCst(LinearExpr expr, double coeff)
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
SumCst(LinearExpr expr, double coeff)
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
override double SolutionValue()
Sum(LinearExpr left, LinearExpr right)
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Patch the MPVariable class to support the natural language API.
Definition Variable.cs:15