[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 {
|
---|
[5615] | 73 | get { return typeof(IProblem); }
|
---|
[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));
|
---|
[5650] | 226 | CollectResultsRecursively("", Results, values);
|
---|
[3260] | 227 | }
|
---|
| 228 |
|
---|
[5650] | 229 | private void CollectResultsRecursively(string path, ResultCollection results, IDictionary<string, IItem> values) {
|
---|
| 230 | foreach (IResult result in results) {
|
---|
| 231 | values.Add(path + result.Name, result.Value);
|
---|
| 232 | ResultCollection childCollection = result.Value as ResultCollection;
|
---|
| 233 | if (childCollection != null) {
|
---|
| 234 | CollectResultsRecursively(path + result.Name + ".", childCollection, values);
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[2851] | 239 | #region Events
|
---|
[3262] | 240 | public event EventHandler ExecutionStateChanged;
|
---|
| 241 | protected virtual void OnExecutionStateChanged() {
|
---|
| 242 | EventHandler handler = ExecutionStateChanged;
|
---|
| 243 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 244 | }
|
---|
| 245 | public event EventHandler ExecutionTimeChanged;
|
---|
| 246 | protected virtual void OnExecutionTimeChanged() {
|
---|
[3262] | 247 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 248 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 249 | }
|
---|
[3262] | 250 | public event EventHandler ProblemChanged;
|
---|
| 251 | protected virtual void OnProblemChanged() {
|
---|
| 252 | EventHandler handler = ProblemChanged;
|
---|
| 253 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 254 | }
|
---|
[4102] | 255 | public event EventHandler StoreAlgorithmInEachRunChanged;
|
---|
| 256 | protected virtual void OnStoreAlgorithmInEachRunChanged() {
|
---|
| 257 | EventHandler handler = StoreAlgorithmInEachRunChanged;
|
---|
| 258 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 259 | }
|
---|
[2851] | 260 | public event EventHandler Prepared;
|
---|
| 261 | protected virtual void OnPrepared() {
|
---|
[3275] | 262 | ExecutionTime = TimeSpan.Zero;
|
---|
[3262] | 263 | ExecutionState = ExecutionState.Prepared;
|
---|
| 264 | EventHandler handler = Prepared;
|
---|
| 265 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 266 | }
|
---|
| 267 | public event EventHandler Started;
|
---|
| 268 | protected virtual void OnStarted() {
|
---|
[3262] | 269 | ExecutionState = ExecutionState.Started;
|
---|
| 270 | EventHandler handler = Started;
|
---|
| 271 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 272 | }
|
---|
[3262] | 273 | public event EventHandler Paused;
|
---|
| 274 | protected virtual void OnPaused() {
|
---|
| 275 | ExecutionState = ExecutionState.Paused;
|
---|
| 276 | EventHandler handler = Paused;
|
---|
| 277 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 278 | }
|
---|
[2851] | 279 | public event EventHandler Stopped;
|
---|
| 280 | protected virtual void OnStopped() {
|
---|
[3262] | 281 | ExecutionState = ExecutionState.Stopped;
|
---|
[3280] | 282 | runsCounter++;
|
---|
[3694] | 283 | runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
|
---|
[3262] | 284 | EventHandler handler = Stopped;
|
---|
| 285 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[2851] | 286 | }
|
---|
| 287 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 288 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
[3262] | 289 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
| 290 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
[2851] | 291 | }
|
---|
[2975] | 292 |
|
---|
[2852] | 293 | protected virtual void DeregisterProblemEvents() {
|
---|
[2975] | 294 | problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
|
---|
[3739] | 295 | problem.Reset -= new EventHandler(Problem_Reset);
|
---|
[2852] | 296 | }
|
---|
| 297 | protected virtual void RegisterProblemEvents() {
|
---|
[2975] | 298 | problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
|
---|
[3739] | 299 | problem.Reset += new EventHandler(Problem_Reset);
|
---|
[2852] | 300 | }
|
---|
[2975] | 301 | protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
|
---|
[3739] | 302 | protected virtual void Problem_Reset(object sender, EventArgs e) {
|
---|
| 303 | Prepare();
|
---|
| 304 | }
|
---|
[3716] | 305 |
|
---|
| 306 | protected virtual void DeregisterRunsEvents() {
|
---|
| 307 | runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
| 308 | }
|
---|
| 309 | protected virtual void RegisterRunsEvents() {
|
---|
| 310 | runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
| 311 | }
|
---|
| 312 | protected virtual void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 313 | runsCounter = runs.Count;
|
---|
| 314 | }
|
---|
[2851] | 315 | #endregion
|
---|
| 316 | }
|
---|
| 317 | }
|
---|