public static void Insert(List <SchedulingTask> tasks, int one, int two) { SchedulingTask temp = tasks.ElementAt(one); tasks.RemoveAt(one); tasks.Insert(two, temp); }
public static void Swap(List <SchedulingTask> tasks, int one, int two) { SchedulingTask temp = tasks[one]; tasks[one] = tasks[two]; tasks[two] = temp; }
public static List <SchedulingTask> SimulatedAnnealing(List <SchedulingTask> tasks, Action <List <SchedulingTask>, int, int> action, double u = 0.8, double T0 = 1000, bool probabilityMOD = false, bool cmaxMOD = false, bool isNeutral = false) { List <SchedulingTask> piZero; if (isNeutral) { piZero = tasks; } else { piZero = SPD_Lab2.Util <SchedulingTask> .NEHAccelerated(tasks).ToList(); } Random random = new Random(); double TEND = 0.0001; double probability; //double T0 = 1000; while (TEND < T0) { SchedulingTask[] temp = new SchedulingTask[tasks.Count]; List <SchedulingTask> piNext = new List <SchedulingTask>(); //Swap(piNext, one, two); do { piZero.CopyTo(temp); piNext = temp.ToList(); int one = random.Next(0, tasks.Count - 1); int two = random.Next(0, tasks.Count - 1); action(piNext, one, two); probability = CalculateProbability(piZero, piNext, T0, probabilityMOD, cmaxMOD); } while (probability < 0); double decide = random.NextDouble(); if (probability >= decide) { piZero = piNext; } T0 = Cooling(T0, u); } return(piZero); }
/// <summary> /// 添加任务计划 /// </summary> /// <returns></returns> public static async Task <RunResult> AddScheduleJob2(SchedulingTask taskModel) { var status = new RunResult(); try { scheduler = await GetScheduler(); //检查任务是否已存在 var jk = new JobKey(taskModel.TaskName, taskModel.TaskGroupName); //如果不存在 if (!await scheduler.CheckExists(jk)) { //使用Cron表达式计算触发间隔时间 var cronSchedule = taskModel.IntervalTime; Type jobType = Type.GetType(taskModel.TaskClassFullName); // 定义这个工作,并将其绑定到我们的IJob实现类 IJobDetail job = new JobDetailImpl(taskModel.TaskName, taskModel.TaskGroupName, jobType) { Description = taskModel.TaskDescription }; ITrigger trigger = TriggerBuilder.Create() .WithIdentity(taskModel.TriggerName, taskModel.TriggerGroupName) .StartAt((DateTimeOffset)taskModel.StartTime) //指定开始时间 .EndAt((DateTimeOffset)taskModel.EndTime) //指定结束时间 .WithCronSchedule(cronSchedule, action => action.WithMisfireHandlingInstructionDoNothing()) //使用Cron表达式 /* * withMisfireHandlingInstructionDoNothing * ——不触发立即执行 * ——等待下次Cron触发频率到达时刻开始按照Cron频率依次执行 * * withMisfireHandlingInstructionIgnoreMisfires * ——以错过的第一个频率时间立刻开始执行 * ——重做错过的所有频率周期后 * ——当下一次触发频率发生时间大于当前时间后,再按照正常的Cron频率依次执行 * * withMisfireHandlingInstructionFireAndProceed * ——以当前时间为触发频率立刻触发一次执行 * ——然后按照Cron频率依次执行 * */ .ForJob(taskModel.TaskName, taskModel.TaskGroupName) //通过JobKey识别作业 .Build(); // 告诉Quartz使用我们的触发器来安排作业 await scheduler.ScheduleJob(job, trigger); await scheduler.Start(); // await Task.Delay(TimeSpan.FromSeconds(5)); status.Status = 1; status.Msg = "任务计划运行成功"; } else // 如果存在,判断cron表达式是否相同,不同则更新 { var trigger = await scheduler.GetTrigger(new Quartz.TriggerKey(taskModel.TriggerName, taskModel.TriggerGroupName)); var cron = taskModel.IntervalTime; if (trigger != null) { var triggerImpl = trigger as CronTriggerImpl; if (triggerImpl.CronExpressionString != cron) { var jobDetail = await scheduler.GetJobDetail(triggerImpl.JobKey); // var t = jobDetail.GetType(); // JobDetailImpl var jobImpl = jobDetail as Quartz.Impl.JobDetailImpl; var newTrigger = TriggerBuilder.Create() .WithIdentity(taskModel.TriggerName, taskModel.TriggerGroupName) .StartAt(DateTimeOffset.UtcNow) //指定开始时间 .EndAt(taskModel.EndTime) //指定结束时间 .WithCronSchedule(cron, action => action.WithMisfireHandlingInstructionDoNothing()) //使用Cron表达式//第一次启动时不执行 .ForJob(jobDetail) //通过JobKey识别作业 .Build(); await scheduler.RescheduleJob(triggerImpl.Key, newTrigger); var firetime = newTrigger.GetNextFireTimeUtc(); var nexttime = firetime.HasValue ? firetime.Value.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") : "无"; status.Status = 1; status.Msg = $"调度任务[{taskModel.TaskName}]更新执行计划成功,下次执行时间:{nexttime}"; } else { status.Status = 2; status.Msg = $"调度任务[{taskModel.TaskName}]运行正常"; } } else { status.Status = 2; status.Msg = $"调度任务[{taskModel.TaskName}]运行正常"; } } } catch (Exception ex) { status.Status = 2; status.Msg = "任务计划运行异常,异常信息:" + ex.Message; } return(status); }
/// <summary> /// 添加任务计划 /// </summary> /// <returns></returns> public static async Task <RunResult> AddScheduleJob(SchedulingTask taskModel) { var status = new RunResult(); try { scheduler = await GetScheduler(); //检查任务是否已存在 var jk = new JobKey(taskModel.TaskName, taskModel.TaskGroupName); //如果不存在 if (!await scheduler.CheckExists(jk)) { //使用Cron表达式计算触发间隔时间 var cronSchedule = CronCommon.CronSchedule(taskModel.IntervalTime); Type jobType = Type.GetType(taskModel.TaskClassFullName); // 定义这个工作,并将其绑定到我们的IJob实现类 IJobDetail job = new JobDetailImpl(taskModel.TaskName, taskModel.TaskGroupName, jobType) { Description = taskModel.TaskDescription }; ITrigger trigger = TriggerBuilder.Create() .WithIdentity(taskModel.TriggerName, taskModel.TriggerGroupName) .StartAt((DateTimeOffset)taskModel.StartTime) //指定开始时间 .EndAt((DateTimeOffset)taskModel.EndTime) //指定结束时间 .WithCronSchedule(cronSchedule, action => action.WithMisfireHandlingInstructionDoNothing()) //使用Cron表达式 /* * withMisfireHandlingInstructionDoNothing * ——不触发立即执行 * ——等待下次Cron触发频率到达时刻开始按照Cron频率依次执行 * * withMisfireHandlingInstructionIgnoreMisfires * ——以错过的第一个频率时间立刻开始执行 * ——重做错过的所有频率周期后 * ——当下一次触发频率发生时间大于当前时间后,再按照正常的Cron频率依次执行 * * withMisfireHandlingInstructionFireAndProceed * ——以当前时间为触发频率立刻触发一次执行 * ——然后按照Cron频率依次执行 * */ .ForJob(taskModel.TaskName, taskModel.TaskGroupName) //通过JobKey识别作业 .Build(); // 告诉Quartz使用我们的触发器来安排作业 await scheduler.ScheduleJob(job, trigger); await scheduler.Start(); // await Task.Delay(TimeSpan.FromSeconds(5)); status.Status = 1; status.Msg = "任务计划运行成功"; } else { status.Status = 1; status.Msg = "任务计划运行正常"; } } catch (Exception ex) { status.Status = 2; status.Msg = "任务计划运行异常,异常信息:" + ex.Message; } return(status); }
static void Main(string[] args) { string dataFileName = "DataSet.txt"; const string resultFileName = "SwapVsInsert.txt"; const string resultFileName2 = "ParamU.txt"; const string resultFileName3 = "ParamT0.txt"; const string resultFileName4 = "ProbMOD.txt"; int minSpanSwap = 0; int minSpanInsert = 0; int amountOfMachines = 20, amountOfTasks = 500; SchedulingTask[] tasks, tasks2 = new SchedulingTask[500]; List <SchedulingTask> result; try { // SwapVsInsert //using (StreamWriter swr = new StreamWriter(resultFileName)) //{ // swr.WriteLine("indeks \t Swap \t Insert"); // int s = 0; // int i = 0; // for (int j = 1; j < 10001; j++) // { // SPD_Lab2.Util<SchedulingTask>.CreateRandomDataSet(dataFileName, amountOfTasks, amountOfMachines); // tasks = SPD_Lab2.Util<SchedulingTask>.SeedData(out int t, out int m, dataFileName); // tasks.CopyTo(tasks2, 0); // result = SPD_Lab3.Util<SchedulingTask>.SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util<SchedulingTask>.Swap); // minSpanSwap = SPD_Lab2.Util<SchedulingTask>.CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); // result = SPD_Lab3.Util<SchedulingTask>.SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util<SchedulingTask>.Insert); // minSpanInsert = SPD_Lab2.Util<SchedulingTask>.CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); // swr.WriteLine($"{j} \t {minSpanSwap} \t {minSpanInsert}"); // if (minSpanInsert < minSpanSwap) i++; // else s++; // } // swr.WriteLine($"Swap: {s} \t Insert: {i}"); //} //parametr u //double[] u = { 0.8, 0.9, 0.95, 0.99, 0.9999 }; //int[] best = { 0, 0, 0, 0, 0 }; //using (StreamWriter swr = new StreamWriter(resultFileName2)) //{ // swr.WriteLine("indeks \t 0.8 \t 0.9 \t 0.95 \t 0.99 \t 0.9999 \t NEHacc"); // for (int j = 1; j < 11; j++) // { // string r = $"{j}"; // SPD_Lab2.Util<SchedulingTask>.CreateRandomDataSet(dataFileName, amountOfTasks, amountOfMachines); // tasks = SPD_Lab2.Util<SchedulingTask>.SeedData(out int t, out int m, dataFileName); // int bestValue = int.MaxValue; // int whichBest = -1; // for (int k = 0; k < u.Length; k++) // { // result = SPD_Lab3.Util<SchedulingTask>.SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util<SchedulingTask>.Swap, u[k]); // int p = SPD_Lab2.Util<SchedulingTask>.CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); // r += $" \t {p}"; // if (p < bestValue) { bestValue = p; whichBest = k; } // } // best[whichBest]++; // r += $" \t {SPD_Lab2.Util<SchedulingTask>.CalculateSpanC2(SPD_Lab2.Util<SchedulingTask>.NEHAccelerated(tasks.ToList()).ToArray(), 0)}"; // swr.WriteLine(r); // } // string x = "\t"; // foreach (var y in best) // { // x += $"{y} \t"; // } // swr.WriteLine(x); //} // parametr T0 //double[] T0 = { 0.0001, 0.001, 0.01, 0.1, 1 }; //int[] spanSum = { 0, 0, 0, 0, 0 }; //using (StreamWriter swr = new StreamWriter(resultFileName3)) //{ // swr.WriteLine("indeks \t 0.0001 \t 0.001 \t 0.01 \t 0.1 \t 1"); // SPD_Lab2.Util<SchedulingTask>.CreateRandomDataSet(dataFileName, amountOfTasks, amountOfMachines); // tasks = SPD_Lab2.Util<SchedulingTask>.SeedData(out int t, out int m, dataFileName); // for (int j = 0; j < 10; j++) // { // //string r = $"{j}"; // for (int k = 0; k < T0.Length; k++) // { // result = SPD_Lab3.Util<SchedulingTask>.SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util<SchedulingTask>.Swap, T0[j]); // int p = SPD_Lab2.Util<SchedulingTask>.CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); // //r += $" \t {p}"; // spanSum[k] += p; // } // //r += $" \t {SPD_Lab2.Util<SchedulingTask>.CalculateSpanC2(SPD_Lab2.Util<SchedulingTask>.NEHAccelerated(tasks.ToList()).ToArray(), 0)}"; // //swr.WriteLine(r); // } // string XD = "\t"; // foreach (var x in spanSum) // { // XD += $"{x / 10} \t"; // } // XD += $" \t {SPD_Lab2.Util<SchedulingTask>.CalculateSpanC2(SPD_Lab2.Util<SchedulingTask>.NEHAccelerated(tasks.ToList()).ToArray(), 0)}"; // swr.WriteLine(XD); //} //probMOD //bool probMOD = false; //using (StreamWriter swr = new StreamWriter(resultFileName4)) //{ // swr.WriteLine(" nie \t tak "); // amountOfMachines = 5; // for (int s = 0; s < 5; s++) // { // int[] spanSum = { 0, 0 }; // amountOfTasks += 5; // SPD_Lab2.Util<SchedulingTask>.CreateRandomDataSet(dataFileName, amountOfTasks, amountOfMachines); // tasks = SPD_Lab2.Util<SchedulingTask>.SeedData(out int t, out int m, dataFileName); // for (int j = 0; j < 10; j++) // { // for (int k = 0; k < spanSum.Length; k++) // { // result = SPD_Lab3.Util<SchedulingTask>.SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util<SchedulingTask>.Swap, probabilityMOD: probMOD); // int p = SPD_Lab2.Util<SchedulingTask>.CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); // spanSum[k] += p; // probMOD = !probMOD; // } // } // string XD = "\t"; // foreach (var x in spanSum) // { // XD += $"{x / 10} \t"; // } // swr.WriteLine(XD); // } //} //mod z Cmax //bool cmaxMOD = false; //using (StreamWriter swr = new StreamWriter(resultFileName4)) //{ // swr.WriteLine(" nie \t tak "); // amountOfTasks = 100; // for (int s = 0; s < 5; s++) // { // int[] spanSum = { 0, 0 }; // amountOfTasks += 50; // SPD_Lab2.Util<SchedulingTask>.CreateRandomDataSet(dataFileName, amountOfTasks, amountOfMachines); // tasks = SPD_Lab2.Util<SchedulingTask>.SeedData(out int t, out int m, dataFileName); // for (int j = 0; j < 10; j++) // { // for (int k = 0; k < spanSum.Length; k++) // { // result = SPD_Lab3.Util<SchedulingTask>.SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util<SchedulingTask>.Swap, cmaxMOD: cmaxMOD); // int p = SPD_Lab2.Util<SchedulingTask>.CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); // spanSum[k] += p; // cmaxMOD = !cmaxMOD; // } // } // string XD = "\t"; // foreach (var x in spanSum) // { // XD += $"{x / 10} \t"; // } // swr.WriteLine(XD); // } //} // ostatni //int p,p2; //SPD_Lab2.Util<SchedulingTask>.CreateRandomDataSet(dataFileName, amountOfTasks, amountOfMachines); //tasks = SPD_Lab2.Util<SchedulingTask>.SeedData(out int t, out int m, dataFileName); //result = SPD_Lab3.Util<SchedulingTask>.SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util<SchedulingTask>.Swap,isNeutral:true); //p = SPD_Lab2.Util<SchedulingTask>.CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); //result = SPD_Lab3.Util<SchedulingTask>.SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util<SchedulingTask>.Swap); //p2 = SPD_Lab2.Util<SchedulingTask>.CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); //Console.WriteLine($"Neutral: {p} \t Neh: {p2}"); // best int p, p2; SPD_Lab2.Util <SchedulingTask> .CreateRandomDataSet("ta.txt", amountOfTasks, amountOfMachines); tasks = SPD_Lab2.Util <SchedulingTask> .SeedData(out int t, out int m, dataFileName); result = SPD_Lab3.Util <SchedulingTask> .SimulatedAnnealing(tasks.ToList(), SPD_Lab3.Util <SchedulingTask> .Swap, u : 0.9999, probabilityMOD : true); var result2 = SPD_Lab2.Util <SchedulingTask> .NEHAccelerated(tasks.ToList()); p = SPD_Lab2.Util <SchedulingTask> .CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result); p2 = SPD_Lab2.Util <SchedulingTask> .CalculateSpanC(tasks.Length, tasks[0].TimeOnMachine.Length, result2); Console.WriteLine($"SA: {p} \t Neh: {p2}"); } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadKey(); } }