Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented first version of algorithm batch processing (#947).

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