public void NextStep(bool print) { Parallel.For(0, brains.Count, b => { Brain brain = brains[b]; brain.Correct = 0; BitStorage output = brain.Output; foreach (var pair in numbers) { BitStorage input = pair.Key; bool isPrime = pair.Value; brain.Process(input, output); bool result = output[0]; if (result == isPrime) { brain.Correct++; } } }); brains = brains.OrderBy(b => b.Correct).ToList(); Brain bestBrain = brains[brains.Count - 1]; Brain secondBrain = brains[brains.Count - 2]; Brain worstBrain = brains[0]; brains.Remove(worstBrain); if (print) { Console.WriteLine("best : " + bestBrain.Correct + ", second: " + secondBrain.Correct + ", worst: " + worstBrain.Correct + ", max: " + maxCorrect); } if (bestBrain.Correct > (maxCorrect * 85) / 100) { AddNumber(); } Genome genome = sex.MakeChild(bestBrain.Genome, secondBrain.Genome, random.Next(1, 100)); Brain newBrain = new Brain(); newBrain.Load(genome); brains.Add(newBrain); }
public void Do(IMonster monster) { CellAreaInfo areaInfo = monster.AreaInfo as CellAreaInfo; CellArea area = areaInfo.Area as CellArea; int x = areaInfo.X; int y = areaInfo.Y; int moveNr = (this[0] ? 1 : 0) + (this[1] ? 2 : 0); switch (moveNr) { case 0: y--; break; case 1: y++; break; case 2: x--; break; case 3: x++; break; } Collision = false; Cell targetCell = area.GetCellAt(x, y); switch (targetCell.ContentType) { case ContentType.Empty: areaInfo.MoveTo(x, y); break; case ContentType.Wall: Collision = true; break; case ContentType.Food: monster.Energy += CellArea.FOOD_ENERGY; targetCell.ContentType = ContentType.Empty; break; case ContentType.Monster: Collision = true; if (monster.Energy < CellArea.MONSTER_ACTIVE_SEX_ENERGY) { break; } IMonster targetMonster = (IMonster)targetCell.Content; if (targetMonster.Energy < CellArea.MONSTER_PASSIVE_SEX_ENERGY) { break; } Genome genome = sex.MakeChild(monster.Brain.Genome, targetMonster.Brain.Genome); if (genome != null) { monster.Energy -= CellArea.MONSTER_INIT_ENERGY * 0.5; targetMonster.Energy -= CellArea.MONSTER_INIT_ENERGY * 0.5; area.UsedEnergy -= CellArea.MONSTER_INIT_ENERGY; SimpleMonster.Monster m = area.AddNewMonster(genome) as SimpleMonster.Monster; m.Parents = (monster as SimpleMonster.Monster).Parents + "S"; } //else //{ // // Get energy from target monster // targetMonster.Energy -= CellArea.FOOD_ENERGY; // monster.Energy += CellArea.FOOD_ENERGY; //} break; } }