source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Algorithms/LinearProgrammingAlgorithm.cs @ 16373

Last change on this file since 16373 was 16373, checked in by ddorfmei, 5 months ago

#2931:

  • upgraded Google OR-Tools to version 6.10
  • added TextValue and TextValueView to be able to display and edit a multiline string
  • added parameter to set solver specific parameters for supported solvers
  • added support for the Protocol Buffers representation of models (import/export)
  • added import of MPS models
  • added pause/stop functionality to CplexSolver and GlpkSolver
  • refactored wrapper (LinearSolver and related enums)
  • added new algorithm category Exact for LinearProgrammingAlgorithm
File size: 7.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Threading;
24using Google.OrTools.LinearSolver;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers;
29using HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers.Base;
30using HeuristicLab.MathematicalOptimization.LinearProgramming.Problems;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34
35namespace HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms {
36
37  [Item("Linear/Mixed Integer Programming (LP/MIP)", "Linear/mixed integer programming implemented in several solvers. " +
38    "See also https://dev.heuristiclab.com/trac.fcgi/wiki/Documentation/Howto/LinearMixedIntegerProgramming")] // TODO: update link
39  [Creatable(CreatableAttribute.Categories.ExactAlgorithms)]
40  [StorableClass]
41  public class LinearProgrammingAlgorithm : BasicAlgorithm {
42
43    [Storable]
44    private readonly IFixedValueParameter<DoubleValue> dualToleranceParam;
45
46    [Storable]
47    private readonly IFixedValueParameter<EnumValue<LpAlgorithmValues>> lpAlgorithmParam;
48
49    [Storable]
50    private readonly IFixedValueParameter<BoolValue> presolveParam;
51
52    [Storable]
53    private readonly IFixedValueParameter<DoubleValue> primalToleranceParam;
54
55    [Storable]
56    private readonly IFixedValueParameter<PercentValue> relativeGapToleranceParam;
57
58    [Storable]
59    private readonly IFixedValueParameter<BoolValue> scalingParam;
60
61    [Storable]
62    private IConstrainedValueParameter<ISolver> solverParam;
63
64    [Storable]
65    private readonly IFixedValueParameter<TimeSpanValue> timeLimitParam;
66
67    public IConstrainedValueParameter<ISolver> SolverParameter {
68      get => solverParam;
69      set => solverParam = value;
70    }
71
72    public LinearProgrammingAlgorithm() {
73      Parameters.Add(solverParam =
74        new ConstrainedValueParameter<ISolver>(nameof(Solver), "The solver used to solve the model."));
75
76      ISolver defaultSolver;
77      solverParam.ValidValues.Add(new BopSolver());
78      solverParam.ValidValues.Add(defaultSolver = new CoinOrSolver());
79      solverParam.ValidValues.Add(new CplexSolver());
80      solverParam.ValidValues.Add(new GlopSolver());
81      solverParam.ValidValues.Add(new GlpkSolver());
82      solverParam.ValidValues.Add(new GurobiSolver());
83      solverParam.ValidValues.Add(new ScipSolver());
84      solverParam.Value = defaultSolver;
85
86      Parameters.Add(relativeGapToleranceParam = new FixedValueParameter<PercentValue>(nameof(RelativeGapTolerance),
87        "Limit for relative MIP gap.", new PercentValue(MPSolverParameters.kDefaultRelativeMipGap)));
88      Parameters.Add(timeLimitParam = new FixedValueParameter<TimeSpanValue>(nameof(TimeLimit),
89        "Limit for runtime. Set to zero for unlimited runtime.", new TimeSpanValue()));
90      Parameters.Add(presolveParam =
91        new FixedValueParameter<BoolValue>(nameof(Presolve), "Advanced usage: presolve mode.", new BoolValue()));
92      Parameters.Add(lpAlgorithmParam = new FixedValueParameter<EnumValue<LpAlgorithmValues>>(nameof(LpAlgorithm),
93        "Algorithm to solve linear programs.", new EnumValue<LpAlgorithmValues>(LpAlgorithmValues.DualSimplex)));
94      Parameters.Add(dualToleranceParam = new FixedValueParameter<DoubleValue>(nameof(DualTolerance),
95        "Advanced usage: tolerance for dual feasibility of basic solutions.",
96        new DoubleValue(MPSolverParameters.kDefaultDualTolerance)));
97      Parameters.Add(primalToleranceParam = new FixedValueParameter<DoubleValue>(nameof(PrimalTolerance),
98        "Advanced usage: tolerance for primal feasibility of basic solutions. " +
99        "This does not control the integer feasibility tolerance of integer " +
100        "solutions for MIP or the tolerance used during presolve.",
101        new DoubleValue(MPSolverParameters.kDefaultPrimalTolerance)));
102      Parameters.Add(scalingParam = new FixedValueParameter<BoolValue>(nameof(Scaling),
103        "Advanced usage: enable or disable matrix scaling.", new BoolValue()));
104
105      Problem = new LinearProgrammingProblem();
106    }
107
108    [StorableConstructor]
109    protected LinearProgrammingAlgorithm(bool deserializing)
110      : base(deserializing) {
111    }
112
113    protected LinearProgrammingAlgorithm(LinearProgrammingAlgorithm original, Cloner cloner)
114      : base(original, cloner) {
115      solverParam = cloner.Clone(original.solverParam);
116      relativeGapToleranceParam = cloner.Clone(original.relativeGapToleranceParam);
117      timeLimitParam = cloner.Clone(original.timeLimitParam);
118      presolveParam = cloner.Clone(original.presolveParam);
119      lpAlgorithmParam = cloner.Clone(original.lpAlgorithmParam);
120      dualToleranceParam = cloner.Clone(original.dualToleranceParam);
121      primalToleranceParam = cloner.Clone(original.primalToleranceParam);
122      scalingParam = cloner.Clone(original.scalingParam);
123    }
124
125    public double DualTolerance {
126      get => dualToleranceParam.Value.Value;
127      set => dualToleranceParam.Value.Value = value;
128    }
129
130    public LpAlgorithmValues LpAlgorithm {
131      get => lpAlgorithmParam.Value.Value;
132      set => lpAlgorithmParam.Value.Value = value;
133    }
134
135    public bool Presolve {
136      get => presolveParam.Value.Value;
137      set => presolveParam.Value.Value = value;
138    }
139
140    public double PrimalTolerance {
141      get => primalToleranceParam.Value.Value;
142      set => primalToleranceParam.Value.Value = value;
143    }
144
145    public new LinearProgrammingProblem Problem {
146      get => (LinearProgrammingProblem)base.Problem;
147      set => base.Problem = value;
148    }
149
150    public override Type ProblemType { get; } = typeof(LinearProgrammingProblem);
151
152    public double RelativeGapTolerance {
153      get => relativeGapToleranceParam.Value.Value;
154      set => relativeGapToleranceParam.Value.Value = value;
155    }
156
157    public override ResultCollection Results { get; } = new ResultCollection();
158
159    public bool Scaling {
160      get => scalingParam.Value.Value;
161      set => scalingParam.Value.Value = value;
162    }
163
164    public ISolver Solver {
165      get => solverParam.Value;
166      set => solverParam.Value = value;
167    }
168
169    public override bool SupportsPause => Solver.SupportsPause;
170
171    public override bool SupportsStop => Solver.SupportsStop;
172
173    public TimeSpan TimeLimit {
174      get => timeLimitParam.Value.Value;
175      set => timeLimitParam.Value.Value = value;
176    }
177
178    public override IDeepCloneable Clone(Cloner cloner) => new LinearProgrammingAlgorithm(this, cloner);
179
180    public override void Pause() {
181      base.Pause();
182      Solver.InterruptSolve();
183    }
184
185    public override void Prepare() {
186      base.Prepare();
187      Results.Clear();
188
189      foreach (var solver in solverParam.ValidValues) {
190        solver.Reset();
191      }
192    }
193
194    public override void Stop() {
195      base.Stop();
196      Solver.InterruptSolve();
197    }
198
199    protected override void Run(CancellationToken cancellationToken) => Solver.Solve(this, cancellationToken);
200  }
201}
Note: See TracBrowser for help on using the repository browser.