Changeset 12316 for branches/SymbolicExpressionTreeEncoding
- Timestamp:
- 04/15/15 11:08:13 (10 years ago)
- Location:
- branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/EnumerableExtensions.cs
r12012 r12316 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Core; 25 using System;26 26 27 27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 28 public static class EnumerableExtensions { 29 public static T SelectRandom<T>(this IEnumerable<T> xs, IRandom random) { 28 29 //This class should not be used anymore. Use HeuristicLab.Random.RandomEnumberable instead 30 //This could not be fixed right now, because the algorithm behavior would be modified => version increment 31 internal static class EnumerableExtensions { 32 internal static T SelectRandom<T>(this IEnumerable<T> xs, IRandom random) { 30 33 var list = xs as IList<T>; 31 34 if (list != null) { … … 36 39 } 37 40 } 38 publicstatic T SelectRandom<T>(this IEnumerable<T> xs, IEnumerable<double> weights, IRandom random) {41 internal static T SelectRandom<T>(this IEnumerable<T> xs, IEnumerable<double> weights, IRandom random) { 39 42 var list = xs as IList<T>; 40 43 var weightsList = weights as IList<double>; -
branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeEncoding.cs
r12314 r12316 29 29 using HeuristicLab.Data; 30 30 using HeuristicLab.Optimization; 31 using HeuristicLab.Parameters; 31 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 33 using HeuristicLab.PluginInfrastructure; … … 72 73 } 73 74 } 75 76 [Storable] 77 private IValueParameter<ISymbolicExpressionGrammar> grammarParameter; 78 public IValueParameter<ISymbolicExpressionGrammar> GrammarParameter { 79 get { return grammarParameter; } 80 set { 81 if (value == null) throw new ArgumentNullException("Grammar parameter must not be null."); 82 if (value.Value == null) throw new ArgumentNullException("Grammar parameter value must not be null."); 83 if (grammarParameter == value) return; 84 85 if (grammarParameter != null) 86 Parameters.Remove(grammarParameter); 87 88 grammarParameter = value; 89 Parameters.Add(grammarParameter); 90 OnGrammarParameterChanged(); 91 } 92 } 93 94 95 [Storable] 96 private IFixedValueParameter<IntValue> functionDefinitionsParameter; 97 public IFixedValueParameter<IntValue> FunctionDefinitionsParameter { 98 get { return functionDefinitionsParameter; } 99 set { 100 if (value == null) throw new ArgumentNullException("Function definitions parameter must not be null."); 101 if (value.Value == null) throw new ArgumentNullException("Function definitions parameter value must not be null."); 102 if (functionDefinitionsParameter == value) return; 103 104 if (functionDefinitionsParameter != null) 105 Parameters.Remove(functionDefinitionsParameter); 106 107 functionDefinitionsParameter = value; 108 Parameters.Add(functionDefinitionsParameter); 109 OnFunctionDefinitionsParameterChanged(); 110 } 111 } 112 113 [Storable] 114 private IFixedValueParameter<IntValue> functionArgumentsParameter; 115 public IFixedValueParameter<IntValue> FunctionArgumentsParameter { 116 get { return functionArgumentsParameter; } 117 set { 118 if (value == null) throw new ArgumentNullException("Function arguments parameter must not be null."); 119 if (value.Value == null) throw new ArgumentNullException("Function arguments parameter value must not be null."); 120 if (functionArgumentsParameter == value) return; 121 122 if (functionArgumentsParameter != null) 123 Parameters.Remove(functionArgumentsParameter); 124 125 functionArgumentsParameter = value; 126 Parameters.Add(functionArgumentsParameter); 127 OnFunctionArgumentsParameterChanged(); 128 } 129 } 130 #endregion 131 132 #region Parameter properties 133 public int TreeLength { 134 get { return TreeLengthParameter.Value.Value; } 135 set { TreeLengthParameter.Value.Value = value; } 136 } 137 138 public int TreeDepth { 139 get { return TreeDepthParameter.Value.Value; } 140 set { TreeDepthParameter.Value.Value = value; } 141 } 142 143 public ISymbolicExpressionGrammar Grammar { 144 get { return GrammarParameter.Value; } 145 set { GrammarParameter.Value = value; } 146 } 147 148 public int FunctionDefinitions { 149 get { return FunctionDefinitionsParameter.Value.Value; } 150 set { FunctionDefinitionsParameter.Value.Value = value; } 151 } 152 153 public int FunctionArguments { 154 get { return FunctionArgumentsParameter.Value.Value; } 155 set { FunctionArgumentsParameter.Value.Value = value; } 156 } 74 157 #endregion 75 158 … … 78 161 public SymbolicExpressionTreeEncoding(bool deserializing) : base(deserializing) { } 79 162 public SymbolicExpressionTreeEncoding() : this("SymbolicExpressionTree") { } 80 public SymbolicExpressionTreeEncoding(string name) : base(name) { } 163 164 public SymbolicExpressionTreeEncoding(string name) 165 : base(name) { 166 treeLengthParameter = new FixedValueParameter<IntValue>("Maximium Tree Length", "Maximal length of the symbolic expression.", new IntValue(50)); 167 treeDepthParameter = new FixedValueParameter<IntValue>("Maximum Tree Depth", "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.", new IntValue(50)); 168 grammarParameter = new ValueParameter<ISymbolicExpressionGrammar>("Grammar", "The grammar that should be used for symbolic expression tree."); 169 functionDefinitionsParameter = new FixedValueParameter<IntValue>("Function Definitions", "Maximal number of automatically defined functions", new IntValue(0)); 170 functionArgumentsParameter = new FixedValueParameter<IntValue>("Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0)); 171 172 //TODO set default grammar 173 174 Parameters.Add(treeLengthParameter); 175 Parameters.Add(treeDepthParameter); 176 Parameters.Add(grammarParameter); 177 Parameters.Add(functionDefinitionsParameter); 178 Parameters.Add(functionArgumentsParameter); 179 180 SolutionCreator = new ProbabilisticTreeCreator(); 181 RegisterParameterEvents(); 182 DiscoverOperators(); 183 } 81 184 82 185 private SymbolicExpressionTreeEncoding(SymbolicExpressionTreeEncoding original, Cloner cloner) … … 84 187 TreeLengthParameter = cloner.Clone(original.TreeLengthParameter); 85 188 TreeDepthParameter = cloner.Clone(original.TreeDepthParameter); 189 GrammarParameter = cloner.Clone(original.GrammarParameter); 190 FunctionDefinitionsParameter = cloner.Clone(original.FunctionDefinitionsParameter); 191 FunctionArgumentsParameter = cloner.Clone(original.FunctionArgumentsParameter); 192 RegisterParameterEvents(); 86 193 } 87 194 … … 90 197 } 91 198 92 93 199 #region changed events 94 200 private void OnLengthParameterChanged() { 95 201 RegisterLengthParameterEvents(); 96 202 ConfigureOperators(Operators); 97 203 } 98 99 204 private void OnDepthParameterChanged() { 100 205 RegisterDepthParameterEvents(); 101 206 ConfigureOperators(Operators); 102 207 } 103 104 private void RegisterEvents() { 208 private void OnGrammarParameterChanged() { 209 RegisterGrammarParameterEvents(); 210 ConfigureOperators(Operators); 211 } 212 213 private void OnFunctionDefinitionsParameterChanged() { 214 RegisterFunctionDefinitionsParameterEvents(); 215 Grammar.MaximumFunctionDefinitions = FunctionDefinitions; 216 } 217 218 private void OnFunctionArgumentsParameterChanged() { 219 RegisterFunctionArgumentsParameterEvetns(); 220 Grammar.MaximumFunctionArguments = FunctionArguments; 221 } 222 223 private void RegisterParameterEvents() { 105 224 RegisterLengthParameterEvents(); 106 225 RegisterLengthParameterEvents(); 107 } 226 RegisterGrammarParameterEvents(); 227 RegisterFunctionDefinitionsParameterEvents(); 228 RegisterFunctionArgumentsParameterEvetns(); 229 } 230 108 231 109 232 private void RegisterLengthParameterEvents() { 110 TreeLengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);111 233 TreeLengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators); 112 234 } 113 235 114 236 private void RegisterDepthParameterEvents() { 115 TreeDepthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);116 237 TreeDepthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators); 117 238 } 239 private void RegisterGrammarParameterEvents() { 240 GrammarParameter.ValueChanged += (o, s) => ConfigureOperators(Operators); 241 GrammarParameter.Value.Changed += (o, s) => ConfigureOperators(Operators); 242 } 243 244 private void RegisterFunctionDefinitionsParameterEvents() { 245 FunctionDefinitionsParameter.Value.ValueChanged += (o, s) => Grammar.MaximumFunctionDefinitions = FunctionDefinitions; 246 } 247 private void RegisterFunctionArgumentsParameterEvetns() { 248 FunctionArgumentsParameter.Value.ValueChanged += (o, s) => Grammar.MaximumFunctionArguments = FunctionArguments; 249 } 250 #endregion 118 251 119 252 #region Operator discovery … … 125 258 typeof(ISymbolicExpressionTreeCreator), 126 259 typeof(ISymbolicExpressionTreeCrossover), 127 typeof(ISymbolicExpressionTreeManipulator) 260 typeof(ISymbolicExpressionTreeManipulator), 261 typeof(ISymbolicExpressionTreeArchitectureAlteringOperator), 262 typeof(ISymbolicExpressionTreeAnalyzer), 128 263 }; 129 264 } … … 144 279 145 280 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 281 ConfigureTreeOperators(operators.OfType<ISymbolicExpressionTreeOperator>()); 282 ConfigureSizeConstraintOperators(operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()); 283 ConfigureGrammarBaseOperators(operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()); 284 ConfigureArchitectureAlteringOperators(operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()); 285 286 ConfigureAnalyzers(operators.OfType<ISymbolicExpressionTreeAnalyzer>()); 146 287 ConfigureCreators(operators.OfType<ISymbolicExpressionTreeCreator>()); 147 288 ConfigureCrossovers(operators.OfType<ISymbolicExpressionTreeCrossover>()); … … 149 290 } 150 291 292 private void ConfigureTreeOperators(IEnumerable<ISymbolicExpressionTreeOperator> treeOperators) { 293 foreach (var treeOperator in treeOperators) { 294 treeOperator.SymbolicExpressionTreeParameter.ActualName = Name; 295 treeOperator.SymbolicExpressionTreeParameter.Hidden = true; 296 } 297 } 298 299 private void ConfigureSizeConstraintOperators(IEnumerable<ISymbolicExpressionTreeSizeConstraintOperator> sizeConstraintOperators) { 300 foreach (var sizeConstraintOperator in sizeConstraintOperators) { 301 sizeConstraintOperator.MaximumSymbolicExpressionTreeLengthParameter.ActualName = TreeLengthParameter.Name; 302 sizeConstraintOperator.MaximumSymbolicExpressionTreeDepthParameter.ActualName = TreeDepthParameter.Name; 303 } 304 } 305 306 private void ConfigureGrammarBaseOperators(IEnumerable<ISymbolicExpressionTreeGrammarBasedOperator> grammarBasedOperators) { 307 foreach (var grammarBasedOperator in grammarBasedOperators) { 308 grammarBasedOperator.SymbolicExpressionTreeGrammarParameter.ActualName = GrammarParameter.Name; 309 } 310 } 311 312 private void ConfigureArchitectureAlteringOperators(IEnumerable<ISymbolicExpressionTreeArchitectureAlteringOperator> architectureAlteringOperators) { 313 foreach (var architectureAlteringOperator in architectureAlteringOperators) { 314 architectureAlteringOperator.MaximumFunctionDefinitionsParameter.ActualName = FunctionDefinitionsParameter.Name; 315 architectureAlteringOperator.MaximumFunctionArgumentsParameter.ActualName = FunctionArgumentsParameter.Name; 316 } 317 318 } 319 320 private void ConfigureAnalyzers(IEnumerable<ISymbolicExpressionTreeAnalyzer> analyzers) { 321 foreach (var analyzer in analyzers) { 322 analyzer.SymbolicExpressionTreeParameter.ActualName = Name; 323 } 324 } 325 151 326 private void ConfigureCreators(IEnumerable<ISymbolicExpressionTreeCreator> creators) { 152 foreach (var creator in creators) { 153 creator.SymbolicExpressionTreeParameter.ActualName = Name; 154 // creator should have depth and length parameters? 155 } 327 //Empty interface 328 foreach (var creator in creators) { } 156 329 } 157 330 … … 159 332 foreach (var crossover in crossovers) { 160 333 crossover.ParentsParameter.ActualName = Name; 161 crossover.SymbolicExpressionTreeParameter.ActualName = Name;162 334 } 163 335 } 164 336 165 337 private void ConfigureManipulators(IEnumerable<ISymbolicExpressionTreeManipulator> manipulators) { 166 foreach (var manipulator in manipulators) { 167 manipulator.SymbolicExpressionTreeParameter.ActualName = Name; 168 manipulator.SymbolicExpressionTreeParameter.Hidden = true; 169 } 338 //Empty interface 339 foreach (var manipulator in manipulators) { } 170 340 } 171 341 #endregion
Note: See TracChangeset
for help on using the changeset viewer.