示例#1
0
文件: Program.cs 项目: ghchoi0427/GA
        public thing mate(thing one, thing two)
        {
            thing offspring = new thing();
            //Console.WriteLine("Offsping ft:" + offspring.Fitness);

            Random r = new Random();
            int divPoint = r.Next(1, one.Gene.Length - 2);
            //Console.WriteLine("divPoint : "+divPoint);
            for(int i = 0; i < offspring.Gene.Length; i++)
            {
                if (i < divPoint)
                    offspring.Gene[i] = one.Gene[i];
                else
                    offspring.Gene[i] = two.Gene[i];
            }

            foreach(int i in offspring.Gene)
            {
                offspring.Fitness += i;
            }
            //Console.WriteLine("fitness(O) : " + offspring.Fitness);

            return offspring;

        }
示例#2
0
文件: Program.cs 项目: ghchoi0427/GA
 public int getFitness(thing t)
 {
     int fitness = 0;
     foreach (int i in t.Gene)
         fitness += i;
     return fitness;
 }
示例#3
0
文件: Program.cs 项目: ghchoi0427/GA
        public void Generation(ref thing[] things)
        {
            thing[] candidates = new thing[population];
            thing[] newGen = new thing[population];

            things = Create();

            for(int i = 0; i < population; i++)
            {
                candidates[i] = Roulette(things);
            }

            Random r1 = new Random(12342);
            Random r2 = new Random(65313);

             int j = 0;

            for(int i = 0; i < population; i++)
            {
                int idx1 = r1.Next(0, population); 
                 int idx2 = r2.Next(0, population);

                if (idx1 == idx2)
                {
                    Console.Write(j+"/");
                    j++;
                }
                newGen[i] = mate(candidates[idx1], candidates[idx2]);
            }

            Console.WriteLine("Average Fitness : " + getAvr(newGen));
        }
示例#4
0
文件: Program.cs 项目: ghchoi0427/GA
 public void print(thing t)
 {
     foreach(int i in t.Gene)
     {
         Console.Write(" " + i);
     }
     Console.WriteLine("  FItness : {0}",t.Fitness);
 }
示例#5
0
文件: Program.cs 项目: ghchoi0427/GA
 thing[] Create()
 {
     thing[] things = new thing[population];
     for(int i = 0; i < things.Length; i++)
     {
         things[i] = new thing();
     }
     return things;
 }
示例#6
0
文件: thing.cs 项目: ghchoi0427/GA
        public int getFitness(thing t)
        {
            int fitness = 0;

            foreach (int i in t.Gene)
            {
                fitness += i;
            }
            return(fitness);
        }