protected override void OnUpdate() { // Should only be one Fittest fittestData = Saved.Fittest[0]; float fitnessToBeat = fittestData.fitness; int tag = 0; Nullable <NetData> netToSave = null; for (int i = 0; i < Entities.Length; ++i) { if (Entities.Stats[i].Fitness > fitnessToBeat) { tag = Entities.Stats[i].Tag; fitnessToBeat = Entities.Stats[i].Fitness; netToSave = Entities.Net[i].Data; } } if (netToSave != null) { Debug.Log("FITTEST: " + fitnessToBeat); fittestData.fitness = fitnessToBeat; fittestData.net = netToSave.Value; fittestData.tag = tag; PostUpdateCommands.SetSharedComponent(Saved.Entities[0], fittestData); } }
//Replace least fittest individual from most fittest offspring public void AddFittestOffspring() { Fittest.CalcFitness(); SecondFittest.CalcFitness(); var leastFittestIndex = Population.GetLeastFittestIndex(); Population.Individuals[leastFittestIndex] = GetFittestOffspring(); }
void Start() { // TODO LOAD/SAVE // Initialize fittest Fittest fittest = new Fittest { fitness = 0, net = new NetData() }; gameObjectEntity.EntityManager.AddSharedComponentData <Fittest>(gameObjectEntity.Entity, fittest); StartCoroutine(updateStats()); }
protected void Spawn(int count) { float widthBound = (ground.width / 2) - 1; float lengthBound = (ground.length / 2) - 1; for (int i = 0; i < count; ++i) { Vector3 position = new Vector3( Random.Range(-widthBound, widthBound), prefab.transform.position.y, Random.Range(-lengthBound, lengthBound) ); GameObject obj = Instantiate(prefab, position, Quaternion.identity, parent); obj.name = prefab.name + " Gen-0 (" + parent.childCount + ")"; // TODO GENERIC if (controller != null && controller.Fittest.HasValue) { Debug.Log("HAS FITTEST"); Fittest fittest = controller.Fittest.Value; if (fittest.tag == obj.tag.GetHashCode() && fittest.fitness > 0) { obj.GetComponent <HerbivoreController>().InitalNet = fittest.net; } } else if (saveData != null) { Debug.Log("Using Save Data"); obj.GetComponent <HerbivoreController>().InitalNet = new NetData { LayerSizes = saveData.LayerSizes, Weights = saveData.Weights }; } } }