Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/EngineAlgorithm/EngineAlgorithmOperator.cs @ 10306

Last change on this file since 10306 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 6.1 KB
Line 
1using System;
2using HeuristicLab.Collections;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Optimization;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Operators {
10
11  [Item("EngineAlgorithmOperator", "An operator the encapsulates a complete algorithm.")]
12  [StorableClass]
13  public class EngineAlgorithmOperator : AlgorithmOperator {
14
15    [Storable]
16    private OperatorGraph algorithmOperatorGraph;
17
18    [Storable]
19    protected EngineAlgorithm algorithm;
20    public EngineAlgorithm Algorithm {
21      get { return algorithm; }
22      set {
23        if (value == algorithm)
24          return;
25        if (algorithm != null) {
26          DeregisterAlgorithmEvents();
27          if (algorithmOperatorGraph != null)
28            DeregisterOperatorGraphEvents();
29          OperatorGraph.InitialOperator = null;
30          OperatorGraph.Operators.Clear();
31        }
32        algorithm = value;
33        OnAlgorithmChanged();
34        if (algorithm != null) {
35          foreach (IOperator op in algorithm.OperatorGraph.Operators)
36            OperatorGraph.Operators.Add(op);
37          OperatorGraph.InitialOperator = algorithm.OperatorGraph.InitialOperator;
38          RegisterAlgorithmEvents();
39          if (algorithm.OperatorGraph != null) {
40            algorithmOperatorGraph = algorithm.OperatorGraph;
41            RegisterOperatorGraphEvents();
42          }
43        }
44      }
45    }
46
47    #region Events
48    public event EventHandler AlgorithmChanged;
49
50    protected virtual void OnAlgorithmChanged() {
51      UpdateParameters();
52      EventHandler handler = AlgorithmChanged;
53      if (handler != null)
54        handler(this, EventArgs.Empty);
55    }
56
57    private void RegisterAlgorithmEvents() {
58      algorithm.OperatorGraphChanged += algorithm_OperatorGraphChanged;
59      algorithm.ProblemChanged +=new EventHandler(algorithm_ProblemChanged);
60    }
61
62    void algorithm_ProblemChanged(object sender, EventArgs e) {
63      UpdateParameters();
64    }
65
66    private void DeregisterAlgorithmEvents() {
67      algorithm.OperatorGraphChanged -= algorithm_OperatorGraphChanged;
68    }
69
70    void algorithm_OperatorGraphChanged(object sender, EventArgs e) {
71      if (algorithmOperatorGraph != null)
72        DeregisterOperatorGraphEvents();
73      algorithmOperatorGraph = null;
74      if (algorithm.OperatorGraph != null) {
75        algorithmOperatorGraph = algorithm.OperatorGraph;
76        RegisterOperatorGraphEvents();
77      }
78    }
79
80    private void RegisterOperatorGraphEvents() {
81      algorithmOperatorGraph.InitialOperatorChanged += algorithmOperatorGraph_InitialOperatorChanged;
82      algorithmOperatorGraph.Operators.CollectionReset += algorithmOperatorGraph_Operators_Changed;
83      algorithmOperatorGraph.Operators.ItemsAdded += algorithmOperatorGraph_Operators_Changed;
84      algorithmOperatorGraph.Operators.ItemsRemoved += algorithmOperatorGraph_Operators_Changed;
85    }
86
87    private void DeregisterOperatorGraphEvents() {
88      algorithmOperatorGraph.InitialOperatorChanged -= algorithmOperatorGraph_InitialOperatorChanged;
89      algorithmOperatorGraph.Operators.CollectionReset -= algorithmOperatorGraph_Operators_Changed;
90      algorithmOperatorGraph.Operators.ItemsAdded -= algorithmOperatorGraph_Operators_Changed;
91      algorithmOperatorGraph.Operators.ItemsRemoved -= algorithmOperatorGraph_Operators_Changed;
92    }
93
94    void algorithmOperatorGraph_Operators_Changed(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
95      OperatorGraph.Operators.Clear();
96      foreach (IOperator op in algorithmOperatorGraph.Operators) {
97        OperatorGraph.Operators.Add(op);
98      }
99    }
100
101    void algorithmOperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
102      OperatorGraph.InitialOperator = algorithmOperatorGraph.InitialOperator;
103    }
104
105    private void UpdateParameters() {
106      // TODO: match previously set parameter values
107      IOperator successor = Successor;
108      Parameters.Clear();
109      Parameters.Add(new OperatorParameter("Successor", successor));
110      var alg = Algorithm as IParameterizedItem;
111      foreach (var param in alg.Parameters) {
112        Parameters.Add(WrapParameter(param));
113      }
114      if (Algorithm.Problem != null) {
115        foreach (var param in Algorithm.Problem.Parameters) {
116          Parameters.Add(WrapParameter(param));
117        }
118      }
119      Parameters.Add(new ValueLookupParameter<NamedItemCollection<IResult>>("Results", "Results collection as passed through to the inner algorithn", "Results", false));
120    }
121
122    private static IValueLookupParameter WrapParameter(IParameter param) {
123      IValueLookupParameter v = (IValueLookupParameter)Activator.CreateInstance(typeof(ValueLookupParameter<>).MakeGenericType(param.DataType), new object[] { param.Name, param.Description });
124      v.ActualName = param.Name;
125      IValueParameter valueParam = param as IValueParameter;
126      if (valueParam != null)
127        v.Value = valueParam.Value;
128      return v;
129    }
130    #endregion
131
132    #region Construction & Cloning
133    [StorableConstructor]
134    protected EngineAlgorithmOperator(bool deserializing) : base(deserializing) { }
135    protected EngineAlgorithmOperator(EngineAlgorithmOperator original, Cloner cloner)
136      : base(original, cloner) {
137      this.algorithm = cloner.Clone(original.algorithm);
138      this.algorithmOperatorGraph = cloner.Clone(original.algorithmOperatorGraph);
139    }
140    public EngineAlgorithmOperator() { }
141    public override IDeepCloneable Clone(Cloner cloner) {
142      return new EngineAlgorithmOperator(this, cloner);
143    }
144    #endregion
145
146    public override IOperation Apply() {
147      var alg = Algorithm as IParameterizedItem;
148      foreach (var param in alg.Parameters) {
149        param.ActualValue = Parameters[param.Name].ActualValue;
150      }
151      if (Algorithm.Problem != null) {
152        foreach (var param in Algorithm.Problem.Parameters) {
153          param.ActualValue = Parameters[param.Name].ActualValue;
154        }
155      }
156      return base.Apply();
157    }
158
159  }
160}
Note: See TracBrowser for help on using the repository browser.