Giải quyết bài toán

Một phần của tài liệu Tìm hiểu và nghiên cứu thuật toán hệ kiến max min (Trang 55 - 69)

Cài đặt thuật toán

Đồ thị (Sơ đồ giao thông) đƣợc lƣu trữ dƣới dạng file text (.tsp) có cấu trúc nhƣ sau:Hàng đầu tiên bao gồm có số đỉnh (n) và số cạnh (m) của đồ thị, m các hàng tiếp theo là hai điểm đầu mút của mỗi cạnh và độ dài tƣơng ứng của các cạnh.

Đồ thị đƣợc lƣu trữ dƣới dạng ma trận kề và đƣợc cập nhật vào chƣơng trình bởi phƣơng thức LoadGraph().

Phƣơng thức Init() là phƣơng thức khởi tạo các giá trị vết mùi và đặt giá trị cho các tham số.

-56-

Sau mỗi lần chọn đỉnh tiếp theo trong hành trình của một con kiến thì, mảng đánh dấu các đỉnh chƣa thăm, tổng xác suất, xác xuất lựa chọn đỉnh tiếp theo… đƣợc tính lại bởi phƣơng thức CalcP().

Đỉnh tiếp theo của con kiến sẽ đƣợc chọn dựa trên nguyên lý bánh xe sổ xố với phƣơng thức Lottery_Wheel().

Sự cập nhật mùi đƣợc thực hiện nhờ phƣơng thức Pheromone_Update().

-57-

Giao diện của chƣơng trình

Form tạo file test

-58-

KẾT LUẬN VÀ HƢỚNG PHÁT TRIỂN

Các nghiên cứu gần đây trên các thuật toán ACO tập trung nhiều vào cải tiến hiệu suất của thuật toán. Trong các thuật toán ACO nhƣ hệ kiến, hệ đàn kiến, hệ kiến MAX-MINthì hiện nay hệ kiến MAX-MIN vẫn đƣợc ƣa dùng hơn cả bởi tính đơn giản, dễ cài đặt của nó.

Sau một thời gian tìm hiểu và nghiên cứu em đã đạt đƣợc những kết quả sau:

- Tìm hiểu đƣợc lý thuyết về đồ thị và một số nguyên lý áp dụng sử dụng trong giải bài toán TSP.

- Hiểu đƣợc thuật toán hệ kiến, và thuật toán hệ kiến cập nhật mùi theo tƣ tƣởng Max – Min.

- Nghiên cứu và áp dụng đƣợc thuật toán hệ kiến Max – Min xây dựng chƣơng trình giải bài toán TSP.

Trong bài khóa luận này, trình bày một cách hệ thống của các thuật toán hệ kiến và giới thiệu thuật toán hệ kiến MAX-MIN nhằm mục đích tạo cho các độc giả quan tâm đến các thuật toán ACO có cái nhìn tổng quan hơn về thuật toán hệ kiến-thuật toán nổi trội hơn so với các thuật toán mô phỏng tự nhiên giải quyết các bài toán tối ƣu tổ hợp hiện nay.

Hiện nay, các thuật toán ACO cho thấy có hiệu suất triển vọng trên các bài toán tối ƣu tổ hợp. Trong tƣơng lai em sẽ nghiên cứu hệ kiến MAX-MIN nhằm cải tiến hiệu xuất cho các thuật toán để giải các bài toán tối ƣu tổ hợp và khả năng ứng dụng của ACO.

-59-

TÀI LIỆU THAM KHẢO

[1] Krzysztof Socha, Joshua Knowles and Michael Sampels (2002), “A MAX-MIN Ant System for the University Course Timetabling Problem”, ANTS 2002, LNCS (2463), pp 1-13.

[2] Krzysztof Socha, Michael Sampels and Max Manfrin, “Ant Algorithms for the Univerrsity Course Timetabling Problem with Regard to the State-of-the-Art”.

[3] Hoang Xuan Huan & Dinh Trung Hoang, “On the ant colony system for the postman problem”, Journal of Science, Natural Sciences and Technology, Viet Nam National Univeristy,Ha Noi, vol.18, no 1, 2002,

pp 29-37.

[4] Hoang Xuan Huan (2003), Convergence Analysis of ACO Algorithms

and New Perpectives, manuscript.

[5] Hoang Xuan Huan, Do Duc Dong and Dinh Quang Huy (2004), “Multi-level Ant System and Typical Combanatorial Optimization Problems”, 2nd Optimization and Scientific Computation Conference, Institue of Mathematics, Ha Noi, Viet Nam, p 15.

[6] M.Dorigo, V.Maniezzo and A.Corloni (1991), Positive feedback as a search strategy, Technical Report 91-109, Departimento di electronica

e informatica, Poletico di Milano, IT. (adsbygoogle = window.adsbygoogle || []).push({});

[7] M.Dorigo (1992), Optimization, learning and natural algorithms, PhD.dissertation, Milan Polytechnique, Italy.

[8] M.Dorigo, V.Maniezzo and A.Corloni (1996), “The Ant System : Optimization by a colony of cooperating agents”, IEEE, Trans.Syst., Man, Cybern.B, vol.26, no.2, pp 29-41.

-60-

[9] M. Dorigo and L.M. Gambardella (1997), “Ant Colony System : A cooperative learning approach to the travelling salesman problem”,

IEEE Trans, on Evolutionary Computation, vol.1, no.1, pp 53-66.

[10] M.Dorigo and M.D.Caro (1999), “The Ant Conoly Optimization metaheuristic, A New Idea in Optimization”, D.Corne, M.Dorigo and F.Glover, Eds. London, U.K, McGraw-Hill, pp.11-32.

[11] M.Dorigo and Thomas Stutzle (2000), The Ant Colony Optimization Metaheuristic : Algorithms, Applications and Advances.

[12] Marco Dorigo and Thomas Stutzle (2002), A short Convergence Proof for a class of Ant Colony Optimization Algorithms, IEEE.

[13] Marco Dorigo, Eric Bonabeau, Guy Theraulaz. Future Generation Computer Systems. Ant System and Stigmergy, 16 (2000) 851–871. [14] Thomas Stutzle and Holger Hoos (1997), MAX-MIN Ant System and

Local Search for the Traveling Salesman Problem, IEEE.

[15] Stutzle, Hoos (2000), “MAX-MIN Ant System”, Future Generation Computer System, pp 889-914.

[16] T. Stutzle (July 1997), “MAX-MIN Ant System for the quadratic assignment problem”, Technical Report AIDA–97–4, FG Intellektik, TU Darmstadt, Germany.

[17] T. Stutzle and M. Dorigo (1999), “ACO algorithms for the quadratic assignment problem”, New Ideas in Optimization, McGraw-Hill,

London, UK, pp. 33–50.

[18] T. Stutzle and H. H. Hoos (1999), “MAX-MIN Ant System and local search for combinatorial optimization problems”, Meta-Heuristics: Advances and Trends in Local Search Paradigms for Optimization,

-61-

[19] J.-L. Deneubourg, S. Goss, J.M. Pasteels, D. Fresneau, J.-P. Lachaud (1987), Self-organization mechanisms in ant societies II:Learning in foraging and division of labour, Experientia Suppl.

[20] P.P. Grassé (1959), “La reconstruction du nid et les coordinations interindividuelles chez bellicositermes natalensis et cubitermes sp”, La théorie de la stigmergie: essai d’interprétation du comportement des termites constructeurs, Insectes Sociaux 6, pp. 41–81.

-62-

PHỤ LỤC

Mã nguồn tham khảo của thuật toán hệ kiến Max-Min giải bài toán TSP

Các thuộc tính của lớp MMAS

public const int max = 20;

public const int a = 2; /* a va b la 2 tham so neu len moi quan he qua lai giua nong do mui va thong tin heuristic*/

public const int b = 5; /* a va b la 2 tham so neu len moi quan he qua lai giua nong do mui va thong tin heuristic*/

public const float tmax = 0.5f; //Nong do mui max public const float tmin = 0.2f; //Nong do mui min public const float maxreal = 50000; //Chi phi toi da public const float rho = 0.8f; //He so bay hoi public const int m = 25; //So con kien.

public const int n_c = 500; //So vong lap.

static public float[,] d;/*mang luu do dai cac canh tren do thi.*/

static public float[,] t;/*mang luu nong do vet mui tren cac canh.*/

static public double[] p;/*mang luu xac xuat lua chon dinh tiep theo tu dinh i.*/ (adsbygoogle = window.adsbygoogle || []).push({});

static public float[,] delta;/*mang so thuc 2 chieu luu su cap nhat mui*/

static public float l_best; //Chi phi chu trinh tot nhat static public int n; // So dinh tren do thi

static int n_loop;

static double sum; // Tong cac xac suat static int dem;

-63-

static float l;

static int[] w; //Luu hanh trinh moi con kien static public int[] ldd; //Mang luu ket qua static int[] uv;

static bool[] mark;

static public string filePath="";

static public int[,] chutrinhtotnhat; /*Mang danh dau chu trinh tot nhat de cap nhat mui*/

Lấy đồ thị từ file (.tsp) vào ma trận kề

static public void LoadGraph() {

string line; string[] fields;

if (File.Exists(filePath)) {

StreamReader file = null; try

{

file = new StreamReader(filePath); if ((line = file.ReadLine()) != null) {

fields = line.Split(' '); n = int.Parse(fields[0]); s = int.Parse(fields[1]); }

d = new float[max, max];

while ((line = file.ReadLine()) != null) {

fields = line.Split(' ');

d[int.Parse(fields[0]),int.Parse(fields[1])] = float.Parse(fields[2]);

-64- = float.Parse(fields[2]); } } finally { if (file != null) file.Close(); } } else {

MessageBox.Show("Khong tim thay file du lieu!"); }

}

Phƣơng thức khởi tạo

static public void Init() {

t = new float[max, max]; delta = new float[max, max]; w = new int[max];

mark = new bool[max]; p = new double[max]; uv = new int[max]; ldd = new int[max];

chutrinhtotnhat = new int[max, max]; for (int i = 1; i < n; i++) (adsbygoogle = window.adsbygoogle || []).push({});

for (int j = i + 1; j <= n; j++) { t[i, j] = tmax; delta[i, j] = 0; t[j, i] = t[i, j]; delta[j, i] = delta[i, j];

-65-

}

n_loop = 0;

l_best = maxreal; }

Phƣơng thức tính toán lại các tham số sau khi chọn một đỉnh trong hành trình một con kiến

static public void CalcP() {

sum = 0; dem = 0;

int currentCity = 0;

#region Khoi tao lai mang p,uv for (int i = 1; i <= n; i++) {

p[i] = 0; uv[i] = 0; }

#endregion

#region Xac dinh mang uv for (int i = 1; i <= n; i++) {

if (w[i] != 0)

currentCity = w[i]; }

for (int i = 1; i <= n; i++) {

if (mark[i] == false && d[currentCity, i] != 0) {

dem++;

uv[dem] = i; }

-66-

#endregion

#region Tinh toan mau so

for (int i = 1; i <= dem; i++) {

sum += Math.Pow(t[currentCity, uv[i]], a) / Math.Pow(d[currentCity, uv[i]], b);

}

#endregion

#region Tinh toan p

for (int i = 1; i <= dem; i++) {

p[uv[i]] = Math.Pow(t[currentCity, uv[i]], a) / (Math.Pow(d[currentCity, uv[i]], b) * sum) * 100; }

#endregion } (adsbygoogle = window.adsbygoogle || []).push({});

Phƣơng thức chọn đỉnh tiếp theo của hành trình một con kiến

static public int Lottery_Wheel() {

int rs = 0; double k;

double tem = 0f; CalcP();

Random random = new Random(); k = random.Next(1, 100);

for (int i = 1; i <= n; i++) { tem += p[uv[i]]; if (tem >= k) { rs = uv[i]; break;

-67- } } return rs; }  Phƣơng thức cập nhật mùi

static public void Pheromone_Update() {

for (int i = 1; i < n; i++)

for (int j = i + 1; j <= n; j++) {

t[i, j] = rho * t[i, j] + delta[i, j]; if (t[i, j] > tmax) { t[i, j] = tmax; } t[j, i] = t[i, j]; } }  Phƣơng thức tìm đƣờng đi ngắn nhất

static public void Ant_Cycle() {

Init(); do

{

n_loop++;

for (int i = 1; i <= m; i++) {

w[1] = 1; /* gia su dinh xuat phat la 1 cho tat ca cac con kien.*/

for (int j = 2; j <= n; j++) /* Khoi tao lai mang danh dau.*/

-68-

mark[j] = false; // Chua co dinh nao visited w[j] = 0;

}

mark[1] = true; // ngoai tru dinh so 1

l = 0; //Chieu dai duong di duoc khoi tao bang 0 #region Thiet lap hanh trinh cua 1 con kien for (int j = 2; j <= n; j++) { w[j] = Lottery_Wheel(); l += d[w[j - 1], w[j]]; mark[w[j]] = true; } l += d[w[n], w[1]]; #endregion if (l < l_best) {

l_best = l; //Luu chi phi duong di tot nhat for (int t = 1; t <= n; t++) ldd[t] = w[t]; #region Khoi tao lai chu trinh tot nhat

for (int t = 1; t <= n; t++) for (int j = 1; j <= n; j++) { chutrinhtotnhat[t, j] = 0; } #endregion

#region Danh giau chu trinh tot nhat for (int ii = 1; ii < n; ii++)

{

chutrinhtotnhat[w[ii], w[ii + 1]] = 1; chutrinhtotnhat[w[ii + 1], w[ii]] = 1; }

-69-

chutrinhtotnhat[w[n], 1] = 1; #endregion (adsbygoogle = window.adsbygoogle || []).push({});

}

#region Tinh su cap nhat mui tren cac canh thuoc chu trinh tot nhat

for (int ii = 1; ii < n; ii++)

for (int j = ii + 1; j <= n; j++) {

if (chutrinhtotnhat[ii, j] == 1) {

delta[ii, j] = delta[ii, j] + 1 / l_best; }

else

if (tmin - t[ii, j] > 0) delta[ii, j] = tmin - rho * t[ii, j]; else

delta[ii, j] = 0; delta[j, ii] = delta[ii, j]; } #endregion Pheromone_Update(); } } while (n_loop < n_c); }

Một phần của tài liệu Tìm hiểu và nghiên cứu thuật toán hệ kiến max min (Trang 55 - 69)