Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-06-2011, 05:49 PM   PM User | #1
ssjdx1
New to the CF scene

 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ssjdx1 is an unknown quantity at this point
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.
ssjdx1 is offline   Reply With Quote
Old 03-15-2012, 04:27 PM   PM User | #2
progbeginnerf
New to the CF scene

 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
progbeginnerf is an unknown quantity at this point
Could I see your small change method?
progbeginnerf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:00 PM.


Advertisement
Log in to turn off these ads.