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;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Optimization {
|
---|
28 | /// <summary>
|
---|
29 | /// A run in which an algorithm is executed a given number of times.
|
---|
30 | /// </summary>
|
---|
31 | [Item("Batch Run", "A run in which an algorithm is executed a given number of times.")]
|
---|
32 | [Creatable("Testing & Analysis")]
|
---|
33 | [StorableClass]
|
---|
34 | public sealed class BatchRun : NamedItem {
|
---|
35 | public override Image ItemImage {
|
---|
36 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private IAlgorithm algorithm;
|
---|
40 | [Storable]
|
---|
41 | private IAlgorithm AlgorithmPersistence {
|
---|
42 | get { return algorithm; }
|
---|
43 | set {
|
---|
44 | if (algorithm != null) DeregisterAlgorithmEvents();
|
---|
45 | algorithm = value;
|
---|
46 | if (algorithm != null) RegisterAlgorithmEvents();
|
---|
47 | }
|
---|
48 | }
|
---|
49 | public IAlgorithm Algorithm {
|
---|
50 | get { return algorithm; }
|
---|
51 | set {
|
---|
52 | if (algorithm != value) {
|
---|
53 | if (algorithm != null) DeregisterAlgorithmEvents();
|
---|
54 | algorithm = value;
|
---|
55 | if (algorithm != null) RegisterAlgorithmEvents();
|
---|
56 | OnAlgorithmChanged();
|
---|
57 | Prepare();
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [Storable]
|
---|
63 | private int repetitions;
|
---|
64 | public int Repetitions {
|
---|
65 | get { return repetitions; }
|
---|
66 | set {
|
---|
67 | if (repetitions != value) {
|
---|
68 | repetitions = value;
|
---|
69 | OnRepetitionsChanged();
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | [Storable]
|
---|
75 | private RunCollection runs;
|
---|
76 | public RunCollection Runs {
|
---|
77 | get { return runs; }
|
---|
78 | }
|
---|
79 |
|
---|
80 | [Storable]
|
---|
81 | private TimeSpan executionTime;
|
---|
82 | public TimeSpan ExecutionTime {
|
---|
83 | get { return executionTime; }
|
---|
84 | private set {
|
---|
85 | if (executionTime != value) {
|
---|
86 | executionTime = value;
|
---|
87 | OnExecutionTimeChanged();
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | private bool running;
|
---|
93 | public bool Running {
|
---|
94 | get { return running; }
|
---|
95 | private set {
|
---|
96 | if (running != value) {
|
---|
97 | running = value;
|
---|
98 | OnRunningChanged();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public bool Finished {
|
---|
104 | get { return ((Algorithm == null) || (Algorithm.Finished && (runs.Count >= repetitions))); }
|
---|
105 | }
|
---|
106 |
|
---|
107 | private bool canceled;
|
---|
108 |
|
---|
109 | public BatchRun()
|
---|
110 | : base() {
|
---|
111 | repetitions = 10;
|
---|
112 | runs = new RunCollection();
|
---|
113 | executionTime = TimeSpan.Zero;
|
---|
114 | }
|
---|
115 | public BatchRun(string name) : base(name) {
|
---|
116 | repetitions = 10;
|
---|
117 | runs = new RunCollection();
|
---|
118 | executionTime = TimeSpan.Zero;
|
---|
119 | }
|
---|
120 | public BatchRun(string name, string description) : base(name, description) {
|
---|
121 | repetitions = 10;
|
---|
122 | runs = new RunCollection();
|
---|
123 | executionTime = TimeSpan.Zero;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
127 | BatchRun clone = (BatchRun)base.Clone(cloner);
|
---|
128 | clone.Algorithm = (IAlgorithm)cloner.Clone(algorithm);
|
---|
129 | clone.repetitions = repetitions;
|
---|
130 | clone.runs = (RunCollection)cloner.Clone(runs);
|
---|
131 | clone.executionTime = executionTime;
|
---|
132 | clone.running = running;
|
---|
133 | clone.canceled = canceled;
|
---|
134 | return clone;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void Prepare() {
|
---|
138 | executionTime = TimeSpan.Zero;
|
---|
139 | runs.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.RunningChanged -= new EventHandler(Algorithm_RunningChanged);
|
---|
199 | algorithm.ExceptionOccurred -= new EventHandler<HeuristicLab.Common.EventArgs<Exception>>(Algorithm_ExceptionOccurred);
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void RegisterAlgorithmEvents() {
|
---|
203 | algorithm.RunningChanged += new EventHandler(Algorithm_RunningChanged);
|
---|
204 | algorithm.ExceptionOccurred += new EventHandler<HeuristicLab.Common.EventArgs<Exception>>(Algorithm_ExceptionOccurred);
|
---|
205 | }
|
---|
206 |
|
---|
207 | private void Algorithm_RunningChanged(object sender, EventArgs e) {
|
---|
208 | if (Algorithm.Running) {
|
---|
209 | Running = true;
|
---|
210 | OnStarted();
|
---|
211 | } else {
|
---|
212 | if (!canceled) {
|
---|
213 | ExecutionTime += Algorithm.ExecutionTime;
|
---|
214 | runs.Add(new Run("Run " + Algorithm.ExecutionTime.ToString(), Algorithm));
|
---|
215 | Algorithm.Prepare();
|
---|
216 | if (runs.Count < repetitions) Algorithm.Start();
|
---|
217 | else OnStopped();
|
---|
218 | } else {
|
---|
219 | OnStopped();
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | private void Algorithm_ExceptionOccurred(object sender, HeuristicLab.Common.EventArgs<Exception> e) {
|
---|
224 | OnExceptionOccurred(e.Value);
|
---|
225 | }
|
---|
226 | #endregion
|
---|
227 | }
|
---|
228 | }
|
---|