Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs @ 16003

Last change on this file since 16003 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 21.6 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.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()); // retrieve 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      if (ExecutionState == ExecutionState.Prepared) return;
292      ExecutionState = ExecutionState.Prepared;
293      EventHandler handler = Prepared;
294      if (handler != null) handler(this, EventArgs.Empty);
295    }
296    public event EventHandler Started;
297    private void OnStarted() {
298      if (ExecutionState == ExecutionState.Started) return;
299      ExecutionState = ExecutionState.Started;
300      EventHandler handler = Started;
301      if (handler != null) handler(this, EventArgs.Empty);
302    }
303    public event EventHandler Paused;
304    private void OnPaused() {
305      if (ExecutionState == ExecutionState.Paused) return;
306      ExecutionState = ExecutionState.Paused;
307      EventHandler handler = Paused;
308      if (handler != null) handler(this, EventArgs.Empty);
309    }
310    public event EventHandler Stopped;
311    private void OnStopped() {
312      if (ExecutionState == ExecutionState.Stopped) return;
313      ExecutionState = ExecutionState.Stopped;
314      EventHandler handler = Stopped;
315      if (handler != null) handler(this, EventArgs.Empty);
316    }
317    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
318    private void OnExceptionOccurred(Exception exception) {
319      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
320      if (handler != null) handler(this, new EventArgs<Exception>(exception));
321    }
322
323    private void RegisterOptimizersEvents() {
324      Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
325      Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
326      Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
327      Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
328    }
329    private void DeregisterOptimizersEvents() {
330      Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
331      Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
332      Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
333      Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
334    }
335    private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
336      foreach (IndexedItem<IOptimizer> item in e.OldItems)
337        RemoveOptimizer(item.Value);
338      foreach (IndexedItem<IOptimizer> item in e.Items)
339        AddOptimizer(item.Value);
340    }
341    private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
342      foreach (IndexedItem<IOptimizer> item in e.Items)
343        AddOptimizer(item.Value);
344    }
345    private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
346      foreach (IndexedItem<IOptimizer> item in e.Items)
347        RemoveOptimizer(item.Value);
348    }
349    private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
350      foreach (IndexedItem<IOptimizer> item in e.OldItems)
351        RemoveOptimizer(item.Value);
352      foreach (IndexedItem<IOptimizer> item in e.Items)
353        AddOptimizer(item.Value);
354    }
355    private void AddOptimizer(IOptimizer optimizer) {
356      RegisterOptimizerEvents(optimizer);
357      Runs.AddRange(optimizer.Runs);
358      optimizer.Prepare();
359      if (ExecutionState == ExecutionState.Stopped && optimizer.ExecutionState == ExecutionState.Prepared)
360        OnPrepared();
361    }
362    private void RemoveOptimizer(IOptimizer optimizer) {
363      DeregisterOptimizerEvents(optimizer);
364      Runs.RemoveRange(optimizer.Runs);
365      if (ExecutionState == ExecutionState.Prepared && !optimizers.Any(opt => opt.ExecutionState == ExecutionState.Prepared))
366        OnStopped();
367    }
368
369    private void RegisterOptimizerEvents(IOptimizer optimizer) {
370      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
371      optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
372      optimizer.Paused += new EventHandler(optimizer_Paused);
373      optimizer.Prepared += new EventHandler(optimizer_Prepared);
374      optimizer.Started += new EventHandler(optimizer_Started);
375      optimizer.Stopped += new EventHandler(optimizer_Stopped);
376      optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
377      optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
378      optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
379    }
380    private void DeregisterOptimizerEvents(IOptimizer optimizer) {
381      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
382      optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
383      optimizer.Paused -= new EventHandler(optimizer_Paused);
384      optimizer.Prepared -= new EventHandler(optimizer_Prepared);
385      optimizer.Started -= new EventHandler(optimizer_Started);
386      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
387      optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
388      optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
389      optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
390    }
391
392    private readonly object locker = new object();
393    private readonly object runsLocker = new object();
394    private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
395      lock (locker)
396        OnExceptionOccurred(e.Value);
397    }
398    private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
399      // only wait for maximally 100ms to acquire lock, otherwise return and don't update the execution time
400      var success = Monitor.TryEnter(locker, 100);
401      if (!success) return;
402      try {
403        ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
404      } finally {
405        Monitor.Exit(locker);
406      }
407    }
408    private void optimizer_Paused(object sender, EventArgs e) {
409      UpdateExecutionState();
410    }
411    private void optimizer_Prepared(object sender, EventArgs e) {
412      UpdateExecutionState();
413    }
414    private void optimizer_Started(object sender, EventArgs e) {
415      lock (locker)
416        if (ExecutionState != ExecutionState.Started) OnStarted();
417    }
418    private void optimizer_Stopped(object sender, EventArgs e) {
419      UpdateExecutionState();
420    }
421    private void UpdateExecutionState() {
422      // Execution states of the Experiment are determined using the following _basic_ rules:
423      //   if any Optimizer is Started      => Experiment is Started  (2. if)
424      //   if any Optimizer is Paused       => Experiment is Paused   (3. if)
425      //   if any Optimizer is Prepared     => Experiment is Prepared (5. if)
426      //   else (all Optimizer are Stopped) => Experiment is Stopped  (6. if)
427      // Additional there are two extra rules:
428      //   if the Experiment is running and there are still optimizers that can be started => keep the Experiment Running (1. if)
429      //   if experiment-stop is pending: Stop Experiment even if there are still Prepared Optimizer               (4. if)
430
431      lock (locker) {
432        // 1. experiment is running & further startable optimizers are available => continue executing
433        if (experimentStarted && StartableOptimizers.Any())
434          return;
435
436        // 2. any optimizer is running => continue executing
437        if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Started))
438          return;
439
440        experimentStarted = false;
441        // 3. any optimizer is paused => experiment paused
442        if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Paused))
443          OnPaused();
444
445        // 4. stop pending & all optimizers either stopped or prepared => experiment stopped
446        else if (experimentStopped)
447          OnStopped();
448
449        // 5. any optimizer prepared => experiment prepared
450        else if (Optimizers.Any(x => x.ExecutionState == ExecutionState.Prepared))
451          OnPrepared();
452
453        // 6. (else) all optimizers stopped
454        else
455          OnStopped();
456      }
457    }
458
459    private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
460      lock (runsLocker) {
461        Runs.RemoveRange(e.OldItems);
462        Runs.AddRange(e.Items);
463      }
464    }
465    private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
466      lock (runsLocker)
467        Runs.AddRange(e.Items);
468    }
469    private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
470      lock (runsLocker)
471        Runs.RemoveRange(e.Items);
472    }
473
474    private void RegisterRunsEvents() {
475      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
476      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
477    }
478    private void DeregisterRunsEvents() {
479      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
480      runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
481    }
482    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
483      foreach (IOptimizer optimizer in Optimizers)
484        optimizer.Runs.RemoveRange(e.OldItems);
485    }
486    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
487      foreach (IOptimizer optimizer in Optimizers)
488        optimizer.Runs.RemoveRange(e.Items);
489    }
490    #endregion
491  }
492}
Note: See TracBrowser for help on using the repository browser.