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

Last change on this file since 16405 was 16405, checked in by ddorfmei, 5 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: 9.3 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.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.MathematicalOptimization.LinearProgramming {
33
34  [Item("Linear/Mixed Integer Programming (LP/MIP)", "Linear/mixed integer programming implemented in several solvers. " +
35    "See also https://dev.heuristiclab.com/trac.fcgi/wiki/Documentation/Howto/LinearMixedIntegerProgramming")] // TODO: update link
36  [Creatable(CreatableAttribute.Categories.ExactAlgorithms)]
37  [StorableClass]
38  public class LinearProgrammingAlgorithm : BasicAlgorithm {
39
40    [Storable]
41    private readonly IFixedValueParameter<DoubleValue> dualToleranceParam;
42
43    [Storable]
44    private readonly IFixedValueParameter<FileValue> exportModelParam;
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 readonly IFixedValueParameter<TimeSpanValue> timeLimitParam;
63
64    [Storable]
65    private IConstrainedValueParameter<ILinearSolver> linearSolverParam;
66
67    public LinearProgrammingAlgorithm() {
68      Parameters.Add(linearSolverParam =
69        new ConstrainedValueParameter<ILinearSolver>(nameof(LinearSolver), "The solver used to solve the model."));
70
71      ILinearSolver defaultSolver;
72      linearSolverParam.ValidValues.Add(new BopSolver());
73      linearSolverParam.ValidValues.Add(defaultSolver = new CoinOrSolver());
74      linearSolverParam.ValidValues.Add(new CplexSolver());
75      linearSolverParam.ValidValues.Add(new GlopSolver());
76      linearSolverParam.ValidValues.Add(new GlpkSolver());
77      linearSolverParam.ValidValues.Add(new GurobiSolver());
78      linearSolverParam.ValidValues.Add(new ScipSolver());
79      linearSolverParam.Value = defaultSolver;
80
81      Parameters.Add(relativeGapToleranceParam = new FixedValueParameter<PercentValue>(nameof(RelativeGapTolerance),
82        "Limit for relative MIP gap.", new PercentValue(MPSolverParameters.kDefaultRelativeMipGap)));
83      Parameters.Add(timeLimitParam = new FixedValueParameter<TimeSpanValue>(nameof(TimeLimit),
84        "Limit for runtime. Set to zero for unlimited runtime.", new TimeSpanValue()));
85      Parameters.Add(presolveParam =
86        new FixedValueParameter<BoolValue>(nameof(Presolve), "Advanced usage: presolve mode.", new BoolValue()));
87      Parameters.Add(lpAlgorithmParam = new FixedValueParameter<EnumValue<LpAlgorithmValues>>(nameof(LpAlgorithm),
88        "Algorithm to solve linear programs.", new EnumValue<LpAlgorithmValues>(LpAlgorithmValues.DualSimplex)));
89      Parameters.Add(dualToleranceParam = new FixedValueParameter<DoubleValue>(nameof(DualTolerance),
90        "Advanced usage: tolerance for dual feasibility of basic solutions.",
91        new DoubleValue(MPSolverParameters.kDefaultDualTolerance)));
92      Parameters.Add(primalToleranceParam = new FixedValueParameter<DoubleValue>(nameof(PrimalTolerance),
93        "Advanced usage: tolerance for primal feasibility of basic solutions. " +
94        "This does not control the integer feasibility tolerance of integer " +
95        "solutions for MIP or the tolerance used during presolve.",
96        new DoubleValue(MPSolverParameters.kDefaultPrimalTolerance)));
97      Parameters.Add(scalingParam = new FixedValueParameter<BoolValue>(nameof(Scaling),
98        "Advanced usage: enable or disable matrix scaling.", new BoolValue()));
99      Parameters.Add(exportModelParam =
100        new FixedValueParameter<FileValue>(nameof(ExportModel),
101          "Path of the file the model should be exported to. Run the algorithm to export the model.",
102          new FileValue {
103            SaveFile = true,
104            FileDialogFilter = "CPLEX LP File (*.lp)|*.lp|" +
105                               "Mathematical Programming System File (*.mps)|*.mps|" +
106                               "Google OR-Tools Protocol Buffers Text File (*.prototxt)|*.prototxt|" +
107                               "Google OR-Tools Protocol Buffers Binary File (*.bin)|*.bin"
108          }));
109
110      Problem = new LinearProgrammingProblem();
111    }
112
113    [StorableConstructor]
114    protected LinearProgrammingAlgorithm(bool deserializing)
115      : base(deserializing) {
116    }
117
118    protected LinearProgrammingAlgorithm(LinearProgrammingAlgorithm original, Cloner cloner)
119      : base(original, cloner) {
120      linearSolverParam = cloner.Clone(original.linearSolverParam);
121      relativeGapToleranceParam = cloner.Clone(original.relativeGapToleranceParam);
122      timeLimitParam = cloner.Clone(original.timeLimitParam);
123      presolveParam = cloner.Clone(original.presolveParam);
124      lpAlgorithmParam = cloner.Clone(original.lpAlgorithmParam);
125      dualToleranceParam = cloner.Clone(original.dualToleranceParam);
126      primalToleranceParam = cloner.Clone(original.primalToleranceParam);
127      scalingParam = cloner.Clone(original.scalingParam);
128      exportModelParam = cloner.Clone(original.exportModelParam);
129    }
130
131    public double DualTolerance {
132      get => dualToleranceParam.Value.Value;
133      set => dualToleranceParam.Value.Value = value;
134    }
135
136    public string ExportModel {
137      get => exportModelParam.Value.Value;
138      set => exportModelParam.Value.Value = value;
139    }
140
141    public ILinearSolver LinearSolver {
142      get => linearSolverParam.Value;
143      set => linearSolverParam.Value = value;
144    }
145
146    public IConstrainedValueParameter<ILinearSolver> LinearSolverParameter {
147      get => linearSolverParam;
148      set => linearSolverParam = value;
149    }
150
151    public LpAlgorithmValues LpAlgorithm {
152      get => lpAlgorithmParam.Value.Value;
153      set => lpAlgorithmParam.Value.Value = value;
154    }
155
156    public bool Presolve {
157      get => presolveParam.Value.Value;
158      set => presolveParam.Value.Value = value;
159    }
160
161    public double PrimalTolerance {
162      get => primalToleranceParam.Value.Value;
163      set => primalToleranceParam.Value.Value = value;
164    }
165
166    public new LinearProgrammingProblem Problem {
167      get => (LinearProgrammingProblem)base.Problem;
168      set => base.Problem = value;
169    }
170
171    public override Type ProblemType { get; } = typeof(LinearProgrammingProblem);
172
173    public double RelativeGapTolerance {
174      get => relativeGapToleranceParam.Value.Value;
175      set => relativeGapToleranceParam.Value.Value = value;
176    }
177
178    public override ResultCollection Results { get; } = new ResultCollection();
179
180    public bool Scaling {
181      get => scalingParam.Value.Value;
182      set => scalingParam.Value.Value = value;
183    }
184
185    public override bool SupportsPause => LinearSolver.SupportsPause;
186
187    public override bool SupportsStop => LinearSolver.SupportsStop;
188
189    public TimeSpan TimeLimit {
190      get => timeLimitParam.Value.Value;
191      set => timeLimitParam.Value.Value = value;
192    }
193
194    public override IDeepCloneable Clone(Cloner cloner) => new LinearProgrammingAlgorithm(this, cloner);
195
196    public override void Pause() {
197      base.Pause();
198      LinearSolver.InterruptSolve();
199    }
200
201    public override void Prepare() {
202      base.Prepare();
203      Results.Clear();
204
205      foreach (var solver in linearSolverParam.ValidValues) {
206        solver.Reset();
207      }
208    }
209
210    public override void Stop() {
211      base.Stop();
212      LinearSolver.InterruptSolve();
213    }
214
215    protected override void Run(CancellationToken cancellationToken) {
216      LinearSolver.PrimalTolerance = PrimalTolerance;
217      LinearSolver.DualTolerance = DualTolerance;
218      LinearSolver.LpAlgorithm = LpAlgorithm;
219      LinearSolver.Presolve = Presolve;
220      LinearSolver.RelativeGapTolerance = RelativeGapTolerance;
221      LinearSolver.Scaling = Scaling;
222      LinearSolver.TimeLimit = TimeLimit;
223      LinearSolver.ExportModel = ExportModel;
224      var executionTime = ExecutionTime;
225      ExecutionTimeChanged += (sender, args) => executionTime = ExecutionTime;
226      LinearSolver.Solve(Problem.ProblemDefinition, ref executionTime, Results, cancellationToken);
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.