1 | package agents.anac.y2013.MetaAgent.parser.cart.tree;
|
---|
2 | import java.util.HashMap;
|
---|
3 | import java.util.Vector;
|
---|
4 |
|
---|
5 |
|
---|
6 | public abstract class Node {
|
---|
7 |
|
---|
8 | int _id;
|
---|
9 | int _observations;
|
---|
10 | int _leftId;
|
---|
11 | int _rightId;
|
---|
12 | int _leftobs;
|
---|
13 | int _rightObs;
|
---|
14 | Node _left;
|
---|
15 | Node _right;
|
---|
16 | double _complexityParam;
|
---|
17 | Vector<PrimarySplit> _primary_splits;
|
---|
18 | Vector<SurrogateSplit> _Surrogate_splits;
|
---|
19 |
|
---|
20 | enum treeType{
|
---|
21 | MEAN,ESTIMATED
|
---|
22 | }
|
---|
23 |
|
---|
24 | protected Node(int _id, int _observations, int _leftId, int _rightId,
|
---|
25 | int _leftobs, int _rightObs, double _complexityParam,Vector<PrimarySplit> _primary_splits,
|
---|
26 | Vector<SurrogateSplit> _Surrogate_splits) {
|
---|
27 | super();
|
---|
28 | this._id = _id;
|
---|
29 | this._observations = _observations;
|
---|
30 | this._leftId = _leftId;
|
---|
31 | this._rightId = _rightId;
|
---|
32 | this._leftobs = _leftobs;
|
---|
33 | this._rightObs = _rightObs;
|
---|
34 | this._complexityParam = _complexityParam;
|
---|
35 | this._primary_splits = _primary_splits;
|
---|
36 | this._Surrogate_splits = _Surrogate_splits;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | protected static Vector< PrimarySplit> parsePrimarySplits(String[] splitsText) {
|
---|
41 | Vector< PrimarySplit> splits=new Vector<PrimarySplit>();
|
---|
42 | for (String string : splitsText) {
|
---|
43 | PrimarySplit s=PrimarySplit.factory(string);
|
---|
44 | splits.add(s );
|
---|
45 | }
|
---|
46 | return splits;
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected static Vector< SurrogateSplit> parseSurrogateSplits(String[] splitsText) {
|
---|
50 | Vector< SurrogateSplit> splits=new Vector<SurrogateSplit>();
|
---|
51 | for (String string : splitsText) {
|
---|
52 | SurrogateSplit s=SurrogateSplit.factory(string);
|
---|
53 | splits.add(s );
|
---|
54 | }
|
---|
55 | return splits;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | public int get_id() {
|
---|
60 | return _id;
|
---|
61 | }
|
---|
62 | public int get_leftId() {
|
---|
63 | return _leftId;
|
---|
64 | }
|
---|
65 | public int get_rightId() {
|
---|
66 | return _rightId;
|
---|
67 | }
|
---|
68 | public Node get_left() {
|
---|
69 | return _left;
|
---|
70 | }
|
---|
71 | public void set_left(Node _left) {
|
---|
72 | this._left = _left;
|
---|
73 | }
|
---|
74 | public Node get_right() {
|
---|
75 | return _right;
|
---|
76 | }
|
---|
77 | public void set_right(Node _right) {
|
---|
78 | this._right = _right;
|
---|
79 | }
|
---|
80 | protected static int parseObs(String text, String dir) {
|
---|
81 | text=substring(text, dir,"obs)");
|
---|
82 | return Integer.parseInt(text.split(" ")[2].substring(1));
|
---|
83 | }
|
---|
84 | public static String substring(String s,String from,String to){
|
---|
85 | s=s.substring(s.indexOf(from)+from.length());
|
---|
86 | if(s.indexOf(to)!=-1)
|
---|
87 | s=s.substring(0,s.indexOf(to));
|
---|
88 |
|
---|
89 | return s;
|
---|
90 | }
|
---|
91 | public static String substring(String s,String from,int to){
|
---|
92 | return s.substring(s.indexOf(from)+from.length(),to);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public Node getBestNode(HashMap<String,Double> values){
|
---|
96 | if(isLeaf()){
|
---|
97 | return this;
|
---|
98 | }
|
---|
99 | else return navigate(values);
|
---|
100 | }
|
---|
101 | protected Node navigate(HashMap<String, Double> values) {
|
---|
102 | if(this._primary_splits!=null){
|
---|
103 | PrimarySplit.Direction dir=this._primary_splits.get(0).getDirection(values);
|
---|
104 | switch (dir) {
|
---|
105 | case LEFT:{
|
---|
106 | return this._left.getBestNode(values);
|
---|
107 | }
|
---|
108 | case RIGHT:{
|
---|
109 | return this._right.getBestNode(values);
|
---|
110 | }
|
---|
111 | default:
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | if(this._Surrogate_splits!=null){
|
---|
116 | PrimarySplit.Direction dir=this._Surrogate_splits.get(0).getDirection(values);
|
---|
117 | switch (dir) {
|
---|
118 | case LEFT:{
|
---|
119 | // System.out.println(this.get_id() +" choose left" );
|
---|
120 | return this._left.getBestNode(values);
|
---|
121 | }
|
---|
122 | case RIGHT:{
|
---|
123 | // System.out.println(this.get_id() +" choose right");
|
---|
124 | return this._right.getBestNode(values);
|
---|
125 | }
|
---|
126 | default:
|
---|
127 | break;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | return null;
|
---|
131 | }
|
---|
132 | protected boolean isLeaf(){
|
---|
133 | if(this._left==null&&this._right==null)
|
---|
134 | return true;
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | public abstract double get_mean();
|
---|
140 | public abstract double get_estimated_rate();
|
---|
141 | } |
---|