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