Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on algorithm batch processing (#947).

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