Prechádzať zdrojové kódy

One of the best variations

Alexey Edelev 5 rokov pred
rodič
commit
1daaf563fe

+ 7 - 6
neuralnetwork/genetic/genetic.go

@@ -3,6 +3,7 @@ package genetic
 import (
 	"fmt"
 	"log"
+	"math/rand"
 
 	"sort"
 
@@ -104,12 +105,12 @@ func crossbreed(firstParent, secondParent *neuralnetwork.NeuralNetwork, crossbre
 		firstParentBiases := firstParent.Biases[l]
 		secondParentBiases := secondParent.Biases[l]
 		r, c := firstParentWeights.Dims()
-		// rp := int(float64(r) * crossbreedPart)
-		// cp := int(float64(c) * crossbreedPart)
-		// r = int(rand.Uint32()) % r //(r-rp) + rp
-		// c = int(rand.Uint32()) % c //(c-cp) + cp
-		for i := 0; i < int(float64(r)*crossbreedPart); i++ {
-			// for i := 0; i < r; i++ {
+		rp := int(float64(r) * crossbreedPart)
+		cp := int(float64(c) * crossbreedPart)
+		r = int(rand.Uint32())%(r-rp) + rp
+		c = int(rand.Uint32())%(c-cp) + cp
+		// for i := 0; i < int(float64(r)*crossbreedPart); i++ {
+		for i := 0; i < r; i++ {
 			for j := 0; j < c; j++ {
 				// Swap first half of weights
 				w := firstParentWeights.At(i, j)

+ 1 - 1
neuralnetwork/genetic/mutagen/dummymutagen.go

@@ -24,7 +24,7 @@ func (rm *DummyMutagen) Mutate(network *neuralnetwork.NeuralNetwork) {
 		randomized := rand.Float64()
 		if randomized < rm.chance {
 			r, c := network.Weights[l].Dims()
-			for o := 0; o < 10; o++ {
+			for o := 0; o < 1; o++ {
 				mutationRow := int(rand.Uint32()) % r
 				mutationColumn := int(rand.Uint32()) % c
 				weight := rand.NormFloat64()

+ 2 - 2
neuralnetwork/main.go

@@ -12,11 +12,11 @@ func main() {
 	go rc.Run()
 	s := snakesimulator.NewSnakeSimulator()
 	s.StartServer()
-	p := genetic.NewPopulation(s, mutagen.NewDummyMutagen(50), genetic.PopulationConfig{PopulationSize: 2000, SelectionSize: 0.01, CrossbreedPart: 0.2}, []int{24, 18, 18, 4})
+	p := genetic.NewPopulation(s, mutagen.NewDummyMutagen(50), genetic.PopulationConfig{PopulationSize: 2000, SelectionSize: 0.01, CrossbreedPart: 0.5}, []int{24, 18, 18, 4})
 	for _, net := range p.Networks {
 		net.SetStateWatcher(rc)
 	}
-	p.NaturalSelection(200)
+	p.NaturalSelection(5000)
 	// s.Run()
 
 	// sizes := []int{13, 8, 12, 3}

+ 12 - 11
neuralnetwork/snakesimulator/snakesimulator.go

@@ -159,17 +159,17 @@ func (s *SnakeSimulator) runSnake(inidividual *neuralnetwork.NeuralNetwork, rand
 
 		if selfCollisionIndex := s.snake.SelfCollision(newHead); selfCollisionIndex > 0 {
 			if selfCollisionIndex == 1 {
-				// switch Direction(direction + 1) {
-				// case Direction_Up:
-				// 	newHead = s.snake.NewHead(Direction_Down)
-				// case Direction_Down:
-				// 	newHead = s.snake.NewHead(Direction_Up)
-				// case Direction_Left:
-				// 	newHead = s.snake.NewHead(Direction_Right)
-				// default:
-				// 	newHead = s.snake.NewHead(Direction_Left)
-				// }
-				continue
+				switch Direction(direction + 1) {
+				case Direction_Up:
+					newHead = s.snake.NewHead(Direction_Down)
+				case Direction_Down:
+					newHead = s.snake.NewHead(Direction_Up)
+				case Direction_Left:
+					newHead = s.snake.NewHead(Direction_Right)
+				default:
+					newHead = s.snake.NewHead(Direction_Left)
+				}
+				// continue
 			} else {
 				fmt.Printf("Game over self collision\n")
 				break
@@ -177,6 +177,7 @@ func (s *SnakeSimulator) runSnake(inidividual *neuralnetwork.NeuralNetwork, rand
 		}
 
 		if newHead.X == s.field.Food.X && newHead.Y == s.field.Food.Y {
+			i = 0
 			s.snake.Feed(newHead)
 			s.field.GenerateNextFood()
 			s.fieldUpdateQueue <- true