[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 |
|
---|
| 22 | using System;
|
---|
[3260] | 23 | using System.Collections.Generic;
|
---|
[2851] | 24 | using System.Drawing;
|
---|
[3716] | 25 | using HeuristicLab.Collections;
|
---|
[2851] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[3694] | 28 | using HeuristicLab.Data;
|
---|
[2851] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Optimization {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// A base class for algorithms.
|
---|
| 34 | /// </summary>
|
---|
| 35 | [Item("Algorithm", "A base class for algorithms.")]
|
---|
[3017] | 36 | [StorableClass]
|
---|
[4437] | 37 | public abstract class Algorithm : ParameterizedNamedItem, IAlgorithm {
|
---|
[2851] | 38 | public override Image ItemImage {
|
---|
[3351] | 39 | get {
|
---|
[5287] | 40 | if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
|
---|
| 41 | else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
|
---|
| 42 | else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused;
|
---|
| 43 | else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
|
---|
| 44 | else return HeuristicLab.Common.Resources.VSImageLibrary.Event;
|
---|
[3351] | 45 | }
|
---|
[2851] | 46 | }
|
---|
| 47 |
|
---|
[3262] | 48 | [Storable]
|
---|
| 49 | private ExecutionState executionState;
|
---|
| 50 | public ExecutionState ExecutionState {
|
---|
| 51 | get { return executionState; }
|
---|
| 52 | private set {
|
---|
| 53 | if (executionState != value) {
|
---|
| 54 | executionState = value;
|
---|
| 55 | OnExecutionStateChanged();
|
---|
[3351] | 56 | OnItemImageChanged();
|
---|
[3262] | 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | [Storable]
|
---|
| 62 | private TimeSpan executionTime;
|
---|
| 63 | public TimeSpan ExecutionTime {
|
---|
| 64 | get { return executionTime; }
|
---|
| 65 | protected set {
|
---|
| 66 | executionTime = value;
|
---|
| 67 | OnExecutionTimeChanged();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[2852] | 71 | public virtual Type ProblemType {
|
---|
| 72 | get { return typeof(IProblem); }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[3280] | 75 | [Storable]
|
---|
[2851] | 76 | private IProblem problem;
|
---|
| 77 | public IProblem Problem {
|
---|
| 78 | get { return problem; }
|
---|
| 79 | set {
|
---|
| 80 | if (problem != value) {
|
---|
[2852] | 81 | if ((value != null) && !ProblemType.IsInstanceOfType(value)) throw new ArgumentException("Invalid problem type.");
|
---|
| 82 | if (problem != null) DeregisterProblemEvents();
|
---|
[2851] | 83 | problem = value;
|
---|
[2852] | 84 | if (problem != null) RegisterProblemEvents();
|
---|
[2851] | 85 | OnProblemChanged();
|
---|
[2864] | 86 | Prepare();
|
---|
[2851] | 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[3226] | 91 | public abstract ResultCollection Results { get; }
|
---|
[2882] | 92 |
|
---|
[3275] | 93 | [Storable]
|
---|
[4102] | 94 | private bool storeAlgorithmInEachRun;
|
---|
| 95 | public bool StoreAlgorithmInEachRun {
|
---|
| 96 | get { return storeAlgorithmInEachRun; }
|
---|
| 97 | set {
|
---|
| 98 | if (storeAlgorithmInEachRun != value) {
|
---|
| 99 | storeAlgorithmInEachRun = value;
|
---|
| 100 | OnStoreAlgorithmInEachRunChanged();
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | [Storable]
|
---|
[3280] | 106 | protected int runsCounter;
|
---|
| 107 |
|
---|
| 108 | [Storable]
|
---|
[3275] | 109 | private RunCollection runs;
|
---|
| 110 | public RunCollection Runs {
|
---|
| 111 | get { return runs; }
|
---|
[3716] | 112 | protected set {
|
---|
| 113 | if (value == null) throw new ArgumentNullException();
|
---|
| 114 | if (runs != value) {
|
---|
| 115 | if (runs != null) DeregisterRunsEvents();
|
---|
| 116 | runs = value;
|
---|
| 117 | if (runs != null) RegisterRunsEvents();
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
[3275] | 120 | }
|
---|
| 121 |
|
---|
[3262] | 122 | protected Algorithm()
|
---|
| 123 | : base() {
|
---|
| 124 | executionState = ExecutionState.Stopped;
|
---|
| 125 | executionTime = TimeSpan.Zero;
|
---|
[5203] | 126 | storeAlgorithmInEachRun = false;
|
---|
[3280] | 127 | runsCounter = 0;
|
---|
[3716] | 128 | Runs = new RunCollection();
|
---|
[2851] | 129 | }
|
---|
[3262] | 130 | protected Algorithm(string name)
|
---|
| 131 | : base(name) {
|
---|
| 132 | executionState = ExecutionState.Stopped;
|
---|
| 133 | executionTime = TimeSpan.Zero;
|
---|
[5203] | 134 | storeAlgorithmInEachRun = false;
|
---|
[3280] | 135 | runsCounter = 0;
|
---|
[3716] | 136 | Runs = new RunCollection();
|
---|
[2851] | 137 | }
|
---|
[3262] | 138 | protected Algorithm(string name, ParameterCollection parameters)
|
---|
| 139 | : base(name, parameters) {
|
---|
| 140 | executionState = ExecutionState.Stopped;
|
---|
| 141 | executionTime = TimeSpan.Zero;
|
---|
[5203] | 142 | storeAlgorithmInEachRun = false;
|
---|
[3280] | 143 | runsCounter = 0;
|
---|
[3716] | 144 | Runs = new RunCollection();
|
---|
[3262] | 145 | }
|
---|
| 146 | protected Algorithm(string name, string description)
|
---|
| 147 | : base(name, description) {
|
---|
| 148 | executionState = ExecutionState.Stopped;
|
---|
| 149 | executionTime = TimeSpan.Zero;
|
---|
[5203] | 150 | storeAlgorithmInEachRun = false;
|
---|
[3280] | 151 | runsCounter = 0;
|
---|
[3716] | 152 | Runs = new RunCollection();
|
---|
[3262] | 153 | }
|
---|
| 154 | protected Algorithm(string name, string description, ParameterCollection parameters)
|
---|
| 155 | : base(name, description, parameters) {
|
---|
| 156 | executionState = ExecutionState.Stopped;
|
---|
| 157 | executionTime = TimeSpan.Zero;
|
---|
[5203] | 158 | storeAlgorithmInEachRun = false;
|
---|
[3280] | 159 | runsCounter = 0;
|
---|
[3716] | 160 | Runs = new RunCollection();
|
---|
[3262] | 161 | }
|
---|
[3280] | 162 | [StorableConstructor]
|
---|
[5203] | 163 | protected Algorithm(bool deserializing) : base(deserializing) { }
|
---|
[3280] | 164 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 165 | private void AfterDeserialization() {
|
---|
| 166 | Initialize();
|
---|
[3280] | 167 | }
|
---|
| 168 |
|
---|
[4722] | 169 | protected Algorithm(Algorithm original, Cloner cloner)
|
---|
| 170 | : base(original, cloner) {
|
---|
[3280] | 171 | if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[4722] | 172 | executionState = original.executionState;
|
---|
| 173 | executionTime = original.executionTime;
|
---|
| 174 | problem = cloner.Clone(original.problem);
|
---|
| 175 | storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
|
---|
| 176 | runsCounter = original.runsCounter;
|
---|
| 177 | runs = cloner.Clone(original.runs);
|
---|
| 178 | Initialize();
|
---|
[2851] | 179 | }
|
---|
[4722] | 180 |
|
---|
| 181 | private void Initialize() {
|
---|
| 182 | if (problem != null) RegisterProblemEvents();
|
---|
| 183 | if (runs != null) RegisterRunsEvents();
|
---|
[3286] | 184 | }
|
---|
[2851] | 185 |
|
---|
[3275] | 186 | public virtual void Prepare() {
|
---|
| 187 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
| 188 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[3274] | 189 | }
|
---|
[3275] | 190 | public void Prepare(bool clearRuns) {
|
---|
[3262] | 191 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
| 192 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[3716] | 193 | if (clearRuns) runs.Clear();
|
---|
[3275] | 194 | Prepare();
|
---|
[2851] | 195 | }
|
---|
[3262] | 196 | public virtual void Start() {
|
---|
| 197 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
| 198 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[2851] | 199 | }
|
---|
[3262] | 200 | public virtual void Pause() {
|
---|
| 201 | if (ExecutionState != ExecutionState.Started)
|
---|
| 202 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[2851] | 203 | }
|
---|
[3262] | 204 | public virtual void Stop() {
|
---|
| 205 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
| 206 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 207 | }
|
---|
[2851] | 208 |
|
---|
[3260] | 209 | public override void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
| 210 | base.CollectParameterValues(values);
|
---|
[3785] | 211 | values.Add("Algorithm Name", new StringValue(Name));
|
---|
| 212 | values.Add("Algorithm Type", new StringValue(this.GetType().GetPrettyName()));
|
---|
| 213 | if (Problem != null) {
|
---|
| 214 | Problem.CollectParameterValues(values);
|
---|
| 215 | values.Add("Problem Name", new StringValue(Problem.Name));
|
---|
| 216 | values.Add("Problem Type", new StringValue(Problem.GetType().GetPrettyName()));
|
---|
| 217 | }
|
---|
[3260] | 218 | }
|
---|
| 219 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
[3694] | 220 | values.Add("Execution Time", new TimeSpanValue(ExecutionTime));
|
---|
[3260] | 221 | foreach (IResult result in Results)
|
---|
[3280] | 222 | values.Add(result.Name, result.Value);
|
---|
[3260] | 223 | }
|
---|
| 224 |
|
---|
[2851] | 225 | #region Events
|
---|
[3262] | 226 | public event EventHandler ExecutionStateChanged;
|
---|
| 227 | protected virtual void OnExecutionStateChanged() {
|
---|
| 228 | EventHandler handler = ExecutionStateChanged;
|
---|
| 229 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 230 | }
|
---|
| 231 | public event EventHandler ExecutionTimeChanged;
|
---|
| 232 | protected virtual void OnExecutionTimeChanged() {
|
---|
[3262] | 233 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 234 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 235 | }
|
---|
[3262] | 236 | public event EventHandler ProblemChanged;
|
---|
| 237 | protected virtual void OnProblemChanged() {
|
---|
| 238 | EventHandler handler = ProblemChanged;
|
---|
| 239 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 240 | }
|
---|
[4102] | 241 | public event EventHandler StoreAlgorithmInEachRunChanged;
|
---|
| 242 | protected virtual void OnStoreAlgorithmInEachRunChanged() {
|
---|
| 243 | EventHandler handler = StoreAlgorithmInEachRunChanged;
|
---|
| 244 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 245 | }
|
---|
[2851] | 246 | public event EventHandler Prepared;
|
---|
| 247 | protected virtual void OnPrepared() {
|
---|
[3275] | 248 | ExecutionTime = TimeSpan.Zero;
|
---|
[3262] | 249 | ExecutionState = ExecutionState.Prepared;
|
---|
| 250 | EventHandler handler = Prepared;
|
---|
| 251 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 252 | }
|
---|
| 253 | public event EventHandler Started;
|
---|
| 254 | protected virtual void OnStarted() {
|
---|
[3262] | 255 | ExecutionState = ExecutionState.Started;
|
---|
| 256 | EventHandler handler = Started;
|
---|
| 257 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 258 | }
|
---|
[3262] | 259 | public event EventHandler Paused;
|
---|
| 260 | protected virtual void OnPaused() {
|
---|
| 261 | ExecutionState = ExecutionState.Paused;
|
---|
| 262 | EventHandler handler = Paused;
|
---|
| 263 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 264 | }
|
---|
[2851] | 265 | public event EventHandler Stopped;
|
---|
| 266 | protected virtual void OnStopped() {
|
---|
[3262] | 267 | ExecutionState = ExecutionState.Stopped;
|
---|
[3280] | 268 | runsCounter++;
|
---|
[3694] | 269 | runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
|
---|
[3262] | 270 | EventHandler handler = Stopped;
|
---|
| 271 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 272 | }
|
---|
| 273 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 274 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
[3262] | 275 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
| 276 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
[2851] | 277 | }
|
---|
[2975] | 278 |
|
---|
[2852] | 279 | protected virtual void DeregisterProblemEvents() {
|
---|
| 280 | problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
|
---|
| 281 | problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
|
---|
[2975] | 282 | problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
|
---|
[3739] | 283 | problem.Reset -= new EventHandler(Problem_Reset);
|
---|
[2852] | 284 | }
|
---|
| 285 | protected virtual void RegisterProblemEvents() {
|
---|
| 286 | problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
|
---|
| 287 | problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
|
---|
[2975] | 288 | problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
|
---|
[3739] | 289 | problem.Reset += new EventHandler(Problem_Reset);
|
---|
[2852] | 290 | }
|
---|
| 291 | protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
|
---|
| 292 | protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
|
---|
[2975] | 293 | protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
|
---|
[3739] | 294 | protected virtual void Problem_Reset(object sender, EventArgs e) {
|
---|
| 295 | Prepare();
|
---|
| 296 | }
|
---|
[3716] | 297 |
|
---|
| 298 | protected virtual void DeregisterRunsEvents() {
|
---|
| 299 | runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
| 300 | }
|
---|
| 301 | protected virtual void RegisterRunsEvents() {
|
---|
| 302 | runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
| 303 | }
|
---|
| 304 | protected virtual void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 305 | runsCounter = runs.Count;
|
---|
| 306 | }
|
---|
[2851] | 307 | #endregion
|
---|
| 308 | }
|
---|
| 309 | }
|
---|