2018-11-10 18:00:53 +01:00
|
|
|
// Copyright 2010-2018 Google LLC
|
2012-01-25 18:02:25 +00:00
|
|
|
// 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.
|
2014-07-09 15:18:27 +00:00
|
|
|
|
2012-01-25 18:02:25 +00:00
|
|
|
using System;
|
|
|
|
|
using Google.OrTools.LinearSolver;
|
|
|
|
|
|
|
|
|
|
public class CsIntegerProgramming
|
|
|
|
|
{
|
|
|
|
|
private static void RunIntegerProgrammingExample(String solverType)
|
|
|
|
|
{
|
2012-01-29 18:15:19 +00:00
|
|
|
Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
|
2012-01-25 18:02:25 +00:00
|
|
|
if (solver == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Could not create solver " + solverType);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// x1 and x2 are integer non-negative variables.
|
2012-01-29 18:15:19 +00:00
|
|
|
Variable x1 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x1");
|
|
|
|
|
Variable x2 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x2");
|
2012-01-25 18:02:25 +00:00
|
|
|
|
|
|
|
|
// Minimize x1 + 2 * x2.
|
2014-07-09 12:21:55 +00:00
|
|
|
Objective objective = solver.Objective();
|
|
|
|
|
objective.SetMinimization();
|
|
|
|
|
objective.SetCoefficient(x1, 1);
|
|
|
|
|
objective.SetCoefficient(x2, 2);
|
2012-01-25 18:02:25 +00:00
|
|
|
|
|
|
|
|
// 2 * x2 + 3 * x1 >= 17.
|
2012-01-29 18:15:19 +00:00
|
|
|
Constraint ct = solver.MakeConstraint(17, double.PositiveInfinity);
|
2012-01-25 18:02:25 +00:00
|
|
|
ct.SetCoefficient(x1, 3);
|
|
|
|
|
ct.SetCoefficient(x2, 2);
|
|
|
|
|
|
2018-12-11 17:03:03 +01:00
|
|
|
Solver.ResultStatus resultStatus = solver.Solve();
|
2012-01-25 18:02:25 +00:00
|
|
|
|
|
|
|
|
// Check that the problem has an optimal solution.
|
2018-12-11 17:03:03 +01:00
|
|
|
if (resultStatus != Solver.ResultStatus.OPTIMAL)
|
2012-01-25 18:02:25 +00:00
|
|
|
{
|
|
|
|
|
Console.WriteLine("The problem does not have an optimal solution!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Problem solved in " + solver.WallTime() +
|
2012-01-30 00:00:41 +00:00
|
|
|
" milliseconds");
|
2012-01-25 18:02:25 +00:00
|
|
|
|
|
|
|
|
// The objective value of the solution.
|
2014-07-09 12:21:55 +00:00
|
|
|
Console.WriteLine("Optimal objective value = " + objective.Value());
|
2012-01-25 18:02:25 +00:00
|
|
|
|
|
|
|
|
// The value of each variable in the solution.
|
|
|
|
|
Console.WriteLine("x1 = " + x1.SolutionValue());
|
|
|
|
|
Console.WriteLine("x2 = " + x2.SolutionValue());
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Advanced usage:");
|
|
|
|
|
Console.WriteLine("Problem solved in " + solver.Nodes() +
|
2012-01-30 00:00:41 +00:00
|
|
|
" branch-and-bound nodes");
|
2012-01-25 18:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-29 13:04:16 +00:00
|
|
|
private static void RunIntegerProgrammingExampleNaturalApi(String solverType)
|
|
|
|
|
{
|
2012-01-29 18:15:19 +00:00
|
|
|
Solver solver = Solver.CreateSolver("IntegerProgramming", solverType);
|
2012-01-29 13:04:16 +00:00
|
|
|
if (solver == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Could not create solver " + solverType);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// x1 and x2 are integer non-negative variables.
|
2012-01-29 18:15:19 +00:00
|
|
|
Variable x1 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x1");
|
|
|
|
|
Variable x2 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x2");
|
2012-01-29 13:04:16 +00:00
|
|
|
|
|
|
|
|
solver.Minimize(x1 + 2 * x2);
|
|
|
|
|
solver.Add(2 * x2 + 3 * x1 >= 17);
|
|
|
|
|
|
2018-12-11 17:03:03 +01:00
|
|
|
Solver.ResultStatus resultStatus = solver.Solve();
|
2012-01-29 13:04:16 +00:00
|
|
|
|
|
|
|
|
// Check that the problem has an optimal solution.
|
2018-12-11 17:03:03 +01:00
|
|
|
if (resultStatus != Solver.ResultStatus.OPTIMAL)
|
2012-01-29 13:04:16 +00:00
|
|
|
{
|
|
|
|
|
Console.WriteLine("The problem does not have an optimal solution!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Problem solved in " + solver.WallTime() +
|
2012-01-30 00:00:41 +00:00
|
|
|
" milliseconds");
|
2012-01-29 13:04:16 +00:00
|
|
|
|
|
|
|
|
// The objective value of the solution.
|
2014-01-17 19:04:26 +00:00
|
|
|
Console.WriteLine("Optimal objective value = " +
|
|
|
|
|
solver.Objective().Value());
|
2012-01-29 13:04:16 +00:00
|
|
|
|
|
|
|
|
// The value of each variable in the solution.
|
|
|
|
|
Console.WriteLine("x1 = " + x1.SolutionValue());
|
|
|
|
|
Console.WriteLine("x2 = " + x2.SolutionValue());
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Advanced usage:");
|
|
|
|
|
Console.WriteLine("Problem solved in " + solver.Nodes() +
|
2012-01-30 00:00:41 +00:00
|
|
|
" branch-and-bound nodes");
|
2012-01-29 13:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
2012-01-25 18:02:25 +00:00
|
|
|
static void Main()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("---- Integer programming example with GLPK ----");
|
|
|
|
|
RunIntegerProgrammingExample("GLPK_MIXED_INTEGER_PROGRAMMING");
|
|
|
|
|
Console.WriteLine("---- Linear programming example with CBC ----");
|
|
|
|
|
RunIntegerProgrammingExample("CBC_MIXED_INTEGER_PROGRAMMING");
|
|
|
|
|
Console.WriteLine("---- Linear programming example with SCIP ----");
|
|
|
|
|
RunIntegerProgrammingExample("SCIP_MIXED_INTEGER_PROGRAMMING");
|
2012-01-29 13:04:16 +00:00
|
|
|
Console.WriteLine(
|
2012-01-29 13:08:12 +00:00
|
|
|
"---- Integer programming example (Natural API) with GLPK ----");
|
2012-01-29 13:04:16 +00:00
|
|
|
RunIntegerProgrammingExampleNaturalApi("GLPK_MIXED_INTEGER_PROGRAMMING");
|
|
|
|
|
Console.WriteLine(
|
2012-01-29 13:08:12 +00:00
|
|
|
"---- Linear programming example (Natural API) with CBC ----");
|
2012-01-29 13:04:16 +00:00
|
|
|
RunIntegerProgrammingExampleNaturalApi("CBC_MIXED_INTEGER_PROGRAMMING");
|
|
|
|
|
Console.WriteLine(
|
2012-01-29 13:08:12 +00:00
|
|
|
"---- Linear programming example (Natural API) with SCIP ----");
|
2012-01-29 13:04:16 +00:00
|
|
|
RunIntegerProgrammingExampleNaturalApi("SCIP_MIXED_INTEGER_PROGRAMMING");
|
2012-01-25 18:02:25 +00:00
|
|
|
}
|
|
|
|
|
}
|