DotNet Reference

DotNet Reference

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