Implement random search algorithm in java
I'm tryin to implement a simple "random search algorithm" in Java
here's a piece of the code:
//execute the algorithm
double bestSolution; //INITIAL SOLUTION!
Vector bestVector=null;
for (int iter=0; iter<maxIterations; iter++) {
//generate random vector-solution
Vector v = Vector.generateRandomVector(problemSize, minOfSearchSpace, maxOfSearchSpace);
double currentObjValue = objectiveFunctionValue(v);
// if a better solution is found
if (currentObjValue < bestSolution); 开发者_如何学Go{
bestVector = v;
bestSolution = currentObjValue;
}
System.out.println("Iteration: "+(iter+1)+" Best solution: "+bestSolution);
} // end for
System.out.println("\n\nBest solution: "+bestVector.toString()+" Objective Value: "+bestSolution);
my problem is: somehow i have to initialize the initial solution "double bestSolution". what initial value should i give? note that for certain objective function, values such as "0" while make the convergence harder.
It seems natural to me to use
double bestSolution = Double.MAX_VALUE
since presumably your first guess will be the best so far, no matter what it is.
or maybe even
double bestSolution = Double.POSITIVE_INFINITY
Check if you're at the first iteration (iter == 0
), and initialize the bestSolution with the computed solution if it's the first iteration, else compare it with the previous bestSolution.
精彩评论