Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/21/18 09:50:12 (6 years ago)
Author:
ddorfmei
Message:

#2931:

  • created LinearProgrammingAlgorithm
    • created definitions for all LP/MIP solvers supported by OR-Tools
  • created LinearProgrammingProblem
    • created classes required for scripting: LinearProgrammingProblemDefinition, LinearProgrammingProblemDefinitionScript, CompiledLinearProgrammingProblemDefinition
    • created views: LinearProgrammingProblemView, LinearProgrammingProblemDefinitionScriptView
  • updated OR-Tools version in ExtLibs to 6.9
Location:
branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Templates
Files:
2 copied

Legend:

Unmodified
Added
Removed
  • branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Templates/CompiledLinearProgrammingProblemDefinition.cs

    r16138 r16172  
    1 using System;
    2 using System.Linq;
    3 using System.Collections.Generic;
    4 using HeuristicLab.Common;
    5 using HeuristicLab.Core;
     1using Google.OrTools.LinearSolver;
    62using HeuristicLab.Data;
    7 using HeuristicLab.Encodings.BinaryVectorEncoding;
    8 using HeuristicLab.Encodings.IntegerVectorEncoding;
    9 using HeuristicLab.Encodings.RealVectorEncoding;
    10 using HeuristicLab.Encodings.PermutationEncoding;
    11 using HeuristicLab.Encodings.LinearLinkageEncoding;
    12 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     3using HeuristicLab.MathematicalOptimization.LinearProgramming.Problems;
    134using HeuristicLab.Optimization;
    145using HeuristicLab.Problems.Programmable;
    156
    16 namespace HeuristicLab.Problems.Programmable {
    17   public class CompiledSingleObjectiveProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition {
    18     public bool Maximization { get { return false; } }
     7namespace HeuristicLab.MathematicalOptimization.LinearProgramming {
     8  public class CompiledLinearProgrammingProblemDefinition : CompiledProblemDefinition, ILinearProgrammingProblemDefinition {
     9
     10    private Variable x;
     11    private Variable y;
    1912
    2013    public override void Initialize() {
    2114      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    22       // Define the solution encoding which can also consist of multiple vectors, examples below
    23       //Encoding = new BinaryVectorEncoding("b", length: 5);
    24       //Encoding = new IntegerVectorEncoding("i", length: 5, min: 2, max: 14, step: 2);
    25       //Encoding = new RealVectorEncoding("r", length: 5, min: -1.0, max: 1.0);
    26       //Encoding = new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute);
    27       //Encoding = new LinearLinkageEncoding("l", length: 5);
    28       //Encoding = new SymbolicExpressionTreeEncoding("s", new SimpleSymbolicExpressionGrammar(), 50, 12);
    29       // The encoding can also be a combination
    30       //Encoding = new MultiEncoding()
    31       //.Add(new BinaryVectorEncoding("b", length: 5))
    32       //.Add(new IntegerVectorEncoding("i", length: 5, min: 2, max: 14, step: 4))
    33       //.Add(new RealVectorEncoding("r", length: 5, min: -1.0, max: 1.0))
    34       //.Add(new PermutationEncoding("p", length: 5, type: PermutationTypes.Absolute))
    35       //.Add(new LinearLinkageEncoding("l", length: 5))
    36       //.Add(new SymbolicExpressionTreeEncoding("s", new SimpleSymbolicExpressionGrammar(), 50, 12))
    37       ;
    3815      // Add additional initialization code e.g. private variables that you need for evaluating
    3916    }
    4017
    41     public double Evaluate(Individual individual, IRandom random) {
     18    public void BuildModel(Solver solver) {
    4219      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    43       var quality = 0.0;
    44       //quality = individual.RealVector("r").Sum(x => x * x);
    45       return quality;
     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);
    4628    }
    4729
    48     public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     30    public void Analyze(Solver solver, ResultCollection results) {
    4931      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    50       // Write or update results given the range of vectors and resulting qualities
    51       // Uncomment the following lines if you want to retrieve the best individual
    52 
    53       //var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
    54       //var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
    55 
    56       //if (!results.ContainsKey("Best Solution")) {
    57       //  results.Add(new Result("Best Solution", typeof(RealVector)));
    58       //}
    59       //results["Best Solution"].Value = (IItem)best.RealVector("r").Clone();
    60     }
    61 
    62     public IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
    63       // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    64       // Create new vectors, based on the given one that represent small changes
    65       // This method is only called from move-based algorithms (Local Search, Simulated Annealing, etc.)
    66       while (true) {
    67         // Algorithm will draw only a finite amount of samples
    68         // Change to a for-loop to return a concrete amount of neighbors
    69         var neighbor = individual.Copy();
    70         // For instance, perform a single bit-flip in a binary parameter
    71         //var bIndex = random.Next(neighbor.BinaryVector("b").Length);
    72         //neighbor.BinaryVector("b")[bIndex] = !neighbor.BinaryVector("b")[bIndex];
    73         yield return neighbor;
    74       }
     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()));
    7538    }
    7639
     
    7841  }
    7942}
    80 
Note: See TracChangeset for help on using the changeset viewer.