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