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