Ejemplo n.º 1
0
        public override void evaluate(EvolutionState state,
			                      Individual ind,
			                      int subpopulation,
			                      int threadnum)
        {
            try
            {
                this.state = state;
                this.ind = ((GPIndividual)ind);
                this.subpopulation = subpopulation;
                this.threadnum = threadnum;

                model.problem = this;
                // Signal model to start simulation
                model._signal.Set();
                // Model plays out scene with individual and sets fitness
                _signal.WaitOne();

                Debug.Log("Fitness " + model.fitness + " Result " + model.result
                    + " = " + this.ind.trees [0].child.makeCTree(true, true, true));

                KozaFitness f = ((KozaFitness)ind.fitness);
                f.setStandardizedFitness(state, model.fitness);
                f.hits = 0;
                ind.evaluated = true;
            } catch (Exception e)
            {
                Debug.LogError(e.Message);
                throw new Exception("Error while evaluating: ", e);
            }
        }
Ejemplo n.º 2
0
        public override void eval(ec.EvolutionState state,
		                  int thread,
		                  GPData input,
		                  ADFStack stack,
		                  GPIndividual individual,
		                  Problem problem)
        {
            UnityProblem p = (UnityProblem)problem;
                        GameObject ball = p.model.ball.gameObject;
                        GameObject car = p.model.car.gameObject;
                        ((RegressionData)input).x = Vector3.Distance(ball.transform.position, car.transform.position);
        }
Ejemplo n.º 3
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var rd = ((DoubleData)(input));

            Children[0].Eval(state, thread, input, stack, individual, problem);
            var result = rd.x;

            Children[1].Eval(state, thread, input, stack, individual, problem);
            rd.x = result * rd.x;
        }
Ejemplo n.º 4
0
 public void EvalPrint(
     IEvolutionState state,
     int thread,
     GPData input,
     ADFStack stack,
     GPIndividual individual,
     IProblem problem,
     int[][] map2)
 {
     // Evaluate children.  Easy as cake.
     ((IEvalPrint)Children[0]).EvalPrint(state, thread, input, stack, individual, problem, map2);
     ((IEvalPrint)Children[1]).EvalPrint(state, thread, input, stack, individual, problem, map2);
     ((IEvalPrint)Children[2]).EvalPrint(state, thread, input, stack, individual, problem, map2);
     ((IEvalPrint)Children[3]).EvalPrint(state, thread, input, stack, individual, problem, map2);
 }
Ejemplo n.º 5
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            // shortcutting OR
            Children[0].Eval(state, thread, input, stack, individual, problem);

            if (((ParityData)input).x == 0)  // return the second item
            {
                Children[1].Eval(state, thread, input, stack, individual, problem);
            }
            // else return the first item (already there)
        }
Ejemplo n.º 6
0
        /**
         * This is an ugly hack to simulate the "Sensible Initialization",
         * First we create a GPIndividual, then reverse-map it to GEIndividuals,
         * We do not need to call IntegerVectorSpecies.newIndividual() since it is overriden
         * by the GPSpecies.newIndividual();
         *
         * Moreover, as in the case for non-identical representations (i,e, GP-GE island
         * models etc,), the grammar rules, tree constraints, ERC's etc, are supposed to be
         * identical across all islands, so we are using the same "gpspecies" inside this class.
         *
         * However, the identicality of the GPTree particulars like grammar, constraints, ADFs,
         * ERC's may not be universally true.
         */
        public override Individual NewIndividual(IEvolutionState state, int thread)
        {
            GEIndividual gei = null;

            if (InitScheme != null && InitScheme.Equals("sensible"))
            {
                GPIndividual gpi = (GPIndividual)GPSpecies.NewIndividual(state, thread);
                gei = ReverseMap(state, gpi, thread);
            }
            else
            {
                gei         = (GEIndividual)base.NewIndividual(state, thread);
                gei.Species = this;
            }
            return(gei);
        }
Ejemplo n.º 7
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var edge = ((EdgeData)(input)).edge;
            var prob = (Edge)problem;

            prob.Accept[prob.To[edge]] = true;

            // pass the edge down

            Children[0].Eval(state, thread, input, stack, individual, problem);
        }
Ejemplo n.º 8
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var rd = ((RegressionData)(input));

            Children[0].Eval(state, thread, input, stack, individual, problem);
            double result = rd.x;

            // can't shortcut because of NaN or +-Infinity

            Children[1].Eval(state, thread, input, stack, individual, problem);
            rd.x = result * rd.x;
        }
Ejemplo n.º 9
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var rd = ((TwoBoxData)(input));

            Children[0].Eval(state, thread, input, stack, individual, problem);
            if (rd.x != 0) // safe to short-circuit
            {
                double result = rd.x;
                Children[1].Eval(state, thread, input, stack, individual, problem);
                rd.x = result * rd.x;
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Default version assumes that every individual is a GEIndividual.
        /// The underlying problem.evaluate() must be prepared for the possibility that some
        /// GPIndividuals handed it are in fact null, meaning that they couldn't be extracted
        /// from the GEIndividual string.  You should assign them bad fitness in some appropriate way.
        /// </summary>
        /// <param name="state">The current EvolutionState.</param>
        /// <param name="ind">The individuals to evaluate together</param>
        /// <param name="updateFitness">Should this individuals' fitness be updated?</param>
        /// <param name="countVictoriesOnly">Don't bother updating Fitness with socres, just victories</param>
        /// <param name="subpops">Subpopulations</param>
        /// <param name="threadnum">Thread number</param>
        public void Evaluate(IEvolutionState state,
                             Individual[] ind,
                             bool[] updateFitness,
                             bool countVictoriesOnly,
                             int[] subpops,
                             int threadnum)
        {
            // the default version assumes that every subpopulation is a GE Individual
            var gpi = new GPIndividual[ind.Length];

            for (var i = 0; i < gpi.Length; i++)
            {
                if (ind[i] is GEIndividual)
                {
                    var indiv   = (GEIndividual)ind[i];
                    var species = (GESpecies)ind[i].Species;

                    // warning: gpi[i] may be null
                    gpi[i] = species.Map(state, indiv, threadnum, null);
                }
                else if (ind[i] is GPIndividual)
                {
                    state.Output.WarnOnce("GPIndividual provided to GEProblem.  Hope that's correct.");
                    gpi[i] = (GPIndividual)ind[i];
                }
                else
                {
                    state.Output.Fatal("Individual " + i +
                                       " passed to Grouped evaluate(...) was neither a GP nor GE Individual: " +
                                       ind[i]);
                }
            }

            ((IGroupedProblem)Problem).Evaluate(state, gpi, updateFitness, countVictoriesOnly, subpops, threadnum);

            for (var i = 0; i < gpi.Length; i++)
            {
                // Now we need to move the evaluated flag from the GPIndividual
                // to the GEIndividual, and also for good measure, let's copy over
                // the GPIndividual's fitness because even though the mapping function
                // set the two Individuals to share the same fitness, it's possible
                // that the evaluation function may have replaced the fitness.
                ind[i].Fitness   = gpi[i].Fitness;   // if it's a GPIndividual anyway it'll just copy onto itself
                ind[i].Evaluated = gpi[i].Evaluated; // if it's a GPIndividual anyway it'll just copy onto itself
            }
        }
Ejemplo n.º 11
0
        public virtual GPNode PickNode(IEvolutionState s, int subpop, int thread, GPIndividual ind, GPTree tree)
        {
            var rnd = s.Random[thread].NextDouble();

            if (rnd > NonterminalProbability + TerminalProbability + RootProbability)
            // pick anyone
            {
                if (Nodes == -1)
                {
                    Nodes = tree.Child.NumNodes(GPNode.NODESEARCH_ALL);
                }
                {
                    return(tree.Child.NodeInPosition(s.Random[thread].NextInt(Nodes), GPNode.NODESEARCH_ALL));
                }
            }
            if (rnd > NonterminalProbability + TerminalProbability)
            // pick the root
            {
                return(tree.Child);
            }
            if (rnd > NonterminalProbability)
            // pick terminals
            {
                if (Terminals == -1)
                {
                    Terminals = tree.Child.NumNodes(GPNode.NODESEARCH_TERMINALS);
                }

                return(tree.Child.NodeInPosition(s.Random[thread].NextInt(Terminals), GPNode.NODESEARCH_TERMINALS));
            }
            // pick nonterminals if you can

            if (Nonterminals == -1)
            {
                Nonterminals = tree.Child.NumNodes(GPNode.NODESEARCH_NONTERMINALS);
            }
            if (Nonterminals > 0)
            // there are some nonterminals
            {
                return(tree.Child.NodeInPosition(s.Random[thread].NextInt(Nonterminals), GPNode.NODESEARCH_NONTERMINALS));
            }
            // there ARE no nonterminals!  It must be the root node

            return(tree.Child);
        }
Ejemplo n.º 12
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            Children[0].Eval(state, thread, input, stack, individual, problem);

            if (((MultiplexerData)input).x == 1)  // return the second item
            {
                Children[1].Eval(state, thread, input, stack, individual, problem);
            }
            else // return the third item.
            {
                Children[2].Eval(state, thread, input, stack, individual, problem);
            }
        }
Ejemplo n.º 13
0
        public override void Eval(IEvolutionState state,
            int thread,
            GPData input,
            ADFStack stack,
            GPIndividual individual,
            IProblem problem)
        {
            var md = (MultiplexerData)input;

            if (md.Status == MultiplexerData.STATUS_3)
                md.Dat3 = Fast.M_3[bitpos + MultiplexerData.STATUS_3];
            else if (md.Status == MultiplexerData.STATUS_6)
                md.Dat6 = Fast.M_6[bitpos + MultiplexerData.STATUS_6];
            else // md.status == MultiplexerData.STATUS_11
                Array.Copy(Fast.M_11[bitpos + MultiplexerData.STATUS_11], 0,
                    md.Dat11, 0,
                    MultiplexerData.MULTI_11_NUM_BITSTRINGS);
        }
Ejemplo n.º 14
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            Children[0].Eval(state, thread, input, stack, individual, problem);

            var  md = (MajorityData)input;
            long y0 = md.Data0;
            long y1 = md.Data1;

            Children[1].Eval(state, thread, input, stack, individual, problem);

            md.Data0 = md.Data0 & y0;
            md.Data1 = md.Data1 & y1;
        }
Ejemplo n.º 15
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var rd = ((RegressionData)(input));

            double[] c = ((Benchmarks)problem).currentValue;
            if (c.Length >= 4)
            {
                rd.x = ((Benchmarks)problem).currentValue[3];
            }
            else
            {
                rd.x = 0;
            }
        }
Ejemplo n.º 16
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            int resultx;
            int resulty;

            var rd = ((LawnmowerData)(input));

            Children[0].Eval(state, thread, input, stack, individual, problem);
            resultx = rd.x;
            resulty = rd.y;

            Children[1].Eval(state, thread, input, stack, individual, problem);
            rd.x = (resultx + rd.x) % MODULO;
            rd.y = (resulty + rd.y) % MODULO;
        }
Ejemplo n.º 17
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var d = ((ParityData)input);

            // shortcutting NAND
            Children[0].Eval(state, thread, input, stack, individual, problem);

            if (d.x == 1)  // return the second item
            {
                Children[1].Eval(state, thread, input, stack, individual, problem);
            }

            // invert
            d.x ^= 1;
        }
Ejemplo n.º 18
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var edge = ((EdgeData)(input)).edge;
            var prob = (Edge)problem;

            if (prob.From.Length == prob.NumEdges)  // we're full, need to expand
            {
                var from_    = new int[prob.NumEdges * 2];
                var to_      = new int[prob.NumEdges * 2];
                var reading_ = new int[prob.NumEdges * 2];
                Array.Copy(prob.From, 0, from_, 0, prob.From.Length);
                Array.Copy(prob.To, 0, to_, 0, prob.To.Length);
                Array.Copy(prob.Reading, 0, reading_, 0, prob.Reading.Length);
                prob.From    = from_;
                prob.To      = to_;
                prob.Reading = reading_;
            }

            var newedge = prob.NumEdges;

            prob.NumEdges++;
            prob.From[newedge]    = prob.To[edge];
            prob.To[newedge]      = prob.To[edge]; // same
            prob.Reading[newedge] = prob.Reading[edge];

            // pass the original edge down the left child

            Children[0].Eval(state, thread, input, stack, individual, problem);

            // reset input for right child
            ((EdgeData)(input)).edge = newedge;

            // pass the new edge down the right child

            Children[1].Eval(state, thread, input, stack, individual, problem);
        }
Ejemplo n.º 19
0
/**
 * Reverse of the original map() function, takes a GPIndividual and returns
 * a corresponding GEIndividual; The GPIndividual may contain more than one trees,
 * and such cases are handled accordingly, see the 3rd bullet below --
 *
 * NOTE:
 * This reverse mapping is only valid for S-expression trees ;
 *
 * This procedure supports ERC for the current population (not for population
 * /subpopulation from other islands); However, that could be done by merging
 * all ERCBanks from all the sub-populations but that is not done yet ;
 *
 * Support for the ADF's are done as follows -- suppose in one GPIndividual,
 * there are N trees -- T1, T2, ,,, Tn and each of them follows n different
 * grammars G1, G2, ,,, Gn respectively; now if they are reverse-mapped to
 * int arrays, there will be n int arrays A1[], A2[], ,,, An[]; and suppose
 * the i-th tree Ti is reverse mapped to int array Ai[] and morevoer Ai[] is
 * the longest among all the arrays (Bj[]s); so Bi[] is sufficient to build
 * all ADF trees Tjs.
 */
        public GEIndividual ReverseMap(IEvolutionState state, GPIndividual ind, int threadnum)
        {
            // create a dummy individual
            GEIndividual newind = (GEIndividual)I_Prototype.Clone();

            // The longest int will be able to contain all ADF trees.
            int longestIntLength = -1;

            int[] longestInt = null;
            // Now go through all the ADF trees.
            for (int treeIndex = 0; treeIndex < ind.Trees.Length; treeIndex++)
            {
                // Flatten the Lisp tree
                ArrayList flatSexp = (ArrayList)FlattenSexp(state, threadnum,
                                                            ind.Trees[treeIndex]);
                // Now convert the flatten list into an array of ints
                // no. of trees == no. of grammars
                int[] genomeVals = ParseSexp(flatSexp, GrammarParser[treeIndex]);
                // store the longest int array
                if (genomeVals.Length >= longestIntLength)
                {
                    longestIntLength = genomeVals.Length;
                    longestInt       = new int[genomeVals.Length];
                    Array.Copy(genomeVals, 0, longestInt, 0, genomeVals.Length);
                }
                genomeVals = null;
            }
            // assign the longest int to the individual's genome
            newind.genome = longestInt;

            // update the GPIndividual's fitness information
            newind.Fitness   = ind.Fitness;
            newind.Evaluated = false;

            // Set the species to me ? not sure.
            newind.Species = this;

            // return it
            return(newind);
        }
Ejemplo n.º 20
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var p = (Lawnmower)problem;
            var d = (LawnmowerData)input;

            switch (p.Orientation)
            {
            case Lawnmower.O_UP:
                p.Orientation = Lawnmower.O_LEFT;
                break;

            case Lawnmower.O_LEFT:
                p.Orientation = Lawnmower.O_DOWN;
                break;

            case Lawnmower.O_DOWN:
                p.Orientation = Lawnmower.O_RIGHT;
                break;

            case Lawnmower.O_RIGHT:
                p.Orientation = Lawnmower.O_UP;
                break;

            default:      // whoa!
                state.Output.Fatal("Whoa, somehow I got a bad orientation! (" + p.Orientation + ")");
                break;
            }
            //p.moves++;

            // return [0,0]
            d.x = 0;
            d.y = 0;
        }
Ejemplo n.º 21
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var rd = ((TwoBoxData)(input));

            // evaluate children[1] first to determine if the demoniator is 0
            Children[1].Eval(state, thread, input, stack, individual, problem);
            if (rd.x == 0.0)
            {
                // the answer is 1.0 since the denominator was 0.0
                rd.x = 1.0;
            }
            else
            {
                double result = rd.x;

                Children[0].Eval(state, thread, input, stack, individual, problem);
                rd.x = rd.x / result;
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Just like eval, but it retraces the map and prints out info.
        /// </summary>
        public void EvalPrint(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem, int[][] map2)
        {
            var p = (Ant)problem;

            switch (p.Orientation)
            {
            case Ant.O_UP:
                p.PosY--;
                if (p.PosY < 0)
                {
                    p.PosY = p.MaxY - 1;
                }
                break;

            case Ant.O_LEFT:
                p.PosX--;
                if (p.PosX < 0)
                {
                    p.PosX = p.MaxX - 1;
                }
                break;

            case Ant.O_DOWN:
                p.PosY++;
                if (p.PosY >= p.MaxY)
                {
                    p.PosY = 0;
                }
                break;

            case Ant.O_RIGHT:
                p.PosX++;
                if (p.PosX >= p.MaxX)
                {
                    p.PosX = 0;
                }
                break;

            default:      // whoa!
                state.Output.Fatal("Whoa, somehow I got a bad orientation! (" + p.Orientation + ")");
                break;
            }

            p.Moves++;
            if (p.Map[p.PosX][p.PosY] == Ant.FOOD && p.Moves < p.MaxMoves)
            {
                p.Sum++;
                p.Map[p.PosX][p.PosY] = Ant.ATE;
            }

            if (p.Moves < p.MaxMoves)
            {
                if (++p.PMod > 122 /* ascii z */)
                {
                    p.PMod = 97;                               /* ascii a */
                }
                map2[p.PosX][p.PosY] = p.PMod;
            }
        }
Ejemplo n.º 23
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            var p = (FeatureExtractionProblem2)problem;

            Children[0].Eval(state, thread, input, stack, individual, problem);
            p.imageTransformer.TransformImage(p.currentImage[thread], Image.ImageTransformer.TransformationType.GAUSSIAN3);
            p.imageTransformer.TransformImage(p.currentImage[thread], Image.ImageTransformer.TransformationType.GAUSSIAN5);
        }
Ejemplo n.º 24
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            ClassificationData data = (ClassificationData)input;

            Children[0].Eval(state, thread, input, stack, individual, problem);
            if (data.boolVal)
            {
                Children[1].Eval(state, thread, input, stack, individual, problem);
            }
            else
            {
                Children[2].Eval(state, thread, input, stack, individual, problem);
            }
        }
Ejemplo n.º 25
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            ClassificationData result = (ClassificationData)input;

            result.boolVal = false;
        }
Ejemplo n.º 26
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            var p = (FeatureExtractionProblem)problem;

            Children[0].Eval(state, thread, input, stack, individual, problem);
            p.currentImage[thread] = p.currentImage[thread].Not();
        }
Ejemplo n.º 27
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            var p  = (FeatureExtractionProblem2)problem;
            var c0 = p.currentImage[thread].Copy();

            Children[0].Eval(state, thread, input, stack, individual, problem);
            var c1 = p.currentImage[thread].Copy();

            c0.CopyTo(p.currentImage[thread]);
            Children[1].Eval(state, thread, input, stack, individual, problem);
            p.currentImage[thread] = p.currentImage[thread].Mul(c1);
        }
Ejemplo n.º 28
0
        public override int Produce(
            int min,
            int max,
            int subpop,
            IList <Individual> inds,
            IEvolutionState state,
            int thread,
            IDictionary <string, object> misc)
        {
            int start = inds.Count;

            // how many individuals should we make?
            int n = TypicalIndsProduced;

            if (n < min)
            {
                n = min;
            }
            if (n > max)
            {
                n = max;
            }

            // should we bother?
            if (!state.Random[thread].NextBoolean(Likelihood))
            {
                // just load from source 0 and clone 'em
                Sources[0].Produce(n, n, subpop, inds, state, thread, misc);
                return(n);
            }

            IntBag[] parentparents   = null;
            IntBag[] preserveParents = null;
            if (misc != null && misc.ContainsKey(KEY_PARENTS))
            {
                preserveParents   = (IntBag[])misc[KEY_PARENTS];
                parentparents     = new IntBag[2];
                misc[KEY_PARENTS] = parentparents;
            }

            GPInitializer initializer = (GPInitializer)state.Initializer;

            for (int q = start; q < n + start; /* no increment */) // keep on going until we're filled up
            {
                Parents.Clear();

                // grab two individuals from our sources
                if (Sources[0] == Sources[1]) // grab from the same source
                {
                    Sources[0].Produce(2, 2, subpop, Parents, state, thread, misc);
                }
                else // grab from different sources
                {
                    Sources[0].Produce(1, 1, subpop, Parents, state, thread, misc);
                    Sources[1].Produce(1, 1, subpop, Parents, state, thread, misc);
                }


                // at this point, Parents[] contains our two selected individuals

                // are our tree values valid?
                if (Tree1 != TREE_UNFIXED && (Tree1 < 0 || Tree1 >= ((GPIndividual)Parents[0]).Trees.Length))
                {
                    // uh oh
                    state.Output.Fatal(
                        "GP Crossover Pipeline attempted to fix tree.0 to a value which was out of bounds of the array of the individual's trees.  Check the pipeline's fixed tree values -- they may be negative or greater than the number of trees in an individual");
                }
                if (Tree2 != TREE_UNFIXED && (Tree2 < 0 || Tree2 >= ((GPIndividual)Parents[1]).Trees.Length))
                {
                    // uh oh
                    state.Output.Fatal(
                        "GP Crossover Pipeline attempted to fix tree.1 to a value which was out of bounds of the array of the individual's trees.  Check the pipeline's fixed tree values -- they may be negative or greater than the number of trees in an individual");
                }

                int t1;
                int t2;
                if (Tree1 == TREE_UNFIXED || Tree2 == TREE_UNFIXED)
                {
                    do
                    // pick random trees -- their GPTreeConstraints must be the same
                    {
                        if (Tree1 == TREE_UNFIXED)
                        {
                            if (((GPIndividual)Parents[0]).Trees.Length > 1)
                            {
                                t1 = state.Random[thread].NextInt(((GPIndividual)Parents[0]).Trees.Length);
                            }
                            else
                            {
                                t1 = 0;
                            }
                        }
                        else
                        {
                            t1 = Tree1;
                        }

                        if (Tree2 == TREE_UNFIXED)
                        {
                            if (((GPIndividual)Parents[1]).Trees.Length > 1)
                            {
                                t2 = state.Random[thread].NextInt(((GPIndividual)Parents[1]).Trees.Length);
                            }
                            else
                            {
                                t2 = 0;
                            }
                        }
                        else
                        {
                            t2 = Tree2;
                        }
                    } while (((GPIndividual)Parents[0]).Trees[t1].Constraints(initializer) !=
                             ((GPIndividual)Parents[1]).Trees[t2].Constraints(initializer));
                }
                else
                {
                    t1 = Tree1;
                    t2 = Tree2;
                    // make sure the constraints are okay
                    if (((GPIndividual)Parents[0]).Trees[t1].Constraints(initializer) !=
                        ((GPIndividual)Parents[1]).Trees[t2].Constraints(initializer)) // uh oh
                    {
                        state.Output.Fatal(
                            "GP Crossover Pipeline's two tree choices are both specified by the user -- but their GPTreeConstraints are not the same");
                    }
                }

                bool res1 = false;
                bool res2 = false;

                // BRS: This is kind of stupid to name it this way!
                GPTree currTree = ((GPIndividual)Parents[1]).Trees[t2];

                // pick some nodes
                GPNode p1 = null;
                GPNode p2 = null;

                // lets walk on parent2 all nodes to get subtrees for each node, doing it once for O(N) and not O(N^2)
                // because depth etc are computed and not stored
                ArrayList nodeToSubtrees = new ArrayList();
                // also Hashtable for size to List() of nodes in that size for O(1) lookup
                Hashtable sizeToNodes = new Hashtable();
                TraverseTreeForDepth(currTree.Child, nodeToSubtrees, sizeToNodes);
                // sort the ArrayList with comparator that sorts by subtrees
                nodeToSubtrees.Sort(new NodeComparator());

                for (int x = 0; x < NumTries; x++)
                {
                    // pick a node in individual 1
                    p1 = NodeSelect1.PickNode(state, subpop, thread, (GPIndividual)Parents[0], ((GPIndividual)Parents[0]).Trees[t1]);
                    // now lets find "similar" in parent 2
                    p2 = FindFairSizeNode(nodeToSubtrees, sizeToNodes, p1, currTree, state, thread);


                    // check for depth and swap-compatibility limits
                    res1 = VerifyPoints(initializer, p2, p1); // p2 can fill p1's spot -- order is important!
                    if (n - (q - start) < 2 || TossSecondParent)
                    {
                        res2 = true;
                    }
                    else
                    {
                        res2 = VerifyPoints(initializer, p1, p2); // p1 can fill p2's spot --  order is important!
                    }
                    // did we get something that had both nodes verified?
                    // we reject if EITHER of them is invalid. This is what lil-gp
                    // does.
                    // Koza only has numTries set to 1, so it's compatible as well.
                    if (res1 && res2)
                    {
                        break;
                    }
                }

                // at this point, res1 AND res2 are valid, OR
                // either res1 OR res2 is valid and we ran out of tries, OR
                // neither res1 nor res2 is valid and we rand out of tries.
                // So now we will transfer to a tree which has res1 or res2
                // valid, otherwise it'll just get replicated. This is
                // compatible with both Koza and lil-gp.

                // at this point I could check to see if my sources were breeding
                // pipelines -- but I'm too lazy to write that code (it's a little
                // complicated) to just swap one individual over or both over,
                // -- it might still entail some copying. Perhaps in the future.
                // It would make things faster perhaps, not requiring all that
                // cloning.

                // Create some new individuals based on the old ones -- since
                // GPTree doesn't deep-clone, this should be just fine. Perhaps we
                // should change this to proto off of the main species prototype,
                // but
                // we have to then copy so much stuff over; it's not worth it.

                GPIndividual j1 = ((GPIndividual)Parents[0]).LightClone();
                GPIndividual j2 = null;
                if (n - (q - start) >= 2 && !TossSecondParent)
                {
                    j2 = ((GPIndividual)Parents[1]).LightClone();
                }

                // Fill in various tree information that didn't get filled in there
                j1.Trees = new GPTree[((GPIndividual)Parents[0]).Trees.Length];
                if (n - (q - start) >= 2 && !TossSecondParent)
                {
                    j2.Trees = new GPTree[((GPIndividual)Parents[1]).Trees.Length];
                }

                // at this point, p1 or p2, or both, may be null.
                // If not, swap one in. Else just copy the parent.

                for (int x = 0; x < j1.Trees.Length; x++)
                {
                    if (x == t1 && res1) // we've got a tree with a kicking cross
                    // position!
                    {
                        j1.Trees[x]                   = ((GPIndividual)Parents[0]).Trees[x].LightClone();
                        j1.Trees[x].Owner             = j1;
                        j1.Trees[x].Child             = ((GPIndividual)Parents[0]).Trees[x].Child.CloneReplacing(p2, p1);
                        j1.Trees[x].Child.Parent      = j1.Trees[x];
                        j1.Trees[x].Child.ArgPosition = 0;
                        j1.Evaluated                  = false;
                    } // it's changed
                    else
                    {
                        j1.Trees[x]                   = ((GPIndividual)Parents[0]).Trees[x].LightClone();
                        j1.Trees[x].Owner             = j1;
                        j1.Trees[x].Child             = (GPNode)((GPIndividual)Parents[0]).Trees[x].Child.Clone();
                        j1.Trees[x].Child.Parent      = j1.Trees[x];
                        j1.Trees[x].Child.ArgPosition = 0;
                    }
                }

                if (n - (q - start) >= 2 && !TossSecondParent)
                {
                    for (int x = 0; x < j2.Trees.Length; x++)
                    {
                        if (x == t2 && res2) // we've got a tree with a kicking
                        // cross position!
                        {
                            j2.Trees[x]                   = ((GPIndividual)Parents[1]).Trees[x].LightClone();
                            j2.Trees[x].Owner             = j2;
                            j2.Trees[x].Child             = ((GPIndividual)Parents[1]).Trees[x].Child.CloneReplacing(p1, p2);
                            j2.Trees[x].Child.Parent      = j2.Trees[x];
                            j2.Trees[x].Child.ArgPosition = 0;
                            j2.Evaluated                  = false;
                        } // it's changed
                        else
                        {
                            j2.Trees[x]                   = ((GPIndividual)Parents[1]).Trees[x].LightClone();
                            j2.Trees[x].Owner             = j2;
                            j2.Trees[x].Child             = (GPNode)((GPIndividual)Parents[1]).Trees[x].Child.Clone();
                            j2.Trees[x].Child.Parent      = j2.Trees[x];
                            j2.Trees[x].Child.ArgPosition = 0;
                        }
                    }
                }

                // add the individuals to the population
                // by Ermo. I think this should be add
                // inds.set(q,j1);
                // Yes -- Sean
                inds.Add(j1);
                if (preserveParents != null)
                {
                    parentparents[0].AddAll(parentparents[1]);
                    preserveParents[q] = parentparents[0];
                }

                q++;
                if (q < n + start && !TossSecondParent)
                {
                    // by Ermo. Same reason, should changed to add
                    //inds[q] = j2;
                    inds.Add(j2);
                    if (preserveParents != null)
                    {
                        preserveParents[q] = parentparents[0];
                    }
                    q++;
                }
            }
            return(n);
        }
Ejemplo n.º 29
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            ClassificationData result = (ClassificationData)input;

            result.stringVal = "Iris-setosa";
        }
Ejemplo n.º 30
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            var p = (FeatureExtractionProblem2)problem;

            Children[0].Eval(state, thread, input, stack, individual, problem);
            Children[1].Eval(state, thread, input, stack, individual, problem);
            int r1 = ((ProblemData)input).range + 60;

            Children[2].Eval(state, thread, input, stack, individual, problem);
            int r2 = ((ProblemData)input).range + 160;

            for (int w = 0; w < 64; w++)
            {
                for (int h = 0; h < 64; h++)
                {
                    if (p.currentImage[thread][w, h].Intensity <r1 || p.currentImage[thread][w, h].Intensity> r2)
                    {
                        p.currentImage[thread][w, h] = new Gray(0);
                    }
                }
            }
        }
Ejemplo n.º 31
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            var p = (FeatureExtractionProblem2)problem;

            Children[0].Eval(state, thread, input, stack, individual, problem);
            p.currentImage[thread] = p.imageTransformer.RotateViaCenter(p.currentImage[thread], 20);
        }
Ejemplo n.º 32
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            var cProblem = (MultiClassifierProblem)problem;

            ((ClassificationData)input).doubleVal = cProblem.CurrentParam[paramIndex];
            ((ClassificationData)input).index     = (int)paramIndex;
        }