Example #1
0
 public BirthEventInfo(Vector2 location, float tick, int parentId, string parentTag, int childId, string childTag, AbstractBrainGenotype childGenotype, int speciesId) : base(tick, location)
 {
     ParentId      = parentId;
     ParentTag     = parentTag;
     ChildId       = childId;
     ChildTag      = childTag;
     ChildGenotype = childGenotype;
 }
Example #2
0
        public override float CompareSimilarity(AbstractBrainGenotype other)
        {
            if (!(other is NeatBrainGenotype))
            {
                throw new Exception("Cannot compare two different types of genome.");
            }
            var(disjoint, excess, weightDiff) = CompareConnectionGenes((NeatBrainGenotype)other);

            return((float)(1.0 * disjoint + 1.0 * excess + 0.5 * weightDiff));
        }
Example #3
0
        protected override void OnPoolEntityChanged(EntityManager pool, Entity entity)
        {
            var index = Compatible.IndexOf(entity);

            // If we already have the entity, check if we are still compatible
            if (index >= 0)
            {
                if (!CompatiblePredicate(entity))
                {
                    SwapRemove.SwapRemoveList(Compatible, index);
                }
            }
            else
            // If we do not have it but we are compatible, add it
            {
                if (CompatiblePredicate(entity))
                {
                    Compatible.Add(entity);
                    var brain = entity.GetComponent <BrainComponent>();

                    // This will happen with the initial critters. Place into 'origin' species.
                    if (brain.ParentSpecies is null)
                    {
                        brain.Species = Species[entity.Tag][0];
                        return;
                    }
                    // Check what species the child should go in.
                    // If compatible with the parent species then place it in that species.
                    // Else, create a new species.
                    if (brain.ParentSpecies.Representative.CompareSimilarity(brain.BrainGenotype) <= _similarityThreshold)
                    {
                        brain.Species = brain.ParentSpecies;
                    }
                    else
                    {
                        int newId = NextSpeciesId;
                        NextSpeciesId++;

                        // Create representative for species
                        AbstractBrainGenotype rep = (AbstractBrainGenotype)brain.BrainGenotype.Clone();

                        var newSpecies = new Species()
                        {
                            Id             = newId,
                            Representative = rep,
                            TimeCreated    = _simulation.Tick * _simulation.SimDeltaTime,
                        };
                        // Create new species.
                        Species[entity.Tag].Add(newSpecies);
                        brain.Species = newSpecies;
                    }
                }
            }
        }
Example #4
0
        public void Update(uint tick, float _gameSpeed)
        {
            Random r = new Random();

            for (int i = 0; i < Compatible.Count; i++)
            {
                var parentEntity = Compatible[i];
                var reproduction = parentEntity.GetComponent <ReproductionComponent>();

                if (reproduction.Reproduce > reproduction.ReproductionThreshold)
                {
                    var energy = parentEntity.GetComponent <EnergyComponent>();

                    // check if we have the energy
                    if (!energy.CanAfford(reproduction.ReproductionEnergyCost))
                    {
                        continue;
                    }
                    if (energy.Energy - reproduction.ReproductionEnergyCost < reproduction.RequiredRemainingEnergy)
                    {
                        continue;
                    }

                    // charge teh parent energy. Each critter has an efficency rating, so some energy gets wasted an the rest gets sent to the child.
                    float charged = energy.ChargeEnergy(reproduction.ReproductionEnergyCost);

                    float toChild = reproduction.Efficency * charged;
                    float wasted  = charged - toChild;

                    // Let the energy manager know about the wasted energy.
                    _energyManager.DepositEnergy(wasted);

                    // Some set up for the new child, setting positions/giving energy.
                    var babyEntity = Pool.AddEntityFromDefinition(reproduction.ChildDefinitionId, _simulation.JsonSettings, parentEntity.Tag);

                    babyEntity.GetComponent <EnergyComponent>().Energy = toChild;
                    var parentPosition = parentEntity.GetComponent <TransformComponent>().LocalPosition;
                    babyEntity.GetComponent <TransformComponent>().LocalPosition = parentPosition;

                    // The new child will need a mutated brain
                    var originalBrainComponent = parentEntity.GetComponent <BrainComponent>();
                    var originalBrain          = originalBrainComponent.Brain;


                    AbstractBrainGenotype parentGenotype = ((NeatBrainPhenotype)originalBrain).Genotype;
                    NeatBrainGenotype     childGenotype  = (NeatBrainGenotype)parentGenotype.Clone();


                    _muationManager.Mutate(childGenotype, _innovationIdManager);
                    var newBrain = babyEntity.GetComponent <BrainComponent>();
                    newBrain.SetGenotype(childGenotype);

                    // Do not need to pass it type as we have already set the brain, therefore it does not need initializing.
                    newBrain.SetUpLinks(null, _simulation.Settings);
                    newBrain.ParentSpecies = originalBrainComponent.Species;

                    BirthEventInfo e = new BirthEventInfo(parentPosition, tick * _gameSpeed, parentEntity.Id, parentEntity.Tag, babyEntity.Id, babyEntity.Tag, childGenotype, originalBrainComponent.Species.Id);
                    BirthEvent?.Invoke(e);
                }
            }
        }
 public override float CompareSimilarity(AbstractBrainGenotype other)
 {
     return(0);
 }