1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Collections;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization {
|
---|
33 | /// <summary>
|
---|
34 | /// A base class for algorithms.
|
---|
35 | /// </summary>
|
---|
36 | [Item("Algorithm", "A base class for algorithms.")]
|
---|
37 | [StorableClass]
|
---|
38 | public abstract class Algorithm : ParameterizedNamedItem, IAlgorithm {
|
---|
39 | public static new Image StaticItemImage
|
---|
40 | {
|
---|
41 | get { return new Bitmap(25, 25); }
|
---|
42 | }
|
---|
43 | public override Image ItemImage
|
---|
44 | {
|
---|
45 | get
|
---|
46 | {
|
---|
47 | if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
|
---|
48 | else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
|
---|
49 | else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused;
|
---|
50 | else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
|
---|
51 | else return base.ItemImage;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | [Storable]
|
---|
56 | private ExecutionState executionState;
|
---|
57 | public ExecutionState ExecutionState
|
---|
58 | {
|
---|
59 | get { return executionState; }
|
---|
60 | private set
|
---|
61 | {
|
---|
62 | if (executionState != value) {
|
---|
63 | executionState = value;
|
---|
64 | OnExecutionStateChanged();
|
---|
65 | OnItemImageChanged();
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | [Storable]
|
---|
71 | private TimeSpan executionTime;
|
---|
72 | public TimeSpan ExecutionTime
|
---|
73 | {
|
---|
74 | get { return executionTime; }
|
---|
75 | protected set
|
---|
76 | {
|
---|
77 | executionTime = value;
|
---|
78 | OnExecutionTimeChanged();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public virtual Type ProblemType
|
---|
83 | {
|
---|
84 | get { return typeof(IProblem); }
|
---|
85 | }
|
---|
86 |
|
---|
87 | [Storable]
|
---|
88 | private IProblem problem;
|
---|
89 | public IProblem Problem
|
---|
90 | {
|
---|
91 | get { return problem; }
|
---|
92 | set
|
---|
93 | {
|
---|
94 | if (problem != value) {
|
---|
95 | if ((value != null) && !ProblemType.IsInstanceOfType(value)) throw new ArgumentException("Invalid problem type.");
|
---|
96 | if (problem != null) DeregisterProblemEvents();
|
---|
97 | problem = value;
|
---|
98 | if (problem != null) RegisterProblemEvents();
|
---|
99 | OnProblemChanged();
|
---|
100 | Prepare();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public abstract ResultCollection Results { get; }
|
---|
106 |
|
---|
107 | [Storable]
|
---|
108 | private bool storeAlgorithmInEachRun;
|
---|
109 | public bool StoreAlgorithmInEachRun
|
---|
110 | {
|
---|
111 | get { return storeAlgorithmInEachRun; }
|
---|
112 | set
|
---|
113 | {
|
---|
114 | if (storeAlgorithmInEachRun != value) {
|
---|
115 | storeAlgorithmInEachRun = value;
|
---|
116 | OnStoreAlgorithmInEachRunChanged();
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | [Storable]
|
---|
122 | protected int runsCounter;
|
---|
123 |
|
---|
124 | [Storable]
|
---|
125 | private RunCollection runs;
|
---|
126 | public RunCollection Runs
|
---|
127 | {
|
---|
128 | get { return runs; }
|
---|
129 | protected set
|
---|
130 | {
|
---|
131 | if (value == null) throw new ArgumentNullException();
|
---|
132 | if (runs != value) {
|
---|
133 | if (runs != null) DeregisterRunsEvents();
|
---|
134 | runs = value;
|
---|
135 | if (runs != null) RegisterRunsEvents();
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | public virtual IEnumerable<IOptimizer> NestedOptimizers
|
---|
141 | {
|
---|
142 | get { return Enumerable.Empty<IOptimizer>(); }
|
---|
143 | }
|
---|
144 |
|
---|
145 | protected Algorithm()
|
---|
146 | : base() {
|
---|
147 | executionState = ExecutionState.Stopped;
|
---|
148 | executionTime = TimeSpan.Zero;
|
---|
149 | storeAlgorithmInEachRun = false;
|
---|
150 | runsCounter = 0;
|
---|
151 | Runs = new RunCollection { OptimizerName = Name };
|
---|
152 | }
|
---|
153 | protected Algorithm(string name)
|
---|
154 | : base(name) {
|
---|
155 | executionState = ExecutionState.Stopped;
|
---|
156 | executionTime = TimeSpan.Zero;
|
---|
157 | storeAlgorithmInEachRun = false;
|
---|
158 | runsCounter = 0;
|
---|
159 | Runs = new RunCollection { OptimizerName = Name };
|
---|
160 | }
|
---|
161 | protected Algorithm(string name, ParameterCollection parameters)
|
---|
162 | : base(name, parameters) {
|
---|
163 | executionState = ExecutionState.Stopped;
|
---|
164 | executionTime = TimeSpan.Zero;
|
---|
165 | storeAlgorithmInEachRun = false;
|
---|
166 | runsCounter = 0;
|
---|
167 | Runs = new RunCollection { OptimizerName = Name };
|
---|
168 | }
|
---|
169 | protected Algorithm(string name, string description)
|
---|
170 | : base(name, description) {
|
---|
171 | executionState = ExecutionState.Stopped;
|
---|
172 | executionTime = TimeSpan.Zero;
|
---|
173 | storeAlgorithmInEachRun = false;
|
---|
174 | runsCounter = 0;
|
---|
175 | Runs = new RunCollection { OptimizerName = Name };
|
---|
176 | }
|
---|
177 | protected Algorithm(string name, string description, ParameterCollection parameters)
|
---|
178 | : base(name, description, parameters) {
|
---|
179 | executionState = ExecutionState.Stopped;
|
---|
180 | executionTime = TimeSpan.Zero;
|
---|
181 | storeAlgorithmInEachRun = false;
|
---|
182 | runsCounter = 0;
|
---|
183 | Runs = new RunCollection { OptimizerName = Name };
|
---|
184 | }
|
---|
185 | [StorableConstructor]
|
---|
186 | protected Algorithm(bool deserializing) : base(deserializing) { }
|
---|
187 | [StorableHook(HookType.AfterDeserialization)]
|
---|
188 | private void AfterDeserialization() {
|
---|
189 | Initialize();
|
---|
190 | }
|
---|
191 |
|
---|
192 | protected Algorithm(Algorithm original, Cloner cloner)
|
---|
193 | : base(original, cloner) {
|
---|
194 | if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
195 | executionState = original.executionState;
|
---|
196 | executionTime = original.executionTime;
|
---|
197 | problem = cloner.Clone(original.problem);
|
---|
198 | storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
|
---|
199 | runsCounter = original.runsCounter;
|
---|
200 | runs = cloner.Clone(original.runs);
|
---|
201 | Initialize();
|
---|
202 | }
|
---|
203 |
|
---|
204 | private void Initialize() {
|
---|
205 | if (problem != null) RegisterProblemEvents();
|
---|
206 | if (runs != null) RegisterRunsEvents();
|
---|
207 | }
|
---|
208 |
|
---|
209 | public virtual void Prepare() {
|
---|
210 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
211 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
212 | }
|
---|
213 | public void Prepare(bool clearRuns) {
|
---|
214 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
215 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
216 | if (clearRuns) runs.Clear();
|
---|
217 | Prepare();
|
---|
218 | }
|
---|
219 | public virtual void Start() {
|
---|
220 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
221 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
222 | }
|
---|
223 | public virtual void Pause() {
|
---|
224 | if (ExecutionState != ExecutionState.Started)
|
---|
225 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
226 | }
|
---|
227 | public virtual void Stop() {
|
---|
228 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
229 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
230 | }
|
---|
231 |
|
---|
232 | public override void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
233 | base.CollectParameterValues(values);
|
---|
234 | values.Add("Algorithm Name", new StringValue(Name));
|
---|
235 | values.Add("Algorithm Type", new StringValue(this.GetType().GetPrettyName()));
|
---|
236 | if (Problem != null) {
|
---|
237 | Problem.CollectParameterValues(values);
|
---|
238 | values.Add("Problem Name", new StringValue(Problem.Name));
|
---|
239 | values.Add("Problem Type", new StringValue(Problem.GetType().GetPrettyName()));
|
---|
240 | }
|
---|
241 | }
|
---|
242 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
243 | values.Add("Execution Time", new TimeSpanValue(ExecutionTime));
|
---|
244 | Results.CollectResultValues(values);
|
---|
245 | }
|
---|
246 |
|
---|
247 | protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
|
---|
248 | var children = base.GetCollectedValues(param);
|
---|
249 | foreach (var child in children) {
|
---|
250 | if (child.Value is IOperator)
|
---|
251 | yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
|
---|
252 | else yield return child;
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | #region Events
|
---|
257 | protected override void OnNameChanged() {
|
---|
258 | base.OnNameChanged();
|
---|
259 | Runs.OptimizerName = Name;
|
---|
260 | }
|
---|
261 |
|
---|
262 | public event EventHandler ExecutionStateChanged;
|
---|
263 | protected virtual void OnExecutionStateChanged() {
|
---|
264 | EventHandler handler = ExecutionStateChanged;
|
---|
265 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
266 | }
|
---|
267 | public event EventHandler ExecutionTimeChanged;
|
---|
268 | protected virtual void OnExecutionTimeChanged() {
|
---|
269 | EventHandler handler = ExecutionTimeChanged;
|
---|
270 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
271 | }
|
---|
272 | public event EventHandler ProblemChanged;
|
---|
273 | protected virtual void OnProblemChanged() {
|
---|
274 | EventHandler handler = ProblemChanged;
|
---|
275 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
276 | }
|
---|
277 | public event EventHandler StoreAlgorithmInEachRunChanged;
|
---|
278 | protected virtual void OnStoreAlgorithmInEachRunChanged() {
|
---|
279 | EventHandler handler = StoreAlgorithmInEachRunChanged;
|
---|
280 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
281 | }
|
---|
282 | public event EventHandler Prepared;
|
---|
283 | protected virtual void OnPrepared() {
|
---|
284 | ExecutionTime = TimeSpan.Zero;
|
---|
285 | foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<object>() { Runs }).OfType<IStatefulItem>()) {
|
---|
286 | statefulObject.InitializeState();
|
---|
287 | }
|
---|
288 | ExecutionState = ExecutionState.Prepared;
|
---|
289 | EventHandler handler = Prepared;
|
---|
290 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
291 | }
|
---|
292 | public event EventHandler Started;
|
---|
293 | protected virtual void OnStarted() {
|
---|
294 | ExecutionState = ExecutionState.Started;
|
---|
295 | EventHandler handler = Started;
|
---|
296 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
297 | }
|
---|
298 | public event EventHandler Paused;
|
---|
299 | protected virtual void OnPaused() {
|
---|
300 | ExecutionState = ExecutionState.Paused;
|
---|
301 | EventHandler handler = Paused;
|
---|
302 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
303 | }
|
---|
304 | public event EventHandler Stopped;
|
---|
305 | protected virtual void OnStopped() {
|
---|
306 | foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<object>() { Runs }).OfType<IStatefulItem>()) {
|
---|
307 | statefulObject.ClearState();
|
---|
308 | }
|
---|
309 | runsCounter++;
|
---|
310 | runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
|
---|
311 | ExecutionState = ExecutionState.Stopped;
|
---|
312 | EventHandler handler = Stopped;
|
---|
313 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
314 | }
|
---|
315 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
316 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
317 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
318 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
319 | }
|
---|
320 |
|
---|
321 | protected virtual void DeregisterProblemEvents() {
|
---|
322 | problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
|
---|
323 | problem.Reset -= new EventHandler(Problem_Reset);
|
---|
324 | }
|
---|
325 | protected virtual void RegisterProblemEvents() {
|
---|
326 | problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
|
---|
327 | problem.Reset += new EventHandler(Problem_Reset);
|
---|
328 | }
|
---|
329 | protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
|
---|
330 | protected virtual void Problem_Reset(object sender, EventArgs e) {
|
---|
331 | Prepare();
|
---|
332 | }
|
---|
333 |
|
---|
334 | protected virtual void DeregisterRunsEvents() {
|
---|
335 | runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
336 | }
|
---|
337 | protected virtual void RegisterRunsEvents() {
|
---|
338 | runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
339 | }
|
---|
340 | protected virtual void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
341 | runsCounter = runs.Count;
|
---|
342 | }
|
---|
343 | #endregion
|
---|
344 | }
|
---|
345 | }
|
---|