Esempio n. 1
0
 protected override JobHandle OnUpdate(JobHandle inputDeps)
 {
     Entities
     .WithoutBurst()
     .ForEach((in Action action, in  FoodEnergy foodEnergy,
               in  SleepEnergy sleepEnergy, in PosXY posXY, in SimID simID, in Genome genome) => {
         EOSGrid.SetAgent(simID.Value, posXY.Value,
                          FoodEnergy.Fitness(foodEnergy.Value), SleepEnergy.Fitness(sleepEnergy.Value),
                          //foodEnergy.Value, sleepEnergy.Value,
                          action.Value, genome);
     }).Run();
Esempio n. 2
0
 /// <summary>
 /// Find Best
 ///    fill bestAgents with current agents
 ///   sorted by combined fitness.
 /// </summary>
 public void StoreBestAgents()
 {
     bestAgents.Clear();
     foreach (var agent in agents)
     {
         var agf = new AgentFitness();
         agf.genome   = em.GetComponentData <Genome>(agent);
         agf.fitness  = FoodEnergy.Fitness(em.GetComponentData <FoodEnergy>(agent).Value);
         agf.fitness += SleepEnergy.Fitness(em.GetComponentData <SleepEnergy>(agent).Value);
         bestAgents.Add(agf);
     }
     bestAgents.Sort((a, b) => b.fitness.CompareTo(a.fitness)); //high to low sort
 }
Esempio n. 3
0
        public void SetActionSystemTest()
        {
            Experiment.hour = 0;
            var instance = m_Manager.CreateEntity();

            m_Manager.AddComponentData(instance, new Genome {
                h0 = Genome.Allele.Eat
            });
            m_Manager.AddComponentData(instance, new Action {
                Value = (Genome.Allele)Genome.Allele.Eat
            });
            m_Manager.AddComponentData(instance, new FoodEnergy()
            {
                Value = 0
            });
            m_Manager.AddComponentData(instance, new SleepEnergy()
            {
                Value = 0
            });
            var setAction = World.CreateSystem <SetActionSystem>();

            Genome.Allele action;
            Genome.Allele currAllele;
            Genome.Allele expected;

            // test Eat & sleep Alleles in genome
            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    m_Manager.SetComponentData(instance, new Genome {
                        h0 = Genome.Allele.Eat
                    });
                    currAllele = Genome.Allele.Eat;
                    expected   = Genome.Allele.Eat;
                }
                else
                {
                    m_Manager.SetComponentData(instance, new Genome {
                        h0 = Genome.Allele.Sleep
                    });
                    currAllele = Genome.Allele.Sleep;
                    expected   = Genome.Allele.Sleep;
                }

                //This also tests for prev Action == Choose which is not a valid condition
                // but as long this does not fail it does not cause a problem.
                for (int prevAction = 0; prevAction < 3; prevAction++)
                {
                    m_Manager.SetComponentData(instance, new Action {
                        Value = (Genome.Allele)prevAction
                    });
                    for (int foodNRG = 0; foodNRG < 2; foodNRG++)
                    {
                        m_Manager.SetComponentData(instance, new FoodEnergy()
                        {
                            Value = foodNRG
                        });
                        for (int sleepNRG = 0; sleepNRG < 2; sleepNRG++)
                        {
                            m_Manager.SetComponentData(instance, new SleepEnergy()
                            {
                                Value = sleepNRG
                            });
                            setAction.Update();
                            action = m_Manager.GetComponentData <Action>(instance).Value;
                            Assert.AreEqual(expected, action, "{0} {1} {2} {3}", prevAction,
                                            currAllele, 0, 1);
                        }
                    }
                }
            }

            Genome.Allele prevAction2;
            // test Choose Alleles in genome
            m_Manager.SetComponentData(instance, new Genome {
                h0 = Genome.Allele.Choose
            });
            currAllele = Genome.Allele.Choose;

            // FoodNrg == SleepNRG
            m_Manager.SetComponentData(instance, new FoodEnergy()
            {
                Value = 0
            });
            m_Manager.SetComponentData(instance, new SleepEnergy()
            {
                Value = 0
            });
            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    m_Manager.SetComponentData(instance, new Action()
                    {
                        Value = Genome.Allele.Eat
                    });
                    prevAction2 = Genome.Allele.Eat;
                    expected    = Genome.Allele.Eat;
                }
                else
                {
                    m_Manager.SetComponentData(instance, new Action {
                        Value = Genome.Allele.Sleep
                    });
                    prevAction2 = Genome.Allele.Sleep;
                    expected    = Genome.Allele.Sleep;
                }

                setAction.Update();
                action = m_Manager.GetComponentData <Action>(instance).Value;
                Assert.AreEqual(expected, action, "{0} {1} {2} {3}", prevAction2, currAllele, 0, 0);
            }

            // FoodNrg > SleepNRG
            float       foodFitness, sleepFitness;
            FoodEnergy  foodEnergy;
            SleepEnergy sleepEnergy;

            foodEnergy = new FoodEnergy()
            {
                Value = 100
            };
            foodFitness = FoodEnergy.Fitness(foodEnergy.Value);
            sleepEnergy = new SleepEnergy()
            {
                Value = 0
            };
            sleepFitness = SleepEnergy.Fitness(sleepEnergy.Value);
            m_Manager.SetComponentData(instance, foodEnergy);
            m_Manager.SetComponentData(instance, sleepEnergy);
            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    m_Manager.SetComponentData(instance, new Action()
                    {
                        Value = Genome.Allele.Eat
                    });
                    prevAction2 = Genome.Allele.Eat;
                    expected    = Genome.Allele.Sleep;
                }
                else
                {
                    m_Manager.SetComponentData(instance, new Action {
                        Value = Genome.Allele.Sleep
                    });
                    prevAction2 = Genome.Allele.Sleep;
                    expected    = Genome.Allele.Sleep;
                }

                setAction.Update();
                action = m_Manager.GetComponentData <Action>(instance).Value;
                Assert.AreEqual(expected, action, "{0} {1} {2} {3} FF{4} SF {5}",
                                prevAction2, currAllele, foodEnergy.Value, sleepEnergy.Value, foodFitness, sleepFitness);
            }

            // FoodNrg < SleepNRG

            foodEnergy = new FoodEnergy()
            {
                Value = 0
            };
            foodFitness = FoodEnergy.Fitness(foodEnergy.Value);
            sleepEnergy = new SleepEnergy()
            {
                Value = 100
            };
            sleepFitness = SleepEnergy.Fitness(sleepEnergy.Value);
            m_Manager.SetComponentData(instance, foodEnergy);
            m_Manager.SetComponentData(instance, sleepEnergy);
            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    m_Manager.SetComponentData(instance, new Action()
                    {
                        Value = Genome.Allele.Eat
                    });
                    prevAction2 = Genome.Allele.Eat;
                    expected    = Genome.Allele.Eat;
                }
                else
                {
                    m_Manager.SetComponentData(instance, new Action {
                        Value = Genome.Allele.Sleep
                    });
                    prevAction2 = Genome.Allele.Sleep;
                    expected    = Genome.Allele.Eat;
                }

                setAction.Update();
                action = m_Manager.GetComponentData <Action>(instance).Value;
                Assert.AreEqual(expected, action, "{0} {1} {2} {3} FF{4} SF {5}",
                                prevAction2, currAllele, foodEnergy.Value, sleepEnergy.Value, foodFitness, sleepFitness);
            }
        }