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 |
|
---|
22 | using System;
|
---|
23 | using System.Threading;
|
---|
24 | using Google.OrTools.LinearSolver;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers;
|
---|
29 | using HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers.Base;
|
---|
30 | using HeuristicLab.MathematicalOptimization.LinearProgramming.Problems;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms {
|
---|
36 |
|
---|
37 | [Item("Linear/Mixed Integer Programming (LP/MIP)", "")]
|
---|
38 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
|
---|
39 | [StorableClass]
|
---|
40 | public class LinearProgrammingAlgorithm : BasicAlgorithm {
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | private readonly IFixedValueParameter<DoubleValue> dualToleranceParam;
|
---|
44 |
|
---|
45 | [Storable]
|
---|
46 | private readonly IFixedValueParameter<EnumValue<LpAlgorithmValues>> lpAlgorithmParam;
|
---|
47 |
|
---|
48 | [Storable]
|
---|
49 | private readonly IFixedValueParameter<BoolValue> presolveParam;
|
---|
50 |
|
---|
51 | [Storable]
|
---|
52 | private readonly IFixedValueParameter<DoubleValue> primalToleranceParam;
|
---|
53 |
|
---|
54 | [Storable]
|
---|
55 | private readonly IFixedValueParameter<DoubleValue> relativeGapToleranceParam;
|
---|
56 |
|
---|
57 | [Storable]
|
---|
58 | private readonly IFixedValueParameter<BoolValue> scalingParam;
|
---|
59 |
|
---|
60 | [Storable]
|
---|
61 | private IConstrainedValueParameter<ISolver> solverParam;
|
---|
62 |
|
---|
63 | [Storable]
|
---|
64 | private readonly IFixedValueParameter<TimeSpanValue> timeLimitParam;
|
---|
65 |
|
---|
66 | public IConstrainedValueParameter<ISolver> SolverParameter {
|
---|
67 | get { return solverParam; }
|
---|
68 | set { solverParam = value; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public LinearProgrammingAlgorithm() {
|
---|
72 | Parameters.Add(solverParam =
|
---|
73 | new ConstrainedValueParameter<ISolver>(nameof(Solver), "The solver used to solve the model."));
|
---|
74 | Parameters.Add(relativeGapToleranceParam = new FixedValueParameter<DoubleValue>(nameof(RelativeGapTolerance),
|
---|
75 | "Limit for relative MIP gap.", new DoubleValue(MPSolverParameters.kDefaultRelativeMipGap)));
|
---|
76 | Parameters.Add(timeLimitParam = new FixedValueParameter<TimeSpanValue>(nameof(TimeLimit),
|
---|
77 | "Limit for runtime. Set to zero for unlimited runtime.", new TimeSpanValue()));
|
---|
78 | Parameters.Add(presolveParam =
|
---|
79 | new FixedValueParameter<BoolValue>(nameof(Presolve), "Advanced usage: presolve mode.", new BoolValue()));
|
---|
80 | Parameters.Add(lpAlgorithmParam = new FixedValueParameter<EnumValue<LpAlgorithmValues>>(nameof(LpAlgorithm),
|
---|
81 | "Algorithm to solve linear programs.", new EnumValue<LpAlgorithmValues>(LpAlgorithmValues.DualSimplex)));
|
---|
82 | Parameters.Add(dualToleranceParam = new FixedValueParameter<DoubleValue>(nameof(DualTolerance),
|
---|
83 | "Advanced usage: tolerance for dual feasibility of basic solutions.",
|
---|
84 | new DoubleValue(MPSolverParameters.kDefaultDualTolerance)));
|
---|
85 | Parameters.Add(primalToleranceParam = new FixedValueParameter<DoubleValue>(nameof(PrimalTolerance),
|
---|
86 | "Advanced usage: tolerance for primal feasibility of basic solutions. " +
|
---|
87 | "This does not control the integer feasibility tolerance of integer " +
|
---|
88 | "solutions for MIP or the tolerance used during presolve.",
|
---|
89 | new DoubleValue(MPSolverParameters.kDefaultPrimalTolerance)));
|
---|
90 | Parameters.Add(scalingParam = new FixedValueParameter<BoolValue>(nameof(Scaling),
|
---|
91 | "Advanced usage: enable or disable matrix scaling.", new BoolValue()));
|
---|
92 |
|
---|
93 | Problem = new LinearProgrammingProblem();
|
---|
94 |
|
---|
95 | solverParam.ValidValues.Add(new CoinOrSolver());
|
---|
96 | solverParam.ValidValues.Add(new CplexSolver());
|
---|
97 | solverParam.ValidValues.Add(new GlpkSolver());
|
---|
98 | solverParam.ValidValues.Add(new GurobiSolver());
|
---|
99 | solverParam.ValidValues.Add(new ScipSolver());
|
---|
100 | solverParam.ValidValues.Add(new BopSolver());
|
---|
101 | solverParam.ValidValues.Add(new GlopSolver());
|
---|
102 | }
|
---|
103 |
|
---|
104 | [StorableConstructor]
|
---|
105 | protected LinearProgrammingAlgorithm(bool deserializing)
|
---|
106 | : base(deserializing) {
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected LinearProgrammingAlgorithm(LinearProgrammingAlgorithm original, Cloner cloner)
|
---|
110 | : base(original, cloner) {
|
---|
111 | solverParam = cloner.Clone(original.solverParam);
|
---|
112 | relativeGapToleranceParam = cloner.Clone(original.relativeGapToleranceParam);
|
---|
113 | timeLimitParam = cloner.Clone(original.timeLimitParam);
|
---|
114 | presolveParam = cloner.Clone(original.presolveParam);
|
---|
115 | lpAlgorithmParam = cloner.Clone(original.lpAlgorithmParam);
|
---|
116 | dualToleranceParam = cloner.Clone(original.dualToleranceParam);
|
---|
117 | primalToleranceParam = cloner.Clone(original.primalToleranceParam);
|
---|
118 | scalingParam = cloner.Clone(original.scalingParam);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public double DualTolerance {
|
---|
122 | get => dualToleranceParam.Value.Value;
|
---|
123 | set => dualToleranceParam.Value.Value = value;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public LpAlgorithmValues LpAlgorithm {
|
---|
127 | get => lpAlgorithmParam.Value.Value;
|
---|
128 | set => lpAlgorithmParam.Value.Value = value;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public bool Presolve {
|
---|
132 | get => presolveParam.Value.Value;
|
---|
133 | set => presolveParam.Value.Value = value;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public double PrimalTolerance {
|
---|
137 | get => primalToleranceParam.Value.Value;
|
---|
138 | set => primalToleranceParam.Value.Value = value;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public new LinearProgrammingProblem Problem {
|
---|
142 | get => (LinearProgrammingProblem)base.Problem;
|
---|
143 | set => base.Problem = value;
|
---|
144 | }
|
---|
145 |
|
---|
146 | public override Type ProblemType { get; } = typeof(LinearProgrammingProblem);
|
---|
147 |
|
---|
148 | public double RelativeGapTolerance {
|
---|
149 | get => relativeGapToleranceParam.Value.Value;
|
---|
150 | set => relativeGapToleranceParam.Value.Value = value;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public override ResultCollection Results { get; } = new ResultCollection();
|
---|
154 |
|
---|
155 | public bool Scaling {
|
---|
156 | get => scalingParam.Value.Value;
|
---|
157 | set => scalingParam.Value.Value = value;
|
---|
158 | }
|
---|
159 |
|
---|
160 | public ISolver Solver {
|
---|
161 | get => solverParam.Value;
|
---|
162 | set => solverParam.Value = value;
|
---|
163 | }
|
---|
164 |
|
---|
165 | public override bool SupportsPause => Solver.SupportsPause;
|
---|
166 |
|
---|
167 | public override bool SupportsStop => Solver.SupportsStop;
|
---|
168 |
|
---|
169 | public TimeSpan TimeLimit {
|
---|
170 | get => timeLimitParam.Value.Value;
|
---|
171 | set => timeLimitParam.Value.Value = value;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public override IDeepCloneable Clone(Cloner cloner) => new LinearProgrammingAlgorithm(this, cloner);
|
---|
175 |
|
---|
176 | public override void Pause() {
|
---|
177 | base.Pause();
|
---|
178 | Solver.Interrupt();
|
---|
179 | }
|
---|
180 |
|
---|
181 | public override void Prepare() {
|
---|
182 | base.Prepare();
|
---|
183 | Results.Clear();
|
---|
184 |
|
---|
185 | foreach (var solver in solverParam.ValidValues) {
|
---|
186 | solver.Reset();
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | public override void Stop() {
|
---|
191 | base.Stop();
|
---|
192 | Solver.Interrupt();
|
---|
193 | }
|
---|
194 |
|
---|
195 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
196 | base.Initialize(cancellationToken);
|
---|
197 | }
|
---|
198 |
|
---|
199 | protected override void Run(CancellationToken cancellationToken) => Solver.Solve(this, cancellationToken);
|
---|
200 | }
|
---|
201 | }
|
---|