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;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.MathematicalOptimization.LinearProgramming.Problems;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 | using HeuristicLab.Problems.Programmable;
|
---|
11 | using Variable = Google.OrTools.LinearSolver.Variable;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.MathematicalOptimization.LinearProgramming {
|
---|
14 |
|
---|
15 | public class CompiledLinearProgrammingProblemDefinition : CompiledProblemDefinition, ILinearProgrammingProblemDefinition {
|
---|
16 | private Variable x;
|
---|
17 | private Variable y;
|
---|
18 |
|
---|
19 | public override void Initialize() {
|
---|
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
|
---|
22 | }
|
---|
23 |
|
---|
24 | public void BuildModel(Solver solver) {
|
---|
25 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
26 | // Example model taken from https://developers.google.com/optimization/mip/integer_opt
|
---|
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);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void Analyze(Solver solver, ResultCollection results) {
|
---|
37 | // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
|
---|
38 | // Write or update results given the solution variables of the decision variables
|
---|
39 | results.AddOrUpdateResult("x", new DoubleValue(x.SolutionValue()));
|
---|
40 | results.AddOrUpdateResult("y", new DoubleValue(y.SolutionValue()));
|
---|
41 | // The decision variables can also be retrieved from the solver
|
---|
42 | //results.AddOrUpdateResult("x", new DoubleValue(solver.LookupVariableOrNull("x").SolutionValue()));
|
---|
43 | //results.AddOrUpdateResult("y", new DoubleValue(solver.LookupVariableOrNull("y").SolutionValue()));
|
---|
44 | }
|
---|
45 |
|
---|
46 | // Implement further classes and methods
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|