DotNet Reference

.Net Reference

LinearExpr.cs
Go to the documentation of this file.
1 // Copyright 2010-2018 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 {
16  using System;
17  using System.Collections.Generic;
18 
19  public class LinearExpr
20  {
21  public virtual double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
22  {
23  return 0;
24  }
25 
26  public double Visit(Dictionary<Variable, double> coefficients)
27  {
28  return DoVisit(coefficients, 1.0);
29  }
30 
31  public static LinearExpr operator +(LinearExpr a, double v)
32  {
33  return new SumCst(a, v);
34  }
35 
36  public static LinearExpr operator +(double v, LinearExpr a)
37  {
38  return new SumCst(a, v);
39  }
40 
42  {
43  return new Sum(a, b);
44  }
45 
46  public static LinearExpr operator -(LinearExpr a, double v)
47  {
48  return new SumCst(a, -v);
49  }
50 
51  public static LinearExpr operator -(double v, LinearExpr a)
52  {
53  return new SumCst(new ProductCst(a, -1.0), v);
54  }
55 
57  {
58  return new Sum(a, new ProductCst(b, -1.0));
59  }
60 
61  public static LinearExpr operator -(LinearExpr a)
62  {
63  return new ProductCst(a, -1.0);
64  }
65 
66  public static LinearExpr operator *(LinearExpr a, double v)
67  {
68  return new ProductCst(a, v);
69  }
70 
71  public static LinearExpr operator /(LinearExpr a, double v)
72  {
73  return new ProductCst(a, 1 / v);
74  }
75 
76  public static LinearExpr operator *(double v, LinearExpr a)
77  {
78  return new ProductCst(a, v);
79  }
80 
81  public static RangeConstraint operator ==(LinearExpr a, double v)
82  {
83  return new RangeConstraint(a, v, v);
84  }
85 
86  public static RangeConstraint operator ==(double v, LinearExpr a)
87  {
88  return new RangeConstraint(a, v, v);
89  }
90 
91  public static RangeConstraint operator !=(LinearExpr a, double v)
92  {
93  return new RangeConstraint(a, 1, -1);
94  }
95 
96  public static RangeConstraint operator !=(double v, LinearExpr a)
97  {
98  return new RangeConstraint(a, 1, -1);
99  }
100 
102  {
103  return new Equality(a, b, true);
104  }
105 
107  {
108  return new Equality(a, b, false);
109  }
110 
111  public static RangeConstraint operator <=(LinearExpr a, double v)
112  {
113  return new RangeConstraint(a, double.NegativeInfinity, v);
114  }
115 
116  public static RangeConstraint operator >=(LinearExpr a, double v)
117  {
118  return new RangeConstraint(a, v, double.PositiveInfinity);
119  }
120 
122  {
123  return a - b <= 0;
124  }
125 
127  {
128  return a - b >= 0;
129  }
130 
131  public static implicit operator LinearExpr(Variable a)
132  {
133  return new VarWrapper(a);
134  }
135  }
136 
137  public static class LinearExprArrayHelper
138  {
139  public static LinearExpr Sum(this LinearExpr[] exprs)
140  {
141  return new SumArray(exprs);
142  }
143 
144  public static LinearExpr Sum(this Variable[] vars)
145  {
146  return new SumVarArray(vars);
147  }
148  }
149 
150  // def __ge__(self, arg):
151  // if isinstance(arg, (int, long, float)):
152  // return LinearConstraint(self, arg, 1e308)
153  // else:
154  // return LinearConstraint(Sum(self, ProductCst(arg, -1)), 0.0, 1e308)
155 
156  // def __le__(self, arg):
157  // if isinstance(arg, (int, long, float)):
158  // return LinearConstraint(self, -1e308, arg)
159  // else:
160  // return LinearConstraint(Sum(self, ProductCst(arg, -1)), -1e308, 0.0)
161 
163  {
164  public ProductCst(LinearExpr expr, double coeff)
165  {
166  this.coeff_ = coeff;
167  this.expr_ = expr;
168  }
169 
170  public override String ToString()
171  {
172  return "(" + expr_.ToString() + " * " + coeff_ + ")";
173  }
174 
175  public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
176  {
177  double current_multiplier = multiplier * coeff_;
178  if (current_multiplier != 0.0)
179  {
180  return expr_.DoVisit(coefficients, current_multiplier);
181  }
182  else
183  {
184  return 0.0;
185  }
186  }
187 
188  private LinearExpr expr_;
189  private double coeff_;
190  }
191 
193  {
194  public SumCst(LinearExpr expr, double coeff)
195  {
196  this.coeff_ = coeff;
197  this.expr_ = expr;
198  }
199 
200  public override String ToString()
201  {
202  return "(" + expr_.ToString() + " + " + coeff_ + ")";
203  }
204 
205  public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
206  {
207  if (multiplier != 0.0)
208  {
209  return coeff_ * multiplier + expr_.DoVisit(coefficients, multiplier);
210  }
211  else
212  {
213  return 0.0;
214  }
215  }
216 
217  private LinearExpr expr_;
218  private double coeff_;
219  }
220 
222  {
223  public VarWrapper(Variable var)
224  {
225  this.var_ = var;
226  }
227 
228  public override String ToString()
229  {
230  return var_.Name();
231  }
232 
233  public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
234  {
235  if (multiplier != 0.0)
236  {
237  if (coefficients.ContainsKey(var_))
238  {
239  coefficients[var_] += multiplier;
240  }
241  else
242  {
243  coefficients[var_] = multiplier;
244  }
245  }
246  return 0.0;
247  }
248 
249  private Variable var_;
250  }
251 
252  class Sum : LinearExpr
253  {
254  public Sum(LinearExpr left, LinearExpr right)
255  {
256  this.left_ = left;
257  this.right_ = right;
258  }
259 
260  public override String ToString()
261  {
262  return "(" + left_.ToString() + " + " + right_.ToString() + ")";
263  }
264 
265  public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
266  {
267  if (multiplier != 0.0)
268  {
269  return left_.DoVisit(coefficients, multiplier) + right_.DoVisit(coefficients, multiplier);
270  }
271  else
272  {
273  return 0.0;
274  }
275  }
276 
277  private LinearExpr left_;
278  private LinearExpr right_;
279  }
280 
281  public class SumArray : LinearExpr
282  {
283  public SumArray(LinearExpr[] array)
284  {
285  this.array_ = array;
286  }
287 
288  public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
289  {
290  if (multiplier != 0.0)
291  {
292  double constant = 0.0;
293  foreach (LinearExpr expr in array_)
294  {
295  constant += expr.DoVisit(coefficients, multiplier);
296  }
297  return constant;
298  }
299  else
300  {
301  return 0.0;
302  }
303  }
304 
305  private LinearExpr[] array_;
306  }
307 
308  public class SumVarArray : LinearExpr
309  {
310  public SumVarArray(Variable[] array)
311  {
312  this.array_ = array;
313  }
314 
315  public override double DoVisit(Dictionary<Variable, double> coefficients, double multiplier)
316  {
317  if (multiplier != 0.0)
318  {
319  foreach (Variable var in array_)
320  {
321  if (coefficients.ContainsKey(var))
322  {
323  coefficients[var] += multiplier;
324  }
325  else
326  {
327  coefficients[var] = multiplier;
328  }
329  }
330  }
331  return 0.0;
332  }
333 
334  private Variable[] array_;
335  }
336 } // namespace Google.OrTools.LinearSolver
static RangeConstraint operator!=(LinearExpr a, double v)
Definition: LinearExpr.cs:91
Definition: Variable.cs:13
Sum(LinearExpr left, LinearExpr right)
Definition: LinearExpr.cs:254
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition: LinearExpr.cs:233
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition: LinearExpr.cs:175
override String ToString()
Definition: LinearExpr.cs:170
VarWrapper(Variable var)
Definition: LinearExpr.cs:223
override String ToString()
Definition: LinearExpr.cs:200
static LinearExpr operator/(LinearExpr a, double v)
Definition: LinearExpr.cs:71
static LinearExpr operator*(LinearExpr a, double v)
Definition: LinearExpr.cs:66
Definition: LinearExpr.cs:253
override String ToString()
Definition: LinearExpr.cs:228
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition: LinearExpr.cs:288
static LinearExpr operator+(LinearExpr a, double v)
Definition: LinearExpr.cs:31
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition: LinearExpr.cs:205
using System
Definition: Program.cs:14
Definition: LinearExpr.cs:222
SumVarArray(Variable[] array)
Definition: LinearExpr.cs:310
override String ToString()
Definition: LinearExpr.cs:260
Definition: LinearExpr.cs:138
SumCst(LinearExpr expr, double coeff)
Definition: LinearExpr.cs:194
Definition: LinearExpr.cs:193
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition: LinearExpr.cs:315
Definition: LinearExpr.cs:282
Definition: LinearExpr.cs:20
static LinearExpr operator-(LinearExpr a, double v)
Definition: LinearExpr.cs:46
static RangeConstraint operator==(LinearExpr a, double v)
Definition: LinearExpr.cs:81
double Visit(Dictionary< Variable, double > coefficients)
Definition: LinearExpr.cs:26
SumArray(LinearExpr[] array)
Definition: LinearExpr.cs:283
static LinearExpr Sum(this Variable[] vars)
Definition: LinearExpr.cs:144
static LinearExpr Sum(this LinearExpr[] exprs)
Definition: LinearExpr.cs:139
Definition: LinearExpr.cs:163
static RangeConstraint operator>=(LinearExpr a, double v)
Definition: LinearExpr.cs:116
ProductCst(LinearExpr expr, double coeff)
Definition: LinearExpr.cs:164
virtual double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition: LinearExpr.cs:21
Definition: LinearExpr.cs:309
static RangeConstraint operator<=(LinearExpr a, double v)
Definition: LinearExpr.cs:111
override double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition: LinearExpr.cs:265
string Name()
Definition: Variable.cs:47