Exemplo n.º 1
0
        private void LoadGridButton_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog
            OpenFileDialog fileDialog = new OpenFileDialog();

            // Set the filter to CSV
            fileDialog.DefaultExt = ".csv";
            fileDialog.Filter     = "CSV Files (*.csv)|*.*";

            Nullable <bool> result = fileDialog.ShowDialog();

            // Get the selected filename
            if (result == true)
            {
                // Setup the grid world
                gridWorld = new GridWorld(grid, GridMapParser.Parse(fileDialog.FileName), CELL_HEIGHT, CELL_WIDTH);

                // Add click events for the rectangles
                foreach (Cell cell in gridWorld.GetCells())
                {
                    cell.GetRectangle().MouseLeftButtonDown += new MouseButtonEventHandler(MoveAgent);
                }

                // Setup the agent
                if (gridWorld.GetAgentStartingPosition() != null && gridWorld.GetAgentStartingPosition().Length == 2)
                {
                    int agentRowIndex = gridWorld.GetAgentStartingPosition()[0], agentColumnIndex = gridWorld.GetAgentStartingPosition()[1];

                    gridWorld.SetAgent(new Agent(grid, CELL_HEIGHT, CELL_WIDTH, agentRowIndex, agentColumnIndex));

                    // Get the starting cell
                    startingCell = gridWorld.GetCells()[agentRowIndex, agentColumnIndex];
                }
                else
                {
                    MessageBox.Show("Error: The agent starting position must be specified in the grid map file with the number 1. Please correct and try again.");
                    return;
                }

                // Setup the reward
                if (gridWorld.GetRewardPosition() != null && gridWorld.GetRewardPosition().Length == 2)
                {
                    int rewardRowIndex = gridWorld.GetRewardPosition()[0], rewardColumnIndex = gridWorld.GetRewardPosition()[1];

                    gridWorld.SetReward(new Reward(grid, CELL_HEIGHT, CELL_WIDTH, rewardRowIndex, rewardColumnIndex));

                    // Get the reward cell
                    rewardCell = gridWorld.GetCells()[rewardRowIndex, rewardColumnIndex];
                }
                else
                {
                    MessageBox.Show("Error: The reward starting position must be specified in the grid map file with the number 2. Please correct and try again.");
                    return;
                }
            }

            // Make the start button active
            StartButton.IsEnabled = true;
            ResetButton.IsEnabled = true;
        }
Exemplo n.º 2
0
        public void UpdateAgentPosition(object sender, EventArgs e)
        {
            if ((bool)AStarRadioButton.IsChecked)
            {
                gridWorld.GetAgent().SetRowIndex(aStarSearch.GetCurrentCell().GetRowIndex());
                gridWorld.GetAgent().SetColumnIndex(aStarSearch.GetCurrentCell().GetColumnIndex());

                // Check if agent is on the reward cell
                // If so, process the reward
                if (aStarSearch.GetCurrentCell().GetRowIndex() == gridWorld.GetRewardPosition()[0] &&
                    aStarSearch.GetCurrentCell().GetColumnIndex() == gridWorld.GetRewardPosition()[1])
                {
                    ProcessFoundReward();
                }
            }
            else // Q-Learning is checked
            {
                gridWorld.GetAgent().SetRowIndex(qLearningSearch.GetCurrentCell().GetRowIndex());
                gridWorld.GetAgent().SetColumnIndex(qLearningSearch.GetCurrentCell().GetColumnIndex());

                IlluminateCell();

                // Check if agent is on the reward cell
                // If so, swap the images and stop the timer
                if (qLearningSearch.GetCurrentCell().GetRowIndex() == gridWorld.GetRewardPosition()[0] &&
                    qLearningSearch.GetCurrentCell().GetColumnIndex() == gridWorld.GetRewardPosition()[1])
                {
                    ProcessFoundReward();
                }
            }

            gridWorld.GetAgent().UpdatePosition();
        }
        private void LoadGridButton_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog
            OpenFileDialog fileDialog = new OpenFileDialog();

            // Set the filter to CSV
            fileDialog.DefaultExt = ".csv";
            fileDialog.Filter = "CSV Files (*.csv)|*.*";

            Nullable<bool> result = fileDialog.ShowDialog();

            // Get the selected filename
            if (result == true)
            {
                // Setup the grid world
                gridWorld = new GridWorld(grid, GridMapParser.Parse(fileDialog.FileName), CELL_HEIGHT, CELL_WIDTH);

                // Add click events for the rectangles
                foreach (Cell cell in gridWorld.GetCells())
                {
                    cell.GetRectangle().MouseLeftButtonDown += new MouseButtonEventHandler(MoveAgent);
                }

                // Setup the agent
                if (gridWorld.GetAgentStartingPosition() != null && gridWorld.GetAgentStartingPosition().Length == 2)
                {
                    int agentRowIndex = gridWorld.GetAgentStartingPosition()[0], agentColumnIndex = gridWorld.GetAgentStartingPosition()[1];

                    gridWorld.SetAgent(new Agent(grid, CELL_HEIGHT, CELL_WIDTH, agentRowIndex, agentColumnIndex));

                    // Get the starting cell
                    startingCell = gridWorld.GetCells()[agentRowIndex, agentColumnIndex];
                }
                else
                {
                    MessageBox.Show("Error: The agent starting position must be specified in the grid map file with the number 1. Please correct and try again.");
                    return;
                }

                // Setup the reward
                if (gridWorld.GetRewardPosition() != null && gridWorld.GetRewardPosition().Length == 2)
                {
                    int rewardRowIndex = gridWorld.GetRewardPosition()[0], rewardColumnIndex = gridWorld.GetRewardPosition()[1];

                    gridWorld.SetReward(new Reward(grid, CELL_HEIGHT, CELL_WIDTH, rewardRowIndex, rewardColumnIndex));

                    // Get the reward cell
                    rewardCell = gridWorld.GetCells()[rewardRowIndex, rewardColumnIndex];
                }
                else
                {
                    MessageBox.Show("Error: The reward starting position must be specified in the grid map file with the number 2. Please correct and try again.");
                    return;
                }
            }

            // Make the start button active
            StartButton.IsEnabled = true;
            ResetButton.IsEnabled = true;
        }