population.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package gago
  2. import (
  3. "log"
  4. "math/rand"
  5. "time"
  6. )
  7. // A Population contains individuals. Individuals mate within a population.
  8. // Individuals can migrate from one population to another. Each population has a
  9. // random number generator to bypass the global rand mutex.
  10. type Population struct {
  11. Individuals Individuals `json:"indis"`
  12. Age time.Duration `json:"age"`
  13. Generations int `json:"generations"`
  14. ID string `json:"id"`
  15. rng *rand.Rand
  16. }
  17. // Generate a new population.
  18. func newPopulation(size int, gf GenomeFactory, id string) Population {
  19. var (
  20. rng = newRandomNumberGenerator()
  21. pop = Population{
  22. Individuals: newIndividuals(size, gf, rng),
  23. ID: id,
  24. rng: rng,
  25. }
  26. )
  27. return pop
  28. }
  29. // Log a Population's current statistics with a provided log.Logger.
  30. func (pop Population) Log(logger *log.Logger) {
  31. logger.Printf(
  32. "pop_id=%s min=%f max=%f avg=%f std=%f",
  33. pop.ID,
  34. pop.Individuals.FitMin(),
  35. pop.Individuals.FitMax(),
  36. pop.Individuals.FitAvg(),
  37. pop.Individuals.FitStd(),
  38. )
  39. }
  40. // Populations type is necessary for migration and speciation purposes.
  41. type Populations []Population