- Timestamp:
- 06/23/15 12:50:05 (9 years ago)
- Location:
- branches/GBT
- Files:
-
- 36 edited
- 3 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GBT
- Property svn:mergeinfo changed
/trunk/sources merged: 12392-12393,12397-12401,12422,12424,12428-12435,12442-12443,12445,12455-12458,12461,12463-12465,12470-12476,12478-12482,12485,12488,12490-12494 -
Property
svn:global-ignores
set to
*.nuget
packages
- Property svn:mergeinfo changed
-
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
-
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionTreeLengthAnalyzer.cs
r12012 r12495 51 51 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 52 52 } 53 54 public ILookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 55 get { return (ILookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 56 } 53 57 public ValueLookupParameter<DataTable> SymbolicExpressionTreeLengthsParameter { 54 58 get { return (ValueLookupParameter<DataTable>)Parameters[SymbolicExpressionTreeLengthsParameterName]; } … … 178 182 } 179 183 180 double maximumAllowedTreeLength = ((LookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]).ActualValue.Value;184 double maximumAllowedTreeLength = MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value; 181 185 182 186 treeLengthsTableRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram; -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.cs
r12012 r12495 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Random; 30 31 31 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 84 85 ISymbolicExpressionTree clonedTree = (ISymbolicExpressionTree)symbolicExpressionTree.Clone(); 85 86 86 var functionDefiningBranches = clonedTree.IterateNodesPrefix().OfType<DefunTreeNode>() ;87 if ( functionDefiningBranches.Count() == 0)87 var functionDefiningBranches = clonedTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList(); 88 if (!functionDefiningBranches.Any()) 88 89 // no function defining branch found => abort 89 90 return false; 90 91 91 92 // select a random function defining branch 92 var selectedDefunBranch = functionDefiningBranches.SelectRandom(random); 93 var selectedDefunBranch = functionDefiningBranches.SampleRandom(random); 94 93 95 var definedArguments = (from symbol in selectedDefunBranch.Grammar.Symbols.OfType<Argument>() 94 96 select symbol.ArgumentIndex).Distinct(); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDeleter.cs
r12012 r12495 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Random; 27 28 28 29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 54 55 int maxFunctionDefinitions, int maxFunctionArguments) { 55 56 56 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>() ;57 if ( functionDefiningBranches.Count() == 0)57 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList(); 58 if (!functionDefiningBranches.Any()) 58 59 // no function defining branch => abort 59 60 return false; 60 var selectedDefunBranch = functionDefiningBranches.SelectRandom(random); 61 62 var selectedDefunBranch = functionDefiningBranches.SampleRandom(random); 61 63 if (selectedDefunBranch.NumberOfArguments <= 1) 62 64 // argument deletion by consolidation is not possible => abort … … 77 79 78 80 // delete the dynamic argument symbol that matches the argument to be removed 79 var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>(). Where(s => s.ArgumentIndex == removedArgument).Single();81 var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().Single(s => s.ArgumentIndex == removedArgument); 80 82 selectedDefunBranch.Grammar.RemoveSymbol(matchingSymbol); 81 83 selectedDefunBranch.NumberOfArguments--; 82 84 // reduce arity in known functions of all root branches 83 85 foreach (var subtree in symbolicExpressionTree.Root.Subtrees) { 84 var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>(). Where(s => s.FunctionName == selectedDefunBranch.FunctionName).SingleOrDefault();86 var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().SingleOrDefault(s => s.FunctionName == selectedDefunBranch.FunctionName); 85 87 if (matchingInvokeSymbol != null) { 86 88 subtree.Grammar.SetSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments, selectedDefunBranch.NumberOfArguments); … … 99 101 select node; 100 102 foreach (var argNode in argNodes) { 101 var replacementSymbol = possibleArgumentSymbols.S electRandom(random);103 var replacementSymbol = possibleArgumentSymbols.SampleRandom(random); 102 104 argNode.Symbol = replacementSymbol; 103 105 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDuplicater.cs
r12012 r12495 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Random; 29 30 30 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 56 57 ISymbolicExpressionTree symbolicExpressionTree, 57 58 int maxFunctionDefinitions, int maxFunctionArguments) { 58 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>() ;59 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList(); 59 60 60 61 var allowedArgumentIndexes = Enumerable.Range(0, maxFunctionArguments); 61 if ( functionDefiningBranches.Count() == 0)62 if (!functionDefiningBranches.Any()) 62 63 // no function defining branches => abort 63 64 return false; 64 65 65 var selectedDefunBranch = functionDefiningBranches.SelectRandom(random); 66 var argumentSymbols = selectedDefunBranch.Grammar.Symbols.OfType<Argument>(); 67 if (argumentSymbols.Count() == 0 || argumentSymbols.Count() >= maxFunctionArguments) 66 var selectedDefunBranch = functionDefiningBranches.SampleRandom(random); 67 68 var argumentSymbols = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().ToList(); 69 if (!argumentSymbols.Any() || argumentSymbols.Count() >= maxFunctionArguments) 68 70 // when no argument or number of arguments is already at max allowed value => abort 69 71 return false; 70 var selectedArgumentSymbol = argumentSymbols.SelectRandom(random); 72 73 var selectedArgumentSymbol = argumentSymbols.SampleRandom(random); 71 74 var takenIndexes = argumentSymbols.Select(s => s.ArgumentIndex); 72 75 var newArgumentIndex = allowedArgumentIndexes.Except(takenIndexes).First(); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineCreater.cs
r12012 r12495 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Random; 31 32 32 33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 113 114 from subtree in parent.Subtrees 114 115 select new CutPoint(parent, subtree)).ToList(); 115 if ( allCutPoints.Count() == 0)116 if (!allCutPoints.Any()) 116 117 // no cut points => abort 117 118 return false; 118 119 string newFunctionName = allowedFunctionNames.Except(functionDefiningBranches.Select(x => x.FunctionName)).First(); 119 var selectedCutPoint = allCutPoints.SelectRandom(random); 120 var selectedCutPoint = allCutPoints.SampleRandom(random); 121 120 122 // select random branches as argument cut-off points (replaced by argument terminal nodes in the function) 121 123 List<CutPoint> argumentCutPoints = SelectRandomArgumentBranches(selectedCutPoint.Child, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDeleter.cs
r12012 r12495 26 26 using HeuristicLab.Data; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Random; 28 29 29 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 55 56 ISymbolicExpressionTree symbolicExpressionTree, 56 57 int maxFunctionDefinitions, int maxFunctionArguments) { 57 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>() ;58 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList(); 58 59 59 if ( functionDefiningBranches.Count() == 0)60 if (!functionDefiningBranches.Any()) 60 61 // no ADF to delete => abort 61 62 return false; 62 var selectedDefunBranch = functionDefiningBranches.SelectRandom(random); 63 64 var selectedDefunBranch = functionDefiningBranches.SampleRandom(random); 63 65 // remove the selected defun 64 66 int defunSubtreeIndex = symbolicExpressionTree.Root.IndexOfSubtree(selectedDefunBranch); … … 92 94 var allowedSymbolsList = invocationCutPoint.Parent.Grammar.GetAllowedChildSymbols(invocationCutPoint.Parent.Symbol, invocationCutPoint.ChildIndex).ToList(); 93 95 var weights = allowedSymbolsList.Select(s => s.InitialFrequency); 96 97 #pragma warning disable 612, 618 94 98 var selectedSymbol = allowedSymbolsList.SelectRandom(weights, random); 99 #pragma warning restore 612, 618 100 95 101 96 102 int minPossibleLength = invocationCutPoint.Parent.Grammar.GetMinimumExpressionLength(selectedSymbol); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDuplicater.cs
r12012 r12495 28 28 using HeuristicLab.Data; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Random; 30 31 31 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 59 60 ISymbolicExpressionTree symbolicExpressionTree, 60 61 int maxFunctionDefinitions, int maxFunctionArguments) { 61 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>() ;62 if ( functionDefiningBranches.Count() == 0|| functionDefiningBranches.Count() == maxFunctionDefinitions)62 var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList(); 63 if (!functionDefiningBranches.Any() || functionDefiningBranches.Count() == maxFunctionDefinitions) 63 64 // no function defining branches to duplicate or already reached the max number of ADFs 64 65 return false; … … 67 68 var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions) 68 69 select "ADF" + index.ToString(formatString); 69 var selectedBranch = functionDefiningBranches.SelectRandom(random); 70 71 var selectedBranch = functionDefiningBranches.SampleRandom(random); 70 72 var duplicatedDefunBranch = (DefunTreeNode)selectedBranch.Clone(); 71 73 string newFunctionName = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First(); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs
r12012 r12495 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 using HeuristicLab.Parameters;28 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 27 using HeuristicLab.PluginInfrastructure; … … 36 34 ISymbolicExpressionTreeSizeConstraintOperator, 37 35 ISymbolicExpressionTreeGrammarBasedOperator { 38 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";39 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";40 41 #region Parameter Properties42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }44 }45 46 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {47 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }48 }49 #endregion50 #region Properties51 public IntValue MaximumSymbolicExpressionTreeDepth {52 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }53 }54 55 public IntValue MaximumSymbolicExpressionTreeLength {56 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }57 }58 59 #endregion60 36 61 37 [StorableConstructor] … … 63 39 protected FullTreeCreator(FullTreeCreator original, Cloner cloner) : base(original, cloner) { } 64 40 65 public FullTreeCreator() 66 : base() { 67 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, 68 "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored).")); 69 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, 70 "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 71 } 41 public FullTreeCreator() : base() { } 72 42 73 43 public override IDeepCloneable Clone(Cloner cloner) { … … 77 47 78 48 protected override ISymbolicExpressionTree Create(IRandom random) { 79 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 49 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 50 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 80 51 } 81 52 … … 131 102 .ToList(); 132 103 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 104 105 #pragma warning disable 612, 618 133 106 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 107 #pragma warning restore 612, 618 108 134 109 var tree = selectedSymbol.CreateTreeNode(); 135 110 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 163 138 throw new InvalidOperationException("No symbols are available for the tree."); 164 139 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 140 141 #pragma warning disable 612, 618 165 142 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 143 #pragma warning restore 612, 618 144 166 145 var tree = selectedSymbol.CreateTreeNode(); 167 146 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 169 148 } 170 149 171 foreach (var subTree in root.Subtrees) 172 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0) 173 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 150 //additional levels should only be added if the maximum depth is not reached yet 151 if (maxDepth > currentDepth) { 152 foreach (var subTree in root.Subtrees) 153 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0) 154 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 155 } 174 156 } 175 157 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs
r12012 r12495 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 using HeuristicLab.Parameters;28 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 27 using HeuristicLab.PluginInfrastructure; … … 33 31 [StorableClass] 34 32 [Item("GrowTreeCreator", "An operator that creates new symbolic expression trees using the 'Grow' method")] 35 public class GrowTreeCreator : SymbolicExpressionTreeCreator, 36 ISymbolicExpressionTreeSizeConstraintOperator, 37 ISymbolicExpressionTreeGrammarBasedOperator { 38 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 39 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 40 41 #region Parameter Properties 42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 44 } 45 46 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 47 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 48 } 49 50 #endregion 51 #region Properties 52 public IntValue MaximumSymbolicExpressionTreeDepth { 53 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 54 } 55 56 public IntValue MaximumSymbolicExpressionTreeLength { 57 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 58 } 59 #endregion 60 33 public class GrowTreeCreator : SymbolicExpressionTreeCreator { 61 34 [StorableConstructor] 62 35 protected GrowTreeCreator(bool deserializing) : base(deserializing) { } 63 36 protected GrowTreeCreator(GrowTreeCreator original, Cloner cloner) : base(original, cloner) { } 64 37 65 public GrowTreeCreator() 66 : base() { 67 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, 68 "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored).")); 69 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, 70 "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 71 } 38 public GrowTreeCreator() : base() { } 72 39 73 40 public override IDeepCloneable Clone(Cloner cloner) { … … 78 45 protected override ISymbolicExpressionTree Create(IRandom random) { 79 46 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 80 MaximumSymbolicExpressionTreeLength .Value, MaximumSymbolicExpressionTreeDepth.Value);47 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 81 48 } 82 49 … … 122 89 throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero."); 123 90 124 var allowedSymbols = seedNode.Grammar.AllowedSymbols 125 .Where(s => s.InitialFrequency > 0.0) 126 .ToList(); 91 var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); 127 92 128 93 for (var i = 0; i < arity; i++) { 129 var possibleSymbols = allowedSymbols 130 .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)) 131 .ToList(); 94 var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList(); 132 95 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 96 97 #pragma warning disable 612, 618 133 98 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 99 #pragma warning restore 612, 618 100 134 101 var tree = selectedSymbol.CreateTreeNode(); 135 102 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 146 113 private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) { 147 114 var arity = SampleArity(random, root); 148 if (arity <= 0)149 throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");115 if (arity == 0) 116 return; 150 117 151 118 var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); 152 119 153 120 for (var i = 0; i < arity; i++) { 154 var possibleSymbols = allowedSymbols 155 .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) && 156 root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth) 157 .ToList(); 121 var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) && 122 root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth).ToList(); 158 123 159 124 if (!possibleSymbols.Any()) 160 125 throw new InvalidOperationException("No symbols are available for the tree."); 126 161 127 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 128 #pragma warning disable 612, 618 162 129 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 130 #pragma warning restore 612, 618 131 163 132 var tree = selectedSymbol.CreateTreeNode(); 164 133 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 166 135 } 167 136 168 foreach (var subTree in root.Subtrees) 169 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0) 170 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 137 if (maxDepth > currentDepth) 138 foreach (var subTree in root.Subtrees) 139 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0) 140 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 171 141 } 172 142 -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r12012 r12495 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Data;28 using HeuristicLab.Parameters;29 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 28 using HeuristicLab.PluginInfrastructure; … … 37 35 ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator { 38 36 private const int MAX_TRIES = 100; 39 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";40 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";41 #region Parameter Properties42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }44 }45 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {46 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }47 }48 #endregion49 #region Properties50 public IntValue MaximumSymbolicExpressionTreeLength {51 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }52 }53 public IntValue MaximumSymbolicExpressionTreeDepth {54 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }55 }56 #endregion57 37 58 38 [StorableConstructor] … … 61 41 public ProbabilisticTreeCreator() 62 42 : base() { 63 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 64 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 43 65 44 } 66 45 … … 71 50 72 51 protected override ISymbolicExpressionTree Create(IRandom random) { 73 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 52 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 53 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 74 54 } 75 55 … … 186 166 if (allowedSymbols.Count == 0) return false; 187 167 var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList(); 168 169 #pragma warning disable 612, 618 188 170 var selectedSymbol = allowedSymbols.SelectRandom(weights, random); 171 #pragma warning restore 612, 618 172 189 173 ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode(); 190 174 if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random); … … 232 216 select g).First().ToList(); 233 217 var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList(); 218 219 #pragma warning disable 612, 618 234 220 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 221 #pragma warning restore 612, 618 222 235 223 var tree = selectedSymbol.CreateTreeNode(); 236 224 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/RampedHalfAndHalfTreeCreator.cs
r12012 r12495 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 using HeuristicLab.Parameters;26 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 25 using HeuristicLab.PluginInfrastructure; … … 31 29 [StorableClass] 32 30 [Item("RampedHalfAndHalfTreeCreator", "An operator that creates new symbolic expression trees in an alternate way: half the trees are created usign the 'Grow' method while the other half are created using the 'Full' method")] 33 public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator, 34 ISymbolicExpressionTreeSizeConstraintOperator, 35 ISymbolicExpressionTreeGrammarBasedOperator { 36 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 37 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 38 39 #region Parameter Properties 40 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 41 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 42 } 43 44 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 45 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 46 } 47 48 #endregion 49 #region Properties 50 public IntValue MaximumSymbolicExpressionTreeDepth { 51 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 52 } 53 54 public IntValue MaximumSymbolicExpressionTreeLength { 55 get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; } 56 } 57 #endregion 58 31 public class RampedHalfAndHalfTreeCreator : SymbolicExpressionTreeCreator { 59 32 [StorableConstructor] 60 33 protected RampedHalfAndHalfTreeCreator(bool deserializing) : base(deserializing) { } 61 34 protected RampedHalfAndHalfTreeCreator(RampedHalfAndHalfTreeCreator original, Cloner cloner) : base(original, cloner) { } 62 35 63 public RampedHalfAndHalfTreeCreator() 64 : base() { 65 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, 66 "The maximal length (number of nodes) of the symbolic expression tree (this parameter is ignored).")); 67 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, 68 "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 69 } 36 public RampedHalfAndHalfTreeCreator() : base() { } 70 37 71 38 public override IDeepCloneable Clone(Cloner cloner) { … … 74 41 75 42 protected override ISymbolicExpressionTree Create(IRandom random) { 76 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 43 return Create(random, ClonedSymbolicExpressionTreeGrammarParameter.ActualValue, 44 MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value, MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value); 77 45 } 78 46 -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/SymbolicExpressionTreeCreator.cs
r12012 r12495 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data; 24 25 using HeuristicLab.Parameters; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 32 33 [StorableClass] 33 34 public abstract class SymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCreator { 34 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 35 private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength"; 36 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 37 35 38 private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar"; 36 39 private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar"; 37 40 38 41 #region Parameter Properties 39 public I LookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {40 get { return (I LookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { 43 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; } 41 44 } 42 45 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 46 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 47 } 43 48 public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter { 44 49 get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; } … … 55 60 protected SymbolicExpressionTreeCreator() 56 61 : base() { 57 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree that should be created.")); 58 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, 59 "The tree grammar that defines the correct syntax of symbolic expression trees that should be created.")); 60 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, 61 "An immutable clone of the concrete grammar that is actually used to create and manipulate trees.")); 62 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree.")); 63 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 64 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created.")); 65 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees.")); 62 66 } 63 67 … … 78 82 (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone())); 79 83 } 80 SymbolicExpressionTreeParameter.ActualValue = Create(Random );84 SymbolicExpressionTreeParameter.ActualValue = Create(RandomParameter.ActualValue); 81 85 return base.InstrumentedApply(); 82 86 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r12012 r12495 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Random; 30 31 31 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 181 182 select branch).ToList(); 182 183 if (allowedInternalBranches.Count > 0) { 183 return allowedInternalBranches.SelectRandom(random); 184 return allowedInternalBranches.SampleRandom(random); 185 184 186 } else { 185 187 // no internal nodes allowed => select leaf nodes … … 187 189 where branch == null || branch.SubtreeCount == 0 188 190 select branch).ToList(); 189 return allowedLeafBranches.S electRandom(random);191 return allowedLeafBranches.SampleRandom(random); 190 192 } 191 193 } else { … … 195 197 select branch).ToList(); 196 198 if (allowedLeafBranches.Count > 0) { 197 return allowedLeafBranches.S electRandom(random);199 return allowedLeafBranches.SampleRandom(random); 198 200 } else { 199 201 allowedInternalBranches = (from branch in branches 200 202 where branch != null && branch.SubtreeCount > 0 201 203 select branch).ToList(); 202 return allowedInternalBranches.SelectRandom(random); 204 return allowedInternalBranches.SampleRandom(random); 205 203 206 } 204 207 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SymbolicExpressionTreeCrossover.cs
r12012 r12495 34 34 public abstract class SymbolicExpressionTreeCrossover : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeCrossover { 35 35 private const string ParentsParameterName = "Parents"; 36 private const string ChildParameterName = "Child";37 36 #region Parameter Properties 38 37 public ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter { 39 38 get { return (ScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[ParentsParameterName]; } 40 39 } 41 public ILookupParameter<ISymbolicExpressionTree> ChildParameter {42 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[ChildParameterName]; }43 }44 40 #endregion 45 41 #region Properties 46 p ublicItemArray<ISymbolicExpressionTree> Parents {42 private ItemArray<ISymbolicExpressionTree> Parents { 47 43 get { return ParentsParameter.ActualValue; } 48 44 } 49 p ublicISymbolicExpressionTree Child {50 get { return ChildParameter.ActualValue; }51 set { ChildParameter.ActualValue = value; }45 private ISymbolicExpressionTree Child { 46 get { return SymbolicExpressionTreeParameter.ActualValue; } 47 set { SymbolicExpressionTreeParameter.ActualValue = value; } 52 48 } 53 49 #endregion … … 58 54 : base() { 59 55 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(ParentsParameterName, "The parent symbolic expression trees which should be crossed.")); 60 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(ChildParameterName, "The child symbolic expression tree resulting from the crossover."));61 56 ParentsParameter.ActualName = "SymbolicExpressionTree"; 62 ChildParameter.ActualName = "SymbolicExpressionTree";63 57 } 64 58 … … 67 61 throw new ArgumentException("Number of parents must be exactly two for symbolic expression tree crossover operators."); 68 62 69 ISymbolicExpressionTree result = Crossover(Random , Parents[0], Parents[1]);63 ISymbolicExpressionTree result = Crossover(RandomParameter.ActualValue, Parents[0], Parents[1]); 70 64 71 65 Child = result; … … 74 68 75 69 public abstract ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 70 71 72 [StorableHook(HookType.AfterDeserialization)] 73 private void AfterDeserialization() { 74 // BackwardsCompatibility3.4 75 #region Backwards compatible code, remove with 3.5 76 if (Parameters.ContainsKey("Child")) { 77 var oldChildParameter = (ILookupParameter<ISymbolicExpressionTree>)Parameters["Child"]; 78 Parameters.Remove("Child"); 79 SymbolicExpressionTreeParameter.ActualName = oldChildParameter.ActualName; 80 } 81 #endregion 82 } 76 83 } 77 84 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/EnumerableExtensions.cs
r12012 r12495 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 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 [Obsolete("This class will be removed in the future, because the functionality is provided in HeuristicLab.Random.RandomEnumerable.")] 28 32 public static class EnumerableExtensions { 33 [Obsolete("This method should not be used anymore. Use the extensions provided by HeuristicLab.Random.RandomEnumberable instead.")] 29 34 public static T SelectRandom<T>(this IEnumerable<T> xs, IRandom random) { 30 35 var list = xs as IList<T>; … … 36 41 } 37 42 } 43 [Obsolete("This method should not be used anymore. Use the extensions provided by HeuristicLab.Random.RandomEnumberable instead.")] 38 44 public static T SelectRandom<T>(this IEnumerable<T> xs, IEnumerable<double> weights, IRandom random) { 39 45 var list = xs as IList<T>; -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/EmptySymbolicExpressionTreeGrammar.cs
r12012 r12495 37 37 38 38 [StorableConstructor] 39 private EmptySymbolicExpressionTreeGrammar(bool deserializing) : base(deserializing) { }39 private EmptySymbolicExpressionTreeGrammar(bool deserializing) : base(deserializing) { } 40 40 internal EmptySymbolicExpressionTreeGrammar(ISymbolicExpressionGrammar grammar) 41 41 : base() { … … 70 70 } 71 71 72 IEnumerable<ISymbol> ISymbolicExpressionGrammarBase.GetAllowedChildSymbols(ISymbol parent) {72 public IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 73 73 return grammar.GetAllowedChildSymbols(parent); 74 74 } 75 75 76 IEnumerable<ISymbol> ISymbolicExpressionGrammarBase.GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {76 public IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) { 77 77 return grammar.GetAllowedChildSymbols(parent, argumentIndex); 78 78 } … … 85 85 } 86 86 87 int ISymbolicExpressionGrammarBase.GetMinimumExpressionDepth(ISymbol symbol) {87 public int GetMinimumExpressionDepth(ISymbol symbol) { 88 88 return grammar.GetMinimumExpressionDepth(symbol); 89 89 } 90 int ISymbolicExpressionGrammarBase.GetMaximumExpressionDepth(ISymbol symbol) {90 public int GetMaximumExpressionDepth(ISymbol symbol) { 91 91 return grammar.GetMaximumExpressionDepth(symbol); 92 92 } 93 int ISymbolicExpressionGrammarBase.GetMinimumExpressionLength(ISymbol symbol) {93 public int GetMinimumExpressionLength(ISymbol symbol) { 94 94 return grammar.GetMinimumExpressionLength(symbol); 95 95 } 96 int ISymbolicExpressionGrammarBase.GetMaximumExpressionLength(ISymbol symbol, int maxDepth) {96 public int GetMaximumExpressionLength(ISymbol symbol, int maxDepth) { 97 97 return grammar.GetMaximumExpressionLength(symbol, maxDepth); 98 98 } 99 99 100 public void AddSymbol(ISymbol symbol) { throw new NotSupportedException(); } 101 public void RemoveSymbol(ISymbol symbol) { throw new NotSupportedException(); } 102 public void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { throw new NotSupportedException(); } 103 public void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { throw new NotSupportedException(); } 104 public void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { throw new NotSupportedException(); } 105 public void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { throw new NotSupportedException(); } 106 public void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { throw new NotSupportedException(); } 107 100 108 101 109 #region ISymbolicExpressionTreeGrammar Members 102 IEnumerable<ISymbol> ISymbolicExpressionTreeGrammar.ModifyableSymbols {110 public IEnumerable<ISymbol> ModifyableSymbols { 103 111 get { return Enumerable.Empty<ISymbol>(); } 104 112 } 105 113 106 bool ISymbolicExpressionTreeGrammar.IsModifyableSymbol(ISymbol symbol) {114 public bool IsModifyableSymbol(ISymbol symbol) { 107 115 return false; 108 116 } 109 117 110 void ISymbolicExpressionTreeGrammar.AddSymbol(ISymbol symbol) { 111 throw new NotSupportedException(); 112 } 113 114 void ISymbolicExpressionTreeGrammar.RemoveSymbol(ISymbol symbol) { 115 throw new NotSupportedException(); 116 } 117 118 void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 119 throw new NotSupportedException(); 120 } 121 122 void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 123 throw new NotSupportedException(); 124 } 125 126 void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 127 throw new NotSupportedException(); 128 } 129 130 void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 131 throw new NotSupportedException(); 132 } 133 134 void ISymbolicExpressionTreeGrammar.SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 135 throw new NotSupportedException(); 136 } 137 138 #pragma warning disable 0067 //disable usage warning 118 #pragma warning disable 0067 //disable usage warning 139 119 public event EventHandler Changed; 140 120 #pragma warning restore 0067 141 121 #endregion 142 122 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionGrammar.cs
r12012 r12495 177 177 } 178 178 179 protected override sealed void AddSymbol(ISymbol symbol) { 179 public override sealed void AddSymbol(ISymbol symbol) { 180 if (ReadOnly) throw new InvalidOperationException(); 180 181 base.AddSymbol(symbol); 181 182 RegisterSymbolEvents(symbol); 182 183 OnChanged(); 183 184 } 184 protected override sealed void RemoveSymbol(ISymbol symbol) { 185 public override sealed void RemoveSymbol(ISymbol symbol) { 186 if (ReadOnly) throw new InvalidOperationException(); 185 187 DeregisterSymbolEvents(symbol); 186 188 base.RemoveSymbol(symbol); … … 204 206 #endregion 205 207 206 #region ISymbolicExpressionGrammar methods 207 void ISymbolicExpressionGrammar.AddSymbol(ISymbol symbol) { 208 if (ReadOnly) throw new InvalidOperationException(); 209 AddSymbol(symbol); 210 } 211 void ISymbolicExpressionGrammar.RemoveSymbol(ISymbol symbol) { 212 if (ReadOnly) throw new InvalidOperationException(); 213 RemoveSymbol(symbol); 214 } 215 216 void ISymbolicExpressionGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 208 public sealed override void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 217 209 if (ReadOnly) throw new InvalidOperationException(); 218 210 base.AddAllowedChildSymbol(parent, child); 219 211 } 220 void ISymbolicExpressionGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {212 public sealed override void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 221 213 if (ReadOnly) throw new InvalidOperationException(); 222 214 base.AddAllowedChildSymbol(parent, child, argumentIndex); 223 215 } 224 void ISymbolicExpressionGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {216 public sealed override void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 225 217 if (ReadOnly) throw new InvalidOperationException(); 226 218 base.RemoveAllowedChildSymbol(parent, child); 227 219 } 228 void ISymbolicExpressionGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {220 public sealed override void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 229 221 if (ReadOnly) throw new InvalidOperationException(); 230 222 base.RemoveAllowedChildSymbol(parent, child, argumentIndex); 231 223 } 232 224 233 void ISymbolicExpressionGrammar.SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {225 public sealed override void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 234 226 if (ReadOnly) throw new InvalidOperationException(); 235 227 base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount); … … 237 229 238 230 private bool suppressEvents = false; 239 void ISymbolicExpressionGrammar.StartGrammarManipulation() {231 public void StartGrammarManipulation() { 240 232 suppressEvents = true; 241 233 } 242 void ISymbolicExpressionGrammar.FinishedGrammarManipulation() {234 public void FinishedGrammarManipulation() { 243 235 suppressEvents = false; 244 236 OnChanged(); 245 237 } 246 238 247 protected override void OnChanged() {239 protected sealed override void OnChanged() { 248 240 if (suppressEvents) return; 249 241 base.OnChanged(); 250 242 } 251 #endregion252 243 253 244 #region symbol events … … 305 296 private void Symbol_NameChanged(object sender, EventArgs e) { 306 297 ISymbol symbol = (ISymbol)sender; 307 string oldName = symbols. Where(x => x.Value == symbol).First().Key;298 string oldName = symbols.First(x => x.Value == symbol).Key; 308 299 string newName = symbol.Name; 309 300 -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionGrammarBase.cs
r12012 r12495 41 41 private IEnumerable<ISymbol> StorableSymbols { 42 42 get { return symbols.Values.ToArray(); } 43 set { symbols = value.ToDictionary(sym => sym.Name); }43 set { foreach (var s in value) symbols.Add(s.Name, s); } 44 44 } 45 45 … … 47 47 private IEnumerable<KeyValuePair<ISymbol, Tuple<int, int>>> StorableSymbolSubtreeCount { 48 48 get { return symbolSubtreeCount.Select(x => new KeyValuePair<ISymbol, Tuple<int, int>>(GetSymbol(x.Key), x.Value)).ToArray(); } 49 set { symbolSubtreeCount = value.ToDictionary(x => x.Key.Name, x => x.Value); }49 set { foreach (var pair in value) symbolSubtreeCount.Add(pair.Key.Name, pair.Value); } 50 50 } 51 51 … … 53 53 private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols { 54 54 get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(GetSymbol).ToArray())).ToArray(); } 55 set { allowedChildSymbols = value.ToDictionary(x => x.Key.Name, x => x.Value.Select(y => y.Name).ToList()); }55 set { foreach (var pair in value) allowedChildSymbols.Add(pair.Key.Name, pair.Value.Select(y => y.Name).ToList()); } 56 56 } 57 57 58 58 [Storable(Name = "AllowedChildSymbolsPerIndex")] 59 59 private IEnumerable<KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>> StorableAllowedChildSymbolsPerIndex { 60 get { return allowedChildSymbolsPerIndex.Select(x => new KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>(Tuple.Create<ISymbol, int>(GetSymbol(x.Key.Item1), x.Key.Item2), x.Value.Select(y => GetSymbol(y)).ToArray())).ToArray(); } 61 set { allowedChildSymbolsPerIndex = value.ToDictionary(x => Tuple.Create(x.Key.Item1.Name, x.Key.Item2), x => x.Value.Select(y => y.Name).ToList()); } 60 get { return allowedChildSymbolsPerIndex.Select(x => new KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>(Tuple.Create(GetSymbol(x.Key.Item1), x.Key.Item2), x.Value.Select(GetSymbol).ToArray())).ToArray(); } 61 set { 62 foreach (var pair in value) 63 allowedChildSymbolsPerIndex.Add(Tuple.Create(pair.Key.Item1.Name, pair.Key.Item2), pair.Value.Select(y => y.Name).ToList()); 64 } 62 65 } 63 66 #endregion 64 67 65 68 private bool suppressEvents; 66 protected Dictionary<string, ISymbol> symbols;67 protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount;68 protected Dictionary<string, List<string>> allowedChildSymbols;69 protected Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex;69 protected readonly Dictionary<string, ISymbol> symbols; 70 protected readonly Dictionary<string, Tuple<int, int>> symbolSubtreeCount; 71 protected readonly Dictionary<string, List<string>> allowedChildSymbols; 72 protected readonly Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex; 70 73 71 74 public override bool CanChangeName { … … 87 90 cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>(); 88 91 92 symbols = new Dictionary<string, ISymbol>(); 93 symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(); 94 allowedChildSymbols = new Dictionary<string, List<string>>(); 95 allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>(); 96 89 97 suppressEvents = false; 90 98 } … … 133 141 134 142 #region protected grammar manipulation methods 135 p rotectedvirtual void AddSymbol(ISymbol symbol) {143 public virtual void AddSymbol(ISymbol symbol) { 136 144 if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined."); 137 145 foreach (var s in symbol.Flatten()) { … … 143 151 } 144 152 145 p rotectedvirtual void RemoveSymbol(ISymbol symbol) {153 public virtual void RemoveSymbol(ISymbol symbol) { 146 154 foreach (var s in symbol.Flatten()) { 147 155 symbols.Remove(s.Name); … … 175 183 } 176 184 177 p rotectedvoid AddAllowedChildSymbol(ISymbol parent, ISymbol child) {185 public virtual void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 178 186 bool changed = false; 179 187 … … 204 212 } 205 213 206 p rotectedvoid AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {214 public virtual void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 207 215 bool changed = false; 208 216 … … 238 246 } 239 247 240 p rotectedvoid RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {248 public virtual void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 241 249 bool changed = false; 242 250 List<string> childSymbols; … … 257 265 } 258 266 259 p rotectedvoid RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {267 public virtual void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 260 268 bool changed = false; 261 269 … … 282 290 } 283 291 284 p rotectedvoid SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {292 public virtual void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 285 293 var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol)); 286 294 if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); … … 308 316 } 309 317 public virtual IEnumerable<ISymbol> AllowedSymbols { 310 get { foreach (var s in Symbols) if (s.Enabled) yield return s; }318 get { return Symbols.Where(s => s.Enabled); } 311 319 } 312 320 public virtual bool ContainsSymbol(ISymbol symbol) { … … 510 518 if (suppressEvents) return; 511 519 var handler = Changed; 512 if (handler != null) Changed(this, EventArgs.Empty);520 if (handler != null) handler(this, EventArgs.Empty); 513 521 } 514 522 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionTreeGrammar.cs
r12012 r12495 55 55 } 56 56 } 57 public override IEnumerable<ISymbol> AllowedSymbols {58 get { return base.AllowedSymbols; }59 }60 57 public IEnumerable<ISymbol> ModifyableSymbols { 61 58 get { return base.symbols.Values; } … … 76 73 } 77 74 78 public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) { 79 return grammar.IsAllowedChildSymbol(parent, child) || base.IsAllowedChildSymbol(parent, child); 80 } 81 public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 82 return grammar.IsAllowedChildSymbol(parent, child, argumentIndex) || base.IsAllowedChildSymbol(parent, child, argumentIndex); 75 public override void RemoveSymbol(ISymbol symbol) { 76 if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException(); 77 base.RemoveSymbol(symbol); 83 78 } 84 79 … … 91 86 return base.GetMaximumSubtreeCount(symbol); 92 87 } 93 94 void ISymbolicExpressionTreeGrammar.AddSymbol(ISymbol symbol) { 95 base.AddSymbol(symbol); 96 } 97 void ISymbolicExpressionTreeGrammar.RemoveSymbol(ISymbol symbol) { 98 if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException(); 99 base.RemoveSymbol(symbol); 100 } 101 void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 102 base.AddAllowedChildSymbol(parent, child); 103 } 104 void ISymbolicExpressionTreeGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 105 base.AddAllowedChildSymbol(parent, child, argumentIndex); 106 } 107 void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 108 base.RemoveAllowedChildSymbol(parent, child); 109 } 110 void ISymbolicExpressionTreeGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 111 base.RemoveAllowedChildSymbol(parent, child, argumentIndex); 112 } 113 114 void ISymbolicExpressionTreeGrammar.SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 88 public override void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 115 89 if (!IsModifyableSymbol(symbol)) throw new InvalidOperationException(); 116 90 base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount); 117 91 } 118 92 119 int ISymbolicExpressionGrammarBase.GetMinimumExpressionDepth(ISymbol symbol) { 120 if (symbols.Count == 0) return grammar.GetMinimumExpressionDepth(symbol); 121 else return base.GetMinimumExpressionDepth(symbol); 93 public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) { 94 return grammar.IsAllowedChildSymbol(parent, child) || base.IsAllowedChildSymbol(parent, child); 122 95 } 123 int ISymbolicExpressionGrammarBase.GetMaximumExpressionDepth(ISymbol symbol) { 124 if (symbols.Count == 0) return grammar.GetMaximumExpressionDepth(symbol); 125 else return base.GetMaximumExpressionDepth(symbol); 96 public override bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 97 return grammar.IsAllowedChildSymbol(parent, child, argumentIndex) || base.IsAllowedChildSymbol(parent, child, argumentIndex); 126 98 } 127 int ISymbolicExpressionGrammarBase.GetMinimumExpressionLength(ISymbol symbol) {128 if (symbols.Count == 0) return grammar.GetMinimumExpressionLength(symbol);129 else return base.GetMinimumExpressionLength(symbol);130 }131 int ISymbolicExpressionGrammarBase.GetMaximumExpressionLength(ISymbol symbol, int maxDepth) {132 if (symbols.Count == 0) return grammar.GetMaximumExpressionLength(symbol, maxDepth);133 else return base.GetMaximumExpressionLength(symbol, maxDepth);134 }135 136 99 } 137 100 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj
r11623 r12495 143 143 <Compile Include="Formatters\SymbolicExpressionTreeHierarchicalFormatter.cs" /> 144 144 <Compile Include="Grammars\EmptySymbolicExpressionTreeGrammar.cs" /> 145 <Compile Include="Grammars\SimpleSymbolicExpressionGrammar.cs" /> 145 146 <Compile Include="Interfaces\IReadOnlySymbol.cs" /> 146 147 <Compile Include="Interfaces\ISymbolicExpressionGrammar.cs" /> … … 176 177 <Compile Include="Grammars\SymbolicExpressionGrammar.cs" /> 177 178 <Compile Include="Grammars\SymbolicExpressionTreeGrammar.cs" /> 179 <Compile Include="SymbolicExpressionTreeEncoding.cs" /> 178 180 <Compile Include="SymbolicExpressionTreeTopLevelNode.cs" /> 179 181 <Compile Include="Crossovers\SubtreeCrossover.cs"> … … 191 193 <Compile Include="Symbols\ArgumentTreeNode.cs" /> 192 194 <Compile Include="Symbols\GroupSymbol.cs" /> 195 <Compile Include="Symbols\SimpleSymbol.cs" /> 193 196 <Compile Include="Symbols\StartSymbol.cs" /> 194 197 <Compile Include="Symbols\InvokeFunction.cs" /> -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionGrammar.cs
r12012 r12495 36 36 event EventHandler ReadOnlyChanged; 37 37 38 void AddSymbol(ISymbol symbol);39 void RemoveSymbol(ISymbol symbol);40 41 void AddAllowedChildSymbol(ISymbol parent, ISymbol child);42 void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex);43 void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child);44 void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex);45 46 void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount);47 48 38 void StartGrammarManipulation(); 49 39 void FinishedGrammarManipulation(); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionGrammarBase.cs
r12012 r12495 27 27 public interface ISymbolicExpressionGrammarBase : INamedItem { 28 28 IEnumerable<ISymbol> Symbols { get; } 29 IEnumerable<ISymbol> AllowedSymbols { get; } 30 31 bool ContainsSymbol(ISymbol symbol); 29 32 ISymbol GetSymbol(string symbolName); 30 IEnumerable<ISymbol> AllowedSymbols { get; } 31 bool ContainsSymbol(ISymbol symbol); 33 34 void AddSymbol(ISymbol symbol); 35 void RemoveSymbol(ISymbol symbol); 32 36 33 37 bool IsAllowedChildSymbol(ISymbol parent, ISymbol child); … … 36 40 IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex); 37 41 42 void AddAllowedChildSymbol(ISymbol parent, ISymbol child); 43 void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex); 44 void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child); 45 void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex); 46 47 38 48 int GetMinimumSubtreeCount(ISymbol symbol); 39 49 int GetMaximumSubtreeCount(ISymbol symbol); 50 void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount); 40 51 41 52 int GetMinimumExpressionDepth(ISymbol start); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeGrammar.cs
r12012 r12495 23 23 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 24 24 public interface ISymbolicExpressionTreeGrammar : ISymbolicExpressionGrammarBase { 25 26 25 IEnumerable<ISymbol> ModifyableSymbols { get; } 27 26 bool IsModifyableSymbol(ISymbol symbol); 28 void AddSymbol(ISymbol symbol);29 void RemoveSymbol(ISymbol symbol);30 31 void AddAllowedChildSymbol(ISymbol parent, ISymbol child);32 void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex);33 void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child);34 void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex);35 36 void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount);37 27 } 38 28 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/Operators/ISymbolicExpressionTreeCreator.cs
r12012 r12495 27 27 /// Interface for operators that create symbolic expression trees. 28 28 /// </summary> 29 public interface ISymbolicExpressionTreeCreator : ISymbolicExpressionTreeOperator, ISolutionCreator { 30 ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { get; } 29 public interface ISymbolicExpressionTreeCreator : ISolutionCreator, ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator { 31 30 ISymbolicExpressionTree CreateTree(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth); 32 31 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/Operators/ISymbolicExpressionTreeCrossover.cs
r12012 r12495 29 29 public interface ISymbolicExpressionTreeCrossover : ISymbolicExpressionTreeOperator, ICrossover { 30 30 ILookupParameter<ItemArray<ISymbolicExpressionTree>> ParentsParameter { get; } 31 ILookupParameter<ISymbolicExpressionTree> ChildParameter { get; }32 31 ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1); 33 32 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/Operators/ISymbolicExpressionTreeManipulator.cs
r12012 r12495 28 28 /// </summary> 29 29 public interface ISymbolicExpressionTreeManipulator : ISymbolicExpressionTreeOperator, IManipulator { 30 ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { get; }31 30 } 32 31 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/Operators/ISymbolicExpressionTreeOperator.cs
r12012 r12495 27 27 /// </summary> 28 28 public interface ISymbolicExpressionTreeOperator : IOperator { 29 ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { get; } 29 30 } 30 31 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs
r12012 r12495 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using System.Collections.Generic;27 27 28 28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 53 53 int tries = 0; 54 54 do { 55 56 #pragma warning disable 612, 618 55 57 parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random); 58 #pragma warning restore 612, 618 59 56 60 childIndex = random.Next(parent.SubtreeCount); 57 61 … … 81 85 if (tries < MAX_TRIES) { 82 86 var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); 87 #pragma warning disable 612, 618 83 88 var newSymbol = allowedSymbols.SelectRandom(weights, random); 89 #pragma warning restore 612, 618 84 90 85 91 // replace the old node with the new node -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/OnePointShaker.cs
r12012 r12495 20 20 #endregion 21 21 22 using System. Linq;22 using System.Collections.Generic; 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Parameters; 28 using System.Collections.Generic; 28 using HeuristicLab.Random; 29 29 30 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 66 66 }); 67 67 if (parametricNodes.Count > 0) { 68 var selectedPoint = parametricNodes.S electRandom(random);68 var selectedPoint = parametricNodes.SampleRandom(random); 69 69 selectedPoint.ShakeLocalParameters(random, shakingFactor); 70 70 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/RemoveBranchManipulation.cs
r12012 r12495 27 27 using HeuristicLab.Parameters; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Random; 29 30 30 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 80 81 var nodes = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).ToList(); 81 82 do { 82 parent = nodes.SelectRandom(random); 83 parent = nodes.SampleRandom(random); 84 83 85 childIndex = random.Next(parent.SubtreeCount); 84 86 var child = parent.GetSubtree(childIndex); … … 111 113 select g).First().ToList(); 112 114 var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList(); 115 116 #pragma warning disable 612, 618 113 117 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 118 #pragma warning restore 612, 618 119 114 120 var newTreeNode = selectedSymbol.CreateTreeNode(); 115 121 if (newTreeNode.HasLocalParameters) newTreeNode.ResetLocalParameters(random); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs
r12012 r12495 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 26 27 using HeuristicLab.Parameters; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using System.Collections.Generic;29 29 30 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 78 78 int tries = 0; 79 79 do { 80 #pragma warning disable 612, 618 80 81 parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random); 82 #pragma warning restore 612, 618 83 81 84 childIndex = random.Next(parent.SubtreeCount); 82 85 var child = parent.GetSubtree(childIndex); … … 99 102 if (tries < MAX_TRIES) { 100 103 var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); 104 #pragma warning disable 612, 618 101 105 var seedSymbol = allowedSymbols.SelectRandom(weights, random); 106 #pragma warning restore 612, 618 107 102 108 // replace the old node with the new node 103 109 var seedNode = seedSymbol.CreateTreeNode(); -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/SymbolicExpressionTreeManipulator.cs
r12012 r12495 32 32 [StorableClass] 33 33 public abstract class SymbolicExpressionTreeManipulator : SymbolicExpressionTreeOperator, ISymbolicExpressionTreeManipulator { 34 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";35 36 #region Parameter Properties37 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {38 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }39 }40 #endregion41 42 #region Properties43 public ISymbolicExpressionTree SymbolicExpressionTree {44 get { return SymbolicExpressionTreeParameter.ActualValue; }45 }46 #endregion47 48 34 [StorableConstructor] 49 35 protected SymbolicExpressionTreeManipulator(bool deserializing) : base(deserializing) { } … … 51 37 public SymbolicExpressionTreeManipulator() 52 38 : base() { 53 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied."));39 54 40 } 55 41 -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Plugin.cs.frame
r12012 r12495 38 38 [PluginDependency("HeuristicLab.Parameters", "3.3")] 39 39 [PluginDependency("HeuristicLab.Persistence", "3.3")] 40 [PluginDependency("HeuristicLab.Random", "3.3")] 40 41 public class HeuristicLabEncodingsSymbolicExpressionTreeEncodingPlugin : PluginBase { 41 42 } -
branches/GBT/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeOperator.cs
r12012 r12495 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Operators; 26 25 using HeuristicLab.Optimization; … … 36 35 public abstract class SymbolicExpressionTreeOperator : InstrumentedOperator, IStochasticOperator, ISymbolicExpressionTreeOperator { 37 36 private const string RandomParameterName = "Random"; 37 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 38 38 39 39 public override bool CanChangeName { … … 45 45 get { return (LookupParameter<IRandom>)Parameters[RandomParameterName]; } 46 46 } 47 #endregion 48 49 #region Properties 50 public IRandom Random { 51 get { return RandomParameter.ActualValue; } 47 public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 48 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 52 49 } 53 50 #endregion … … 59 56 : base() { 60 57 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The pseudo random number generator which should be used for symbolic expression tree operators.")); 58 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 59 } 60 61 [StorableHook(HookType.AfterDeserialization)] 62 private void AfterDeserialization() { 63 // BackwardsCompatibility3.3 64 #region Backwards compatible code, remove with 3.4 65 if (!Parameters.ContainsKey(SymbolicExpressionTreeParameterName)) 66 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree on which the operator should be applied.")); 67 #endregion 61 68 } 62 69 }
Note: See TracChangeset
for help on using the changeset viewer.