genetic.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package genetic
  2. import (
  3. "fmt"
  4. "log"
  5. "sort"
  6. neuralnetwork "../neuralnetwork"
  7. )
  8. type PopulationConfig struct {
  9. PopulationSize int
  10. SelectionSize float64 // 0..1 persentage of success individuals to be used as parents for population
  11. CrossbreedPart float64 // 0..1 persentage of weights and biases to be exchanged beetween individuals while Crossbreed
  12. }
  13. type Population struct {
  14. populationConfig *PopulationConfig
  15. Networks []*neuralnetwork.NeuralNetwork
  16. verifier PopulationVerifier
  17. mutagen Mutagen
  18. etalonsCount int
  19. }
  20. func NewPopulation(verifier PopulationVerifier, mutagen Mutagen, populationConfig PopulationConfig, sizes []int) (p *Population) {
  21. if populationConfig.PopulationSize%2 != 0 {
  22. return nil
  23. }
  24. p = &Population{
  25. populationConfig: &populationConfig,
  26. Networks: make([]*neuralnetwork.NeuralNetwork, populationConfig.PopulationSize),
  27. verifier: verifier,
  28. mutagen: mutagen,
  29. etalonsCount: int(float64(populationConfig.PopulationSize) * populationConfig.SelectionSize),
  30. }
  31. if p.etalonsCount%2 != 0 {
  32. p.etalonsCount -= 1
  33. }
  34. for i := 0; i < populationConfig.PopulationSize; i++ {
  35. var err error
  36. p.Networks[i], err = neuralnetwork.NewNeuralNetwork(sizes, nil)
  37. if err != nil {
  38. log.Fatal("Could not initialize NeuralNetwork")
  39. }
  40. }
  41. return
  42. }
  43. func (p *Population) NaturalSelection(generationCount int) {
  44. for g := 0; g < generationCount; g++ {
  45. p.crossbreedPopulation(p.verifier.Verify(p))
  46. }
  47. }
  48. func (p *Population) crossbreedPopulation(fitnesses []*IndividalFitness) {
  49. sort.Slice(fitnesses, func(i, j int) bool {
  50. return fitnesses[i].Fitness > fitnesses[j].Fitness //Descent order best will be on top, worst in the bottom
  51. })
  52. //Collect etalons from upper part of neural network list and crossbreed/mutate them
  53. etalonNetworks := make([]*neuralnetwork.NeuralNetwork, p.etalonsCount)
  54. for i := 1; i < p.etalonsCount; i += 2 {
  55. firstParent := fitnesses[i-1].Index
  56. secondParent := fitnesses[i].Index
  57. fmt.Printf("Result i %v firstParent %v secondParent %v firstFitness %v secondFitness %v\n", i, firstParent, secondParent, fitnesses[i-1].Fitness, fitnesses[i].Fitness)
  58. etalonNetworks[i-1] = p.Networks[firstParent].Copy()
  59. etalonNetworks[i] = p.Networks[secondParent].Copy()
  60. crossbreed(p.Networks[firstParent], p.Networks[secondParent], p.populationConfig.CrossbreedPart)
  61. p.mutagen.Mutate(p.Networks[firstParent])
  62. p.mutagen.Mutate(p.Networks[secondParent])
  63. }
  64. //Rest of networks are based on collected etalons but crossbreed/mutate own way
  65. for i := p.etalonsCount + 1; i < p.populationConfig.PopulationSize; i += 2 {
  66. firstParent := fitnesses[i-1].Index
  67. secondParent := fitnesses[i].Index
  68. fmt.Printf("Result i %v firstParent %v secondParent %v firstFitness %v secondFitness %v firstEtalon %v secondEtalon %v\n",
  69. i, firstParent, secondParent,
  70. fitnesses[i-1].Fitness, fitnesses[i].Fitness,
  71. fitnesses[(i-1)%p.etalonsCount].Index, fitnesses[(i)%p.etalonsCount].Index)
  72. firstParentEtalon := etalonNetworks[(i-1)%p.etalonsCount]
  73. secondParenEtalon := etalonNetworks[(i)%p.etalonsCount]
  74. p.Networks[firstParent] = firstParentEtalon.Copy()
  75. p.Networks[secondParent] = secondParenEtalon.Copy()
  76. crossbreed(p.Networks[firstParent], p.Networks[secondParent], p.populationConfig.CrossbreedPart)
  77. p.mutagen.Mutate(p.Networks[firstParent])
  78. p.mutagen.Mutate(p.Networks[secondParent])
  79. }
  80. }
  81. func crossbreed(firstParent, secondParent *neuralnetwork.NeuralNetwork, crossbreedPart float64) {
  82. for l := 1; l < firstParent.LayerCount; l++ {
  83. firstParentWeights := firstParent.Weights[l]
  84. secondParentWeights := secondParent.Weights[l]
  85. firstParentBiases := firstParent.Biases[l]
  86. secondParentBiases := secondParent.Biases[l]
  87. r, c := firstParentWeights.Dims()
  88. // rp := int(float64(r) * crossbreedPart)
  89. // cp := int(float64(c) * crossbreedPart)
  90. // r = int(rand.Uint32()) % r //(r-rp) + rp
  91. // c = int(rand.Uint32()) % c //(c-cp) + cp
  92. for i := 0; i < int(float64(r)*crossbreedPart); i++ {
  93. // for i := 0; i < r; i++ {
  94. for j := 0; j < c; j++ {
  95. // Swap first half of weights
  96. w := firstParentWeights.At(i, j)
  97. firstParentWeights.Set(i, j, secondParentWeights.At(i, j))
  98. secondParentWeights.Set(i, j, w)
  99. }
  100. // Swap first half of biases
  101. b := firstParentBiases.At(i, 0)
  102. firstParentBiases.Set(i, 0, secondParentBiases.At(i, 0))
  103. secondParentBiases.Set(i, 0, b)
  104. }
  105. }
  106. }