Example #1
0
        public BuildingState Clone()
        {
            var buildingState = new BuildingState(ElementNames);

            Array.Copy(_chipLocations, buildingState._chipLocations, NumberOfElements);
            Array.Copy(_generatorLocations, buildingState._generatorLocations, NumberOfElements);

            buildingState.ElevatorLocation = ElevatorLocation;

            return(buildingState);
        }
Example #2
0
        public void create_goal_state()
        {
            var currentBuildingState = CreateBuildingState();

            currentBuildingState.SetChipLocation('H', 1);
            currentBuildingState.SetGeneratorLocation('H', 1);
            currentBuildingState.SetChipLocation('L', 0);
            currentBuildingState.SetGeneratorLocation('L', 2);

            var goalState = BuildingState.CreateGoalState(currentBuildingState);

            Assert.IsTrue(goalState.IsGoalState());
        }
Example #3
0
        public static BuildingState CreateGoalState(BuildingState currentState)
        {
            var goalState = new BuildingState(currentState.ElementNames)
            {
                ElevatorLocation = TopFloor
            };

            foreach (var element in currentState.ElementNames)
            {
                goalState.SetChipLocation(element, TopFloor);
                goalState.SetGeneratorLocation(element, TopFloor);
            }

            return(goalState);
        }
Example #4
0
        private static BuildingState CreateBuildingState(int elevatorLocation = 0, params char[] elementNames)
        {
            BuildingState buildingState;

            if (elementNames.Length > 0)
            {
                buildingState = new BuildingState(elementNames);
            }
            else
            {
                buildingState = new BuildingState(new List <char> {
                    'H', 'L'
                });
            }

            buildingState.ElevatorLocation = elevatorLocation;
            return(buildingState);
        }
        public int GetStepsToGoalState(BuildingState currentState, BuildingState goalState)
        {
            var currentStates = new Queue <BuildingState>();

            currentStates.Enqueue(currentState);

            var nextStates = new Queue <BuildingState>();
            var depth      = 0;

            var visitedStates = new HashSet <int>();

            visitedStates.Add(currentState.GetHashCode());

            while (currentStates.Count > 0)
            {
                var current = currentStates.Dequeue();

                if (current.Equals(goalState))
                {
                    return(depth);
                }

                var next = current.GetValidNextStates();

                foreach (var nextState in next)
                {
                    if (!visitedStates.Contains(nextState.GetHashCode()))
                    {
                        nextStates.Enqueue(nextState);
                        visitedStates.Add(nextState.GetHashCode());
                    }
                }

                if (currentStates.Count == 0)
                {
                    currentStates = nextStates;
                    nextStates    = new Queue <BuildingState>();
                    depth++;
                }
            }

            return(-1);
        }
Example #6
0
        public void indicate_current_state_is_goal_state()
        {
            var buildingState = new BuildingState(new List <char>()
            {
                'H', 'L'
            });

            buildingState.SetChipLocation('H', 3);
            buildingState.SetGeneratorLocation('H', 3);
            buildingState.SetChipLocation('L', 3);
            buildingState.SetGeneratorLocation('L', 3);
            buildingState.ElevatorLocation = 3;

            var goalState = BuildingState.CreateGoalState(buildingState);

            var stateTraverser = new StateTraverser();

            var stepsToGoalState = stateTraverser.GetStepsToGoalState(buildingState, goalState);

            Assert.AreEqual(0, stepsToGoalState);
        }
Example #7
0
        public void indicate_no_possible_path_from_current_state()
        {
            var buildingState = new BuildingState(new List <char>()
            {
                'H', 'L', 'S'
            });

            buildingState.SetChipLocation('H', 0);
            buildingState.SetGeneratorLocation('H', 0);
            buildingState.SetChipLocation('L', 1);
            buildingState.SetGeneratorLocation('L', 3);
            buildingState.SetChipLocation('S', 2);
            buildingState.SetGeneratorLocation('S', 2);
            buildingState.ElevatorLocation = 1;

            var goalState = BuildingState.CreateGoalState(buildingState);

            var stateTraverser = new StateTraverser();

            var stepsToGoalState = stateTraverser.GetStepsToGoalState(buildingState, goalState);

            Assert.AreEqual(-1, stepsToGoalState);
        }