示例#1
0
        /// <summary>
        ///     Entry point.
        /// </summary>
        private static void Main()
        {
            var begin = DateTime.UtcNow;
            var previous = DateTime.UtcNow;
            var universe = new World();
            var list = new List<Statistic>(new[] { universe.Statistic });
            list.AddRange(
                Enumerable.Range(1, IterationsNumber).Select(
                    i =>
                        {
                            universe.Run(8);
                            var current = DateTime.UtcNow;
                            WriteLine(
                                $"[{(current - begin).TotalSeconds:000.00}|+{(current - previous).TotalSeconds:000.00}]\t<=-\t{i}/{IterationsNumber}\t-=>");
                            previous = current;
                            return universe.Statistic;
                        }));

            Write(Environment.NewLine);
            PrintPopulationInfo(list);
            PrintGenesInfo(list);
            ForegroundColor = ConsoleColor.White;
            Write(Environment.NewLine);
            Write(Environment.NewLine);
            Write(Environment.NewLine);
            WriteLine("Press enter for exit...");
            ReadLine();
        }
示例#2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Statistic" /> class.
        /// </summary>
        /// <param name="age">
        ///     The age.
        /// </param>
        /// <param name="world">
        ///     The world.
        /// </param>
        public Statistic(int age, World world)
        {
            Debug.Assert(world != null);
            this.Age = age;
            this.SpeciesNumber = world.Species.Length;
            this.PopulationInfo = new int[this.SpeciesNumber];
            var selfishGenes = 0;
            var altruisticGenes = 0;
            var creatureLevelGenes = 0;
            Parallel.For(
                0,
                world.Species.Length,
                i =>
                    {
                        var threadSelfishGenes = 0;
                        var threadAltruisticGenes = 0;
                        var threadCreatureLevelGenes = 0;
                        var creatures = world.Species[i];
                        this.PopulationInfo[i] = creatures.Count;
                        checked
                        {
                            foreach (var creature in creatures)
                            {
                                threadSelfishGenes += creature.SelfishGenes;
                                threadAltruisticGenes += creature.AltruisticGenes;
                                threadCreatureLevelGenes += creature.CreatureLevelGenes;
                            }

                            Interlocked.Add(ref selfishGenes, threadSelfishGenes);
                            Interlocked.Add(ref altruisticGenes, threadAltruisticGenes);
                            Interlocked.Add(ref creatureLevelGenes, threadCreatureLevelGenes);
                        }
                    });

            int allGenes;
            checked
            {
                allGenes = selfishGenes + altruisticGenes + creatureLevelGenes;
            }

            this.SelfishPercentPerCreature = (double)selfishGenes / allGenes;
            this.AltruisticPercentPerCreature = (double)altruisticGenes / allGenes;
            this.CreatureLevelGenesPercentPerCreature = (double)creatureLevelGenes / allGenes;
        }
示例#3
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="Creature" /> class.
 /// </summary>
 /// <param name="idOfSpecies">
 ///     The id of species.
 /// </param>
 /// <param name="world">
 ///     The world.
 /// </param>
 public Creature(int idOfSpecies, World world)
 {
     Contract.Requires<ArgumentNullException>(world != null);
     Contract.Ensures(this.IdOfSpecies == idOfSpecies);
     this.IdOfSpecies = idOfSpecies;
     this.world = world;
     for (var i = 0; i < this.genes.Length; i++)
     {
         this.genes[i] = EnumHelper.CreateRandomGene();
     }
 }
示例#4
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="Creature" /> class.
 /// </summary>
 /// <param name="mommy">
 ///     The mommy.
 /// </param>
 /// <param name="daddy">
 ///     The daddy.
 /// </param>
 public Creature(Creature mommy, Creature daddy)
 {
     Debug.Assert(mommy.IdOfSpecies == daddy.IdOfSpecies, "Interspecies relation are FORBIDDEN!!!");
     this.mother = mommy;
     this.father = daddy;
     mommy.childs.Add(this);
     daddy.childs.Add(this);
     this.world = mommy.world;
     this.IdOfSpecies = mommy.IdOfSpecies;
     for (var i = 0; i < this.genes.Length; i++)
     {
         this.genes[i] = EnumHelper.ChooseRandomGene(mommy.genes[i], daddy.genes[i]);
     }
 }