Search in sources :

Example 6 with StateNode

use of beast.core.StateNode in project beast2 by CompEvol.

the class CompoundValuable method recompute.

/**
 * collect values of the compounds into an array *
 */
private void recompute() {
    int k = 0;
    for (BEASTObject beastObject : m_values.get()) {
        Function valuable = (Function) beastObject;
        if (beastObject instanceof StateNode) {
            valuable = ((StateNode) beastObject).getCurrent();
        }
        int dimension = valuable.getDimension();
        for (int i = 0; i < dimension; i++) {
            m_fValues[k++] = valuable.getArrayValue(i);
        }
    }
    m_bRecompute = false;
}
Also used : Function(beast.core.Function) StateNode(beast.core.StateNode) BEASTObject(beast.core.BEASTObject)

Example 7 with StateNode

use of beast.core.StateNode in project beast2 by CompEvol.

the class OperatorListInputEditor method getLabel.

String getLabel(Operator operator) {
    String name = operator.getClass().getName();
    name = name.substring(name.lastIndexOf('.') + 1);
    name = name.replaceAll("Operator", "");
    if (name.matches(".*[A-Z].*")) {
        name = name.replaceAll("(.)([A-Z])", "$1 $2");
    }
    name += ": ";
    try {
        for (BEASTInterface beastObject2 : operator.listActiveBEASTObjects()) {
            if (beastObject2 instanceof StateNode && ((StateNode) beastObject2).isEstimatedInput.get()) {
                name += beastObject2.getID() + " ";
            }
            // issue https://github.com/CompEvol/beast2/issues/661
            if (name.length() > 100) {
                name += "... ";
                break;
            }
        }
    } catch (Exception e) {
    // ignore
    }
    String tipText = getDoc().tipTextMap.get(operator.getID());
    if (tipText != null) {
        name += " " + tipText;
    }
    return name;
}
Also used : StateNode(beast.core.StateNode) BEASTInterface(beast.core.BEASTInterface)

Example 8 with StateNode

use of beast.core.StateNode in project beast2 by CompEvol.

the class BeautiBase method stateAsString.

String stateAsString() {
    State state = (State) doc.pluginmap.get("state");
    List<StateNode> stateNodes = state.stateNodeInput.get();
    return "assertStateEquals" + pluginListAsString(stateNodes);
}
Also used : State(beast.core.State) StateNode(beast.core.StateNode)

Example 9 with StateNode

use of beast.core.StateNode 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;
}
Also used : StateNode(beast.core.StateNode) ArrayList(java.util.ArrayList) StateNodeInitialiser(beast.core.StateNodeInitialiser) State(beast.core.State) Tree(beast.evolution.tree.Tree) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with StateNode

use of beast.core.StateNode in project beast2 by CompEvol.

the class UpDownOperator method proposal.

/**
 * override this for proposals,
 *
 * @return log of Hastings Ratio, or Double.NEGATIVE_INFINITY if proposal
 *         should not be accepted
 */
@Override
public final double proposal() {
    final double scale = (scaleFactor + (Randomizer.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
    int goingUp = 0, goingDown = 0;
    if (elementWiseInput.get()) {
        int size = 0;
        for (StateNode up : upInput.get()) {
            if (size == 0)
                size = up.getDimension();
            if (size > 0 && up.getDimension() != size) {
                throw new RuntimeException("elementWise=true but parameters of differing lengths!");
            }
            goingUp += 1;
        }
        for (StateNode down : downInput.get()) {
            if (size == 0)
                size = down.getDimension();
            if (size > 0 && down.getDimension() != size) {
                throw new RuntimeException("elementWise=true but parameters of differing lengths!");
            }
            goingDown += 1;
        }
        int index = Randomizer.nextInt(size);
        for (StateNode up : upInput.get()) {
            if (up instanceof RealParameter) {
                RealParameter p = (RealParameter) up;
                p.setValue(p.getValue(index) * scale);
            }
            if (outsideBounds(up)) {
                return Double.NEGATIVE_INFINITY;
            }
        }
        for (StateNode down : downInput.get()) {
            if (down instanceof RealParameter) {
                RealParameter p = (RealParameter) down;
                p.setValue(p.getValue(index) / scale);
            }
            if (outsideBounds(down)) {
                return Double.NEGATIVE_INFINITY;
            }
        }
    } else {
        try {
            for (StateNode up : upInput.get()) {
                up = up.getCurrentEditable(this);
                goingUp += up.scale(scale);
            }
            // Same below for down
            for (StateNode up : upInput.get()) {
                if (outsideBounds(up)) {
                    return Double.NEGATIVE_INFINITY;
                }
            }
            for (StateNode down : downInput.get()) {
                down = down.getCurrentEditable(this);
                goingDown += down.scale(1.0 / scale);
            }
            for (StateNode down : downInput.get()) {
                if (outsideBounds(down)) {
                    return Double.NEGATIVE_INFINITY;
                }
            }
        } catch (Exception e) {
            // scale resulted in invalid StateNode, abort proposal
            return Double.NEGATIVE_INFINITY;
        }
    }
    return (goingUp - goingDown - 2) * Math.log(scale);
}
Also used : StateNode(beast.core.StateNode) RealParameter(beast.core.parameter.RealParameter)

Aggregations

StateNode (beast.core.StateNode)10 State (beast.core.State)5 BEASTInterface (beast.core.BEASTInterface)3 Tree (beast.evolution.tree.Tree)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BEASTObject (beast.core.BEASTObject)2 Function (beast.core.Function)2 RealParameter (beast.core.parameter.RealParameter)2 Distribution (beast.core.Distribution)1 Input (beast.core.Input)1 MCMC (beast.core.MCMC)1 StateNodeInitialiser (beast.core.StateNodeInitialiser)1 Parameter (beast.core.parameter.Parameter)1 CompoundDistribution (beast.core.util.CompoundDistribution)1 Alignment (beast.evolution.alignment.Alignment)1 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)1 TaxonSet (beast.evolution.alignment.TaxonSet)1 BranchRateModel (beast.evolution.branchratemodel.BranchRateModel)1 GenericTreeLikelihood (beast.evolution.likelihood.GenericTreeLikelihood)1