Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
SolverHelper.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
19// Patch the MPSolver class to:
20// - support custom versions of the array-based APIs (MakeVarArray, etc).
21// - customize the construction, and the OptimizationProblemType enum.
22// - support the natural language API.
23public partial class Solver
24{
25 public Variable[] MakeVarArray(int count, double lb, double ub, bool integer)
26 {
27 Variable[] array = new Variable[count];
28 for (int i = 0; i < count; ++i)
29 {
30 array[i] = MakeVar(lb, ub, integer, "");
31 }
32 return array;
33 }
34
35 public Variable[] MakeVarArray(int count, double lb, double ub, bool integer, string var_name)
36 {
37 Variable[] array = new Variable[count];
38 for (int i = 0; i < count; ++i)
39 {
40 array[i] = MakeVar(lb, ub, integer, var_name + i);
41 }
42 return array;
43 }
44
45 public Variable[,] MakeVarMatrix(int rows, int cols, double lb, double ub, bool integer)
46 {
47 Variable[,] matrix = new Variable[rows, cols];
48 for (int i = 0; i < rows; ++i)
49 {
50 for (int j = 0; j < cols; ++j)
51 {
52 matrix[i, j] = MakeVar(lb, ub, integer, "");
53 }
54 }
55 return matrix;
56 }
57
58 public Variable[,] MakeVarMatrix(int rows, int cols, double lb, double ub, bool integer, string name)
59 {
60 Variable[,] matrix = new Variable[rows, cols];
61 for (int i = 0; i < rows; ++i)
62 {
63 for (int j = 0; j < cols; ++j)
64 {
65 string var_name = name + "[" + i + ", " + j + "]";
66 matrix[i, j] = MakeVar(lb, ub, integer, var_name);
67 }
68 }
69 return matrix;
70 }
71
72 public Variable[] MakeNumVarArray(int count, double lb, double ub)
73 {
74 return MakeVarArray(count, lb, ub, false);
75 }
76
77 public Variable[] MakeNumVarArray(int count, double lb, double ub, string var_name)
78 {
79 return MakeVarArray(count, lb, ub, false, var_name);
80 }
81
82 public Variable[,] MakeNumVarMatrix(int rows, int cols, double lb, double ub)
83 {
84 Variable[,] matrix = new Variable[rows, cols];
85 for (int i = 0; i < rows; ++i)
86 {
87 for (int j = 0; j < cols; ++j)
88 {
89 matrix[i, j] = MakeNumVar(lb, ub, "");
90 }
91 }
92 return matrix;
93 }
94
95 public Variable[,] MakeNumVarMatrix(int rows, int cols, double lb, double ub, string name)
96 {
97 Variable[,] matrix = new Variable[rows, cols];
98 for (int i = 0; i < rows; ++i)
99 {
100 for (int j = 0; j < cols; ++j)
101 {
102 string var_name = name + "[" + i + ", " + j + "]";
103 matrix[i, j] = MakeNumVar(lb, ub, var_name);
104 }
105 }
106 return matrix;
107 }
108
109 public Variable[] MakeIntVarArray(int count, double lb, double ub)
110 {
111 return MakeVarArray(count, lb, ub, true);
112 }
113
114 public Variable[] MakeIntVarArray(int count, double lb, double ub, string var_name)
115 {
116 return MakeVarArray(count, lb, ub, true, var_name);
117 }
118
119 public Variable[,] MakeIntVarMatrix(int rows, int cols, double lb, double ub)
120 {
121 Variable[,] matrix = new Variable[rows, cols];
122 for (int i = 0; i < rows; ++i)
123 {
124 for (int j = 0; j < cols; ++j)
125 {
126 matrix[i, j] = MakeIntVar(lb, ub, "");
127 }
128 }
129 return matrix;
130 }
131
132 public Variable[,] MakeIntVarMatrix(int rows, int cols, double lb, double ub, string name)
133 {
134 Variable[,] matrix = new Variable[rows, cols];
135 for (int i = 0; i < rows; ++i)
136 {
137 for (int j = 0; j < cols; ++j)
138 {
139 string var_name = name + "[" + i + ", " + j + "]";
140 matrix[i, j] = MakeIntVar(lb, ub, var_name);
141 }
142 }
143 return matrix;
144 }
145
146 public Variable[] MakeBoolVarArray(int count)
147 {
148 return MakeVarArray(count, 0.0, 1.0, true);
149 }
150
151 public Variable[] MakeBoolVarArray(int count, string var_name)
152 {
153 return MakeVarArray(count, 0.0, 1.0, true, var_name);
154 }
155
156 public Variable[,] MakeBoolVarMatrix(int rows, int cols)
157 {
158 Variable[,] matrix = new Variable[rows, cols];
159 for (int i = 0; i < rows; ++i)
160 {
161 for (int j = 0; j < cols; ++j)
162 {
163 matrix[i, j] = MakeBoolVar("");
164 }
165 }
166 return matrix;
167 }
168
169 public Variable[,] MakeBoolVarMatrix(int rows, int cols, string name)
170 {
171 Variable[,] matrix = new Variable[rows, cols];
172 for (int i = 0; i < rows; ++i)
173 {
174 for (int j = 0; j < cols; ++j)
175 {
176 string var_name = name + "[" + i + ", " + j + "]";
177 matrix[i, j] = MakeBoolVar(var_name);
178 }
179 }
180 return matrix;
181 }
182
183 public Constraint Add(LinearConstraint constraint)
184 {
185 return constraint.Extract(this);
186 }
187
188 public void Minimize(LinearExpr expr)
189 {
190 Objective().Clear();
192 Dictionary<Variable, double> coefficients = new Dictionary<Variable, double>();
193 double constant = expr.Visit(coefficients);
194 foreach (KeyValuePair<Variable, double> pair in coefficients)
195 {
196 Objective().SetCoefficient(pair.Key, pair.Value);
197 }
198 Objective().SetOffset(constant);
199 }
200
201 public void Maximize(LinearExpr expr)
202 {
203 Objective().Clear();
205 Dictionary<Variable, double> coefficients = new Dictionary<Variable, double>();
206 double constant = expr.Visit(coefficients);
207 foreach (KeyValuePair<Variable, double> pair in coefficients)
208 {
209 Objective().SetCoefficient(pair.Key, pair.Value);
210 }
211 Objective().SetOffset(constant);
212 }
213
214 public void Minimize(Variable var)
215 {
216 Objective().Clear();
218 Objective().SetCoefficient(var, 1.0);
219 }
220
221 public void Maximize(Variable var)
222 {
223 Objective().Clear();
225 Objective().SetCoefficient(var, 1.0);
226 }
227}
228
229} // namespace Google.OrTools.LinearSolver
virtual Constraint Extract(Solver solver)
double Visit(Dictionary< Variable, double > coefficients)
Definition LinearExpr.cs:31
void SetCoefficient(Variable var, double coeff)
Definition Objective.cs:66
Variable[,] MakeVarMatrix(int rows, int cols, double lb, double ub, bool integer, string name)
Variable MakeVar(double lb, double ub, bool integer, string name)
Definition Solver.cs:110
Variable[,] MakeBoolVarMatrix(int rows, int cols)
Variable[] MakeBoolVarArray(int count)
Variable[] MakeVarArray(int count, double lb, double ub, bool integer)
Variable[] MakeBoolVarArray(int count, string var_name)
Variable[] MakeNumVarArray(int count, double lb, double ub, string var_name)
Variable[,] MakeVarMatrix(int rows, int cols, double lb, double ub, bool integer)
Variable[,] MakeIntVarMatrix(int rows, int cols, double lb, double ub, string name)
Variable MakeBoolVar(string name)
Definition Solver.cs:131
Variable[] MakeVarArray(int count, double lb, double ub, bool integer, string var_name)
Variable[,] MakeBoolVarMatrix(int rows, int cols, string name)
Variable[] MakeIntVarArray(int count, double lb, double ub, string var_name)
Variable[,] MakeIntVarMatrix(int rows, int cols, double lb, double ub)
Variable MakeIntVar(double lb, double ub, string name)
Definition Solver.cs:124
Variable[] MakeNumVarArray(int count, double lb, double ub)
Constraint Add(LinearConstraint constraint)
Variable MakeNumVar(double lb, double ub, string name)
Definition Solver.cs:117
Variable[,] MakeNumVarMatrix(int rows, int cols, double lb, double ub)
Variable[,] MakeNumVarMatrix(int rows, int cols, double lb, double ub, string name)
Variable[] MakeIntVarArray(int count, double lb, double ub)
Patch the MPVariable class to support the natural language API.
Definition Variable.cs:15