DotNet Reference

DotNet Reference

SimpleLpProgram.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 // Minimal example to call the GLOP solver.
15 // [START program]
16 using System;
18 
19 public class SimpleLpProgram
20 {
21  static void Main()
22  {
23  // [START solver]
24  // Create the linear solver with the GLOP backend.
25  Solver solver = Solver.CreateSolver("SimpleLpProgram", "GLOP_LINEAR_PROGRAMMING");
26  // [END solver]
27 
28  // [START variables]
29  // Create the variables x and y.
30  Variable x = solver.MakeNumVar(0.0, 1.0, "x");
31  Variable y = solver.MakeNumVar(0.0, 2.0, "y");
32 
33  Console.WriteLine("Number of variables = " + solver.NumVariables());
34  // [END variables]
35 
36  // [START constraints]
37  // Create a linear constraint, 0 <= x + y <= 2.
38  Constraint ct = solver.MakeConstraint(0.0, 2.0, "ct");
39  ct.SetCoefficient(x, 1);
40  ct.SetCoefficient(y, 1);
41 
42  Console.WriteLine("Number of constraints = " + solver.NumConstraints());
43  // [END constraints]
44 
45  // [START objective]
46  // Create the objective function, 3 * x + y.
47  Objective objective = solver.Objective();
48  objective.SetCoefficient(x, 3);
49  objective.SetCoefficient(y, 1);
50  objective.SetMaximization();
51  // [END objective]
52 
53  // [START solve]
54  solver.Solve();
55  // [END solve]
56 
57  // [START print_solution]
58  Console.WriteLine("Solution:");
59  Console.WriteLine("Objective value = " + solver.Objective().Value());
60  Console.WriteLine("x = " + x.SolutionValue());
61  Console.WriteLine("y = " + y.SolutionValue());
62  // [END print_solution]
63  }
64 }
65 // [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)