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

Last change on this file since 2932 was 2932, checked in by swagner, 12 years ago

Operator architecture refactoring (#95)

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