source: trunk/sources/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs @ 15452

Last change on this file since 15452 was 15452, checked in by pfleck, 4 years ago

#2822

  • Removed ManualResetEventSlim allOptimizerFinished.
  • Reworked execution state transitions into simple, documented rules
  • Fixed potentiall NullReferenceException in ExperimentView
File size: 21.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Drawing;
25using System.Linq;
26using System.Threading;
27using System.Threading.Tasks;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Optimization {
34  /// <summary>
35  /// An experiment which contains multiple algorithms, batch runs or other experiments.
36  /// </summary>
37  [Item("Experiment", "An experiment which contains multiple algorithms, batch runs or other experiments.")]
38  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 100)]
39  [StorableClass]
40  public sealed class Experiment : NamedItem, IOptimizer, IStorableContent {
41    public string Filename { get; set; }
42
43    public static new Image StaticItemImage {
44      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
45    }
46    public override Image ItemImage {
47      get {
48        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExperimentPrepared;
49        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExperimentStarted;
50        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExperimentPaused;
51        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExperimentStopped;
52        else return base.ItemImage;
53      }
54    }
55
56    [Storable]
57    private ExecutionState executionState;
58    public ExecutionState ExecutionState {
59      get { return executionState; }
60      private set {
61        if (executionState != value) {
62          executionState = value;
63          OnExecutionStateChanged();
64          OnItemImageChanged();
65        }
66      }
67    }
68
69    [Storable]
70    private TimeSpan executionTime;
71    public TimeSpan ExecutionTime {
72      get { return executionTime; }
73      private set {
74        executionTime = value;
75        OnExecutionTimeChanged();
76      }
77    }
78
79    [Storable]
80    private OptimizerList optimizers;
81    public OptimizerList Optimizers {
82      get { return optimizers; }
83    }
84
85    [Storable]
86    private RunCollection runs;
87    public RunCollection Runs {
88      get { return runs; }
89      private set {
90        if (value == null) throw new ArgumentNullException();
91        if (runs != value) {
92          if (runs != null) DeregisterRunsEvents();
93          runs = value;
94          if (runs != null) RegisterRunsEvents();
95        }
96      }
97    }
98
99    [Storable]
100    private int numberOfWorkers = 1;
101    public int NumberOfWorkers {
102      get { return numberOfWorkers; }
103      set {
104        if (value < 1) throw new ArgumentException("Number of Workers must not be lower than one.");
105        numberOfWorkers = value;
106      }
107    }
108
109    public IEnumerable<IOptimizer> NestedOptimizers {
110      get {
111        if (Optimizers == null) yield break;
112
113        foreach (IOptimizer opt in Optimizers) {
114          yield return opt;
115          foreach (IOptimizer nestedOpt in opt.NestedOptimizers)
116            yield return nestedOpt;
117        }
118      }
119    }
120
121    private bool experimentStarted = false;
122    private bool experimentStopped = false;
123
124    // track already started optimizers (.StartAsync does not set the executionstate immediately)
125    // and to avoid restarting optimizers that were manually paused/stopped by the user
126    private readonly IDictionary<IOptimizer, Task> startedOptimizers = new Dictionary<IOptimizer, Task>();
127    private IEnumerable<IOptimizer> StartableOptimizers {
128      get {
129        return Optimizers
130          .Where(x => x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused)
131          .Where(o => !startedOptimizers.ContainsKey(o));  // all startable optimizers that were not startet yet
132      }
133    }
134
135    public Experiment()
136      : base() {
137      name = ItemName;
138      description = ItemDescription;
139      executionState = ExecutionState.Stopped;
140      executionTime = TimeSpan.Zero;
141      optimizers = new OptimizerList();
142      Runs = new RunCollection { OptimizerName = Name };
143      Initialize();
144    }
145    public Experiment(string name)
146      : base(name) {
147      description = ItemDescription;
148      executionState = ExecutionState.Stopped;
149      executionTime = TimeSpan.Zero;
150      optimizers = new OptimizerList();
151      Runs = new RunCollection { OptimizerName = Name };
152      Initialize();
153    }
154    public Experiment(string name, string description)
155      : base(name, description) {
156      executionState = ExecutionState.Stopped;
157      executionTime = TimeSpan.Zero;
158      optimizers = new OptimizerList();
159      Runs = new RunCollection { OptimizerName = Name };
160      Initialize();
161    }
162    [StorableConstructor]
163    private Experiment(bool deserializing) : base(deserializing) { }
164    [StorableHook(HookType.AfterDeserialization)]
165    private void AfterDeserialization() {
166      Initialize();
167    }
168    private Experiment(Experiment original, Cloner cloner)
169      : base(original, cloner) {
170      executionState = original.executionState;
171      executionTime = original.executionTime;
172      optimizers = cloner.Clone(original.optimizers);
173      runs = cloner.Clone(original.runs);
174
175      experimentStarted = original.experimentStarted;
176      experimentStopped = original.experimentStopped;
177      numberOfWorkers = original.numberOfWorkers;
178      Initialize();
179    }
180    public override IDeepCloneable Clone(Cloner cloner) {
181      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
182      return new Experiment(this, cloner);
183    }
184
185    private void Initialize() {
186      RegisterOptimizersEvents();
187      foreach (IOptimizer optimizer in optimizers)
188        RegisterOptimizerEvents(optimizer);
189      if (runs != null) RegisterRunsEvents();
190    }
191
192    public void Prepare() {
193      Prepare(false);
194    }
195    public void Prepare(bool clearRuns) {
196      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
197        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
198      if (Optimizers.Count == 0) return;
199
200      if (clearRuns) runs.Clear();
201
202      experimentStarted = false;
203      experimentStopped = false;
204      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) {
205        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
206        try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
207      }
208    }
209    public void Start() {
210      Start(CancellationToken.None);
211    }
212    public void Start(CancellationToken cancellationToken) {
213      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
214        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
215
216      startedOptimizers.Clear();
217      if (!StartableOptimizers.Any()) return;
218
219      experimentStarted = true;
220      experimentStopped = false;
221
222      using (var availableWorkers = new SemaphoreSlim(NumberOfWorkers, NumberOfWorkers)) {
223        while (StartableOptimizers.Any()) {
224          try {
225            availableWorkers.Wait(cancellationToken);
226            var optimizer = StartableOptimizers.FirstOrDefault();
227            if (experimentStopped || !experimentStarted || optimizer == null) break;
228
229            var startedTask = optimizer.StartAsync(cancellationToken).ContinueWith(async t => {
230              availableWorkers.Release(); // is guaranteed to be not disposed yet because Task.WaitAll blocks before the end of the using
231              await t; // trigger a potential exception on the optimizerTask
232            });
233            startedOptimizers.Add(optimizer, startedTask.Unwrap()); // unwrap task because lambda of .ContinueWith is async
234          } catch (InvalidOperationException) { } catch (OperationCanceledException) { }
235        }
236
237        Task.WaitAll(startedOptimizers.Values.ToArray()); // retreive exeptions of the asyncrounously started optimizer
238      }
239    }
240    public async Task StartAsync() { await StartAsync(CancellationToken.None); }
241    public async Task StartAsync(CancellationToken cancellationToken) {
242      await AsyncHelper.DoAsync(Start, cancellationToken);
243    }
244    public void Pause() {
245      if (ExecutionState != ExecutionState.Started)
246        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
247      if (Optimizers.Count == 0) return;
248
249      experimentStarted = false;
250      experimentStopped = false;
251      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) {
252        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
253        try { optimizer.Pause(); } catch (InvalidOperationException) { } catch (NotSupportedException) { }
254      }
255    }
256    public void Stop() {
257      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
258        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
259      if (Optimizers.Count == 0) return;
260
261      experimentStarted = false;
262      experimentStopped = true;
263      if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
264        foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
265          // a race-condition may occur when the optimizer has changed the state by itself in the meantime
266          try { optimizer.Stop(); } catch (InvalidOperationException) { }
267        }
268      } else {
269        OnStopped();
270      }
271    }
272
273    #region Events
274    protected override void OnNameChanged() {
275      base.OnNameChanged();
276      Runs.OptimizerName = Name;
277    }
278
279    public event EventHandler ExecutionStateChanged;
280    private void OnExecutionStateChanged() {
281      EventHandler handler = ExecutionStateChanged;
282      if (handler != null) handler(this, EventArgs.Empty);
283    }
284    public event EventHandler ExecutionTimeChanged;
285    private void OnExecutionTimeChanged() {
286      EventHandler handler = ExecutionTimeChanged;
287      if (handler != null) handler(this, EventArgs.Empty);
288    }
289    public event EventHandler Prepared;
290    private void OnPrepared() {
291      ExecutionState = ExecutionState.Prepared;
292      EventHandler handler = Prepared;
293      if (handler != null) handler(this, EventArgs.Empty);
294    }
295    public event EventHandler Started;
296    private void OnStarted() {
297      ExecutionState = ExecutionState.Started;
298      EventHandler handler = Started;
299      if (handler != null) handler(this, EventArgs.Empty);
300    }
301    public event EventHandler Paused;
302    private void OnPaused() {
303      ExecutionState = ExecutionState.Paused;
304      EventHandler handler = Paused;
305      if (handler != null) handler(this, EventArgs.Empty);
306    }
307    public event EventHandler Stopped;
308    private void OnStopped() {
309      ExecutionState = ExecutionState.Stopped;
310      EventHandler handler = Stopped;
311      if (handler != null) handler(this, EventArgs.Empty);
312    }
313    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
314    private void OnExceptionOccurred(Exception exception) {
315      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
316      if (handler != null) handler(this, new EventArgs<Exception>(exception));
317    }
318
319    private void RegisterOptimizersEvents() {
320      Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
321      Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
322      Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
323      Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
324    }
325    private void DeregisterOptimizersEvents() {
326      Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
327      Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
328      Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
329      Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
330    }
331    private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
332      foreach (IndexedItem<IOptimizer> item in e.OldItems)
333        RemoveOptimizer(item.Value);
334      foreach (IndexedItem<IOptimizer> item in e.Items)
335        AddOptimizer(item.Value);
336    }
337    private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
338      foreach (IndexedItem<IOptimizer> item in e.Items)
339        AddOptimizer(item.Value);
340    }
341    private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
342      foreach (IndexedItem<IOptimizer> item in e.Items)
343        RemoveOptimizer(item.Value);
344    }
345    private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
346      foreach (IndexedItem<IOptimizer> item in e.OldItems)
347        RemoveOptimizer(item.Value);
348      foreach (IndexedItem<IOptimizer> item in e.Items)
349        AddOptimizer(item.Value);
350    }
351    private void AddOptimizer(IOptimizer optimizer) {
352      RegisterOptimizerEvents(optimizer);
353      Runs.AddRange(optimizer.Runs);
354      optimizer.Prepare();
355      if (ExecutionState == ExecutionState.Stopped && optimizer.ExecutionState == ExecutionState.Prepared)
356        OnPrepared();
357    }
358    private void RemoveOptimizer(IOptimizer optimizer) {
359      DeregisterOptimizerEvents(optimizer);
360      Runs.RemoveRange(optimizer.Runs);
361      if (ExecutionState == ExecutionState.Prepared && !optimizers.Any(opt => opt.ExecutionState == ExecutionState.Prepared))
362        OnStopped();
363    }
364
365    private void RegisterOptimizerEvents(IOptimizer optimizer) {
366      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
367      optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
368      optimizer.Paused += new EventHandler(optimizer_Paused);
369      optimizer.Prepared += new EventHandler(optimizer_Prepared);
370      optimizer.Started += new EventHandler(optimizer_Started);
371      optimizer.Stopped += new EventHandler(optimizer_Stopped);
372      optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
373      optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
374      optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
375    }
376    private void DeregisterOptimizerEvents(IOptimizer optimizer) {
377      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
378      optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
379      optimizer.Paused -= new EventHandler(optimizer_Paused);
380      optimizer.Prepared -= new EventHandler(optimizer_Prepared);
381      optimizer.Started -= new EventHandler(optimizer_Started);
382      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
383      optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
384      optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
385      optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
386    }
387
388    private readonly object locker = new object();
389    private readonly object runsLocker = new object();
390    private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
391      lock (locker)
392        OnExceptionOccurred(e.Value);
393    }
394    private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
395      // only wait for maximally 100ms to acquire lock, otherwise return and don't update the execution time
396      var success = Monitor.TryEnter(locker, 100);
397      if (!success) return;
398      try {
399        ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
400      } finally {
401        Monitor.Exit(locker);
402      }
403    }
404    private void optimizer_Paused(object sender, EventArgs e) {
405      UpdateExecutionState();
406    }
407    private void optimizer_Prepared(object sender, EventArgs e) {
408      UpdateExecutionState();
409    }
410    private void optimizer_Started(object sender, EventArgs e) {
411      lock (locker)
412        if (ExecutionState != ExecutionState.Started) OnStarted();
413    }
414    private void optimizer_Stopped(object sender, EventArgs e) {
415      UpdateExecutionState();
416    }
417    private void UpdateExecutionState() {
418      // Execution states of the Experiment are determined using the following _basic_ rules:
419      //   if any Optimizer is Started      => Experiment is Started  (2. if)
420      //   if any Optimizer is Paused       => Experiment is Paused   (3. if)
421      //   if any Optimizer is Prepared     => Experiment is Prepared (5. if)
422      //   else (all Optimizer are Stopped) => Experiment is Stopped  (6. if)
423      // Additional there are two extra rules:
424      //   if the Experiment is running and there are still optimizers that can be started => keep the Experiment Running (1. if)
425      //   if experiment-stop is pending: Stop Experiment even if there are still Prepared Optimizer               (4. if)
426
427      lock (locker) {
428        // 1. experiment is running & further startable optimizers are available => continue executing
429        if (experimentStarted && StartableOptimizers.Any()) return;
430
431        // 2. any optimizer is running => continue executing
432        else if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Started))
433          return;
434
435        // 3. any optimizer is paused => experiment paused
436        else if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Paused))
437          OnPaused();
438
439        // 4. stop pending & all optimizers either stopped or prepared => experiment stopped
440        else if (experimentStopped)
441          OnStopped();
442
443        // 5. any optimizer prepared => experiment prepared
444        else if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Prepared))
445          OnPrepared();
446
447        // 6. (else) all optimizers stopped
448        else
449          OnStopped();
450      }
451    }
452
453    private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
454      lock (runsLocker) {
455        Runs.RemoveRange(e.OldItems);
456        Runs.AddRange(e.Items);
457      }
458    }
459    private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
460      lock (runsLocker)
461        Runs.AddRange(e.Items);
462    }
463    private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
464      lock (runsLocker)
465        Runs.RemoveRange(e.Items);
466    }
467
468    private void RegisterRunsEvents() {
469      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
470      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
471    }
472    private void DeregisterRunsEvents() {
473      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
474      runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
475    }
476    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
477      foreach (IOptimizer optimizer in Optimizers)
478        optimizer.Runs.RemoveRange(e.OldItems);
479    }
480    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
481      foreach (IOptimizer optimizer in Optimizers)
482        optimizer.Runs.RemoveRange(e.Items);
483    }
484    #endregion
485  }
486}
Note: See TracBrowser for help on using the repository browser.