예제 #1
0
        void WalkSolution()
        {
            int nStartCell  = 0;
            int nFinishCell = 0;

            for (int i = 0; i < nRows; ++i)
            {
                for (int j = 0; j < nColumns; ++j)
                {
                    if (cellStates[i, j] == CellState.START)
                    {
                        nStartCell = i * nColumns + j;
                    }
                    else if (cellStates[i, j] == CellState.FINISH)
                    {
                        nFinishCell = i * nColumns + j;
                    }
                }
            }

            try
            {
                List <int> steps = QLearning.Walk(nStartCell, nFinishCell, Q, R);
                for (int s = 1; s < steps.Count; ++s)
                {
                    int k = steps[s];
                    int i = k / nColumns;
                    int j = k % nColumns;

                    Button button = (Button)grid.GetControlFromPosition(j, i);
                    button.BackColor = Color.Yellow;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Nu se poate ajunge la acea destinatie");
            }
        }
예제 #2
0
        private void SolveMaze()
        {
            int nCells = nRows * nColumns;

            for (int i = 0; i < nCells; ++i)
            {
                R[i] = new double[nCells];
                for (int j = 0; j < nCells; ++j)
                {
                    R[i][j] = double.MinValue;
                }
            }

            int nFinishCell = 0;

            for (int i = 0; i < nRows; ++i)
            {
                for (int j = 0; j < nColumns; ++j)
                {
                    int nCell = i * nColumns + j;

                    if (cellStates[i, j] == CellState.FINISH)
                    {
                        nFinishCell = nCell;
                    }

                    if (cellStates[i, j] == CellState.BACKGROUND ||
                        cellStates[i, j] == CellState.START)
                    {
                        List <Point> v = new List <Point>();
                        if (j < nColumns - 1)
                        {
                            v.Add(new Point(j + 1, i));
                        }
                        if (j > 0)
                        {
                            v.Add(new Point(j - 1, i));
                        }
                        if (i < nRows - 1)
                        {
                            v.Add(new Point(j, i + 1));
                        }
                        if (i > 0)
                        {
                            v.Add(new Point(j, i - 1));
                        }

                        for (int k = 0; k < v.Count; ++k)
                        {
                            int vCell = v[k].Y * nColumns + v[k].X;

                            switch (cellStates[v[k].Y, v[k].X])
                            {
                            case CellState.BACKGROUND:
                            case CellState.START:
                                R[nCell][vCell] = -0.01;
                                break;

                            case CellState.OBSTACLE:
                                R[nCell][vCell] = double.MinValue;
                                break;

                            case CellState.FINISH:
                                R[nCell][vCell] = 1000.0;
                                break;

                            case CellState.BONUS:
                                R[nCell][vCell] = 1.0;
                                break;
                            }
                        }
                    }
                }
            }

            R[nFinishCell][nFinishCell] = 0.0;
            QLearning.Solve(R, Q, nFinishCell, 0.5, 0.5, 1000, 200000);

            WalkSolution();
        }