Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem implementing Random Hill Climbing Algorithm

    Hey, I am currently stuck on creating the Random Hill Climing Algorithm that evaluates the fittness of my scalesfittness code before and after a small change, now the fittness number before the change should be greater then the number after the change however it tends to fluctuate.

    Here is the pseudo code for the algorithm:-
    Algorithm 1. RMHC(ITER)
    Input: ITER- the number of iterations to run for
    1) Let S be a random point in the search space,
    let F be its fitness
    2) For i = 1 to ITER (number of iterations)
    3) Let S’ be a random point close to S,
    let F’ be its fitness
    4) If F’ is better than F Then
    5) Let S = S’ and F = F’
    6) End If
    5) Next i
    Output: S- a solution


    Here is my implementation of it in eclipse:-
    public static ScalesSolution RMHC(ArrayList<Double> weights,int n,int iter)
    {
    ScalesSolution sol = new ScalesSolution(n);

    ScalesSolution oldSol = new ScalesSolution(sol.GetSol());

    for(int i = 0; i < iter; i++)

    {

    // this is the old solution
    System.out.println("##NEW ITERATION##");
    System.out.println("Old Solution : ");
    oldSol.println();
    double f = oldSol.ScalesFitness(weights);
    System.out.println("Old Fittness: ");
    System.out.println(f);
    //the new solution after copying the string from scalesolution
    sol.SmallChange();
    System.out.println("New Solution : ");
    sol.println();
    System.out.println("New Fittness: ");
    System.out.println(sol.ScalesFitness(weights));

    if (oldSol.ScalesFitness(weights) > sol.ScalesFitness(weights))
    {

    oldSol = new ScalesSolution(sol.GetSol());


    }

    }

    return(oldSol);
    }


    My code within the main is as follows :-
    ArrayList<Double> weight = new ArrayList<Double>();


    weight.add(1.0);
    weight.add(2.0);
    weight.add(3.0);
    weight.add(4.0);
    weight.add(10.0);


    System.out.println(" Final Solution: "+RMHC(weight,5,5).ScalesFitness(weight));

    However my output is as so:-

    ##NEW ITERATION##
    Old Solution :
    01010
    Old Fittness:
    8.0
    New Solution :
    00010
    New Fittness:
    12.0
    ##NEW ITERATION##
    Old Solution :
    01010
    Old Fittness:
    8.0
    New Solution :
    10010
    New Fittness:
    10.0
    ##NEW ITERATION##
    Old Solution :
    01010
    Old Fittness:
    8.0
    New Solution :
    10000
    New Fittness:
    18.0
    ##NEW ITERATION##
    Old Solution :
    01010
    Old Fittness:
    8.0
    New Solution :
    10100
    New Fittness:
    12.0
    ##NEW ITERATION##
    Old Solution :
    01010
    Old Fittness:
    8.0
    New Solution :
    10101
    New Fittness:
    8.0
    Final Solution: 8.0


    I cant seem to figure out where I have gone wrong if any one at all could please help me implement this code it would be a massive help, if anyone would like to see any of my other methods or classes please let me know.

  • #2
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could I see your small change method?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •