Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs @ 12771

Last change on this file since 12771 was 12771, checked in by abeham, 9 years ago

#2431:

  • Added run collection view
  • Changed name of analyzers
  • Modified algorithms to include Execution Time as a result
File size: 9.2 KB
RevLine 
[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
22using System;
[2916]23using System.Linq;
[12771]24using System.Reflection;
[2851]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[12771]27using HeuristicLab.Data;
[2851]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2916]29using HeuristicLab.PluginInfrastructure;
[2851]30
31namespace HeuristicLab.Optimization {
32  /// <summary>
33  /// A base class for algorithms which use an engine for execution.
34  /// </summary>
35  [Item("EngineAlgorithm", "A base class for algorithms which use an engine for execution.")]
[3017]36  [StorableClass]
[2851]37  public abstract class EngineAlgorithm : Algorithm {
[3280]38    [Storable]
[2851]39    private OperatorGraph operatorGraph;
[3361]40    public OperatorGraph OperatorGraph {
[2851]41      get { return operatorGraph; }
[3361]42      protected set {
[2851]43        if (value == null) throw new ArgumentNullException();
44        if (value != operatorGraph) {
[3280]45          operatorGraph.InitialOperatorChanged -= new EventHandler(OperatorGraph_InitialOperatorChanged);
[2851]46          operatorGraph = value;
[3280]47          operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
[2851]48          OnOperatorGraphChanged();
49          Prepare();
50        }
51      }
52    }
53
54    [Storable]
55    private IScope globalScope;
56    protected IScope GlobalScope {
57      get { return globalScope; }
58    }
59
[3280]60    [Storable]
[2851]61    private IEngine engine;
62    public IEngine Engine {
63      get { return engine; }
64      set {
65        if (engine != value) {
66          if (engine != null) DeregisterEngineEvents();
67          engine = value;
68          if (engine != null) RegisterEngineEvents();
69          OnEngineChanged();
70          Prepare();
71        }
72      }
73    }
74
[12771]75    protected override void OnExecutionTimeChanged() {
76      base.OnExecutionTimeChanged();
77      IResult result;
78      if (!Results.TryGetValue("Execution Time", out result))
79        Results.Add(new Result("Execution Time", new TimeSpanValue(ExecutionTime)));
80      else ((TimeSpanValue)result.Value).Value = ExecutionTime;
81    }
82
[3226]83    public override ResultCollection Results {
[2882]84      get {
[3226]85        return (ResultCollection)globalScope.Variables["Results"].Value;
[2882]86      }
87    }
88
[2851]89    protected EngineAlgorithm()
90      : base() {
[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)
97      : base(name) {
[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, ParameterCollection parameters)
104      : base(name, parameters) {
[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)
111      : base(name, description) {
[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    }
117    protected EngineAlgorithm(string name, string description, ParameterCollection parameters)
118      : base(name, description, parameters) {
[2924]119      globalScope = new Scope("Global Scope");
[3226]120      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[3280]121      operatorGraph = new OperatorGraph();
122      Initialize();
[2851]123    }
[3280]124    [StorableConstructor]
125    protected EngineAlgorithm(bool deserializing) : base(deserializing) { }
[4722]126    [StorableHook(HookType.AfterDeserialization)]
127    private void AfterDeserialization() {
128      Initialize();
[5195]129
130      // BackwardsCompatibility3.3
131      #region Backwards compatible code (remove with 3.4)
132      // clear global scope if it contains any sub-scopes or additional variables
133      if ((ExecutionState == Core.ExecutionState.Stopped) && ((globalScope.SubScopes.Count > 0) || (globalScope.Variables.Count > 1))) {
134        ResultCollection results = Results;
135        globalScope.Clear();
136        globalScope.Variables.Add(new Variable("Results", results));
137      }
138      #endregion
[4722]139    }
[2851]140
[4722]141    protected EngineAlgorithm(EngineAlgorithm original, Cloner cloner)
142      : base(original, cloner) {
143      globalScope = cloner.Clone(original.globalScope);
144      engine = cloner.Clone(original.engine);
145      operatorGraph = cloner.Clone(original.operatorGraph);
146      Initialize();
147    }
148
[3280]149    private void Initialize() {
150      operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
[3303]151      if (engine == null) {
[2917]152        var types = ApplicationManager.Manager.GetTypes(typeof(IEngine));
153        Type t = types.FirstOrDefault(x => x.Name.Equals("SequentialEngine"));
154        if (t == null) t = types.FirstOrDefault();
[3280]155        if (t != null) engine = (IEngine)Activator.CreateInstance(t);
[2917]156      }
[3280]157      if (engine != null) RegisterEngineEvents();
[2916]158    }
159
[3551]160    public virtual IAlgorithm CreateUserDefinedAlgorithm() {
[4722]161      return new UserDefinedAlgorithm(this, new Cloner());
[2864]162    }
163
[3275]164    public override void Prepare() {
165      base.Prepare();
[2851]166      globalScope.Clear();
[3275]167      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
[2882]168
[3770]169      if ((engine != null) && (operatorGraph.InitialOperator != null)) {
[2851]170        ExecutionContext context = null;
[11961]171        if (Problem != null) {
172          foreach (var item in Problem.ExecutionContextItems)
173            context = new ExecutionContext(context, item, globalScope);
174        }
[3770]175        context = new ExecutionContext(context, this, globalScope);
176        context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
[2851]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() {
[4722]196      EventHandler handler = EngineChanged;
197      if (handler != null) handler(this, EventArgs.Empty);
[2851]198    }
[3361]199    public event EventHandler OperatorGraphChanged;
200    protected virtual void OnOperatorGraphChanged() {
201      EventHandler handler = OperatorGraphChanged;
202      if (handler != null) handler(this, EventArgs.Empty);
203    }
[2851]204
205    private void RegisterEngineEvents() {
206      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
207      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
[3262]208      Engine.Paused += new EventHandler(Engine_Paused);
209      Engine.Prepared += new EventHandler(Engine_Prepared);
210      Engine.Started += new EventHandler(Engine_Started);
[3261]211      Engine.Stopped += new EventHandler(Engine_Stopped);
[2851]212    }
213    private void DeregisterEngineEvents() {
214      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
215      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
[3262]216      Engine.Paused -= new EventHandler(Engine_Paused);
217      Engine.Prepared -= new EventHandler(Engine_Prepared);
218      Engine.Started -= new EventHandler(Engine_Started);
[3261]219      Engine.Stopped -= new EventHandler(Engine_Stopped);
[2851]220    }
221    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
222      OnExceptionOccurred(e.Value);
223    }
224    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
[3262]225      ExecutionTime = Engine.ExecutionTime;
[2851]226    }
[3262]227    private void Engine_Paused(object sender, EventArgs e) {
228      OnPaused();
229    }
230    private void Engine_Prepared(object sender, EventArgs e) {
231      OnPrepared();
232    }
233    private void Engine_Started(object sender, EventArgs e) {
234      OnStarted();
235    }
[3261]236    private void Engine_Stopped(object sender, EventArgs e) {
[5195]237      ResultCollection results = Results;
238      globalScope.Clear();
239      globalScope.Variables.Add(new Variable("Results", results));
[3261]240      OnStopped();
[2851]241    }
[3262]242
243    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
244      Prepare();
245    }
246    #endregion
[2851]247  }
248}
Note: See TracBrowser for help on using the repository browser.