protected virtual int GetMutationDepth(SelectionGenome genome)
        {
            int nMut = (int) (Math.Abs(GA.Random.NextGaussian())
                              * (genome.OperationNodes.Count - 1) / 3 + 1);

            nMut = Math.Min(nMut, genome.OperationNodes.Count);

            return (int) Math.Log(nMut, 2) + 1;
        }
 protected virtual void CrossSelection(
     SelectionGenome genome1, SelectionGenome genome2,
     IList operationsToCross)
 {
     foreach (OperationNode op in operationsToCross) {
         CoveringInfo ci1 = (CoveringInfo) genome1.Selection[op];
         CoveringInfo ci2 = (CoveringInfo) genome2.Selection[op];
         genome1.Selection[op] = ci2;
         genome2.Selection[op] = ci1;
     }
 }
        protected virtual void ExtendOperationsToCross(
            SelectionGenome genome1, SelectionGenome genome2,
            IList operationsToCross)
        {
            IList operationsToAdd = new ArrayList();
            bool operationAdded = true;

            while (operationAdded) {

                operationAdded = false;

                foreach (OperationNode op in operationsToCross) {

                    CoveringInfo ci1 = (CoveringInfo) genome1.Selection[op];

                    foreach (OperationNode coveredOp in ci1.CoveredOperations) {

                        if (!operationsToCross.Contains(coveredOp) &&
                            !operationsToAdd.Contains(coveredOp)) {

                            operationsToAdd.Add(coveredOp);
                            operationAdded = true;
                        }
                    }

                    CoveringInfo ci2 = (CoveringInfo) genome2.Selection[op];

                    foreach (OperationNode coveredOp in ci2.CoveredOperations) {

                        if (!operationsToCross.Contains(coveredOp) &&
                            !operationsToAdd.Contains(coveredOp)) {

                            operationsToAdd.Add(coveredOp);
                            operationAdded = true;
                        }
                    }
                }
            }

            foreach (OperationNode op in operationsToAdd)
                operationsToCross.Add(op);
        }
        protected virtual void ExtendOperationsToMutate(
            SelectionGenome genome, IList operationsToMutate)
        {
            IList operationsToAdd = new ArrayList();

            foreach (OperationNode op in operationsToMutate) {

                CoveringInfo ci = (CoveringInfo) genome.Selection[op];

                foreach (OperationNode coveredOp in ci.CoveredOperations) {

                    if (!operationsToMutate.Contains(coveredOp) &&
                        !operationsToAdd.Contains(coveredOp)) {

                        operationsToAdd.Add(coveredOp);
                    }
                }
            }

            foreach (OperationNode op in operationsToAdd)
                operationsToMutate.Add(op);
        }
        protected virtual void MutateSelection(
            SelectionGenome genome, IList operationsToMutate)
        {
            while (operationsToMutate.Count > 0) {

                OperationNode mutOp =
                    (OperationNode) operationsToMutate[0];

                IList coveringDesc =
                    (IList) genome.CoveringDesc[mutOp.ResultValue];

                CoveringInfo coveringInfo = null;
                bool coveringFound = false;

                while (!coveringFound) {

                    coveringInfo =
                        (CoveringInfo) coveringDesc[
                            GA.Random.Next(coveringDesc.Count)];

                    bool containsAllOps = true;

                    foreach (OperationNode op in coveringInfo.CoveredOperations) {
                        if (!operationsToMutate.Contains(op)) {
                            containsAllOps = false;
                            break;
                        }
                    }

                    coveringFound = containsAllOps;
                }

                foreach (OperationNode op in coveringInfo.CoveredOperations) {
                    genome.Selection[op] = coveringInfo;
                    operationsToMutate.Remove(op);
                }
            }
        }
        protected virtual void SelectionMutation(SelectionGenome genome)
        {
            int depth = GetMutationDepth(genome);

            OperationNode rootOp =
                (OperationNode) genome.OperationNodes[
                    GA.Random.Next(genome.OperationNodes.Count)];

            CoveringInfo rootCi = (CoveringInfo) genome.Selection[rootOp];
            ValueNode rootVal = rootCi.ResultValue;

            IList operationsToMutate = new ArrayList();

            GetOperationsToMutate(genome.ProgramGraph, rootVal, depth,
                                  operationsToMutate);
            ExtendOperationsToMutate(genome, operationsToMutate);
            MutateSelection(genome, operationsToMutate);
        }
        protected virtual int GetCrossoverDepth(
            SelectionGenome genome1, SelectionGenome genome2)
        {
            int nCross = (int) (Math.Abs(GA.Random.NextGaussian())
                                * (genome1.OperationNodes.Count - 1) / 3 + 1);

            nCross = Math.Min(nCross, genome1.OperationNodes.Count);

            return (int) Math.Log(nCross, 2) + 1;
        }
        protected virtual void SelectionCrossover(
            SelectionGenome genome1, SelectionGenome genome2)
        {
            int depth = GetCrossoverDepth(genome1, genome2);
            ValueNode rootVal = GetRootValue(genome1, genome2);
            IList operationsToCross = new ArrayList();

            GetOperationsToCross(genome1.ProgramGraph, rootVal, depth,
                                 operationsToCross);
            ExtendOperationsToCross(genome1, genome2, operationsToCross);
            CrossSelection(genome1, genome2, operationsToCross);
        }
        protected virtual ValueNode GetRootValue(
            SelectionGenome genome1, SelectionGenome genome2)
        {
            ValueNode rootVal = null;
            OperationNode rootOp;
            CoveringInfo rootCi1;
            CoveringInfo rootCi2;

            bool foundRootOp = false;

            while (!foundRootOp) {

                rootOp = (OperationNode) genome1.OperationNodes[
                    GA.Random.Next(genome1.OperationNodes.Count)];

                rootCi1 = (CoveringInfo) genome1.Selection[rootOp];
                rootCi2 = (CoveringInfo) genome2.Selection[rootOp];

                if (rootCi1.ResultValue == rootCi2.ResultValue) {
                    rootVal = rootCi1.ResultValue;
                    foundRootOp = true;
                }
            }

            return rootVal;
        }
Exemple #10
0
 public virtual object Clone()
 {
     SelectionGenome clone = new SelectionGenome(population);
     clone.Copy(this);
     return clone;
 }