DotNet Reference

DotNet Reference

SimpleMipProgram.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 public class SimpleMipProgram
21 {
22  static void Main()
23  {
24  // [START solver]
25  // Create the linear solver with the CBC backend.
26  Solver solver = Solver.CreateSolver("SimpleMipProgram", "CBC_MIXED_INTEGER_PROGRAMMING");
27  // [END solver]
28 
29  // [START variables]
30  // x and y are integer non-negative variables.
31  Variable x = solver.MakeIntVar(0.0, double.PositiveInfinity, "x");
32  Variable y = solver.MakeIntVar(0.0, double.PositiveInfinity, "y");
33 
34  Console.WriteLine("Number of variables = " + solver.NumVariables());
35  // [END variables]
36 
37  // [START constraints]
38  // x + 7 * y <= 17.5.
39  solver.Add(x + 7 * y <= 17.5);
40 
41  // x <= 3.5.
42  solver.Add(x <= 3.5);
43 
44  Console.WriteLine("Number of constraints = " + solver.NumConstraints());
45  // [END constraints]
46 
47  // [START objective]
48  // Maximize x + 10 * y.
49  solver.Maximize(x + 10 * y);
50  // [END objective]
51 
52  // [START solve]
53  Solver.ResultStatus resultStatus = solver.Solve();
54  // [END solve]
55 
56  // [START print_solution]
57  // Check that the problem has an optimal solution.
58  if (resultStatus != Solver.ResultStatus.OPTIMAL)
59  {
60  Console.WriteLine("The problem does not have an optimal solution!");
61  return;
62  }
63  Console.WriteLine("Solution:");
64  Console.WriteLine("Objective value = " + solver.Objective().Value());
65  Console.WriteLine("x = " + x.SolutionValue());
66  Console.WriteLine("y = " + y.SolutionValue());
67  // [END print_solution]
68 
69  // [START advanced]
70  Console.WriteLine("\nAdvanced usage:");
71  Console.WriteLine("Problem solved in " + solver.WallTime() + " milliseconds");
72  Console.WriteLine("Problem solved in " + solver.Iterations() + " iterations");
73  Console.WriteLine("Problem solved in " + solver.Nodes() + " branch-and-bound nodes");
74  // [END advanced]
75  }
76 }
77 // [END program]
int NumConstraints()
Definition: Variable.cs:13
Solver.ResultStatus Solve()
void Maximize(LinearExpr expr)
double SolutionValue()
Definition: Variable.cs:56
int NumVariables()
long Iterations()
double Value()
Definition: Objective.cs:91
Objective Objective()
long WallTime()
Constraint Add(LinearConstraint constraint)
long Nodes()
ResultStatus
Variable MakeIntVar(double lb, double ub, string name)
static Solver CreateSolver(String name, String type)