DotNet Reference

DotNet Reference

LinearProgrammingExample.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 using System;
17 
19 {
20  static void Main()
21  {
22  // [START solver]
23  Solver solver = Solver.CreateSolver("LinearProgrammingExample", "GLOP_LINEAR_PROGRAMMING");
24  // [END solver]
25  // x and y are continuous non-negative variables.
26  // [START variables]
27  Variable x = solver.MakeNumVar(0.0, double.PositiveInfinity, "x");
28  Variable y = solver.MakeNumVar(0.0, double.PositiveInfinity, "y");
29  // [END variables]
30 
31  // [START constraints]
32  // x + 2y <= 14.
33  Constraint c0 = solver.MakeConstraint(double.NegativeInfinity, 14.0);
34  c0.SetCoefficient(x, 1);
35  c0.SetCoefficient(y, 2);
36 
37  // 3x - y >= 0.
38  Constraint c1 = solver.MakeConstraint(0.0, double.PositiveInfinity);
39  c1.SetCoefficient(x, 3);
40  c1.SetCoefficient(y, -1);
41 
42  // x - y <= 2.
43  Constraint c2 = solver.MakeConstraint(double.NegativeInfinity, 2.0);
44  c2.SetCoefficient(x, 1);
45  c2.SetCoefficient(y, -1);
46  // [END constraints]
47 
48  // [START objective]
49  // Objective function: 3x + 4y.
50  Objective objective = solver.Objective();
51  objective.SetCoefficient(x, 3);
52  objective.SetCoefficient(y, 4);
53  objective.SetMaximization();
54  // [END objective]
55 
56  // [START solve]
57  solver.Solve();
58  // [END solve]
59 
60  // [START print_solution]
61  Console.WriteLine("Number of variables = " + solver.NumVariables());
62  Console.WriteLine("Number of constraints = " + solver.NumConstraints());
63  // The value of each variable in the solution.
64  Console.WriteLine("Solution:");
65  Console.WriteLine("x = " + x.SolutionValue());
66  Console.WriteLine("y = " + y.SolutionValue());
67  // The objective value of the solution.
68  Console.WriteLine("Optimal objective value = " +
69  solver.Objective().Value());
70  // [END print_solution]
71  }
72 }
73 // [END program]
int NumConstraints()
Definition: Variable.cs:13
Solver.ResultStatus Solve()
double SolutionValue()
Definition: Variable.cs:56
int NumVariables()
void SetCoefficient(Variable var, double coeff)
Definition: Objective.cs:13
double Value()
Definition: Objective.cs:91
Objective Objective()
void SetCoefficient(Variable var, double coeff)
Definition: Objective.cs:51
void SetMaximization()
Definition: Objective.cs:77
static Solver CreateSolver(String name, String type)
Variable MakeNumVar(double lb, double ub, string name)
Constraint MakeConstraint(double lb, double ub)