|
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.
|