/trunk/Schedule/Algorithm/GeneralAlgorithm.cs
http://thoikhoabieu.googlecode.com/ · C# · 319 lines · 260 code · 52 blank · 7 comment · 42 complexity · 5e6bf93b5b7393fe94543aa657a890ea MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- using Schedule;
-
- namespace Algorithm
- {
- public class GeneralAlgorithm
- {
- private ArrayList _localsearchs;
- // Tabu list
- public int BestCost; // BestCost = vo cung`
- private const int MaxCost = 10000000;
- private const int MaxMove = 35;
- private int point = 0;
-
-
- #region Tabu Data
- private ArrayList _tabuList;
- private Bcfc _bcfc;
-
- #endregion
-
- private double p = 0.35; // xac xuat dung cho Wsat
- private ArrayList _violate;
-
- public Bcfc BCFC
- {
- get { return _bcfc; }
- set { _bcfc = value; }
- }
-
- public ArrayList Localsearchs
- {
- get { return _localsearchs; }
- set { _localsearchs = value; }
- }
-
- // Add LocalSearch vao arraylist localsearchs
- public GeneralAlgorithm(Constraint constraint, Bcfc bcfc)
- {
- _localsearchs = new ArrayList();
- _violate = new ArrayList();
- _bcfc = bcfc;
- int i = 0;
- Variable v = (Variable)_bcfc.Variables[0];
- int maLop = v.Cl.Malop;
- _localsearchs.Add(new LocalSearch(maLop));
- _tabuList = new ArrayList();
-
- foreach (Variable variable in _bcfc.Variables)
- {
- if (maLop != variable.Cl.Malop)
- {
- maLop = variable.Cl.Malop;
- LocalSearch ls = new LocalSearch(maLop);
- ls.Variables.Add(variable);
- _localsearchs.Add(ls);
- ls.MaCn = variable.Cl.MaCn;
- i++;
- }
-
-
- else if (maLop == variable.Cl.Malop)
- {
- LocalSearch local = (LocalSearch) _localsearchs[i];
- local.Variables.Add(variable);
- }
-
- //them cac bien cung giao vien vao arraylist cua cac bien
-
-
- foreach (Variable var in _bcfc.Variables)
- {
- if (var.Teacher.MaGvien == variable.Teacher.MaGvien && var.Cl.Malop != variable.Cl.Malop)
- {
- variable.SameTeacherVariables.Add(var);
- }
- }
-
- }
-
-
- SoftConstraint softConstraint = new SoftConstraint();
-
- foreach (LocalSearch ls in _localsearchs)
- {
- Variable va = (Variable)ls.Variables[0];
- ls.Point = softConstraint.DanhGia(ls, va, va.Period);
- BestCost += ls.Point;
- }
-
- point = BestCost;
- }
-
- #region Tabu function
- public ArrayList GenerateLocalMovesTabu(int i, ref int totalMove)
- {
- LocalSearch ls = (LocalSearch)_localsearchs[i];
- ArrayList moves = new ArrayList();
- int danhgia;
-
- foreach (Variable variable in ls.Variables)
- {
- ls.GenerateNeighbour(variable); //update cac bien ke can cua variable
- int n = variable.Period;
- foreach (int m in variable.Neighbour)
- {
- int move = i * 10000 + n * 100 + m;
- danhgia = DanhGia(move);
-
-
- if (!_tabuList.Contains(move) && danhgia <= BestCost)
- {
- if (danhgia < BestCost)
- {
- BestCost = danhgia;
- moves.Clear();
- }
- moves.Add(move); // i: index cua local search, n tiet nguon, m tiet dich
- }
- }
- }
-
-
- return moves;
- }
-
- public void MakeLocalMovesTabu(ArrayList moves, ref int totalMove)
- {
- //move
-
- totalMove = totalMove + 1;
- int move = (int)moves[0];
- int index = move / 10000;
- int n = (move % 10000) / 100;
- int m = (move % 10000) % 100;
- LocalSearch ls = (LocalSearch)_localsearchs[index];
- ls.Swap(n, m);
-
- //Update tabulist
- _tabuList.Add(move);
- if (_tabuList.Count == 20)
- {
- _tabuList.RemoveAt(0);
- }
- }
-
- public void LocalSearchTabu()
- {
- int totalMove = 0;
- Random random = new Random();
- while (BestCost > MaxCost && totalMove < MaxMove)
- {
- int index = random.Next(0, _localsearchs.Count);
- ArrayList moves = GenerateLocalMovesTabu(index, ref totalMove);
- if (moves.Count != 0)
- MakeLocalMovesTabu(moves, ref totalMove);
- }
- }
- #endregion
-
- #region WSAT
- public ArrayList GenerateLocalMovesWsat(ref int totalMove)
- {
- LocalSearch ls;
- ArrayList moves = new ArrayList();
- int danhgia;
- Random random = new Random();
- int typeConstraint = random.Next(1, 5);
- double temp = random.NextDouble();
- GenerateViolatedLocalsearch(typeConstraint);
-
-
- if(p > temp)
- {
- while (moves.Count == 0)
- {
- if (_violate.Count == 0)
- break;
- int index = random.Next(0, _violate.Count);
- Variable v = (Variable) _violate[index];
-
- ls = SearchLs(v);
- ls.GenerateNeighbour(v);
- int neighbourIndex = random.Next(0, v.Neighbour.Count);
- if (v.Neighbour.Count == 0)
- break;
-
- int m = (int) v.Neighbour[neighbourIndex];
- int n = v.Period;
- int move = _localsearchs.IndexOf(ls)*10000 + n*100 + m;
-
- if (ls.IsImprove(move, typeConstraint))
- {
- moves.Add(move);
- }
- _violate.Remove(v);
- }
- }
- else
- {
- foreach (Variable variable in _violate)
- {
- ls = SearchLs(variable);
- ls.GenerateNeighbour(variable); //update cac bien ke can cua variable
- int n = variable.Period;
- foreach (int m in variable.Neighbour)
- {
- int move = _localsearchs.IndexOf(ls) * 10000 + n * 100 + m;
- danhgia = DanhGia(move);
-
-
- if (danhgia <= BestCost && ls.IsImprove(move, typeConstraint))
- {
- if (danhgia < BestCost)
- {
- BestCost = danhgia;
- moves.Clear();
- }
- moves.Add(move); // i: index cua local search, n tiet nguon, m tiet dich
- }
- }
- }
- }
-
- return moves;
- }
-
- public void MakeLocalMovesWsat(ArrayList moves, ref int totalMove)
- {
- //move
- Random random = new Random();
- int mv = random.Next(0, moves.Count);
- int move = (int)moves[mv];
- int danhgia = DanhGia(move);
- if (danhgia <= point || p > random.NextDouble())
- {
- int index = move / 10000;
- int n = (move % 10000) / 100;
- int m = (move % 10000) % 100;
- LocalSearch ls = (LocalSearch)_localsearchs[index];
- ls.Swap(n, m);
- point = danhgia;
- }
- totalMove = totalMove + 1;
- }
-
- public void LocalSearchWsat()
- {
- int totalMove = 0;
- Random random = new Random();
- while (BestCost > MaxCost && totalMove < MaxMove)
- {
- int index = random.Next(0, _localsearchs.Count);
- ArrayList moves = GenerateLocalMovesWsat(ref totalMove);
- if (moves.Count != 0)
- MakeLocalMovesWsat(moves, ref totalMove);
- }
- }
-
- private void GenerateViolatedLocalsearch(int typeConstraint)
- {
- foreach (LocalSearch ls in _localsearchs)
- {
- ls.GenerateViolateVariable(typeConstraint);
- _violate.AddRange(ls.ViolatedVariables);
- }
- }
- #endregion
-
-
- private int DanhGia(int move)
- {
- int index = move / 10000;
- int n = (move % 10000) / 100;
- int m = (move % 10000) % 100;
- int changePoint = 0;
- int point = 0;
-
- LocalSearch ls = (LocalSearch)_localsearchs[index];
- SoftConstraint sc = new SoftConstraint();
- Variable from = ls.GetVariable(n);
- Variable to = ls.GetVariable(m);
-
- //////
- if (from == null && to == null)
- {
- Console.Write("hehe");
- }
-
- changePoint = from == null ? sc.DanhGia(ls, to, n) : sc.DanhGia(ls, from, m);
-
-
-
- point = BestCost - ls.Point + changePoint;
- if (point < BestCost)
- {
- ls.Point = changePoint;
- }
-
- return point;
- }
-
- private LocalSearch SearchLs(Variable variable)
- {
- foreach (LocalSearch ls in _localsearchs)
- {
- if (ls.Variables.Contains(variable))
- {
- return ls;
- }
- }
-
- return null;
- }
- }
- }