[16288] | 1 | using System; |
---|
| 2 | using System.Linq; |
---|
| 3 | using System.Collections.Generic; |
---|
| 4 | using Google.OrTools.LinearSolver; |
---|
| 5 | using HeuristicLab.Common; |
---|
| 6 | using HeuristicLab.Core; |
---|
[11753] | 7 | using HeuristicLab.Data; |
---|
[16172] | 8 | using HeuristicLab.MathematicalOptimization.LinearProgramming.Problems; |
---|
[11753] | 9 | using HeuristicLab.Optimization; |
---|
| 10 | using HeuristicLab.Problems.Programmable; |
---|
[16288] | 11 | using Variable = Google.OrTools.LinearSolver.Variable; |
---|
[11753] | 12 | |
---|
[16172] | 13 | namespace HeuristicLab.MathematicalOptimization.LinearProgramming { |
---|
[16233] | 14 | |
---|
[16172] | 15 | public class CompiledLinearProgrammingProblemDefinition : CompiledProblemDefinition, ILinearProgrammingProblemDefinition { |
---|
| 16 | private Variable x; |
---|
| 17 | private Variable y; |
---|
[16373] | 18 | |
---|
[11753] | 19 | public override void Initialize() { |
---|
[11880] | 20 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable |
---|
| 21 | // Add additional initialization code e.g. private variables that you need for evaluating |
---|
[11753] | 22 | } |
---|
[16373] | 23 | |
---|
| 24 | public bool BuildModel(Solver solver) { |
---|
[11880] | 25 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable |
---|
[16233] | 26 | // Example model taken from https://developers.google.com/optimization/mip/integer_opt |
---|
[16172] | 27 | // Define the decision variables |
---|
| 28 | x = solver.MakeIntVar(0, 3.5, "x"); |
---|
| 29 | y = solver.MakeIntVar(0, double.PositiveInfinity, "y"); |
---|
| 30 | // Define the constraints |
---|
| 31 | solver.Add(x + 7 * y <= 17.5); |
---|
| 32 | // Define the objective |
---|
| 33 | solver.Maximize(x + 10 * y); |
---|
[16373] | 34 | return true; |
---|
[11753] | 35 | } |
---|
[16373] | 36 | |
---|
[16172] | 37 | public void Analyze(Solver solver, ResultCollection results) { |
---|
[11880] | 38 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable |
---|
[16172] | 39 | // Write or update results given the solution variables of the decision variables |
---|
| 40 | results.AddOrUpdateResult("x", new DoubleValue(x.SolutionValue())); |
---|
| 41 | results.AddOrUpdateResult("y", new DoubleValue(y.SolutionValue())); |
---|
| 42 | // The decision variables can also be retrieved from the solver |
---|
| 43 | //results.AddOrUpdateResult("x", new DoubleValue(solver.LookupVariableOrNull("x").SolutionValue())); |
---|
| 44 | //results.AddOrUpdateResult("y", new DoubleValue(solver.LookupVariableOrNull("y").SolutionValue())); |
---|
[11753] | 45 | } |
---|
[16373] | 46 | |
---|
[11880] | 47 | // Implement further classes and methods |
---|
[11753] | 48 | } |
---|
[16288] | 49 | } |
---|
| 50 | |
---|