我正在尝试用Java实现一个简单的“随机搜索算法”
这是一段代码:
//执行算法
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); {
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);
我的问题是:不知怎的,我必须初始化初始解决方案“double bestSolution”。我应该给出什么初始值?请注意,对于某些目标函数,诸如“0”之类的值会使收敛变得更难。
检查您是否处于第一次迭代(iter == 0
),如果是第一次迭代,则使用计算解决方案初始化bestSolution,否则将其与之前的bestSolution进行比较。
我觉得使用起来很自然
double bestSolution = Double.MAX_VALUE
因为可能你的第一个猜测是迄今为止最好的,无论它是什么。
或者甚至是
double bestSolution = Double.POSITIVE_INFINITY