Beispiel #1
0
        public Chromosome deepCopy()
        {
            string     previousString = HumanReadableChromosome();
            Chromosome ret            = new Chromosome();

            ret.cells        = new List <Cell>();
            ret.classBits    = new BitArray(classBits);
            ret.affinityBits = new BitArray(affinityBits);
            foreach (Cell x in cells)
            {
                ret.AddCell(x.DeepCopy());
            }
            ret.updateCellNum();
            ret.notFlag = NotFlag;
            string newString = ret.HumanReadableChromosome();

            Debug.Assert(newString == previousString);
            return(ret);
        }
Beispiel #2
0
        public static Chromosome[] CrossOver(Chromosome a, Chromosome b)
        {
            string     aDebugStr = a.HumanReadableChromosome(), bDebugStr = b.HumanReadableChromosome();
            Chromosome target = a, notTarget = b;

            Chromosome[] ret = new Chromosome[2];
            ret[0] = new Chromosome();
            ret[1] = new Chromosome();
            a.updateCellNum(); b.updateCellNum();


            string aOutString = a.HumanReadableChromosome(), boutString = b.HumanReadableChromosome();

            Debug.Assert(aOutString == aDebugStr);
            Debug.Assert(boutString == bDebugStr);

            int least = Math.Min(a.NumCells, b.NumCells);

            CrossChromoSpecificBits(a, b, ref target, ref notTarget, ret);
            target       = ret[0];
            notTarget    = ret[1];
            ret[0].cells = new List <Cell>();
            ret[1].cells = new List <Cell>();
            //Chromosome crossover done, now for cells
            List <int> aCrossed = new List <int>(), bCrossed = new List <int>();

            int stop1, stop2;

            stop1 = OptoGlobals.RNG.Next(0, least);
            stop2 = OptoGlobals.RNG.Next(stop1, least);
            switch (OptoGlobals.CrossoverMode)
            {
            case OptoGlobals.CrossoverModes.Uniform:
                for (int i = 0; i < least; ++i)
                {
                    if (OptoGlobals.RNG.NextDouble() < OptoGlobals.CrossoverChance)
                    {
                        Util.switchTargets(ret[0], ret[1], ref target, ref notTarget);
                    }
                    //target.JoinCell(a[i]);
                    //notTarget.JoinCell(b[i]);
                    Cell[] offspring = Cell.Crossover(a[i], b[i]);
                    target.JoinCell(offspring[0]);
                    notTarget.JoinCell(offspring[1]);


                    aCrossed.Add(i);
                    bCrossed.Add(i);
                }
                break;

            //Uniform, SinglePointChromosome, TwoPointChromosome
            case OptoGlobals.CrossoverModes.SinglePointChromosome:
            case OptoGlobals.CrossoverModes.TwoPointChromosome:
                for (int i = 0; i < least; ++i)
                {
                    if (i == stop1 ||
                        (OptoGlobals.CrossoverMode == OptoGlobals.CrossoverModes.TwoPointChromosome && i == stop2))
                    {
                        Util.switchTargets(ret[0], ret[1], ref target, ref notTarget);
                    }
                    target.JoinCell(a[i]);
                    notTarget.JoinCell(b[i]);
                    aCrossed.Add(i);
                    bCrossed.Add(i);
                }
                break;

            default:
                break;
            }


            //Now, all the common cells have been crossed over.
            //update target to point to the chromosome with the most cells, which will be supplying the rest of the info
            List <int> mostCrossed;
            Chromosome mostCells;

            if (least == a.NumCells)
            {
                //if (aCrossed.Count == a.NumCells)

                mostCrossed = bCrossed;
                mostCells   = b;
            }
            else
            {
                mostCrossed = aCrossed;
                mostCells   = a;
            }
            while (mostCells.NumCells != mostCrossed.Count)//distribute remaining cells uniformly
            {
                int i = GetUnpickedInt(mostCells, mostCrossed);
                if (OptoGlobals.RNG.NextDouble() < OptoGlobals.CrossoverChance)
                {
                    Util.switchTargets(ret[0], ret[1], ref target, ref notTarget);
                }
                target.JoinCell(mostCells[i]);
                mostCrossed.Add(i);
            }
            ret[0].JoinAllCells();
            ret[0].ErrorCheck();
            ret[1].JoinAllCells();
            ret[1].ErrorCheck();

            aOutString = a.HumanReadableChromosome();
            boutString = b.HumanReadableChromosome();
            Debug.Assert(aOutString == aDebugStr);
            Debug.Assert(boutString == bDebugStr);
            return(ret);
        }