a星算法求解旅行商问题.doc

12
Astar 算算算算算算算算算 一、 n算算算 ,一 A算算算 一一 A ,。 算算 1 算算算 ,。 A 算算算 () A···(( n-1 算算算算 算 )), A 2 1 算算 n 算算算 ,,, open 算open 算 ); 2算 算算 open算 ,(3 ),( 7算算 3算算 open 算算算算算算算算 close 算 算 open 算 4 算算 num num ≥ n+1 算算 ,,(7算算 5算 算算num==n 算算算 ,一, isVisited[0]算算算 false 算算 ,,; 6算 算算num<n open 算 算 vector 算 算 fvalue 算 算 open 算 算 ,(2算算 7算算算 close 算 算 算 算 算 一,,,。 3 f(n)=g(n)+h(n) h(n)≤h*(n)算 g(n) n算算 ,。 h(n) 算算算算 n 算算算算 depth city_num (city_num-depth)算算算 n min 算算 h(n)=min*(city_num-deep)算算算算算算算算算 n 算 h(n)≤h*(n)算 算算 一。 算算 算算算算算算算 算算算算算depth 算 算算算算 ,,; num 算 id_list 算 算 算 算 一,, 1算 isVisited 算 算 算 算 一,, false ,; fvalue 算 算算 n*n 算算 city_distance 算 算 n 算算算算算算算算算算算 k 算算算算算算算算算算算算算算算算算 k-1 算算算 )。 open 算 算 算 :, fvalue 算 close 算 算 :。 算算 open算 算算算 ,一 id_list 算算 算算 1 算算 8算算算 ,一 8*8算 算 算 算 ,。一 0 0 算算算算 1 算 算 ,。

Upload: lin

Post on 17-Nov-2015

28 views

Category:

Documents


6 download

DESCRIPTION

A星算法求解旅行商问题

TRANSCRIPT

Astar

nAA

1

A

An-1A

2

1nopenopen

2 open37

3opencloseopen

4numnum n+17

5num==nisVisited[0]false

6num 0){//ij

if(min == 0 || (city_distance[i][j] < min)){

min = city_distance[i][j];

}

}

}

}

return min;

}

//gvalue

public int get_gvalue(City city){

int gvalue = 0;

for(int i = 1; i < city.num; i ++){

gvalue += city_distance[city.id_list[i]-1][city.id_list[i-1]-1];

}

return gvalue;

}

//hvalue

public int get_hvalue(int depth){

return (city_num - depth)*min_distance;

}

//A*

public City AStar(int start){

int i, j;

//open

MyQueue open = new MyQueue(city_num);

//close

Vector close = new Vector();

//

City city = new City(city_num);

city.id_list[city.num ++] = start;

city.isVisited[start - 1] = true;//10

city.fvalue = get_gvalue(city) + get_hvalue(city.depth);

//open

open.queueIn(city);

//open

while(!open.isEmpty()){

//openclose

City head_elem = open.queueOut();

close.add(head_elem);

//

if(head_elem.num >= city_num+1){

break;

}

if(head_elem.num == city_num){

head_elem.isVisited[0] = false;

}

//

Vector v = new Vector();

for(i = 0; i < city_num; i ++){

int id = head_elem.num-1;

//

if(city_distance[head_elem.id_list[id]-1][i] > 0 && !head_elem.isVisited[i]){

//

City new_city = new City(city_num);

//

new_city.depth = head_elem.depth + 1;

//

for(j = 0; j < head_elem.num; j ++){

new_city.id_list[j] = head_elem.id_list[j];

}

new_city.num = head_elem.num;

new_city.id_list[new_city.num ++] = i+1;//01

//

int len = head_elem.isVisited.length;

for(j = 0; j < len; j ++){

new_city.isVisited[j] = head_elem.isVisited[j];

}

new_city.isVisited[i] = true;

//fvalue

new_city.fvalue = get_gvalue(new_city) + get_hvalue(new_city.depth);

v.addElement(new_city);

/*System.out.print(new_city.id_list[new_city.num-1] + " ");*/

}

}

while(!open.isEmpty()){

v.addElement(open.queueOut());

}

/*int size = v.size();

System.out.println(size);

for(i = 0; i < size; i ++){

City c = v.get(i);

System.out.print(c.id_list[c.num-1] + " ");

}*/

open = sort(v);

}

return close.lastElement();

}

//Citycity.fvalue

public MyQueue sort(Vector v){

int i, j;

//fvalue

int size = v.size();

/*System.out.println("size = " + size);*/

int[] fvalue_arr = new int[size];

for(i = 0; i < size; i ++){

fvalue_arr[i] = v.get(i).fvalue;

/*System.out.println(i + " " +fvalue_arr[i]);*/

}

//fvalue_arr

int count = 0;

int[] index_of_fvalue = new int[size];

boolean[] bool = new boolean[size];

for(i = 0; i < size; i ++){

count = 0;

for(j = 0; j < size; j ++){//fvalue_arr[i]

if(i != j && fvalue_arr[j]