#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Drawing;
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Optimization {
///
/// An experiment which contains multiple batch runs of algorithms.
///
[Item("Experiment", "An experiment which contains multiple batch runs of algorithms.")]
[Creatable("Testing & Analysis")]
[StorableClass]
public sealed class Experiment : NamedItem, IOptimizer {
public override Image ItemImage {
get {
if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared;
else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStarted;
else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePaused;
else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;
else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
}
}
[Storable]
private ExecutionState executionState;
public ExecutionState ExecutionState {
get { return executionState; }
private set {
if (executionState != value) {
executionState = value;
OnExecutionStateChanged();
OnItemImageChanged();
}
}
}
[Storable]
private TimeSpan executionTime;
public TimeSpan ExecutionTime {
get { return executionTime; }
private set {
executionTime = value;
OnExecutionTimeChanged();
}
}
[Storable]
private OptimizerList optimizers;
public OptimizerList Optimizers {
get { return optimizers; }
}
[Storable]
private RunCollection runs;
public RunCollection Runs {
get { return runs; }
}
private bool stopPending;
public Experiment()
: base() {
name = ItemName;
description = ItemDescription;
executionState = ExecutionState.Stopped;
executionTime = TimeSpan.Zero;
optimizers = new OptimizerList();
runs = new RunCollection();
stopPending = false;
Initialize();
}
public Experiment(string name)
: base(name) {
description = ItemDescription;
executionState = ExecutionState.Stopped;
executionTime = TimeSpan.Zero;
optimizers = new OptimizerList();
runs = new RunCollection();
stopPending = false;
Initialize();
}
public Experiment(string name, string description)
: base(name, description) {
executionState = ExecutionState.Stopped;
executionTime = TimeSpan.Zero;
optimizers = new OptimizerList();
runs = new RunCollection();
stopPending = false;
Initialize();
}
[StorableConstructor]
private Experiment(bool deserializing)
: base(deserializing) {
stopPending = false;
}
[StorableHook(HookType.AfterDeserialization)]
private void Initialize() {
RegisterOptimizersEvents();
foreach (IOptimizer optimizer in optimizers)
RegisterOptimizerEvents(optimizer);
}
public override IDeepCloneable Clone(Cloner cloner) {
if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
Experiment clone = (Experiment)base.Clone(cloner);
clone.executionState = executionState;
clone.executionTime = executionTime;
clone.optimizers = (OptimizerList)cloner.Clone(optimizers);
clone.runs = (RunCollection)cloner.Clone(runs);
clone.stopPending = stopPending;
clone.Initialize();
return clone;
}
public void Prepare() {
Prepare(false);
}
public void Prepare(bool clearRuns) {
if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
if (Optimizers.Count > 0) {
if (clearRuns) runs.Clear();
foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
optimizer.Prepare(clearRuns);
}
}
public void Start() {
if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
stopPending = false;
if (Optimizers.Count > 0) {
IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
if (optimizer != null) optimizer.Start();
}
}
public void Pause() {
if (ExecutionState != ExecutionState.Started)
throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
if (Optimizers.Count > 0) {
foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
optimizer.Pause();
}
}
public void Stop() {
if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
stopPending = true;
if (Optimizers.Count > 0) {
foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
optimizer.Stop();
}
}
#region Events
public event EventHandler ExecutionStateChanged;
private void OnExecutionStateChanged() {
EventHandler handler = ExecutionStateChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler ExecutionTimeChanged;
private void OnExecutionTimeChanged() {
EventHandler handler = ExecutionTimeChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Prepared;
private void OnPrepared() {
ExecutionState = ExecutionState.Prepared;
EventHandler handler = Prepared;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Started;
private void OnStarted() {
ExecutionState = ExecutionState.Started;
EventHandler handler = Started;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Paused;
private void OnPaused() {
ExecutionState = ExecutionState.Paused;
EventHandler handler = Paused;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Stopped;
private void OnStopped() {
ExecutionState = ExecutionState.Stopped;
EventHandler handler = Stopped;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler> ExceptionOccurred;
private void OnExceptionOccurred(Exception exception) {
EventHandler> handler = ExceptionOccurred;
if (handler != null) handler(this, new EventArgs(exception));
}
private void RegisterOptimizersEvents() {
Optimizers.CollectionReset += new CollectionItemsChangedEventHandler>(Optimizers_CollectionReset);
Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler>(Optimizers_ItemsAdded);
Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler>(Optimizers_ItemsRemoved);
Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler>(Optimizers_ItemsReplaced);
}
private void DeregisterOptimizersEvents() {
Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler>(Optimizers_CollectionReset);
Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler>(Optimizers_ItemsAdded);
Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler>(Optimizers_ItemsRemoved);
Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler>(Optimizers_ItemsReplaced);
}
private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.OldItems) {
DeregisterOptimizerEvents(item.Value);
}
foreach (IndexedItem item in e.Items) {
RegisterOptimizerEvents(item.Value);
}
}
private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.Items) {
RegisterOptimizerEvents(item.Value);
}
}
private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.Items) {
DeregisterOptimizerEvents(item.Value);
}
}
private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.OldItems) {
DeregisterOptimizerEvents(item.Value);
}
foreach (IndexedItem item in e.Items) {
RegisterOptimizerEvents(item.Value);
}
}
private void RegisterOptimizerEvents(IOptimizer optimizer) {
optimizer.ExceptionOccurred += new EventHandler>(optimizer_ExceptionOccurred);
optimizer.ExecutionTimeChanged += new EventHandler(optimizer_ExecutionTimeChanged);
optimizer.Paused += new EventHandler(optimizer_Paused);
optimizer.Prepared += new EventHandler(optimizer_Prepared);
optimizer.Started += new EventHandler(optimizer_Started);
optimizer.Stopped += new EventHandler(optimizer_Stopped);
optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler(optimizer_Runs_CollectionReset);
optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler(optimizer_Runs_ItemsAdded);
optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler(optimizer_Runs_ItemsRemoved);
}
private void DeregisterOptimizerEvents(IOptimizer optimizer) {
optimizer.ExceptionOccurred -= new EventHandler>(optimizer_ExceptionOccurred);
optimizer.ExecutionTimeChanged -= new EventHandler(optimizer_ExecutionTimeChanged);
optimizer.Paused -= new EventHandler(optimizer_Paused);
optimizer.Prepared -= new EventHandler(optimizer_Prepared);
optimizer.Started -= new EventHandler(optimizer_Started);
optimizer.Stopped -= new EventHandler(optimizer_Stopped);
optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler(optimizer_Runs_CollectionReset);
optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler(optimizer_Runs_ItemsAdded);
optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler(optimizer_Runs_ItemsRemoved);
}
private void optimizer_ExceptionOccurred(object sender, EventArgs e) {
OnExceptionOccurred(e.Value);
}
private void optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
ExecutionTime = Optimizers.Aggregate(TimeSpan.Zero, (t, o) => t + o.ExecutionTime);
}
private void optimizer_Paused(object sender, EventArgs e) {
if (Optimizers.All(x => x.ExecutionState != ExecutionState.Started))
OnPaused();
}
private void optimizer_Prepared(object sender, EventArgs e) {
if (ExecutionState == ExecutionState.Stopped)
OnPrepared();
}
private void optimizer_Started(object sender, EventArgs e) {
if (ExecutionState != ExecutionState.Started)
OnStarted();
}
private void optimizer_Stopped(object sender, EventArgs e) {
if (!stopPending && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
} else {
if (Optimizers.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) {
stopPending = false;
OnStopped();
}
}
}
private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
Runs.RemoveRange(e.OldItems);
Runs.AddRange(e.Items);
}
private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) {
Runs.AddRange(e.Items);
}
private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) {
Runs.RemoveRange(e.Items);
}
#endregion
}
}