Changeset 12420 for branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Timestamp:
- 06/10/15 10:49:31 (9 years ago)
- Location:
- branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDeleter.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDuplicater.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineCreater.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDeleter.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDuplicater.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs
r12346 r12420 102 102 .ToList(); 103 103 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 104 105 #pragma warning disable 612, 618 104 106 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 107 #pragma warning restore 612, 618 108 105 109 var tree = selectedSymbol.CreateTreeNode(); 106 110 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 134 138 throw new InvalidOperationException("No symbols are available for the tree."); 135 139 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 140 141 #pragma warning disable 612, 618 136 142 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 143 #pragma warning restore 612, 618 144 137 145 var tree = selectedSymbol.CreateTreeNode(); 138 146 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); -
branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs
r12347 r12420 94 94 var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList(); 95 95 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 96 97 #pragma warning disable 612, 618 96 98 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 99 #pragma warning restore 612, 618 100 97 101 var tree = selectedSymbol.CreateTreeNode(); 98 102 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); … … 122 126 123 127 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 128 #pragma warning disable 612, 618 124 129 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 130 #pragma warning restore 612, 618 131 125 132 var tree = selectedSymbol.CreateTreeNode(); 126 133 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); -
branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r12313 r12420 166 166 if (allowedSymbols.Count == 0) return false; 167 167 var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList(); 168 169 #pragma warning disable 612, 618 168 170 var selectedSymbol = allowedSymbols.SelectRandom(weights, random); 171 #pragma warning restore 612, 618 172 169 173 ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode(); 170 174 if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random); … … 212 216 select g).First().ToList(); 213 217 var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList(); 218 219 #pragma warning disable 612, 618 214 220 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); 221 #pragma warning restore 612, 618 222 215 223 var tree = selectedSymbol.CreateTreeNode(); 216 224 if (tree.HasLocalParameters) tree.ResetLocalParameters(random); -
branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/EnumerableExtensions.cs
r12341 r12420 29 29 //This class should not be used anymore. Use HeuristicLab.Random.RandomEnumberable instead 30 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.")] 31 32 public static class EnumerableExtensions { 33 [Obsolete("This method should not be used anymore. Use the extensions provided by HeuristicLab.Random.RandomEnumberable instead.")] 32 34 public static T SelectRandom<T>(this IEnumerable<T> xs, IRandom random) { 33 35 var list = xs as IList<T>; … … 39 41 } 40 42 } 43 [Obsolete("This method should not be used anymore. Use the extensions provided by HeuristicLab.Random.RandomEnumberable instead.")] 41 44 public static T SelectRandom<T>(this IEnumerable<T> xs, IEnumerable<double> weights, IRandom random) { 42 45 var list = xs as IList<T>; -
branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/OnePointShaker.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/RemoveBranchManipulation.cs
r12012 r12420 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/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs
r12012 r12420 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();
Note: See TracChangeset
for help on using the changeset viewer.