Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs @ 15683

Last change on this file since 15683 was 15605, checked in by abeham, 7 years ago

#1614: merged trunk into branch

File size: 14.2 KB
Line 
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;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Linq;
27using System.Threading;
28using System.Threading.Tasks;
29using HeuristicLab.Collections;
30using HeuristicLab.Common;
31using HeuristicLab.Common.Resources;
32using HeuristicLab.Core;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34
35namespace HeuristicLab.Optimization {
36  /// <summary>
37  /// A run in which an algorithm is executed for a certain maximum time only.
38  /// </summary>
39  [Item("Timelimit Run", "A run in which an optimizer is executed a certain maximum time.")]
40  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 115)]
41  [StorableClass]
42  public sealed class TimeLimitRun : NamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged {
43    public string Filename { get; set; }
44
45    #region ItemImage
46    public static new Image StaticItemImage {
47      get { return VSImageLibrary.Event; }
48    }
49    public override Image ItemImage {
50      get { return (Algorithm != null) ? Algorithm.ItemImage : VSImageLibrary.ExecutableStopped; }
51    }
52    #endregion
53
54    private bool pausedForSnapshot = false;
55    private bool pausedForTermination = false;
56
57    [Storable]
58    private TimeSpan maximumExecutionTime;
59    public TimeSpan MaximumExecutionTime {
60      get { return maximumExecutionTime; }
61      set {
62        if (maximumExecutionTime == value) return;
63        maximumExecutionTime = value;
64        OnPropertyChanged("MaximumExecutionTime");
65      }
66    }
67
68    [Storable]
69    private int snapshotTimesIndex;
70    [Storable]
71    private ObservableList<TimeSpan> snapshotTimes;
72    public ObservableList<TimeSpan> SnapshotTimes {
73      get { return snapshotTimes; }
74      set {
75        if (snapshotTimes == value) return;
76        snapshotTimes = value;
77        snapshotTimes.Sort();
78        FindNextSnapshotTimeIndex(ExecutionTime);
79        OnPropertyChanged("SnapshotTimes");
80      }
81    }
82
83    [Storable]
84    private bool storeAlgorithmInEachSnapshot;
85    [Storable]
86    public bool StoreAlgorithmInEachSnapshot {
87      get { return storeAlgorithmInEachSnapshot; }
88      set {
89        if (storeAlgorithmInEachSnapshot == value) return;
90        storeAlgorithmInEachSnapshot = value;
91        OnPropertyChanged("StoreAlgorithmInEachSnapshot");
92      }
93    }
94
95    [Storable]
96    private RunCollection snapshots;
97    public RunCollection Snapshots {
98      get { return snapshots; }
99      set {
100        if (snapshots == value) return;
101        snapshots = value;
102        OnPropertyChanged("Snapshots");
103      }
104    }
105
106    #region Inherited Properties
107    public ExecutionState ExecutionState {
108      get { return (Algorithm != null) ? Algorithm.ExecutionState : ExecutionState.Stopped; }
109    }
110
111    public TimeSpan ExecutionTime {
112      get { return (Algorithm != null) ? Algorithm.ExecutionTime : TimeSpan.FromSeconds(0); }
113    }
114
115    [Storable]
116    private IAlgorithm algorithm;
117    public IAlgorithm Algorithm {
118      get { return algorithm; }
119      set {
120        if (algorithm == value) return;
121        if (algorithm != null) DeregisterAlgorithmEvents();
122        algorithm = value;
123        if (algorithm != null) {
124          RegisterAlgorithmEvents();
125        }
126        OnPropertyChanged("Algorithm");
127        Prepare();
128      }
129    }
130
131    [Storable]
132    private RunCollection runs;
133    public RunCollection Runs {
134      get { return runs; }
135      private set {
136        if (value == null) throw new ArgumentNullException();
137        if (runs == value) return;
138        runs = value;
139        OnPropertyChanged("Runs");
140      }
141    }
142
143    public IEnumerable<IOptimizer> NestedOptimizers {
144      get {
145        if (Algorithm == null) yield break;
146        yield return Algorithm;
147        foreach (var opt in Algorithm.NestedOptimizers)
148          yield return opt;
149      }
150    }
151    #endregion
152
153    [StorableConstructor]
154    private TimeLimitRun(bool deserializing) : base(deserializing) { }
155    private TimeLimitRun(TimeLimitRun original, Cloner cloner)
156      : base(original, cloner) {
157      maximumExecutionTime = original.maximumExecutionTime;
158      snapshotTimes = new ObservableList<TimeSpan>(original.snapshotTimes);
159      snapshotTimesIndex = original.snapshotTimesIndex;
160      snapshots = cloner.Clone(original.snapshots);
161      storeAlgorithmInEachSnapshot = original.storeAlgorithmInEachSnapshot;
162      algorithm = cloner.Clone(original.algorithm);
163      runs = cloner.Clone(original.runs);
164
165      Initialize();
166    }
167    public TimeLimitRun()
168      : base() {
169      name = ItemName;
170      description = ItemDescription;
171      maximumExecutionTime = TimeSpan.FromMinutes(.5);
172      snapshotTimes = new ObservableList<TimeSpan>(new[] {
173          TimeSpan.FromSeconds(5),
174          TimeSpan.FromSeconds(10),
175          TimeSpan.FromSeconds(15) });
176      snapshotTimesIndex = 0;
177      snapshots = new RunCollection();
178      Runs = new RunCollection { OptimizerName = Name };
179      Initialize();
180    }
181    public TimeLimitRun(string name)
182      : base(name) {
183      description = ItemDescription;
184      maximumExecutionTime = TimeSpan.FromMinutes(.5);
185      snapshotTimes = new ObservableList<TimeSpan>(new[] {
186          TimeSpan.FromSeconds(5),
187          TimeSpan.FromSeconds(10),
188          TimeSpan.FromSeconds(15) });
189      snapshotTimesIndex = 0;
190      Runs = new RunCollection { OptimizerName = Name };
191      Initialize();
192    }
193    public TimeLimitRun(string name, string description)
194      : base(name, description) {
195      maximumExecutionTime = TimeSpan.FromMinutes(.5);
196      snapshotTimes = new ObservableList<TimeSpan>(new[] {
197          TimeSpan.FromSeconds(5),
198          TimeSpan.FromSeconds(10),
199          TimeSpan.FromSeconds(15) });
200      snapshotTimesIndex = 0;
201      Runs = new RunCollection { OptimizerName = Name };
202      Initialize();
203    }
204
205    public override IDeepCloneable Clone(Cloner cloner) {
206      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
207      return new TimeLimitRun(this, cloner);
208    }
209
210    [StorableHook(HookType.AfterDeserialization)]
211    private void AfterDeserialization() {
212      Initialize();
213    }
214
215    private void Initialize() {
216      timer = new System.Timers.Timer();
217      timer.Interval = 50;
218      timer.Elapsed += Timer_Elapsed;
219      if (algorithm != null) RegisterAlgorithmEvents();
220      snapshotTimes.ItemsAdded += snapshotTimes_Changed;
221      snapshotTimes.ItemsMoved += snapshotTimes_Changed;
222      snapshotTimes.ItemsRemoved += snapshotTimes_Changed;
223      snapshotTimes.ItemsReplaced += snapshotTimes_Changed;
224      snapshotTimes.CollectionReset += snapshotTimes_Changed;
225    }
226
227    private void snapshotTimes_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<TimeSpan>> e) {
228      if (e.Items.Any()) snapshotTimes.Sort();
229      FindNextSnapshotTimeIndex(ExecutionTime);
230    }
231
232    public void Snapshot() {
233      if (Algorithm == null || Algorithm.ExecutionState != ExecutionState.Paused) throw new InvalidOperationException("Snapshot not allowed in execution states other than Paused");
234      Task.Factory.StartNew(MakeSnapshot);
235    }
236
237    public void Prepare() {
238      Prepare(false);
239    }
240    public void Prepare(bool clearRuns) {
241      Algorithm.Prepare(clearRuns);
242    }
243    public void Start() {
244      Start(CancellationToken.None);
245    }
246    public void Start(CancellationToken cancellationToken) {
247      Algorithm.Start(cancellationToken);
248    }
249    public async Task StartAsync() { await StartAsync(CancellationToken.None); }
250    public async Task StartAsync(CancellationToken cancellationToken) {
251      await AsyncHelper.DoAsync(Start, cancellationToken);
252    }
253    public void Pause() {
254      Algorithm.Pause();
255    }
256    public void Stop() {
257      Algorithm.Stop();
258    }
259
260    private System.Timers.Timer timer;
261
262    private void Timer_Elapsed(object sender, EventArgs e) {
263      if (Algorithm.ExecutionState == ExecutionState.Started) {
264        if (snapshotTimesIndex < SnapshotTimes.Count && ExecutionTime >= SnapshotTimes[snapshotTimesIndex]
265          && !pausedForSnapshot) {
266          pausedForSnapshot = true;
267          Algorithm.Pause();
268        }
269        if (ExecutionTime >= MaximumExecutionTime && !pausedForTermination) {
270          pausedForTermination = true;
271          if (!pausedForSnapshot) Algorithm.Pause();
272        }
273      }
274    }
275
276    #region Events
277    protected override void OnNameChanged() {
278      base.OnNameChanged();
279      runs.OptimizerName = Name;
280    }
281
282    public event PropertyChangedEventHandler PropertyChanged;
283    private void OnPropertyChanged(string property) {
284      var handler = PropertyChanged;
285      if (handler != null) handler(this, new PropertyChangedEventArgs(property));
286    }
287
288    #region IExecutable Events
289    public event EventHandler ExecutionStateChanged;
290    private void OnExecutionStateChanged() {
291      var handler = ExecutionStateChanged;
292      if (handler != null) handler(this, EventArgs.Empty);
293    }
294    [Obsolete("Deprecated needs to be removed")]
295    public event EventHandler ExecutionTimeChanged;
296    public event EventHandler Prepared;
297    private void OnPrepared() {
298      var handler = Prepared;
299      if (handler != null) handler(this, EventArgs.Empty);
300    }
301    public event EventHandler Started;
302    private void OnStarted() {
303      timer.Start();
304      var handler = Started;
305      if (handler != null) handler(this, EventArgs.Empty);
306    }
307    public event EventHandler Paused;
308    private void OnPaused() {
309      timer.Stop();
310      var handler = Paused;
311      if (handler != null) handler(this, EventArgs.Empty);
312    }
313    public event EventHandler Stopped;
314    private void OnStopped() {
315      timer.Stop();
316      var handler = Stopped;
317      if (handler != null) handler(this, EventArgs.Empty);
318    }
319    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
320    private void OnExceptionOccurred(Exception exception) {
321      var handler = ExceptionOccurred;
322      if (handler != null) handler(this, new EventArgs<Exception>(exception));
323    }
324    #endregion
325
326    #region Algorithm Events
327    private void RegisterAlgorithmEvents() {
328      algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
329      algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged;
330      algorithm.Paused += Algorithm_Paused;
331      algorithm.Prepared += Algorithm_Prepared;
332      algorithm.Started += Algorithm_Started;
333      algorithm.Stopped += Algorithm_Stopped;
334    }
335    private void DeregisterAlgorithmEvents() {
336      algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
337      algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged;
338      algorithm.Paused -= Algorithm_Paused;
339      algorithm.Prepared -= Algorithm_Prepared;
340      algorithm.Started -= Algorithm_Started;
341      algorithm.Stopped -= Algorithm_Stopped;
342    }
343    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
344      OnExceptionOccurred(e.Value);
345    }
346    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
347      OnExecutionStateChanged();
348    }
349    private void Algorithm_Paused(object sender, EventArgs e) {
350      var action = pausedForTermination ? ExecutionState.Stopped : (pausedForSnapshot ? ExecutionState.Started : ExecutionState.Paused);
351      if (pausedForSnapshot || pausedForTermination) {
352        pausedForSnapshot = pausedForTermination = false;
353        MakeSnapshot();
354        FindNextSnapshotTimeIndex(ExecutionTime);
355      }
356      OnPaused();
357      if (action == ExecutionState.Started) Algorithm.Start();
358      else if (action == ExecutionState.Stopped) Algorithm.Stop();
359    }
360    private void Algorithm_Prepared(object sender, EventArgs e) {
361      snapshotTimesIndex = 0;
362      snapshots.Clear();
363      OnPrepared();
364    }
365    private void Algorithm_Started(object sender, EventArgs e) {
366      OnStarted();
367    }
368    private void Algorithm_Stopped(object sender, EventArgs e) {
369      var cloner = new Cloner();
370      var algRun = cloner.Clone(Algorithm.Runs.Last());
371      var clonedSnapshots = cloner.Clone(snapshots);
372      algRun.Results.Add("TimeLimitRunSnapshots", clonedSnapshots);
373      Runs.Add(algRun);
374      Algorithm.Runs.Clear();
375      OnStopped();
376    }
377    #endregion
378    #endregion
379
380    private void FindNextSnapshotTimeIndex(TimeSpan reference) {
381      var index = 0;
382      while (index < snapshotTimes.Count && snapshotTimes[index] <= reference) {
383        index++;
384      };
385      snapshotTimesIndex = index;
386    }
387
388    private void MakeSnapshot() {
389      string time = Math.Round(ExecutionTime.TotalSeconds, 1).ToString("0.0");
390      string runName = "Snapshot " + time + "s " + algorithm.Name;
391      var changed = false;
392      if (StoreAlgorithmInEachSnapshot && !Algorithm.StoreAlgorithmInEachRun) {
393        Algorithm.StoreAlgorithmInEachRun = true;
394        changed = true;
395      } else if (!StoreAlgorithmInEachSnapshot && Algorithm.StoreAlgorithmInEachRun) {
396        Algorithm.StoreAlgorithmInEachRun = false;
397        changed = true;
398      }
399      var run = new Run(runName, Algorithm);
400      if (changed)
401        Algorithm.StoreAlgorithmInEachRun = !Algorithm.StoreAlgorithmInEachRun;
402
403      snapshots.Add(run);
404    }
405  }
406}
Note: See TracBrowser for help on using the repository browser.