Пример #1
0
        private void Mutate(MathExpressionTree expression)
        {
            MathExpressionNode randomNode = expression.GetRandomNode();

            randomNode.SubstiteValue(randomNode.IsLeaf ?
                                     stohasticGenerator.GetRandomOperand()
                                           : stohasticGenerator.GetRandomOperator(randomNode.LeftChild, randomNode.RightChild));
        }
Пример #2
0
        private void Crossover(MathExpressionTree first, MathExpressionTree second)
        {
            MathExpressionNode crossoverPointOfFirst  = first.GetRandomSubtree();
            MathExpressionNode crossoverPointOfSecond = second.GetRandomSubtree();
            MathExpressionNode firstParent            = crossoverPointOfFirst.Parent;
            MathExpressionNode secondParent           = crossoverPointOfSecond.Parent;

            if (firstParent != null)
            {
                if (firstParent.LeftChild.Id == crossoverPointOfFirst.Id)
                {
                    firstParent.LeftChild = crossoverPointOfSecond;
                }
                else
                {
                    firstParent.RightChild = crossoverPointOfSecond;
                }
            }
            else
            {
                first.Root = crossoverPointOfSecond;
            }
            if (secondParent != null)
            {
                if (secondParent.LeftChild.Id == crossoverPointOfSecond.Id)
                {
                    secondParent.LeftChild = crossoverPointOfFirst;
                }
                else
                {
                    secondParent.RightChild = crossoverPointOfFirst;
                }
            }
            else
            {
                second.Root = crossoverPointOfFirst;
            }

            crossoverPointOfFirst.Parent  = secondParent;
            crossoverPointOfSecond.Parent = firstParent;
        }
Пример #3
0
        public MathExpressionTree GenerateIndividual()
        {
            MathExpressionTree validExpression;

            do
            {
                int operandsCount = stohasticGenerator.OperandsCount();
                List <MathExpressionNode> operatorsAndOperands = Enumerable.Range(0, 2 * operandsCount - 1)
                                                                 .Select(x => x % 2 == 0 ? stohasticGenerator.GetRandomOperand() : stohasticGenerator.GetRandomOperator())
                                                                 .ToList();
                MathExpressionNode root = operatorsAndOperands.Where(x => !x.IsLeaf)
                                          .OrderBy(x => Guid.NewGuid())
                                          .First();

                MathExpressionNode getGenome(List <MathExpressionNode> elements, MathExpressionNode parent, MathExpressionNode rootNode = null)
                {
                    if (elements.Count == 0)
                    {
                        return(null);
                    }
                    MathExpressionNode currentNode = (rootNode ?? elements.FirstOrDefault(x => !x.IsLeaf)) ?? elements.First();

                    currentNode.Parent = parent;
                    if (!currentNode.IsLeaf)
                    {
                        int currentNodeIndex = elements.IndexOf(currentNode);
                        currentNode.LeftChild  = getGenome(elements.Take(currentNodeIndex).ToList(), currentNode);
                        currentNode.RightChild = getGenome(elements.Skip(currentNodeIndex + 1).ToList(), currentNode);
                    }
                    return(currentNode);
                }

                validExpression = new MathExpressionTree(getGenome(operatorsAndOperands, null, root));
            } while (!validExpression.IsValidExpression());
            return(validExpression);
        }
Пример #4
0
 public Operator GetRandomOperator(MathExpressionNode firstOperand, MathExpressionNode secondOperand)
 => new Operator((Operation)randomGenerator.Next(max: operationCount))
 {
     LeftChild  = firstOperand,
     RightChild = secondOperand
 };