static ProblemInstance[] SelectProblemInstances() { ProblemInstance[] Instances; if (Settings.PROBLEM_INSTANCES_TO_USE.Length > 0) { // Load instance names from the files // INSTANCENAMES = System.IO.Directory.GetFiles(INSTANCEFOLDER); // for (int _i = 0; _i < INSTANCENAMES.Length; _i++) // { // string[] temp = INSTANCENAMES[_i].Split('\\'); // INSTANCENAMES[_i] = temp[temp.Length - 1]; // } Instances = new ProblemInstance[Settings.PROBLEM_INSTANCES_TO_USE.Length]; //int InstancesFilledCounter = 0; for (int PI_id = 0; PI_id < Settings.PROBLEM_INSTANCES_TO_USE.Length; PI_id++) { Instances[PI_id] = new ProblemInstance(); Instances[PI_id].ReadFromFile(INSTANCEFOLDER + Settings.PROBLEM_INSTANCES_TO_USE[PI_id], Settings.PROBLEM_INSTANCES_TO_USE[PI_id]); } /* * for (int i = 0; i < Settings.PROBLEM_INSTANCES_TO_USE.Length + 1; i++) * { * foreach (int ID in Settings.PI_IDS_TO_USE) * { * if (i == ID) * { * Instances[InstancesFilledCounter] = new ProblemInstance(); * Instances[InstancesFilledCounter].ReadFromFile(INSTANCEFOLDER + INSTANCENAMES[i], INSTANCENAMES[i]); * InstancesFilledCounter++; * } * } * } */ } else { //Load block instances Instances = AllBlokInstances(); } return(Instances); }
static Schedule NewSchedule(ProblemInstance Ins, string AssignmentType, string StartTimeType) { Schedule Sched = new Schedule(Ins); switch (AssignmentType) { case "Random": Sched.MakeRandomAssignment(); break; case "RMA": Sched.MakeRollingMachineAssignment(); break; case "GLB": Sched.MakeGreedyLoadAssignment(); break; default: throw new Exception("AssignmentType string not one of allowed strings"); } Sched.StartTimeDescription = StartTimeType; switch (StartTimeType) { case "ESS": Sched.CalcESS(); Sched.SetESS(); Sched.MakeHTMLImage(string.Format("ESS {0} schedule for {1}", Sched.AssignmentDescription, Ins.Description)); break; case "LSS": Sched.CalcLSS(); Sched.SetLSS(); Sched.MakeHTMLImage(string.Format("LSS {0} schedule for {1}", Sched.AssignmentDescription, Ins.Description)); break; default: throw new Exception("StartTimeType string not one of the allowed strings"); } Sched.EstimateCmax(); // LocalSearch.MLS(200, Sched.Problem, FitnessFunctions.MeanBasedCmax, NeighborhoodFunctions.NeighborSwaps); return(Sched); }
public Schedule(ProblemInstance prob) { Problem = prob; PrecedenceDAG = Problem.DAG; Machines = new List <Machine>(prob.NMachines); for (int i = 0; i < prob.NMachines; i++) { Machines.Add(new Machine(i + 1, PrecedenceDAG.N)); } Starttimes = new double[PrecedenceDAG.N]; for (int i = 0; i < Starttimes.Length; i++) { Starttimes[i] = -1; } AssignedMachineID = new int[PrecedenceDAG.N]; LSS = new double[PrecedenceDAG.N]; ESS = new double[PrecedenceDAG.N]; // MachineArcPointers = new MachineArcPointer[PrecedenceDAG.N]; RMs = new List <RM>(); }
/// <summary> /// Returns the best schedule found after creating and performing LS on NRun schedules. /// </summary> /// <param name="NRuns">Number of schedules to create and improve to LO</param> /// <param name="Prob">Problem Instance</param> /// <param name="MakeAssignment">Method by which to create a new Schedule</param> /// <param name="FitnessFunction"></param> /// <param name="NeighborhoodOperator"></param> /// <returns></returns> static public List <Schedule> MLS(int NRuns, int NBestSchedules, ProblemInstance Prob, string AssignmentType, Func <Schedule, double> FitnessFunction, Func <Schedule, Func <Schedule, double>, Schedule> NeighborhoodOperator) { Console.WriteLine("Running MLS {0} for schedules with assingment type {1}...", NRuns, AssignmentType); List <Schedule> Top_N_Schedules = new List <Schedule>(NBestSchedules); //double BestFitness = -double.MaxValue; double CurrentFitness = -double.MaxValue; for (int i = 0; i < NRuns; i++) { // create random solution Schedule CurrentSchedule = new Schedule(Prob); switch (AssignmentType) { case "Random": CurrentSchedule.MakeRandomAssignment(); break; case "GLB": CurrentSchedule.MakeGreedyLoadAssignment(); break; case "RMA": CurrentSchedule.MakeRollingMachineAssignment(); break; default: throw new Exception("AssignmentType not recognized"); } CurrentSchedule.CalcESS(); CurrentSchedule.SetESS(); CurrentSchedule.AssignmentDescription += string.Format("MLS_run_{0}", i); //optimize it HillClimb(CurrentSchedule, NeighborhoodOperator, FitnessFunction); if (!CurrentSchedule.IsCritical(CurrentSchedule.PrecedenceDAG.GetJobById(0))) { throw new Exception("Weird stuff"); } CurrentFitness = FitnessFunction(CurrentSchedule); CurrentSchedule.LSFitness = CurrentFitness; //insert into list of best schedules if applicable int InsertPosition = Top_N_Schedules.Count; for (int listindex = Top_N_Schedules.Count - 1; listindex >= 0; listindex--) { //move the current schedule left until its fitness equals or is worse than that of its left neighbor if (Top_N_Schedules[listindex].LSFitness < CurrentFitness) { InsertPosition--; } else { break; } } //if the new schedule is one of the best NBest, add it to the list: if (InsertPosition < NBestSchedules) { Top_N_Schedules.Insert(InsertPosition, CurrentSchedule); //insert does not remove. //Remove the last schedule if list overflows if (Top_N_Schedules.Count > NBestSchedules) { Top_N_Schedules.RemoveAt(Top_N_Schedules.Count - 1); } } /* * if (CurrentFitness > BestFitness) * { * Console.WriteLine("MLS opt updated. New fitness: {0}",CurrentFitness); * BestFitness = CurrentFitness; * MLSOptimumSched = new Schedule(CurrentSchedule); // create a copy * }*/ if (i % 20 == 0) { Console.WriteLine("MLS at {0}/{1} ", i, NRuns); } Console.Write("."); } Console.WriteLine("MLS{0} optimal schedule with fitness {1}(check {2}):", NRuns, Top_N_Schedules[0].LSFitness, FitnessFunction(Top_N_Schedules[0])); Console.WriteLine("Nruns: {0}, List.Count: {1}, NBest: {2}", NRuns, Top_N_Schedules.Count, NBestSchedules); Top_N_Schedules[0].Print(); return(Top_N_Schedules); //return the best }
public static List <Schedule> ReadMLSSchedsFor(ProblemInstance PI, string AH, int Nruns, int NBest, string HF, bool CheckCompatibility) { // check if file exists: string FileName = String.Format("MLSSCheds_For_{0}_{1}_Top_{3}{4}.txt", PI.Description, string.Format("{0}MLS{1}", AH, Nruns), Nruns, NBest, HF); string path = String.Format("{0}{1}", Program.SCHEDULEFOLDER, FileName); using (StreamReader rf = new StreamReader(File.OpenRead(path))) { //check Params: string[] Params = rf.ReadLine().Split(); if (!CheckCompatibility) { Console.WriteLine("Warning... File compatibility not checked on loading MLS Schedule"); } if (!CheckCompatibility || (Params[0] == PI.Description && Params[1] == (AH + "MLS" + Nruns) && int.Parse(Params[2]) == Nruns && int.Parse(Params[3]) == NBest && Params[4] == HF) ) { //Loaded correct file int CurrentMachineID; List <Schedule> MLSScheduleList = new List <Schedule>(NBest); for (int i = 0; i < NBest; i++) { Schedule CurrentSchedule = new Schedule(PI); CurrentSchedule.LSFitness = double.Parse(rf.ReadLine()); for (int j = 0; j < PI.NMachines; j++) { string[] Line = rf.ReadLine().Split(); CurrentMachineID = int.Parse(Line[0]); for (int k = 1; k < Line.Length - 1; k++) { CurrentSchedule.AssignJobToMachineById(int.Parse(Line[k]), CurrentMachineID); } } CurrentSchedule.AssignmentDescription = AH; CurrentSchedule.CalcESS(); CurrentSchedule.SetESS(); CurrentSchedule.EstimateCmax(); CurrentSchedule.CalcRMs(); MLSScheduleList.Add(CurrentSchedule); Console.Write("."); } return(MLSScheduleList); } else { Console.WriteLine("ERROR: File Parameters do not match file name for file:"); Console.WriteLine(Params[0] == PI.Description); Console.WriteLine("{0} == {1}, {2}", Params[1], (AH + "MLS" + Nruns), Params[1] == (AH + "MLS" + Nruns)); Console.WriteLine(int.Parse(Params[2]) == Nruns); Console.WriteLine(int.Parse(Params[3]) == NBest); Console.WriteLine(Params[4] == HF); Console.WriteLine(path); throw new Exception("File Paramaters do not match file name!"); } } }