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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace 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 | OnChanged();
|
---|
115 | }
|
---|
116 | public event EventHandler ExecutionTimeChanged;
|
---|
117 | protected virtual void OnExecutionTimeChanged() {
|
---|
118 | if (ExecutionTimeChanged != null)
|
---|
119 | ExecutionTimeChanged(this, EventArgs.Empty);
|
---|
120 | OnChanged();
|
---|
121 | }
|
---|
122 | public event EventHandler Prepared;
|
---|
123 | protected virtual void OnPrepared() {
|
---|
124 | if (Prepared != null)
|
---|
125 | Prepared(this, EventArgs.Empty);
|
---|
126 | OnChanged();
|
---|
127 | }
|
---|
128 | public event EventHandler Started;
|
---|
129 | protected virtual void OnStarted() {
|
---|
130 | if (Started != null)
|
---|
131 | Started(this, EventArgs.Empty);
|
---|
132 | OnChanged();
|
---|
133 | }
|
---|
134 | public event EventHandler Stopped;
|
---|
135 | protected virtual void OnStopped() {
|
---|
136 | if (Stopped != null)
|
---|
137 | Stopped(this, EventArgs.Empty);
|
---|
138 | OnChanged();
|
---|
139 | }
|
---|
140 | protected virtual void OnCanceledChanged() { }
|
---|
141 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
142 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
143 | if (ExceptionOccurred != null)
|
---|
144 | ExceptionOccurred(this, new EventArgs<Exception>(exception));
|
---|
145 | }
|
---|
146 | protected virtual void DeregisterProblemEvents() {
|
---|
147 | problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
|
---|
148 | problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
|
---|
149 | problem.Changed -= new ChangedEventHandler(Problem_Changed);
|
---|
150 | }
|
---|
151 | protected virtual void RegisterProblemEvents() {
|
---|
152 | problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
|
---|
153 | problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
|
---|
154 | problem.Changed += new ChangedEventHandler(Problem_Changed);
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
|
---|
158 | protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
|
---|
159 | private void Problem_Changed(object sender, ChangedEventArgs e) {
|
---|
160 | OnChanged(e);
|
---|
161 | }
|
---|
162 | #endregion
|
---|
163 | }
|
---|
164 | }
|
---|