/// <summary> /// Displays the solution in chart /// </summary> /// <param name="ps">solution state as input</param> private void DisplaySolution(ProblemState ps) { chart1.Visible = true; for (int i = 0; i < ps.NumberOfIntervals; i++) { chart1.Series.Add("Series" + i); chart1.Series["Series" + i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.RangeBar; chart1.Series["Series" + i].ChartArea = "ChartArea1"; chart1.Series["Series" + i]["DrawSideBySide"] = "false"; chart1.Series["Series" + i].YValuesPerPoint = 2; chart1.Series["Series" + i].AxisLabel = (i + 1).ToString(); for (int j = 0; j < ps.Length; j++) { chart1.Series["Series" + i].Points.AddXY(i, j, j + ps.Solution[j, i]); } } chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0; chart1.ChartAreas["ChartArea1"].AxisY.Maximum = ps.Length; chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true; chart1.ChartAreas["ChartArea1"].AxisY.Interval = 1; chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.IntervalOffset = -0.5; chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "0"; chart1.ChartAreas["ChartArea1"].AxisY.Title = "POWER UNITS"; chart1.ChartAreas["ChartArea1"].AxisX.Title = "INTERVALS"; }
/// <summary> /// Function to display the solution of the problem found by the Hill Climbing approach and SelectMax approach /// </summary> /// <param name="ps"> an instance of the ProblemState</param> public static void DisplayResult(ProblemState ps) { //Hill Climbing Console.WriteLine("=====Output of the Hill Climbing approach====="); int [] netReservePerInterval = new int[ps.NumberOfIntervals ]; for (int i = 0; i < ps.NumberOfIntervals; i++) { Console.WriteLine("Interval {0}", i+1); Console.Write("Units Scheduled : "); for (int j = 0; j < ps.Length; j++) if(ps.Solution[j, i] == 1) { Console.Write(" {0} ", j+1); netReservePerInterval[i] += ps.Solution[j, i] * ps.Capacity[j]; } netReservePerInterval[i] = ps.Total - ps.MaxPerInterval[i] - netReservePerInterval[i]; Console.WriteLine(); Console.WriteLine("Net Reserve : {0}", netReservePerInterval[i]); Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Number of steps to reach the solution = {0}", numberOfSteps); Console.WriteLine("Number of Initializations = {0}", numberOfInits); Console.WriteLine(); }
/// <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)); } } }
/// <summary> /// Displays the solution in chart /// </summary> /// <param name="ps">solution state as input</param> private void DisplaySolution(ProblemState ps) { chart1.Visible = true; for (int i = 0; i < ps.NumberOfIntervals; i++) { chart1.Series.Add("Series" + i); chart1.Series["Series" + i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.RangeBar; chart1.Series["Series" + i].ChartArea = "ChartArea1"; chart1.Series["Series" + i]["DrawSideBySide"] = "false"; chart1.Series["Series" + i].YValuesPerPoint = 2; chart1.Series["Series" + i].AxisLabel = (i + 1).ToString(); for (int j = 0; j < ps.Length; j++) chart1.Series["Series" + i].Points.AddXY(i,j,j+ps.Solution[j, i]); } chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0; chart1.ChartAreas["ChartArea1"].AxisY.Maximum = ps.Length; chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true; chart1.ChartAreas["ChartArea1"].AxisY.Interval = 1; chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.IntervalOffset = -0.5; chart1.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format="0"; chart1.ChartAreas["ChartArea1"].AxisY.Title = "POWER UNITS"; chart1.ChartAreas["ChartArea1"].AxisX.Title = "INTERVALS"; }
/// <summary> /// Function to display the solution of the problem found by the Hill Climbing approach and SelectMax approach /// </summary> /// <param name="ps"> an instance of the ProblemState</param> public static void DisplayResult(ProblemState ps) { //Hill Climbing Console.WriteLine("=====Output of the Hill Climbing approach====="); int [] netReservePerInterval = new int[ps.NumberOfIntervals]; for (int i = 0; i < ps.NumberOfIntervals; i++) { Console.WriteLine("Interval {0}", i + 1); Console.Write("Units Scheduled : "); for (int j = 0; j < ps.Length; j++) { if (ps.Solution[j, i] == 1) { Console.Write(" {0} ", j + 1); netReservePerInterval[i] += ps.Solution[j, i] * ps.Capacity[j]; } } netReservePerInterval[i] = ps.Total - ps.MaxPerInterval[i] - netReservePerInterval[i]; Console.WriteLine(); Console.WriteLine("Net Reserve : {0}", netReservePerInterval[i]); Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Number of steps to reach the solution = {0}", numberOfSteps); Console.WriteLine("Number of Initializations = {0}", numberOfInits); Console.WriteLine(); }
/// <summary> /// Main method where the execution starts /// </summary> /// <param name="args"></param> static void Main(string[] args) { //Get the input from the user int[] capacity = null, maxPerInterval = null, intervalsLeft = null; Console.Write("Enter 1 to provide input from console or Enter any other key to take input from file:"); int display; string input = Console.ReadLine(); if (Int32.TryParse(input, out display) && display == 1) { Program.GetInputFromConsole(ref capacity, ref maxPerInterval, ref intervalsLeft); } else { Program.GetInputFromFile(ref capacity, ref maxPerInterval, ref intervalsLeft); } //Call the FindSolution method if input is OK if (capacity != null && maxPerInterval != null && intervalsLeft != null) { ProblemState ps = Program.FindSolution(capacity, maxPerInterval, intervalsLeft); Program.DisplayResult(ps); Application.Run(new Visualization(ps)); } }
/// <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)); }
/// <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); } } }
/// <summary> /// Changes a position in a given state to create a new state /// the position is given as parameters /// </summary> /// <param name="x">Unit number</param> /// <param name="y">interval in which unit must be scheduled</param> /// <param name="z">current interval in which unit is scheduled</param> /// <returns></returns> public ProblemState Operation(int x, int y, int z) { ProblemState newState = new ProblemState(this.Capacity, this.MaxPerInterval, this.IntervalsLeft); for (int i = 0; i < Length; i++) { for (int j = 0; j < NumberOfIntervals; j++) { newState.Solution[i, j] = this.Solution[i, j]; } } newState.Solution[x, y] = 1; newState.Solution[x, z] = 0; return(newState); }
/// <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); }
/// <summary> /// Changes a position in a given state to create a new state /// the position is given as parameters /// </summary> /// <param name="x">Unit number</param> /// <param name="y">interval in which unit must be scheduled</param> /// <param name="z">current interval in which unit is scheduled</param> /// <returns></returns> public ProblemState Operation(int x, int y, int z) { ProblemState newState = new ProblemState(this.Capacity, this.MaxPerInterval, this.IntervalsLeft); for (int i = 0; i < Length; i++) { for (int j = 0; j < NumberOfIntervals; j++) newState.Solution[i, j] = this.Solution[i, j]; } newState.Solution[x, y] = 1; newState.Solution[x, z] = 0; return newState; }
/// <summary> /// Constructor accepts the ProblemState object to display it in chart /// </summary> /// <param name="ps"></param> public Visualization( ProblemState ps) { InitializeComponent(); DisplaySolution(ps); }
/// <summary> /// Constructor accepts the ProblemState object to display it in chart /// </summary> /// <param name="ps"></param> public Visualization(ProblemState ps) { InitializeComponent(); DisplaySolution(ps); }