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.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Optimization {
|
---|
30 | /// <summary>
|
---|
31 | /// A base class for algorithms.
|
---|
32 | /// </summary>
|
---|
33 | [Item("Algorithm", "A base class for algorithms.")]
|
---|
34 | [StorableClass]
|
---|
35 | public abstract class Algorithm : ParameterizedNamedItem, IAlgorithm {
|
---|
36 | public override Image ItemImage {
|
---|
37 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private ExecutionState executionState;
|
---|
42 | public ExecutionState ExecutionState {
|
---|
43 | get { return executionState; }
|
---|
44 | private set {
|
---|
45 | if (executionState != value) {
|
---|
46 | executionState = value;
|
---|
47 | OnExecutionStateChanged();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [Storable]
|
---|
53 | private TimeSpan executionTime;
|
---|
54 | public TimeSpan ExecutionTime {
|
---|
55 | get { return executionTime; }
|
---|
56 | protected set {
|
---|
57 | executionTime = value;
|
---|
58 | OnExecutionTimeChanged();
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public virtual Type ProblemType {
|
---|
63 | get { return typeof(IProblem); }
|
---|
64 | }
|
---|
65 |
|
---|
66 | private IProblem problem;
|
---|
67 | [Storable]
|
---|
68 | private IProblem ProblemPersistence {
|
---|
69 | get { return problem; }
|
---|
70 | set {
|
---|
71 | if (problem != null) DeregisterProblemEvents();
|
---|
72 | problem = value;
|
---|
73 | if (problem != null) RegisterProblemEvents();
|
---|
74 | }
|
---|
75 | }
|
---|
76 | public IProblem Problem {
|
---|
77 | get { return problem; }
|
---|
78 | set {
|
---|
79 | if (problem != value) {
|
---|
80 | if ((value != null) && !ProblemType.IsInstanceOfType(value)) throw new ArgumentException("Invalid problem type.");
|
---|
81 | if (problem != null) DeregisterProblemEvents();
|
---|
82 | problem = value;
|
---|
83 | if (problem != null) RegisterProblemEvents();
|
---|
84 | OnProblemChanged();
|
---|
85 | Prepare();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public abstract ResultCollection Results { get; }
|
---|
91 |
|
---|
92 | [Storable]
|
---|
93 | private RunCollection runs;
|
---|
94 | public RunCollection Runs {
|
---|
95 | get { return runs; }
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected Algorithm()
|
---|
99 | : base() {
|
---|
100 | executionState = ExecutionState.Stopped;
|
---|
101 | executionTime = TimeSpan.Zero;
|
---|
102 | runs = new RunCollection();
|
---|
103 | }
|
---|
104 | protected Algorithm(string name)
|
---|
105 | : base(name) {
|
---|
106 | executionState = ExecutionState.Stopped;
|
---|
107 | executionTime = TimeSpan.Zero;
|
---|
108 | runs = new RunCollection();
|
---|
109 | }
|
---|
110 | protected Algorithm(string name, ParameterCollection parameters)
|
---|
111 | : base(name, parameters) {
|
---|
112 | executionState = ExecutionState.Stopped;
|
---|
113 | executionTime = TimeSpan.Zero;
|
---|
114 | runs = new RunCollection();
|
---|
115 | }
|
---|
116 | protected Algorithm(string name, string description)
|
---|
117 | : base(name, description) {
|
---|
118 | executionState = ExecutionState.Stopped;
|
---|
119 | executionTime = TimeSpan.Zero;
|
---|
120 | runs = new RunCollection();
|
---|
121 | }
|
---|
122 | protected Algorithm(string name, string description, ParameterCollection parameters)
|
---|
123 | : base(name, description, parameters) {
|
---|
124 | executionState = ExecutionState.Stopped;
|
---|
125 | executionTime = TimeSpan.Zero;
|
---|
126 | runs = new RunCollection();
|
---|
127 | }
|
---|
128 |
|
---|
129 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
130 | Algorithm clone = (Algorithm)base.Clone(cloner);
|
---|
131 | clone.executionState = executionState;
|
---|
132 | clone.executionTime = executionTime;
|
---|
133 | clone.Problem = (IProblem)cloner.Clone(problem);
|
---|
134 | clone.runs = (RunCollection)cloner.Clone(runs);
|
---|
135 | return clone;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public virtual void Prepare() {
|
---|
139 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
140 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
141 | }
|
---|
142 | public void Prepare(bool clearRuns) {
|
---|
143 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
144 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
145 | if (clearRuns) runs.Clear();
|
---|
146 | Prepare();
|
---|
147 | }
|
---|
148 | public virtual void Start() {
|
---|
149 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
150 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
151 | }
|
---|
152 | public virtual void Pause() {
|
---|
153 | if (ExecutionState != ExecutionState.Started)
|
---|
154 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
155 | }
|
---|
156 | public virtual void Stop() {
|
---|
157 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
158 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
159 | }
|
---|
160 |
|
---|
161 | public override void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
162 | base.CollectParameterValues(values);
|
---|
163 | Problem.CollectParameterValues(values);
|
---|
164 | }
|
---|
165 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
166 | foreach (IResult result in Results)
|
---|
167 | values.Add(result.Name, result.Value != null ? (IItem)result.Value.Clone() : null);
|
---|
168 | }
|
---|
169 |
|
---|
170 | #region Events
|
---|
171 | public event EventHandler ExecutionStateChanged;
|
---|
172 | protected virtual void OnExecutionStateChanged() {
|
---|
173 | EventHandler handler = ExecutionStateChanged;
|
---|
174 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
175 | }
|
---|
176 | public event EventHandler ExecutionTimeChanged;
|
---|
177 | protected virtual void OnExecutionTimeChanged() {
|
---|
178 | EventHandler handler = ExecutionTimeChanged;
|
---|
179 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
180 | }
|
---|
181 | public event EventHandler ProblemChanged;
|
---|
182 | protected virtual void OnProblemChanged() {
|
---|
183 | EventHandler handler = ProblemChanged;
|
---|
184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
185 | }
|
---|
186 | public event EventHandler Prepared;
|
---|
187 | protected virtual void OnPrepared() {
|
---|
188 | ExecutionTime = TimeSpan.Zero;
|
---|
189 | ExecutionState = ExecutionState.Prepared;
|
---|
190 | EventHandler handler = Prepared;
|
---|
191 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
192 | }
|
---|
193 | public event EventHandler Started;
|
---|
194 | protected virtual void OnStarted() {
|
---|
195 | ExecutionState = ExecutionState.Started;
|
---|
196 | EventHandler handler = Started;
|
---|
197 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
198 | }
|
---|
199 | public event EventHandler Paused;
|
---|
200 | protected virtual void OnPaused() {
|
---|
201 | ExecutionState = ExecutionState.Paused;
|
---|
202 | EventHandler handler = Paused;
|
---|
203 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
204 | }
|
---|
205 | public event EventHandler Stopped;
|
---|
206 | protected virtual void OnStopped() {
|
---|
207 | Run run = new Run("Run (" + ExecutionTime.ToString() + ")");
|
---|
208 | CollectParameterValues(run.Parameters);
|
---|
209 | CollectResultValues(run.Results);
|
---|
210 | runs.Add(run);
|
---|
211 | ExecutionState = ExecutionState.Stopped;
|
---|
212 | EventHandler handler = Stopped;
|
---|
213 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
214 | }
|
---|
215 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
216 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
217 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
218 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
219 | }
|
---|
220 |
|
---|
221 | protected virtual void DeregisterProblemEvents() {
|
---|
222 | problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
|
---|
223 | problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
|
---|
224 | problem.VisualizerChanged -= new EventHandler(Problem_VisualizerChanged);
|
---|
225 | problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
|
---|
226 | }
|
---|
227 | protected virtual void RegisterProblemEvents() {
|
---|
228 | problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
|
---|
229 | problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
|
---|
230 | problem.VisualizerChanged += new EventHandler(Problem_VisualizerChanged);
|
---|
231 | problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
|
---|
232 | }
|
---|
233 |
|
---|
234 | protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
|
---|
235 | protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
|
---|
236 | protected virtual void Problem_VisualizerChanged(object sender, EventArgs e) { }
|
---|
237 | protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
|
---|
238 | #endregion
|
---|
239 | }
|
---|
240 | }
|
---|