Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

3 a. hil climbing


Published on

hill climbing correct source code

Published in: Sports, Education
  • Be the first to comment

  • Be the first to like this

3 a. hil climbing

  1. 1. 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 **/
  2. 2. 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]);
  3. 3. 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(); } } }
  4. 4. OUTPUT: