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, 12 years ago

Continued work on algorithm batch processing (#947).

File size: 9.0 KB
Line 
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;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.PluginInfrastructure;
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.")]
34  [StorableClass]
35  public abstract class EngineAlgorithm : Algorithm {
36    private OperatorGraph operatorGraph;
37    [Storable]
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    }
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]
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    }
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
89    public override ResultCollection Results {
90      get {
91        return (ResultCollection)globalScope.Variables["Results"].Value;
92      }
93    }
94
95    protected EngineAlgorithm()
96      : base() {
97      globalScope = new Scope("Global Scope");
98      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
99      OperatorGraph = new OperatorGraph();
100      InitializeEngine();
101    }
102    protected EngineAlgorithm(string name)
103      : base(name) {
104      globalScope = new Scope("Global Scope");
105      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
106      OperatorGraph = new OperatorGraph();
107      InitializeEngine();
108    }
109    protected EngineAlgorithm(string name, ParameterCollection parameters)
110      : base(name, parameters) {
111      globalScope = new Scope("Global Scope");
112      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
113      OperatorGraph = new OperatorGraph();
114      InitializeEngine();
115    }
116    protected EngineAlgorithm(string name, string description)
117      : base(name, description) {
118      globalScope = new Scope("Global Scope");
119      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
120      OperatorGraph = new OperatorGraph();
121      InitializeEngine();
122    }
123    protected EngineAlgorithm(string name, string description, ParameterCollection parameters)
124      : base(name, description, parameters) {
125      globalScope = new Scope("Global Scope");
126      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
127      OperatorGraph = new OperatorGraph();
128      InitializeEngine();
129    }
130
131    private void InitializeEngine() {
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      }
138    }
139
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
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
159    public override void Prepare() {
160      base.Prepare();
161      globalScope.Clear();
162      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
163
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    }
180    public override void Start() {
181      base.Start();
182      if (engine != null) engine.Start();
183    }
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    }
192
193    #region Events
194    public event EventHandler EngineChanged;
195    protected virtual void OnEngineChanged() {
196      if (EngineChanged != null)
197        EngineChanged(this, EventArgs.Empty);
198    }
199    protected virtual void OnOperatorGraphChanged() { }
200
201    private void RegisterEngineEvents() {
202      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
203      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
204      Engine.Paused += new EventHandler(Engine_Paused);
205      Engine.Prepared += new EventHandler(Engine_Prepared);
206      Engine.Started += new EventHandler(Engine_Started);
207      Engine.Stopped += new EventHandler(Engine_Stopped);
208    }
209    private void DeregisterEngineEvents() {
210      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
211      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
212      Engine.Paused -= new EventHandler(Engine_Paused);
213      Engine.Prepared -= new EventHandler(Engine_Prepared);
214      Engine.Started -= new EventHandler(Engine_Started);
215      Engine.Stopped -= new EventHandler(Engine_Stopped);
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) {
221      ExecutionTime = Engine.ExecutionTime;
222    }
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    }
232    private void Engine_Stopped(object sender, EventArgs e) {
233      OnStopped();
234    }
235
236    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
237      Prepare();
238    }
239    #endregion
240  }
241}
Note: See TracBrowser for help on using the repository browser.