Changeset 16916
- Timestamp:
- 05/07/19 22:48:10 (6 years ago)
- Location:
- addons/HeuristicLab.FitnessLandscapeAnalysis
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.sln
r15733 r16916 7 7 EndProject 8 8 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Analysis.FitnessLandscape.Views", "HeuristicLab.Analysis.FitnessLandscape.Views\HeuristicLab.Analysis.FitnessLandscape.Views.csproj", "{09F27845-A5B0-476E-901E-9D61576C8581}" 9 EndProject 10 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6918D699-500C-43AB-8080-C8982A23AB21}" 11 ProjectSection(SolutionItems) = preProject 12 .editorconfig = .editorconfig 13 EndProjectSection 9 14 EndProject 10 15 Global … … 46 51 HideSolutionNode = FALSE 47 52 EndGlobalSection 53 GlobalSection(ExtensibilityGlobals) = postSolution 54 SolutionGuid = {57F4192E-DC9D-4075-867D-E68177A1B8FD} 55 EndGlobalSection 48 56 EndGlobal -
addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/EngineAlgorithm/EngineAlgorithmOperator.cs
r16877 r16916 1 using HEAL.Attic; 1 using System; 2 using HEAL.Attic; 2 3 using HeuristicLab.Collections; 3 4 using HeuristicLab.Common; … … 5 6 using HeuristicLab.Optimization; 6 7 using HeuristicLab.Parameters; 7 using System;8 8 9 namespace HeuristicLab.Operators 10 { 9 namespace HeuristicLab.Operators { 11 10 12 [Item("EngineAlgorithmOperator", "An operator the encapsulates a complete algorithm.")] 13 [StorableType("8BEF3D75-7DA3-4683-95C5-AF7B6AC0A810")] 14 public class EngineAlgorithmOperator : AlgorithmOperator 15 { 11 [Item("EngineAlgorithmOperator", "An operator the encapsulates a complete algorithm.")] 12 [StorableType("8BEF3D75-7DA3-4683-95C5-AF7B6AC0A810")] 13 public class EngineAlgorithmOperator : AlgorithmOperator { 16 14 17 18 15 [Storable] 16 private OperatorGraph algorithmOperatorGraph; 19 17 20 [Storable] 21 protected EngineAlgorithm algorithm; 22 public EngineAlgorithm Algorithm 23 { 24 get { return algorithm; } 25 set 26 { 27 if (value == algorithm) 28 return; 29 if (algorithm != null) 30 { 31 DeregisterAlgorithmEvents(); 32 if (algorithmOperatorGraph != null) 33 DeregisterOperatorGraphEvents(); 34 OperatorGraph.InitialOperator = null; 35 OperatorGraph.Operators.Clear(); 36 } 37 algorithm = value; 38 OnAlgorithmChanged(); 39 if (algorithm != null) 40 { 41 foreach (IOperator op in algorithm.OperatorGraph.Operators) 42 OperatorGraph.Operators.Add(op); 43 OperatorGraph.InitialOperator = algorithm.OperatorGraph.InitialOperator; 44 RegisterAlgorithmEvents(); 45 if (algorithm.OperatorGraph != null) 46 { 47 algorithmOperatorGraph = algorithm.OperatorGraph; 48 RegisterOperatorGraphEvents(); 49 } 50 } 51 } 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(); 52 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 } 53 46 54 55 47 #region Events 48 public event EventHandler AlgorithmChanged; 56 49 57 protected virtual void OnAlgorithmChanged() 58 { 59 UpdateParameters(); 60 EventHandler handler = AlgorithmChanged; 61 if (handler != null) 62 handler(this, EventArgs.Empty); 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 private void algorithm_ProblemChanged(object sender, EventArgs e) { 63 UpdateParameters(); 64 } 65 66 private void DeregisterAlgorithmEvents() { 67 algorithm.OperatorGraphChanged -= algorithm_OperatorGraphChanged; 68 } 69 70 private 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 private 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 private 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)); 63 117 } 118 } 119 Parameters.Add(new ValueLookupParameter<NamedItemCollection<IResult>>("Results", "Results collection as passed through to the inner algorithn", "Results") { GetsCollected = false }); 120 } 64 121 65 private void RegisterAlgorithmEvents() 66 { 67 algorithm.OperatorGraphChanged += algorithm_OperatorGraphChanged; 68 algorithm.ProblemChanged += new EventHandler(algorithm_ProblemChanged); 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(StorableConstructorFlag _) : base(_) { } 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; 69 154 } 155 } 156 return base.Apply(); 157 } 70 158 71 private void algorithm_ProblemChanged(object sender, EventArgs e) 72 { 73 UpdateParameters(); 74 } 75 76 private void DeregisterAlgorithmEvents() 77 { 78 algorithm.OperatorGraphChanged -= algorithm_OperatorGraphChanged; 79 } 80 81 private void algorithm_OperatorGraphChanged(object sender, EventArgs e) 82 { 83 if (algorithmOperatorGraph != null) 84 DeregisterOperatorGraphEvents(); 85 algorithmOperatorGraph = null; 86 if (algorithm.OperatorGraph != null) 87 { 88 algorithmOperatorGraph = algorithm.OperatorGraph; 89 RegisterOperatorGraphEvents(); 90 } 91 } 92 93 private void RegisterOperatorGraphEvents() 94 { 95 algorithmOperatorGraph.InitialOperatorChanged += algorithmOperatorGraph_InitialOperatorChanged; 96 algorithmOperatorGraph.Operators.CollectionReset += algorithmOperatorGraph_Operators_Changed; 97 algorithmOperatorGraph.Operators.ItemsAdded += algorithmOperatorGraph_Operators_Changed; 98 algorithmOperatorGraph.Operators.ItemsRemoved += algorithmOperatorGraph_Operators_Changed; 99 } 100 101 private void DeregisterOperatorGraphEvents() 102 { 103 algorithmOperatorGraph.InitialOperatorChanged -= algorithmOperatorGraph_InitialOperatorChanged; 104 algorithmOperatorGraph.Operators.CollectionReset -= algorithmOperatorGraph_Operators_Changed; 105 algorithmOperatorGraph.Operators.ItemsAdded -= algorithmOperatorGraph_Operators_Changed; 106 algorithmOperatorGraph.Operators.ItemsRemoved -= algorithmOperatorGraph_Operators_Changed; 107 } 108 109 private void algorithmOperatorGraph_Operators_Changed(object sender, CollectionItemsChangedEventArgs<IOperator> e) 110 { 111 OperatorGraph.Operators.Clear(); 112 foreach (IOperator op in algorithmOperatorGraph.Operators) 113 { 114 OperatorGraph.Operators.Add(op); 115 } 116 } 117 118 private void algorithmOperatorGraph_InitialOperatorChanged(object sender, EventArgs e) 119 { 120 OperatorGraph.InitialOperator = algorithmOperatorGraph.InitialOperator; 121 } 122 123 private void UpdateParameters() 124 { 125 // TODO: match previously set parameter values 126 IOperator successor = Successor; 127 Parameters.Clear(); 128 Parameters.Add(new OperatorParameter("Successor", successor)); 129 var alg = Algorithm as IParameterizedItem; 130 foreach (var param in alg.Parameters) 131 { 132 Parameters.Add(WrapParameter(param)); 133 } 134 if (Algorithm.Problem != null) 135 { 136 foreach (var param in Algorithm.Problem.Parameters) 137 { 138 Parameters.Add(WrapParameter(param)); 139 } 140 } 141 Parameters.Add(new ValueLookupParameter<NamedItemCollection<IResult>>("Results", "Results collection as passed through to the inner algorithn", "Results") { GetsCollected = false }); 142 } 143 144 private static IValueLookupParameter WrapParameter(IParameter param) 145 { 146 IValueLookupParameter v = (IValueLookupParameter)Activator.CreateInstance(typeof(ValueLookupParameter<>).MakeGenericType(param.DataType), new object[] { param.Name, param.Description }); 147 v.ActualName = param.Name; 148 IValueParameter valueParam = param as IValueParameter; 149 if (valueParam != null) 150 v.Value = valueParam.Value; 151 return v; 152 } 153 #endregion 154 155 #region Construction & Cloning 156 [StorableConstructor] 157 protected EngineAlgorithmOperator(StorableConstructorFlag _) : base(_) { } 158 protected EngineAlgorithmOperator(EngineAlgorithmOperator original, Cloner cloner) 159 : base(original, cloner) 160 { 161 this.algorithm = cloner.Clone(original.algorithm); 162 this.algorithmOperatorGraph = cloner.Clone(original.algorithmOperatorGraph); 163 } 164 public EngineAlgorithmOperator() { } 165 public override IDeepCloneable Clone(Cloner cloner) 166 { 167 return new EngineAlgorithmOperator(this, cloner); 168 } 169 #endregion 170 171 public override IOperation Apply() 172 { 173 var alg = Algorithm as IParameterizedItem; 174 foreach (var param in alg.Parameters) 175 { 176 param.ActualValue = Parameters[param.Name].ActualValue; 177 } 178 if (Algorithm.Problem != null) 179 { 180 foreach (var param in Algorithm.Problem.Parameters) 181 { 182 param.ActualValue = Parameters[param.Name].ActualValue; 183 } 184 } 185 return base.Apply(); 186 } 187 188 } 159 } 189 160 }
Note: See TracChangeset
for help on using the changeset viewer.