Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs @ 15719

Last change on this file since 15719 was 15719, checked in by abeham, 6 years ago

#1614:

  • branched analysis and analysis views
  • merged changes from #2457 into optimization and optimization.views
File size: 8.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    [Storable]
37    private OperatorGraph operatorGraph;
38    public OperatorGraph OperatorGraph {
39      get { return operatorGraph; }
40      protected set {
41        if (value == null) throw new ArgumentNullException();
42        if (value != operatorGraph) {
43          operatorGraph.InitialOperatorChanged -= new EventHandler(OperatorGraph_InitialOperatorChanged);
44          operatorGraph = value;
45          operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
46          OnOperatorGraphChanged();
47          Prepare();
48        }
49      }
50    }
51
52    [Storable]
53    private IScope globalScope;
54    protected IScope GlobalScope {
55      get { return globalScope; }
56    }
57
58    [Storable]
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
73    public override TimeSpan ExecutionTime {
74      get { return engine.ExecutionTime; }
75    }
76
77    public override ResultCollection Results {
78      get {
79        return (ResultCollection)globalScope.Variables["Results"].Value;
80      }
81    }
82
83    protected EngineAlgorithm()
84      : base() {
85      globalScope = new Scope("Global Scope");
86      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
87      operatorGraph = new OperatorGraph();
88      Initialize();
89    }
90    protected EngineAlgorithm(string name)
91      : base(name) {
92      globalScope = new Scope("Global Scope");
93      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
94      operatorGraph = new OperatorGraph();
95      Initialize();
96    }
97    protected EngineAlgorithm(string name, ParameterCollection parameters)
98      : base(name, parameters) {
99      globalScope = new Scope("Global Scope");
100      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
101      operatorGraph = new OperatorGraph();
102      Initialize();
103    }
104    protected EngineAlgorithm(string name, string description)
105      : base(name, description) {
106      globalScope = new Scope("Global Scope");
107      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
108      operatorGraph = new OperatorGraph();
109      Initialize();
110    }
111    protected EngineAlgorithm(string name, string description, ParameterCollection parameters)
112      : base(name, description, parameters) {
113      globalScope = new Scope("Global Scope");
114      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
115      operatorGraph = new OperatorGraph();
116      Initialize();
117    }
118    [StorableConstructor]
119    protected EngineAlgorithm(bool deserializing) : base(deserializing) { }
120    [StorableHook(HookType.AfterDeserialization)]
121    private void AfterDeserialization() {
122      Initialize();
123
124      // BackwardsCompatibility3.3
125      #region Backwards compatible code (remove with 3.4)
126      // clear global scope if it contains any sub-scopes or additional variables
127      if ((ExecutionState == Core.ExecutionState.Stopped) && ((globalScope.SubScopes.Count > 0) || (globalScope.Variables.Count > 1))) {
128        ResultCollection results = Results;
129        globalScope.Clear();
130        globalScope.Variables.Add(new Variable("Results", results));
131      }
132      #endregion
133    }
134
135    protected EngineAlgorithm(EngineAlgorithm original, Cloner cloner)
136      : base(original, cloner) {
137      globalScope = cloner.Clone(original.globalScope);
138      engine = cloner.Clone(original.engine);
139      operatorGraph = cloner.Clone(original.operatorGraph);
140      Initialize();
141    }
142
143    private void Initialize() {
144      operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
145      if (engine == null) {
146        var types = ApplicationManager.Manager.GetTypes(typeof(IEngine));
147        Type t = types.FirstOrDefault(x => x.Name.Equals("SequentialEngine"));
148        if (t == null) t = types.FirstOrDefault();
149        if (t != null) engine = (IEngine)Activator.CreateInstance(t);
150      }
151      if (engine != null) RegisterEngineEvents();
152    }
153
154    public virtual IAlgorithm CreateUserDefinedAlgorithm() {
155      return new UserDefinedAlgorithm(this, new Cloner());
156    }
157
158    public override void Prepare() {
159      base.Prepare();
160      globalScope.Clear();
161      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
162
163      if ((engine != null) && (operatorGraph.InitialOperator != null)) {
164        ExecutionContext context = null;
165        if (Problem != null) {
166          foreach (var item in Problem.ExecutionContextItems)
167            context = new ExecutionContext(context, item, globalScope);
168        }
169        context = new ExecutionContext(context, this, globalScope);
170        context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
171        engine.Prepare(context);
172        engine.Log.Clear();
173      }
174    }
175    public override void Start(System.Threading.CancellationToken cancellationToken) {
176      base.Start(cancellationToken);
177      if (engine != null) engine.Start(cancellationToken);
178    }
179    public override void Pause() {
180      base.Pause();
181      if (engine != null) engine.Pause();
182    }
183    public override void Stop() {
184      base.Stop();
185      if (engine != null) engine.Stop();
186    }
187
188    #region Events
189    public event EventHandler EngineChanged;
190    protected virtual void OnEngineChanged() {
191      EventHandler handler = EngineChanged;
192      if (handler != null) handler(this, EventArgs.Empty);
193    }
194    public event EventHandler OperatorGraphChanged;
195    protected virtual void OnOperatorGraphChanged() {
196      EventHandler handler = OperatorGraphChanged;
197      if (handler != null) handler(this, EventArgs.Empty);
198    }
199
200    private void RegisterEngineEvents() {
201      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
202      Engine.Paused += new EventHandler(Engine_Paused);
203      Engine.Prepared += new EventHandler(Engine_Prepared);
204      Engine.Started += new EventHandler(Engine_Started);
205      Engine.Stopped += new EventHandler(Engine_Stopped);
206    }
207    private void DeregisterEngineEvents() {
208      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
209      Engine.Paused -= new EventHandler(Engine_Paused);
210      Engine.Prepared -= new EventHandler(Engine_Prepared);
211      Engine.Started -= new EventHandler(Engine_Started);
212      Engine.Stopped -= new EventHandler(Engine_Stopped);
213    }
214    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
215      OnExceptionOccurred(e.Value);
216    }
217    private void Engine_Paused(object sender, EventArgs e) {
218      OnPaused();
219    }
220    private void Engine_Prepared(object sender, EventArgs e) {
221      OnPrepared();
222    }
223    private void Engine_Started(object sender, EventArgs e) {
224      OnStarted();
225    }
226    private void Engine_Stopped(object sender, EventArgs e) {
227      ResultCollection results = Results;
228      globalScope.Clear();
229      globalScope.Variables.Add(new Variable("Results", results));
230      OnStopped();
231    }
232
233    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
234      Prepare();
235    }
236    #endregion
237  }
238}
Note: See TracBrowser for help on using the repository browser.