Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/go-code/go-symexpr/stats.go @ 16183

Last change on this file since 16183 was 16183, checked in by hmaislin, 6 years ago

#2929: Adapted PGE Go code to work as dll

File size: 3.8 KB
Line 
1package symexpr
2
3type ExprParams struct {
4
5  // bounds on tree
6  MaxS, MaxD,
7  MinS, MinD int
8
9  // bounds on some operators
10  NumDim, NumSys, NumCoeff int
11
12  // usable terms at each location
13  Roots, Nodes, Leafs, NonTrig []Expr
14}
15
16type ExprStats struct {
17  depth   int // layers from top (root == 1)
18  height  int // layers of subtree (leaf == 1)
19  size    int
20  pos     int
21  numchld int
22}
23
24func (es *ExprStats) Depth() int       { return es.depth }
25func (es *ExprStats) Height() int      { return es.height }
26func (es *ExprStats) Size() int        { return es.size }
27func (es *ExprStats) Pos() int         { return es.pos }
28func (es *ExprStats) NumChildren() int { return es.numchld }
29
30func (t *Leaf) CalcExprStats() {
31  t.depth = 1
32  t.height = 1
33  t.size = 1
34  t.pos = 0
35  t.numchld = 0
36}
37func (t *Leaf) calcExprStatsR(depth int, pos *int) {
38  t.depth = depth + 1
39  t.height = 1
40  t.size = 1
41  t.pos = *pos
42  (*pos)++
43  t.numchld = 0
44}
45
46func (u *Unary) CalcExprStats() {
47  pos := 1
48  u.depth = 1
49  u.C.calcExprStatsR(1, &pos)
50  u.height = 1 + u.C.Height()
51  u.size = 1 + u.C.Size()
52  u.pos = 0
53  u.numchld = 1
54}
55func (u *Unary) calcExprStatsR(depth int, pos *int) {
56  u.pos = *pos
57  (*pos)++
58  u.depth = depth + 1
59  u.C.calcExprStatsR(depth+1, pos)
60  u.height = 1 + u.C.Height()
61  u.size = 1 + u.C.Size()
62  u.numchld = 1
63}
64
65func (n *N_ary) CalcExprStats() {
66  c_high := 0
67  pos := 1
68  n.depth = 1
69  n.size = 1
70  n.pos = 0
71  n.numchld = len(n.CS)
72  for _, C := range n.CS {
73    if C == nil {
74      continue
75    } else {
76      n.numchld++
77    }
78    C.calcExprStatsR(1, &pos)
79    n.size += C.Size()
80    if c_high < C.Height() {
81      c_high = C.Height()
82    }
83  }
84  n.height = 1 + c_high
85}
86
87func (n *N_ary) calcExprStatsR(depth int, pos *int) {
88  c_high := 0
89  n.depth = depth + 1
90  n.size = 1
91  n.pos = *pos
92  (*pos)++
93  n.numchld = len(n.CS)
94  for _, C := range n.CS {
95    if C == nil {
96      continue
97    } else {
98      n.numchld++
99    }
100    C.calcExprStatsR(depth+1, pos)
101    n.size += C.Size()
102    if c_high < C.Height() {
103      c_high = C.Height()
104    }
105  }
106  n.height = 1 + c_high
107}
108
109func (u *PowI) CalcExprStats() {
110  pos := 1
111  u.Base.calcExprStatsR(1, &pos)
112  u.depth = 1
113  u.height = 1 + u.Base.Height()
114  u.size = 1 + u.Base.Size()
115  u.pos = 0
116  u.numchld = 1
117}
118
119func (u *PowI) calcExprStatsR(depth int, pos *int) {
120  u.pos = *pos
121  (*pos)++
122  u.Base.calcExprStatsR(depth+1, pos)
123  u.depth = depth + 1
124  u.height = 1 + u.Base.Height()
125  u.size = 1 + u.Base.Size()
126  u.numchld = 1
127}
128
129func (u *PowF) CalcExprStats() {
130  pos := 1
131  u.Base.calcExprStatsR(1, &pos)
132  u.depth = 1
133  u.height = 1 + u.Base.Height()
134  u.size = 1 + u.Base.Size()
135  u.pos = 0
136  u.numchld = 1
137}
138
139func (u *PowF) calcExprStatsR(depth int, pos *int) {
140  u.pos = *pos
141  (*pos)++
142  u.Base.calcExprStatsR(depth+1, pos)
143  u.depth = depth + 1
144  u.height = 1 + u.Base.Height()
145  u.size = 1 + u.Base.Size()
146  u.numchld = 1
147}
148
149func max(l, r int) int {
150  if l > r {
151    return l
152  }
153  return r
154}
155
156func (n *PowE) CalcExprStats() {
157  pos := 1
158  n.Base.calcExprStatsR(1, &pos)
159  n.Power.calcExprStatsR(1, &pos)
160  n.depth = 1
161  n.height = 1 + max(n.Base.Height(), n.Power.Height())
162  n.size = 1 + n.Base.Size() + n.Power.Size()
163  n.numchld = 2
164}
165func (n *PowE) calcExprStatsR(depth int, pos *int) {
166  n.pos = *pos
167  (*pos)++
168  n.Base.calcExprStatsR(depth+1, pos)
169  n.Power.calcExprStatsR(depth+1, pos)
170  n.depth = depth + 1
171  n.height = 1 + max(n.Base.Height(), n.Power.Height())
172  n.size = 1 + n.Base.Size() + n.Power.Size()
173  n.numchld = 2
174}
175
176func (n *Div) CalcExprStats() {
177  pos := 1
178  n.Numer.calcExprStatsR(1, &pos)
179  n.Denom.calcExprStatsR(1, &pos)
180  n.depth = 1
181  n.height = 1 + max(n.Numer.Height(), n.Denom.Height())
182  n.size = 1 + n.Numer.Size() + n.Denom.Size()
183  n.numchld = 2
184}
185func (n *Div) calcExprStatsR(depth int, pos *int) {
186  n.pos = *pos
187  (*pos)++
188  n.Numer.calcExprStatsR(depth+1, pos)
189  n.Denom.calcExprStatsR(depth+1, pos)
190  n.depth = depth + 1
191  n.height = 1 + max(n.Numer.Height(), n.Denom.Height())
192  n.size = 1 + n.Numer.Size() + n.Denom.Size()
193  n.numchld = 2
194}
Note: See TracBrowser for help on using the repository browser.