[16288] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 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;
|
---|
[16582] | 23 | using System.Diagnostics;
|
---|
[16233] | 24 | using System.Linq;
|
---|
| 25 | using System.Threading;
|
---|
| 26 | using HeuristicLab.Analysis;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
[16405] | 30 | using HeuristicLab.Optimization;
|
---|
[16233] | 31 | using HeuristicLab.Parameters;
|
---|
[16736] | 32 | using HEAL.Attic;
|
---|
[16233] | 33 |
|
---|
[16582] | 34 | namespace HeuristicLab.ExactOptimization.LinearProgramming {
|
---|
[16233] | 35 |
|
---|
[16736] | 36 | [StorableType("8730ECDD-8F38-47C2-B0D9-2B1F38FC0A27")]
|
---|
[16405] | 37 | public class IncrementalLinearSolver : LinearSolver, IIncrementalLinearSolver {
|
---|
[16288] | 38 |
|
---|
[16582] | 39 | [Storable]
|
---|
| 40 | protected readonly IValueParameter<TimeSpanValue> qualityUpdateIntervalParam;
|
---|
[16233] | 41 |
|
---|
[16582] | 42 | private readonly Stopwatch stopwatch = new Stopwatch();
|
---|
[16233] | 43 |
|
---|
[16582] | 44 | [Storable]
|
---|
| 45 | private TimeSpan executionTime = TimeSpan.Zero;
|
---|
[16233] | 46 |
|
---|
[16582] | 47 | [Storable]
|
---|
| 48 | private IndexedDataTable<double> qualityPerClock;
|
---|
| 49 |
|
---|
[16405] | 50 | public IncrementalLinearSolver() {
|
---|
[16233] | 51 | Parameters.Add(qualityUpdateIntervalParam =
|
---|
| 52 | new ValueParameter<TimeSpanValue>(nameof(QualityUpdateInterval),
|
---|
[16373] | 53 | "Time interval before solver is paused, results are retrieved and solver is resumed. " +
|
---|
| 54 | "Set to zero for no intermediate results and faster solving.", new TimeSpanValue(new TimeSpan(0, 0, 10))));
|
---|
| 55 | problemTypeParam.Value.ValueChanged += (sender, args) => {
|
---|
| 56 | if (SupportsQualityUpdate) {
|
---|
| 57 | if (!Parameters.Contains(qualityUpdateIntervalParam)) {
|
---|
| 58 | Parameters.Add(qualityUpdateIntervalParam);
|
---|
| 59 | }
|
---|
[16233] | 60 | } else {
|
---|
[16373] | 61 | Parameters.Remove(qualityUpdateIntervalParam);
|
---|
[16233] | 62 | }
|
---|
| 63 | };
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[16405] | 66 | [StorableConstructor]
|
---|
[16736] | 67 | protected IncrementalLinearSolver(StorableConstructorFlag _) : base(_) { }
|
---|
[16405] | 68 |
|
---|
| 69 | protected IncrementalLinearSolver(IncrementalLinearSolver original, Cloner cloner)
|
---|
| 70 | : base(original, cloner) {
|
---|
[16373] | 71 | problemTypeParam = cloner.Clone(original.problemTypeParam);
|
---|
[16233] | 72 | qualityUpdateIntervalParam = cloner.Clone(original.qualityUpdateIntervalParam);
|
---|
| 73 | if (original.qualityPerClock != null)
|
---|
| 74 | qualityPerClock = cloner.Clone(original.qualityPerClock);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public TimeSpan QualityUpdateInterval {
|
---|
| 78 | get => qualityUpdateIntervalParam.Value.Value;
|
---|
| 79 | set => qualityUpdateIntervalParam.Value.Value = value;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[16582] | 82 | public IValueParameter<TimeSpanValue> QualityUpdateIntervalParameter => qualityUpdateIntervalParam;
|
---|
| 83 |
|
---|
[16405] | 84 | public virtual bool SupportsQualityUpdate => true;
|
---|
[16582] | 85 |
|
---|
[16405] | 86 | protected virtual TimeSpan IntermediateTimeLimit => QualityUpdateInterval;
|
---|
[16373] | 87 |
|
---|
[16582] | 88 | public override void Reset() {
|
---|
| 89 | base.Reset();
|
---|
| 90 | executionTime = TimeSpan.Zero;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public override void Solve(ILinearProblemDefinition problemDefinition,
|
---|
[16405] | 94 | ResultCollection results, CancellationToken cancellationToken) {
|
---|
[16373] | 95 | if (!SupportsQualityUpdate || QualityUpdateInterval == TimeSpan.Zero) {
|
---|
[16582] | 96 | base.Solve(problemDefinition, results, cancellationToken);
|
---|
[16233] | 97 | return;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[16405] | 100 | var timeLimit = TimeLimit;
|
---|
[16233] | 101 | var unlimitedRuntime = timeLimit == TimeSpan.Zero;
|
---|
| 102 |
|
---|
| 103 | if (!unlimitedRuntime) {
|
---|
[16405] | 104 | timeLimit -= executionTime;
|
---|
[16233] | 105 | }
|
---|
[16234] | 106 |
|
---|
[16233] | 107 | var iterations = (long)timeLimit.TotalMilliseconds / (long)QualityUpdateInterval.TotalMilliseconds;
|
---|
| 108 | var remaining = timeLimit - TimeSpan.FromMilliseconds(iterations * QualityUpdateInterval.TotalMilliseconds);
|
---|
[16373] | 109 | var validResultStatuses = new[] { ResultStatus.NotSolved, ResultStatus.Feasible };
|
---|
[16233] | 110 |
|
---|
| 111 | while (unlimitedRuntime || iterations > 0) {
|
---|
[16373] | 112 | if (cancellationToken.IsCancellationRequested)
|
---|
| 113 | return;
|
---|
| 114 |
|
---|
[16910] | 115 | stopwatch.Restart();
|
---|
[16405] | 116 | Solve(problemDefinition, results, IntermediateTimeLimit);
|
---|
[16582] | 117 | stopwatch.Stop();
|
---|
| 118 | executionTime += stopwatch.Elapsed;
|
---|
[16405] | 119 | UpdateQuality(results, executionTime);
|
---|
[16233] | 120 |
|
---|
[16405] | 121 | var resultStatus = ((EnumValue<ResultStatus>)results["ResultStatus"].Value).Value;
|
---|
[16373] | 122 | if (!validResultStatuses.Contains(resultStatus))
|
---|
[16233] | 123 | return;
|
---|
| 124 |
|
---|
| 125 | if (!unlimitedRuntime)
|
---|
| 126 | iterations--;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | if (remaining > TimeSpan.Zero) {
|
---|
[16405] | 130 | Solve(problemDefinition, results, remaining);
|
---|
| 131 | UpdateQuality(results, executionTime);
|
---|
[16233] | 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[16405] | 135 | private void UpdateQuality(ResultCollection results, TimeSpan executionTime) {
|
---|
[16582] | 136 | IndexedDataRow<double> qpcRow;
|
---|
| 137 | IndexedDataRow<double> bpcRow;
|
---|
| 138 |
|
---|
[16405] | 139 | if (!results.Exists(r => r.Name == "QualityPerClock")) {
|
---|
[16233] | 140 | qualityPerClock = new IndexedDataTable<double>("Quality per Clock");
|
---|
[16373] | 141 | qpcRow = new IndexedDataRow<double>("Objective Value");
|
---|
| 142 | bpcRow = new IndexedDataRow<double>("Bound");
|
---|
[16582] | 143 | qualityPerClock.Rows.Add(qpcRow);
|
---|
| 144 | qualityPerClock.Rows.Add(bpcRow);
|
---|
[16405] | 145 | results.AddOrUpdateResult("QualityPerClock", qualityPerClock);
|
---|
[16582] | 146 | } else {
|
---|
| 147 | qpcRow = qualityPerClock.Rows["Objective Value"];
|
---|
| 148 | bpcRow = qualityPerClock.Rows["Bound"];
|
---|
[16233] | 149 | }
|
---|
| 150 |
|
---|
[16405] | 151 | var resultStatus = ((EnumValue<ResultStatus>)results["ResultStatus"].Value).Value;
|
---|
[16233] | 152 |
|
---|
[16373] | 153 | if (new[] { ResultStatus.Abnormal, ResultStatus.NotSolved, ResultStatus.Unbounded }.Contains(resultStatus))
|
---|
[16233] | 154 | return;
|
---|
| 155 |
|
---|
[16405] | 156 | var objective = ((DoubleValue)results["BestObjectiveValue"].Value).Value;
|
---|
[16582] | 157 | var bound = solver.IsMip() ? ((DoubleValue)results["BestObjectiveBound"].Value).Value : double.NaN;
|
---|
[16405] | 158 | var time = executionTime.TotalSeconds;
|
---|
[16233] | 159 |
|
---|
| 160 | if (!qpcRow.Values.Any()) {
|
---|
| 161 | if (!double.IsInfinity(objective) && !double.IsNaN(objective)) {
|
---|
| 162 | qpcRow.Values.Add(Tuple.Create(time, objective));
|
---|
| 163 | qpcRow.Values.Add(Tuple.Create(time, objective));
|
---|
[16405] | 164 | results.AddOrUpdateResult("BestObjectiveValueFoundAt", new TimeSpanValue(TimeSpan.FromSeconds(time)));
|
---|
[16233] | 165 | }
|
---|
| 166 | } else {
|
---|
| 167 | var previousBest = qpcRow.Values.Last().Item2;
|
---|
| 168 | qpcRow.Values[qpcRow.Values.Count - 1] = Tuple.Create(time, objective);
|
---|
| 169 | if (!objective.IsAlmost(previousBest)) {
|
---|
| 170 | qpcRow.Values.Add(Tuple.Create(time, objective));
|
---|
[16405] | 171 | results.AddOrUpdateResult("BestObjectiveValueFoundAt", new TimeSpanValue(TimeSpan.FromSeconds(time)));
|
---|
[16233] | 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[16582] | 175 | if (!solver.IsMip())
|
---|
[16373] | 176 | return;
|
---|
| 177 |
|
---|
[16233] | 178 | if (!bpcRow.Values.Any()) {
|
---|
| 179 | if (!double.IsInfinity(bound) && !double.IsNaN(bound)) {
|
---|
| 180 | bpcRow.Values.Add(Tuple.Create(time, bound));
|
---|
| 181 | bpcRow.Values.Add(Tuple.Create(time, bound));
|
---|
| 182 | }
|
---|
| 183 | } else {
|
---|
| 184 | var previousBest = bpcRow.Values.Last().Item2;
|
---|
| 185 | bpcRow.Values[bpcRow.Values.Count - 1] = Tuple.Create(time, bound);
|
---|
| 186 | if (!bound.IsAlmost(previousBest)) {
|
---|
| 187 | bpcRow.Values.Add(Tuple.Create(time, bound));
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
[16288] | 192 | }
|
---|