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