Beispiel #1
0
 /// <summary>
 /// A method to breed this gene with another gene
 /// </summary>
 /// <param name="otherGene">The gene to breed this gene with</param>
 /// <returns>The result of breeding these two genes together</returns>
 public Gene breedWith(Gene otherGene)
 {
     Cell[][] newCells = new Cell[10][];
     for (int i = 0; i < cells.Length; i++)
     {
         newCells[i] = new Cell[10];
         for (int j = 0; j < cells.Length; j++)
         {
             int mutationChance = Simulation.getMutationChance();
             if (random.Next(mutationChance) == 1)
             {
                 newCells[i][j] = new Cell(random.Next(7), random.Next(7));
             }
             else
             {
                 int colour1 = this.getPart(i, j);
                 int colour2 = otherGene.getPart(i, j);
                 newCells[i][j] = new Cell(colour1, colour2);
             }
         }
     }
     return new Gene(newCells);
 }
Beispiel #2
0
        /// <summary>
        /// Produces a 10x10 texture representation of a given genome, based on a method found at: 
        /// http://core-fusion.googlecode.com/svn-history/r154/trunk/Game2084/CoreFusion/Graphics/ColorTexture.cs
        /// </summary>
        /// <param name="dna"></param>
        /// <returns></returns>
        public static Texture2D drawGenome(Gene dna)
        {
            GraphicsDeviceManager gdm = Simulation.getGraphicsDeviceManager();
            GraphicsDevice graphicsDevice = gdm.GraphicsDevice;
            Color[] colourMap = Simulation.getColours();
            Color[] geneColours = new Color[dna.getSizeX() * dna.getSizeY()];

            Texture2D tex = new Texture2D(graphicsDevice, dna.getSizeX(), dna.getSizeY());
            int nextFree = 0;
            for(int col = 0; col < dna.getSizeY(); col++)
            {
                for (int row = 0; row < dna.getSizeX(); row++)
                {
                    Color c = new Color(colourMap[dna.getColour(row, col)].ToVector3());
                    geneColours[nextFree] = c;

                    nextFree++;
                }
            }

            tex.SetData(geneColours);
            return tex;
        }
Beispiel #3
0
 /// <summary>
 /// Overloaded constructor for creature, takes a Random object and uses that as the random to create the
 /// Gene object with instead of the WorldState's static RandomNumberGenerator
 /// </summary>
 /// <param name="r">The Random object to be used by this creature and its genome</param>
 public Creature(Random r)
 {
     dna = new Gene(r);
     init(r);
 }
Beispiel #4
0
 /// <summary>
 /// Overloaded constructor for creature, takes a Gene object and uses that as this creature's dna
 /// </summary>
 /// <param name="dna"></param>
 public Creature(Gene dna)
 {
     this.dna = dna;
     init(WorldState.RandomNumberGenerator);
 }
Beispiel #5
0
        private WorldState world; //The world the creature lives in

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Constructor for creatures, sets up a random genome and then calls init
        /// </summary>
        public Creature()
        {
            dna = new Gene();
            init(WorldState.RandomNumberGenerator);
        }
Beispiel #6
0
        /// <summary>
        /// The update method of the Simulation, calls the update method of the current state of the simulation
        /// </summary>
        /// <param name="gameTime">The time since update was last called</param>
        protected override void Update(GameTime gameTime)
        {
            #region testing
            #if CELLTEST
            int[][] colours = new int[6][];
            colours[0] = new int[] { 0, 0 }; //Check the same
            colours[1] = new int[] { 7, 7 }; //Check the same
            colours[2] = new int[] { 1, 3 }; //Check different
            colours[3] = new int[] { 3, 1 }; //Check different but the other way around
            colours[4] = new int[] { 7, 1 }; //Check different and wrapping
            colours[5] = new int[] { 1, 7 }; //Check different and wrapping but the other way around
            foreach(int[] i in colours)
            {
                Cell c = new Cell(i[0], i[1]);
                Console.WriteLine("C1: " + i[0] + " C2: " + i[1] + " DOM: " + c.getDomColour());
            }
            Environment.Exit(0);
            #endif
            #if PATTERNTEST
            //Code for checking various things
            Cell[][] c = new Cell[3][];
            c[0] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(0, 0) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(1, 1), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(1, 1) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(0, 0) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(1, 1), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(0, 0) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(1, 1) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(1, 1), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(0, 0) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(1, 1) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(1, 1), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(1, 1) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(0, 0), new Cell(6, 6), new Cell(1, 1), new Cell(1, 1) };
            c[1] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(6, 6), new Cell(1, 1) };
            c[2] = new Cell[] { new Cell(1, 1), new Cell(1, 1), new Cell(0, 0), new Cell(6, 6) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(0, 0) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(1, 1) };

            Cell[][] rCells = new Cell[1][];
            rCells[0] = new Cell[] { new Cell(1, 1) };
            List<ParamToken> rMods = new List<ParamToken>();
            rMods.Add(ParamToken.STRENGTH);
            recognisedShapes = new List<Shape>();
            recognisedShapes.Add(new Shape(rCells, rMods, new List<ParamToken>()));

            geneTest(new Gene(c));

            Environment.Exit(0);
            #endif
            #if POLLTEST
            Cell[][] newGene = new Cell[10][];
            for (int colour = 0; colour < 7; colour++)
            {
                for (int i = 0; i < newGene.Length; i++)
                {
                    newGene[i] = new Cell[10];
                    for (int j = 0; j < newGene[i].Length; j++)
                    {
                        newGene[i][j] = new Cell(colour, colour);
                    }
                }
                creatureTest(newGene);
            }

            for (int i = 0; i < newGene.Length; i++)
            {
                newGene[i] = new Cell[10];
                for (int j = 0; j < newGene[i].Length; j++)
                {
                    newGene[i][j] = new Cell(0, 0);
                }
            }
            newGene[0][0] = new Cell(0, 0); newGene[0][1] = new Cell(0, 0); newGene[0][2] = new Cell(0, 0);
            newGene[1][0] = new Cell(0, 0); newGene[1][1] = new Cell(0, 0); newGene[1][2] = new Cell(0, 0);
            newGene[2][0] = new Cell(0, 0); newGene[2][1] = new Cell(0, 0); newGene[2][2] = new Cell(1, 1);
            creatureTest(newGene);

            newGene[0][0] = new Cell(0, 0); newGene[0][1] = new Cell(0, 0); newGene[0][2] = new Cell(0, 0);
            newGene[1][0] = new Cell(0, 0); newGene[1][1] = new Cell(2, 2); newGene[1][2] = new Cell(1, 1);
            newGene[2][0] = new Cell(1, 1); newGene[2][1] = new Cell(1, 1); newGene[2][2] = new Cell(1, 1);
            creatureTest(newGene);

            newGene[0][0] = new Cell(1, 1); newGene[0][1] = new Cell(0, 0); newGene[0][2] = new Cell(0, 0);
            newGene[1][0] = new Cell(0, 0); newGene[1][1] = new Cell(2, 2); newGene[1][2] = new Cell(1, 1);
            newGene[2][0] = new Cell(1, 1); newGene[2][1] = new Cell(1, 1); newGene[2][2] = new Cell(0, 0);
            creatureTest(newGene);

            Environment.Exit(0);
            #endif
            #if DIETTEST
            Cell[][] cc = new Cell[10][];
            Cell[][] ch = new Cell[10][];
            for (int i = 0; i < 10; i++)
            {
                cc[i] = new Cell[10];
                ch[i] = new Cell[10];
                for (int j = 0; j < 10; j++)
                {
                    cc[i][j] = new Cell(0, 0);
                    ch[i][j] = new Cell(6, 6);
                }
            }
            Creature carn = new Creature(new Gene(cc));
            Creature herb = new Creature(new Gene(ch));
            List<FoodSource> foodToSort = new List<FoodSource>();
            plantFoodValue = 1000;
            remainsFoodValue = 1000;
            foodToSort.Add(new Plant(new Random()));
            foodToSort.Add(new Remains(new Random()));

            FoodSource carnChoice = carn.pubGetMostNourishing(foodToSort);
            FoodSource herbChoice = herb.pubGetMostNourishing(foodToSort);

            Console.WriteLine("Diet: Carnivore : " + carn.getDiet() + " Herbivore: " + herb.getDiet());
            Console.WriteLine("Actual values: Plant: " + foodToSort[0].getFoodValue() + " Remains: " + foodToSort[1].getFoodValue());
            Console.WriteLine("Carnivore sees plant as: " + carn.pubGetNourishmentAmt(foodToSort[0]) + " and remains as " + carn.pubGetNourishmentAmt(foodToSort[1]));
            Console.WriteLine("Herbivore sees plant as: " + herb.pubGetNourishmentAmt(foodToSort[0]) + " and remains as " + herb.pubGetNourishmentAmt(foodToSort[1]));
            Console.WriteLine("Carnivore chooses meat: " + !carnChoice.isPlant());
            Console.WriteLine("Herbivore chooses plant: " + herbChoice.isPlant());
            Environment.Exit(0);
            #endif
            #if BREEDTEST
            Cell[][] newGene = new Cell[10][];
            for (int i = 0; i < newGene.Length; i++)
            {
                newGene[i] = new Cell[10];
                for (int j = 0; j < newGene[i].Length; j++)
                {
                    newGene[i][j] = new Cell(0, 0);
                }
            }
            Gene g = new Gene(newGene, new Random());
            for (int i = 0; i < 100; i++)
            {
                g.breedWith(g);
            }
            Environment.Exit(0);
            #endif
            #endregion
            base.Update(gameTime);
            state.update(gameTime);
        }
Beispiel #7
0
 private void geneTest(Gene g)
 {
     List<ParamToken> pm = g.getPosMods();
     List<ParamToken> nm = g.getNegMods();
     Console.WriteLine();
     Console.WriteLine("POSITIVES");
     foreach (ParamToken pos in pm)
     {
         Console.WriteLine(pos);
     }
     Console.WriteLine();
     Console.WriteLine("NEGATIVES");
     foreach (ParamToken neg in nm)
     {
         Console.WriteLine(neg);
     }
 }