Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3269 was 3262, checked in by swagner, 15 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;
[2882]24using HeuristicLab.Collections;
[2851]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2916]28using HeuristicLab.PluginInfrastructure;
[2851]29
30namespace 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
[3226]90    public override ResultCollection Results {
[2882]91      get {
[3226]92        return (ResultCollection)globalScope.Variables["Results"].Value;
[2882]93      }
94    }
95
[2851]96    protected EngineAlgorithm()
97      : base() {
[2924]98      globalScope = new Scope("Global Scope");
[3226]99      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[2851]100      OperatorGraph = new OperatorGraph();
[2916]101      InitializeEngine();
[2851]102    }
103    protected EngineAlgorithm(string name)
104      : base(name) {
[2924]105      globalScope = new Scope("Global Scope");
[3226]106      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[2851]107      OperatorGraph = new OperatorGraph();
[2916]108      InitializeEngine();
[2851]109    }
110    protected EngineAlgorithm(string name, ParameterCollection parameters)
111      : base(name, parameters) {
[2924]112      globalScope = new Scope("Global Scope");
[3226]113      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[2851]114      OperatorGraph = new OperatorGraph();
[2916]115      InitializeEngine();
[2851]116    }
117    protected EngineAlgorithm(string name, string description)
118      : base(name, description) {
[2924]119      globalScope = new Scope("Global Scope");
[3226]120      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[2851]121      OperatorGraph = new OperatorGraph();
[2916]122      InitializeEngine();
[2851]123    }
124    protected EngineAlgorithm(string name, string description, ParameterCollection parameters)
125      : base(name, description, parameters) {
[2924]126      globalScope = new Scope("Global Scope");
[3226]127      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[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
[3262]160    public override void Prepare() {
161      base.Prepare();
[2851]162      globalScope.Clear();
[3226]163      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[2882]164
[2851]165      if (engine != null) {
166        ExecutionContext context = null;
167        if (operatorGraph.InitialOperator != null) {
168          if (Problem != null) {
169            context = new ExecutionContext(context, Problem, globalScope);
170            foreach (IParameter param in Problem.Parameters)
171              param.ExecutionContext = context;
172          }
173          context = new ExecutionContext(context, this, globalScope);
174          foreach (IParameter param in this.Parameters)
175            param.ExecutionContext = context;
176          context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
177        }
178        engine.Prepare(context);
179      }
180    }
[3262]181    public override void Start() {
182      base.Start();
[2851]183      if (engine != null) engine.Start();
184    }
[3262]185    public override void Pause() {
186      base.Pause();
187      if (engine != null) engine.Pause();
188    }
189    public override void Stop() {
190      base.Stop();
191      if (engine != null) engine.Stop();
192    }
[2851]193
[3262]194    #region Events
[2851]195    public event EventHandler EngineChanged;
196    protected virtual void OnEngineChanged() {
197      if (EngineChanged != null)
198        EngineChanged(this, EventArgs.Empty);
199    }
[3262]200    protected virtual void OnOperatorGraphChanged() { }
[2851]201
202    private void RegisterEngineEvents() {
203      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
204      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
[3262]205      Engine.Paused += new EventHandler(Engine_Paused);
206      Engine.Prepared += new EventHandler(Engine_Prepared);
207      Engine.Started += new EventHandler(Engine_Started);
[3261]208      Engine.Stopped += new EventHandler(Engine_Stopped);
[2851]209    }
210    private void DeregisterEngineEvents() {
211      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
212      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
[3262]213      Engine.Paused -= new EventHandler(Engine_Paused);
214      Engine.Prepared -= new EventHandler(Engine_Prepared);
215      Engine.Started -= new EventHandler(Engine_Started);
[3261]216      Engine.Stopped -= new EventHandler(Engine_Stopped);
[2851]217    }
218    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
219      OnExceptionOccurred(e.Value);
220    }
221    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
[3262]222      ExecutionTime = Engine.ExecutionTime;
[2851]223    }
[3262]224    private void Engine_Paused(object sender, EventArgs e) {
225      OnPaused();
226    }
227    private void Engine_Prepared(object sender, EventArgs e) {
228      OnPrepared();
229    }
230    private void Engine_Started(object sender, EventArgs e) {
231      OnStarted();
232    }
[3261]233    private void Engine_Stopped(object sender, EventArgs e) {
234      OnStopped();
[2851]235    }
[3262]236
237    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
238      Prepare();
239    }
240    #endregion
[2851]241  }
242}
Note: See TracBrowser for help on using the repository browser.