Debian Codegraph @

src/dr/evolution/tree/RankedNode.java

raw symbols SCIP
1 /*
2 * RankedNode.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.tree;
27
28
29 import jebl.evolution.io.ImportException;
30 import jebl.evolution.io.NewickImporter;
31 import jebl.evolution.trees.RootedTree;
32
33 import java.io.IOException;
34 import java.io.StringReader;
35 import java.util.ArrayList;
36 import java.util.BitSet;
37 import java.util.List;
38
39 /**
40 * @author Alexei Drummond
41 */
42 public class RankedNode {
43
44 int rank;
45 int n;
46 RankedNode child1, child2;
47 //Clade clade;
48
49 BitSet cladeBits;
50
51
52 public RankedNode(int i, int n) {
53 rank = i - n;
54 this.n = n;
55 //clade = new TerminalClade(rank);
56 child1 = null;
57 child2 = null;
58
59 cladeBits = new BitSet(n);
60 cladeBits.set(i);
61 }
62
63 public RankedNode(int rank, RankedNode child1, RankedNode child2) {
64
65 this.rank = rank;
66 n = child1.n;
67
68
69 if (!child1.cladeBits.intersects(child2.cladeBits)) {
70
71 cladeBits = new BitSet();
72 cladeBits.or(child1.cladeBits);
73 cladeBits.or(child2.cladeBits);
74 this.child1 = child1;
75 this.child2 = child2;
76 } else throw new IllegalArgumentException();
77 }
78
79 static BitSet inter = new BitSet();
80
81 public boolean compatible(BitSet constraint) {
82
83 if (cladeBits.intersects(constraint)) {
84 inter.clear();
85 inter.or(cladeBits);
86 inter.and(constraint);
87
88 //System.out.println(cladeBits + " and " + constraint + " : compatible=" + Math.min(cladeBits.cardinality(), constraint.cardinality()));
89
90 return (inter.cardinality() == Math.min(cladeBits.cardinality(), constraint.cardinality()));
91 }
92 return true;
93 }
94
95 public boolean isExternal() {
96 return (child1 == null) && (child2 == null);
97 }
98
99 public RankedNode[] getChildren() {
100 if (isExternal()) return new RankedNode[]{};
101 return new RankedNode[]{child1, child2};
102 }
103
104 public RootedTree getTree() {
105
106 try {
107 NewickImporter importer = new NewickImporter(new StringReader(toNewick()), true);
108 return (RootedTree) importer.importNextTree();
109 } catch (Exception e) {
110 return null;
111 }
112
113 }
114
115 public String toNewick() {
116 return toNewick(this, null);
117 }
118
119 private String toNewick(RankedNode node, RankedNode parent) {
120
121 if (node.isExternal()) {
122 return ((char) (node.rank + node.n + 65)) + ":" + (parent.rank + 1);
123 } else {
124
125 if (node.child1.compare(node.child2) > 0) {
126 RankedNode temp = node.child1;
127 node.child1 = node.child2;
128 node.child2 = temp;
129 }
130
131 return "(" + toNewick(node.child1, node) + ", " + toNewick(node.child2, node) + "):" + ((parent != null) ? parent.rank - node.rank : 0);
132 }
133 }
134
135 private int compare(RankedNode node2) {
136 if (isExternal()) {
137 if (node2.isExternal()) {
138 return rank - node2.rank;
139 } else {
140 return 1 - node2.cladeBits.cardinality();
141 }
142 } else {
143 return cladeBits.cardinality() - node2.cladeBits.cardinality();
144 }
145 }
146
147 // class Clade {
148 //
149 // boolean[] in;
150 //
151 // public Clade() {
152 // in = new boolean[n];
153 // }
154 //
155 // public Clade(Clade c1, Clade c2) {
156 // in = new boolean[n];
157 //
158 // for (int i = 0; i < n; i++) {
159 // if (c1.contains(i - n) && c2.contains(i - n)) {
160 // throw new IllegalArgumentException();
161 // } else {
162 // in[i] = c1.contains(i - n) || c2.contains(i - n);
163 // }
164 // }
165 // }
166 //
167 // public void add(int node) {
168 // in[node + n] = true;
169 // }
170 //
171 // public boolean contains(int node) {
172 // return in[node + n];
173 // }
174 //
175 // public void set(Clade clade) {
176 // System.arraycopy(clade.in, 0, in, 0, in.length);
177 // }
178 //
179 // public boolean equals(Clade c) {
180 // for (int i = 0; i < in.length; i++) {
181 // if (c.in[i] != in[i]) return false;
182 // }
183 // return true;
184 // }
185 //
186 // public boolean compatible(Clade clade) {
187 //
188 // return outsize(clade) == 0 || insize(clade) == 0 || insize(clade) == clade.size();
189 // }
190 //
191 // public int size() {
192 // int size = 0;
193 // for (int i = 0; i < n; i++) {
194 // size += in[i] ? 1 : 0;
195 // }
196 // return size;
197 // }
198 //
199 // private int insize(Clade clade) {
200 // int insize = 0;
201 // for (int i = 0; i < n; i++) {
202 // if (contains(i - n)) {
203 // if (clade.contains(i - n)) {
204 // insize += 1;
205 // }
206 // }
207 // }
208 // return insize;
209 // }
210 //
211 // private int outsize(Clade clade) {
212 // int outsize = 0;
213 // for (int i = 0; i < n; i++) {
214 // if (contains(i - n)) {
215 // if (!clade.contains(i - n)) {
216 // outsize += 1;
217 // }
218 // }
219 // }
220 // return outsize;
221 // }
222 //
223 //
224 // public boolean disjoint(Clade clade) {
225 // for (int i = 0; i < n; i++) {
226 // if (contains(i - n) && clade.contains(i - n)) {
227 // return false;
228 // }
229 // }
230 // return true;
231 // }
232 //
233 // public String toString() {
234 // StringBuilder builder = new StringBuilder();
235 // for (boolean b : in) {
236 // if (b) builder.append('1');
237 // else builder.append('0');
238 // }
239 // return builder.toString();
240 // }
241 //
242 // }
243 //
244 // class TerminalClade extends Clade {
245 // int terminal;
246 //
247 // public TerminalClade(int terminal) {
248 // this.terminal = terminal;
249 // }
250 //
251 // public void add(int node) {
252 // throw new IllegalArgumentException();
253 // }
254 //
255 // public boolean contains(int node) {
256 // return terminal == node;
257 // }
258 //
259 // public void set(Clade clade) {
260 // throw new IllegalArgumentException();
261 // }
262 //
263 // public String toString() {
264 // StringBuilder builder = new StringBuilder();
265 // for (int i = 0; i < n; i++) {
266 // if (contains(i - n)) builder.append('1');
267 // else builder.append('0');
268 // }
269 // return builder.toString();
270 // }
271 // }
272 static long[] Rn;
273
274 public static void main(String[] args) throws IOException, ImportException {
275
276 // String[] constraintStrings = {
277 // "111110000000000",
278 // "111111110000000",
279 // "111111111000000",
280 // "000000000110000",
281 // "000000000001100",
282 // "000000000111100",
283 // "111111111111111"};
284
285 String[] constraintStrings = {
286 "111110000",
287 "111111110"};
288
289 // //String[] constraintStrings = {"00000000011", "00000001100"};
290
291 // String[] constraintStrings = { "0000011", "1110000", "1111111"};
292
293 int n = constraintStrings[0].length();
294
295 Rn = new long[12];
296 Rn[0] = 1;
297 Rn[1] = 1;
298 for (int i = 2; i < Rn.length; i++) {
299 Rn[i] = Rn[i - 1] * ((i + 1) * i / 2);
300 System.out.println((i + 1) + "\t" + Rn[i]);
301 }
302
303 RankedForest start = new RankedForest.Default(n, false);
304
305 List<RankedNode> complete = new ArrayList<RankedNode>();
306
307 List<BitSet> constraints = new ArrayList<BitSet>();
308
309 for (String constraintString : constraintStrings) {
310
311 constraints.add(start.getNodes().get(0).createClade(constraintString));
312 }
313 int[] count = new int[]{0, 0};
314
315 long startTime = System.currentTimeMillis();
316
317 processHistory(start, complete, constraints, count);
318
319 long finishTime = System.currentTimeMillis();
320
321 System.out.println("n = " + n);
322
323 System.out.println("Constraints:");
324 for (BitSet constraint : constraints) {
325 System.out.println(" " + constraint);
326 }
327
328 int total = complete.size() + count[1];
329
330 System.out.println(total + " histories found in " + count[0] + " calls");
331
332 System.out.println("Took " + Math.round((finishTime - startTime) / 10.0) / 100.0 + " seconds");
333
334 System.out.println("Max size of clear forest: " + max);
335 }
336
337 private BitSet createClade(String s) {
338 BitSet bitSet = new BitSet();
339 for (int i = 0; i < s.length(); i++) {
340 if (s.charAt(i) == '1') {
341 bitSet.set(i);
342 }
343 }
344 return bitSet;
345 }
346
347 static int max = 1;
348
349 private static void processHistory(RankedForest history, List<RankedNode> completeHistories, List<BitSet> constraints, int[] count) {
350
351 count[0] += 1;
352
353 List<RankedNode> nodes = history.getNodes();
354 int k = nodes.size();
355 //System.out.println("k = " + k);
356
357 if (k == 1) {
358 count[1] += 1;
359 if (count[0] % 10000000 == 0) System.out.println(count[1]);
360 //completeHistories.add(nodes.get(0));
361 } else {
362
363 if (history.isClear()) {
364 int treeCount = history.getNodes().size();
365 if (treeCount > max) max = treeCount;
366
367 count[1] += Rn[treeCount - 1];
368 } else {
369
370 for (int i = 0; i < k; i++) {
371 for (int j = i + 1; j < k; j++) {
372
373 RankedNode parent = new RankedNode(history.rank() + 1, nodes.get(i), nodes.get(j));
374
375 boolean compatible = true;
376 if (constraints != null) {
377 for (BitSet constraint : constraints) {
378 if (!parent.compatible(constraint)) {
379 //System.out.println(parent.cladeBits + " not compatible with " + constraint);
380 compatible = false;
381 break;
382 }
383 }
384 }
385 if (compatible) {
386
387 //System.out.println(parent.toNewick() + " is compatible!");
388
389 RankedForest newHis = new RankedForest.Parent(history, parent, constraints);
390
391 // check if order of constraints okay
392 if (newHis.compatibleRank(constraints)) {
393 processHistory(newHis, completeHistories, constraints, count);
394 }
395 }
396 //System.out.println(i + " " + j);
397 }
398 }
399 }
400 }
401 }
402 }