DotNet Reference

DotNet Reference

MipVarArray.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 
14 // [START program]
15 // [START import]
16 using System;
18 // [END import]
19 
20 // [START program_part1]
21 public class MipVarArray
22 {
23  // [START data_model]
24  class DataModel
25  {
26  public double[,] ConstraintCoeffs = {
27  {5, 7, 9, 2, 1},
28  {18, 4, -9, 10, 12},
29  {4, 7, 3, 8, 5},
30  {5, 13, 16, 3, -7},
31  };
32  public double[] Bounds = { 250, 285, 211, 315 };
33  public double[] ObjCoeffs = { 7, 8, 2, 9, 6 };
34  public int NumVars = 5;
35  public int NumConstraints = 4;
36  }
37  // [END data_model]
38  public static void Main()
39  {
40  // [START data]
41  DataModel data = new DataModel();
42  // [END data]
43  // [END program_part1]
44 
45  // [START solver]
46  // Create the linear solver with the CBC backend.
47  Solver solver = Solver.CreateSolver("SimpleMipProgram", "CBC_MIXED_INTEGER_PROGRAMMING");
48  // [END solver]
49 
50  // [START program_part2]
51  // [START variables]
52  Variable[] x = new Variable[data.NumVars];
53  for (int j = 0; j < data.NumVars; j++)
54  {
55  x[j] = solver.MakeIntVar(0.0, double.PositiveInfinity, $"x_{j}");
56  }
57  Console.WriteLine("Number of variables = " + solver.NumVariables());
58  // [END variables]
59 
60  // [START constraints]
61  for (int i = 0; i < data.NumConstraints; ++i)
62  {
63  Constraint constraint = solver.MakeConstraint(0, data.Bounds[i], "");
64  for (int j = 0; j < data.NumVars; ++j)
65  {
66  constraint.SetCoefficient(x[j], data.ConstraintCoeffs[i, j]);
67  }
68  }
69  Console.WriteLine("Number of constraints = " + solver.NumConstraints());
70  // [END constraints]
71 
72  // [START objective]
73  Objective objective = solver.Objective();
74  for (int j = 0; j < data.NumVars; ++j)
75  {
76  objective.SetCoefficient(x[j], data.ObjCoeffs[j]);
77  }
78  objective.SetMaximization();
79  // [END objective]
80 
81  // [START solve]
82  Solver.ResultStatus resultStatus = solver.Solve();
83  // [END solve]
84 
85  // [START print_solution]
86  // Check that the problem has an optimal solution.
87  if (resultStatus != Solver.ResultStatus.OPTIMAL)
88  {
89  Console.WriteLine("The problem does not have an optimal solution!");
90  return;
91  }
92 
93  Console.WriteLine("Solution:");
94  Console.WriteLine("Optimal objective value = " + solver.Objective().Value());
95 
96  for (int j = 0; j < data.NumVars; ++j)
97  {
98  Console.WriteLine("x[" + j + "] = " + x[j].SolutionValue());
99  }
100  // [END print_solution]
101 
102  // [START advanced]
103  Console.WriteLine("\nAdvanced usage:");
104  Console.WriteLine("Problem solved in " + solver.WallTime() + " milliseconds");
105  Console.WriteLine("Problem solved in " + solver.Iterations() + " iterations");
106  Console.WriteLine("Problem solved in " + solver.Nodes() + " branch-and-bound nodes");
107  // [END advanced]
108  }
109 }
110 // [END program_part2]
111 // [END program]
int NumConstraints()
Definition: Variable.cs:13
Solver.ResultStatus Solve()
int NumVariables()
static void Main()
Definition: MipVarArray.cs:38
void SetCoefficient(Variable var, double coeff)
long Iterations()
Definition: Objective.cs:13
double Value()
Definition: Objective.cs:91
Objective Objective()
Definition: MipVarArray.cs:22
long WallTime()
void SetCoefficient(Variable var, double coeff)
Definition: Objective.cs:51
long Nodes()
ResultStatus
Variable MakeIntVar(double lb, double ub, string name)
void SetMaximization()
Definition: Objective.cs:77
static Solver CreateSolver(String name, String type)
Constraint MakeConstraint(double lb, double ub)