Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simple Hill Climbing algorithm?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    London, UK
    Thanked 0 Times in 0 Posts

    Default Simple Hill Climbing algorithm?

    Hi everyone,

    I need a bit of help in trying to create a simple hill climbing algorithm in order to solve the travelling salesman problem.

    I have some pseudo code that i cannot turn into java, mostly because i have not done Java in a while. I was hoping someone on here could help me.

    Here is the first bit of pseudo code i have:

    1) Create a random integer p that ranges between 0 and n-1.
    2) Create an empty new string say x.
    3) Copy from elements 0 to p-1 from scasol to x.
    4) Copy the changed version of position p of string scasol to x.
    5) Copy from p+1 to n-1 of scasol to x.
    6) Set scasol to be x.

    and here is what i have done so far on it:

    public void SmallChange()
    	Random rand = new Random();
    	int n = scasol.length();
    	int p = Math.abs(rand.nextInt() % n);
    	String x;

    I've basically converted the first two bits of pseudo into java but cannot do the rest, can anyone help? Would really appreciate it.

    Here is the second pseudo code:

    1) We need to add a For loop that iterates for the specified number of iterations.
    2) We need to create an initial random solution of size n.
    3) We need to evaluate the fitness of our current solution within the loop.
    4) We need to copy the current solution (say oldsol).
    5) We make a small change to the current solution and evaluate the fitness to another variable.
    6) If the new fitness is worse than the old, we copy oldsol back to being our current solution.
    7) After the For loop has completed we return the current solution.

    I cannot do this one at all so i was hoping that someone could point me in the right direction? I'm really worried about doing this since i've become really stuck.

    I would greatly appreciate anyones help on this. I've tried do this for a few days now and i've hit a brick wall so i was hoping that i could get some help.

    Thanks in advance.
    Last edited by RED_; March 21st, 2011 at 12:56 AM.

  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    My Mood
    Thanked 12 Times in 12 Posts

    Default Re: Simple Hill Climbing algorithm?

    String Functions

    the above link can help you
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. SDES algorithm
    By low1988 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 2nd, 2010, 09:57 AM
  2. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 04:12 PM
  3. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  4. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM
  5. algorithm
    By AmmrO in forum Algorithms & Recursion
    Replies: 13
    Last Post: September 24th, 2009, 09:18 PM