Exemplo n.º 1
0
        public void Setup()
        {
            _initialState = new VacuumWorldState(2);
            _initialState.MakeSquareDirty(0, 0);
            _initialState.MakeSquareDirty(0, 1);

            _problem = new VacuumWorldSearchProblem(_initialState,
                                                    VacuumWorldActionHandler.CreateDeterministicActionHandler());
        }
Exemplo n.º 2
0
        public void SolveErratic2X2VacuumWorldWithDfs()
        {
            var initialState = new VacuumWorldState(2);

            initialState.MakeSquareDirty(1, 1);

            var problem  = new ErraticVacuumWorldSearchProblem();
            var solver   = new NonDeterministicDfsSearch <VacuumWorldState, VacuumWorldAction>(problem, initialState);
            var solution = solver.GetSolution();

            var       actionCounter = 0;
            const int actionLimit   = 1000;
            var       erraticWorld  = VacuumWorld.CreateErraticVacuumWorld(initialState);

            while (!problem.IsGoal(erraticWorld.State))
            {
                var action = solution.NextAction(erraticWorld.State);

                erraticWorld.DoAction(action);

                if (++actionCounter > actionLimit)
                {
                    Assert.Fail($"failed to reach goal after {actionCounter} actions");
                }
            }
        }
Exemplo n.º 3
0
        public void DoAction(VacuumWorldState state, VacuumWorldAction action)
        {
            if (action != VacuumWorldAction.Suck)
            {
                throw new InvalidOperationException();
            }

            var squareWasClean = !state.SquareIsDirty(state.VacuumPos);

            _decoratedHandler.DoAction(state, action);

            if (squareWasClean)
            {
                if (_random.TrueWithProbability(_makeDirtyProbability))
                {
                    state.MakeSquareDirty(state.VacuumPos);
                }
            }
            else
            {
                if (_random.TrueWithProbability(_cleanExtraProbability))
                {
                    CleanRandomDirtyNeighbour(state);
                }
            }
        }
Exemplo n.º 4
0
        public void GivenSomeSquaresAreDirty_ShouldNotBeGoalState()
        {
            var stateWithDirtySquares = new VacuumWorldState(3);

            stateWithDirtySquares.MakeSquareDirty(1, 1);

            Assert.IsFalse(_searchProblem.IsGoal(stateWithDirtySquares));
        }
Exemplo n.º 5
0
        public void Suck_dirty_should_make_it_clean()
        {
            _state.MakeSquareDirty(1, 1);
            _state.VacuumPos = new Point2D(1, 1);

            _handler.DoAction(_state, VacuumWorldAction.Suck);

            Assert.IsFalse(_state.SquareIsDirty(_state.VacuumPos));
        }
Exemplo n.º 6
0
        public void States_with_different_dirty_squares_should_not_be_equal()
        {
            var a = new VacuumWorldState(3);
            var b = new VacuumWorldState(3);

            a.MakeSquareDirty(1, 1);

            Assert.AreNotEqual(a, b);
            Assert.AreNotEqual(a.GetHashCode(), b.GetHashCode());
        }
Exemplo n.º 7
0
        public void States_with_same_data_should_be_equal()
        {
            var a = new VacuumWorldState(3);
            var b = new VacuumWorldState(3);

            a.VacuumPos = new Point2D(1, 2);
            a.MakeSquareDirty(1, 1);
            b.VacuumPos = new Point2D(1, 2);
            b.MakeSquareDirty(1, 1);

            Assert.AreEqual(a, b);
            Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
        }
Exemplo n.º 8
0
        public void GivenOnlyVacuumSquareIsDirty_Suck_ShouldReturnOnePossibleState()
        {
            var currentState = new VacuumWorldState(3);

            currentState.MakeSquareDirty(currentState.VacuumPos);

            var onlyPossibleState = currentState.Clone();

            onlyPossibleState.CleanSquare(currentState.VacuumPos);

            // act
            var potentialStates = _searchProblem.DoAction(currentState, VacuumWorldAction.Suck).ToList();

            // assert
            Assert.AreEqual(1, potentialStates.Count);
            Assert.AreEqual(1, potentialStates.Count(s => s.Equals(onlyPossibleState)));
        }