Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4058354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:04:08+00:00 2026-05-20T15:04:08+00:00

I’m trying to use the Simple hill climbing algorithm to solve the travelling salesman

  • 0

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:

  • Stochastic Hill Climber
  • Random Restart Hill Climber
  • Simulated Annealing.

Anyway back to the simple hill climbing algorithm I already have this:

import java.util.*;
public class HCSA 
{
    static private Random rand; 
    static public void main(String args[])
    {
        for(int i=0;i<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);
    }
}

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.

Would really appreciate any help on this.

—– EDIT —-

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.

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<x.length;++i)
        {
            for(int j=0;j<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<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<Integer> ReadIntegerFile(String filename)
    {
        ArrayList<Integer> res = new ArrayList<Integer>();
        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);
    }
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:04:09+00:00Added an answer on May 20, 2026 at 3:04 pm

    I’m not sure what the code you pasted has to do with Traveling Salesman. You have a function UR which generates a random number in the interval [a,b).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.