示例#1
0
        static int nChromVals = 1 << chromLeng;         // Number of values for that many bits

        public static void Main(string[] args)
        {
            // Create the population, either from the file or from scratch
            // Presumably the popSize would be the number of NPCs that will be
            // spawned for a round.  The data file name is set here as well by
            // passing it into the constructor.
            // The 0.9 is the crossover probability, and the 0.1 is the mutation
            // probability, which will be used in breeding new individuals.
            // Normally, the crossover probability should be high (near 1.0),
            // and the mutation probability should be low (near 0.0).
            ThreshPop tp = new ThreshPop(chromLeng, popSize, "test1.txt", 0.9, 0.1);

            // Local storage for the chromosomes and fitness values to demonstrate
            // how the ThreshPop is used.  In this case, we'll just store an array
            // of chromosomes to represent the checked out population and manipulate
            // them in simple loops to make something happen.
            // In your game, a given threshold would be an attribute of an NPC,
            // and the fitness would be determined when that NPC is "done".
            uint [] chroms = new uint[popSize];

            // Check out all the individuals from the population to get their chroms.
            // In your game a CheckOut would be done when an NPC is spawned,
            // one at a time.
            int i = 0;

            while (!tp.AllCheckedOut())
            {
                chroms[i] = tp.CheckOut();
                i++;
            }

            // Determine fitness values for everyone & check them all back into
            // the new population.
            // In the your game, this would happen one at a time as each NPC
            // is "done" and its fitness can be figured.
            // Note that the Individuals can be checked into the new population
            // in any order, likely not the order they were checked out.
            i = 0;
            while (!tp.AllCheckedIn())
            {
                int fit = Fitness(Gen2Phen(chroms[i]));                 // Determine fitness
                tp.CheckIn(chroms[i], fit);                             // CheckIn to next generation
                i++;
            }

            // Save the new population for next time.
            // This would be done at the end of each "round" of your game.
            tp.WritePop();

            // Display new population on the Console to see what happened.
            tp.DisplayPop(1);
        }
示例#2
0
文件: Program.cs 项目: Nolnocn/Siege
        static int popSize = 20; // Population size

        #endregion Fields

        #region Methods

        public static void Main(string[] args)
        {
            // Create the population, either from the file or from scratch
            // Presumably the popSize would be the number of NPCs that will be
            // spawned for a round.  The data file name is set here as well by
            // passing it into the constructor.
            // The 0.9 is the crossover probability, and the 0.1 is the mutation
            // probability, which will be used in breeding new individuals.
            // Normally, the crossover probability should be high (near 1.0),
            // and the mutation probability should be low (near 0.0).
            ThreshPop tp = new ThreshPop(chromLeng, popSize, "test1.txt", 0.9, 0.1);

            // Local storage for the chromosomes and fitness values to demonstrate
            // how the ThreshPop is used.  In this case, we'll just store an array
            // of chromosomes to represent the checked out population and manipulate
            // them in simple loops to make something happen.
            // In your game, a given threshold would be an attribute of an NPC,
            // and the fitness would be determined when that NPC is "done".
            uint [] chroms = new uint[popSize];

            // Check out all the individuals from the population to get their chroms.
            // In your game a CheckOut would be done when an NPC is spawned,
            // one at a time.
            int i = 0;
            while (! tp.AllCheckedOut())
            {
                chroms[i] = tp.CheckOut();
                i++;
            }

            // Determine fitness values for everyone & check them all back into
            // the new population.
            // In the your game, this would happen one at a time as each NPC
            // is "done" and its fitness can be figured.
            // Note that the Individuals can be checked into the new population
            // in any order, likely not the order they were checked out.
            i = 0;
            while (! tp.AllCheckedIn())
            {
                int fit = Fitness(Gen2Phen(chroms[i]));	// Determine fitness
                tp.CheckIn(chroms[i], fit);				// CheckIn to next generation
                i++;
            }

            // Save the new population for next time.
            // This would be done at the end of each "round" of your game.
            tp.WritePop();

            // Display new population on the Console to see what happened.
            tp.DisplayPop(1);
        }