Ejemplo n.º 1
0
        protected override ProblemState <T> UpdateState <TPercepted>(
            ProblemState <T> state, TPercepted percept)
        {
            var perceptedObj = percept as PuzzleBoard <T>;

            return(perceptedObj.ToProblemState());
        }
Ejemplo n.º 2
0
        public ProblemAction[] Actions(ProblemState <T> state)
        {
            var(rowNumber, columnNumber) = state.FindEmpty();

            var actions = new List <ProblemAction>();

            if (columnNumber > 0)
            {
                actions.Add(ProblemAction.Left);
            }

            if (columnNumber < state.GetLength(0) - 1)
            {
                actions.Add(ProblemAction.Right);
            }

            if (rowNumber > 0)
            {
                actions.Add(ProblemAction.Up);
            }

            if (rowNumber < state.GetLength(1) - 1)
            {
                actions.Add(ProblemAction.Down);
            }

            return(actions.ToArray());
        }
Ejemplo n.º 3
0
        protected override ProblemState <T> FormulateGoal(ProblemState <T> state)
        {
            var values = state.ToValues();

            Array.Sort(values);

            return(values.ToProblemState(state.GetLength(0), state.GetLength(1)));
        }
Ejemplo n.º 4
0
 protected override ISearchProblem <ProblemState <T>, ProblemAction> FormulateProblem(
     ProblemState <T> state, ProblemState <T> goal)
 {
     return(new ProblemDefinition <T>()
     {
         InitialState = state,
         GoalState = goal
     });
 }
Ejemplo n.º 5
0
        public static ProblemState <T> ToProblemState <T>(this T[] values, int rows, int columns)
        {
            var state = new ProblemState <T>(rows, columns);

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    state[i, j] = values[rows * i + j];
                }
            }

            return(state);
        }
Ejemplo n.º 6
0
        public static bool StateEquals <T>(this ProblemState <T> left, ProblemState <T> right)
        {
            for (int i = 0; i < left.GetLength(0); i++)
            {
                for (int j = 0; j < left.GetLength(1); j++)
                {
                    if (!Equals(left[i, j], right[i, j]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 7
0
        public static ProblemState <T> ToProblemState <T>(this PuzzleBoard <T> perceptedObj)
        {
            var perceptedState = new ProblemState <T>(
                perceptedObj.Pieces.GetLength(0), perceptedObj.Pieces.GetLength(1));

            for (int i = 0; i < perceptedState.GetLength(0); i++)
            {
                for (int j = 0; j < perceptedState.GetLength(1); j++)
                {
                    perceptedState[i, j] = perceptedObj.Pieces[i, j].Value;
                }
            }

            return(perceptedState);
        }
Ejemplo n.º 8
0
        public static T[] ToValues <T>(this ProblemState <T> state)
        {
            int rows = state.GetLength(0);

            var values = new T[rows * state.GetLength(1)];

            for (int i = 0; i < state.GetLength(0); i++)
            {
                for (int j = 0; j < state.GetLength(1); j++)
                {
                    values[rows * i + j] = state[i, j];
                }
            }

            return(values);
        }
Ejemplo n.º 9
0
        public static PuzzleBoard <T> ToPuzzleBoard <T>(this ProblemState <T> problemState)
        {
            var state = new PuzzleBoard <T>(new PuzzlePiece <T> [problemState.GetLength(0), problemState.GetLength(1)]);

            for (int i = 0; i < problemState.GetLength(0); i++)
            {
                for (int j = 0; j < problemState.GetLength(0); j++)
                {
                    state.Pieces[i, j] = new PuzzlePiece <T> {
                        Value = problemState[i, j]
                    };
                }
            }

            return(state);
        }
Ejemplo n.º 10
0
        public static double StraightLineDistance(
            ProblemState <int> stateSrc, ProblemState <int> stateDest)
        {
            double totalDist = 0;

            for (int i = 0; i < stateDest.GetLength(0); i++)
            {
                for (int j = 0; j < stateDest.GetLength(1); j++)
                {
                    totalDist += StraightLineDistance(
                        stateSrc.FindValueIndex(stateDest[i, j]),
                        (i, j));
                }
            }

            return(totalDist);
        }
Ejemplo n.º 11
0
        public ProblemState <T> Result(ProblemState <T> state, ProblemAction action)
        {
            var(rowNumber, columnNumber) = state.FindEmpty();

            var transitionState = new ProblemState <T>(state.GetLength(0), state.GetLength(1));

            ProblemState <T> .Copy(state, transitionState, state.Length);

            int row = rowNumber, col = columnNumber;

            if (action == ProblemAction.Up)
            {
                row = rowNumber - 1;
            }

            if (action == ProblemAction.Down)
            {
                row = rowNumber + 1;
            }

            if (action == ProblemAction.Left)
            {
                col = columnNumber - 1;
            }

            if (action == ProblemAction.Right)
            {
                col = columnNumber + 1;
            }

            transitionState[row, col] = state[rowNumber, columnNumber];

            transitionState[rowNumber, columnNumber] = state[row, col];

            return(transitionState);
        }
Ejemplo n.º 12
0
 public static (int rowNumber, int columnNumber) FindEmpty <T>(this ProblemState <T> state)
 {
     return(state.FindValueIndex(default));
Ejemplo n.º 13
0
 public double StepCost(ProblemState <T> state, ProblemAction action)
 {
     return(1);
 }
Ejemplo n.º 14
0
 public bool GoalTest(ProblemState <T> state)
 {
     return(GoalState.StateEquals(state));
 }