[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;
|
---|
| 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.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[2851] | 35 | public abstract class Algorithm : ParameterizedNamedItem, IAlgorithm {
|
---|
| 36 | public override Image ItemImage {
|
---|
| 37 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[2852] | 40 | public virtual Type ProblemType {
|
---|
| 41 | get { return typeof(IProblem); }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[2851] | 44 | private IProblem problem;
|
---|
| 45 | [Storable]
|
---|
[2933] | 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 | }
|
---|
[2851] | 54 | public IProblem Problem {
|
---|
| 55 | get { return problem; }
|
---|
| 56 | set {
|
---|
| 57 | if (problem != value) {
|
---|
[2852] | 58 | if ((value != null) && !ProblemType.IsInstanceOfType(value)) throw new ArgumentException("Invalid problem type.");
|
---|
| 59 | if (problem != null) DeregisterProblemEvents();
|
---|
[2851] | 60 | problem = value;
|
---|
[2852] | 61 | if (problem != null) RegisterProblemEvents();
|
---|
[2851] | 62 | OnProblemChanged();
|
---|
[2864] | 63 | Prepare();
|
---|
[2851] | 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[3226] | 68 | public abstract ResultCollection Results { get; }
|
---|
[2882] | 69 |
|
---|
[2851] | 70 | public abstract TimeSpan ExecutionTime { get; }
|
---|
| 71 |
|
---|
| 72 | private bool running;
|
---|
| 73 | public bool Running {
|
---|
| 74 | get { return running; }
|
---|
[3226] | 75 | protected set {
|
---|
| 76 | if (running != value) {
|
---|
| 77 | running = value;
|
---|
| 78 | OnRunningChanged();
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
[2851] | 81 | }
|
---|
| 82 |
|
---|
| 83 | public abstract bool Finished { get; }
|
---|
| 84 |
|
---|
| 85 | private bool canceled;
|
---|
| 86 | protected bool Canceled {
|
---|
| 87 | get { return canceled; }
|
---|
| 88 | private set {
|
---|
| 89 | if (canceled != value) {
|
---|
| 90 | canceled = value;
|
---|
| 91 | OnCanceledChanged();
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | protected Algorithm() : base() { }
|
---|
| 97 | protected Algorithm(string name) : base(name) { }
|
---|
| 98 | protected Algorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
|
---|
| 99 | protected Algorithm(string name, string description) : base(name, description) { }
|
---|
| 100 | protected Algorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
|
---|
| 101 |
|
---|
| 102 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 103 | Algorithm clone = (Algorithm)base.Clone(cloner);
|
---|
| 104 | clone.Problem = (IProblem)cloner.Clone(problem);
|
---|
| 105 | clone.running = running;
|
---|
[3226] | 106 | clone.canceled = canceled;
|
---|
[2851] | 107 | return clone;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | public void Prepare() {
|
---|
| 111 | OnPrepared();
|
---|
| 112 | }
|
---|
| 113 | public void Start() {
|
---|
[3226] | 114 | Running = true;
|
---|
[2851] | 115 | Canceled = false;
|
---|
| 116 | OnStarted();
|
---|
| 117 | }
|
---|
| 118 | public void Stop() {
|
---|
| 119 | Canceled = true;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[3260] | 122 | public override void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
| 123 | base.CollectParameterValues(values);
|
---|
| 124 | Problem.CollectParameterValues(values);
|
---|
| 125 | }
|
---|
| 126 | public virtual void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
| 127 | foreach (IResult result in Results)
|
---|
| 128 | values.Add(result.Name, result.Value != null ? (IItem)result.Value.Clone() : null);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[2851] | 131 | #region Events
|
---|
| 132 | public event EventHandler ProblemChanged;
|
---|
| 133 | protected virtual void OnProblemChanged() {
|
---|
| 134 | if (ProblemChanged != null)
|
---|
| 135 | ProblemChanged(this, EventArgs.Empty);
|
---|
| 136 | }
|
---|
| 137 | public event EventHandler ExecutionTimeChanged;
|
---|
| 138 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 139 | if (ExecutionTimeChanged != null)
|
---|
| 140 | ExecutionTimeChanged(this, EventArgs.Empty);
|
---|
| 141 | }
|
---|
[3226] | 142 | public event EventHandler RunningChanged;
|
---|
| 143 | protected virtual void OnRunningChanged() {
|
---|
| 144 | if (RunningChanged != null)
|
---|
| 145 | RunningChanged(this, EventArgs.Empty);
|
---|
| 146 | }
|
---|
[2851] | 147 | public event EventHandler Prepared;
|
---|
| 148 | protected virtual void OnPrepared() {
|
---|
| 149 | if (Prepared != null)
|
---|
| 150 | Prepared(this, EventArgs.Empty);
|
---|
| 151 | }
|
---|
| 152 | public event EventHandler Started;
|
---|
| 153 | protected virtual void OnStarted() {
|
---|
| 154 | if (Started != null)
|
---|
| 155 | Started(this, EventArgs.Empty);
|
---|
| 156 | }
|
---|
| 157 | public event EventHandler Stopped;
|
---|
| 158 | protected virtual void OnStopped() {
|
---|
| 159 | if (Stopped != null)
|
---|
| 160 | Stopped(this, EventArgs.Empty);
|
---|
[3226] | 161 | Canceled = false;
|
---|
| 162 | Running = false;
|
---|
[2851] | 163 | }
|
---|
| 164 | protected virtual void OnCanceledChanged() { }
|
---|
| 165 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 166 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
| 167 | if (ExceptionOccurred != null)
|
---|
| 168 | ExceptionOccurred(this, new EventArgs<Exception>(exception));
|
---|
| 169 | }
|
---|
[2975] | 170 |
|
---|
[2852] | 171 | protected virtual void DeregisterProblemEvents() {
|
---|
| 172 | problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
|
---|
| 173 | problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
|
---|
[3107] | 174 | problem.VisualizerChanged -= new EventHandler(Problem_VisualizerChanged);
|
---|
[2975] | 175 | problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
|
---|
[2852] | 176 | }
|
---|
| 177 | protected virtual void RegisterProblemEvents() {
|
---|
| 178 | problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
|
---|
| 179 | problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
|
---|
[3107] | 180 | problem.VisualizerChanged += new EventHandler(Problem_VisualizerChanged);
|
---|
[2975] | 181 | problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
|
---|
[2852] | 182 | }
|
---|
| 183 |
|
---|
| 184 | protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
|
---|
| 185 | protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
|
---|
[3107] | 186 | protected virtual void Problem_VisualizerChanged(object sender, EventArgs e) { }
|
---|
[2975] | 187 | protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
|
---|
[2851] | 188 | #endregion
|
---|
| 189 | }
|
---|
| 190 | }
|
---|