예제 #1
0
        public override IChromosome Clone()
        {
            ProgramChromosome newChromosome = (ProgramChromosome)this.CreateNew();

            for (int i = 0; i < this.slotCount; ++i)
            {
                newChromosome.program[i] = this.program[i];
            }
            return(newChromosome);
        }
예제 #2
0
        public double Evaluate(IChromosome chromosome)
        {
            ProgramChromosome programChromosome = (ProgramChromosome)chromosome;

            Debug.Assert(this.totalSlots == programChromosome.SlotCount);
            Program program = this.ChromosomeToProgram(programChromosome);

            int starsEaten;

            this.puzzle.TrySolveWith(program, out starsEaten);
            return(starsEaten + 1);
        }
        public Program ChromosomeToProgram(ProgramChromosome chromosome)
        {
            ProgramSlot[][] programCode = new ProgramSlot[this.funcSlotCount.Count][];
            int index = 0;
            for (int i = 0; i < this.funcSlotCount.Count; ++i)
            {
                programCode[i] = new ProgramSlot[this.funcSlotCount[i]];
                for (int j = 0; j < this.funcSlotCount[i]; ++j)
                    programCode[i][j] = chromosome.GetSlot(index++);
            }

            return new Program(programCode);
        }
예제 #4
0
        public override void Crossover(IChromosome pair)
        {
            ProgramChromosome pairCasted = (ProgramChromosome)pair;

            int randomPos1 = random.Next(this.slotCount);
            int randomPos2 = random.Next(this.slotCount);

            for (int i = Math.Min(randomPos1, randomPos2); i <= Math.Max(randomPos1, randomPos2); ++i)
            {
                ProgramSlot temp = pairCasted.program[i];
                pairCasted.program[i] = this.program[i];
                this.program[i]       = temp;
            }
        }
예제 #5
0
        public Program ChromosomeToProgram(ProgramChromosome chromosome)
        {
            ProgramSlot[][] programCode = new ProgramSlot[this.funcSlotCount.Count][];
            int             index       = 0;

            for (int i = 0; i < this.funcSlotCount.Count; ++i)
            {
                programCode[i] = new ProgramSlot[this.funcSlotCount[i]];
                for (int j = 0; j < this.funcSlotCount[i]; ++j)
                {
                    programCode[i][j] = chromosome.GetSlot(index++);
                }
            }

            return(new Program(programCode));
        }