Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/17/18 17:51:26 (6 years ago)
Author:
ddorfmei
Message:

#2931:

  • added all available parameters OR-Tools's linear_solver to LinearProgrammingAlgorithm
    • added necessary parameter enums
  • moved solving logic to Solver
    • created IncrementalSolver, ExternalSolver, ExternalIncrementalSolver
    • added logic for solvers that can be stopped and resumed
  • added SupportsStop property to BasicAlgorithm
  • added quality per time chart for incremental solvers
File:
1 edited

Legend:

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

    r16172 r16233  
    11using System;
    22using System.Threading;
     3using Google.OrTools.LinearSolver;
    34using HeuristicLab.Common;
    45using HeuristicLab.Core;
    56using HeuristicLab.Data;
     7using HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers;
     8using HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers.Base;
    69using HeuristicLab.MathematicalOptimization.LinearProgramming.Problems;
    710using HeuristicLab.Optimization;
     
    1013
    1114namespace HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms {
    12   [Item("Linear Programming Algorithm", "")]
    13   [Creatable(CreatableAttribute.Categories.Algorithms, Priority = 162)]
     15
     16  [Item("Linear/Mixed Integer Programming (LP/MIP)", "")]
     17  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
    1418  [StorableClass]
    1519  public class LinearProgrammingAlgorithm : BasicAlgorithm {
     20
     21    [Storable]
     22    private readonly IFixedValueParameter<DoubleValue> dualToleranceParam;
     23
     24    [Storable]
     25    private readonly IFixedValueParameter<EnumValue<LpAlgorithmValues>> lpAlgorithmParam;
     26
     27    [Storable]
     28    private readonly IFixedValueParameter<BoolValue> presolveParam;
     29
     30    [Storable]
     31    private readonly IFixedValueParameter<DoubleValue> primalToleranceParam;
     32
     33    [Storable]
     34    private readonly IFixedValueParameter<DoubleValue> relativeGapToleranceParam;
     35
     36    [Storable]
     37    private readonly IFixedValueParameter<BoolValue> scalingParam;
    1638
    1739    [Storable]
     
    1941
    2042    [Storable]
    21     private readonly IFixedValueParameter<DoubleValue> relativeGapToleranceParam;
    22 
    23     [Storable]
    2443    private readonly IFixedValueParameter<TimeSpanValue> timeLimitParam;
    2544
    2645    public LinearProgrammingAlgorithm() {
    27       Parameters.Add(solverParam = new ConstrainedValueParameter<ISolver>(nameof(Solver)));
    28       Parameters.Add(relativeGapToleranceParam = new FixedValueParameter<DoubleValue>(nameof(RelativeGapTolerance), new DoubleValue()));
    29       Parameters.Add(timeLimitParam = new FixedValueParameter<TimeSpanValue>(nameof(TimeLimit), new TimeSpanValue()));
     46      Parameters.Add(solverParam =
     47        new ConstrainedValueParameter<ISolver>(nameof(Solver), "The solver used to solve the model."));
     48      Parameters.Add(relativeGapToleranceParam = new FixedValueParameter<DoubleValue>(nameof(RelativeGapTolerance),
     49        "Limit for relative MIP gap.", new DoubleValue(MPSolverParameters.kDefaultRelativeMipGap)));
     50      Parameters.Add(timeLimitParam = new FixedValueParameter<TimeSpanValue>(nameof(TimeLimit),
     51        "Limit for runtime. Set to zero for unlimited runtime.", new TimeSpanValue()));
     52      Parameters.Add(presolveParam =
     53        new FixedValueParameter<BoolValue>(nameof(Presolve), "Advanced usage: presolve mode.", new BoolValue()));
     54      Parameters.Add(lpAlgorithmParam = new FixedValueParameter<EnumValue<LpAlgorithmValues>>(nameof(LpAlgorithm),
     55        "Algorithm to solve linear programs.", new EnumValue<LpAlgorithmValues>(LpAlgorithmValues.DualSimplex)));
     56      Parameters.Add(dualToleranceParam = new FixedValueParameter<DoubleValue>(nameof(DualTolerance),
     57        "Advanced usage: tolerance for dual feasibility of basic solutions.",
     58        new DoubleValue(MPSolverParameters.kDefaultDualTolerance)));
     59      Parameters.Add(primalToleranceParam = new FixedValueParameter<DoubleValue>(nameof(PrimalTolerance),
     60        "Advanced usage: tolerance for primal feasibility of basic solutions. " +
     61        "This does not control the integer feasibility tolerance of integer " +
     62        "solutions for MIP or the tolerance used during presolve.",
     63        new DoubleValue(MPSolverParameters.kDefaultPrimalTolerance)));
     64      Parameters.Add(scalingParam = new FixedValueParameter<BoolValue>(nameof(Scaling),
     65        "Advanced usage: enable or disable matrix scaling.", new BoolValue()));
    3066
    3167      Problem = new LinearProgrammingProblem();
     
    4278    [StorableConstructor]
    4379    private LinearProgrammingAlgorithm(bool deserializing)
    44       : base(deserializing) { }
     80      : base(deserializing) {
     81    }
    4582
    4683    private LinearProgrammingAlgorithm(LinearProgrammingAlgorithm original, Cloner cloner)
     
    4986      relativeGapToleranceParam = cloner.Clone(original.relativeGapToleranceParam);
    5087      timeLimitParam = cloner.Clone(original.timeLimitParam);
     88      presolveParam = cloner.Clone(original.presolveParam);
     89      lpAlgorithmParam = cloner.Clone(original.lpAlgorithmParam);
     90      dualToleranceParam = cloner.Clone(original.dualToleranceParam);
     91      primalToleranceParam = cloner.Clone(original.primalToleranceParam);
     92      scalingParam = cloner.Clone(original.scalingParam);
    5193    }
    5294
    53     public ISolver Solver {
    54       get => solverParam.Value;
    55       set => solverParam.Value = value;
     95    public double DualTolerance {
     96      get => dualToleranceParam.Value.Value;
     97      set => dualToleranceParam.Value.Value = value;
     98    }
     99
     100    public LpAlgorithmValues LpAlgorithm {
     101      get => lpAlgorithmParam.Value.Value;
     102      set => lpAlgorithmParam.Value.Value = value;
     103    }
     104
     105    public bool Presolve {
     106      get => presolveParam.Value.Value;
     107      set => presolveParam.Value.Value = value;
     108    }
     109
     110    public double PrimalTolerance {
     111      get => primalToleranceParam.Value.Value;
     112      set => primalToleranceParam.Value.Value = value;
    56113    }
    57114
     
    62119
    63120    public override Type ProblemType { get; } = typeof(LinearProgrammingProblem);
     121
    64122    public double RelativeGapTolerance {
    65123      get => relativeGapToleranceParam.Value.Value;
     
    69127    public override ResultCollection Results { get; } = new ResultCollection();
    70128
    71     public override bool SupportsPause { get; } = false;
     129    public bool Scaling {
     130      get => scalingParam.Value.Value;
     131      set => scalingParam.Value.Value = value;
     132    }
     133
     134    public ISolver Solver {
     135      get => solverParam.Value;
     136      set => solverParam.Value = value;
     137    }
     138
     139    public override bool SupportsPause => Solver.SupportsPause;
     140
     141    public override bool SupportsStop => Solver.SupportsStop;
    72142
    73143    public TimeSpan TimeLimit {
     
    77147
    78148    public override IDeepCloneable Clone(Cloner cloner) => new LinearProgrammingAlgorithm(this, cloner);
    79     protected override void Run(CancellationToken cancellationToken) {
    80       using (var solver = LinearSolver.CreateSolver(Solver.OptimizationProblemType, Name,
    81         Solver.LibraryName ?? string.Empty, s => Problem.ProblemDefinition.BuildModel(s))) {
    82         solver.RelativeGapTolerance = RelativeGapTolerance;
    83         solver.TimeLimit = TimeLimit;
    84149
    85         solver.Solve();
     150    public override void Pause() {
     151      base.Pause();
     152      Solver.Interrupt();
     153    }
    86154
    87         Problem.ProblemDefinition.Analyze(solver.Solver, Results);
    88         Results.AddOrUpdateResult("Result Status", new EnumValue<ResultStatus>(solver.ResultStatus));
    89         Results.AddOrUpdateResult("Best Objective Value", new DoubleValue(solver.ObjectiveValue ?? double.NaN));
    90         Results.AddOrUpdateResult("Absolute Gap", new DoubleValue(solver.AbsoluteGap ?? double.NaN));
    91         Results.AddOrUpdateResult("Relative Gap", new DoubleValue(solver.RelativeGap ?? double.NaN));
    92         Results.AddOrUpdateResult("Number of Constraints", new IntValue(solver.NumberOfConstraints));
    93         Results.AddOrUpdateResult("Number of Variables", new IntValue(solver.NumberOfVariables));
    94         Results.AddOrUpdateResult("Number of Nodes", new DoubleValue(solver.NumberOfNodes));
    95         Results.AddOrUpdateResult("Iterations", new DoubleValue(solver.Iterations));
     155    public override void Prepare() {
     156      base.Prepare();
     157      Results.Clear();
     158
     159      foreach (var solver in solverParam.ValidValues) {
     160        solver.Reset();
    96161      }
    97162    }
     163
     164    public override void Stop() {
     165      base.Stop();
     166      Solver.Interrupt();
     167    }
     168    protected override void Initialize(CancellationToken cancellationToken) {
     169      base.Initialize(cancellationToken);
     170    }
     171    protected override void Run(CancellationToken cancellationToken) => Solver.Solve(this, cancellationToken);
    98172  }
    99173}
Note: See TracChangeset for help on using the changeset viewer.