| 1 |
package test.dr.evomodel.operators; |
| 2 |
|
| 3 |
import dr.inference.model.Bounds; |
| 4 |
import dr.inference.model.Parameter; |
| 5 |
import dr.inference.model.Variable; |
| 6 |
import dr.inference.operators.UniformIntegerOperator; |
| 7 |
import junit.framework.Test; |
| 8 |
import junit.framework.TestCase; |
| 9 |
import junit.framework.TestSuite; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public class UniformIntegerOperatorTest extends TestCase { |
| 15 |
private final int dimension = 3; |
| 16 |
private int[][] count; |
| 17 |
|
| 18 |
public static Test suite() { |
| 19 |
return new TestSuite(UniformIntegerOperatorTest.class); |
| 20 |
} |
| 21 |
|
| 22 |
public void testParameterBound() { |
| 23 |
count = new int[dimension][4]; |
| 24 |
Parameter parameter = new Parameter.Default(new double[]{1.0, 0.0, 3.0}); |
| 25 |
parameter.addBounds(new Parameter.DefaultBounds(new double[]{3.0, 3.0, 3.0}, new double[dimension])); |
| 26 |
UniformIntegerOperator uniformIntegerOperator = new UniformIntegerOperator(parameter, 0, 3, 10, 1); |
| 27 |
for (int i = 0; i < 300; i++) { |
| 28 |
uniformIntegerOperator.doOperation(); |
| 29 |
countParaValueFrequency(uniformIntegerOperator.getVariable()); |
| 30 |
} |
| 31 |
printCount("Parameter (Double) lower = 0, upper = 3"); |
| 32 |
|
| 33 |
assertTrue("Expected count[0][0-3] > 0", count[0][0] > 0 && count[0][1] > 0 && count[0][2] > 0 && count[0][3] > 0); |
| 34 |
assertTrue("Expected count[1][0-3] > 0", count[1][0] > 0 && count[1][1] > 0 && count[1][2] > 0 && count[1][3] > 0); |
| 35 |
assertTrue("Expected count[2][0-3] > 0", count[2][0] > 0 && count[2][1] > 0 && count[2][2] > 0 && count[2][3] > 0); |
| 36 |
} |
| 37 |
|
| 38 |
public void testIntegerParameterStaircaseBound() { |
| 39 |
count = new int[dimension][3]; |
| 40 |
Variable<Integer> parameterInt = new Variable.I(new int[dimension-1]); |
| 41 |
parameterInt.addBounds(new Bounds.Staircase(parameterInt)); |
| 42 |
UniformIntegerOperator uniformIntegerOperator = new UniformIntegerOperator(parameterInt, 10, 1); |
| 43 |
for (int i = 0; i < 300; i++) { |
| 44 |
uniformIntegerOperator.doOperation(); |
| 45 |
countParaValueFrequency(uniformIntegerOperator.getVariable()); |
| 46 |
} |
| 47 |
printCount("Integer Parameter using Staircase Bound"); |
| 48 |
|
| 49 |
|
| 50 |
assertTrue("Expected count[1][0] && [1][1] > 0", count[1][0] > 0 && count[1][1] > 0); |
| 51 |
assertTrue("Expected count[2][0] && [2][1] && [2][2] > 0", count[2][0] > 0 && count[2][1] > 0 && count[2][2] > 0); |
| 52 |
} |
| 53 |
|
| 54 |
private void countParaValueFrequency(Variable para) { |
| 55 |
for (int i = 0; i < para.getSize(); i++) { |
| 56 |
int j; |
| 57 |
if (para.getValue(i) instanceof Double) { |
| 58 |
j = (int) (double) (Double) para.getValue(i); |
| 59 |
count[i][j] += 1; |
| 60 |
} else { |
| 61 |
j = (Integer) para.getValue(i); |
| 62 |
count[i+1][j] += 1; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
private void printCount(String m) { |
| 68 |
System.out.println("\n-----------------------\n"); |
| 69 |
System.out.println(m); |
| 70 |
int i = 0; |
| 71 |
for (int[] row : count) { |
| 72 |
System.out.print(i + " : "); |
| 73 |
i++; |
| 74 |
for (int col : row) { |
| 75 |
System.out.printf("\t%10d", col); |
| 76 |
} |
| 77 |
System.out.print("\n"); |
| 78 |
} |
| 79 |
} |
| 80 |
} |