[16288] | 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;
|
---|
[16172] | 23 | using System.Threading;
|
---|
[16233] | 24 | using Google.OrTools.LinearSolver;
|
---|
[16172] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[16233] | 28 | using HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers;
|
---|
| 29 | using HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers.Base;
|
---|
[16172] | 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 {
|
---|
[16233] | 36 |
|
---|
| 37 | [Item("Linear/Mixed Integer Programming (LP/MIP)", "")]
|
---|
| 38 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
|
---|
[16172] | 39 | [StorableClass]
|
---|
| 40 | public class LinearProgrammingAlgorithm : BasicAlgorithm {
|
---|
| 41 |
|
---|
| 42 | [Storable]
|
---|
[16233] | 43 | private readonly IFixedValueParameter<DoubleValue> dualToleranceParam;
|
---|
[16172] | 44 |
|
---|
| 45 | [Storable]
|
---|
[16233] | 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]
|
---|
[16172] | 55 | private readonly IFixedValueParameter<DoubleValue> relativeGapToleranceParam;
|
---|
| 56 |
|
---|
| 57 | [Storable]
|
---|
[16233] | 58 | private readonly IFixedValueParameter<BoolValue> scalingParam;
|
---|
| 59 |
|
---|
| 60 | [Storable]
|
---|
[16288] | 61 | private IConstrainedValueParameter<ISolver> solverParam;
|
---|
[16233] | 62 |
|
---|
| 63 | [Storable]
|
---|
[16172] | 64 | private readonly IFixedValueParameter<TimeSpanValue> timeLimitParam;
|
---|
| 65 |
|
---|
[16288] | 66 | public IConstrainedValueParameter<ISolver> SolverParameter {
|
---|
| 67 | get { return solverParam; }
|
---|
| 68 | set { solverParam = value; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[16172] | 71 | public LinearProgrammingAlgorithm() {
|
---|
[16233] | 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()));
|
---|
[16172] | 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]
|
---|
[16288] | 105 | protected LinearProgrammingAlgorithm(bool deserializing)
|
---|
[16233] | 106 | : base(deserializing) {
|
---|
| 107 | }
|
---|
[16172] | 108 |
|
---|
[16288] | 109 | protected LinearProgrammingAlgorithm(LinearProgrammingAlgorithm original, Cloner cloner)
|
---|
[16172] | 110 | : base(original, cloner) {
|
---|
| 111 | solverParam = cloner.Clone(original.solverParam);
|
---|
| 112 | relativeGapToleranceParam = cloner.Clone(original.relativeGapToleranceParam);
|
---|
| 113 | timeLimitParam = cloner.Clone(original.timeLimitParam);
|
---|
[16233] | 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);
|
---|
[16172] | 119 | }
|
---|
| 120 |
|
---|
[16233] | 121 | public double DualTolerance {
|
---|
| 122 | get => dualToleranceParam.Value.Value;
|
---|
| 123 | set => dualToleranceParam.Value.Value = value;
|
---|
[16172] | 124 | }
|
---|
| 125 |
|
---|
[16233] | 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 |
|
---|
[16172] | 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);
|
---|
[16233] | 147 |
|
---|
[16172] | 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 |
|
---|
[16233] | 155 | public bool Scaling {
|
---|
| 156 | get => scalingParam.Value.Value;
|
---|
| 157 | set => scalingParam.Value.Value = value;
|
---|
| 158 | }
|
---|
[16172] | 159 |
|
---|
[16233] | 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 |
|
---|
[16172] | 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 |
|
---|
[16233] | 176 | public override void Pause() {
|
---|
| 177 | base.Pause();
|
---|
| 178 | Solver.Interrupt();
|
---|
| 179 | }
|
---|
[16172] | 180 |
|
---|
[16233] | 181 | public override void Prepare() {
|
---|
| 182 | base.Prepare();
|
---|
| 183 | Results.Clear();
|
---|
| 184 |
|
---|
| 185 | foreach (var solver in solverParam.ValidValues) {
|
---|
| 186 | solver.Reset();
|
---|
[16172] | 187 | }
|
---|
| 188 | }
|
---|
[16233] | 189 |
|
---|
| 190 | public override void Stop() {
|
---|
| 191 | base.Stop();
|
---|
| 192 | Solver.Interrupt();
|
---|
| 193 | }
|
---|
[16288] | 194 |
|
---|
[16233] | 195 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
| 196 | base.Initialize(cancellationToken);
|
---|
| 197 | }
|
---|
[16288] | 198 |
|
---|
[16233] | 199 | protected override void Run(CancellationToken cancellationToken) => Solver.Solve(this, cancellationToken);
|
---|
[16172] | 200 | }
|
---|
[16288] | 201 | }
|
---|