Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2900 was 2882, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • worked on algorithms
File size: 7.8 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 HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization {
29  /// <summary>
30  /// A base class for algorithms which use an engine for execution.
31  /// </summary>
32  [Item("EngineAlgorithm", "A base class for algorithms which use an engine for execution.")]
33  public abstract class EngineAlgorithm : Algorithm {
34    private OperatorGraph operatorGraph;
35    [Storable]
36    protected OperatorGraph OperatorGraph {
37      get { return operatorGraph; }
38      set {
39        if (value == null) throw new ArgumentNullException();
40        if (value != operatorGraph) {
41          if (operatorGraph != null) operatorGraph.InitialOperatorChanged -= new EventHandler(OperatorGraph_InitialOperatorChanged);
42          operatorGraph = value;
43          if (operatorGraph != null) operatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
44          OnOperatorGraphChanged();
45          Prepare();
46        }
47      }
48    }
49
50    [Storable]
51    private IScope globalScope;
52    protected IScope GlobalScope {
53      get { return globalScope; }
54    }
55
56    private IEngine engine;
57    [Storable]
58    public IEngine Engine {
59      get { return engine; }
60      set {
61        if (engine != value) {
62          if (engine != null) DeregisterEngineEvents();
63          engine = value;
64          if (engine != null) RegisterEngineEvents();
65          OnEngineChanged();
66          Prepare();
67        }
68      }
69    }
70
71    private ReadOnlyObservableKeyedCollection<string, IVariable> readOnlyResults;
72    public override IObservableKeyedCollection<string, IVariable> Results {
73      get {
74        if (readOnlyResults == null)
75          readOnlyResults = ((VariableCollection)globalScope.Variables["Results"].Value).AsReadOnly();
76        return readOnlyResults;
77      }
78    }
79
80    public override TimeSpan ExecutionTime {
81      get {
82        if (engine == null) return TimeSpan.Zero;
83        else return engine.ExecutionTime;
84      }
85    }
86
87    public override bool Finished {
88      get {
89        if (engine == null) return true;
90        else return engine.Finished;
91      }
92    }
93
94    protected EngineAlgorithm()
95      : base() {
96      globalScope = new Scope();
97      globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
98      OperatorGraph = new OperatorGraph();
99    }
100    protected EngineAlgorithm(string name)
101      : base(name) {
102      globalScope = new Scope();
103      globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
104      OperatorGraph = new OperatorGraph();
105    }
106    protected EngineAlgorithm(string name, ParameterCollection parameters)
107      : base(name, parameters) {
108      globalScope = new Scope();
109      globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
110      OperatorGraph = new OperatorGraph();
111    }
112    protected EngineAlgorithm(string name, string description)
113      : base(name, description) {
114      globalScope = new Scope();
115      globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
116      OperatorGraph = new OperatorGraph();
117    }
118    protected EngineAlgorithm(string name, string description, ParameterCollection parameters)
119      : base(name, description, parameters) {
120      globalScope = new Scope();
121      globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
122      OperatorGraph = new OperatorGraph();
123    }
124
125    public override IDeepCloneable Clone(Cloner cloner) {
126      EngineAlgorithm clone = (EngineAlgorithm)base.Clone(cloner);
127      clone.globalScope = (IScope)cloner.Clone(globalScope);
128      clone.Engine = (IEngine)cloner.Clone(engine);
129      clone.OperatorGraph = (OperatorGraph)cloner.Clone(operatorGraph);
130      return clone;
131    }
132
133    public UserDefinedAlgorithm CreateUserDefinedAlgorithm() {
134      UserDefinedAlgorithm algorithm = new UserDefinedAlgorithm(Name, Description);
135      Cloner cloner = new Cloner();
136      foreach (IParameter param in Parameters)
137        algorithm.Parameters.Add((IParameter)cloner.Clone(param));
138      algorithm.Problem = (IProblem)cloner.Clone(Problem);
139      algorithm.Engine = (IEngine)cloner.Clone(engine);
140      algorithm.OperatorGraph = (OperatorGraph)cloner.Clone(operatorGraph);
141      return algorithm;
142    }
143
144    protected override void OnCanceledChanged() {
145      if (Canceled && (engine != null))
146        engine.Stop();
147    }
148    protected override void OnPrepared() {
149      globalScope.Clear();
150      globalScope.Variables.Add(new Variable("Results", new VariableCollection()));
151      readOnlyResults = null;
152
153      if (engine != null) {
154        ExecutionContext context = null;
155        if (operatorGraph.InitialOperator != null) {
156          if (Problem != null) {
157            context = new ExecutionContext(context, Problem, globalScope);
158            foreach (IParameter param in Problem.Parameters)
159              param.ExecutionContext = context;
160          }
161          context = new ExecutionContext(context, this, globalScope);
162          foreach (IParameter param in this.Parameters)
163            param.ExecutionContext = context;
164          context = new ExecutionContext(context, operatorGraph.InitialOperator, globalScope);
165        }
166        engine.Prepare(context);
167      }
168      base.OnPrepared();
169    }
170    protected override void OnStarted() {
171      if (engine != null) engine.Start();
172      base.OnStarted();
173    }
174
175    protected virtual void OnOperatorGraphChanged() { }
176
177    public event EventHandler EngineChanged;
178    protected virtual void OnEngineChanged() {
179      if (EngineChanged != null)
180        EngineChanged(this, EventArgs.Empty);
181      OnChanged();
182    }
183
184    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
185      Prepare();
186    }
187    private void RegisterEngineEvents() {
188      Engine.Changed += new ChangedEventHandler(Engine_Changed);
189      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
190      Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
191      Engine.Stopped += new EventHandler(Engine_Stopped);
192    }
193    private void DeregisterEngineEvents() {
194      Engine.Changed -= new ChangedEventHandler(Engine_Changed);
195      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
196      Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
197      Engine.Stopped -= new EventHandler(Engine_Stopped);
198    }
199
200    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
201      OnExceptionOccurred(e.Value);
202    }
203    private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
204      OnExecutionTimeChanged();
205    }
206    private void Engine_Stopped(object sender, EventArgs e) {
207      OnStopped();
208    }
209    private void Engine_Changed(object sender, ChangedEventArgs e) {
210      OnChanged(e);
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.