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 | [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 IObservableKeyedCollection<string, IVariable> Results { get; }
|
---|
69 |
|
---|
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 | }
|
---|
150 |
|
---|
151 | protected virtual void DeregisterProblemEvents() {
|
---|
152 | problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
|
---|
153 | problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
|
---|
154 | problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
|
---|
155 | }
|
---|
156 | protected virtual void RegisterProblemEvents() {
|
---|
157 | problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
|
---|
158 | problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
|
---|
159 | problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
|
---|
160 | }
|
---|
161 |
|
---|
162 | protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
|
---|
163 | protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
|
---|
164 | protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
|
---|
165 | #endregion
|
---|
166 | }
|
---|
167 | }
|
---|