getBestScore()

22 views
Skip to first unread message

J J Farrell

unread,
Dec 16, 2021, 9:40:49 PM12/16/21
to OptaPlanner development
The Solver's getBestScore was removed in OptaPlanner 8 with a recommendation to use an EventListener to catch the current best score. However, an event is only raised when the current best score is bettered. I hit a situation where the first solution generated happened to be a very good one, so it was a long time before it was bettered and the first event was raised.

How do I find the current best score in the minutes or hours before the first event is received? I was fool enough to be using old documentation, and moving from using an event to getting the info directly from the solver before finding that was no longer possible. The code to do it via the solver was much more straightforward and intuitive, it's unfortunate it's no longer available ;).

It makes for messy code to be unable to report progress until the first event arrives. Is there any way around this?

Geoffrey De Smet

unread,
Dec 17, 2021, 5:01:52 AM12/17/21
to optapla...@googlegroups.com

Interesting challenge. I presume you're talking about getting the starting score such as -40init/0hard/0soft.

The solvingStarted event, which creates the initial best score a few milliseconds after the solve() method is called,
does indeed NOT send out a best solution event.
But solver.getBestScore() could indeed extract that starting score, such as -40init/0hard/0soft.

Or did you also get CH score improvement, such as -38init/0hard/-5soft and -36init/0hard/-25soft?


If we do start sending out a best score event for that, and users don't do the isFeasible() or isInitialized() checks, then it could surpsise them after upgrading.
Something to think about.

Workaround:
Use ScoreManager to calculate the starting score and use that until the first new best solution even comes in - typically with an initialized score.

With kind regards,
Geoffrey De Smet

--
You received this message because you are subscribed to the Google Groups "OptaPlanner development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to optaplanner-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/optaplanner-dev/2bdcadb3-34d5-4fa6-862b-f54a51950822n%40googlegroups.com.

J J Farrell

unread,
Dec 17, 2021, 11:44:18 AM12/17/21
to OptaPlanner development
Thanks Geoffrey. I'm new to this, and I've inherited a bunch of code which saves the info from the event for other threads to make use of. There's an implicit assumption that this data will be valid as of 5 seconds after calling solve(). That has always worked in the past as the first event usually comes in within a few milliseconds of calling solve(), but I just happened on a dataset where the first event doesn't arrive within 5 seconds. With OptaPlanner debug logging I see this as solve() starts:

[INFO ] 2021-12-14 22:06:08.969 [Thread-2] DefaultSolver - Solving started: time spent (105), best score (0hard/-1309soft), environment mode (REPRODUCIBLE), move thread count (NONE), random (JDK with seed 0).
[DEBUG] 2021-12-14 22:06:09.086 [Thread-2] DefaultLocalSearchPhase -     LS step (0), time spent (231), score (0hard/-1311soft),     best score (0hard/-1309soft), accepted/selected move count (4/7), picked move ...

and the best score does not improve within 5 seconds.

On review I decided that most of the uses of this event data were better served by getting it from elsewhere anyway. The one problem remaining was its use to regularly log and display the current best score as the solver ran, for which Solver.getBestScore() was the obvious solution ;). I'll look at using ScoreManager to get the initial score as you suggest.

Many Thanks,
                                           jjf
Reply all
Reply to author
Forward
0 new messages