[3226] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Drawing;
|
---|
[3265] | 24 | using HeuristicLab.Common;
|
---|
[3226] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3275] | 27 | using HeuristicLab.Collections;
|
---|
[3226] | 28 |
|
---|
| 29 | namespace HeuristicLab.Optimization {
|
---|
| 30 | /// <summary>
|
---|
| 31 | /// A run in which an algorithm is executed a given number of times.
|
---|
| 32 | /// </summary>
|
---|
| 33 | [Item("Batch Run", "A run in which an algorithm is executed a given number of times.")]
|
---|
| 34 | [Creatable("Testing & Analysis")]
|
---|
| 35 | [StorableClass]
|
---|
[3274] | 36 | public sealed class BatchRun : NamedItem, IOptimizer {
|
---|
[3226] | 37 | public override Image ItemImage {
|
---|
[3351] | 38 | get {
|
---|
| 39 | if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
|
---|
| 40 | else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VS2008ImageLibrary.Play;
|
---|
| 41 | else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VS2008ImageLibrary.Pause;
|
---|
| 42 | else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VS2008ImageLibrary.Stop;
|
---|
| 43 | else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
|
---|
| 44 | }
|
---|
[3226] | 45 | }
|
---|
| 46 |
|
---|
[3265] | 47 | [Storable]
|
---|
| 48 | private ExecutionState executionState;
|
---|
| 49 | public ExecutionState ExecutionState {
|
---|
| 50 | get { return executionState; }
|
---|
| 51 | private set {
|
---|
| 52 | if (executionState != value) {
|
---|
| 53 | executionState = value;
|
---|
| 54 | OnExecutionStateChanged();
|
---|
[3351] | 55 | OnItemImageChanged();
|
---|
[3265] | 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | [Storable]
|
---|
| 61 | private TimeSpan executionTime;
|
---|
| 62 | public TimeSpan ExecutionTime {
|
---|
| 63 | get {
|
---|
| 64 | if ((Algorithm != null) && (Algorithm.ExecutionState != ExecutionState.Stopped))
|
---|
| 65 | return executionTime + Algorithm.ExecutionTime;
|
---|
| 66 | else
|
---|
| 67 | return executionTime;
|
---|
| 68 | }
|
---|
| 69 | private set {
|
---|
| 70 | executionTime = value;
|
---|
| 71 | OnExecutionTimeChanged();
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[3280] | 75 | [Storable]
|
---|
[3226] | 76 | private IAlgorithm algorithm;
|
---|
| 77 | public IAlgorithm Algorithm {
|
---|
| 78 | get { return algorithm; }
|
---|
| 79 | set {
|
---|
| 80 | if (algorithm != value) {
|
---|
| 81 | if (algorithm != null) DeregisterAlgorithmEvents();
|
---|
| 82 | algorithm = value;
|
---|
| 83 | if (algorithm != null) RegisterAlgorithmEvents();
|
---|
| 84 | OnAlgorithmChanged();
|
---|
[3275] | 85 | Prepare(true);
|
---|
[3226] | 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | [Storable]
|
---|
| 91 | private int repetitions;
|
---|
| 92 | public int Repetitions {
|
---|
| 93 | get { return repetitions; }
|
---|
| 94 | set {
|
---|
| 95 | if (repetitions != value) {
|
---|
| 96 | repetitions = value;
|
---|
| 97 | OnRepetitionsChanged();
|
---|
[3265] | 98 | if ((runs.Count < repetitions) && (Algorithm != null) && (Algorithm.ExecutionState == ExecutionState.Stopped))
|
---|
[3275] | 99 | Prepare();
|
---|
[3226] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | [Storable]
|
---|
[3260] | 105 | private RunCollection runs;
|
---|
| 106 | public RunCollection Runs {
|
---|
| 107 | get { return runs; }
|
---|
[3226] | 108 | }
|
---|
| 109 |
|
---|
[3265] | 110 | private bool stopPending;
|
---|
[3226] | 111 |
|
---|
| 112 | public BatchRun()
|
---|
| 113 | : base() {
|
---|
[3280] | 114 | name = ItemName;
|
---|
| 115 | description = ItemDescription;
|
---|
[3265] | 116 | executionState = ExecutionState.Stopped;
|
---|
| 117 | executionTime = TimeSpan.Zero;
|
---|
[3226] | 118 | repetitions = 10;
|
---|
[3260] | 119 | runs = new RunCollection();
|
---|
[3265] | 120 | stopPending = false;
|
---|
[3226] | 121 | }
|
---|
[3280] | 122 | public BatchRun(string name)
|
---|
| 123 | : base(name) {
|
---|
| 124 | description = ItemDescription;
|
---|
[3265] | 125 | executionState = ExecutionState.Stopped;
|
---|
| 126 | executionTime = TimeSpan.Zero;
|
---|
[3226] | 127 | repetitions = 10;
|
---|
[3260] | 128 | runs = new RunCollection();
|
---|
[3265] | 129 | stopPending = false;
|
---|
[3226] | 130 | }
|
---|
[3280] | 131 | public BatchRun(string name, string description)
|
---|
| 132 | : base(name, description) {
|
---|
[3265] | 133 | executionState = ExecutionState.Stopped;
|
---|
| 134 | executionTime = TimeSpan.Zero;
|
---|
[3226] | 135 | repetitions = 10;
|
---|
[3260] | 136 | runs = new RunCollection();
|
---|
[3265] | 137 | stopPending = false;
|
---|
[3226] | 138 | }
|
---|
[3280] | 139 | [StorableConstructor]
|
---|
| 140 | private BatchRun(bool deserializing)
|
---|
| 141 | : base(deserializing) {
|
---|
| 142 | stopPending = false;
|
---|
| 143 | }
|
---|
[3226] | 144 |
|
---|
[3280] | 145 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 146 | private void Initialize() {
|
---|
| 147 | if (algorithm != null) RegisterAlgorithmEvents();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[3226] | 150 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3280] | 151 | if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[3226] | 152 | BatchRun clone = (BatchRun)base.Clone(cloner);
|
---|
[3265] | 153 | clone.executionState = executionState;
|
---|
| 154 | clone.executionTime = executionTime;
|
---|
[3280] | 155 | clone.algorithm = (IAlgorithm)cloner.Clone(algorithm);
|
---|
[3226] | 156 | clone.repetitions = repetitions;
|
---|
[3260] | 157 | clone.runs = (RunCollection)cloner.Clone(runs);
|
---|
[3265] | 158 | clone.stopPending = stopPending;
|
---|
[3280] | 159 | clone.Initialize();
|
---|
[3226] | 160 | return clone;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | public void Prepare() {
|
---|
[3275] | 164 | Prepare(false);
|
---|
[3226] | 165 | }
|
---|
[3275] | 166 | public void Prepare(bool clearRuns) {
|
---|
[3265] | 167 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
| 168 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[3261] | 169 | if (Algorithm != null) {
|
---|
[3275] | 170 | if (clearRuns) {
|
---|
[3265] | 171 | ExecutionTime = TimeSpan.Zero;
|
---|
| 172 | runs.Clear();
|
---|
| 173 | }
|
---|
[3275] | 174 | Algorithm.Prepare(clearRuns);
|
---|
[3261] | 175 | }
|
---|
[3226] | 176 | }
|
---|
[3265] | 177 | public void Start() {
|
---|
| 178 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
| 179 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 180 | if (Algorithm != null) Algorithm.Start();
|
---|
| 181 | }
|
---|
| 182 | public void Pause() {
|
---|
| 183 | if (ExecutionState != ExecutionState.Started)
|
---|
| 184 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 185 | if (Algorithm != null) Algorithm.Pause();
|
---|
| 186 | }
|
---|
[3226] | 187 | public void Stop() {
|
---|
[3265] | 188 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
| 189 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 190 | stopPending = true;
|
---|
| 191 | if (Algorithm != null) Algorithm.Stop();
|
---|
[3226] | 192 | }
|
---|
| 193 |
|
---|
| 194 | #region Events
|
---|
[3265] | 195 | public event EventHandler ExecutionStateChanged;
|
---|
| 196 | private void OnExecutionStateChanged() {
|
---|
| 197 | EventHandler handler = ExecutionStateChanged;
|
---|
| 198 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 199 | }
|
---|
| 200 | public event EventHandler ExecutionTimeChanged;
|
---|
| 201 | private void OnExecutionTimeChanged() {
|
---|
| 202 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 203 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 204 | }
|
---|
[3226] | 205 | public event EventHandler AlgorithmChanged;
|
---|
| 206 | private void OnAlgorithmChanged() {
|
---|
[3265] | 207 | EventHandler handler = AlgorithmChanged;
|
---|
| 208 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 209 | }
|
---|
| 210 | public event EventHandler RepetitionsChanged;
|
---|
| 211 | private void OnRepetitionsChanged() {
|
---|
[3265] | 212 | EventHandler handler = RepetitionsChanged;
|
---|
| 213 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 214 | }
|
---|
| 215 | public event EventHandler Prepared;
|
---|
| 216 | private void OnPrepared() {
|
---|
[3265] | 217 | ExecutionState = ExecutionState.Prepared;
|
---|
| 218 | EventHandler handler = Prepared;
|
---|
| 219 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 220 | }
|
---|
| 221 | public event EventHandler Started;
|
---|
| 222 | private void OnStarted() {
|
---|
[3265] | 223 | ExecutionState = ExecutionState.Started;
|
---|
| 224 | EventHandler handler = Started;
|
---|
| 225 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 226 | }
|
---|
[3265] | 227 | public event EventHandler Paused;
|
---|
| 228 | private void OnPaused() {
|
---|
| 229 | ExecutionState = ExecutionState.Paused;
|
---|
| 230 | EventHandler handler = Paused;
|
---|
| 231 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 232 | }
|
---|
[3226] | 233 | public event EventHandler Stopped;
|
---|
| 234 | private void OnStopped() {
|
---|
[3265] | 235 | ExecutionState = ExecutionState.Stopped;
|
---|
| 236 | EventHandler handler = Stopped;
|
---|
| 237 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 238 | }
|
---|
[3265] | 239 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
[3226] | 240 | private void OnExceptionOccurred(Exception exception) {
|
---|
[3265] | 241 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
| 242 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
[3226] | 243 | }
|
---|
| 244 |
|
---|
[3261] | 245 | private void RegisterAlgorithmEvents() {
|
---|
[3265] | 246 | algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
|
---|
| 247 | algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
|
---|
| 248 | algorithm.Paused += new EventHandler(Algorithm_Paused);
|
---|
| 249 | algorithm.Prepared += new EventHandler(Algorithm_Prepared);
|
---|
[3261] | 250 | algorithm.Started += new EventHandler(Algorithm_Started);
|
---|
| 251 | algorithm.Stopped += new EventHandler(Algorithm_Stopped);
|
---|
[3280] | 252 | algorithm.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_CollectionReset);
|
---|
| 253 | algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
|
---|
| 254 | algorithm.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsRemoved);
|
---|
[3261] | 255 | }
|
---|
[3226] | 256 | private void DeregisterAlgorithmEvents() {
|
---|
[3265] | 257 | algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
|
---|
| 258 | algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
|
---|
| 259 | algorithm.Paused -= new EventHandler(Algorithm_Paused);
|
---|
| 260 | algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
|
---|
[3261] | 261 | algorithm.Started -= new EventHandler(Algorithm_Started);
|
---|
| 262 | algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
|
---|
[3280] | 263 | algorithm.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_CollectionReset);
|
---|
| 264 | algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
|
---|
| 265 | algorithm.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsRemoved);
|
---|
[3226] | 266 | }
|
---|
[3265] | 267 | private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
[3261] | 268 | OnExceptionOccurred(e.Value);
|
---|
[3226] | 269 | }
|
---|
[3265] | 270 | private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 271 | OnExecutionTimeChanged();
|
---|
| 272 | }
|
---|
| 273 | private void Algorithm_Paused(object sender, EventArgs e) {
|
---|
| 274 | OnPaused();
|
---|
| 275 | }
|
---|
| 276 | private void Algorithm_Prepared(object sender, EventArgs e) {
|
---|
[3276] | 277 | if ((ExecutionState == ExecutionState.Paused) || (ExecutionState == ExecutionState.Stopped))
|
---|
| 278 | OnPrepared();
|
---|
[3265] | 279 | }
|
---|
[3261] | 280 | private void Algorithm_Started(object sender, EventArgs e) {
|
---|
[3265] | 281 | stopPending = false;
|
---|
[3276] | 282 | if (ExecutionState != ExecutionState.Started)
|
---|
| 283 | OnStarted();
|
---|
[3261] | 284 | }
|
---|
| 285 | private void Algorithm_Stopped(object sender, EventArgs e) {
|
---|
[3265] | 286 | ExecutionTime += Algorithm.ExecutionTime;
|
---|
| 287 |
|
---|
| 288 | if (!stopPending && (runs.Count < repetitions)) {
|
---|
[3261] | 289 | Algorithm.Prepare();
|
---|
[3265] | 290 | Algorithm.Start();
|
---|
[3276] | 291 | } else {
|
---|
| 292 | OnStopped();
|
---|
[3226] | 293 | }
|
---|
| 294 | }
|
---|
[3280] | 295 | private void Algorithm_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[3275] | 296 | Runs.RemoveRange(e.OldItems);
|
---|
| 297 | Runs.AddRange(e.Items);
|
---|
| 298 | }
|
---|
[3280] | 299 | private void Algorithm_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[3275] | 300 | Runs.AddRange(e.Items);
|
---|
| 301 | }
|
---|
[3280] | 302 | private void Algorithm_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[3275] | 303 | Runs.RemoveRange(e.Items);
|
---|
| 304 | }
|
---|
[3226] | 305 | #endregion
|
---|
| 306 | }
|
---|
| 307 | }
|
---|