[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;
|
---|
[2916] | 23 | using System.Linq;
|
---|
[2851] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2916] | 27 | using HeuristicLab.PluginInfrastructure;
|
---|
[2851] | 28 |
|
---|
| 29 | namespace HeuristicLab.Optimization {
|
---|
| 30 | /// <summary>
|
---|
| 31 | /// A base class for algorithms which use an engine for execution.
|
---|
| 32 | /// </summary>
|
---|
| 33 | [Item("EngineAlgorithm", "A base class for algorithms which use an engine for execution.")]
|
---|
[3017] | 34 | [StorableClass]
|
---|
[2851] | 35 | public abstract class EngineAlgorithm : Algorithm {
|
---|
[3280] | 36 | [Storable]
|
---|
[2851] | 37 | private OperatorGraph operatorGraph;
|
---|
[3361] | 38 | public OperatorGraph OperatorGraph {
|
---|
[2851] | 39 | get { return operatorGraph; }
|
---|
[3361] | 40 | protected set {
|
---|
[2851] | 41 | if (value == null) throw new ArgumentNullException();
|
---|
| 42 | if (value != operatorGraph) {
|
---|
[3280] | 43 | operatorGraph.InitialOperatorChanged -= new EventHandler(OperatorGraph_InitialOperatorChanged);
|
---|
[2851] | 44 | operatorGraph = value;
|
---|
[3280] | 45 | operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
|
---|
[2851] | 46 | OnOperatorGraphChanged();
|
---|
| 47 | Prepare();
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [Storable]
|
---|
| 53 | private IScope globalScope;
|
---|
| 54 | protected IScope GlobalScope {
|
---|
| 55 | get { return globalScope; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[3280] | 58 | [Storable]
|
---|
[2851] | 59 | private IEngine engine;
|
---|
| 60 | public IEngine Engine {
|
---|
| 61 | get { return engine; }
|
---|
| 62 | set {
|
---|
| 63 | if (engine != value) {
|
---|
| 64 | if (engine != null) DeregisterEngineEvents();
|
---|
| 65 | engine = value;
|
---|
| 66 | if (engine != null) RegisterEngineEvents();
|
---|
| 67 | OnEngineChanged();
|
---|
| 68 | Prepare();
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[3226] | 73 | public override ResultCollection Results {
|
---|
[2882] | 74 | get {
|
---|
[3226] | 75 | return (ResultCollection)globalScope.Variables["Results"].Value;
|
---|
[2882] | 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[2851] | 79 | protected EngineAlgorithm()
|
---|
| 80 | : base() {
|
---|
[2924] | 81 | globalScope = new Scope("Global Scope");
|
---|
[3226] | 82 | globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
|
---|
[3280] | 83 | operatorGraph = new OperatorGraph();
|
---|
| 84 | Initialize();
|
---|
[2851] | 85 | }
|
---|
| 86 | protected EngineAlgorithm(string name)
|
---|
| 87 | : base(name) {
|
---|
[2924] | 88 | globalScope = new Scope("Global Scope");
|
---|
[3226] | 89 | globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
|
---|
[3280] | 90 | operatorGraph = new OperatorGraph();
|
---|
| 91 | Initialize();
|
---|
[2851] | 92 | }
|
---|
| 93 | protected EngineAlgorithm(string name, ParameterCollection parameters)
|
---|
| 94 | : base(name, parameters) {
|
---|
[2924] | 95 | globalScope = new Scope("Global Scope");
|
---|
[3226] | 96 | globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
|
---|
[3280] | 97 | operatorGraph = new OperatorGraph();
|
---|
| 98 | Initialize();
|
---|
[2851] | 99 | }
|
---|
| 100 | protected EngineAlgorithm(string name, string description)
|
---|
| 101 | : base(name, description) {
|
---|
[2924] | 102 | globalScope = new Scope("Global Scope");
|
---|
[3226] | 103 | globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
|
---|
[3280] | 104 | operatorGraph = new OperatorGraph();
|
---|
| 105 | Initialize();
|
---|
[2851] | 106 | }
|
---|
| 107 | protected EngineAlgorithm(string name, string description, ParameterCollection parameters)
|
---|
| 108 | : base(name, description, parameters) {
|
---|
[2924] | 109 | globalScope = new Scope("Global Scope");
|
---|
[3226] | 110 | globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
|
---|
[3280] | 111 | operatorGraph = new OperatorGraph();
|
---|
| 112 | Initialize();
|
---|
[2851] | 113 | }
|
---|
[3280] | 114 | [StorableConstructor]
|
---|
| 115 | protected EngineAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
[2851] | 116 |
|
---|
[3280] | 117 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 118 | private void Initialize() {
|
---|
| 119 | operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
|
---|
[3303] | 120 | if (engine == null) {
|
---|
[2917] | 121 | var types = ApplicationManager.Manager.GetTypes(typeof(IEngine));
|
---|
| 122 | Type t = types.FirstOrDefault(x => x.Name.Equals("SequentialEngine"));
|
---|
| 123 | if (t == null) t = types.FirstOrDefault();
|
---|
[3280] | 124 | if (t != null) engine = (IEngine)Activator.CreateInstance(t);
|
---|
[2917] | 125 | }
|
---|
[3280] | 126 | if (engine != null) RegisterEngineEvents();
|
---|
[2916] | 127 | }
|
---|
| 128 |
|
---|
[2851] | 129 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 130 | EngineAlgorithm clone = (EngineAlgorithm)base.Clone(cloner);
|
---|
| 131 | clone.globalScope = (IScope)cloner.Clone(globalScope);
|
---|
[3280] | 132 | clone.engine = (IEngine)cloner.Clone(engine);
|
---|
| 133 | clone.operatorGraph = (OperatorGraph)cloner.Clone(operatorGraph);
|
---|
| 134 | clone.Initialize();
|
---|
[2851] | 135 | return clone;
|
---|
| 136 | }
|
---|
[3286] | 137 | protected override void Clone(IDeepCloneable clone, Cloner cloner) {
|
---|
| 138 | base.Clone(clone, cloner);
|
---|
| 139 | EngineAlgorithm algorithm = clone as EngineAlgorithm;
|
---|
| 140 | if (algorithm != null) {
|
---|
| 141 | algorithm.globalScope = (IScope)cloner.Clone(globalScope);
|
---|
| 142 | algorithm.engine = (IEngine)cloner.Clone(engine);
|
---|
| 143 | algorithm.operatorGraph = (OperatorGraph)cloner.Clone(operatorGraph);
|
---|
| 144 | algorithm.Initialize();
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[2851] | 147 |
|
---|
[3551] | 148 | public virtual IAlgorithm CreateUserDefinedAlgorithm() {
|
---|
[3286] | 149 | UserDefinedAlgorithm algorithm = new UserDefinedAlgorithm();
|
---|
| 150 | Cloner cloner = new Cloner();
|
---|
| 151 | cloner.RegisterClonedObject(this, algorithm);
|
---|
| 152 | Clone(algorithm, cloner);
|
---|
| 153 | return algorithm;
|
---|
[2864] | 154 | }
|
---|
| 155 |
|
---|
[3275] | 156 | public override void Prepare() {
|
---|
| 157 | base.Prepare();
|
---|
[2851] | 158 | globalScope.Clear();
|
---|
[3275] | 159 | globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
|
---|
[2882] | 160 |
|
---|
[3770] | 161 | if ((engine != null) && (operatorGraph.InitialOperator != null)) {
|
---|
[2851] | 162 | ExecutionContext context = null;
|
---|
[3770] | 163 | if (Problem != null) context = new ExecutionContext(context, Problem, globalScope);
|
---|
| 164 | context = new ExecutionContext(context, this, globalScope);
|
---|
| 165 | context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
|
---|
[2851] | 166 | engine.Prepare(context);
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
[3262] | 169 | public override void Start() {
|
---|
| 170 | base.Start();
|
---|
[2851] | 171 | if (engine != null) engine.Start();
|
---|
| 172 | }
|
---|
[3262] | 173 | public override void Pause() {
|
---|
| 174 | base.Pause();
|
---|
| 175 | if (engine != null) engine.Pause();
|
---|
| 176 | }
|
---|
| 177 | public override void Stop() {
|
---|
| 178 | base.Stop();
|
---|
| 179 | if (engine != null) engine.Stop();
|
---|
| 180 | }
|
---|
[2851] | 181 |
|
---|
[3262] | 182 | #region Events
|
---|
[2851] | 183 | public event EventHandler EngineChanged;
|
---|
| 184 | protected virtual void OnEngineChanged() {
|
---|
| 185 | if (EngineChanged != null)
|
---|
| 186 | EngineChanged(this, EventArgs.Empty);
|
---|
| 187 | }
|
---|
[3361] | 188 | public event EventHandler OperatorGraphChanged;
|
---|
| 189 | protected virtual void OnOperatorGraphChanged() {
|
---|
| 190 | EventHandler handler = OperatorGraphChanged;
|
---|
| 191 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 192 | }
|
---|
[2851] | 193 |
|
---|
| 194 | private void RegisterEngineEvents() {
|
---|
| 195 | Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
|
---|
| 196 | Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
|
---|
[3262] | 197 | Engine.Paused += new EventHandler(Engine_Paused);
|
---|
| 198 | Engine.Prepared += new EventHandler(Engine_Prepared);
|
---|
| 199 | Engine.Started += new EventHandler(Engine_Started);
|
---|
[3261] | 200 | Engine.Stopped += new EventHandler(Engine_Stopped);
|
---|
[2851] | 201 | }
|
---|
| 202 | private void DeregisterEngineEvents() {
|
---|
| 203 | Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
|
---|
| 204 | Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
|
---|
[3262] | 205 | Engine.Paused -= new EventHandler(Engine_Paused);
|
---|
| 206 | Engine.Prepared -= new EventHandler(Engine_Prepared);
|
---|
| 207 | Engine.Started -= new EventHandler(Engine_Started);
|
---|
[3261] | 208 | Engine.Stopped -= new EventHandler(Engine_Stopped);
|
---|
[2851] | 209 | }
|
---|
| 210 | private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 211 | OnExceptionOccurred(e.Value);
|
---|
| 212 | }
|
---|
| 213 | private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
[3262] | 214 | ExecutionTime = Engine.ExecutionTime;
|
---|
[2851] | 215 | }
|
---|
[3262] | 216 | private void Engine_Paused(object sender, EventArgs e) {
|
---|
| 217 | OnPaused();
|
---|
| 218 | }
|
---|
| 219 | private void Engine_Prepared(object sender, EventArgs e) {
|
---|
| 220 | OnPrepared();
|
---|
| 221 | }
|
---|
| 222 | private void Engine_Started(object sender, EventArgs e) {
|
---|
| 223 | OnStarted();
|
---|
| 224 | }
|
---|
[3261] | 225 | private void Engine_Stopped(object sender, EventArgs e) {
|
---|
| 226 | OnStopped();
|
---|
[2851] | 227 | }
|
---|
[3262] | 228 |
|
---|
| 229 | private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
|
---|
| 230 | Prepare();
|
---|
| 231 | }
|
---|
| 232 | #endregion
|
---|
[2851] | 233 | }
|
---|
| 234 | }
|
---|