[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 |
|
---|
[2882] | 90 | private ReadOnlyObservableKeyedCollection<string, IVariable> readOnlyResults;
|
---|
| 91 | public override IObservableKeyedCollection<string, IVariable> Results {
|
---|
| 92 | get {
|
---|
| 93 | if (readOnlyResults == null)
|
---|
| 94 | readOnlyResults = ((VariableCollection)globalScope.Variables["Results"].Value).AsReadOnly();
|
---|
| 95 | return readOnlyResults;
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[2851] | 99 | public override TimeSpan ExecutionTime {
|
---|
| 100 | get {
|
---|
| 101 | if (engine == null) return TimeSpan.Zero;
|
---|
| 102 | else return engine.ExecutionTime;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public override bool Finished {
|
---|
| 107 | get {
|
---|
| 108 | if (engine == null) return true;
|
---|
| 109 | else return engine.Finished;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | protected EngineAlgorithm()
|
---|
| 114 | : base() {
|
---|
[2924] | 115 | globalScope = new Scope("Global Scope");
|
---|
[2882] | 116 | globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
|
---|
[2851] | 117 | OperatorGraph = new OperatorGraph();
|
---|
[2916] | 118 | InitializeEngine();
|
---|
[2851] | 119 | }
|
---|
| 120 | protected EngineAlgorithm(string name)
|
---|
| 121 | : base(name) {
|
---|
[2924] | 122 | globalScope = new Scope("Global Scope");
|
---|
[2882] | 123 | globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
|
---|
[2851] | 124 | OperatorGraph = new OperatorGraph();
|
---|
[2916] | 125 | InitializeEngine();
|
---|
[2851] | 126 | }
|
---|
| 127 | protected EngineAlgorithm(string name, ParameterCollection parameters)
|
---|
| 128 | : base(name, parameters) {
|
---|
[2924] | 129 | globalScope = new Scope("Global Scope");
|
---|
[2882] | 130 | globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
|
---|
[2851] | 131 | OperatorGraph = new OperatorGraph();
|
---|
[2916] | 132 | InitializeEngine();
|
---|
[2851] | 133 | }
|
---|
| 134 | protected EngineAlgorithm(string name, string description)
|
---|
| 135 | : base(name, description) {
|
---|
[2924] | 136 | globalScope = new Scope("Global Scope");
|
---|
[2882] | 137 | globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
|
---|
[2851] | 138 | OperatorGraph = new OperatorGraph();
|
---|
[2916] | 139 | InitializeEngine();
|
---|
[2851] | 140 | }
|
---|
| 141 | protected EngineAlgorithm(string name, string description, ParameterCollection parameters)
|
---|
| 142 | : base(name, description, parameters) {
|
---|
[2924] | 143 | globalScope = new Scope("Global Scope");
|
---|
[2882] | 144 | globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
|
---|
[2851] | 145 | OperatorGraph = new OperatorGraph();
|
---|
[2916] | 146 | InitializeEngine();
|
---|
[2851] | 147 | }
|
---|
| 148 |
|
---|
[2916] | 149 | private void InitializeEngine() {
|
---|
[2917] | 150 | if (ApplicationManager.Manager != null) {
|
---|
| 151 | var types = ApplicationManager.Manager.GetTypes(typeof(IEngine));
|
---|
| 152 | Type t = types.FirstOrDefault(x => x.Name.Equals("SequentialEngine"));
|
---|
| 153 | if (t == null) t = types.FirstOrDefault();
|
---|
| 154 | if (t != null) Engine = (IEngine)Activator.CreateInstance(t);
|
---|
| 155 | }
|
---|
[2916] | 156 | }
|
---|
| 157 |
|
---|
[2851] | 158 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 159 | EngineAlgorithm clone = (EngineAlgorithm)base.Clone(cloner);
|
---|
| 160 | clone.globalScope = (IScope)cloner.Clone(globalScope);
|
---|
| 161 | clone.Engine = (IEngine)cloner.Clone(engine);
|
---|
| 162 | clone.OperatorGraph = (OperatorGraph)cloner.Clone(operatorGraph);
|
---|
| 163 | return clone;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[2864] | 166 | public UserDefinedAlgorithm CreateUserDefinedAlgorithm() {
|
---|
| 167 | UserDefinedAlgorithm algorithm = new UserDefinedAlgorithm(Name, Description);
|
---|
| 168 | Cloner cloner = new Cloner();
|
---|
| 169 | foreach (IParameter param in Parameters)
|
---|
| 170 | algorithm.Parameters.Add((IParameter)cloner.Clone(param));
|
---|
| 171 | algorithm.Problem = (IProblem)cloner.Clone(Problem);
|
---|
| 172 | algorithm.Engine = (IEngine)cloner.Clone(engine);
|
---|
| 173 | algorithm.OperatorGraph = (OperatorGraph)cloner.Clone(operatorGraph);
|
---|
| 174 | return algorithm;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[2851] | 177 | protected override void OnCanceledChanged() {
|
---|
| 178 | if (Canceled && (engine != null))
|
---|
| 179 | engine.Stop();
|
---|
| 180 | }
|
---|
| 181 | protected override void OnPrepared() {
|
---|
| 182 | globalScope.Clear();
|
---|
[2882] | 183 | globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
|
---|
| 184 | readOnlyResults = null;
|
---|
| 185 |
|
---|
[2851] | 186 | if (engine != null) {
|
---|
| 187 | ExecutionContext context = null;
|
---|
| 188 | if (operatorGraph.InitialOperator != null) {
|
---|
| 189 | if (Problem != null) {
|
---|
| 190 | context = new ExecutionContext(context, Problem, globalScope);
|
---|
| 191 | foreach (IParameter param in Problem.Parameters)
|
---|
| 192 | param.ExecutionContext = context;
|
---|
| 193 | }
|
---|
| 194 | context = new ExecutionContext(context, this, globalScope);
|
---|
| 195 | foreach (IParameter param in this.Parameters)
|
---|
| 196 | param.ExecutionContext = context;
|
---|
| 197 | context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
|
---|
| 198 | }
|
---|
| 199 | engine.Prepare(context);
|
---|
| 200 | }
|
---|
| 201 | base.OnPrepared();
|
---|
| 202 | }
|
---|
| 203 | protected override void OnStarted() {
|
---|
| 204 | if (engine != null) engine.Start();
|
---|
| 205 | base.OnStarted();
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | protected virtual void OnOperatorGraphChanged() { }
|
---|
| 209 |
|
---|
| 210 | public event EventHandler EngineChanged;
|
---|
| 211 | protected virtual void OnEngineChanged() {
|
---|
| 212 | if (EngineChanged != null)
|
---|
| 213 | EngineChanged(this, EventArgs.Empty);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
|
---|
| 217 | Prepare();
|
---|
| 218 | }
|
---|
| 219 | private void RegisterEngineEvents() {
|
---|
| 220 | Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
|
---|
| 221 | Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
|
---|
| 222 | Engine.Stopped += new EventHandler(Engine_Stopped);
|
---|
| 223 | }
|
---|
| 224 | private void DeregisterEngineEvents() {
|
---|
| 225 | Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
|
---|
| 226 | Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
|
---|
| 227 | Engine.Stopped -= new EventHandler(Engine_Stopped);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 231 | OnExceptionOccurred(e.Value);
|
---|
| 232 | }
|
---|
| 233 | private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 234 | OnExecutionTimeChanged();
|
---|
| 235 | }
|
---|
| 236 | private void Engine_Stopped(object sender, EventArgs e) {
|
---|
| 237 | OnStopped();
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | }
|
---|