Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ModelBuilderConstraint.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{
17using System;
18using System.Collections;
19using System.Collections.Generic;
20using System.Linq;
21using System.Runtime.CompilerServices;
22using Google.Protobuf.Collections;
23
27public class LinearConstraint
28{
30 {
31 helper_ = helper;
32 index_ = helper_.AddLinearConstraint();
33 }
34
35 public LinearConstraint(ModelBuilderHelper helper, int index)
36 {
37 helper_ = helper;
38 index_ = index;
39 }
40
44 public int Index
45 {
46 get {
47 return index_;
48 }
49 }
50
55 {
56 get {
57 return helper_;
58 }
59 }
60
64 public double LowerBound
65 {
66 get {
67 return helper_.ConstraintLowerBound(index_);
68 }
69 set {
70 helper_.SetConstraintLowerBound(index_, value);
71 }
72 }
73
77 public double UpperBound
78 {
79 get {
80 return helper_.ConstraintUpperBound(index_);
81 }
82 set {
83 helper_.SetConstraintUpperBound(index_, value);
84 }
85 }
86
90 public String Name
91 {
92 get {
93 return helper_.ConstraintName(index_);
94 }
95 set {
96 helper_.SetConstraintName(index_, value);
97 }
98 }
99
105 public void AddTerm(Variable var, double coeff)
106 {
107 helper_.SafeAddConstraintTerm(index_, var.Index, coeff);
108 }
109
115 public void SetVariableCoefficient(Variable var, double coeff)
116 {
117 helper_.SetConstraintCoefficient(index_, var.Index, coeff);
118 }
119
123 public void ClearTerms()
124 {
125 helper_.ClearConstraintTerms(index_);
126 }
127
133 public LinearConstraint WithName(String name)
134 {
135 Name = name;
136 return this;
137 }
138
139 private readonly int index_;
140 private ModelBuilderHelper helper_;
141}
142
147{
149 {
150 helper_ = helper;
151 index_ = helper_.AddEnforcedLinearConstraint();
152 }
153
155 {
156 if (!helper.IsEnforcedConstraint(index))
157 {
158 throw new ArgumentException("the given index does not refer to an enforced linear constraint");
159 }
160 helper_ = helper;
161 index_ = index;
162 }
163
167 public int Index
168 {
169 get {
170 return index_;
171 }
172 }
173
178 {
179 get {
180 return helper_;
181 }
182 }
183
187 public double LowerBound
188 {
189 get {
190 return helper_.EnforcedConstraintLowerBound(index_);
191 }
192 set {
193 helper_.SetEnforcedConstraintLowerBound(index_, value);
194 }
195 }
196
200 public double UpperBound
201 {
202 get {
203 return helper_.EnforcedConstraintUpperBound(index_);
204 }
205 set {
206 helper_.SetEnforcedConstraintUpperBound(index_, value);
207 }
208 }
209
214 {
215 get {
216 return new Variable(helper_, helper_.EnforcedIndicatorVariableIndex(index_));
217 }
218 set {
219 helper_.SetEnforcedIndicatorVariableIndex(index_, value.Index);
220 }
221 }
222
226 public bool IndicatorValue
227 {
228 get {
229 return helper_.EnforcedIndicatorValue(index_);
230 }
231 set {
232 helper_.SetEnforcedIndicatorValue(index_, value);
233 }
234 }
235
239 public String Name
240 {
241 get {
242 return helper_.EnforcedConstraintName(index_);
243 }
244 set {
245 helper_.SetEnforcedConstraintName(index_, value);
246 }
247 }
248
254 public void AddTerm(Variable var, double coeff)
255 {
256 helper_.SafeAddEnforcedConstraintTerm(index_, var.Index, coeff);
257 }
258
264 public void SetVariableCoefficient(Variable var, double coeff)
265 {
266 helper_.SetEnforcedConstraintCoefficient(index_, var.Index, coeff);
267 }
268
272 public void ClearTerms()
273 {
274 helper_.ClearEnforcedConstraintTerms(index_);
275 }
276
283 {
284 Name = name;
285 return this;
286 }
287
288 private readonly int index_;
289 private ModelBuilderHelper helper_;
290}
291
292} // namespace Google.OrTools.ModelBuilder
Wrapper around an enforced linear constraint stored in the ModelBuilderHelper instance.
int Index
Returns the index of the constraint in the model.
double UpperBound
The upper bound of the constraint.
String Name
The name of the variable given upon creation.
Variable IndicatorVariable
The indicator variable of the constraint.
void AddTerm(Variable var, double coeff)
Adds var * coeff to the constraint.
EnforcedLinearConstraint WithName(String name)
Inline setter.
double LowerBound
The lower bound of the constraint.
void ClearTerms()
Clear all terms of the constraint.
bool IndicatorValue
The indicator value of the constraint.
EnforcedLinearConstraint(ModelBuilderHelper helper, int index)
ModelBuilderHelper Helper
Returns the constraint builder.
void SetVariableCoefficient(Variable var, double coeff)
Sets the coefficient of var to coeff, adding or removing a term if needed.
Wrapper around a linear constraint stored in the ModelBuilderHelper instance.
ModelBuilderHelper Helper
Returns the constraint builder.
void AddTerm(Variable var, double coeff)
Adds var * coeff to the constraint.
double LowerBound
The lower bound of the constraint.
int Index
Returns the index of the constraint in the model.
String Name
The name of the variable given upon creation.
LinearConstraint WithName(String name)
Inline name setter.
double UpperBound
The upper bound of the constraint.
LinearConstraint(ModelBuilderHelper helper, int index)
void SetVariableCoefficient(Variable var, double coeff)
Sets the coefficient of var to coeff, adding or removing a term if needed.
void ClearTerms()
Clear all terms of the constraint.
void SetEnforcedConstraintCoefficient(int ct_index, int var_index, double coeff)
void SetConstraintLowerBound(int ct_index, double lb)
void SafeAddEnforcedConstraintTerm(int ct_index, int var_index, double coeff)
void SetEnforcedConstraintUpperBound(int ct_index, double ub)
void SetConstraintCoefficient(int ct_index, int var_index, double coeff)
void SetEnforcedConstraintLowerBound(int ct_index, double lb)
void SetConstraintUpperBound(int ct_index, double ub)
void SetConstraintName(int ct_index, string name)
void SetEnforcedConstraintName(int ct_index, string name)
void SafeAddConstraintTerm(int ct_index, int var_index, double coeff)
void SetEnforcedIndicatorVariableIndex(int ct_index, int var_index)
void SetEnforcedIndicatorValue(int ct_index, bool positive)