Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs @ 3249

Last change on this file since 3249 was 3246, checked in by swagner, 15 years ago

Continued work on algorithm batch processing (#947).

File size: 7.4 KB
RevLine 
[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
22using System;
23using System.Drawing;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization {
29  /// <summary>
30  /// A run in which an algorithm is executed a given number of times.
31  /// </summary>
32  [Item("Batch Run", "A run in which an algorithm is executed a given number of times.")]
33  [Creatable("Testing & Analysis")]
34  [StorableClass]
35  public sealed class BatchRun : NamedItem {
36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
38    }
39
40    private IAlgorithm algorithm;
41    [Storable]
42    private IAlgorithm AlgorithmPersistence {
43      get { return algorithm; }
44      set {
45        if (algorithm != null) DeregisterAlgorithmEvents();
46        algorithm = value;
47        if (algorithm != null) RegisterAlgorithmEvents();
48      }
49    }
50    public IAlgorithm Algorithm {
51      get { return algorithm; }
52      set {
53        if (algorithm != value) {
54          if (algorithm != null) DeregisterAlgorithmEvents();
55          algorithm = value;
56          if (algorithm != null) RegisterAlgorithmEvents();
57          OnAlgorithmChanged();
58          Prepare();
59        }
60      }
61    }
62
63    [Storable]
64    private int repetitions;
65    public int Repetitions {
66      get { return repetitions; }
67      set {
68        if (repetitions != value) {
69          repetitions = value;
70          OnRepetitionsChanged();
71        }
72      }
73    }
74
75    [Storable]
[3246]76    private ResultCollection results;
77    public ResultCollection Results {
[3226]78      get { return results; }
79    }
80
81    [Storable]
82    private TimeSpan executionTime;
83    public TimeSpan ExecutionTime {
84      get { return executionTime; }
85      private set {
86        if (executionTime != value) {
87          executionTime = value;
88          OnExecutionTimeChanged();
89        }
90      }
91    }
92
93    private bool running;
94    public bool Running {
95      get { return running; }
96      private set {
97        if (running != value) {
98          running = value;
99          OnRunningChanged();
100        }
101      }
102    }
103
104    public bool Finished {
105      get { return ((Algorithm == null) || (Algorithm.Finished && (results.Count >= repetitions))); }
106    }
107
108    private bool canceled;
109
110    public BatchRun()
111      : base() {
112      repetitions = 10;
[3246]113      results = new ResultCollection();
[3226]114      executionTime = TimeSpan.Zero;
115    }
116    public BatchRun(string name) : base(name) {
117      repetitions = 10;
[3246]118      results = new ResultCollection();
[3226]119      executionTime = TimeSpan.Zero;
120    }
121    public BatchRun(string name, string description) : base(name, description) {
122      repetitions = 10;
[3246]123      results = new ResultCollection();
[3226]124      executionTime = TimeSpan.Zero;
125    }
126
127    public override IDeepCloneable Clone(Cloner cloner) {
128      BatchRun clone = (BatchRun)base.Clone(cloner);
129      clone.Algorithm = (IAlgorithm)cloner.Clone(algorithm);
130      clone.repetitions = repetitions;
[3246]131      clone.results = (ResultCollection)cloner.Clone(results);
[3226]132      clone.executionTime = executionTime;
133      clone.running = running;
134      clone.canceled = canceled;
135      return clone;
136    }
137
138    public void Prepare() {
139      results.Clear();
140      if (Algorithm != null) Algorithm.Prepare();
141      OnPrepared();
142    }
143    public void Start() {
144      if (Algorithm != null) Algorithm.Start();
145    }
146    public void Stop() {
147      if (Algorithm != null) {
148        canceled = true;
149        Algorithm.Stop();
150      }
151    }
152
153    #region Events
154    public event EventHandler AlgorithmChanged;
155    private void OnAlgorithmChanged() {
156      if (AlgorithmChanged != null)
157        AlgorithmChanged(this, EventArgs.Empty);
158    }
159    public event EventHandler RepetitionsChanged;
160    private void OnRepetitionsChanged() {
161      if (RepetitionsChanged != null)
162        RepetitionsChanged(this, EventArgs.Empty);
163    }
164    public event EventHandler ExecutionTimeChanged;
165    private void OnExecutionTimeChanged() {
166      if (ExecutionTimeChanged != null)
167        ExecutionTimeChanged(this, EventArgs.Empty);
168    }
169    public event EventHandler RunningChanged;
170    private void OnRunningChanged() {
171      if (RunningChanged != null)
172        RunningChanged(this, EventArgs.Empty);
173    }
174    public event EventHandler Prepared;
175    private void OnPrepared() {
176      if (Prepared != null)
177        Prepared(this, EventArgs.Empty);
178    }
179    public event EventHandler Started;
180    private void OnStarted() {
181      if (Started != null)
182        Started(this, EventArgs.Empty);
183    }
184    public event EventHandler Stopped;
185    private void OnStopped() {
186      if (Stopped != null)
187        Stopped(this, EventArgs.Empty);
188      canceled = false;
189      Running = false;
190    }
191    public event EventHandler<HeuristicLab.Common.EventArgs<Exception>> ExceptionOccurred;
192    private void OnExceptionOccurred(Exception exception) {
193      if (ExceptionOccurred != null)
194        ExceptionOccurred(this, new HeuristicLab.Common.EventArgs<Exception>(exception));
195    }
196
197    private void DeregisterAlgorithmEvents() {
198      algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
199      algorithm.RunningChanged -= new EventHandler(Algorithm_RunningChanged);
200      algorithm.ExceptionOccurred -= new EventHandler<HeuristicLab.Common.EventArgs<Exception>>(Algorithm_ExceptionOccurred);
201    }
202
203    private void RegisterAlgorithmEvents() {
204      algorithm.Prepared += new EventHandler(Algorithm_Prepared);
205      algorithm.RunningChanged += new EventHandler(Algorithm_RunningChanged);
206      algorithm.ExceptionOccurred += new EventHandler<HeuristicLab.Common.EventArgs<Exception>>(Algorithm_ExceptionOccurred);
207    }
208
209    private void Algorithm_Prepared(object sender, EventArgs e) {
[3246]210      results.Add(new Result("Run " + DateTime.Now.ToString(), Algorithm.Results));
[3226]211    }
212    private void Algorithm_RunningChanged(object sender, EventArgs e) {
213      if (Algorithm.Running) {
214        Running = true;
215        OnStarted();
216      } else {
217        if (!canceled && (results.Count < repetitions)) {
218          ExecutionTime += Algorithm.ExecutionTime;
219          Algorithm.Prepare();
220          Algorithm.Start();
221        } else {
222          if (Algorithm.Finished) ExecutionTime += Algorithm.ExecutionTime;
223          OnStopped();
224        }
225      }
226    }
227    private void Algorithm_ExceptionOccurred(object sender, HeuristicLab.Common.EventArgs<Exception> e) {
228      OnExceptionOccurred(e.Value);
229    }
230    #endregion
231  }
232}
Note: See TracBrowser for help on using the repository browser.