[16288] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17209] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[16288] | 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;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[16736] | 30 | using HEAL.Attic;
|
---|
[16172] | 31 |
|
---|
[16582] | 32 | namespace HeuristicLab.ExactOptimization.LinearProgramming {
|
---|
[16233] | 33 |
|
---|
[16582] | 34 | [Item("Mixed-Integer Linear Programming (LP, MIP)", "Linear/mixed integer programming implemented in several solvers. " +
|
---|
| 35 | "See also https://dev.heuristiclab.com/trac.fcgi/wiki/Documentation/Reference/ExactOptimization")]
|
---|
[16373] | 36 | [Creatable(CreatableAttribute.Categories.ExactAlgorithms)]
|
---|
[16736] | 37 | [StorableType("D6BAE020-6315-4C8A-928F-E47C67F3BE8F")]
|
---|
[16582] | 38 | public sealed class LinearProgrammingAlgorithm : BasicAlgorithm {
|
---|
[16172] | 39 |
|
---|
| 40 | [Storable]
|
---|
[16233] | 41 | private readonly IFixedValueParameter<DoubleValue> dualToleranceParam;
|
---|
[16172] | 42 |
|
---|
| 43 | [Storable]
|
---|
[16233] | 44 | private readonly IFixedValueParameter<BoolValue> presolveParam;
|
---|
| 45 |
|
---|
| 46 | [Storable]
|
---|
| 47 | private readonly IFixedValueParameter<DoubleValue> primalToleranceParam;
|
---|
| 48 |
|
---|
| 49 | [Storable]
|
---|
[16373] | 50 | private readonly IFixedValueParameter<PercentValue> relativeGapToleranceParam;
|
---|
[16172] | 51 |
|
---|
| 52 | [Storable]
|
---|
[16233] | 53 | private readonly IFixedValueParameter<BoolValue> scalingParam;
|
---|
| 54 |
|
---|
| 55 | [Storable]
|
---|
[16405] | 56 | private readonly IFixedValueParameter<TimeSpanValue> timeLimitParam;
|
---|
[16233] | 57 |
|
---|
| 58 | [Storable]
|
---|
[16405] | 59 | private IConstrainedValueParameter<ILinearSolver> linearSolverParam;
|
---|
[16172] | 60 |
|
---|
[16582] | 61 | #region Problem Properties
|
---|
[16373] | 62 |
|
---|
[16582] | 63 | public new LinearProblem Problem {
|
---|
| 64 | get => (LinearProblem)base.Problem;
|
---|
| 65 | set => base.Problem = value;
|
---|
| 66 | }
|
---|
[16373] | 67 |
|
---|
[16582] | 68 | public override Type ProblemType { get; } = typeof(LinearProblem);
|
---|
[16172] | 69 |
|
---|
[16582] | 70 | #endregion
|
---|
| 71 | #region Parameter Properties
|
---|
[16172] | 72 |
|
---|
[16582] | 73 | public IFixedValueParameter<DoubleValue> DualToleranceParameter => dualToleranceParam;
|
---|
| 74 | public IConstrainedValueParameter<ILinearSolver> LinearSolverParameter => linearSolverParam;
|
---|
| 75 | public IFixedValueParameter<BoolValue> PresolveParameter => presolveParam;
|
---|
| 76 | public IFixedValueParameter<DoubleValue> PrimalToleranceParameter => primalToleranceParam;
|
---|
| 77 | public IFixedValueParameter<PercentValue> RelativeGapToleranceParameter => relativeGapToleranceParam;
|
---|
| 78 | public IFixedValueParameter<BoolValue> ScalingParameter => scalingParam;
|
---|
| 79 | public IFixedValueParameter<TimeSpanValue> TimeLimitParameter => timeLimitParam;
|
---|
[16172] | 80 |
|
---|
[16582] | 81 | #endregion
|
---|
| 82 | #region Properties
|
---|
[16172] | 83 |
|
---|
[16233] | 84 | public double DualTolerance {
|
---|
| 85 | get => dualToleranceParam.Value.Value;
|
---|
| 86 | set => dualToleranceParam.Value.Value = value;
|
---|
[16172] | 87 | }
|
---|
| 88 |
|
---|
[16405] | 89 | public ILinearSolver LinearSolver {
|
---|
| 90 | get => linearSolverParam.Value;
|
---|
| 91 | set => linearSolverParam.Value = value;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[16233] | 94 | public bool Presolve {
|
---|
| 95 | get => presolveParam.Value.Value;
|
---|
| 96 | set => presolveParam.Value.Value = value;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public double PrimalTolerance {
|
---|
| 100 | get => primalToleranceParam.Value.Value;
|
---|
| 101 | set => primalToleranceParam.Value.Value = value;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[16172] | 104 | public double RelativeGapTolerance {
|
---|
| 105 | get => relativeGapToleranceParam.Value.Value;
|
---|
| 106 | set => relativeGapToleranceParam.Value.Value = value;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[16233] | 109 | public bool Scaling {
|
---|
| 110 | get => scalingParam.Value.Value;
|
---|
| 111 | set => scalingParam.Value.Value = value;
|
---|
| 112 | }
|
---|
[16172] | 113 |
|
---|
[16405] | 114 | public override bool SupportsPause => LinearSolver.SupportsPause;
|
---|
| 115 | public override bool SupportsStop => LinearSolver.SupportsStop;
|
---|
[16233] | 116 |
|
---|
[16172] | 117 | public TimeSpan TimeLimit {
|
---|
| 118 | get => timeLimitParam.Value.Value;
|
---|
| 119 | set => timeLimitParam.Value.Value = value;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[16582] | 122 | #endregion
|
---|
| 123 |
|
---|
| 124 | public LinearProgrammingAlgorithm() {
|
---|
| 125 | Parameters.Add(linearSolverParam =
|
---|
| 126 | new ConstrainedValueParameter<ILinearSolver>(nameof(LinearSolver), "The solver used to solve the model."));
|
---|
| 127 |
|
---|
| 128 | ILinearSolver defaultSolver;
|
---|
| 129 | linearSolverParam.ValidValues.Add(defaultSolver = new CoinOrSolver());
|
---|
| 130 | linearSolverParam.ValidValues.Add(new CplexSolver());
|
---|
| 131 | linearSolverParam.ValidValues.Add(new GlopSolver());
|
---|
| 132 | linearSolverParam.ValidValues.Add(new GurobiSolver());
|
---|
| 133 | linearSolverParam.ValidValues.Add(new ScipSolver());
|
---|
| 134 | linearSolverParam.Value = defaultSolver;
|
---|
| 135 |
|
---|
| 136 | Parameters.Add(relativeGapToleranceParam = new FixedValueParameter<PercentValue>(nameof(RelativeGapTolerance),
|
---|
| 137 | "Limit for relative MIP gap.", new PercentValue(SolverParameters.DefaultRelativeMipGap)));
|
---|
| 138 | Parameters.Add(timeLimitParam = new FixedValueParameter<TimeSpanValue>(nameof(TimeLimit),
|
---|
| 139 | "Limit for runtime. Set to zero for unlimited runtime.",
|
---|
| 140 | new TimeSpanValue(new TimeSpan(0, 1, 0))));
|
---|
| 141 | Parameters.Add(presolveParam =
|
---|
| 142 | new FixedValueParameter<BoolValue>(nameof(Presolve), "Advanced usage: presolve mode.",
|
---|
| 143 | new BoolValue(SolverParameters.DefaultPresolve == SolverParameters.PresolveValues.PresolveOn)) { Hidden = true });
|
---|
| 144 | Parameters.Add(dualToleranceParam = new FixedValueParameter<DoubleValue>(nameof(DualTolerance),
|
---|
| 145 | "Advanced usage: tolerance for dual feasibility of basic solutions.",
|
---|
| 146 | new DoubleValue(SolverParameters.DefaultDualTolerance)) { Hidden = true });
|
---|
| 147 | Parameters.Add(primalToleranceParam = new FixedValueParameter<DoubleValue>(nameof(PrimalTolerance),
|
---|
| 148 | "Advanced usage: tolerance for primal feasibility of basic solutions. " +
|
---|
| 149 | "This does not control the integer feasibility tolerance of integer " +
|
---|
| 150 | "solutions for MIP or the tolerance used during presolve.",
|
---|
| 151 | new DoubleValue(SolverParameters.DefaultPrimalTolerance)) { Hidden = true });
|
---|
| 152 | Parameters.Add(scalingParam = new FixedValueParameter<BoolValue>(nameof(Scaling),
|
---|
| 153 | "Advanced usage: enable or disable matrix scaling.", new BoolValue()) { Hidden = true });
|
---|
| 154 |
|
---|
| 155 | Problem = new LinearProblem();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | [StorableConstructor]
|
---|
[16736] | 159 | private LinearProgrammingAlgorithm(StorableConstructorFlag _) : base(_) { }
|
---|
[16582] | 160 |
|
---|
| 161 | private LinearProgrammingAlgorithm(LinearProgrammingAlgorithm original, Cloner cloner)
|
---|
| 162 | : base(original, cloner) {
|
---|
| 163 | linearSolverParam = cloner.Clone(original.linearSolverParam);
|
---|
| 164 | relativeGapToleranceParam = cloner.Clone(original.relativeGapToleranceParam);
|
---|
| 165 | timeLimitParam = cloner.Clone(original.timeLimitParam);
|
---|
| 166 | presolveParam = cloner.Clone(original.presolveParam);
|
---|
| 167 | dualToleranceParam = cloner.Clone(original.dualToleranceParam);
|
---|
| 168 | primalToleranceParam = cloner.Clone(original.primalToleranceParam);
|
---|
| 169 | scalingParam = cloner.Clone(original.scalingParam);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[16172] | 172 | public override IDeepCloneable Clone(Cloner cloner) => new LinearProgrammingAlgorithm(this, cloner);
|
---|
| 173 |
|
---|
[16233] | 174 | public override void Pause() {
|
---|
| 175 | base.Pause();
|
---|
[16405] | 176 | LinearSolver.InterruptSolve();
|
---|
[16233] | 177 | }
|
---|
[16172] | 178 |
|
---|
[16233] | 179 | public override void Prepare() {
|
---|
| 180 | base.Prepare();
|
---|
| 181 | Results.Clear();
|
---|
| 182 |
|
---|
[16405] | 183 | foreach (var solver in linearSolverParam.ValidValues) {
|
---|
[16233] | 184 | solver.Reset();
|
---|
[16172] | 185 | }
|
---|
| 186 | }
|
---|
[16233] | 187 |
|
---|
| 188 | public override void Stop() {
|
---|
| 189 | base.Stop();
|
---|
[16405] | 190 | LinearSolver.InterruptSolve();
|
---|
[16233] | 191 | }
|
---|
[16288] | 192 |
|
---|
[16405] | 193 | protected override void Run(CancellationToken cancellationToken) {
|
---|
| 194 | LinearSolver.PrimalTolerance = PrimalTolerance;
|
---|
| 195 | LinearSolver.DualTolerance = DualTolerance;
|
---|
| 196 | LinearSolver.Presolve = Presolve;
|
---|
| 197 | LinearSolver.RelativeGapTolerance = RelativeGapTolerance;
|
---|
| 198 | LinearSolver.Scaling = Scaling;
|
---|
| 199 | LinearSolver.TimeLimit = TimeLimit;
|
---|
[16582] | 200 | LinearSolver.Solve(Problem.ProblemDefinition, Results, cancellationToken);
|
---|
[16405] | 201 | }
|
---|
[16172] | 202 | }
|
---|
[16288] | 203 | }
|
---|