Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/EngineAlgorithm/EngineAlgorithmOperator.cs @ 16573

Last change on this file since 16573 was 16573, checked in by gkronber, 4 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

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