Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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