[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;
|
---|
| 23 | using System.Drawing;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Optimization {
|
---|
| 29 | /// <summary>
|
---|
| 30 | /// A base class for algorithms.
|
---|
| 31 | /// </summary>
|
---|
| 32 | [Item("Algorithm", "A base class for algorithms.")]
|
---|
| 33 | public abstract class Algorithm : ParameterizedNamedItem, IAlgorithm {
|
---|
| 34 | public override Image ItemImage {
|
---|
| 35 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | private IProblem problem;
|
---|
| 39 | [Storable]
|
---|
| 40 | public IProblem Problem {
|
---|
| 41 | get { return problem; }
|
---|
| 42 | set {
|
---|
| 43 | if (problem != value) {
|
---|
| 44 | if (problem != null) problem.Changed -= new ChangedEventHandler(Problem_Changed);
|
---|
| 45 | problem = value;
|
---|
| 46 | if (problem != null) problem.Changed += new ChangedEventHandler(Problem_Changed);
|
---|
| 47 | OnProblemChanged();
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public abstract TimeSpan ExecutionTime { get; }
|
---|
| 53 |
|
---|
| 54 | private bool running;
|
---|
| 55 | public bool Running {
|
---|
| 56 | get { return running; }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public abstract bool Finished { get; }
|
---|
| 60 |
|
---|
| 61 | private bool canceled;
|
---|
| 62 | protected bool Canceled {
|
---|
| 63 | get { return canceled; }
|
---|
| 64 | private set {
|
---|
| 65 | if (canceled != value) {
|
---|
| 66 | canceled = value;
|
---|
| 67 | OnCanceledChanged();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | protected Algorithm() : base() { }
|
---|
| 73 | protected Algorithm(string name) : base(name) { }
|
---|
| 74 | protected Algorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
|
---|
| 75 | protected Algorithm(string name, string description) : base(name, description) { }
|
---|
| 76 | protected Algorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
|
---|
| 77 |
|
---|
| 78 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 79 | Algorithm clone = (Algorithm)base.Clone(cloner);
|
---|
| 80 | clone.Problem = (IProblem)cloner.Clone(problem);
|
---|
| 81 | clone.running = running;
|
---|
| 82 | clone.Canceled = canceled;
|
---|
| 83 | return clone;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public void Prepare() {
|
---|
| 87 | running = false;
|
---|
| 88 | Canceled = false;
|
---|
| 89 | OnPrepared();
|
---|
| 90 | }
|
---|
| 91 | public void Start() {
|
---|
| 92 | running = true;
|
---|
| 93 | Canceled = false;
|
---|
| 94 | OnStarted();
|
---|
| 95 | }
|
---|
| 96 | public void Stop() {
|
---|
| 97 | Canceled = true;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | #region Events
|
---|
| 101 | public event EventHandler ProblemChanged;
|
---|
| 102 | protected virtual void OnProblemChanged() {
|
---|
| 103 | if (ProblemChanged != null)
|
---|
| 104 | ProblemChanged(this, EventArgs.Empty);
|
---|
| 105 | OnChanged();
|
---|
| 106 | }
|
---|
| 107 | public event EventHandler ExecutionTimeChanged;
|
---|
| 108 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 109 | if (ExecutionTimeChanged != null)
|
---|
| 110 | ExecutionTimeChanged(this, EventArgs.Empty);
|
---|
| 111 | OnChanged();
|
---|
| 112 | }
|
---|
| 113 | public event EventHandler Prepared;
|
---|
| 114 | protected virtual void OnPrepared() {
|
---|
| 115 | if (Prepared != null)
|
---|
| 116 | Prepared(this, EventArgs.Empty);
|
---|
| 117 | OnChanged();
|
---|
| 118 | }
|
---|
| 119 | public event EventHandler Started;
|
---|
| 120 | protected virtual void OnStarted() {
|
---|
| 121 | if (Started != null)
|
---|
| 122 | Started(this, EventArgs.Empty);
|
---|
| 123 | OnChanged();
|
---|
| 124 | }
|
---|
| 125 | public event EventHandler Stopped;
|
---|
| 126 | protected virtual void OnStopped() {
|
---|
| 127 | if (Stopped != null)
|
---|
| 128 | Stopped(this, EventArgs.Empty);
|
---|
| 129 | OnChanged();
|
---|
| 130 | }
|
---|
| 131 | protected virtual void OnCanceledChanged() { }
|
---|
| 132 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 133 | protected virtual void OnExceptionOccurred(Exception exception) {
|
---|
| 134 | if (ExceptionOccurred != null)
|
---|
| 135 | ExceptionOccurred(this, new EventArgs<Exception>(exception));
|
---|
| 136 | }
|
---|
| 137 | private void Problem_Changed(object sender, ChangedEventArgs e) {
|
---|
| 138 | OnChanged(e);
|
---|
| 139 | }
|
---|
| 140 | #endregion
|
---|
| 141 | }
|
---|
| 142 | }
|
---|