Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2852 was 2852, checked in by swagner, 14 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        }
54      }
55    }
56
57    public abstract TimeSpan ExecutionTime { get; }
58
59    private bool running;
60    public bool Running {
61      get { return running; }
62    }
63
64    public abstract bool Finished { get; }
65
66    private bool canceled;
67    protected bool Canceled {
68      get { return canceled; }
69      private set {
70        if (canceled != value) {
71          canceled = value;
72          OnCanceledChanged();
73        }
74      }
75    }
76
77    protected Algorithm() : base() { }
78    protected Algorithm(string name) : base(name) { }
79    protected Algorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
80    protected Algorithm(string name, string description) : base(name, description) { }
81    protected Algorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
82
83    public override IDeepCloneable Clone(Cloner cloner) {
84      Algorithm clone = (Algorithm)base.Clone(cloner);
85      clone.Problem = (IProblem)cloner.Clone(problem);
86      clone.running = running;
87      clone.Canceled = canceled;
88      return clone;
89    }
90
91    public void Prepare() {
92      running = false;
93      Canceled = false;
94      OnPrepared();
95    }
96    public void Start() {
97      running = true;
98      Canceled = false;
99      OnStarted();
100    }
101    public void Stop() {
102      Canceled = true;
103    }
104
105    #region Events
106    public event EventHandler ProblemChanged;
107    protected virtual void OnProblemChanged() {
108      if (ProblemChanged != null)
109        ProblemChanged(this, EventArgs.Empty);
110      OnChanged();
111    }
112    public event EventHandler ExecutionTimeChanged;
113    protected virtual void OnExecutionTimeChanged() {
114      if (ExecutionTimeChanged != null)
115        ExecutionTimeChanged(this, EventArgs.Empty);
116      OnChanged();
117    }
118    public event EventHandler Prepared;
119    protected virtual void OnPrepared() {
120      if (Prepared != null)
121        Prepared(this, EventArgs.Empty);
122      OnChanged();
123    }
124    public event EventHandler Started;
125    protected virtual void OnStarted() {
126      if (Started != null)
127        Started(this, EventArgs.Empty);
128      OnChanged();
129    }
130    public event EventHandler Stopped;
131    protected virtual void OnStopped() {
132      if (Stopped != null)
133        Stopped(this, EventArgs.Empty);
134      OnChanged();
135    }
136    protected virtual void OnCanceledChanged() { }
137    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
138    protected virtual void OnExceptionOccurred(Exception exception) {
139      if (ExceptionOccurred != null)
140        ExceptionOccurred(this, new EventArgs<Exception>(exception));
141    }
142    protected virtual void DeregisterProblemEvents() {
143      problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
144      problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
145      problem.Changed -= new ChangedEventHandler(Problem_Changed);
146    }
147    protected virtual void RegisterProblemEvents() {
148      problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
149      problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
150      problem.Changed += new ChangedEventHandler(Problem_Changed);
151    }
152
153    protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
154    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
155    private void Problem_Changed(object sender, ChangedEventArgs e) {
156      OnChanged(e);
157    }
158    #endregion
159  }
160}
Note: See TracBrowser for help on using the repository browser.