DotNet Reference

.Net Reference

LinearConstraint.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 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
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 Dictionary<Variable, double> coefficients = new Dictionary<Variable, double>();
85 double constant = left_.Visit(coefficients);
86 constant += right_.DoVisit(coefficients, -1);
87 Constraint ct = solver.MakeConstraint(-constant, -constant);
88 foreach (KeyValuePair<Variable, double> pair in coefficients)
89 {
90 ct.SetCoefficient(pair.Key, pair.Value);
91 }
92 return ct;
93 }
94
95 public static implicit operator bool(Equality ct)
96 {
97 return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_;
98 }
99
100 private LinearExpr left_;
101 private LinearExpr right_;
102 private bool equality_;
103 }
104
106 {
107 public VarEquality(Variable left, Variable right, bool equality)
108 {
109 this.left_ = left;
110 this.right_ = right;
111 this.equality_ = equality;
112 }
113
114 public override String ToString()
115 {
116 return "" + left_.Name() + " == " + right_.Name();
117 }
118
119 public override Constraint Extract(Solver solver)
120 {
121 Constraint ct = solver.MakeConstraint(0.0, 0.0);
122 ct.SetCoefficient(left_, 1.0);
123 ct.SetCoefficient(right_, -1.0);
124 return ct;
125 }
126
127 public static implicit operator bool(VarEquality ct)
128 {
129 return (object)ct.left_ == (object)ct.right_ ? ct.equality_ : !ct.equality_;
130 }
131
132 private Variable left_;
133 private Variable right_;
134 private bool equality_;
135 }
136
137 // TODO(user): Try to move this code back to the .swig with @define macros.
138 public partial class MPConstraintVector : IDisposable,
139 System.Collections.IEnumerable
140#if !SWIG_DOTNET_1
141 ,
142 System.Collections.Generic.IList<Constraint>
143#endif
144 {
145 // cast from C# MPConstraint array
146 public static implicit operator MPConstraintVector(Constraint[] inVal)
147 {
148 var outVal = new MPConstraintVector();
149 foreach (Constraint element in inVal)
150 {
151 outVal.Add(element);
152 }
153 return outVal;
154 }
155
156 // cast to C# MPConstraint array
157 public static implicit operator Constraint[](MPConstraintVector inVal)
158 {
159 var outVal = new Constraint[inVal.Count];
160 inVal.CopyTo(outVal);
161 return outVal;
162 }
163 }
164} // namespace Google.OrTools.LinearSolver
using System
Definition: Program.cs:14
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)