Exemple #1
0
func Run(conf Config) error {
	rand.Seed(time.Now().UTC().UnixNano())

	fmt.Println("Loading proteins...")
	id, start, end, err := proteindb.GetProteins(conf.ProteinDB)
	if err != nil {
		panic(err)
	}

	fmt.Println("Initializing probabilities...")
	r, _ := rules.Create(conf.CA.InitStates, conf.CA.TransStates, conf.CA.HasJoker, conf.CA.R)
	probs := NewProbs(r.Prm)
	// fmt.Println(probs)

	var pop Population

	pop.rule = make([]*rules.Rule, conf.EDA.Population)
	pop.fitness = make([]float64, conf.EDA.Population)

	cellAuto := make([]*ca.CellAuto1D, conf.EDA.Population)

	var wg1 sync.WaitGroup
	// tmp := 0
	for i := 0; i < conf.EDA.Population; i++ {
		wg1.Add(1)
		go func(pop *Population, i int) {
			defer wg1.Done()
			pop.rule[i] = probs.GenRule()
			// ca, _ := ca.Create1D(id, start, end, pop.rule[i], conf.CA.Steps, conf.CA.Consensus)
			cellAuto[i], _ = ca.Create1D(id, start, end, pop.rule[i], conf.CA.Steps, conf.CA.Consensus)
			pop.fitness[i] = Fitness(cellAuto[i])
		}(&pop, i) //preciso definir o que por aqui
		if i%48 == 0 && i > 0 {
			fmt.Println("Waiting", i)
			wg1.Wait()
		}
	}
	wg1.Wait()

	fmt.Println("População inicial = ", conf.EDA.Population, "OK")
	// tmp := make([]float64, len(pop.fitness))
	// for j := range tmp {
	// 	tmp[j] = pop.fitness[j] * 1.1
	// }

	var wg2 sync.WaitGroup
	for i := 0; i < conf.EDA.Generations; i++ {
		fmt.Println("Generation", i+1)
		fmt.Println("Adjusting probabilities...")
		probs.AdjustProbs(pop, conf.EDA.Selection, conf.EDA.Tournament)
		fmt.Println("OK")

		if probs.Converged() {
			fmt.Println("Probabilities converged\nDONE")
			break
		}

		if (i+1)%conf.EDA.SaveSteps == 0 {
			ioutil.WriteFile(fmt.Sprintf("%s_%d", conf.EDA.OutputProbs, i+1), []byte(probs.String()), 0644)
		}

		for j := 0; j < len(pop.rule); j++ {
			wg2.Add(1)
			go func(pop *Population, j int) {
				defer wg2.Done()
				pop.rule[j] = probs.GenRule()
				// ca, _ := ca.Create1D(id, start, end, pop.rule[j], conf.CA.Steps, conf.CA.Consensus)
				cellAuto[j].SetRule(pop.rule[j])
				pop.fitness[j] = Fitness(cellAuto[j])

			}(&pop, j)
			if j%24 == 0 && j > 0 {
				fmt.Println("Waiting", j)
				wg2.Wait()
			}
		}
		fmt.Printf("Wait - Setting new rule")
		wg2.Wait()
		fmt.Println("OK")
		//Este é o melhor lugar para criar o grafico? pop
		plot.Histogram(pop.fitness, nil, i)
	}

	err = ioutil.WriteFile(conf.EDA.OutputProbs, []byte(probs.String()), 0644)
	if err != nil {
		fmt.Println("Erro gravar as probabilidades")
		fmt.Println(probs)
	}

	return nil
}
Exemple #2
0
func Run(conf Config) error {
	rand.Seed(time.Now().UTC().UnixNano())

	fmt.Println("Loading proteins...")
	id, start, end, err := proteindb.GetProteins(conf.ProteinDB)
	if err != nil {
		panic(err)
	}

	var pop Population

	pop.rule = make([]*rules.Rule, conf.GA.Population)
	pop.fitness = make([]float64, conf.GA.Population)

	var wg1 sync.WaitGroup
	// tmp := 0
	for i := 0; i < conf.GA.Population; i++ {
		wg1.Add(1)
		go func(pop *Population, i int) {
			defer wg1.Done()
			pop.rule[i], _ = rules.Create(conf.CA.InitStates, conf.CA.TransStates, conf.CA.HasJoker, conf.CA.R)
			ca, _ := ca.Create1D(id, start, end, pop.rule[i], conf.CA.Steps, conf.CA.Consensus)
			pop.fitness[i] = Fitness(ca)
		}(&pop, i) //preciso definir o que por aqui
		if i%100 == 0 && i > 0 {
			fmt.Println("Waiting", i)
			wg1.Wait()
		}
	}
	wg1.Wait()

	var selection Selection
	selection.rule = make([]*rules.Rule, conf.GA.Selection)
	selection.fitness = make([]float64, conf.GA.Selection)

	// var tournament Tournament
	// tournament.rule = make([]*rules.Rule, conf.GA.Tournament)
	// tournament.fitness = make([]float64, conf.GA.Tournament)
	var wg2 sync.WaitGroup
	for i := 0; i < conf.GA.Generations; i++ {
		fmt.Println("Gen", i)
		sort.Sort(sort.Reverse(pop))
		for j := 0; j < conf.GA.Selection; j++ {
			winner := len(pop.rule)
			for k := 0; k < conf.GA.Tournament; k++ {
				x := rand.Intn(len(pop.rule))
				if x < winner {
					winner = x
				}
			}

			//sort.Sort(sort.Reverse(tournament))

			selection.rule[j] = pop.rule[winner]
			selection.fitness[j] = pop.fitness[winner]
		}
		fmt.Println("Selection OK")
		plot.Histogram(pop.fitness, selection.fitness, i)
		fmt.Println("Plot", i, "ok")
		for p := 0; p < len(pop.rule); p++ {
			wg2.Add(1)
			go func(pop *Population, p int) {
				defer wg2.Done()
				s := rand.Intn(len(selection.rule))
				Mutate(selection.rule[s], conf.GA.Mutation)
				pop.rule[p] = selection.rule[s]
				ca, _ := ca.Create1D(id, start, end, pop.rule[p], conf.CA.Steps, conf.CA.Consensus)
				pop.fitness[p] = Fitness(ca)
			}(&pop, p)
			if p%10 == 0 && p > 0 {
				fmt.Println("Waiting", p)
				wg2.Wait()
			}
		}
		fmt.Println("New pop OK")
	}

	return nil
}