Skip to content

Commit b22c67f

Browse files
committed
count nr of params
1 parent 99a3370 commit b22c67f

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/beast/gss/MultiThreadedNS.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public void initAndValidate() {
4646
String xml = xmlProducer.toRawXML(this);
4747
xml = "<beast version='2.4'>\n" +
4848
xml.replaceAll("spec='" + this.getClass().getCanonicalName() + "'",
49-
"spec='" + NSThread.class.getCanonicalName() + "'")
49+
"spec='" + NSThread.class.getCanonicalName() + "'").
50+
replaceAll("threads='" + threadCount+"'", "")
5051
+ "\n</beast>";
5152

5253
NS = new NSThread[threadCount];

src/beast/gss/NS.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import beast.core.NSLogger;
1919
import beast.core.Operator;
2020
import beast.core.State;
21+
import beast.core.StateNode;
2122
import beast.core.StateNodeInitialiser;
23+
import beast.core.parameter.Parameter;
2224
import beast.core.util.CompoundDistribution;
2325
import beast.core.util.Evaluator;
2426
import beast.core.util.Log;
27+
import beast.evolution.tree.Tree;
2528
import beast.util.NSLogAnalyser;
2629
import beast.util.Randomizer;
2730

@@ -79,6 +82,10 @@ public class NS extends MCMC {
7982
List<Double> likelihoods = new ArrayList<>();
8083
boolean finished = false;
8184

85+
int paramCount = 0;
86+
double paramCountFactor = 1.0;
87+
boolean autoLoopLength = false;
88+
8289
public NS() {}
8390

8491
public NS(int chainLength, int preBurnin, int particleCount, int subChainLength, State state, List<Operator> operators, CompoundDistribution distribution, Double epsilon) {
@@ -155,6 +162,19 @@ public void initAndValidate() {
155162
H = 0;
156163

157164
NSloggers = new ArrayList<>();
165+
166+
167+
paramCount = 0;
168+
for (StateNode node : state.stateNodeInput.get()) {
169+
if (node instanceof Parameter<?>) {
170+
Parameter<?> param = (Parameter<?>) node;
171+
paramCount += param.getDimension();
172+
} else if (node instanceof Tree) {
173+
Tree tree = (Tree) node;
174+
paramCount += tree.getNodeCount() * 2;
175+
}
176+
}
177+
Log.warning("Counting " + paramCount + " parameters");
158178
}
159179

160180
@Override
@@ -512,8 +532,22 @@ protected void updateParticle(int sampleNr) {
512532
oldLogPrior += d.getArrayValue();
513533
}
514534

515-
for (int j = 0; j < subChainLength; j++) {
516-
composeProposal(j + subChainLength * sampleNr);
535+
536+
if (autoLoopLength) {
537+
int acceptCount = 0;
538+
int j = 0;
539+
while (acceptCount < paramCount * paramCountFactor) {
540+
boolean accept = composeProposal(j + subChainLength * sampleNr);
541+
if (accept) {
542+
acceptCount++;
543+
}
544+
j++;
545+
}
546+
//System.err.println(j);
547+
} else {
548+
for (int j = 0; j < subChainLength; j++) {
549+
composeProposal(j + subChainLength * sampleNr);
550+
}
517551
}
518552
}
519553

@@ -546,7 +580,8 @@ public static double logPlus(double x, double y) {
546580
* the current state
547581
* @return the selected {@link beast.core.Operator}
548582
*/
549-
protected Operator composeProposal(final int currState) {
583+
protected boolean composeProposal(final int currState) {
584+
boolean accept = false;
550585
state.store(currState);
551586

552587
final Operator operator = operatorSchedule.selectOperator();
@@ -612,6 +647,7 @@ public double evaluate() {
612647
}
613648
if (printDebugInfo)
614649
System.err.print(" accept");
650+
accept = true;
615651
} else {
616652
// reject
617653
if (currState >= 0) {
@@ -637,6 +673,6 @@ public double evaluate() {
637673
System.err.print(" direct reject");
638674
}
639675
log(currState);
640-
return operator;
676+
return accept;
641677
}
642678
}

0 commit comments

Comments
 (0)