Ejemplo n.º 1
0
        private void ChangeAgentStartPosition(MazeAgent agent, TextBox sourceTextBox)
        {
            int newStartPosition;
            int oldStartPosition;

            if (agent.StartPosition.ToString() != sourceTextBox.Text &&
                MazeTextChanged(sourceTextBox, false))
            {
                oldStartPosition    = agent.StartPosition;
                newStartPosition    = Convert.ToInt32(sourceTextBox.Text);
                agent.StartPosition = newStartPosition;
                bool isPrimary  = (agent == _agentPrimary);
                var  otherAgent = (isPrimary ? _agentSecondary : _agentPrimary);

                GetSpaceByPosition(oldStartPosition).SetStart(false, isPrimary);
                GetSpaceByPosition(newStartPosition).SetStart(true, isPrimary);

                if (otherAgent.StartPosition == oldStartPosition &&
                    _useSecondaryAgent)
                {
                    var space = GetSpaceByPosition(oldStartPosition);
                    space.SetStart(true, !isPrimary);
                    space.SetActive(!isPrimary);
                }
            }
        }
Ejemplo n.º 2
0
        private void OpenMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "Maze Files (*.maze)|*.maze";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                _walls.Clear();
                mazeSpace.Enabled = false;

                var loaded = MazeUtilities.LoadObject <MazeAgent>(dlg.FileName);
                _agentPrimary                 = MazeUtilities.ConvertLoadedAgent(loaded);
                _agentSecondary               = MazeUtilities.ConvertLoadedAgent(loaded);
                _agentSecondary.Environment   = MazeUtilities.CopyEnvironment(loaded.Environment);
                _agentSecondary.StartPosition = _agentPrimary.StartPosition;

                AgentSubscribeEvents(_agentPrimary);
                AgentSubscribeEvents(_agentSecondary);

                SetFormValuesFromMaze();

                _needsRetrain     = true;
                mazeSpace.Enabled = true;
            }
        }
Ejemplo n.º 3
0
 private void OnTriggerEnter(Collider otherCollider)
 {
     if (otherCollider.CompareTag("agent"))
     {
         MazeAgent agent = otherCollider.GetComponent <MazeAgent>();
         agent.MazeSuccesful();
     }
 }
Ejemplo n.º 4
0
 public void UpdateGraphics(MazeAgent agent)
 {
     for (int i = 0; i < agent.mazeDimension.x; ++i)
     {
         for (int j = 0; j < agent.mazeDimension.y; ++j)
         {
             blocks[i, j].color = ChooseColor((int)agent.map[i, j], agent);
         }
     }
 }
Ejemplo n.º 5
0
        private void ChangeStartPosition(MazeAgent agent, int newStart)
        {
            var space = MazeVm.GetSpaceByPosition(agent.StartPosition);

            space.IsStart = false;

            agent.StartPosition = newStart;
            space         = MazeVm.GetSpaceByPosition(agent.StartPosition);
            space.IsStart = true;
            MazeVm.SetActiveState(newStart, agent == PrimaryAgent);
        }
Ejemplo n.º 6
0
        private MazeAgent InitializeAgent()
        {
            var agent = new MazeAgent(
                0.5,
                0.5,
                1000,
                1000,
                3);

            AgentSubscribeEvents(agent);
            return(agent);
        }
Ejemplo n.º 7
0
        private Task RunAgent(MazeAgent agent)
        {
            try
            {
                agent.Run(agent.StartPosition);
            }
            catch (Exception ex)
            {
                string agentVal = agent == PrimaryAgent ? "Primary" : "Secondary";

                MessageBox.Show($"Error with {agentVal} Agent: {ex.Message}");
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 8
0
    public void InitializeGraphic(MazeAgent agent)
    {
        blocks = new SpriteRenderer[agent.mazeDimension.x, agent.mazeDimension.y];

        Vector3 offset = transform.position - new Vector3((agent.mazeDimension.x - 1) * blockDimension / 2, (agent.mazeDimension.y - 1) * blockDimension / 2);

        for (int i = 0; i < agent.mazeDimension.x; ++i)
        {
            for (int j = 0; j < agent.mazeDimension.y; ++j)
            {
                GameObject obj = GameObject.Instantiate(blockPrefab, new Vector3(i * blockDimension, j * blockDimension) + offset, Quaternion.identity, this.transform);
                blocks[i, j] = obj.GetComponent <SpriteRenderer>();
            }
        }
    }
Ejemplo n.º 9
0
        private void NewAgentMaze()
        {
            PrimaryAgent = new MazeAgent(0.5, 0.5, 1000, 1000, 3);
            PrimaryAgent.MaximumAllowedBacktracks = 3;
            PrimaryAgent.MaximumAllowedMoves      = 1000;

            PrimaryAgent.Environment = new MazeBase(1, 1, 0, 0, 200);

            SecondaryAgent             = MazeUtilities.ConvertLoadedAgent(PrimaryAgent);
            SecondaryAgent.Environment = MazeUtilities.CopyEnvironment(PrimaryAgent.Environment);

            MazeVm = new MazeViewModel(PrimaryAgent.Environment);

            OnPropertyChanged(nameof(PrimaryAgent));
            OnPropertyChanged(nameof(SecondaryAgent));
            OnPropertyChanged(nameof(GoalPosition));
        }
Ejemplo n.º 10
0
 private Color ChooseColor(int blockType, MazeAgent agent)
 {
     if (blockType == agent.WallInt)
     {
         return(Color.red);
     }
     else if (blockType == agent.GoalInt)
     {
         return(Color.green);
     }
     else if (blockType == agent.PlayerInt)
     {
         return(Color.yellow);
     }
     else
     {
         return(Color.black);
     }
 }
Ejemplo n.º 11
0
    public bool LoadState(int key)
    {
        MazeAgent fromEnv = this;

        if (fromEnv.savedState.ContainsKey(key))
        {
            GameState state = fromEnv.savedState[key];
            System.Buffer.BlockCopy(state.map, 0, map, 0, map.Length * sizeof(float));
            currentPlayerPosition = state.currentPlayerPosition;
            goalPosition          = state.goalPosition;
            startPosition         = state.startPosition;
            Win = state.win;
            viewer.UpdateGraphics(this);
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 12
0
        public TrainingProgress(MazeAgent agentPrimary, MazeAgent agentSecondary)
        {
            InitializeComponent();
            _agentPrimary   = agentPrimary;
            _agentSecondary = agentSecondary;

            if (_agentPrimary.MaximumAllowedBacktracks < 0)
            {
                _agentPrimary.MaximumAllowedBacktracks = 3;
            }

            _showEvery = _agentPrimary.NumberOfTrainingEpisodes / 5;

            //_maze = agent.Environment;
            _agentPrimary.TrainingEpisodeCompleted += _maze_TrainingEpisodeCompleted;
            _totalMoves   = 0;
            _totalScore   = 0;
            _averageMoves = 0;
            _averageScore = 0;
        }
        private MazeAgent GetTestAgent()
        {
            MazeAgent agent = new MazeAgent
            {
                DiscountFactor           = _agent.DiscountFactor,
                Environment              = GetTestMaze(),
                EpsilonDecayEnd          = _agent.EpsilonDecayEnd,
                EpsilonDecayStart        = _agent.EpsilonDecayStart,
                LearningRate             = _agent.LearningRate,
                MaximumAllowedBacktracks = _agent.MaximumAllowedBacktracks,
                MaximumAllowedMoves      = _agent.MaximumAllowedMoves,
                NumberOfTrainingEpisodes = _agent.NumberOfTrainingEpisodes
            };


            if (agent.MaximumAllowedBacktracks < 0)
            {
                agent.MaximumAllowedBacktracks = 3;
            }

            return(agent);
        }
Ejemplo n.º 14
0
        private Task RunAgentAsync(MazeAgent agent)
        {
            try
            {
                agent.Run(agent.StartPosition);
            }
            catch (Exception ex)
            {
                string prefix;

                if (agent == _agentPrimary)
                {
                    prefix = "Primary";
                }
                else
                {
                    prefix = "Secondary";
                }

                MessageBox.Show($"{prefix} Agent Error: {ex.Message}");
            }
            return(Task.CompletedTask);
        }
Ejemplo n.º 15
0
        public static MazeAgent ConvertLoadedAgent(MazeAgent loaded)
        {
            var converted = new MazeAgent
            {
                DiscountFactor           = loaded.DiscountFactor,
                Environment              = loaded.Environment,
                StartPosition            = loaded.StartPosition,
                EpsilonDecayEnd          = loaded.EpsilonDecayEnd,
                EpsilonDecayStart        = loaded.EpsilonDecayStart,
                LearningRate             = loaded.LearningRate,
                MaximumAllowedBacktracks = loaded.MaximumAllowedBacktracks,
                MaximumAllowedMoves      = loaded.MaximumAllowedMoves,
                NumberOfTrainingEpisodes = loaded.NumberOfTrainingEpisodes,
                TrainingSessions         = loaded.TrainingSessions
            };


            if (converted.Environment.ObjectiveReward == 0)
            {
                converted.Environment.ObjectiveReward = 20;
            }

            return(converted);
        }
Ejemplo n.º 16
0
 private Task TrainAgent(MazeAgent agent)
 {
     agent.Train();
     return(Task.CompletedTask);
 }
Ejemplo n.º 17
0
 public TrainingSessionsViewModel()
 {
     _agent = new MazeAgent();
 }
Ejemplo n.º 18
0
 private void AgentUnsubscribeEvents(MazeAgent agent)
 {
     agent.AgentStateChanged -= Maze_AgentStateChanged;
     agent.AgentCompleted    -= Maze_AgentCompleted;
 }
Ejemplo n.º 19
0
 public TrainingSessionsViewModel(MazeAgent agent)
 {
     _agent = agent;
 }
Ejemplo n.º 20
0
 private void InitializeAgents()
 {
     _agentPrimary   = InitializeAgent();
     _agentSecondary = InitializeAgent();
 }
 public TrainingSessionSelector(MazeAgent agent)
 {
     InitializeComponent();
     _agent = agent;
 }