コード例 #1
0
        private void MutateSingle(TermExpression expression, int factorSimpleMutation)
        {
            var lengthTokensTable = tokensTable.Count();
            var rnd           = new Random();
            var tablePosition = rnd.Next(lengthTokensTable - 1);

            var lengthEpression = expression.ExpressionTokens.Length;
            var position        = rnd.Next(lengthEpression - 1);

            var typeMutation = rnd.Next(factorSimpleMutation);

            if (typeMutation != 0)
            {
                //mutate element
                expression.ExpressionTokens[position] = tokensTable.ElementAt(tablePosition);
            }
            else
            {
                //add element to end
                var token = tokensTable.ElementAt(tablePosition);
                var expr  = expression.ExpressionTokens.ToList();
                expr.Add(token);
                if (!token.IsTerminal)
                {
                    var terminals      = tokensTable.Where(x => x.IsTerminal);
                    var countTerminals = terminals.Count();
                    for (var i = 0; i < token.Arity; i++)
                    {
                        expr.Add(terminals.ElementAt(rnd.Next(countTerminals - 1)));
                    }
                }
            }
        }
コード例 #2
0
        private ICollection <TermExpression> Recombine(TermExpression item, TermExpression secondItem)
        {
            var firstLength  = item.ExpressionTokens.Length;
            var seconfLength = secondItem.ExpressionTokens.Length;
            var tokenLength  = (firstLength > seconfLength) ? seconfLength : firstLength;

            var takePositions = new Random().Next(tokenLength - 1);

            var firstParticleOfFirst   = item.ExpressionTokens.Take(takePositions).AsEnumerable();
            var SecondParticleOfFirst  = item.ExpressionTokens.Skip(takePositions).AsEnumerable();
            var firstParticleOfSecond  = secondItem.ExpressionTokens.Take(takePositions).AsEnumerable();
            var SecondParticleOfSecond = secondItem.ExpressionTokens.Skip(takePositions).AsEnumerable();

            var result = new List <TermExpression>
            {
                new TermExpression
                {
                    ExpressionTokens = Enumerable.Concat(firstParticleOfFirst, SecondParticleOfSecond).ToArray(),
                },
                new TermExpression
                {
                    ExpressionTokens = Enumerable.Concat(firstParticleOfSecond, SecondParticleOfFirst).ToArray(),
                }
            };

            return(result);
        }