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