Browse Source

Fix PrismUtils.doublesAreEqual() and use in more places.

Make this method, which is used to check that doubles are (approximately) equal,
use relative rather than absolute error, which is more robust in general.

This is now used in places where the same check was done, but with an (identical)
hard-coded epsilon of 1e-12. Property.getExpectedResultString has also been changed,
which previously used 1e-10, but there seems no reason for this not to use 1e-12.
accumulation-v4.7
Dave Parker 5 years ago
parent
commit
233e1235b6
  1. 2
      prism/src/explicit/Distribution.java
  2. 4
      prism/src/explicit/MDP.java
  3. 4
      prism/src/explicit/MDPSparse.java
  4. 4
      prism/src/explicit/STPGAbstrSimple.java
  5. 2
      prism/src/parser/ast/Property.java
  6. 2
      prism/src/prism/PrismUtils.java

2
prism/src/explicit/Distribution.java

@ -303,7 +303,7 @@ public class Distribution implements Iterable<Entry<Integer, Double>>
Map.Entry<Integer, Double> e = i.next();
d1 = e.getValue();
d2 = d.map.get(e.getKey());
if (d2 == null || !PrismUtils.doublesAreClose(d1, d2, 1e-12, false))
if (d2 == null || !PrismUtils.doublesAreEqual(d1, d2))
return false;
}
return true;

4
prism/src/explicit/MDP.java

@ -409,7 +409,7 @@ public interface MDP extends MDPGeneric<Double>
double d = mvMultSingle(s, choice, vect);
// Store strategy info if value matches
if (PrismUtils.doublesAreClose(val, d, 1e-12, false)) {
if (PrismUtils.doublesAreEqual(val, d)) {
result.add(choice);
}
}
@ -900,7 +900,7 @@ public interface MDP extends MDPGeneric<Double>
for (int choice = 0, numChoices = getNumChoices(s); choice < numChoices; choice++) {
double d = mvMultRewSingle(s, choice, vect, mdpRewards);
// Store strategy info if value matches
if (PrismUtils.doublesAreClose(val, d, 1e-12, false)) {
if (PrismUtils.doublesAreEqual(val, d)) {
result.add(choice);
}
}

4
prism/src/explicit/MDPSparse.java

@ -855,7 +855,7 @@ public class MDPSparse extends MDPExplicit
d += nonZeros[k] * vect[cols[k]];
}
// Store strategy info if value matches
if (PrismUtils.doublesAreClose(val, d, 1e-12, false)) {
if (PrismUtils.doublesAreEqual(val, d)) {
res.add(j - l1);
}
}
@ -1103,7 +1103,7 @@ public class MDPSparse extends MDPExplicit
}
d += mdpRewards.getStateReward(s);
// Store strategy info if value matches
if (PrismUtils.doublesAreClose(val, d, 1e-12, false)) {
if (PrismUtils.doublesAreEqual(val, d)) {
res.add(j - l1);
}
}

4
prism/src/explicit/STPGAbstrSimple.java

@ -699,7 +699,7 @@ public class STPGAbstrSimple extends ModelExplicit implements STPG, NondetModelS
}
// Store strategy info if value matches
//if (PrismUtils.doublesAreClose(val, d, termCritParam, termCrit == TermCrit.ABSOLUTE)) {
if (PrismUtils.doublesAreClose(val, minmax2, 1e-12, false)) {
if (PrismUtils.doublesAreEqual(val, minmax2)) {
res.add(j);
//res.add(distrs.getAction());
}
@ -849,7 +849,7 @@ public class STPGAbstrSimple extends ModelExplicit implements STPG, NondetModelS
minmax2 += rewards.getTransitionReward(s, dsIter);
// Store strategy info if value matches
//if (PrismUtils.doublesAreClose(val, d, termCritParam, termCrit == TermCrit.ABSOLUTE)) {
if (PrismUtils.doublesAreClose(val, minmax2, 1e-12, false)) {
if (PrismUtils.doublesAreEqual(val, minmax2)) {
res.add(dsIter);
//res.add(distrs.getAction());
}

2
prism/src/parser/ast/Property.java

@ -209,7 +209,7 @@ public class Property extends ASTElement
match = false;
// Check doubles numerically
else if (constValToMatch instanceof Double)
match = PrismUtils.doublesAreCloseRel(((Double) constValToMatch).doubleValue(), DefinedConstant.parseDouble(constVal), 1e-10);
match = PrismUtils.doublesAreEqual(((Double) constValToMatch).doubleValue(), DefinedConstant.parseDouble(constVal));
// if constant is exact rational number, compare exactly
else if (constValToMatch instanceof BigRational)
match = BigRational.from(constVal).equals(constValToMatch);

2
prism/src/prism/PrismUtils.java

@ -288,7 +288,7 @@ public class PrismUtils
*/
public static boolean doublesAreEqual(double d1, double d2)
{
return doublesAreCloseAbs(d1, d2, epsilonDouble);
return doublesAreCloseRel(d1, d2, epsilonDouble);
}
/**

Loading…
Cancel
Save