[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;
|
---|
[16233] | 23 | using System.Threading;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
[16172] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[16233] | 30 | namespace HeuristicLab.MathematicalOptimization.LinearProgramming.Algorithms.Solvers.Base {
|
---|
[16172] | 31 |
|
---|
[16233] | 32 | [StorableClass]
|
---|
| 33 | public class Solver : ParameterizedNamedItem, ISolver, IDisposable {
|
---|
[16172] | 34 |
|
---|
| 35 | [Storable]
|
---|
[16373] | 36 | protected IValueParameter<EnumValue<ProblemType>> problemTypeParam;
|
---|
[16172] | 37 |
|
---|
[16233] | 38 | protected LinearSolver solver;
|
---|
[16172] | 39 |
|
---|
[16373] | 40 | [Storable]
|
---|
| 41 | protected IFixedValueParameter<TextValue> solverSpecificParametersParam;
|
---|
| 42 |
|
---|
[16172] | 43 | public Solver() {
|
---|
[16373] | 44 | Parameters.Add(problemTypeParam =
|
---|
| 45 | new ValueParameter<EnumValue<ProblemType>>(nameof(ProblemType), new EnumValue<ProblemType>()));
|
---|
| 46 | Parameters.Add(solverSpecificParametersParam =
|
---|
| 47 | new FixedValueParameter<TextValue>(nameof(SolverSpecificParameters), new TextValue()));
|
---|
[16172] | 48 | }
|
---|
| 49 |
|
---|
| 50 | [StorableConstructor]
|
---|
| 51 | protected Solver(bool deserializing)
|
---|
[16233] | 52 | : base(deserializing) {
|
---|
| 53 | }
|
---|
[16172] | 54 |
|
---|
| 55 | protected Solver(Solver original, Cloner cloner)
|
---|
| 56 | : base(original, cloner) {
|
---|
[16373] | 57 | problemTypeParam = cloner.Clone(original.problemTypeParam);
|
---|
| 58 | solverSpecificParametersParam = cloner.Clone(original.solverSpecificParametersParam);
|
---|
[16172] | 59 | }
|
---|
| 60 |
|
---|
[16373] | 61 | public ProblemType ProblemType {
|
---|
| 62 | get => problemTypeParam.Value.Value;
|
---|
| 63 | set => problemTypeParam.Value.Value = value;
|
---|
[16172] | 64 | }
|
---|
| 65 |
|
---|
[16373] | 66 | public TextValue SolverSpecificParameters => solverSpecificParametersParam.Value;
|
---|
| 67 |
|
---|
| 68 | public virtual bool SupportsPause => true;
|
---|
| 69 | public virtual bool SupportsStop => true;
|
---|
[16234] | 70 | protected virtual OptimizationProblemType OptimizationProblemType { get; }
|
---|
[16172] | 71 |
|
---|
[16233] | 72 | public override IDeepCloneable Clone(Cloner cloner) => new Solver(this, cloner);
|
---|
| 73 |
|
---|
| 74 | public void Dispose() => solver?.Dispose();
|
---|
| 75 |
|
---|
[16373] | 76 | public bool InterruptSolve() => solver?.InterruptSolve() ?? false;
|
---|
[16233] | 77 |
|
---|
| 78 | public virtual void Reset() {
|
---|
[16288] | 79 | solver?.Dispose();
|
---|
[16233] | 80 | solver = null;
|
---|
[16172] | 81 | }
|
---|
| 82 |
|
---|
[16233] | 83 | public virtual void Solve(LinearProgrammingAlgorithm algorithm, CancellationToken cancellationToken) =>
|
---|
[16234] | 84 | Solve(algorithm);
|
---|
[16233] | 85 |
|
---|
| 86 | public virtual void Solve(LinearProgrammingAlgorithm algorithm) =>
|
---|
[16373] | 87 | Solve(algorithm, algorithm.TimeLimit);
|
---|
[16233] | 88 |
|
---|
[16373] | 89 | public virtual void Solve(LinearProgrammingAlgorithm algorithm, TimeSpan timeLimit) {
|
---|
[16234] | 90 | string libraryName = null;
|
---|
| 91 | if (this is IExternalSolver externalSolver)
|
---|
[16233] | 92 | libraryName = externalSolver.LibraryName;
|
---|
| 93 |
|
---|
| 94 | if (solver == null) {
|
---|
[16373] | 95 | solver = new LinearSolver(OptimizationProblemType, s => algorithm.Problem.ProblemDefinition.BuildModel(s), Name,
|
---|
| 96 | libraryName);
|
---|
[16233] | 97 | }
|
---|
| 98 |
|
---|
| 99 | solver.TimeLimit = timeLimit;
|
---|
| 100 | solver.RelativeGapTolerance = algorithm.RelativeGapTolerance;
|
---|
| 101 | solver.PrimalTolerance = algorithm.PrimalTolerance;
|
---|
| 102 | solver.DualTolerance = algorithm.DualTolerance;
|
---|
| 103 | solver.Presolve = algorithm.Presolve;
|
---|
| 104 | solver.Scaling = algorithm.Scaling;
|
---|
| 105 | solver.LpAlgorithm = algorithm.LpAlgorithm;
|
---|
[16373] | 106 | solver.Incrementality = true;
|
---|
[16233] | 107 |
|
---|
[16373] | 108 | if (!solver.SetSolverSpecificParameters(SolverSpecificParameters.Value))
|
---|
| 109 | throw new ArgumentException("Solver specific parameters could not be set.");
|
---|
| 110 |
|
---|
[16233] | 111 | solver.Solve();
|
---|
| 112 |
|
---|
| 113 | algorithm.Problem.ProblemDefinition.Analyze(solver.Solver, algorithm.Results);
|
---|
[16373] | 114 | algorithm.Results.AddOrUpdateResult(nameof(solver.ResultStatus),
|
---|
| 115 | new EnumValue<ResultStatus>(solver.ResultStatus));
|
---|
| 116 | algorithm.Results.AddOrUpdateResult($"Best{nameof(solver.ObjectiveValue)}",
|
---|
[16233] | 117 | new DoubleValue(solver.ObjectiveValue ?? double.NaN));
|
---|
[16373] | 118 |
|
---|
| 119 | if (solver.IsMip) {
|
---|
| 120 | algorithm.Results.AddOrUpdateResult($"Best{nameof(solver.ObjectiveBound)}",
|
---|
| 121 | new DoubleValue(solver.ObjectiveBound ?? double.NaN));
|
---|
| 122 | algorithm.Results.AddOrUpdateResult(nameof(solver.AbsoluteGap),
|
---|
| 123 | new DoubleValue(solver.AbsoluteGap ?? double.NaN));
|
---|
| 124 | algorithm.Results.AddOrUpdateResult(nameof(solver.RelativeGap),
|
---|
| 125 | new PercentValue(solver.RelativeGap ?? double.NaN));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | algorithm.Results.AddOrUpdateResult(nameof(solver.NumberOfConstraints), new IntValue(solver.NumberOfConstraints));
|
---|
| 129 | algorithm.Results.AddOrUpdateResult(nameof(solver.NumberOfVariables), new IntValue(solver.NumberOfVariables));
|
---|
| 130 |
|
---|
| 131 | if (solver.IsMip) {
|
---|
| 132 | algorithm.Results.AddOrUpdateResult(nameof(solver.NumberOfNodes), new DoubleValue(solver.NumberOfNodes));
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | algorithm.Results.AddOrUpdateResult(nameof(solver.Iterations), new DoubleValue(solver.Iterations));
|
---|
| 136 | algorithm.Results.AddOrUpdateResult(nameof(solver.SolverVersion), new StringValue(solver.SolverVersion));
|
---|
[16233] | 137 | }
|
---|
[16172] | 138 | }
|
---|
[16288] | 139 | }
|
---|