title: Simple hill climbing algorithm?
body: <p>I'm trying to use the Simple hill climbing algorithm to solve the travelling salesman problem. I want to create a Java program to do this. I know it's not the best one to use but I mainly want it to see the results and then compare the results with the following that I will also create: </p>
  
  <ul>
  <li>Stochastic Hill Climber</li>
  <li>Random Restart Hill Climber</li>
  <li>Simulated Annealing. </li>
  </ul>
  
  <p>Anyway back to the simple hill climbing algorithm I already have this: </p>
  
  <pre><code>import java.util.*;
  public class HCSA 
  {
      static private Random rand; 
      static public void main(String args[])
      {
          for(int i=0;i&lt;10;++i) System.out.println(UR(3,4));
      }
      static public double UR(double a,double b)
      {
          if (rand == null) 
          {
              rand = new Random();
              rand.setSeed(System.nanoTime());
          }
          return((b-a)*rand.nextDouble()+a);
      }
  }
  </code></pre>
  
  <p>Is this all I need? Is this code even right..? I have a range of different datasets in text documents that I want the program to read from and then produce results. </p>
  
  <p>Would really appreciate any help on this.</p>
  
  <p><em>----- EDIT ----</em></p>
  
  <p>I was being an idiot and opened the Java file straight into Eclipse when i should have opened it in notepad first.. here is the code i have now got. </p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.Reader;
  import java.io.StreamTokenizer;
  import java.util.ArrayList;
  
  {
      //Print a 2D double array to the console Window
      static public void PrintArray(double x[][])
      {
          for(int i=0;i&lt;x.length;++i)
          {
              for(int j=0;j&lt;x[i].length;++j)
              {
                  System.out.print(x[i][j]);
                  System.out.print(" ");
              }
              System.out.println();
          }
      }
      //reads in a text file and parses all of the numbers in it
      //is for reading in a square 2D numeric array from a text file
      //This code is not very good and can be improved!
      //But it should work!!!
      //'sep' is the separator between columns
      static public double[][] ReadArrayFile(String filename,String sep)
      {
          double res[][] = null;
          try
          {
              BufferedReader input = null;
              input = new BufferedReader(new FileReader(filename));
              String line = null;
              int ncol = 0;
              int nrow = 0;
  
              while ((line = input.readLine()) != null) 
              {
                  ++nrow;
                  String[] columns = line.split(sep);
                  ncol = Math.max(ncol,columns.length);
              }
              res = new double[nrow][ncol];
              input = new BufferedReader(new FileReader(filename));
              int i=0,j=0;
              while ((line = input.readLine()) != null) 
              {
  
                  String[] columns = line.split(sep);
                  for(j=0;j&lt;columns.length;++j)
                  {
                      res[i][j] = Double.parseDouble(columns[j]);
                  }
                  ++i;
              }
          }
          catch(Exception E)
          {
              System.out.println("+++ReadArrayFile: "+E.getMessage());
          }
          return(res);
      }
      //This method reads in a text file and parses all of the numbers in it
      //This code is not very good and can be improved!
      //But it should work!!!
      //It takes in as input a string filename and returns an array list of Integers
      static public ArrayList&lt;Integer&gt; ReadIntegerFile(String filename)
      {
          ArrayList&lt;Integer&gt; res = new ArrayList&lt;Integer&gt;();
          Reader r;
          try
          {
              r = new BufferedReader(new FileReader(filename));
              StreamTokenizer stok = new StreamTokenizer(r);
              stok.parseNumbers();
              stok.nextToken();
              while (stok.ttype != StreamTokenizer.TT_EOF) 
              {
                  if (stok.ttype == StreamTokenizer.TT_NUMBER)
                  {
                      res.add((int)(stok.nval));
                  }
                  stok.nextToken();
              }
          }
          catch(Exception E)
          {
              System.out.println("+++ReadIntegerFile: "+E.getMessage());
          }
          return(res);
      }
  }
  </code></pre>