use of beast.core.State in project beast2 by CompEvol.
the class BooleanParameterListTest method test1.
@Test
public void test1() throws Exception {
BooleanParameterList parameterList = new BooleanParameterList();
// Parameters with which to initialise list
BooleanParameter param1 = new BooleanParameter();
param1.initByName("value", "true false true");
BooleanParameter param2 = new BooleanParameter();
param2.initByName("value", "false true false");
// Initialise parameter list
parameterList.initByName("dimension", 3, "initialParam", param1, "initialParam", param2);
// Create dummy state to allow statenode editing
State state = new State();
state.initByName("stateNode", parameterList);
state.initialise();
// Test parameter value modification
parameterList.get(0).setValue(0, false);
// Test parameter creation and modification
Parameter<Boolean> newParam = parameterList.addNewParam();
newParam.setValue(0, true);
newParam.setValue(1, true);
newParam.setValue(2, true);
assertTrue(parameterList.get(0).getValue(0) == false);
assertTrue(parameterList.get(0).getValue(1) == false);
assertTrue(parameterList.get(0).getValue(2) == true);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue(0) == false);
assertTrue(parameterList.get(1).getValue(1) == true);
assertTrue(parameterList.get(1).getValue(2) == false);
assertTrue(parameterList.get(1).getKey() == 1);
assertTrue(parameterList.get(2).getValue(0) == true);
assertTrue(parameterList.get(2).getValue(1) == true);
assertTrue(parameterList.get(2).getValue(2) == true);
assertTrue(parameterList.get(2).getKey() == 2);
assertTrue(parameterList.size() == 3);
parameterList.remove(1);
newParam = parameterList.addNewParam();
newParam.setValue(0, false);
newParam.setValue(1, false);
newParam.setValue(2, false);
assertTrue(parameterList.get(0).getValue(0) == false);
assertTrue(parameterList.get(0).getValue(1) == false);
assertTrue(parameterList.get(0).getValue(2) == true);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue(0) == true);
assertTrue(parameterList.get(1).getValue(1) == true);
assertTrue(parameterList.get(1).getValue(2) == true);
assertTrue(parameterList.get(1).getKey() == 2);
assertTrue(parameterList.get(2).getValue(0) == false);
assertTrue(parameterList.get(2).getValue(1) == false);
assertTrue(parameterList.get(2).getValue(2) == false);
assertTrue(parameterList.get(2).getKey() == 1);
assertTrue(parameterList.size() == 3);
// Test state restore
parameterList.restore();
assertTrue(parameterList.get(0).getValue(0) == true);
assertTrue(parameterList.get(0).getValue(1) == false);
assertTrue(parameterList.get(0).getValue(2) == true);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue(0) == false);
assertTrue(parameterList.get(1).getValue(1) == true);
assertTrue(parameterList.get(1).getValue(2) == false);
assertTrue(parameterList.get(1).getKey() == 1);
assertTrue(parameterList.size() == 2);
// Test serialization
parameterList.addNewParam(newParam);
String xmlStr = parameterList.toXML();
assertEquals(xmlStr, "<statenode id='null'>" + "Dimension: [3, 1], " + "Bounds: [false,true], " + "AvailableKeys: [], " + "NextKey: 3, " + "Parameters: [[true,false,true],[false,true,false],[false,false,false]], " + "ParameterKeys: [0,1,2]" + "</statenode>\n");
// Test deserialization
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xmlStr.getBytes()));
doc.normalize();
NodeList nodes = doc.getElementsByTagName("*");
org.w3c.dom.Node docNode = nodes.item(0);
BooleanParameterList newParameterList = new BooleanParameterList();
newParameterList.initAndValidate();
newParameterList.fromXML(docNode);
assertTrue(newParameterList.get(0).getValue(0) == true);
assertTrue(newParameterList.get(0).getValue(1) == false);
assertTrue(newParameterList.get(0).getValue(2) == true);
assertTrue(newParameterList.get(0).getKey() == 0);
assertTrue(newParameterList.get(1).getValue(0) == false);
assertTrue(newParameterList.get(1).getValue(1) == true);
assertTrue(newParameterList.get(1).getValue(2) == false);
assertTrue(newParameterList.get(1).getKey() == 1);
assertTrue(newParameterList.get(2).getValue(0) == false);
assertTrue(newParameterList.get(2).getValue(1) == false);
assertTrue(newParameterList.get(2).getValue(2) == false);
assertTrue(newParameterList.get(2).getKey() == 2);
assertTrue(newParameterList.size() == 3);
}
use of beast.core.State in project beast2 by CompEvol.
the class UniformIntegerOperatorTest method testParameterBound.
public void testParameterBound() {
try {
// 4 vaules {0, 1, 2, 3}
count = new int[dimension][4];
RealParameter parameter = new RealParameter(new Double[] { 1.0, 0.0, 2.0 });
parameter.setLower(0.0);
parameter.setUpper(3.0);
State state = new State();
state.initByName("stateNode", parameter);
state.initialise();
UniformOperator uniformOperator = new UniformOperator();
uniformOperator.initByName("parameter", parameter, "howMany", 3, "weight", 1.0);
for (int i = 0; i < 400; i++) {
uniformOperator.proposal();
Double[] values = parameter.getValues();
for (int k = 0; k < values.length; k++) {
int j = (int) (double) values[k];
count[k][j] += 1;
}
}
System.out.println("Discretized real distributions lower = 0.0, upper = 3.0");
for (int j = 0; j < count.length; j++) {
System.out.println("x[" + j + "] = " + Arrays.toString(count[j]));
}
assertTrue("Expected count[0][0-2] > 0 && count[0][3] == 0", (count[0][0] > 0) && (count[0][1] > 0) && (count[0][2] > 0) && (count[0][3] == 0));
assertTrue("Expected count[1][0-2] > 0 && count[1][3] == 0", (count[1][0] > 0) && (count[1][1] > 0) && (count[1][2] > 0) && (count[1][3] == 0));
assertTrue("Expected count[2][0-2] > 0 && count[2][3] == 0", (count[2][0] > 0) && (count[2][1] > 0) && (count[2][2] > 0) && (count[2][3] == 0));
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations