Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on algorithm batch processing (#947).

File size: 8.6 KB
Line 
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.Collections.Generic;
24using System.Drawing;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace 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    protected Algorithm()
93      : base() {
94      executionState = ExecutionState.Stopped;
95      executionTime = TimeSpan.Zero;
96    }
97    protected Algorithm(string name)
98      : base(name) {
99      executionState = ExecutionState.Stopped;
100      executionTime = TimeSpan.Zero;
101    }
102    protected Algorithm(string name, ParameterCollection parameters)
103      : base(name, parameters) {
104      executionState = ExecutionState.Stopped;
105      executionTime = TimeSpan.Zero;
106    }
107    protected Algorithm(string name, string description)
108      : base(name, description) {
109      executionState = ExecutionState.Stopped;
110      executionTime = TimeSpan.Zero;
111    }
112    protected Algorithm(string name, string description, ParameterCollection parameters)
113      : base(name, description, parameters) {
114      executionState = ExecutionState.Stopped;
115      executionTime = TimeSpan.Zero;
116    }
117
118    public override IDeepCloneable Clone(Cloner cloner) {
119      Algorithm clone = (Algorithm)base.Clone(cloner);
120      clone.executionState = executionState;
121      clone.executionTime = executionTime;
122      clone.Problem = (IProblem)cloner.Clone(problem);
123      return clone;
124    }
125
126    public void Prepare() {
127      Prepare(true);
128    }
129    public virtual void Prepare(bool clearResults) {
130      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
131        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
132      ExecutionTime = TimeSpan.Zero;
133    }
134    public virtual void Start() {
135      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
136        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
137    }
138    public virtual void Pause() {
139      if (ExecutionState != ExecutionState.Started)
140        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
141    }
142    public virtual void Stop() {
143      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
144        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
145    }
146
147    public override void CollectParameterValues(IDictionary<string, IItem> values) {
148      base.CollectParameterValues(values);
149      Problem.CollectParameterValues(values);
150    }
151    public virtual void CollectResultValues(IDictionary<string, IItem> values) {
152      foreach (IResult result in Results)
153        values.Add(result.Name, result.Value != null ? (IItem)result.Value.Clone() : null);
154    }
155
156    #region Events
157    public event EventHandler ExecutionStateChanged;
158    protected virtual void OnExecutionStateChanged() {
159      EventHandler handler = ExecutionStateChanged;
160      if (handler != null) handler(this, EventArgs.Empty);
161    }
162    public event EventHandler ExecutionTimeChanged;
163    protected virtual void OnExecutionTimeChanged() {
164      EventHandler handler = ExecutionTimeChanged;
165      if (handler != null) handler(this, EventArgs.Empty);
166    }
167    public event EventHandler ProblemChanged;
168    protected virtual void OnProblemChanged() {
169      EventHandler handler = ProblemChanged;
170      if (handler != null) handler(this, EventArgs.Empty);
171    }
172    public event EventHandler Prepared;
173    protected virtual void OnPrepared() {
174      ExecutionState = ExecutionState.Prepared;
175      EventHandler handler = Prepared;
176      if (handler != null) handler(this, EventArgs.Empty);
177    }
178    public event EventHandler Started;
179    protected virtual void OnStarted() {
180      ExecutionState = ExecutionState.Started;
181      EventHandler handler = Started;
182      if (handler != null) handler(this, EventArgs.Empty);
183    }
184    public event EventHandler Paused;
185    protected virtual void OnPaused() {
186      ExecutionState = ExecutionState.Paused;
187      EventHandler handler = Paused;
188      if (handler != null) handler(this, EventArgs.Empty);
189    }
190    public event EventHandler Stopped;
191    protected virtual void OnStopped() {
192      ExecutionState = ExecutionState.Stopped;
193      EventHandler handler = Stopped;
194      if (handler != null) handler(this, EventArgs.Empty);
195    }
196    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
197    protected virtual void OnExceptionOccurred(Exception exception) {
198      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
199      if (handler != null) handler(this, new EventArgs<Exception>(exception));
200    }
201
202    protected virtual void DeregisterProblemEvents() {
203      problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
204      problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
205      problem.VisualizerChanged -= new EventHandler(Problem_VisualizerChanged);
206      problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
207    }
208    protected virtual void RegisterProblemEvents() {
209      problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
210      problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
211      problem.VisualizerChanged += new EventHandler(Problem_VisualizerChanged);
212      problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
213    }
214
215    protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
216    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
217    protected virtual void Problem_VisualizerChanged(object sender, EventArgs e) { }
218    protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
219    #endregion
220  }
221}
Note: See TracBrowser for help on using the repository browser.