Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2864 was 2864, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • worked on algorithms
File size: 5.4 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.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization {
29  /// <summary>
30  /// A base class for algorithms.
31  /// </summary>
32  [Item("Algorithm", "A base class for algorithms.")]
33  public abstract class Algorithm : ParameterizedNamedItem, IAlgorithm {
34    public override Image ItemImage {
35      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
36    }
37
38    public virtual Type ProblemType {
39      get { return typeof(IProblem); }
40    }
41
42    private IProblem problem;
43    [Storable]
44    public IProblem Problem {
45      get { return problem; }
46      set {
47        if (problem != value) {
48          if ((value != null) && !ProblemType.IsInstanceOfType(value)) throw new ArgumentException("Invalid problem type.");
49          if (problem != null) DeregisterProblemEvents();
50          problem = value;
51          if (problem != null) RegisterProblemEvents();
52          OnProblemChanged();
53          Prepare();
54        }
55      }
56    }
57
58    public abstract TimeSpan ExecutionTime { get; }
59
60    private bool running;
61    public bool Running {
62      get { return running; }
63    }
64
65    public abstract bool Finished { get; }
66
67    private bool canceled;
68    protected bool Canceled {
69      get { return canceled; }
70      private set {
71        if (canceled != value) {
72          canceled = value;
73          OnCanceledChanged();
74        }
75      }
76    }
77
78    protected Algorithm() : base() { }
79    protected Algorithm(string name) : base(name) { }
80    protected Algorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
81    protected Algorithm(string name, string description) : base(name, description) { }
82    protected Algorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      Algorithm clone = (Algorithm)base.Clone(cloner);
86      clone.Problem = (IProblem)cloner.Clone(problem);
87      clone.running = running;
88      clone.Canceled = canceled;
89      return clone;
90    }
91
92    public void Prepare() {
93      running = false;
94      Canceled = false;
95      OnPrepared();
96    }
97    public void Start() {
98      running = true;
99      Canceled = false;
100      OnStarted();
101    }
102    public void Stop() {
103      Canceled = true;
104    }
105
106    #region Events
107    public event EventHandler ProblemChanged;
108    protected virtual void OnProblemChanged() {
109      if (ProblemChanged != null)
110        ProblemChanged(this, EventArgs.Empty);
111      OnChanged();
112    }
113    public event EventHandler ExecutionTimeChanged;
114    protected virtual void OnExecutionTimeChanged() {
115      if (ExecutionTimeChanged != null)
116        ExecutionTimeChanged(this, EventArgs.Empty);
117      OnChanged();
118    }
119    public event EventHandler Prepared;
120    protected virtual void OnPrepared() {
121      if (Prepared != null)
122        Prepared(this, EventArgs.Empty);
123      OnChanged();
124    }
125    public event EventHandler Started;
126    protected virtual void OnStarted() {
127      if (Started != null)
128        Started(this, EventArgs.Empty);
129      OnChanged();
130    }
131    public event EventHandler Stopped;
132    protected virtual void OnStopped() {
133      if (Stopped != null)
134        Stopped(this, EventArgs.Empty);
135      OnChanged();
136    }
137    protected virtual void OnCanceledChanged() { }
138    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
139    protected virtual void OnExceptionOccurred(Exception exception) {
140      if (ExceptionOccurred != null)
141        ExceptionOccurred(this, new EventArgs<Exception>(exception));
142    }
143    protected virtual void DeregisterProblemEvents() {
144      problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
145      problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
146      problem.Changed -= new ChangedEventHandler(Problem_Changed);
147    }
148    protected virtual void RegisterProblemEvents() {
149      problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
150      problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
151      problem.Changed += new ChangedEventHandler(Problem_Changed);
152    }
153
154    protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
155    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
156    private void Problem_Changed(object sender, ChangedEventArgs e) {
157      OnChanged(e);
158    }
159    #endregion
160  }
161}
Note: See TracBrowser for help on using the repository browser.