/// <summary> /// تابع سازنده کلاس که شروع به حل مساله میکند /// </summary> /// <param name="input">نام فایل ورودی</param> /// <param name="output">نام فایل خروجی</param> public tabuSolver(string input, string output) { pomov = 0; impomov = 0; try { IReader = new StreamReader(input); OWriter = new StreamWriter(output); logWriter = new StreamWriter("log_" + output); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("press any to exit..."); Console.Read(); return; } DateTime startRuntime = new DateTime(); startRuntime = DateTime.Now; readInput(out prblm);//Algorithm 3 line 1 Xi = 0; theta = theta0 = 10; Eta = EtaMin = 4; EtaMax = 15; landa = 0.3; simpleSwapTabuList = new List <simpleMove>(); kempeSwapTabuList = new List <kempeSwap>(); X0.initSol(prblm); int[] pos = new int[2]; do { int ci = X0.HR1(); pos = X0.HR2(ci); X0.insertLecture(ci, pos[0], pos[1]); //OWriter.WriteLine(prblm.courses[ci].CourseID + " " + prblm.Rooms[pos[1]].ID + " " + pos[0] / prblm.Periods_per_day + " " + pos[0] % prblm.Periods_per_day); logWriter.WriteLine("ci:" + ci + " " + prblm.courses[ci].CourseID + " " + prblm.Rooms[pos[1]].ID + " " + pos[0] / prblm.Periods_per_day + " " + pos[0] % prblm.Periods_per_day); Console.WriteLine("ci:" + ci + " " + prblm.courses[ci].CourseID + " " + prblm.Rooms[pos[1]].ID + " " + pos[0] / prblm.Periods_per_day + " " + pos[0] % prblm.Periods_per_day); } while (X0.LC.Count > 0 && pos[0] != -1);//end of part1: initialization poindex = 0; Xstar = new solution(); solutionCopier.copy(X0, out Xstar); solutionCopier.copy(TS(X0, theta), out Xstar); int loopCount1 = 10; int loopCount2 = 20; int tbcouner = 0; do { loopCount1 = 10; solutionCopier.copy(perturb(Xstar, Eta), out Xprime); solutionCopier.copy(TS(Xprime, theta), out XstarPrime); double XstarPrime_Cost = validator.getCost(prblm, XstarPrime); double Xstar_Cost = validator.getCost(prblm, Xstar); if (XstarPrime_Cost < Xstar_Cost + 2) { do { theta = (int)(1.6 * theta0); solutionCopier.copy(TS(Xprime, theta), out XstarPrime); } while (loopCount1-- > 0); } if (validator.getCost(prblm, XstarPrime) < validator.getCost(prblm, Xstar)) { solutionCopier.copy(XstarPrime, out Xstar); theta = theta0; Eta = EtaMin; } else { theta = theta0; Xi++; Eta = Math.Max((int)(EtaMin + landa * Xi), EtaMax); } Console.WriteLine("tabu list count = {0}", simpleSwapTabuList.Count); Console.WriteLine("cost = {0}", validator.getCost(prblm, Xstar)); logWriter.WriteLine("tabu list count = {0}", simpleSwapTabuList.Count); logWriter.WriteLine("cost = {0}", validator.getCost(prblm, Xstar)); if (tbcouner != 0 && tbcouner == simpleSwapTabuList.Count) { loopCount2 = 0; } else { tbcouner = simpleSwapTabuList.Count; } } while (loopCount2-- > 0); for (int t = 0; t < Xstar.timeTable.Length; t++) { for (int r = 0; r < Xstar.timeTable[0].Length; r++) { course c = Xstar.timeTable[t][r]; if (c.CourseID != null) { OWriter.WriteLine(c.CourseID + " " + prblm.Rooms[r].ID + " " + t / prblm.Periods_per_day + " " + t % prblm.Periods_per_day); } } } OWriter.Close(); DateTime endRuntime = new DateTime(); endRuntime = DateTime.Now; Console.WriteLine("{0} possible moves \n{1} impossible move \n", pomov, impomov); Console.WriteLine("run time = {0}", endRuntime - startRuntime); logWriter.WriteLine("{0} possible moves \n {1} impossible move \n", pomov, impomov); logWriter.WriteLine("run time = {0}", endRuntime - startRuntime); logWriter.Close(); Console.WriteLine("press any to exit..."); Console.Read(); }
public void initSol(problemInstance P) { this.p = P; timeTable = new course[p.Periods_per_day * p.Days][]; for (int i = 0; i < p.Periods_per_day * p.Days; i++) { timeTable[i] = new course[p.Rooms.Length]; } LC = new Dictionary<int, course>(); for (int i = 0; i < p.courses.Length; i++) { LC.Add(i, p.courses[i]);//Algorithm 1 line 3: init LC } nl = new int[p.courses.Length]; nr = new int[p.courses.Length]; nd = new int[p.courses.Length]; for (int i = 0; i < p.courses.Length; i++) { nl[i] = p.courses[i].Lectures;//init nl nr[i] = 0; nd[i] = 0; } initApd(); initArm(); initAps(); initConstraints(); }
public bool swap(solution X, int[] swap) { course c1 = X.timeTable[swap[0]][swap[1]]; course c2 = X.timeTable[swap[2]][swap[3]]; if (c1.CourseID != null)//mojaz bodan enteqal ra barasi kon { if (!isMoveAv(X, c1.CourseID, swap[2], swap[3])) { // Console.WriteLine("{0} can't move to {1}",c1.CourseID,swap[2]); impomov++; return(false); } } if (c2.CourseID != null)//mojaz bodan enteqal ra barasi kon { if (!isMoveAv(X, c2.CourseID, swap[0], swap[1])) { // Console.WriteLine("{0} can't move to {1}", c2.CourseID, swap[0]); impomov++; return(false); } } pomov++; X.timeTable[swap[0]][swap[1]] = c2; X.timeTable[swap[2]][swap[3]] = c1; int c1Index, c2Index; c1Index = c2Index = 0; for (int i = 0; i < prblm.courses.Length; i++) { if (prblm.courses[i].CourseID == c1.CourseID) { c1Index = i; } if (prblm.courses[i].CourseID == c2.CourseID) { c2Index = i; } } if (c1.CourseID != null) { simpleMove m1 = new simpleMove(); m1.courseId = c1.CourseID; m1.t = swap[2]; m1.r = swap[3]; X.setNr(c1.CourseID); X.setNd(c1Index); simpleSwapTabuList.Add(m1); } if (c2.CourseID != null) { simpleMove m2 = new simpleMove(); m2.courseId = c2.CourseID; m2.t = swap[0]; m2.r = swap[1]; X.setNr(c2.CourseID); X.setNd(c2Index); simpleSwapTabuList.Add(m2); } return(true); }