Пример #1
0
        public override IEnumerable<Individual> Crossover(Individual anotherIndividual, IRandomGenerator randomGenerator)
        {
            var another = anotherIndividual as FiniteAutomaton;
            if (another == null) throw new ArgumentException();

            var children = new FiniteAutomaton[2];
            for (int i = 0; i < 2; i++)
                children[i] = Create(0, StatesNumber, EventsNumber);

            if (randomGenerator.Next(2) == 1)
            {
                children[0].InitialState = another.InitialState;
                children[1].InitialState = InitialState;
            }
            else
            {
                children[0].InitialState = InitialState;
                children[1].InitialState = another.InitialState;
            }

            for (int state = 0; state < StatesNumber; state++)
            {
                int flag = randomGenerator.Next(4);
                switch (flag)
                {
                    case 0:
                        for (int @event = 0; @event < EventsNumber; @event++)
                        {
                            children[0].Transitions[state, @event] = another.Transitions[state, @event];
                            children[1].Transitions[state, @event] = Transitions[state, @event];
                        }
                        break;

                    case 1:
                        for (int @event = 0; @event < EventsNumber; @event++)
                        {
                            children[0].Transitions[state, @event] = Transitions[state, @event];
                            children[1].Transitions[state, @event] = another.Transitions[state, @event];
                        }
                        break;

                    case 2:
                        for (int @event = 0; @event < EventsNumber / 2; @event++)
                        {
                            int secondHalf = @event + EventsNumber / 2;
                            children[0].Transitions[state, @event] = another.Transitions[state, @event];
                            children[0].Transitions[state, secondHalf] = Transitions[state, secondHalf];
                            children[1].Transitions[state, @event] = another.Transitions[state, @event];
                            children[1].Transitions[state, secondHalf] = Transitions[state, secondHalf];
                        }
                        break;

                    case 3:
                        for (int @event = 0; @event < EventsNumber / 2; @event++)
                        {
                            int secondHalf = @event + EventsNumber / 2;
                            children[0].Transitions[state, @event] = Transitions[state, @event];
                            children[0].Transitions[state, secondHalf] = another.Transitions[state, secondHalf];
                            children[1].Transitions[state, @event] = Transitions[state, @event];
                            children[1].Transitions[state, secondHalf] = another.Transitions[state, secondHalf];
                        }
                        break;
                }
            }

            return children;
        }
Пример #2
0
        public override IEnumerable <Individual> Crossover(Individual anotherIndividual, IRandomGenerator randomGenerator)
        {
            var another = anotherIndividual as FiniteAutomaton;

            if (another == null)
            {
                throw new ArgumentException();
            }

            var children = new FiniteAutomaton[2];

            for (int i = 0; i < 2; i++)
            {
                children[i] = Create(0, StatesNumber, EventsNumber);
            }

            if (randomGenerator.Next(2) == 1)
            {
                children[0].InitialState = another.InitialState;
                children[1].InitialState = InitialState;
            }
            else
            {
                children[0].InitialState = InitialState;
                children[1].InitialState = another.InitialState;
            }

            for (int state = 0; state < StatesNumber; state++)
            {
                int flag = randomGenerator.Next(4);
                switch (flag)
                {
                case 0:
                    for (int @event = 0; @event < EventsNumber; @event++)
                    {
                        children[0].Transitions[state, @event] = another.Transitions[state, @event];
                        children[1].Transitions[state, @event] = Transitions[state, @event];
                    }
                    break;

                case 1:
                    for (int @event = 0; @event < EventsNumber; @event++)
                    {
                        children[0].Transitions[state, @event] = Transitions[state, @event];
                        children[1].Transitions[state, @event] = another.Transitions[state, @event];
                    }
                    break;

                case 2:
                    for (int @event = 0; @event < EventsNumber / 2; @event++)
                    {
                        int secondHalf = @event + EventsNumber / 2;
                        children[0].Transitions[state, @event]     = another.Transitions[state, @event];
                        children[0].Transitions[state, secondHalf] = Transitions[state, secondHalf];
                        children[1].Transitions[state, @event]     = another.Transitions[state, @event];
                        children[1].Transitions[state, secondHalf] = Transitions[state, secondHalf];
                    }
                    break;

                case 3:
                    for (int @event = 0; @event < EventsNumber / 2; @event++)
                    {
                        int secondHalf = @event + EventsNumber / 2;
                        children[0].Transitions[state, @event]     = Transitions[state, @event];
                        children[0].Transitions[state, secondHalf] = another.Transitions[state, secondHalf];
                        children[1].Transitions[state, @event]     = Transitions[state, @event];
                        children[1].Transitions[state, secondHalf] = another.Transitions[state, secondHalf];
                    }
                    break;
                }
            }

            return(children);
        }
Пример #3
0
 public AntMover(FiniteAutomaton automation, AppleField field)
 {
     Ant = automation;
     _originalField = field;
     Restart();
 }