1

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); { 
            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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

pretty reasonable idea but think of this example:
min x^2 using that initial value the algorithm will never converge
I don't see where in your code you are using the value of bestSolution as a seed for generating the next vector. I must be missing something, does generateRandomVector() somehow use bestSolution? As I understand it you're just generating random candidates, so the initial value shouldn't affect the result.
that's what exacty random search does. create random solutions using generateRandomVector() in a pre-specified search space and comparees with the pevious best solution.
in that case the solution will not converge any faster no matter what your initial choice of bestSolution is.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.