Question

Using HARDSOFTBIGDECIMAL score.

In the config file I set scoreAttained to 0hard/0soft.

I get this error:

Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(Unknown Source)
at org.optaplanner.core.impl.score.buildin.hardsoftbigdecimal.HardSoftBigDecimalScoreDefinition.calculateTimeGradient(HardSoftBigDecimalScoreDefinition.java:96)
at org.optaplanner.core.impl.score.buildin.hardsoftbigdecimal.HardSoftBigDecimalScoreDefinition.calculateTimeGradient(HardSoftBigDecimalScoreDefinition.java:27)
at org.optaplanner.core.impl.termination.ScoreAttainedTermination.calculateSolverTimeGradient(ScoreAttainedTermination.java:50)
at org.optaplanner.core.impl.termination.OrCompositeTermination.calculateSolverTimeGradient(OrCompositeTermination.java:69)
at org.optaplanner.core.impl.termination.OrCompositeTermination.calculateSolverTimeGradient(OrCompositeTermination.java:69)
at org.optaplanner.core.impl.termination.PhaseToSolverTerminationBridge.calculatePhaseTimeGradient(PhaseToSolverTerminationBridge.java:80)
at org.optaplanner.core.impl.localsearch.DefaultLocalSearchSolverPhase.solve(DefaultLocalSearchSolverPhase.java:60)
at org.optaplanner.core.impl.solver.DefaultSolver.runSolverPhases(DefaultSolver.java:190)
at org.optaplanner.core.impl.solver.DefaultSolver.solve(DefaultSolver.java:155)

EDIT:

solved the problem by extending HARDSOFTBIGDECIMALSCOREDEFINITION and overriding calculateTimeGradient(..) method. Here when the divide method is called on BigDecimal, I rounded up.

Was it helpful?

Solution

You are more than likely dividing two numbers that have an exact quotient that is infinite.

For example: 1/3 = 0.33333...

From the docs:

When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.)

As a corollary of computing the exact result, the rounding mode setting of a MathContext object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3.

If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

So to fix this you need to provide a scale of precision for BigDecimal, like so:

x.divide(y, 2, RoundingMode.HALF_UP)

Where 2 is the precision and RoundingMode.HALF_UP is the rounding mode.

You can read more about rounding here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top