func (p *Probs) GenRule() *rules.Rule { var rule rules.Rule rule.Prm = p.rulePrm st := rules.RuleStates(rule.Prm) rule.Code = make([][][]byte, len(st)) for ln := range st { rule.Code[ln] = make([][]byte, len(st)) for c := range st { rule.Code[ln][c] = make([]byte, len(st)) for rn := range st { randv := rand.Float64() for i, v := range p.probs[ln][c][rn] { // fmt.Println("valor", v) // fmt.Println("valor", randv) randv -= v if randv < 0.0 { rule.Code[ln][c][rn] = rule.Prm.TransitionStates[i] break } } } } } return &rule }
func NewProbs(prm rules.Params) *Probs { var p Probs p.rulePrm = prm st := rules.RuleStates(prm) p.probs = make([][][][]float64, len(st)) for ln := range st { p.probs[ln] = make([][][]float64, len(st)) for c := range st { p.probs[ln][c] = make([][]float64, len(st)) for rn := range st { //p.probs[ln][c][rn] = prm.transitionStates[rand.Intn(len(prm.transitionStates))] p.probs[ln][c][rn] = make([]float64, len(prm.TransitionStates)) for pv := range p.probs[ln][c][rn] { if c != 0 { p.probs[ln][c][rn][pv] = 1.0 / float64(len(p.probs[ln][c][rn])) } else { if pv == len(p.probs[ln][c][rn])-1 { p.probs[ln][c][rn][pv] = 1.0 } else { p.probs[ln][c][rn][pv] = 0.0 } } } } } } return &p }
func (p *Probs) AdjustProbs(pop Population, n_selection, n_tournament int) { // selection := make([]*ca.CellAuto1D, n_selection) mean_fitness := 0.0 var tournament Tournament tournament.rule = make([]*rules.Rule, n_tournament) tournament.fitness = make([]float64, n_tournament) var count [][][][]float64 st := rules.RuleStates(p.rulePrm) count = make([][][][]float64, len(st)) for ln := range st { count[ln] = make([][][]float64, len(st)) for c := range st { count[ln][c] = make([][]float64, len(st)) for rn := range st { //p.probs[ln][c][rn] = prm.transitionStates[rand.Intn(len(prm.transitionStates))] count[ln][c][rn] = make([]float64, len(p.rulePrm.TransitionStates)) } } } // var wg2 sync.WaitGroup // var wg3 sync.WaitGroup for j := 0; j < n_selection; j++ { for i := 0; i < n_tournament; i++ { x := rand.Intn(len(pop.rule)) tournament.rule[i] = pop.rule[x] tournament.fitness[i] = pop.fitness[x] } sort.Sort(sort.Reverse(tournament)) mean_fitness += tournament.fitness[0] n := len(tournament.rule[0].Code) for ln := 0; ln < n; ln++ { for c := 0; c < n; c++ { for rn := 0; rn < n; rn++ { index := bytes.IndexByte(p.rulePrm.TransitionStates, tournament.rule[0].Code[ln][c][rn]) count[ln][c][rn][index] += 1 } } } } n := len(tournament.rule[0].Code) for ln := 0; ln < n; ln++ { for c := 0; c < n; c++ { for rn := 0; rn < n; rn++ { for i := 0; i < len(count[ln][c][rn]); i++ { p.probs[ln][c][rn][i] = count[ln][c][rn][i] / float64(n_selection) } } } } fmt.Println("Mean Fitness:", mean_fitness/float64(n_selection)) }
func (p *Probs) AdjustProbs(pop []Individual) { // n := len(pop.rule) var count [][][][]float64 st := rules.RuleStates(p.rulePrm) count = make([][][][]float64, len(st)) for ln := range st { count[ln] = make([][][]float64, len(st)) for c := range st { count[ln][c] = make([][]float64, len(st)) for rn := range st { //p.probs[ln][c][rn] = prm.transitionStates[rand.Intn(len(prm.transitionStates))] count[ln][c][rn] = make([]float64, len(p.rulePrm.TransitionStates)) } } } for j := 0; j < len(pop); j++ { n := len(pop[0].Rule.Code) for ln := 0; ln < n; ln++ { for c := 0; c < n; c++ { for rn := 0; rn < n; rn++ { index := bytes.IndexByte(p.rulePrm.TransitionStates, pop[j].Rule.Code[ln][c][rn]) count[ln][c][rn][index] += 1 } } } } n := len(pop[0].Rule.Code) for ln := 0; ln < n; ln++ { for c := 0; c < n; c++ { for rn := 0; rn < n; rn++ { for i := 0; i < len(count[ln][c][rn]); i++ { p.probs[ln][c][rn][i] = count[ln][c][rn][i] / float64(len(pop)) } // fmt.Printf("Contagem %f %f %f %f \n", count[ln][c][rn][0], count[ln][c][rn][1], count[ln][c][rn][2], count[ln][c][rn][3]) // fmt.Printf("len pop rule %d \n", len(pop.rule)) // fmt.Printf("Probabilidade %f %f %f %f \n", p.probs[ln][c][rn][0], p.probs[ln][c][rn][1], p.probs[ln][c][rn][2], p.probs[ln][c][rn][3]) } } } }