Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.MathematicalOptimization/3.3/LinearProgramming/Algorithms/Solvers/Base/IncrementalLinearSolver.cs @ 16582

Last change on this file since 16582 was 16582, checked in by ddorfmei, 5 years ago

#2931: solved the issues found during the review

File size: 7.3 KB
RevLine 
[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
22using System;
[16582]23using System.Diagnostics;
[16233]24using System.Linq;
25using System.Threading;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
[16405]30using HeuristicLab.Optimization;
[16233]31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
[16582]34namespace HeuristicLab.ExactOptimization.LinearProgramming {
[16233]35
36  [StorableClass]
[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]
67    protected IncrementalLinearSolver(bool deserializing)
68      : base(deserializing) {
69    }
70
71    protected IncrementalLinearSolver(IncrementalLinearSolver original, Cloner cloner)
72      : base(original, cloner) {
[16373]73      problemTypeParam = cloner.Clone(original.problemTypeParam);
[16233]74      qualityUpdateIntervalParam = cloner.Clone(original.qualityUpdateIntervalParam);
75      if (original.qualityPerClock != null)
76        qualityPerClock = cloner.Clone(original.qualityPerClock);
77    }
78
79    public TimeSpan QualityUpdateInterval {
80      get => qualityUpdateIntervalParam.Value.Value;
81      set => qualityUpdateIntervalParam.Value.Value = value;
82    }
83
[16582]84    public IValueParameter<TimeSpanValue> QualityUpdateIntervalParameter => qualityUpdateIntervalParam;
85
[16405]86    public virtual bool SupportsQualityUpdate => true;
[16582]87
[16405]88    protected virtual TimeSpan IntermediateTimeLimit => QualityUpdateInterval;
[16373]89
[16582]90    public override void Reset() {
91      base.Reset();
92      stopwatch.Reset();
93      executionTime = TimeSpan.Zero;
94    }
95
96    public override void Solve(ILinearProblemDefinition problemDefinition,
[16405]97      ResultCollection results, CancellationToken cancellationToken) {
[16373]98      if (!SupportsQualityUpdate || QualityUpdateInterval == TimeSpan.Zero) {
[16582]99        base.Solve(problemDefinition, results, cancellationToken);
[16233]100        return;
101      }
102
[16405]103      var timeLimit = TimeLimit;
[16233]104      var unlimitedRuntime = timeLimit == TimeSpan.Zero;
105
106      if (!unlimitedRuntime) {
[16405]107        timeLimit -= executionTime;
[16233]108      }
[16234]109
[16233]110      var iterations = (long)timeLimit.TotalMilliseconds / (long)QualityUpdateInterval.TotalMilliseconds;
111      var remaining = timeLimit - TimeSpan.FromMilliseconds(iterations * QualityUpdateInterval.TotalMilliseconds);
[16373]112      var validResultStatuses = new[] { ResultStatus.NotSolved, ResultStatus.Feasible };
[16233]113
114      while (unlimitedRuntime || iterations > 0) {
[16373]115        if (cancellationToken.IsCancellationRequested)
116          return;
117
[16582]118        stopwatch.Start();
[16405]119        Solve(problemDefinition, results, IntermediateTimeLimit);
[16582]120        stopwatch.Stop();
121        executionTime += stopwatch.Elapsed;
[16405]122        UpdateQuality(results, executionTime);
[16233]123
[16405]124        var resultStatus = ((EnumValue<ResultStatus>)results["ResultStatus"].Value).Value;
[16373]125        if (!validResultStatuses.Contains(resultStatus))
[16233]126          return;
127
128        if (!unlimitedRuntime)
129          iterations--;
130      }
131
132      if (remaining > TimeSpan.Zero) {
[16405]133        Solve(problemDefinition, results, remaining);
134        UpdateQuality(results, executionTime);
[16233]135      }
136    }
137
[16405]138    private void UpdateQuality(ResultCollection results, TimeSpan executionTime) {
[16582]139      IndexedDataRow<double> qpcRow;
140      IndexedDataRow<double> bpcRow;
141
[16405]142      if (!results.Exists(r => r.Name == "QualityPerClock")) {
[16233]143        qualityPerClock = new IndexedDataTable<double>("Quality per Clock");
[16373]144        qpcRow = new IndexedDataRow<double>("Objective Value");
145        bpcRow = new IndexedDataRow<double>("Bound");
[16582]146        qualityPerClock.Rows.Add(qpcRow);
147        qualityPerClock.Rows.Add(bpcRow);
[16405]148        results.AddOrUpdateResult("QualityPerClock", qualityPerClock);
[16582]149      } else {
150        qpcRow = qualityPerClock.Rows["Objective Value"];
151        bpcRow = qualityPerClock.Rows["Bound"];
[16233]152      }
153
[16405]154      var resultStatus = ((EnumValue<ResultStatus>)results["ResultStatus"].Value).Value;
[16233]155
[16373]156      if (new[] { ResultStatus.Abnormal, ResultStatus.NotSolved, ResultStatus.Unbounded }.Contains(resultStatus))
[16233]157        return;
158
[16405]159      var objective = ((DoubleValue)results["BestObjectiveValue"].Value).Value;
[16582]160      var bound = solver.IsMip() ? ((DoubleValue)results["BestObjectiveBound"].Value).Value : double.NaN;
[16405]161      var time = executionTime.TotalSeconds;
[16233]162
163      if (!qpcRow.Values.Any()) {
164        if (!double.IsInfinity(objective) && !double.IsNaN(objective)) {
165          qpcRow.Values.Add(Tuple.Create(time, objective));
166          qpcRow.Values.Add(Tuple.Create(time, objective));
[16405]167          results.AddOrUpdateResult("BestObjectiveValueFoundAt", new TimeSpanValue(TimeSpan.FromSeconds(time)));
[16233]168        }
169      } else {
170        var previousBest = qpcRow.Values.Last().Item2;
171        qpcRow.Values[qpcRow.Values.Count - 1] = Tuple.Create(time, objective);
172        if (!objective.IsAlmost(previousBest)) {
173          qpcRow.Values.Add(Tuple.Create(time, objective));
[16405]174          results.AddOrUpdateResult("BestObjectiveValueFoundAt", new TimeSpanValue(TimeSpan.FromSeconds(time)));
[16233]175        }
176      }
177
[16582]178      if (!solver.IsMip())
[16373]179        return;
180
[16233]181      if (!bpcRow.Values.Any()) {
182        if (!double.IsInfinity(bound) && !double.IsNaN(bound)) {
183          bpcRow.Values.Add(Tuple.Create(time, bound));
184          bpcRow.Values.Add(Tuple.Create(time, bound));
185        }
186      } else {
187        var previousBest = bpcRow.Values.Last().Item2;
188        bpcRow.Values[bpcRow.Values.Count - 1] = Tuple.Create(time, bound);
189        if (!bound.IsAlmost(previousBest)) {
190          bpcRow.Values.Add(Tuple.Create(time, bound));
191        }
192      }
193    }
194  }
[16288]195}
Note: See TracBrowser for help on using the repository browser.