Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/07/19 06:59:25 (5 years ago)
Author:
hmaislin
Message:

#2929: Updated DLL, result error seems to be fixed, more testing is necessary

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/go-code/go-pge/plug/pge_search.go

    r16231 r16510  
    1717)
    1818
    19 type pgeConfig struct {
     19type PgeConfig struct {
    2020  // search params
    2121  maxGen        int
     
    4040func pgeConfigParser(field, value string, config interface{}) (err error) {
    4141
    42   PC := config.(*pgeConfig)
     42  PC := config.(*PgeConfig)
    4343
    4444  switch strings.ToUpper(field) {
     
    9292type PgeSearch struct {
    9393  id   int
    94   cnfg pgeConfig
     94  cnfg PgeConfig
    9595  prob *probs.ExprProblem
    9696  iter int
     
    151151  return PS.cnfg.maxGen
    152152}
     153func (PS *PgeSearch) GetPeelCount() int {
     154  return PS.cnfg.peelCnt
     155}
    153156func (PS *PgeSearch) SetMaxIter(iter int) {
    154157  PS.cnfg.maxGen = iter
     
    180183
    181184func (PS *PgeSearch) Init(done chan int, prob *probs.ExprProblem, logdir string, input interface{}) {
    182   fmt.Printf("Init'n PGE\n")
     185
     186  //fmt.Printf("Init'n PGE\n")
    183187  // setup data
    184188
    185189  // open logs
    186190  PS.initLogs(logdir)
     191 
     192  PS.stop = false
    187193
    188194  // copy in common config options
     
    195201  PS.cnfg.simprules = srules
    196202
    197   fmt.Println("Roots:   ", PS.cnfg.treecfg.RootsS)
    198   fmt.Println("Nodes:   ", PS.cnfg.treecfg.NodesS)
    199   fmt.Println("Leafs:   ", PS.cnfg.treecfg.LeafsS)
    200   fmt.Println("NonTrig: ", PS.cnfg.treecfg.NonTrigS)
     203  //fmt.Println("Roots:   ", PS.cnfg.treecfg.RootsS)
     204  //fmt.Println("Nodes:   ", PS.cnfg.treecfg.NodesS)
     205  //fmt.Println("Leafs:   ", PS.cnfg.treecfg.LeafsS)
     206  //fmt.Println("NonTrig: ", PS.cnfg.treecfg.NonTrigS)
    201207
    202208  PS.GenRoots = make([]expr.Expr, len(PS.cnfg.treecfg.Roots))
     
    212218    PS.GenNonTrig[i] = PS.cnfg.treecfg.NonTrig[i].Clone()
    213219  }
     220 
     221  fmt.Printf("Init1 %v\n", len(PS.cnfg.treecfg.LeafsT))
    214222
    215223  PS.GenLeafs = make([]expr.Expr, 0)
     
    220228
    221229    case expr.VAR:
    222       fmt.Println("Use Vars: ", PS.cnfg.treecfg.UsableVars)
     230      //fmt.Println("Use Vars: ", PS.cnfg.treecfg.UsableVars)
    223231      for _, i := range PS.cnfg.treecfg.UsableVars {
    224232        PS.GenLeafs = append(PS.GenLeafs, expr.NewVar(i))
     
    232240    }
    233241  }
     242  fmt.Printf("Init2\n")
    234243  /*** FIX ME
    235244  PS.GenLeafs = make([]expr.Expr, len(PS.cnfg.treecfg.Leafs))
     
    238247  }
    239248  ***/
    240 
    241   fmt.Println("Roots:   ", PS.GenRoots)
    242   fmt.Println("Nodes:   ", PS.GenNodes)
    243   fmt.Println("Leafs:   ", PS.GenLeafs)
    244   fmt.Println("NonTrig: ", PS.GenNonTrig)
     249 
     250  fmt.Printf("Init3\n")
     251
     252  //fmt.Println("Roots:   ", PS.GenRoots)
     253  //fmt.Println("Nodes:   ", PS.GenNodes)
     254  //fmt.Println("Leafs:   ", PS.GenLeafs)
     255  //fmt.Println("NonTrig: ", PS.GenNonTrig)
    245256
    246257  // setup communication struct
     
    254265  PS.Best = probs.NewReportQueue()
    255266  PS.Best.SetSort(probs.GPSORT_PARETO_TST_ERR)
     267  fmt.Printf("Init4\n")
     268
    256269  PS.Queue = PS.GenInitExpr()
    257270  PS.Queue.SetSort(probs.PESORT_PARETO_TST_ERR)
     
    264277  PS.eval_out = make(chan *probs.ExprReport, 4048)
    265278
     279  fmt.Printf("Init5a\n")
    266280  for i := 0; i < PS.cnfg.evalrCount; i++ {
    267281    go PS.Evaluate()
    268282  }
     283  fmt.Printf("Init5\n")
    269284}
    270285
     
    276291      continue
    277292    }
    278     PS.eval_out <- RegressExpr(e, PS.prob)
     293    re := RegressExpr(e, PS.prob)
     294    ////fmt.Printf("reg: %v\n", re)
     295    PS.eval_out <- re
    279296  }
    280297
     
    284301  fmt.Printf("Running PGE\n")
    285302
    286   PS.loop()
     303  PS.Loop()
    287304
    288305  fmt.Println("PGE exitting")
     
    292309}
    293310
    294 func (PS *PgeSearch) loop() {
     311func (PS *PgeSearch) Loop() {
    295312
    296313  PS.checkMessages()
     
    298315
    299316    fmt.Println("in: PS.step() ", PS.iter)
    300     PS.step()
     317    PS.Step()
    301318
    302319    // if PS.iter%PS.cnfg.pgeRptEpoch == 0 {
     
    348365}
    349366
    350 func (PS *PgeSearch) step() {
    351 
     367
     368type PeelResult struct {
     369  Es *probs.ExprReport
     370
     371  Nobestpush bool
     372  BestNewMinErr bool
     373 
     374  Bestlen1 int
     375  Bestlen2 int
     376 
     377  Coeff []float64
     378  TestScore int
     379 
     380  Expre expr.Expr
     381  ExpreRe *probs.ExprReport
     382}
     383
     384func (PS *PgeSearch) SingleStep2() {
    352385  loop := 0
    353386  eval_cnt := 0 // for channeled eval
     
    411444      }
    412445    }
    413 
     446   
     447    if doIns {
     448      re.SetProcID(PS.id)
     449      re.SetIterID(PS.iter)
     450      re.SetUnitID(loop)
     451      re.SetUniqID(PS.neqns)
     452      loop++
     453      PS.neqns++
     454     
     455      //fmt.Printf("SS2Queue.Push(): %v\n%v\n\n", re.Expr(), serial)
     456      ////fmt.Printf("INSQueue.Push(): %v\n", re)
     457      ////fmt.Printf("INSQueue.Push(): %v\n", re.Expr())
     458
     459      PS.Queue.Push(re)
     460
     461    }
     462  }
     463  // } // for sequential eval
     464  PS.Queue.Sort()                                                                                                                                       
     465}
     466
     467//Problem
     468func (PS *PgeSearch) SingleStep() []*PeelResult {
     469  if os.Getenv("PGEDEBUG") == "1" {
     470    fmt.Printf("Init PgeSearch: %#v \n", PS)
     471    fmt.Printf("Init PgeProblem: %#v \n", PS.prob)
     472  }
     473
     474
     475  loop := 0
     476  eval_cnt := 0
     477  PR := PS.peelRes()
     478 
     479  es := make([]*probs.ExprReport, len(PR))
     480 
     481  for i, elem := range PR {
     482    if(elem != nil) {
     483      es[i] = elem.ExpreRe
     484    //} else {
     485    //  es[i] = new(probs.ExprReport)
     486    }
     487  }
     488 
     489  ex := PS.expandPeeled(es)
     490
     491  for cnt := range ex {
     492    E := ex[cnt]
     493    if E == nil {
     494      continue
     495    }
     496    for _, e := range E {
     497      if e == nil {
     498        continue
     499      }
     500      if !PS.cnfg.treecfg.CheckExpr(e) {
     501        continue
     502      }
     503      serial := make([]int, 0, 64)
     504      serial = e.Serial(serial)
     505      ins := PS.Trie.InsertSerial(serial)
     506     
     507      if !ins {
     508        continue
     509      }
     510      PS.eval_in <- e
     511      eval_cnt++
     512    }
     513  }
     514  for i := 0; i < eval_cnt; i++ {
     515    re := <-PS.eval_out
     516    if math.IsNaN(re.TestError()) || math.IsInf(re.TestError(), 0) {
     517      continue
     518    }
     519    if re.TestError() < PS.minError {
     520      PS.minError = re.TestError()
     521    }
     522    doIns := true
     523    for _, c := range re.Coeff() {
     524      if math.Abs(c) < PS.cnfg.zeroEpsilon {
     525        doIns = false
     526        break
     527      }
     528    }
     529    if doIns {
     530      re.SetProcID(PS.id)
     531      re.SetIterID(PS.iter)
     532      re.SetUnitID(loop)
     533      re.SetUniqID(PS.neqns)
     534      loop++
     535      PS.neqns++
     536      PS.Queue.Push(re)
     537    }
     538  }
     539  PS.Queue.Sort()
     540
     541  PS.reportExpr()
     542  PS.iter++
     543 
     544  return PR
     545}
     546
     547//Problem
     548func (PS *PgeSearch) peelRes() []*PeelResult {
     549  //es := make([]*probs.ExprReport, PS.cnfg.peelCnt)
     550  PRes := make([]*PeelResult, PS.cnfg.peelCnt)
     551 
     552  for p := 0; p < PS.cnfg.peelCnt && PS.Queue.Len() > 0; p++ {
     553    PR := new(PeelResult)
     554     
     555    PR.BestNewMinErr = false
     556    PR.Nobestpush = false
     557   
     558    e := PS.Queue.Pop().(*probs.ExprReport)
     559   
     560    bPush := true
     561    if len(e.Coeff()) == 1 && math.Abs(e.Coeff()[0]) < PS.cnfg.zeroEpsilon {
     562      //fmt.Println("No Best Push")
     563      PR.Nobestpush = true
     564      p--
     565      continue
     566    }
     567
     568    if bPush {
     569      //fmt.Printf("pop/push (%d,%d): %v\n", p, PS.Best.Len(), e.Expr())
     570      PS.Best.Push(e)
     571    }
     572
     573    //es[p] = e
     574    PR.Bestlen1 = p
     575    PR.Bestlen2 = PS.Best.Len()
     576    PR.Expre = e.Expr()
     577    PR.ExpreRe = e
     578    PR.Es = e
     579    PR.Coeff = e.Coeff()
     580    PR.TestScore = e.TestScore()
     581   
     582   
     583    if e.TestScore() > PS.maxScore {
     584      PS.maxScore = e.TestScore()
     585    }
     586    if e.TestError() < PS.minError {
     587      PS.minError = e.TestError()
     588      PR.BestNewMinErr = true
     589      //fmt.Printf("Best New Min Error:  %v\n", e)
     590    }
     591    if e.Size() > PS.maxSize {
     592      PS.maxSize = e.Size()
     593    }
     594   
     595    PRes[p] = PR
     596  }
     597 
     598  return PRes
     599}
     600
     601func (PS *PgeSearch) Step() int {
     602
     603  loop := 0
     604  eval_cnt := 0 // for channeled eval
     605
     606  es := PS.peel()
     607
     608  ex := PS.expandPeeled(es)
     609 
     610  cnt_ins := 0
     611
     612  for cnt := range ex {
     613    E := ex[cnt]
     614
     615    if E == nil {
     616      continue
     617    }
     618
     619    for _, e := range E {
     620      if e == nil {
     621        continue
     622      }
     623      if !PS.cnfg.treecfg.CheckExpr(e) {
     624        continue
     625      }
     626
     627      // check ipre_trie
     628      serial := make([]int, 0, 64)
     629      serial = e.Serial(serial)
     630      ins := PS.Trie.InsertSerial(serial)
     631      if !ins {
     632        continue
     633      }
     634
     635      // for serial eval
     636      // re := RegressExpr(e, PS.prob)
     637
     638      // start channeled eval
     639      PS.eval_in <- e
     640      eval_cnt++
     641    }
     642  }
     643 
     644  for i := 0; i < eval_cnt; i++ {
     645    re := <-PS.eval_out
     646    // end channeled eval
     647
     648    // check for NaN/Inf in re.error  and  if so, skip
     649    if math.IsNaN(re.TestError()) || math.IsInf(re.TestError(), 0) {
     650      // fmt.Printf("Bad Error\n%v\n", re)
     651      continue
     652    }
     653
     654    if re.TestError() < PS.minError {
     655      PS.minError = re.TestError()
     656    }
     657
     658    // check for coeff == 0
     659    doIns := true
     660    for _, c := range re.Coeff() {
     661      // i > 0 for free coeff
     662      if math.Abs(c) < PS.cnfg.zeroEpsilon {
     663        doIns = false
     664        break
     665      }
     666    }
     667
     668    //fmt.Printf("StepQueue.Push(): %v\n", re)
     669    //fmt.Printf("StepQueue.Push(): %v\n", re.Expr())
     670   
    414671    if doIns {
    415672      re.SetProcID(PS.id)
     
    422679      // fmt.Printf("Queue.Push(): %v\n", re)
    423680      // fmt.Printf("Queue.Push(): %v\n", re.Expr())
    424 
     681      cnt_ins++
    425682      PS.Queue.Push(re)
    426 
    427683    }
    428684  }
     
    430686  PS.Queue.Sort()
    431687
     688  PS.iter++
     689  PS.reportExpr() //new
     690  return 3
    432691}
    433692
    434693func (PS *PgeSearch) peel() []*probs.ExprReport {
    435694  es := make([]*probs.ExprReport, PS.cnfg.peelCnt)
     695 
     696  rpt := make(probs.ExprReportArray, 0)
     697 
    436698  for p := 0; p < PS.cnfg.peelCnt && PS.Queue.Len() > 0; p++ {
    437699
     
    440702    bPush := true
    441703    if len(e.Coeff()) == 1 && math.Abs(e.Coeff()[0]) < PS.cnfg.zeroEpsilon {
    442       fmt.Println("No Best Push")
     704      ////fmt.Println("No Best Push")
    443705      p--
    444706      continue
     
    446708
    447709    if bPush {
    448       fmt.Printf("pop/push(%d,%d): %v\n", p, PS.Best.Len(), e.Expr())
     710      fmt.Printf("BEST PUSH/push(%d,%d): %v\n", p, PS.Best.Len(), e.Expr())
    449711      PS.Best.Push(e)
     712      rpt = append(rpt, e)
    450713    }
    451714
     
    457720    if e.TestError() < PS.minError {
    458721      PS.minError = e.TestError()
    459       fmt.Printf("Best New Min Error:  %v\n", e)
     722      ////fmt.Printf("Best New Min Error:  %v\n", e)
    460723    }
    461724    if e.Size() > PS.maxSize {
    462725      PS.maxSize = e.Size()
    463726    }
    464 
    465   }
     727   
     728  }
     729  PS.commup.Rpts <- &rpt
    466730  return es
    467731}
     
    491755}
    492756
    493 func (PS *PgeSearch) reportExpr() {
    494 
     757//Hier ist das pgeRpt problem, liefert immer die besten pgerpt cnt ergebnisse zurck und ich schneide aus arrazy, werden auch unsortiert
     758// //*
     759func (PS *PgeSearch) reportExpr() int {
     760  //PS.cnfg.pgeRptCount++
    495761  cnt := PS.cnfg.pgeRptCount
    496   PS.Best.Sort()
     762  //*PS.Best.Sort()
    497763
    498764  // repot best equations
    499765  rpt := make(probs.ExprReportArray, cnt)
    500   if PS.Best.Len() < cnt {
    501     cnt = PS.Best.Len()
    502   }
     766  //*if PS.Best.Len() < cnt {
     767  //* cnt = PS.Best.Len()
     768  //*}
    503769  copy(rpt, PS.Best.GetQueue()[:cnt])
    504770
     
    513779  }
    514780
    515   PS.mainLog.Printf("Iter: %d  %f  %f\n", PS.iter, errSum/float64(errCnt), PS.minError)
     781  ////fmt.Printf("Iter: %d  %f  %f\n", PS.iter, errSum/float64(errCnt), PS.minError)
    516782
    517783  PS.ipreLog.Println(PS.iter, PS.neqns, PS.Trie.cnt, PS.Trie.vst)
    518784  PS.fitnessLog.Println(PS.iter, PS.neqns, PS.Trie.cnt, PS.Trie.vst, errSum/float64(errCnt), PS.minError)
    519785
    520   PS.commup.Rpts <- &rpt
    521 
     786  //PS.commup.Rpts <- &rpt
     787 
     788  return errCnt
    522789}
    523790
    524791func (PS *PgeSearch) Clean() {
    525   // fmt.Printf("Cleaning PGE\n")
    526 
    527792  PS.errLogBuf.Flush()
    528793  PS.mainLogBuf.Flush()
     
    530795  PS.fitnessLogBuf.Flush()
    531796  PS.ipreLogBuf.Flush()
    532 
    533797}
    534798
     
    605869  if len(guess) > 0 {
    606870
    607     // fmt.Printf("x_dims:  %d  %d\n", x_dim, x_dim2)
     871    //fmt.Printf("IMREGEXP %v %v\n", P.SearchType, P.SearchVar)
     872    //fmt.Printf("REGPTrain %T | %#v | %v\n", P.Train, P.Train, P.Train)
     873    //fmt.Printf("eqn %T | %#v | %v\n", eqn, eqn, eqn)
     874    //fmt.Printf("guess %T | %#v | %v\n", guess, guess, guess)
    608875
    609876    // Callback version
     877   
     878   
    610879    coeff = levmar.LevmarExpr(eqn, P.SearchVar, P.SearchType, guess, P.Train, P.Test)
     880   
    611881
    612882    // Stack version
     
    641911  R = new(probs.ExprReport)
    642912  R.SetExpr(eqn) /*.ConvertToConstantFs(coeff)*/
     913
    643914  R.SetCoeff(coeff)
    644915  R.Expr().CalcExprStats()
     
    707978  return
    708979}
     980
     981func (PS *PgeSearch) CreateDS(maxGen int, pgeRptEpoch int, pgeRptCount int, pgeArchiveCap int, peelCnt int, evalrCount int, zeroEpsilon float64, initMethod string, growMethod string, sortType int) {
     982  PS.id = 0 //maxGen
     983 
     984  var PC PgeConfig
     985 
     986  PC.pgeRptEpoch = pgeRptEpoch
     987  PC.pgeRptCount = pgeRptCount
     988  PC.pgeArchiveCap = pgeArchiveCap
     989  PC.peelCnt = peelCnt
     990  PC.evalrCount = evalrCount
     991  PC.zeroEpsilon = zeroEpsilon
     992  PC.initMethod = initMethod
     993  PC.growMethod = growMethod
     994 
     995  if sortType == 1 {
     996    PC.sortType = probs.PESORT_PARETO_TRN_ERR
     997  } else { 
     998    PC.sortType = probs.PESORT_PARETO_TST_ERR
     999  }
     1000
     1001  PC.maxGen = maxGen
     1002  PS.cnfg = PC
     1003  //PS.Stop = false is in init
     1004  PS.iter = 0
     1005}
     1006
     1007func (PS *PgeSearch) SetProb(ep *probs.ExprProblem) {
     1008  PS.prob = ep
     1009}
     1010func (PS *PgeSearch) GetIter() int {
     1011  return PS.iter
     1012}
Note: See TracChangeset for help on using the changeset viewer.