Files
ortools-clone/ortools/linear_solver/samples/LinearProgrammingExample.cs

82 lines
2.6 KiB
C#
Raw Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 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.
// [START program]
// [START import]
using System;
using Google.OrTools.LinearSolver;
// [END import]
2020-11-03 10:04:19 +01:00
public class LinearProgrammingExample
{
static void Main()
{
// [START solver]
Solver solver = Solver.CreateSolver("GLOP");
if (solver is null)
{
return;
}
2020-11-03 10:04:19 +01:00
// [END solver]
// x and y are continuous non-negative variables.
// [START variables]
Variable x = solver.MakeNumVar(0.0, double.PositiveInfinity, "x");
Variable y = solver.MakeNumVar(0.0, double.PositiveInfinity, "y");
2020-12-07 14:57:58 +01:00
Console.WriteLine("Number of variables = " + solver.NumVariables());
2020-11-03 10:04:19 +01:00
// [END variables]
2020-11-03 10:04:19 +01:00
// [START constraints]
// x + 2y <= 14.
2020-12-09 16:46:40 +01:00
solver.Add(x + 2 * y <= 14.0);
2020-11-03 10:04:19 +01:00
// 3x - y >= 0.
2020-12-09 16:46:40 +01:00
solver.Add(3 * x - y >= 0.0);
2020-11-03 10:04:19 +01:00
// x - y <= 2.
2020-12-09 16:46:40 +01:00
solver.Add(x - y <= 2.0);
2020-12-07 14:57:58 +01:00
Console.WriteLine("Number of constraints = " + solver.NumConstraints());
2020-11-03 10:04:19 +01:00
// [END constraints]
2020-11-03 10:04:19 +01:00
// [START objective]
// Objective function: 3x + 4y.
2020-12-09 16:46:40 +01:00
solver.Maximize(3 * x + 4 * y);
2020-11-03 10:04:19 +01:00
// [END objective]
2020-11-03 10:04:19 +01:00
// [START solve]
2020-12-07 14:57:58 +01:00
Solver.ResultStatus resultStatus = solver.Solve();
2020-11-03 10:04:19 +01:00
// [END solve]
2020-11-03 10:04:19 +01:00
// [START print_solution]
2020-12-07 14:57:58 +01:00
// Check that the problem has an optimal solution.
if (resultStatus != Solver.ResultStatus.OPTIMAL)
{
Console.WriteLine("The problem does not have an optimal solution!");
return;
}
2020-11-03 10:04:19 +01:00
Console.WriteLine("Solution:");
2020-12-07 14:57:58 +01:00
Console.WriteLine("Objective value = " + solver.Objective().Value());
2020-11-03 10:04:19 +01:00
Console.WriteLine("x = " + x.SolutionValue());
Console.WriteLine("y = " + y.SolutionValue());
// [END print_solution]
2020-12-07 14:57:58 +01:00
// [START advanced]
Console.WriteLine("\nAdvanced usage:");
Console.WriteLine("Problem solved in " + solver.WallTime() + " milliseconds");
Console.WriteLine("Problem solved in " + solver.Iterations() + " iterations");
// [END advanced]
2020-11-03 10:04:19 +01:00
}
}
// [END program]