Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/src/go-pge/problems/treeparams.go @ 16620

Last change on this file since 16620 was 16620, checked in by hmaislin, 5 years ago

#2929: Reorganized folder structure for make script, removed explicit marshalling, erased go-side logging

File size: 7.5 KB
Line 
1package problems
2
3import (
4  "bufio"
5  "fmt"
6  "strconv"
7  "strings"
8
9  expr "go-symexpr"
10)
11
12type TreeParams struct {
13
14  // bounds on tree
15  MaxSize, MaxDepth,
16  MinSize, MinDepth int
17
18  // usable terms at each location
19  RootsS, NodesS, LeafsS, NonTrigS []string
20  RootsT, NodesT, LeafsT, NonTrigT []expr.ExprType
21  Roots, Nodes, Leafs, NonTrig     []expr.Expr
22
23  // simplify options
24  DoSimp bool
25  SRules expr.SimpRules
26
27  // bounds on some operands
28  UsableVars               []int
29  NumDim, NumSys, NumCoeff int
30
31  // tpm bounds on tree (for subtree distributions)
32  TmpMaxSize, TmpMaxDepth,
33  TmpMinSize, TmpMinDepth int
34
35  // Current values
36  CurrSize, CurrDepth int
37  InTrig              bool
38  CoeffCount          int
39}
40
41func (t *TreeParams) Clone() *TreeParams {
42  n := new(TreeParams)
43  n.MaxSize = t.MaxSize
44  n.MaxDepth = t.MaxDepth
45  n.MinSize = t.MinSize
46  n.MinDepth = t.MinDepth
47
48  n.RootsS = make([]string, len(t.RootsS))
49  copy(n.RootsS, t.RootsS)
50  n.NodesS = make([]string, len(t.NodesS))
51  copy(n.NodesS, t.NodesS)
52  n.LeafsS = make([]string, len(t.LeafsS))
53  copy(n.LeafsS, t.LeafsS)
54  n.NonTrigS = make([]string, len(t.NonTrigS))
55  copy(n.NonTrigS, t.NonTrigS)
56
57  n.RootsT = make([]expr.ExprType, len(t.RootsT))
58  copy(n.RootsT, t.RootsT)
59  n.NodesT = make([]expr.ExprType, len(t.NodesT))
60  copy(n.NodesT, t.NodesT)
61  n.LeafsT = make([]expr.ExprType, len(t.LeafsT))
62  copy(n.LeafsT, t.LeafsT)
63  n.NonTrigT = make([]expr.ExprType, len(t.NonTrigT))
64  copy(n.NonTrigT, t.NonTrigT)
65
66  n.Roots = make([]expr.Expr, len(t.Roots))
67  copy(n.Roots, t.Roots)
68  n.Nodes = make([]expr.Expr, len(t.Nodes))
69  copy(n.Nodes, t.Nodes)
70  n.Leafs = make([]expr.Expr, len(t.Leafs))
71  copy(n.Leafs, t.Leafs)
72  n.NonTrig = make([]expr.Expr, len(t.NonTrig))
73  copy(n.NonTrig, t.NonTrig)
74
75  n.DoSimp = t.DoSimp
76  n.SRules = t.SRules
77
78  n.UsableVars = make([]int, len(t.UsableVars))
79  copy(n.UsableVars, t.UsableVars)
80
81  n.NumDim = t.NumDim
82  n.NumSys = t.NumSys
83  n.NumCoeff = t.NumCoeff
84
85  return n
86}
87func ParseTreeParams(field, value string, config interface{}) (found bool, err error) {
88
89  TP := config.(*TreeParams)
90  found = true
91
92  switch strings.ToUpper(field) {
93  case "ROOTS":
94    TP.RootsS = strings.Fields(value)
95    TP.RootsT, TP.Roots = fillExprStuff(TP.RootsS)
96  case "NODES":
97    TP.NodesS = strings.Fields(value)
98    TP.NodesT, TP.Nodes = fillExprStuff(TP.NodesS)
99  case "NONTRIG":
100    TP.NonTrigS = strings.Fields(value)
101    TP.NonTrigT, TP.NonTrig = fillExprStuff(TP.NonTrigS)
102  case "LEAFS":
103    TP.LeafsS = strings.Fields(value)
104    TP.LeafsT, TP.Leafs = fillExprStuff(TP.LeafsS)
105
106  case "USABLEVARS":
107    usable := strings.Fields(value)
108    for _, v := range usable {
109      ival, cerr := strconv.Atoi(v)
110      if cerr != nil {
111        return found, cerr
112      }
113      TP.UsableVars = append(TP.UsableVars, ival)
114    }
115
116  case "MAXSIZE":
117    ival, cerr := strconv.Atoi(value)
118    if cerr != nil {
119      return found, cerr
120    }
121    TP.MaxSize = ival
122  case "MINSIZE":
123    ival, cerr := strconv.Atoi(value)
124    if cerr != nil {
125      return found, cerr
126    }
127    TP.MinSize = ival
128  case "MAXDEPTH":
129    ival, cerr := strconv.Atoi(value)
130    if cerr != nil {
131      return found, cerr
132    }
133    TP.MaxDepth = ival
134  case "MINDEPTH":
135    ival, cerr := strconv.Atoi(value)
136    if cerr != nil {
137      return found, cerr
138    }
139    TP.MinDepth = ival
140
141  default:
142    found = false
143
144  }
145  return
146}
147
148func fillExprStuff(names []string) (types []expr.ExprType, exprs []expr.Expr) {
149  types = make([]expr.ExprType, len(names))
150  exprs = make([]expr.Expr, len(names))
151  for i, n := range names {
152    switch strings.ToLower(n) {
153    case "constant":
154      types[i] = expr.CONSTANT
155      exprs[i] = new(expr.Constant)
156    case "constantf":
157      types[i] = expr.CONSTANTF
158      exprs[i] = new(expr.ConstantF)
159    case "time":
160      types[i] = expr.TIME
161      exprs[i] = new(expr.Time)
162    case "system":
163      types[i] = expr.SYSTEM
164      exprs[i] = new(expr.System)
165    case "var":
166      types[i] = expr.VAR
167      exprs[i] = new(expr.Var)
168
169    case "neg":
170      types[i] = expr.NEG
171      exprs[i] = new(expr.Neg)
172    case "abs":
173      types[i] = expr.ABS
174      exprs[i] = new(expr.Abs)
175    case "sqrt":
176      types[i] = expr.SQRT
177      exprs[i] = new(expr.Sqrt)
178    case "sin":
179      types[i] = expr.SIN
180      exprs[i] = new(expr.Sin)
181    case "cos":
182      types[i] = expr.COS
183      exprs[i] = new(expr.Cos)
184    case "tan":
185      types[i] = expr.TAN
186      exprs[i] = new(expr.Tan)
187    case "exp":
188      types[i] = expr.EXP
189      exprs[i] = new(expr.Exp)
190    case "log":
191      types[i] = expr.LOG
192      exprs[i] = new(expr.Log)
193
194    case "powi":
195      types[i] = expr.POWI
196      exprs[i] = new(expr.PowI)
197    case "powf":
198      types[i] = expr.POWF
199      exprs[i] = new(expr.PowF)
200    case "powe":
201      types[i] = expr.POWE
202      exprs[i] = new(expr.PowE)
203
204    case "add":
205      types[i] = expr.ADD
206      exprs[i] = expr.NewAdd()
207    case "mul":
208      types[i] = expr.MUL
209      exprs[i] = expr.NewMul()
210    case "div":
211      types[i] = expr.DIV
212      exprs[i] = new(expr.Div)
213    default:
214    }
215  }
216  return
217}
218
219func (tp *TreeParams) CheckExprTmp(e expr.Expr) bool {
220  if e.Size() < tp.TmpMinSize || e.Size() > tp.TmpMaxSize ||
221    e.Height() < tp.TmpMinDepth || e.Height() > tp.TmpMaxDepth {
222    return false
223  }
224  return true
225}
226func (tp *TreeParams) CheckExpr(e expr.Expr) bool {
227  if e.Size() < tp.MinSize {
228    //    fmt.Printf( "Too SMALL:  e:%v  l:%v\n", e.Size(), tp.TmpMinSize )
229    return false
230  } else if e.Size() > tp.MaxSize {
231    //    fmt.Printf( "Too LARGE:  e:%v  l:%v\n", e.Size(), tp.TmpMaxSize )
232    return false
233  } else if e.Height() < tp.MinDepth {
234    //    fmt.Printf( "Too SHORT:  e:%v  l:%v\n", e.Height(), tp.TmpMinDepth )
235    return false
236  } else if e.Height() > tp.MaxDepth {
237    //    fmt.Printf( "Too TALL:  e:%v  l:%v\n", e.Height(), tp.TmpMaxDepth )
238    return false
239  }
240  return true
241}
242
243func (tp *TreeParams) CheckExprLog(e expr.Expr, log *bufio.Writer) bool {
244  //   if e.Size() < tp.TmpMinSize || e.Size() > tp.TmpMaxSize ||
245  //     e.Height() < tp.TmpMinDepth || e.Height() > tp.TmpMaxDepth {
246  //       return false
247  //     }
248  if e.Size() < tp.MinSize {
249    return false
250  } else if e.Size() > tp.MaxSize {
251    return false
252  } else if e.Height() < tp.MinDepth {
253    return false
254  } else if e.Height() > tp.MaxDepth {
255    return false
256  }
257  return true
258}
259func (tp *TreeParams) CheckExprPrint(e expr.Expr) bool {
260  //   if e.Size() < tp.TmpMinSize || e.Size() > tp.TmpMaxSize ||
261  //     e.Height() < tp.TmpMinDepth || e.Height() > tp.TmpMaxDepth {
262  //       return false
263  //     }
264  if e.Size() < tp.MinSize {
265    fmt.Printf("Too SMALL:  e:%v  l:%v\n", e.Size(), tp.MinSize)
266    return false
267  } else if e.Size() > tp.MaxSize {
268    fmt.Printf("Too LARGE:  e:%v  l:%v\n", e.Size(), tp.MaxSize)
269    return false
270  } else if e.Height() < tp.MinDepth {
271    fmt.Printf("Too SHORT:  e:%v  l:%v\n", e.Height(), tp.MinDepth)
272    return false
273  } else if e.Height() > tp.MaxDepth {
274    fmt.Printf("Too TALL:  e:%v  l:%v\n", e.Height(), tp.MaxDepth)
275    return false
276  }
277  return true
278}
279
280func (tp *TreeParams) ResetCurr() {
281  tp.CurrSize, tp.CurrDepth, tp.InTrig, tp.CoeffCount = 0, 0, false, 0
282}
283func (tp *TreeParams) ResetTemp() {
284  tp.TmpMaxSize, tp.TmpMaxDepth = tp.MaxSize, tp.MaxDepth
285  tp.TmpMinSize, tp.TmpMinDepth = tp.MinSize, tp.MinDepth
286}
287
288
289func (TP *TreeParams) CreateTreeParams(Roots string, Nodes string, NonTrig string, Leafs string, UsableVars []int, MaxSize int, MinSize int, MaxDepth int, MinDepth int) {
290  TP.RootsS = strings.Fields(Roots)
291  TP.RootsT, TP.Roots = fillExprStuff(TP.RootsS)
292 
293  TP.NodesS = strings.Fields(Nodes)
294  TP.NodesT, TP.Nodes = fillExprStuff(TP.NodesS)
295 
296  TP.NonTrigS = strings.Fields(NonTrig)
297  TP.NonTrigT, TP.NonTrig = fillExprStuff(TP.NonTrigS)
298 
299  TP.LeafsS = strings.Fields(Leafs)
300  TP.LeafsT, TP.Leafs = fillExprStuff(TP.LeafsS)
301
302  TP.UsableVars = UsableVars[:]
303
304  TP.MaxSize = MaxSize
305  TP.MinSize = MinSize
306  TP.MaxDepth = MaxDepth
307  TP.MinDepth = MinDepth
308}
Note: See TracBrowser for help on using the repository browser.