Beispiel #1
0
        /// <summary>
        /// Takes the initialState as the parameter
        /// generates its successors and select the best successor based on heuristic
        /// </summary>
        /// <param name="initialState">ProblemState instance which is the start state</param>
        /// <returns>ProblemState instance which is the solution state</returns>
        public static ProblemState HillClimbing(ProblemState initialState)
        {
            ProblemState current = initialState;

            while (true)
            {
                if (Program.numberOfSteps < 100)
                {
                    //Generate the successors
                    List <ProblemState> successors = current.GenerateNextStates();
                    if (successors.Count > 0)
                    {
                        List <ProblemState> bestSuccessor = new List <ProblemState>();
                        bestSuccessor.Add(successors[0]);
                        //Select the best successor
                        foreach (ProblemState successor in successors)
                        {
                            if (successor.HCost() == bestSuccessor[0].HCost())
                            {
                                bestSuccessor.Add(successor);
                            }
                            else if (successor.HCost() < bestSuccessor[0].HCost())
                            {
                                bestSuccessor.Clear();
                                bestSuccessor.Add(successor);
                            }
                        }

                        Program.numberOfSteps++;
                        Random random             = new Random();
                        int    chooseOneSuccessor = random.Next(bestSuccessor.Count);
                        //if current better than best successor return current
                        if (bestSuccessor[chooseOneSuccessor].HCost() > current.HCost())
                        {
                            return(current);
                        }
                        //else set current to best successor
                        current = bestSuccessor[chooseOneSuccessor];
                    }
                    else
                    {
                        //enters here if the Hill-Climbing is stuck due to no successors
                        Program.numberOfInits++;
                        Program.numberOfSteps = 0;
                        initialState.Solution = new int[initialState.Length, initialState.NumberOfIntervals];
                        initialState.InitializeState();
                        return(HillClimbing(initialState));
                    }
                }
                else
                {
                    //enters here if the Hill-Climbing is stuck due to local maximum or plateau or ridge
                    Program.numberOfInits++;
                    Program.numberOfSteps = 0;
                    initialState.Solution = new int[initialState.Length, initialState.NumberOfIntervals];
                    initialState.InitializeState();
                    return(HillClimbing(initialState));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Takes the initialState as the parameter
        /// generates its successors and select the best successor based on heuristic
        /// </summary>
        /// <param name="initialState">ProblemState instance which is the start state</param>
        /// <returns>ProblemState instance which is the solution state</returns>
        public static ProblemState HillClimbing(ProblemState initialState)
        {
            ProblemState current = initialState ;
            while(true)
            {
                if (Program.numberOfSteps < 100)
                {
                    //Generate the successors
                    List<ProblemState> successors = current.GenerateNextStates();
                    if (successors.Count>0)
                    {
                        List<ProblemState> bestSuccessor = new List<ProblemState>();
                        bestSuccessor.Add(successors[0]);
                        //Select the best successor
                        foreach (ProblemState successor in successors)
                        {
                            if (successor.HCost() == bestSuccessor[0].HCost())
                                bestSuccessor.Add(successor);
                            else if (successor.HCost() < bestSuccessor[0].HCost())
                            {
                                bestSuccessor.Clear();
                                bestSuccessor.Add(successor);
                            }
                        }

                        Program.numberOfSteps++;
                        Random random = new Random();
                        int chooseOneSuccessor = random.Next(bestSuccessor.Count);
                        //if current better than best successor return current
                        if (bestSuccessor[chooseOneSuccessor].HCost() > current.HCost()) return current;
                        //else set current to best successor
                        current = bestSuccessor[chooseOneSuccessor];
                    }
                    else
                    {
                        //enters here if the Hill-Climbing is stuck due to no successors
                        Program.numberOfInits++;
                        Program.numberOfSteps = 0;
                        initialState.Solution = new int[initialState.Length, initialState.NumberOfIntervals];
                        initialState.InitializeState();
                        return HillClimbing(initialState);
                    }
                }
                else
                {
                    //enters here if the Hill-Climbing is stuck due to local maximum or plateau or ridge
                    Program.numberOfInits++;
                    Program.numberOfSteps = 0;
                    initialState.Solution = new int[initialState.Length, initialState.NumberOfIntervals];
                    initialState.InitializeState();
                    return HillClimbing(initialState);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Creates an instance of ProblemState, initializes it and calls HillClimbing method to get the solution
        /// </summary>
        /// <param name="capacity"> array containing capacity of the power units  </param>
        /// <param name="maxPerInterval">array containing the maximum power needed in each interval</param>
        /// <param name="intervalsLeft">array containing the intervals needed for each unit to compplete the maintenance</param>
        /// <returns> the state found by the HillClimbing technique</returns>
        public static ProblemState FindSolution(int[] capacity, int[] maxPerInterval, int[] intervalsLeft)
        {
            ProblemState ps = new ProblemState(capacity, maxPerInterval, intervalsLeft);

            ps.InitializeState();

            return(HillClimbingProblem.HillClimbing(ps));
        }
Beispiel #4
0
        /// <summary>
        /// Creates an instance of ProblemState, initializes it and calls HillClimbing method to get the solution
        /// </summary>
        /// <param name="capacity"> array containing capacity of the power units  </param>
        /// <param name="maxPerInterval">array containing the maximum power needed in each interval</param>
        /// <param name="intervalsLeft">array containing the intervals needed for each unit to compplete the maintenance</param>
        /// <returns> the state found by the HillClimbing technique</returns>
        public static ProblemState FindSolution(int[] capacity, int[] maxPerInterval, int[] intervalsLeft)
        {
            ProblemState ps = new ProblemState(capacity, maxPerInterval, intervalsLeft);
            ps.InitializeState();

            return HillClimbingProblem.HillClimbing(ps);
        }