Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/src/go-pge/pge/pge_expand_method_2.go

Last change on this file 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: 3.9 KB
Line 
1package pge
2
3import (
4  // "fmt"
5  "sort"
6
7  expr "go-symexpr"
8)
9
10func (PS *PgeSearch) ExpandMethod2(O expr.Expr) (ret []expr.Expr) {
11  O.Sort()
12  ret = make([]expr.Expr, 0)
13  // fmt.Printf("Expanding expression:  %v\n", O)
14
15  add := O.(*expr.Add)
16
17  // adding term to addition
18  for _, B := range PS.ffxBases {
19    found := false
20    for _, C := range add.CS {
21      // fmt.Printf("checking %v in %v\n", B, add)
22      if C.AmISame(B) {
23        // fmt.Println("found\n\n")
24        found = true
25        break
26      }
27    }
28    if !found {
29      e := O.Clone()
30      a := e.(*expr.Add)
31      a.Insert(B.Clone())
32      sort.Sort(a)
33      a.CalcExprStats()
34      good := PS.cnfg.treecfg.CheckExpr(a)
35      if good {
36        ret = append(ret, a)
37      }
38      // fmt.Printf("grew  %v\n\n", a)
39      // ret = append(ret, a)
40    }
41  }
42
43  // extending terms in addition
44  for _, B := range PS.ffxBases {
45    for i, C := range add.CS {
46      if C.ExprType() == expr.MUL {
47        m := C.(*expr.Mul)
48        if len(m.CS) > 3 {
49          continue
50        }
51      }
52      e := O.Clone()
53      a := e.(*expr.Add)
54      mul := expr.NewMul()
55      mul.Insert(a.CS[i])
56      mul.Insert(B.Clone())
57      a.CS[i] = mul
58      sort.Sort(a)
59      a.CalcExprStats()
60      good := PS.cnfg.treecfg.CheckExpr(a)
61      if good {
62        ret = append(ret, a)
63      }
64      // fmt.Printf("grew  %v\n\n", a)
65      ret = append(ret, a)
66    }
67  }
68
69  // deepening terms
70  // if len(add.CS) < 2 {
71  //  return ret
72  // }
73  for i, C := range add.CS {
74    if C.ExprType() == expr.MUL {
75      m := C.(*expr.Mul)
76      if len(m.CS) != 2 {
77        continue
78      }
79      if m.CS[1].ExprType() == expr.ADD {
80        continue
81      }
82    } else {
83      continue
84    }
85
86    for _, B := range PS.ffxBases {
87      e := O.Clone()
88      a := e.(*expr.Add)
89      m := a.CS[i].(*expr.Mul)
90      n := m.CS[1]
91
92      switch n.ExprType() {
93      case expr.SQRT:
94        A := expr.NewAdd()
95        A.Insert(n.(*expr.Sqrt).C)
96        A.Insert(B.Clone())
97        n.(*expr.Sqrt).C = A
98      case expr.SIN:
99        A := expr.NewAdd()
100        A.Insert(n.(*expr.Sin).C)
101        A.Insert(B.Clone())
102        n.(*expr.Sin).C = A
103      case expr.COS:
104        A := expr.NewAdd()
105        A.Insert(n.(*expr.Cos).C)
106        A.Insert(B.Clone())
107        n.(*expr.Cos).C = A
108      case expr.TAN:
109        A := expr.NewAdd()
110        A.Insert(n.(*expr.Tan).C)
111        A.Insert(B.Clone())
112        n.(*expr.Tan).C = A
113      case expr.EXP:
114        A := expr.NewAdd()
115        A.Insert(n.(*expr.Exp).C)
116        A.Insert(B.Clone())
117        n.(*expr.Exp).C = A
118      case expr.LOG:
119        A := expr.NewAdd()
120        A.Insert(n.(*expr.Log).C)
121        A.Insert(B.Clone())
122        n.(*expr.Log).C = A
123
124      default:
125        A := expr.NewAdd()
126        A.Insert(m.CS[1])
127        A.Insert(B.Clone())
128        m.CS[1] = A
129      }
130      sort.Sort(a)
131      a.CalcExprStats()
132      good := PS.cnfg.treecfg.CheckExpr(a)
133      if good {
134        ret = append(ret, a)
135      }
136      // fmt.Printf("grew  %v\n", a)
137      ret = append(ret, a)
138    }
139  }
140  for i, C := range add.CS {
141    if C.ExprType() == expr.MUL {
142      m := C.(*expr.Mul)
143      if len(m.CS) != 2 {
144        continue
145      }
146      if m.CS[1].ExprType() == expr.ADD {
147        continue
148      }
149    } else {
150      continue
151    }
152
153    for _, B := range PS.ffxBases {
154      e := O.Clone()
155      a := e.(*expr.Add)
156      m := a.CS[i].(*expr.Mul)
157      n := m.CS[1]
158
159      switch n.ExprType() {
160      case expr.SQRT:
161        M := expr.NewMul()
162        M.Insert(n.(*expr.Sqrt).C)
163        M.Insert(B.Clone())
164        n.(*expr.Sqrt).C = M
165      case expr.SIN:
166        M := expr.NewMul()
167        M.Insert(n.(*expr.Sin).C)
168        M.Insert(B.Clone())
169        n.(*expr.Sin).C = M
170      case expr.COS:
171        M := expr.NewMul()
172        M.Insert(n.(*expr.Cos).C)
173        M.Insert(B.Clone())
174        n.(*expr.Cos).C = M
175      case expr.TAN:
176        M := expr.NewMul()
177        M.Insert(n.(*expr.Tan).C)
178        M.Insert(B.Clone())
179        n.(*expr.Tan).C = M
180      case expr.EXP:
181        M := expr.NewMul()
182        M.Insert(n.(*expr.Exp).C)
183        M.Insert(B.Clone())
184        n.(*expr.Exp).C = M
185      case expr.LOG:
186        M := expr.NewMul()
187        M.Insert(n.(*expr.Log).C)
188        M.Insert(B.Clone())
189        n.(*expr.Log).C = M
190      }
191      sort.Sort(a)
192      a.CalcExprStats()
193      good := PS.cnfg.treecfg.CheckExpr(a)
194      if good {
195        ret = append(ret, a)
196      }
197      // fmt.Printf("grew  %v\n", a)
198      ret = append(ret, a)
199    }
200  }
201  // fmt.Println("Len of ret = ", len(ret))
202  return ret
203}
Note: See TracBrowser for help on using the repository browser.