Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/Algorithm.cs @ 5533

Last change on this file since 5533 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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