Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs @ 12949

Last change on this file since 12949 was 12949, checked in by gkronber, 9 years ago

#2061: fixed a contention problem when updating the execution time of an experiment with many concurrent optimizers

File size: 18.6 KB
RevLine 
[3267]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3267]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;
[5419]23using System.Collections.Generic;
[3267]24using System.Drawing;
25using System.Linq;
[12949]26using System.Threading;
[3267]27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Optimization {
33  /// <summary>
[5300]34  /// An experiment which contains multiple algorithms, batch runs or other experiments.
[3267]35  /// </summary>
[5300]36  [Item("Experiment", "An experiment which contains multiple algorithms, batch runs or other experiments.")]
[12504]37  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 100)]
[3267]38  [StorableClass]
[4419]39  public sealed class Experiment : NamedItem, IOptimizer, IStorableContent {
40    public string Filename { get; set; }
41
[7201]42    public static new Image StaticItemImage {
43      get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
44    }
[3267]45    public override Image ItemImage {
[3351]46      get {
[6534]47        if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExperimentPrepared;
48        else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExperimentStarted;
49        else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExperimentPaused;
50        else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExperimentStopped;
[7201]51        else return base.ItemImage;
[3351]52      }
[3267]53    }
54
55    [Storable]
56    private ExecutionState executionState;
57    public ExecutionState ExecutionState {
58      get { return executionState; }
59      private set {
60        if (executionState != value) {
61          executionState = value;
62          OnExecutionStateChanged();
[3351]63          OnItemImageChanged();
[3267]64        }
65      }
66    }
67
68    [Storable]
69    private TimeSpan executionTime;
70    public TimeSpan ExecutionTime {
71      get { return executionTime; }
72      private set {
73        executionTime = value;
74        OnExecutionTimeChanged();
75      }
76    }
77
[3280]78    [Storable]
[3274]79    private OptimizerList optimizers;
80    public OptimizerList Optimizers {
81      get { return optimizers; }
[3267]82    }
83
[3275]84    [Storable]
85    private RunCollection runs;
86    public RunCollection Runs {
87      get { return runs; }
[3716]88      private set {
89        if (value == null) throw new ArgumentNullException();
90        if (runs != value) {
91          if (runs != null) DeregisterRunsEvents();
92          runs = value;
93          if (runs != null) RegisterRunsEvents();
94        }
95      }
[3275]96    }
97
[5419]98    public IEnumerable<IOptimizer> NestedOptimizers {
99      get {
100        if (Optimizers == null) yield break;
101
102        foreach (IOptimizer opt in Optimizers) {
103          yield return opt;
104          foreach (IOptimizer nestedOpt in opt.NestedOptimizers)
105            yield return nestedOpt;
106        }
107      }
108    }
109
[6767]110    private bool experimentStarted = false;
111    private bool experimentStopped = false;
[3267]112
113    public Experiment()
114      : base() {
[3280]115      name = ItemName;
116      description = ItemDescription;
[3267]117      executionState = ExecutionState.Stopped;
118      executionTime = TimeSpan.Zero;
[3280]119      optimizers = new OptimizerList();
[8975]120      Runs = new RunCollection { OptimizerName = Name };
[3280]121      Initialize();
[3267]122    }
[3280]123    public Experiment(string name)
124      : base(name) {
125      description = ItemDescription;
[3267]126      executionState = ExecutionState.Stopped;
127      executionTime = TimeSpan.Zero;
[3280]128      optimizers = new OptimizerList();
[8975]129      Runs = new RunCollection { OptimizerName = Name };
[3280]130      Initialize();
[3267]131    }
[3280]132    public Experiment(string name, string description)
133      : base(name, description) {
[3267]134      executionState = ExecutionState.Stopped;
135      executionTime = TimeSpan.Zero;
[3280]136      optimizers = new OptimizerList();
[8975]137      Runs = new RunCollection { OptimizerName = Name };
[3280]138      Initialize();
[3267]139    }
[3280]140    [StorableConstructor]
[6767]141    private Experiment(bool deserializing) : base(deserializing) { }
[4722]142    [StorableHook(HookType.AfterDeserialization)]
143    private void AfterDeserialization() {
144      Initialize();
145    }
146    private Experiment(Experiment original, Cloner cloner)
147      : base(original, cloner) {
148      executionState = original.executionState;
149      executionTime = original.executionTime;
150      optimizers = cloner.Clone(original.optimizers);
151      runs = cloner.Clone(original.runs);
[6767]152
153      experimentStarted = original.experimentStarted;
154      experimentStopped = original.experimentStopped;
[4722]155      Initialize();
156    }
157    public override IDeepCloneable Clone(Cloner cloner) {
158      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
159      return new Experiment(this, cloner);
160    }
[3267]161
[3280]162    private void Initialize() {
163      RegisterOptimizersEvents();
164      foreach (IOptimizer optimizer in optimizers)
165        RegisterOptimizerEvents(optimizer);
[3716]166      if (runs != null) RegisterRunsEvents();
[3280]167    }
168
[3267]169    public void Prepare() {
[3275]170      Prepare(false);
[3267]171    }
[3275]172    public void Prepare(bool clearRuns) {
[3267]173      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
174        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
[6767]175      if (Optimizers.Count == 0) return;
176
177      if (clearRuns) runs.Clear();
[7241]178
179      experimentStarted = false;
180      experimentStopped = false;
[8194]181      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) {
182        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
[12504]183        try { optimizer.Prepare(clearRuns); }
184        catch (InvalidOperationException) { }
[8194]185      }
[3267]186    }
187    public void Start() {
188      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
189        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
[6767]190      if (Optimizers.Count == 0) return;
191
192      experimentStarted = true;
193      experimentStopped = false;
194      IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
[8194]195      if (optimizer != null) {
196        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
[12504]197        try { optimizer.Start(); }
198        catch (InvalidOperationException) { }
[8194]199      }
[3267]200    }
201    public void Pause() {
202      if (ExecutionState != ExecutionState.Started)
203        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
[6767]204      if (Optimizers.Count == 0) return;
205
206      experimentStarted = false;
207      experimentStopped = false;
[8194]208      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) {
209        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
[12504]210        try { optimizer.Pause(); }
211        catch (InvalidOperationException) { }
[8194]212      }
[3267]213    }
214    public void Stop() {
215      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
216        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
[6767]217      if (Optimizers.Count == 0) return;
218
219      experimentStarted = false;
220      experimentStopped = true;
[7241]221      if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
[8194]222        foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
223          // a race-condition may occur when the optimizer has changed the state by itself in the meantime
[12504]224          try { optimizer.Stop(); }
225          catch (InvalidOperationException) { }
[8194]226        }
[7241]227      } else {
228        OnStopped();
229      }
[3267]230    }
231
232    #region Events
[8738]233    protected override void OnNameChanged() {
234      base.OnNameChanged();
[8975]235      Runs.OptimizerName = Name;
[8738]236    }
237
[3267]238    public event EventHandler ExecutionStateChanged;
239    private void OnExecutionStateChanged() {
240      EventHandler handler = ExecutionStateChanged;
241      if (handler != null) handler(this, EventArgs.Empty);
242    }
243    public event EventHandler ExecutionTimeChanged;
244    private void OnExecutionTimeChanged() {
245      EventHandler handler = ExecutionTimeChanged;
246      if (handler != null) handler(this, EventArgs.Empty);
247    }
248    public event EventHandler Prepared;
249    private void OnPrepared() {
250      ExecutionState = ExecutionState.Prepared;
251      EventHandler handler = Prepared;
252      if (handler != null) handler(this, EventArgs.Empty);
253    }
254    public event EventHandler Started;
255    private void OnStarted() {
256      ExecutionState = ExecutionState.Started;
257      EventHandler handler = Started;
258      if (handler != null) handler(this, EventArgs.Empty);
259    }
260    public event EventHandler Paused;
261    private void OnPaused() {
262      ExecutionState = ExecutionState.Paused;
263      EventHandler handler = Paused;
264      if (handler != null) handler(this, EventArgs.Empty);
265    }
266    public event EventHandler Stopped;
267    private void OnStopped() {
268      ExecutionState = ExecutionState.Stopped;
269      EventHandler handler = Stopped;
270      if (handler != null) handler(this, EventArgs.Empty);
271    }
272    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
273    private void OnExceptionOccurred(Exception exception) {
274      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
275      if (handler != null) handler(this, new EventArgs<Exception>(exception));
276    }
277
[3274]278    private void RegisterOptimizersEvents() {
279      Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
280      Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
281      Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
282      Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
[3267]283    }
[3274]284    private void DeregisterOptimizersEvents() {
285      Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
286      Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
287      Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
288      Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
[3267]289    }
[3274]290    private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
[4115]291      foreach (IndexedItem<IOptimizer> item in e.OldItems)
292        RemoveOptimizer(item.Value);
[4110]293      foreach (IndexedItem<IOptimizer> item in e.Items)
294        AddOptimizer(item.Value);
[3267]295    }
[3274]296    private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
[4110]297      foreach (IndexedItem<IOptimizer> item in e.Items)
298        AddOptimizer(item.Value);
[3267]299    }
[3274]300    private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
[4115]301      foreach (IndexedItem<IOptimizer> item in e.Items)
302        RemoveOptimizer(item.Value);
[3267]303    }
[3274]304    private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
[4115]305      foreach (IndexedItem<IOptimizer> item in e.OldItems)
306        RemoveOptimizer(item.Value);
[4110]307      foreach (IndexedItem<IOptimizer> item in e.Items)
308        AddOptimizer(item.Value);
[3267]309    }
[4110]310    private void AddOptimizer(IOptimizer optimizer) {
311      RegisterOptimizerEvents(optimizer);
312      Runs.AddRange(optimizer.Runs);
313      optimizer.Prepare();
[4551]314      if (ExecutionState == ExecutionState.Stopped && optimizer.ExecutionState == ExecutionState.Prepared)
315        OnPrepared();
[4110]316    }
[4115]317    private void RemoveOptimizer(IOptimizer optimizer) {
318      DeregisterOptimizerEvents(optimizer);
319      Runs.RemoveRange(optimizer.Runs);
[4551]320      if (ExecutionState == ExecutionState.Prepared && !optimizers.Any(opt => opt.ExecutionState == ExecutionState.Prepared))
321        OnStopped();
[4115]322    }
[3267]323
[3274]324    private void RegisterOptimizerEvents(IOptimizer optimizer) {
325      optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
326      optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
327      optimizer.Paused += new EventHandler(optimizer_Paused);
328      optimizer.Prepared += new EventHandler(optimizer_Prepared);
329      optimizer.Started += new EventHandler(optimizer_Started);
330      optimizer.Stopped += new EventHandler(optimizer_Stopped);
[3280]331      optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
332      optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
333      optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
[3267]334    }
[3274]335    private void DeregisterOptimizerEvents(IOptimizer optimizer) {
336      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
337      optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
338      optimizer.Paused -= new EventHandler(optimizer_Paused);
339      optimizer.Prepared -= new EventHandler(optimizer_Prepared);
340      optimizer.Started -= new EventHandler(optimizer_Started);
341      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
[3280]342      optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_CollectionReset);
343      optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsAdded);
344      optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(optimizer_Runs_ItemsRemoved);
[3267]345    }
[8170]346
347    private readonly object locker = new object();
[12949]348    private readonly object runsLocker = new object();
[3274]349    private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
[8170]350      lock (locker)
351        OnExceptionOccurred(e.Value);
[3267]352    }
[3274]353    private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
[12949]354      // only wait for maximally 100ms to acquire lock, otherwise return and don't update the execution time
355      var success = Monitor.TryEnter(locker, 100);
356      if (!success) return;
357      ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
358      Monitor.Exit(locker);
[3274]359    }
360    private void optimizer_Paused(object sender, EventArgs e) {
[8170]361      lock (locker)
362        if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started)) OnPaused();
[3267]363    }
[3274]364    private void optimizer_Prepared(object sender, EventArgs e) {
[8170]365      lock (locker)
366        if (Optimizers.All(x => x.ExecutionState == ExecutionState.Prepared)) OnPrepared();
[3267]367    }
[3274]368    private void optimizer_Started(object sender, EventArgs e) {
[8170]369      lock (locker)
370        if (ExecutionState != ExecutionState.Started) OnStarted();
[3267]371    }
[3274]372    private void optimizer_Stopped(object sender, EventArgs e) {
[8129]373      lock (locker) {
374        if (experimentStopped) {
375          if (Optimizers.All(x => (x.ExecutionState == ExecutionState.Stopped) || (x.ExecutionState == ExecutionState.Prepared))) OnStopped();
376        } else {
377          if (experimentStarted && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
378            Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
379          } else if (Optimizers.All(x => x.ExecutionState == ExecutionState.Stopped)) OnStopped();
380          else if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
381        }
[3267]382      }
383    }
[3280]384    private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12949]385      lock (runsLocker) {
[8170]386        Runs.RemoveRange(e.OldItems);
387        Runs.AddRange(e.Items);
388      }
[3275]389    }
[3280]390    private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12949]391      lock (runsLocker)
[8170]392        Runs.AddRange(e.Items);
[3275]393    }
[3280]394    private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[12949]395      lock (runsLocker)
[8170]396        Runs.RemoveRange(e.Items);
[3275]397    }
[3716]398
399    private void RegisterRunsEvents() {
400      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
401      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
402    }
403    private void DeregisterRunsEvents() {
404      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
405      runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
406    }
407    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
408      foreach (IOptimizer optimizer in Optimizers)
409        optimizer.Runs.RemoveRange(e.OldItems);
410    }
411    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
412      foreach (IOptimizer optimizer in Optimizers)
413        optimizer.Runs.RemoveRange(e.Items);
414    }
[3267]415    #endregion
416  }
417}
Note: See TracBrowser for help on using the repository browser.