public Task[] StartSolving(int maxIterations, int opCount, int maxNoChange, int maxNoChangeAdd) { top10Schedules = new Schedule[10]; LocalSolver solver = new HillClimbLocalSolver(); //LocalSolver solver = new SaLocalSolver(0.5, 0.9999); ///* var tasks = new Task[threads]; int i = 0; while (i < threads) { int index = i; tasks[index] = Task.Factory.StartNew(() => DoSolving(startSchedules[index], maxIterations, opCount, maxNoChange, maxNoChangeAdd, solver)); //tasks[i] = Task.Run(() => DoSolving(startSchedules[i], 0, maxIterations, opCount, 0, maxNoChange)); i++; } return(tasks); }
void DoSolving(Schedule state, int maxIterations, int opCount, int maxNoChange, int maxNochangeAdd, LocalSolver solver) { Console.WriteLine("Adding..."); LocalSolver hc = new HillClimbLocalSolver(); hc.Init(); int noChange = 0; for (int iter = 0; iter < maxIterations; iter++) { List <NeighborResult> results = new List <NeighborResult>(opCount); for (int i = 0; i < opCount; i++) { Func <Schedule, NeighborResult> op = Schedule.addOperator; NeighborResult res = op(state); // <- { AddResult, ImpossibleResult } results.Add(res); } if (!hc.ApplyAccordingly(results)) { noChange++; } else { noChange = 0; } if (noChange > maxNochangeAdd) { break; } } Console.WriteLine($"Adding done. Result: {state.ToString()}"); //Console.ReadKey(); /* * Console.WriteLine("Starting Transfering..."); * solver.Init(true); * * int[] distro = { 1, 1, 1 }; * List<Func<Schedule, NeighborResult>> funcs = new List<Func<Schedule, NeighborResult>>(); * for (int i = 0; i < Schedule.neighborOperators.Length; i++) * { * for (int j = 0; j < distro[i]; j++) * { * funcs.Add(Schedule.neighborOperators[i]); * } * } * Random rnd = new Random(); * noChange = 0; * for (int iter = 0; iter < maxIterations; iter++) * { * if(iter % 1000 == 0) Console.WriteLine($"Iteration {iter}"); * List<NeighborResult> results = new List<NeighborResult>(opCount); * for(int op = 0; op < opCount; op++) * { * //Func<Schedule, NeighborResult> func = Schedule.transferOperator;// funcs[rnd.Next(0, funcs.Count)]; * Func<Schedule, NeighborResult> func = funcs[rnd.Next(0, funcs.Count)]; * NeighborResult res = func(state); * results.Add(res); * } * if (!solver.ApplyAccordingly(results)) * { * noChange++; * } * else * { * noChange = 0; * } * if (noChange > maxNoChange) * { * Console.WriteLine("Terminated due to noChange"); * break; * } * //Console.ReadKey(); * } * Console.WriteLine($"between done. Result: {state.ToString()}"); * Console.WriteLine("Adding..."); * hc.Init(); * noChange = 0; * for (int iter = 0; iter < maxIterations; iter++) * { * List<NeighborResult> results = new List<NeighborResult>(opCount); * for (int i = 0; i < opCount; i++) * { * Func<Schedule, NeighborResult> op = Schedule.addOperator; * NeighborResult res = op(state); // <- { AddResult, ImpossibleResult } * results.Add(res); * } * if (!hc.ApplyAccordingly(results)) * { * noChange++; * } * else noChange = 0; * if (noChange > maxNochangeAdd) * { * break; * } * } * Console.WriteLine($"Adding done. Result: {state.ToString()}"); */ lock (addlock) { AddScheduleToTop(state); } }