To implement local search algorithm for Travelling Salesman Problem using Hill Climb > Java Program

To implement local search algorithm for Travelling Salesman Problem using Hill Climb > Java Program

Artificial Intelligence

Program:

package javaapplication5;
import java.util.*;
public class JavaApplication5 
{
    Integer weight[]={25,8,20,6,12,0};
    int[][] graph={{0,1,1,0,0,0},
                   {1,0,1,1,0,0},
                   {1,1,0,0,1,0},
                   {0,1,0,0,1,1},
                   {0,0,1,1,0,1},
                   {0,0,0,1,1,0}};
    int[] FLAG_ARR={0,0,0,0,0,0};
    int[] temp=new int[3];
    List successors(int i)
    {
        List t=new Vector();
        FLAG_ARR[i]=1;
        for(int j=0;j<6;j++)
        {
            if(graph[i][j]==1 && FLAG_ARR[j]!=1)
            {  
                  t.add(weight[j]);
               //FLAG_ARR[j]=1;
            }
            else if(FLAG_ARR[j]==1)
                t.remove(weight[j]);
        }
        return t;
    }
    public static void main(String[] args)
    {
        //Scanner sc=new Scanner(System.in);
        JavaApplication5 jap=new JavaApplication5();
       ArrayList que=new ArrayList();
       int a=0;
        for(int i=0;i<6;i++)
        {
            if(a==5)
            {
                System.out.println("SUCCESSFULLY IMPLEMENTED");
                break;
            }
            else
            {
                que.addAll(jap.successors(a));
                Collections.sort(que);
            }
            a=Arrays.asList(jap.weight).indexOf(que.get(0));
            System.out.print(a);
            if(que.isEmpty())
                System.out.print("FAILURE");
            que.remove(0);
            System.out.println(que);
        } 
    }
  
}

/*------
run:
1[20]
3[20, 20]
5[12, 20, 20]
SUCCESSFULLY IMPLEMENTED
BUILD SUCCESSFUL (total time: 0 seconds)
*/

Comments

Popular posts from this blog

Intermediate Code Generation > C Program