PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Schedule/Algorithm/GeneralAlgorithm.cs

http://thoikhoabieu.googlecode.com/
C# | 319 lines | 260 code | 52 blank | 7 comment | 42 complexity | 5e6bf93b5b7393fe94543aa657a890ea MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using Schedule;
  6. namespace Algorithm
  7. {
  8. public class GeneralAlgorithm
  9. {
  10. private ArrayList _localsearchs;
  11. // Tabu list
  12. public int BestCost; // BestCost = vo cung`
  13. private const int MaxCost = 10000000;
  14. private const int MaxMove = 35;
  15. private int point = 0;
  16. #region Tabu Data
  17. private ArrayList _tabuList;
  18. private Bcfc _bcfc;
  19. #endregion
  20. private double p = 0.35; // xac xuat dung cho Wsat
  21. private ArrayList _violate;
  22. public Bcfc BCFC
  23. {
  24. get { return _bcfc; }
  25. set { _bcfc = value; }
  26. }
  27. public ArrayList Localsearchs
  28. {
  29. get { return _localsearchs; }
  30. set { _localsearchs = value; }
  31. }
  32. // Add LocalSearch vao arraylist localsearchs
  33. public GeneralAlgorithm(Constraint constraint, Bcfc bcfc)
  34. {
  35. _localsearchs = new ArrayList();
  36. _violate = new ArrayList();
  37. _bcfc = bcfc;
  38. int i = 0;
  39. Variable v = (Variable)_bcfc.Variables[0];
  40. int maLop = v.Cl.Malop;
  41. _localsearchs.Add(new LocalSearch(maLop));
  42. _tabuList = new ArrayList();
  43. foreach (Variable variable in _bcfc.Variables)
  44. {
  45. if (maLop != variable.Cl.Malop)
  46. {
  47. maLop = variable.Cl.Malop;
  48. LocalSearch ls = new LocalSearch(maLop);
  49. ls.Variables.Add(variable);
  50. _localsearchs.Add(ls);
  51. ls.MaCn = variable.Cl.MaCn;
  52. i++;
  53. }
  54. else if (maLop == variable.Cl.Malop)
  55. {
  56. LocalSearch local = (LocalSearch) _localsearchs[i];
  57. local.Variables.Add(variable);
  58. }
  59. //them cac bien cung giao vien vao arraylist cua cac bien
  60. foreach (Variable var in _bcfc.Variables)
  61. {
  62. if (var.Teacher.MaGvien == variable.Teacher.MaGvien && var.Cl.Malop != variable.Cl.Malop)
  63. {
  64. variable.SameTeacherVariables.Add(var);
  65. }
  66. }
  67. }
  68. SoftConstraint softConstraint = new SoftConstraint();
  69. foreach (LocalSearch ls in _localsearchs)
  70. {
  71. Variable va = (Variable)ls.Variables[0];
  72. ls.Point = softConstraint.DanhGia(ls, va, va.Period);
  73. BestCost += ls.Point;
  74. }
  75. point = BestCost;
  76. }
  77. #region Tabu function
  78. public ArrayList GenerateLocalMovesTabu(int i, ref int totalMove)
  79. {
  80. LocalSearch ls = (LocalSearch)_localsearchs[i];
  81. ArrayList moves = new ArrayList();
  82. int danhgia;
  83. foreach (Variable variable in ls.Variables)
  84. {
  85. ls.GenerateNeighbour(variable); //update cac bien ke can cua variable
  86. int n = variable.Period;
  87. foreach (int m in variable.Neighbour)
  88. {
  89. int move = i * 10000 + n * 100 + m;
  90. danhgia = DanhGia(move);
  91. if (!_tabuList.Contains(move) && danhgia <= BestCost)
  92. {
  93. if (danhgia < BestCost)
  94. {
  95. BestCost = danhgia;
  96. moves.Clear();
  97. }
  98. moves.Add(move); // i: index cua local search, n tiet nguon, m tiet dich
  99. }
  100. }
  101. }
  102. return moves;
  103. }
  104. public void MakeLocalMovesTabu(ArrayList moves, ref int totalMove)
  105. {
  106. //move
  107. totalMove = totalMove + 1;
  108. int move = (int)moves[0];
  109. int index = move / 10000;
  110. int n = (move % 10000) / 100;
  111. int m = (move % 10000) % 100;
  112. LocalSearch ls = (LocalSearch)_localsearchs[index];
  113. ls.Swap(n, m);
  114. //Update tabulist
  115. _tabuList.Add(move);
  116. if (_tabuList.Count == 20)
  117. {
  118. _tabuList.RemoveAt(0);
  119. }
  120. }
  121. public void LocalSearchTabu()
  122. {
  123. int totalMove = 0;
  124. Random random = new Random();
  125. while (BestCost > MaxCost && totalMove < MaxMove)
  126. {
  127. int index = random.Next(0, _localsearchs.Count);
  128. ArrayList moves = GenerateLocalMovesTabu(index, ref totalMove);
  129. if (moves.Count != 0)
  130. MakeLocalMovesTabu(moves, ref totalMove);
  131. }
  132. }
  133. #endregion
  134. #region WSAT
  135. public ArrayList GenerateLocalMovesWsat(ref int totalMove)
  136. {
  137. LocalSearch ls;
  138. ArrayList moves = new ArrayList();
  139. int danhgia;
  140. Random random = new Random();
  141. int typeConstraint = random.Next(1, 5);
  142. double temp = random.NextDouble();
  143. GenerateViolatedLocalsearch(typeConstraint);
  144. if(p > temp)
  145. {
  146. while (moves.Count == 0)
  147. {
  148. if (_violate.Count == 0)
  149. break;
  150. int index = random.Next(0, _violate.Count);
  151. Variable v = (Variable) _violate[index];
  152. ls = SearchLs(v);
  153. ls.GenerateNeighbour(v);
  154. int neighbourIndex = random.Next(0, v.Neighbour.Count);
  155. if (v.Neighbour.Count == 0)
  156. break;
  157. int m = (int) v.Neighbour[neighbourIndex];
  158. int n = v.Period;
  159. int move = _localsearchs.IndexOf(ls)*10000 + n*100 + m;
  160. if (ls.IsImprove(move, typeConstraint))
  161. {
  162. moves.Add(move);
  163. }
  164. _violate.Remove(v);
  165. }
  166. }
  167. else
  168. {
  169. foreach (Variable variable in _violate)
  170. {
  171. ls = SearchLs(variable);
  172. ls.GenerateNeighbour(variable); //update cac bien ke can cua variable
  173. int n = variable.Period;
  174. foreach (int m in variable.Neighbour)
  175. {
  176. int move = _localsearchs.IndexOf(ls) * 10000 + n * 100 + m;
  177. danhgia = DanhGia(move);
  178. if (danhgia <= BestCost && ls.IsImprove(move, typeConstraint))
  179. {
  180. if (danhgia < BestCost)
  181. {
  182. BestCost = danhgia;
  183. moves.Clear();
  184. }
  185. moves.Add(move); // i: index cua local search, n tiet nguon, m tiet dich
  186. }
  187. }
  188. }
  189. }
  190. return moves;
  191. }
  192. public void MakeLocalMovesWsat(ArrayList moves, ref int totalMove)
  193. {
  194. //move
  195. Random random = new Random();
  196. int mv = random.Next(0, moves.Count);
  197. int move = (int)moves[mv];
  198. int danhgia = DanhGia(move);
  199. if (danhgia <= point || p > random.NextDouble())
  200. {
  201. int index = move / 10000;
  202. int n = (move % 10000) / 100;
  203. int m = (move % 10000) % 100;
  204. LocalSearch ls = (LocalSearch)_localsearchs[index];
  205. ls.Swap(n, m);
  206. point = danhgia;
  207. }
  208. totalMove = totalMove + 1;
  209. }
  210. public void LocalSearchWsat()
  211. {
  212. int totalMove = 0;
  213. Random random = new Random();
  214. while (BestCost > MaxCost && totalMove < MaxMove)
  215. {
  216. int index = random.Next(0, _localsearchs.Count);
  217. ArrayList moves = GenerateLocalMovesWsat(ref totalMove);
  218. if (moves.Count != 0)
  219. MakeLocalMovesWsat(moves, ref totalMove);
  220. }
  221. }
  222. private void GenerateViolatedLocalsearch(int typeConstraint)
  223. {
  224. foreach (LocalSearch ls in _localsearchs)
  225. {
  226. ls.GenerateViolateVariable(typeConstraint);
  227. _violate.AddRange(ls.ViolatedVariables);
  228. }
  229. }
  230. #endregion
  231. private int DanhGia(int move)
  232. {
  233. int index = move / 10000;
  234. int n = (move % 10000) / 100;
  235. int m = (move % 10000) % 100;
  236. int changePoint = 0;
  237. int point = 0;
  238. LocalSearch ls = (LocalSearch)_localsearchs[index];
  239. SoftConstraint sc = new SoftConstraint();
  240. Variable from = ls.GetVariable(n);
  241. Variable to = ls.GetVariable(m);
  242. //////
  243. if (from == null && to == null)
  244. {
  245. Console.Write("hehe");
  246. }
  247. changePoint = from == null ? sc.DanhGia(ls, to, n) : sc.DanhGia(ls, from, m);
  248. point = BestCost - ls.Point + changePoint;
  249. if (point < BestCost)
  250. {
  251. ls.Point = changePoint;
  252. }
  253. return point;
  254. }
  255. private LocalSearch SearchLs(Variable variable)
  256. {
  257. foreach (LocalSearch ls in _localsearchs)
  258. {
  259. if (ls.Variables.Contains(variable))
  260. {
  261. return ls;
  262. }
  263. }
  264. return null;
  265. }
  266. }
  267. }