Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued to implement TSP tour visualization (#924)

File size: 6.0 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
[2882]68    public abstract IObservableKeyedCollection<string, IVariable> Results { get; }
69
[2851]70    public abstract TimeSpan ExecutionTime { get; }
71
72    private bool running;
73    public bool Running {
74      get { return running; }
75    }
76
77    public abstract bool Finished { get; }
78
79    private bool canceled;
80    protected bool Canceled {
81      get { return canceled; }
82      private set {
83        if (canceled != value) {
84          canceled = value;
85          OnCanceledChanged();
86        }
87      }
88    }
89
90    protected Algorithm() : base() { }
91    protected Algorithm(string name) : base(name) { }
92    protected Algorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
93    protected Algorithm(string name, string description) : base(name, description) { }
94    protected Algorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      Algorithm clone = (Algorithm)base.Clone(cloner);
98      clone.Problem = (IProblem)cloner.Clone(problem);
99      clone.running = running;
100      clone.Canceled = canceled;
101      return clone;
102    }
103
104    public void Prepare() {
105      running = false;
106      Canceled = false;
107      OnPrepared();
108    }
109    public void Start() {
110      running = true;
111      Canceled = false;
112      OnStarted();
113    }
114    public void Stop() {
115      Canceled = true;
116    }
117
118    #region Events
119    public event EventHandler ProblemChanged;
120    protected virtual void OnProblemChanged() {
121      if (ProblemChanged != null)
122        ProblemChanged(this, EventArgs.Empty);
123    }
124    public event EventHandler ExecutionTimeChanged;
125    protected virtual void OnExecutionTimeChanged() {
126      if (ExecutionTimeChanged != null)
127        ExecutionTimeChanged(this, EventArgs.Empty);
128    }
129    public event EventHandler Prepared;
130    protected virtual void OnPrepared() {
131      if (Prepared != null)
132        Prepared(this, EventArgs.Empty);
133    }
134    public event EventHandler Started;
135    protected virtual void OnStarted() {
136      if (Started != null)
137        Started(this, EventArgs.Empty);
138    }
139    public event EventHandler Stopped;
140    protected virtual void OnStopped() {
141      if (Stopped != null)
142        Stopped(this, EventArgs.Empty);
143    }
144    protected virtual void OnCanceledChanged() { }
145    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
146    protected virtual void OnExceptionOccurred(Exception exception) {
147      if (ExceptionOccurred != null)
148        ExceptionOccurred(this, new EventArgs<Exception>(exception));
149    }
[2975]150
[2852]151    protected virtual void DeregisterProblemEvents() {
152      problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
153      problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
[3107]154      problem.VisualizerChanged -= new EventHandler(Problem_VisualizerChanged);
[2975]155      problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
[2852]156    }
157    protected virtual void RegisterProblemEvents() {
158      problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
159      problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
[3107]160      problem.VisualizerChanged += new EventHandler(Problem_VisualizerChanged);
[2975]161      problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
[2852]162    }
163
164    protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
165    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
[3107]166    protected virtual void Problem_VisualizerChanged(object sender, EventArgs e) { }
[2975]167    protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
[2851]168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.