use of beast.core.State in project beast2 by CompEvol.
the class IntegerParameterListTest method test1.
@Test
public void test1() throws Exception {
IntegerParameterList parameterList = new IntegerParameterList();
// Parameters with which to initialise list
IntegerParameter param1 = new IntegerParameter();
param1.initByName("value", "2");
IntegerParameter param2 = new IntegerParameter();
param2.initByName("value", "3");
// Initialise parameter list
parameterList.initByName("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(20);
// Test parameter creation and modification
Parameter<Integer> newParam = parameterList.addNewParam();
newParam.setValue(53);
assertTrue(parameterList.get(0).getValue() == 20);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue() == 3);
assertTrue(parameterList.get(1).getKey() == 1);
assertTrue(parameterList.get(2).getValue() == 53);
assertTrue(parameterList.get(2).getKey() == 2);
assertTrue(parameterList.size() == 3);
parameterList.remove(1);
newParam = parameterList.addNewParam();
newParam.setValue(42);
assertTrue(parameterList.get(0).getValue() == 20);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue() == 53);
assertTrue(parameterList.get(1).getKey() == 2);
assertTrue(parameterList.get(2).getValue() == 42);
assertTrue(parameterList.get(2).getKey() == 1);
assertTrue(parameterList.size() == 3);
// Test state restore
parameterList.restore();
assertTrue(parameterList.get(0).getValue() == 2);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue() == 3);
assertTrue(parameterList.get(1).getKey() == 1);
assertTrue(parameterList.size() == 2);
// Test serialization
String xmlStr = parameterList.toXML();
assertEquals(xmlStr, "<statenode id='null'>" + "Dimension: [1, 1], " + "Bounds: [-2147483647,2147483646], " + "AvailableKeys: [], " + "NextKey: 2, " + "Parameters: [[2],[3]], " + "ParameterKeys: [0,1]" + "</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);
IntegerParameterList newParameterList = new IntegerParameterList();
newParameterList.initAndValidate();
newParameterList.fromXML(docNode);
assertTrue(newParameterList.get(0).getValue() == 2);
assertTrue(newParameterList.get(0).getKey() == 0);
assertTrue(newParameterList.get(1).getValue() == 3);
assertTrue(newParameterList.get(1).getKey() == 1);
assertTrue(newParameterList.size() == 2);
}
use of beast.core.State in project beast2 by CompEvol.
the class JSONParser method createBeastObject.
/**
* create BEASTInterface either using Inputs, or using annotated constructor *
*/
private BEASTInterface createBeastObject(JSONObject node, String ID, String clazzName, List<NameValuePair> inputInfo) throws JSONParserException {
BEASTInterface beastObject = useAnnotatedConstructor(node, ID, clazzName, inputInfo);
if (beastObject != null) {
return beastObject;
}
// create new instance using class name
Object o = null;
try {
Class<?> c = Class.forName(clazzName);
o = c.newInstance();
} catch (InstantiationException e) {
// created for instance because it is abstract
throw new JSONParserException(node, "Cannot instantiate class. Please check the spec attribute.", 1006);
} catch (ClassNotFoundException e) {
// ignore -- class was found in beastObjectNames before
} catch (IllegalAccessException e) {
// T O D O Auto-generated catch block
e.printStackTrace();
throw new JSONParserException(node, "Cannot access class. Please check the spec attribute.", 1011);
}
// set id
beastObject = (BEASTInterface) o;
beastObject.setID(ID);
// hack required to make log-parsing easier
if (o instanceof State) {
state = (State) o;
}
// process inputs for annotated constructors
for (NameValuePair pair : inputInfo) {
setInput(node, beastObject, pair.name, pair.value);
}
// fill in missing inputs, if an input provider is available
try {
if (requiredInputProvider != null) {
for (Input<?> input : beastObject.listInputs()) {
if (input.get() == null && input.getRule() == Validate.REQUIRED) {
Object o2 = requiredInputProvider.createInput(beastObject, input, partitionContext);
if (o2 != null) {
input.setValue(o2, beastObject);
}
}
}
}
} catch (Exception e) {
throw new JSONParserException(node, e.getMessage(), 1008);
}
// sanity check: all attributes should be valid input names
if (!(beastObject instanceof Map)) {
for (String name : node.keySet()) {
if (!(name.equals("id") || name.equals("idref") || name.equals("spec") || name.equals("name"))) {
try {
beastObject.getInput(name);
} catch (Exception e) {
throw new JSONParserException(node, e.getMessage(), 1009);
}
}
}
}
// make sure object o is in outputs of inputs
for (NameValuePair pair : inputInfo) {
if (pair.value instanceof BEASTInterface) {
((BEASTInterface) pair.value).getOutputs().add((BEASTInterface) o);
}
}
register(node, beastObject);
return beastObject;
}
use of beast.core.State in project beast2 by CompEvol.
the class DeltaExchangeOperatorTest method testCanOperate.
@Test
public void testCanOperate() {
// Test whether a validly initialised operator may make proposals
State state = new State();
RealParameter parameter = new RealParameter(new Double[] { 1., 1., 1., 1. });
state.initByName("stateNode", parameter);
state.initialise();
DeltaExchangeOperator d = new DeltaExchangeOperator();
// proposals
try {
d.initByName("parameter", parameter);
} catch (RuntimeException e) {
return;
}
d.proposal();
}
use of beast.core.State in project beast2 by CompEvol.
the class IntRandomWalkOperatorTest method instantiate.
public void instantiate(int dimension, int upper, int runs, Integer[] windowSizes) {
for (int r = 0; r < runs; r++) {
for (Integer windowSize : windowSizes) {
try {
int[][] count = new int[dimension][upper + 1];
Integer[] init = new Integer[dimension];
Arrays.fill(init, 0);
IntegerParameter parameter = new IntegerParameter(init);
parameter.setLower(0);
parameter.setUpper(upper);
State state = new State();
state.initByName("stateNode", parameter);
state.initialise();
IntRandomWalkOperator operator = new IntRandomWalkOperator();
operator.initByName("parameter", parameter, "windowSize", windowSize, "weight", 1.0);
for (int i = 0; i < 1000000 * (upper + 1); i++) {
operator.proposal();
Integer[] values = parameter.getValues();
for (int k = 0; k < values.length; k++) {
int j = values[k];
count[k][j] += 1;
}
}
System.out.print("Distribution lower = 0, upper = " + upper + " windowSize = " + windowSize);
for (int j = 0; j < count.length; j++) {
// System.out.println("x[" +j + "] = " + Arrays.toString(count[j]));
}
int sum = 0;
for (int i = 0; i < dimension; i++) {
for (int k = 0; k < count[i].length; k++) {
sum += Math.abs(count[i][k] - 1000000);
}
}
System.out.println(" Average deviation: " + sum / (dimension * (upper + 1)));
assertTrue("average deviation (" + sum / (dimension * (upper + 1)) + ") exceeds 10000", sum / (dimension * (upper + 1)) < 10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
use of beast.core.State in project beast2 by CompEvol.
the class StateNodeInitialiserListInputEditor method customConnector.
public static boolean customConnector(BeautiDoc doc) {
// scrub Tree initialisers
// 0. collect state node info
List<StateNodeInitialiser> inits = ((MCMC) doc.mcmc.get()).initialisersInput.get();
State state = ((MCMC) doc.mcmc.get()).startStateInput.get();
List<StateNode> stateNodes = state.stateNodeInput.get();
List<Tree> trees = new ArrayList<>();
for (StateNode s : stateNodes) {
if (s instanceof Tree) {
trees.add((Tree) s);
}
}
List<List<StateNode>> initStateNodes = new ArrayList<>();
for (StateNodeInitialiser init : inits) {
List<StateNode> initStateNodes0 = new ArrayList<>();
init.getInitialisedStateNodes(initStateNodes0);
for (int i = initStateNodes0.size() - 1; i >= 0; i--) {
if (!(initStateNodes0.get(i) instanceof Tree)) {
initStateNodes0.remove(i);
}
}
initStateNodes.add(initStateNodes0);
}
// 1. remove initialisers that have no stateNode in state
for (int i = inits.size() - 1; i >= 0; i--) {
boolean found = false;
for (StateNode stateNode : initStateNodes.get(i)) {
if (trees.contains(stateNode)) {
found = true;
break;
}
}
if (!found) {
inits.remove(i);
initStateNodes.remove(i);
}
}
// 2. remove initialisers that share stateNodes
for (int i = inits.size() - 1; i >= 0; i--) {
for (int j = i - 1; j >= 0; j--) {
boolean found = false;
for (StateNode stateNode : initStateNodes.get(i)) {
if (initStateNodes.get(j).contains(stateNode)) {
found = true;
break;
}
}
if (found) {
inits.remove(i);
initStateNodes.remove(i);
}
}
}
// 3. add RandomTree for those trees not having a stateNodeInitialiser
boolean[] hasInitialiser = new boolean[trees.size()];
for (int i = inits.size() - 1; i >= 0; i--) {
for (StateNode stateNode : initStateNodes.get(i)) {
int k = trees.indexOf(stateNode);
if (k >= 0) {
hasInitialiser[k] = true;
break;
}
}
}
for (int i = 0; i < hasInitialiser.length; i++) {
if (!hasInitialiser[i]) {
for (BeautiSubTemplate tmp : doc.beautiConfig.subTemplates) {
if (tmp.getID().equals("RandomTree")) {
PartitionContext partition = doc.getContextFor(trees.get(i));
Object o = tmp.createSubNet(partition, false);
inits.add((StateNodeInitialiser) o);
}
}
}
}
return true;
}
Aggregations