Upload
aravindh-ramanan
View
73
Download
2
Embed Size (px)
DESCRIPTION
hill climbing correct source code
Citation preview
EXP. 3A- Implementation Of Hill Climbing Algorithm
CODE:
import java.util.*;
public class TravelingSalesPerson
{
public double[][] fillDistanceMatrix(int size, int seed)
{
double[][] distances=new double[size][size];
Random random=new Random(seed);
for(int i=0; i<size; i++)
for(int j=i+1; j<size; j++)
{
distances[i][j]=random.nextDouble();
distances[j][i]=distances[i][j];
}
return distances;
}
/**swaps two cities in the tour and returns the
* change in cost of the tour**/
public double swap(int[] tour, int tourIndexA, int tourIndexB, double[][] distances)
{
return 0;
}
/**performs a hill climbing algorithm on the TSP with
* random restart
**/
public double getBestTour(int[] tour,double[][] distances, int restarts, int seed)
{
//must use random to create new randomly ordered tours
Random random=new Random(seed);
for(int i=0; i<restarts; i++)
{
randomizeTour(tour, random);
//put code here
}
return 0;
}
public void randomizeTour(int[] tour, Random random)
{
for(int i=0; i<tour.length; i++)
tour[i]=i;
//randomize the tour from here
}
public static void main(String[] argv)
{
int[] sizes={10, 20, 30, 40};
int[] seeds={1, 2, 3, 4};
int[] restarts={20, 20, 20, 20};
TravelingSalesPerson sales=new TravelingSalesPerson();
for(int i=0; i<sizes.length; i++)
{
double[][] distances=sales.fillDistanceMatrix(sizes[i], seeds[i]);
int[] tour=new int[sizes[i]];
double cost=sales.getBestTour(tour, distances, restarts[i], seeds[i]);
System.out.println("The following tour costs "+cost);
for(int j=0; j<tour.length; j++)
System.out.print(tour[j]+" ");
System.out.println();
}
}
}
OUTPUT: