Ejemplo n.º 1
0
        /// <summary>
        /// Execute an action on an agent
        /// </summary>
        public void Execute(Agent agent, Action action)
        {
            if (action == null)
            {
                return;
            }

            var executed = false;

            switch (action.Type)
            {
            case ActionType.Move:
                executed |= MoveAgent(agent, agent.Direction);
                break;

            case ActionType.Turn:
                var turnAction    = action as TurnAction;
                var turnDirection = agent.Direction.GetRelative(turnAction.GetDirection());

                if (Maze.CanGoInDirection(agent.Location, turnDirection))
                {
                    agent.Direction = turnDirection;
                    executed        = true;
                }
                break;

            case ActionType.Look:
                var lookAction = action as LookAction;
                var reaction   = lookAction.Reaction(agent.Direction, Maze.Look(agent.Location));

                agent.Direction = Maze.CanGoInDirection(agent.Location, reaction)
                            ? reaction
                            : Maze.GetAvailableLocationDirections(agent.Location)
                                  .First(location => location.Direction != agent.Direction)
                                  .Direction;

                executed = true;
                break;
            }
            agent.CurrentAction = null;

            if (executed)
            {
                agent.ActionsExecuted++;
            }

            SetActionsText(agent.Type, agent.GetActionCount());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Prints current Gene values and resets the environment with new genes selected
        /// </summary>
        public void Reset()
        {
            AddLine(AgentType.Pacman, string.Format("\nGeneration: {3}/{0} Actions: {2} Fitness: {1}", _pacmanGeneIndex, GetFitnessValue(Pacman), Pacman.GetActionCount(), Pacman.Evolutions), true);
            AddLine(AgentType.Ghost, string.Format("\nGeneration: {3}/{0} Actions: {2} Fitness: {1}", _ghostGeneIndex, GetFitnessValue(Ghost), Ghost.GetActionCount(), Ghost.Evolutions), true);

            Maze.MoveAgent(Pacman, Pacman.Reset());
            Maze.MoveAgent(Ghost, Ghost.Reset());

            Maze.ReplaceFruits(_settings.RandomFruitPositions);

            SetNextGenes();

            _fruitsCollected = 0;
            SetFruitsCollected(0);

            SetActionsText(AgentType.Ghost, 0);
            SetActionsText(AgentType.Pacman, 0);

            _isPlaying = true;
        }