Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/EngineAlgorithm.cs @ 4745

Last change on this file since 4745 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 8.1 KB
RevLine 
[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
22using System;
[2916]23using System.Linq;
[2851]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2916]27using HeuristicLab.PluginInfrastructure;
[2851]28
29namespace 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) { }
[4722]116    [StorableHook(HookType.AfterDeserialization)]
117    private void AfterDeserialization() {
118      Initialize();
119    }
[2851]120
[4722]121    protected EngineAlgorithm(EngineAlgorithm original, Cloner cloner)
122      : base(original, cloner) {
123      globalScope = cloner.Clone(original.globalScope);
124      engine = cloner.Clone(original.engine);
125      operatorGraph = cloner.Clone(original.operatorGraph);
126      Initialize();
127    }
128
[3280]129    private void Initialize() {
130      operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
[3303]131      if (engine == null) {
[2917]132        var types = ApplicationManager.Manager.GetTypes(typeof(IEngine));
133        Type t = types.FirstOrDefault(x => x.Name.Equals("SequentialEngine"));
134        if (t == null) t = types.FirstOrDefault();
[3280]135        if (t != null) engine = (IEngine)Activator.CreateInstance(t);
[2917]136      }
[3280]137      if (engine != null) RegisterEngineEvents();
[2916]138    }
139
[3551]140    public virtual IAlgorithm CreateUserDefinedAlgorithm() {
[4722]141      return new UserDefinedAlgorithm(this, new Cloner());
[2864]142    }
143
[3275]144    public override void Prepare() {
145      base.Prepare();
[2851]146      globalScope.Clear();
[3275]147      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[2882]148
[3770]149      if ((engine != null) && (operatorGraph.InitialOperator != null)) {
[2851]150        ExecutionContext context = null;
[3770]151        if (Problem != null) context = new ExecutionContext(context, Problem, globalScope);
152        context = new ExecutionContext(context, this, globalScope);
153        context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
[2851]154        engine.Prepare(context);
155      }
156    }
[3262]157    public override void Start() {
158      base.Start();
[2851]159      if (engine != null) engine.Start();
160    }
[3262]161    public override void Pause() {
162      base.Pause();
163      if (engine != null) engine.Pause();
164    }
165    public override void Stop() {
166      base.Stop();
167      if (engine != null) engine.Stop();
168    }
[2851]169
[3262]170    #region Events
[2851]171    public event EventHandler EngineChanged;
172    protected virtual void OnEngineChanged() {
[4722]173      EventHandler handler = EngineChanged;
174      if (handler != null) handler(this, EventArgs.Empty);
[2851]175    }
[3361]176    public event EventHandler OperatorGraphChanged;
177    protected virtual void OnOperatorGraphChanged() {
178      EventHandler handler = OperatorGraphChanged;
179      if (handler != null) handler(this, EventArgs.Empty);
180    }
[2851]181
182    private void RegisterEngineEvents() {
183      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
184      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
[3262]185      Engine.Paused += new EventHandler(Engine_Paused);
186      Engine.Prepared += new EventHandler(Engine_Prepared);
187      Engine.Started += new EventHandler(Engine_Started);
[3261]188      Engine.Stopped += new EventHandler(Engine_Stopped);
[2851]189    }
190    private void DeregisterEngineEvents() {
191      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
192      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
[3262]193      Engine.Paused -= new EventHandler(Engine_Paused);
194      Engine.Prepared -= new EventHandler(Engine_Prepared);
195      Engine.Started -= new EventHandler(Engine_Started);
[3261]196      Engine.Stopped -= new EventHandler(Engine_Stopped);
[2851]197    }
198    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
199      OnExceptionOccurred(e.Value);
200    }
201    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
[3262]202      ExecutionTime = Engine.ExecutionTime;
[2851]203    }
[3262]204    private void Engine_Paused(object sender, EventArgs e) {
205      OnPaused();
206    }
207    private void Engine_Prepared(object sender, EventArgs e) {
208      OnPrepared();
209    }
210    private void Engine_Started(object sender, EventArgs e) {
211      OnStarted();
212    }
[3261]213    private void Engine_Stopped(object sender, EventArgs e) {
214      OnStopped();
[2851]215    }
[3262]216
217    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
218      Prepare();
219    }
220    #endregion
[2851]221  }
222}
Note: See TracBrowser for help on using the repository browser.