public static Stack <GPTreeNode> GetNodeStack(string originalExpression) { if (string.IsNullOrWhiteSpace(originalExpression)) { return(null); } var customGene = new FlexibleGPGene(FlexibleGPGene.AllFunctions.ToList(), 1); //gets the reverse (polish notation) expression and the elements in the expression var elements = originalExpression.Split(' '); //creates stack of nodes for each element in the reverse expression var nodeStack = new Stack <GPTreeNode>(); foreach (var element in elements) { if (!string.IsNullOrEmpty(element)) { nodeStack.Push(IsZeroNode(element) ? ZeroNode : new GPTreeNode(customGene.CreateNew(element))); } } return(nodeStack); }
public static HashSet <IGPChromosome> GenerateAllCombinations(GPTreeNode node) { var combChromosomes = new HashSet <IGPChromosome>(); //tests end condition, ie no more children, add node itself if ((node.Children == null) || (node.Children.Count == 0)) { combChromosomes.Add(new GPExpressionChromosome(node.ToString())); return(combChromosomes); } //add all combinations from each child foreach (var child in node.Children) { combChromosomes.UnionWith(GenerateAllCombinations(child)); } //for one argument functions, gets children var children0 = GetAllChildren(node.Children[0]); //creates combinations for all children foreach (var child0 in children0) { var newNode = (GPTreeNode)node.Clone(); newNode.Children[0] = child0; combChromosomes.Add(new GPExpressionChromosome(newNode.ToString())); } //for two argument functions if (node.Children.Count < 2) { return(combChromosomes); } //gets children from both sides var children1 = GetAllChildren(node.Children[1]); //creates combinations between the different children foreach (var child0 in children0) { foreach (var child1 in children1) { var newNode = (GPTreeNode)node.Clone(); newNode.Children[0] = child0; newNode.Children[1] = child1; combChromosomes.Add(new GPExpressionChromosome(newNode.ToString())); } } //if function is subtraction if (!node.Gene.ToString().Equals(FlexibleGPGene.GetFunctionTypeString(FunctionType.Subtract))) { return(combChromosomes); } //consider right side negative by adding "0 - 'childNode'" foreach (var child1 in children1) { var newNode = (GPTreeNode)node.Clone(); newNode.Children[0] = ZeroNode; newNode.Children[1] = child1; combChromosomes.Add(new GPExpressionChromosome(newNode.ToString())); } return(combChromosomes); }