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