Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on algorithm batch processing (#947).

File size: 6.6 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.Collections.Generic;
24using System.Drawing;
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  [StorableClass]
35  public abstract class Algorithm : ParameterizedNamedItem, IAlgorithm {
36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
38    }
39
40    public virtual Type ProblemType {
41      get { return typeof(IProblem); }
42    }
43
44    private IProblem problem;
45    [Storable]
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    }
54    public IProblem Problem {
55      get { return problem; }
56      set {
57        if (problem != value) {
58          if ((value != null) && !ProblemType.IsInstanceOfType(value)) throw new ArgumentException("Invalid problem type.");
59          if (problem != null) DeregisterProblemEvents();
60          problem = value;
61          if (problem != null) RegisterProblemEvents();
62          OnProblemChanged();
63          Prepare();
64        }
65      }
66    }
67
68    public abstract ResultCollection Results { get; }
69
70    public abstract TimeSpan ExecutionTime { get; }
71
72    private bool running;
73    public bool Running {
74      get { return running; }
75      protected set {
76        if (running != value) {
77          running = value;
78          OnRunningChanged();
79        }
80      }
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;
106      clone.canceled = canceled;
107      return clone;
108    }
109
110    public void Prepare() {
111      OnPrepared();
112    }
113    public void Start() {
114      Running = true;
115      Canceled = false;
116      OnStarted();
117    }
118    public void Stop() {
119      Canceled = true;
120    }
121
122    public override void CollectParameterValues(IDictionary<string, IItem> values) {
123      base.CollectParameterValues(values);
124      Problem.CollectParameterValues(values);
125    }
126    public virtual void CollectResultValues(IDictionary<string, IItem> values) {
127      foreach (IResult result in Results)
128        values.Add(result.Name, result.Value != null ? (IItem)result.Value.Clone() : null);
129    }
130
131    #region Events
132    public event EventHandler ProblemChanged;
133    protected virtual void OnProblemChanged() {
134      if (ProblemChanged != null)
135        ProblemChanged(this, EventArgs.Empty);
136    }
137    public event EventHandler ExecutionTimeChanged;
138    protected virtual void OnExecutionTimeChanged() {
139      if (ExecutionTimeChanged != null)
140        ExecutionTimeChanged(this, EventArgs.Empty);
141    }
142    public event EventHandler RunningChanged;
143    protected virtual void OnRunningChanged() {
144      if (RunningChanged != null)
145        RunningChanged(this, EventArgs.Empty);
146    }
147    public event EventHandler Prepared;
148    protected virtual void OnPrepared() {
149      if (Prepared != null)
150        Prepared(this, EventArgs.Empty);
151    }
152    public event EventHandler Started;
153    protected virtual void OnStarted() {
154      if (Started != null)
155        Started(this, EventArgs.Empty);
156    }
157    public event EventHandler Stopped;
158    protected virtual void OnStopped() {
159      if (Stopped != null)
160        Stopped(this, EventArgs.Empty);
161      Canceled = false;
162      Running = false;
163    }
164    protected virtual void OnCanceledChanged() { }
165    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
166    protected virtual void OnExceptionOccurred(Exception exception) {
167      if (ExceptionOccurred != null)
168        ExceptionOccurred(this, new EventArgs<Exception>(exception));
169    }
170
171    protected virtual void DeregisterProblemEvents() {
172      problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
173      problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
174      problem.VisualizerChanged -= new EventHandler(Problem_VisualizerChanged);
175      problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
176    }
177    protected virtual void RegisterProblemEvents() {
178      problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
179      problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
180      problem.VisualizerChanged += new EventHandler(Problem_VisualizerChanged);
181      problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
182    }
183
184    protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
185    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
186    protected virtual void Problem_VisualizerChanged(object sender, EventArgs e) { }
187    protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
188    #endregion
189  }
190}
Note: See TracBrowser for help on using the repository browser.