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