source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Templates/CompiledLinearProgrammingProblemDefinition.cs @ 16405

Last change on this file since 16405 was 16405, checked in by ddorfmei, 3 months ago

#2931:

  • moved views to separate plugin HeuristicLab.MathematicalOptimization.Views
  • added button in LinearProgrammingProblemView to select the problem definition type
  • added views for problem definitions
  • added ExportFile parameter to LinearProgrammingAlgorithm
  • extended FileValue and FileValueView by the option to save a file
  • code cleanup
File size: 2.1 KB
Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using Google.OrTools.LinearSolver;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Optimization;
9using Variable = Google.OrTools.LinearSolver.Variable;
10
11namespace HeuristicLab.MathematicalOptimization.LinearProgramming {
12
13  public class CompiledLinearProgrammingProblemDefinition : CompiledProblemDefinition, ILinearProgrammingProblemDefinition {
14    private Variable x;
15    private Variable y;
16
17    public override void Initialize() {
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
20    }
21
22    public void BuildModel(Solver solver) {
23      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
24      // How to define a model using Google OR-Tools: https://developers.google.com/optimization/introduction/cs
25      // Example model taken from https://developers.google.com/optimization/mip/integer_opt
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);
33    }
34
35    public void Analyze(Solver solver, ResultCollection results) {
36      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
37      // Write or update results given the solution values of the decision variables
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()));
43    }
44
45    // Implement further classes and methods
46  }
47}
48
Note: See TracBrowser for help on using the repository browser.