DotNet Reference

.Net Reference

LinearConstraint.cs
Go to the documentation of this file.
1 // Copyright 2010-2022 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 LinearConstraint
20 {
21  public virtual String ToString()
22  {
23  return "LinearConstraint";
24  }
25 
26  public virtual Constraint Extract(Solver solver)
27  {
28  return null;
29  }
30 }
31 
33 {
34  public RangeConstraint(LinearExpr expr, double lb, double ub)
35  {
36  this.expr_ = expr;
37  this.lb_ = lb;
38  this.ub_ = ub;
39  }
40 
41  public override String ToString()
42  {
43  return "" + lb_ + " <= " + expr_.ToString() + " <= " + ub_;
44  }
45 
46  public override Constraint Extract(Solver solver)
47  {
48  Dictionary<Variable, double> coefficients = new Dictionary<Variable, double>();
49  double constant = expr_.Visit(coefficients);
50  Constraint ct = solver.MakeConstraint(lb_ - constant, ub_ - constant);
51  foreach (KeyValuePair<Variable, double> pair in coefficients)
52  {
53  ct.SetCoefficient(pair.Key, pair.Value);
54  }
55  return ct;
56  }
57 
58  public static implicit operator bool(RangeConstraint ct)
59  {
60  return false;
61  }
62 
63  private LinearExpr expr_;
64  private double lb_;
65  private double ub_;
66 }
67 
68 public class Equality : LinearConstraint
69 {
70  public Equality(LinearExpr left, LinearExpr right, bool equality)
71  {
72  this.left_ = left;
73  this.right_ = right;
74  this.equality_ = equality;
75  }
76 
77  public override String ToString()
78  {
79  return "" + left_.ToString() + " == " + right_.ToString();
80  }
81 
82  public override Constraint Extract(Solver solver)
83  {
84  if (!equality_)
85  {
86  throw new ArgumentException("Operator != not supported for LinearExpression");
87  }
88  Dictionary<Variable, double> coefficients = new Dictionary<Variable, double>();
89  double constant = left_.Visit(coefficients);
90  constant += right_.DoVisit(coefficients, -1);
91  Constraint ct = solver.MakeConstraint(-constant, -constant);
92  foreach (KeyValuePair<Variable, double> pair in coefficients)
93  {
94  ct.SetCoefficient(pair.Key, pair.Value);
95  }
96  return ct;
97  }
98 
99  public static implicit operator bool(Equality ct)
100  {
101  return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_;
102  }
103 
104  private LinearExpr left_;
105  private LinearExpr right_;
106  private bool equality_;
107 }
108 
110 {
111  public VarEquality(Variable left, Variable right, bool equality)
112  {
113  this.left_ = left;
114  this.right_ = right;
115  this.equality_ = equality;
116  }
117 
118  public override String ToString()
119  {
120  return "" + left_.Name() + " == " + right_.Name();
121  }
122 
123  public override Constraint Extract(Solver solver)
124  {
125  Constraint ct = solver.MakeConstraint(0.0, 0.0);
126  ct.SetCoefficient(left_, 1.0);
127  ct.SetCoefficient(right_, -1.0);
128  return ct;
129  }
130 
131  public static implicit operator bool(VarEquality ct)
132  {
133  return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_;
134  }
135 
136  private Variable left_;
137  private Variable right_;
138  private bool equality_;
139 }
140 
141 // TODO(user): Try to move this code back to the .i with @define macros.
142 public partial class MPConstraintVector : IDisposable,
143  System.Collections.IEnumerable
144 #if !SWIG_DOTNET_1
145  ,
146  System.Collections.Generic.IList<Constraint>
147 #endif
148 {
149  // cast from C# MPConstraint array
150  public static implicit operator MPConstraintVector(Constraint[] inVal)
151  {
152  var outVal = new MPConstraintVector();
153  foreach (Constraint element in inVal)
154  {
155  outVal.Add(element);
156  }
157  return outVal;
158  }
159 
160  // cast to C# MPConstraint array
161  public static implicit operator Constraint[](MPConstraintVector inVal)
162  {
163  var outVal = new Constraint[inVal.Count];
164  inVal.CopyTo(outVal);
165  return outVal;
166  }
167 }
168 } // namespace Google.OrTools.LinearSolver
Equality(LinearExpr left, LinearExpr right, bool equality)
override Constraint Extract(Solver solver)
virtual Constraint Extract(Solver solver)
double Visit(Dictionary< Variable, double > coefficients)
Definition: LinearExpr.cs:26
virtual double DoVisit(Dictionary< Variable, double > coefficients, double multiplier)
Definition: LinearExpr.cs:21
RangeConstraint(LinearExpr expr, double lb, double ub)
override Constraint Extract(Solver solver)
VarEquality(Variable left, Variable right, bool equality)
override Constraint Extract(Solver solver)