cslinearprogramming.cs 7.12 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Google.OrTools.LinearSolver;

public class CsLinearProgramming
{
  private static void RunLinearProgrammingExample(String solverType)
  {
    Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
    if (solver == null)
    {
      Console.WriteLine("Could not create solver " + solverType);
      return;
    }
    // x1, x2 and x3 are continuous non-negative variables.
    Variable x1 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x1");
    Variable x2 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x2");
    Variable x3 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x3");

    // Maximize 10 * x1 + 6 * x2 + 4 * x3.
    Objective objective = solver.Objective();
    objective.SetCoefficient(x1, 10);
    objective.SetCoefficient(x2, 6);
    objective.SetCoefficient(x3, 4);
    objective.SetMaximization();

    // x1 + x2 + x3 <= 100.
    Constraint c0 = solver.MakeConstraint(double.NegativeInfinity, 100.0);
    c0.SetCoefficient(x1, 1);
    c0.SetCoefficient(x2, 1);
    c0.SetCoefficient(x3, 1);

    // 10 * x1 + 4 * x2 + 5 * x3 <= 600.
    Constraint c1 = solver.MakeConstraint(double.NegativeInfinity, 600.0);
    c1.SetCoefficient(x1, 10);
    c1.SetCoefficient(x2, 4);
    c1.SetCoefficient(x3, 5);

    // 2 * x1 + 2 * x2 + 6 * x3 <= 300.
    Constraint c2 = solver.MakeConstraint(double.NegativeInfinity, 300.0);
    c2.SetCoefficient(x1, 2);
    c2.SetCoefficient(x2, 2);
    c2.SetCoefficient(x3, 6);

    Console.WriteLine("Number of variables = " + solver.NumVariables());
    Console.WriteLine("Number of constraints = " + solver.NumConstraints());

    Solver.ResultStatus resultStatus = solver.Solve();

    // Check that the problem has an optimal solution.
    if (resultStatus != Solver.ResultStatus.OPTIMAL) {
      Console.WriteLine("The problem does not have an optimal solution!");
      return;
    }

    Console.WriteLine("Problem solved in " + solver.WallTime() +
                      " milliseconds");

    // The objective value of the solution.
    Console.WriteLine("Optimal objective value = " +
                      solver.Objective().Value());

    // The value of each variable in the solution.
    Console.WriteLine("x1 = " + x1.SolutionValue());
    Console.WriteLine("x2 = " + x2.SolutionValue());
    Console.WriteLine("x3 = " + x3.SolutionValue());

    Console.WriteLine("Advanced usage:");
    double[] activities = solver.ComputeConstraintActivities();

    Console.WriteLine("Problem solved in " + solver.Iterations() +
                       " iterations");
    Console.WriteLine("x1: reduced cost = " + x1.ReducedCost());
    Console.WriteLine("x2: reduced cost = " + x2.ReducedCost());
    Console.WriteLine("x3: reduced cost = " + x3.ReducedCost());
    Console.WriteLine("c0: dual value = " + c0.DualValue());
    Console.WriteLine("    activity = " + activities[c0.Index()]);
    Console.WriteLine("c1: dual value = " + c1.DualValue());
    Console.WriteLine("    activity = " + activities[c1.Index()]);
    Console.WriteLine("c2: dual value = " + c2.DualValue());
    Console.WriteLine("    activity = " + activities[c2.Index()]);
  }

  private static void RunLinearProgrammingExampleNaturalApi(
      String solverType, bool printModel)
  {
    Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
    if (solver == null)
    {
      Console.WriteLine("Could not create solver " + solverType);
      return;
    }
    // x1, x2 and x3 are continuous non-negative variables.
    Variable x1 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x1");
    Variable x2 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x2");
    Variable x3 = solver.MakeNumVar(0.0, double.PositiveInfinity, "x3");

    solver.Maximize(10 * x1 + 6 * x2 + 4 * x3);
    Constraint c0 = solver.Add(x1 + x2 + x3 <= 100);
    Constraint c1 = solver.Add(10 * x1 + x2 * 4 + 5 * x3 <= 600);
    Constraint c2 = solver.Add(2 * x1 + 2 * x2 + 6 * x3 <= 300);

    Console.WriteLine("Number of variables = " + solver.NumVariables());
    Console.WriteLine("Number of constraints = " + solver.NumConstraints());

    if (printModel) {
      string model = solver.ExportModelAsLpFormat(false);
      Console.WriteLine(model);
    }

    Solver.ResultStatus resultStatus = solver.Solve();

    // Check that the problem has an optimal solution.
    if (resultStatus != Solver.ResultStatus.OPTIMAL) {
      Console.WriteLine("The problem does not have an optimal solution!");
      return;
    }

    Console.WriteLine("Problem solved in " + solver.WallTime() +
                      " milliseconds");

    // The objective value of the solution.
    Console.WriteLine("Optimal objective value = " +
                      solver.Objective().Value());

    // The value of each variable in the solution.
    Console.WriteLine("x1 = " + x1.SolutionValue());
    Console.WriteLine("x2 = " + x2.SolutionValue());
    Console.WriteLine("x3 = " + x3.SolutionValue());

    Console.WriteLine("Advanced usage:");
    double[] activities = solver.ComputeConstraintActivities();
    Console.WriteLine("Problem solved in " + solver.Iterations() +
                       " iterations");
    Console.WriteLine("x1: reduced cost = " + x1.ReducedCost());
    Console.WriteLine("x2: reduced cost = " + x2.ReducedCost());
    Console.WriteLine("x3: reduced cost = " + x3.ReducedCost());
    Console.WriteLine("c0: dual value = " + c0.DualValue());
    Console.WriteLine("    activity = " + activities[c0.Index()]);
    Console.WriteLine("c1: dual value = " + c1.DualValue());
    Console.WriteLine("    activity = " + activities[c1.Index()]);
    Console.WriteLine("c2: dual value = " + c2.DualValue());
    Console.WriteLine("    activity = " + activities[c2.Index()]);
  }

  static void Main()
  {
    Console.WriteLine("---- Linear programming example with GLOP ----");
    RunLinearProgrammingExample("GLOP_LINEAR_PROGRAMMING");
    Console.WriteLine("---- Linear programming example with GLPK ----");
    RunLinearProgrammingExample("GLPK_LINEAR_PROGRAMMING");
    Console.WriteLine("---- Linear programming example with CLP ----");
    RunLinearProgrammingExample("CLP_LINEAR_PROGRAMMING");
    Console.WriteLine(
        "---- Linear programming example (Natural API) with GLOP ----");
    RunLinearProgrammingExampleNaturalApi("GLOP_LINEAR_PROGRAMMING", true);
    Console.WriteLine(
        "---- Linear programming example (Natural API) with GLPK ----");
    RunLinearProgrammingExampleNaturalApi("GLPK_LINEAR_PROGRAMMING", false);
    Console.WriteLine(
        "---- Linear programming example (Natural API) with CLP ----");
    RunLinearProgrammingExampleNaturalApi("CLP_LINEAR_PROGRAMMING", false);
  }
}