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