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