Ejemplo n.º 1
0
 public override void Setup(IEvolutionState state, IParameter paramBase)
 {
     base.Setup(state, paramBase);
     Velocity = new double[genome.Length];
 }
Ejemplo n.º 2
0
        public override void Eval(IEvolutionState state, int thread, GPData input, ADFStack stack, GPIndividual individual, IProblem problem)
        {
            ClassificationData result = (ClassificationData)input;

            result.boolVal = true;
        }
Ejemplo n.º 3
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.º 4
0
 public override void ReadNode(IEvolutionState state, BinaryReader dataInput) // throws IOException
 {
     value = dataInput.ReadDouble();
 }
Ejemplo n.º 5
0
        /**
         * Where the actual reproduce is happening, it will grab the candidate
         * parents, and calls the crossover or mutation method on these parents
         * individuals.
         */
        public bool Reproduce(IEvolutionState state, int thread, int subpop, IList <NEATSubspecies> sortedSubspecies)
        {
            if (ExpectedOffspring > 0 && Individuals.Count == 0)
            {
                state.Output.Fatal("Attempt to reproduce out of empty subspecies");
                return(false);
            }

            if (ExpectedOffspring > state.Population.Subpops[subpop].InitialSize)
            {
                state.Output.Fatal("Attempt to reproduce too many individuals");
                return(false);
            }

            NEATSpecies species = (NEATSpecies)state.Population.Subpops[subpop].Species;

            // bestIndividual of the 'this' specie is the first element of the
            // species
            // note, we already sort the individuals based on the fitness (not sure
            // if this is still correct to say)
            NEATIndividual bestIndividual = (NEATIndividual)First();



            // create the designated number of offspring for the Species one at a
            // time
            bool bestIndividualDone = false;

            for (int i = 0; i < ExpectedOffspring; ++i)
            {
                NEATIndividual newInd;

                if (bestIndividual.SuperChampionOffspring > 0)
                {
                    newInd = (NEATIndividual)bestIndividual.Clone();

                    // Most super champion offspring will have their connection
                    // weights mutated only
                    // The last offspring will be an exact duplicate of this super
                    // champion
                    // Note: Super champion offspring only occur with stolen babies!
                    // Settings used for published experiments did not use this

                    if (bestIndividual.SuperChampionOffspring > 1)
                    {
                        if (state.Random[thread].NextBoolean(0.8) || species.MutateAddLinkProb.Equals(0.0))
                        {
                            newInd.MutateLinkWeights(state, thread, species, species.WeightMutationPower, 1.0,
                                                     NEATSpecies.MutationType.GAUSSIAN);
                        }
                        else
                        {
                            // Sometime we add a link to a superchamp
                            newInd.CreateNetwork(); // make sure we have the network
                            newInd.MutateAddLink(state, thread);
                        }
                    }
                    if (bestIndividual.SuperChampionOffspring == 1)
                    {
                        if (bestIndividual.PopChampion)
                        {
                            newInd.PopChampionChild = true;
                            newInd.HighFit          = bestIndividual.Fitness.Value;
                        }
                    }

                    bestIndividual.SuperChampionOffspring--;
                }
                else if (!bestIndividualDone && ExpectedOffspring > 5)
                {
                    newInd             = (NEATIndividual)bestIndividual.Clone();
                    bestIndividualDone = true;
                }
                // Decide whether to mate or mutate
                // If there is only one individual, then always mutate
                else if (state.Random[thread].NextBoolean(species.MutateOnlyProb) || Individuals.Count == 1)
                {
                    // Choose the random parent
                    int        parentIndex = state.Random[thread].NextInt(Individuals.Count);
                    Individual parent      = Individuals[parentIndex];
                    newInd = (NEATIndividual)parent.Clone();


                    newInd.DefaultMutate((EvolutionState)state, thread);
                }
                else // Otherwise we should mate
                {
                    // random choose the first parent
                    int            parentIndex = state.Random[thread].NextInt(Individuals.Count);
                    NEATIndividual firstParent = (NEATIndividual)Individuals[parentIndex];
                    NEATIndividual secondParent;
                    // Mate within subspecies, choose random second parent
                    if (state.Random[thread].NextBoolean(1.0 - species.InterspeciesMateRate))
                    {
                        parentIndex  = state.Random[thread].NextInt(Individuals.Count);
                        secondParent = (NEATIndividual)Individuals[parentIndex];
                    }
                    else // Mate outside subspecies
                    {
                        // Select a random species
                        NEATSubspecies randomSubspecies = this;
                        // Give up if you cant find a different Species
                        int giveUp = 0;
                        while (randomSubspecies == this && giveUp < 5)
                        {
                            // Choose a random species tending towards better
                            // species
                            double value = state.Random[thread].NextGaussian() / 4;
                            if (value > 1.0)
                            {
                                value = 1.0;
                            }
                            // This tends to select better species

                            int upperBound = (int)Math.Floor(value * (sortedSubspecies.Count - 1.0) + 0.5);
                            int index      = 0;
                            while (index < upperBound)
                            {
                                index++;
                            }
                            randomSubspecies = sortedSubspecies[index];
                            giveUp++;
                        }

                        secondParent = (NEATIndividual)randomSubspecies.First();
                    }

                    newInd = firstParent.Crossover(state, thread, secondParent);


                    // Determine whether to mutate the baby's Genome
                    // This is done randomly or if the parents are the same
                    // individual
                    if (state.Random[thread].NextBoolean(1.0 - species.MateOnlyProb) || firstParent == secondParent ||
                        species.Compatibility(firstParent, secondParent).Equals(0.0))
                    {
                        newInd.DefaultMutate((EvolutionState)state, thread);
                    }
                }



                newInd.SetGeneration(state);
                newInd.CreateNetwork();

                // Add the new individual to its proper subspecies
                // this could create new subspecies
                species.Speciate(state, newInd);
            }



            return(true);
        }
Ejemplo n.º 6
0
 public override void PrintIndividualForHumans(IEvolutionState state, int log)
 {
     state.Output.PrintLn(EVALUATED_PREAMBLE + (Evaluated ? "true" : "false"), log);
     Fitness.PrintFitnessForHumans(state, log);
     PrintTrees(state, log);
 }
Ejemplo n.º 7
0
        public override void Eval(IEvolutionState state,
                                  int thread,
                                  GPData input,
                                  ADFStack stack,
                                  GPIndividual individual,
                                  IProblem problem)
        {
            var md = (MultiplexerData)input;

            long[] dat_11_1 = null;  // quiets compiler complaints
            long[] dat_11_2 = null;  // quiets compiler complaints
            long   dat_6_1  = 0L;
            long   dat_6_2  = 0L;
            byte   dat_3_1  = 0;
            byte   dat_3_2  = 0;

            // No shortcuts for now
            Children[0].Eval(state, thread, input, stack, individual, problem);

            if (md.Status == MultiplexerData.STATUS_3)
            {
                dat_3_1 = md.Dat3;
            }
            else if (md.Status == MultiplexerData.STATUS_6)
            {
                dat_6_1 = md.Dat6;
            }
            else // md.status == MultiplexerData.STATUS_11
            {
                dat_11_1 = md.PopDat11();
                Array.Copy(md.Dat11, 0,
                           dat_11_1, 0,
                           MultiplexerData.MULTI_11_NUM_BITSTRINGS);
            }

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

            if (md.Status == MultiplexerData.STATUS_3)
            {
                dat_3_2 = md.Dat3;
            }
            else if (md.Status == MultiplexerData.STATUS_6)
            {
                dat_6_2 = md.Dat6;
            }
            else // md.status == MultiplexerData.STATUS_11
            {
                dat_11_2 = md.PopDat11();
                Array.Copy(md.Dat11, 0,
                           dat_11_2, 0,
                           MultiplexerData.MULTI_11_NUM_BITSTRINGS);
            }

            // tweak -- if a then b else c is equivalent to
            // (a -> b) ^ (~a -> c) which is equivalent to
            // (~a v b) ^ (a v c).  In Java, ^ (-1) is the same
            // is bitwise not.

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

            // BRS: NOTE: Byte vs. SByte needs some checking
            if (md.Status == MultiplexerData.STATUS_3)
            {
                md.Dat3 = (byte)(
                    ((dat_3_1 ^ (sbyte)(-1)) | dat_3_2) &
                    ((dat_3_1 | md.Dat3)));
            }

            else if (md.Status == MultiplexerData.STATUS_6)
            {
                md.Dat6 =
                    ((dat_6_1 ^ (-1L)) | dat_6_2) &
                    ((dat_6_1 | md.Dat6));
            }

            else // md.status == MultiplexerData.STATUS_11
            {
                for (var x = 0; x < MultiplexerData.MULTI_11_NUM_BITSTRINGS; x++)
                {
                    md.Dat11[x] =
                        ((dat_11_1[x] ^ (-1L)) | dat_11_2[x]) &
                        ((dat_11_1[x] | md.Dat11[x]));
                }
                md.PushDat11(dat_11_2);
                md.PushDat11(dat_11_1);
            }
        }
Ejemplo n.º 8
0
        public static void Main(string[] args)
        {
            IEvolutionState    state      = null;
            IParameterDatabase parameters = null;
            Output             output     = null;

            //bool store;
            int x;

            // 0. find the parameter database
            for (x = 0; x < args.Length - 1; x++)
            {
                if (args[x].Equals(A_FILE))
                {
                    try
                    {
                        parameters = new ParameterDatabase(new FileInfo(new FileInfo(args[x + 1]).FullName), args);

                        // add the fact that I am a slave:      eval.i-am-slave = true
                        // this is used only by the Evaluator to determine whether to use the MasterProblem
                        parameters.SetParameter(new Parameter(EvolutionState.P_EVALUATOR).Push(Evaluator.P_IAMSLAVE),
                                                "true");
                        break;
                    }
                    catch (FileNotFoundException e)
                    {
                        Output.InitialError(
                            "A File Not Found Exception was generated upon" + " reading the parameter file \""
                            + args[x + 1] + "\".\nHere it is:\n" + e, false);
                        Environment.Exit(1);
                        // This was originally part of the InitialError call in ECJ. But we make Slave responsible.
                    }
                    catch (IOException e)
                    {
                        Output.InitialError("An IO Exception was generated upon reading the" + " parameter file \""
                                            + args[x + 1] + "\".\nHere it is:\n" + e, false);
                        Environment.Exit(1);
                        // This was originally part of the InitialError call in ECJ. But we make Slave responsible.
                    }
                }
            }
            if (parameters == null)
            {
                Output.InitialError("No parameter file was specified.", false);
                Environment.Exit(1);
                // This was originally part of the InitialError call in ECJ. But we make Slave responsible.
            }

            // 5. Determine whether or not to return entire Individuals or just Fitnesses
            //    (plus whether or not the Individual has been evaluated).

            var returnIndividuals = parameters.GetBoolean(new Parameter(P_RETURNINDIVIDUALS), null, false);

            // 5.5 should we silence the whole thing?

            bool silent = parameters.GetBoolean(new Parameter(P_SILENT), null, false);

            if (parameters.ParameterExists(new Parameter(P_MUZZLE), null))
            {
                Output.InitialWarning("" + new Parameter(P_MUZZLE) + " has been deprecated.  We suggest you use " +
                                      new Parameter(P_SILENT) + " or similar newer options.");
            }
            silent = silent || parameters.GetBoolean(new Parameter(P_MUZZLE), null, false);


            // 6. Open a server socket and listen for requests
            var slaveName = parameters.GetString(new Parameter(P_EVALSLAVENAME), null);

            var masterHost = parameters.GetString(new Parameter(P_EVALMASTERHOST), null);

            if (masterHost == null)
            {
                Output.InitialError("Master Host missing", new Parameter(P_EVALMASTERHOST));
            }

            var masterPort = parameters.GetInt(new Parameter(P_EVALMASTERPORT), null, 0);

            if (masterPort == -1)
            {
                Output.InitialError("Master Port missing", new Parameter(P_EVALMASTERPORT));
            }

            var useCompression = parameters.GetBoolean(new Parameter(P_EVALCOMPRESSION), null, false);

            RunTime = parameters.GetInt(new Parameter(P_RUNTIME), null, 0);

            RunEvolve = parameters.GetBoolean(new Parameter(P_RUNEVOLVE), null, false);

            OneShot = parameters.GetBoolean(new Parameter(P_ONESHOT), null, true);

            if (RunEvolve && !returnIndividuals)
            {
                Output.InitialError(
                    "You have the slave running in 'evolve' mode, but it's only returning fitnesses to the master, not whole individuals.  This is almost certainly wrong.",
                    new Parameter(P_RUNEVOLVE), new Parameter(P_RETURNINDIVIDUALS));
                Environment.Exit(1);
                // This was originally part of the InitialError call in ECJ. But we make Slave responsible.
            }

            if (!silent)
            {
                Output.InitialMessage("ECCS Slave");
                if (RunEvolve)
                {
                    Output.InitialMessage("Running in Evolve mode, evolve time is " + RunTime + " milliseconds");
                }
                if (returnIndividuals)
                {
                    Output.InitialMessage("Whole individuals will be returned");
                }
                else
                {
                    Output.InitialMessage("Only fitnesses will be returned");
                }
            }


            // Continue to serve new masters until killed.
            TcpClient socket = null; // BRS: TcpClient is a wrapper around the Socket class

            while (true)
            {
                try
                {
                    long connectAttemptCount = 0;
                    if (!silent)
                    {
                        Output.InitialMessage("Connecting to master at " + masterHost + ":" + masterPort);
                    }

                    while (true)
                    {
                        try
                        {
                            socket = new TcpClient(masterHost, masterPort);
                            break;
                        }
                        catch (Exception)
                        // it's not up yet...
                        {
                            connectAttemptCount++;
                            try
                            {
                                Thread.Sleep(new TimeSpan((Int64)10000 * SLEEP_TIME));
                            }
                            catch (ThreadInterruptedException)
                            {
                            }
                        }
                    }
                    if (!silent)
                    {
                        Output.InitialMessage("Connected to master after " + (connectAttemptCount * SLEEP_TIME) + " ms");
                    }

                    BinaryReader dataIn  = null;
                    BinaryWriter dataOut = null;

                    try
                    {
                        Stream tmpIn  = socket.GetStream();
                        Stream tmpOut = socket.GetStream();
                        if (useCompression)
                        {
                            //Output.InitialError("JDK 1.5 has broken compression.  For now, you must set eval.compression=false");
                            //Environment.Exit(1); // This was originally part of the InitialError call in ECJ. But we make Slave responsible.

                            /*
                             * tmpIn = new CompressingInputStream(tmpIn);
                             * tmpOut = new CompressingOutputStream(tmpOut);
                             */
                            tmpIn  = Output.MakeCompressingInputStream(tmpIn);
                            tmpOut = Output.MakeCompressingOutputStream(tmpOut);
                            if (tmpIn == null || tmpOut == null)
                            {
                                var err = "You do not appear to have JZLib installed on your system, and so must set eval.compression=false.  "
                                          + "To get JZLib, download from the ECJ website or from http://www.jcraft.com/jzlib/";
                                if (!silent)
                                {
                                    Output.InitialMessage(err);
                                }
                                throw new OutputExitException(err);
                            }
                        }

                        dataIn  = new BinaryReader(tmpIn);
                        dataOut = new BinaryWriter(tmpOut);
                    }
                    catch (IOException e)
                    {
                        var err = "Unable to open input stream from socket:\n" + e;
                        if (!silent)
                        {
                            Output.InitialMessage(err);
                        }
                        throw new OutputExitException(err);
                    }

                    // specify the slaveName
                    if (slaveName == null)
                    {
                        // BRS : TODO : Check equivalence of the address returned from .NET socket.Client.LocalEndPoint
                        slaveName = socket.Client.LocalEndPoint + "/" + (DateTime.Now.Ticks - 621355968000000000) / 10000;
                        if (!silent)
                        {
                            Output.InitialMessage("No slave name specified.  Using: " + slaveName);
                        }
                    }

                    dataOut.Write(slaveName); // Default encoding of BinaryWriter is UTF-8
                    dataOut.Flush();

                    // 1. create the output
                    // store = parameters.GetBoolean(new Parameter(P_STORE), null, false);

                    if (output != null)
                    {
                        output.Close();
                    }
                    output = new Output(storeAnnouncementsInMemory: false) // do not store messages, just print them
                    {
                        ThrowsErrors = true                                // don't do System.exit(1)
                    };


                    // stdout is always log #0. stderr is always log #1.
                    // stderr accepts announcements, and both are fully verbose by default.
                    output.AddLog(Log.D_STDOUT, false);
                    output.AddLog(Log.D_STDERR, true);

                    if (silent)
                    {
                        output.GetLog(0).Silent = true;
                        output.GetLog(1).Silent = true;
                    }

                    if (!silent)
                    {
                        output.SystemMessage(ECVersion.Message());
                    }


                    // 2. set up thread values

                    int breedthreads = Evolve.DetermineThreads(output, parameters, new Parameter(Evolve.P_BREEDTHREADS));
                    int evalthreads  = Evolve.DetermineThreads(output, parameters, new Parameter(Evolve.P_EVALTHREADS));

                    // Note that either breedthreads or evalthreads (or both) may be 'auto'.  We don't warn about this because
                    // the user isn't providing the thread seeds.


                    // 3. create the Mersenne Twister random number generators, one per thread

                    var random = new IMersenneTwister[breedthreads > evalthreads ? breedthreads : evalthreads];

                    var seed = dataIn.ReadInt32();
                    for (var i = 0; i < random.Length; i++)
                    {
                        random[i] = Evolve.PrimeGenerator(new MersenneTwisterFast(seed++));
                    }
                    // we prime the generator to be more sure of randomness.

                    // 4. Set up the evolution state

                    // what evolution state to use?
                    state =
                        (IEvolutionState)
                        parameters.GetInstanceForParameter(new Parameter(Evolve.P_STATE), null, typeof(IEvolutionState));
                    state.Parameters = new ParameterDatabase();
                    state.Parameters.AddParent(parameters);
                    state.Random       = random;
                    state.Output       = output;
                    state.EvalThreads  = evalthreads;
                    state.BreedThreads = breedthreads;

                    state.Setup(state, null);
                    state.Population = state.Initializer.SetupPopulation(state, 0);


                    // 5. Optionally do further loading
                    var storage = state.Evaluator.MasterProblem;
                    storage.ReceiveAdditionalData(state, dataIn);
                    storage.TransferAdditionalData(state);

                    try
                    {
                        while (true)
                        {
                            var newState = state;

                            if (RunEvolve)
                            {
                                // Construct and use a new EvolutionState.  This will be inefficient the first time around
                                // as we've set up TWO EvolutionStates in a row with no good reason.
                                IParameterDatabase coverDatabase = new ParameterDatabase();
                                // protect the underlying one
                                coverDatabase.AddParent(state.Parameters);
                                newState = Evolve.Initialize(coverDatabase, 0);
                                newState.StartFresh();
                                newState.Output.Message("Replacing random number generators, ignore above seed message");
                                newState.Random = state.Random;           // continue with RNG
                                storage.TransferAdditionalData(newState); // load the arbitrary data again
                            }

                            // 0 means to shut down
                            //Console.Error.WriteLine("reading next problem");
                            int problemType = dataIn.ReadByte();
                            //Console.Error.WriteLine("Read problem: " + problemType);
                            switch (problemType)
                            {
                            case (int)SlaveEvaluationType.Shutdown:
                                socket.Close();
                                if (OneShot)
                                {
                                    return; // we're outa here
                                }
                                else
                                {
                                    throw new OutputExitException("SHUTDOWN");
                                }

                            case (int)SlaveEvaluationType.Simple:
                                EvaluateSimpleProblem(newState, returnIndividuals, dataIn, dataOut, args);
                                break;

                            case (int)SlaveEvaluationType.Grouped:
                                EvaluateGroupedProblem(newState, returnIndividuals, dataIn, dataOut);
                                break;

                            default:
                                state.Output.Fatal("Unknown problem form specified: " + problemType);
                                break;
                            }
                            //System.err.PrintLn("Done Evaluating Individual");
                        }
                    }
                    catch (IOException e)
                    {
                        // Since an IOException can happen here if the peer closes the socket
                        // on it's end, we don't necessarily have to exit.  Maybe we don't
                        // even need to print a warning, but we'll do so just to indicate
                        // something happened.
                        state.Output.Fatal(
                            "Unable to read type of evaluation from master.  Maybe the master closed its socket and exited?:\n" +
                            e);
                    }
                    catch (Exception e)
                    {
                        if (state != null)
                        {
                            state.Output.Fatal(e.Message);
                        }
                        else if (!silent)
                        {
                            Console.Error.WriteLine("FATAL ERROR (EvolutionState not created yet): " + e.Message);
                        }
                    }
                }
                catch (OutputExitException e)
                {
                    // here we restart if necessary
                    try { socket.Close(); } catch (Exception e2) { }
                    if (OneShot)
                    {
                        Environment.Exit(0);
                    }
                }
                catch (OutOfMemoryException e)
                {
                    // Let's try fixing things
                    state = null;

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    try { socket.Close(); } catch (Exception e2) { }
                    socket = null;

                    // TODO: Overkill? Track memory before and after.
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    Console.Error.WriteLine(e);
                    if (OneShot)
                    {
                        Environment.Exit(0);
                    }
                }
                if (!silent)
                {
                    Output.InitialMessage("\n\nResetting Slave");
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Checks to make sure that the Problem implements ISimpleProblem.
        /// </summary>
        public override void Setup(IEvolutionState state, IParameter paramBase)
        {
            base.Setup(state, paramBase);
            if (!(p_problem is ISimpleProblem))
            {
                state.Output.Fatal(GetType().Name + " used, but the Problem is not of ISimpleProblem", paramBase.Push(P_PROBLEM));
            }

            CloneProblem = state.Parameters.GetBoolean(paramBase.Push(P_CLONE_PROBLEM), null, true);
            if (!CloneProblem && state.BreedThreads > 1) // uh oh, this can't be right
            {
                state.Output.Fatal("The Evaluator is not cloning its Problem, but you have more than one thread.", paramBase.Push(P_CLONE_PROBLEM));
            }

            numTests = state.Parameters.GetInt(paramBase.Push(P_NUM_TESTS), null, 1);
            if (numTests < 1)
            {
                numTests = 1;
            }
            else if (numTests > 1)
            {
                String m = state.Parameters.GetString(paramBase.Push(P_MERGE), null);
                if (m == null)
                {
                    state.Output.Warning("Merge method not provided to SimpleEvaluator.  Assuming 'mean'");
                }
                else if (m.Equals(V_MEAN))
                {
                    mergeForm = MERGE_MEAN;
                }
                else if (m.Equals(V_MEDIAN))
                {
                    mergeForm = MERGE_MEDIAN;
                }
                else if (m.Equals(V_BEST))
                {
                    mergeForm = MERGE_BEST;
                }
                else
                {
                    state.Output.Fatal("Bad merge method: " + m, paramBase.Push(P_NUM_TESTS), null);
                }
            }

            if (!state.Parameters.ParameterExists(paramBase.Push(P_CHUNK_SIZE), null))
            {
                chunkSize = C_AUTO;
            }
            else if (state.Parameters.GetString(paramBase.Push(P_CHUNK_SIZE), null).Equals(V_AUTO, StringComparison.InvariantCultureIgnoreCase))
            {
                chunkSize = C_AUTO;
            }
            else
            {
                chunkSize = (state.Parameters.GetInt(paramBase.Push(P_CHUNK_SIZE), null, 1));
                if (chunkSize == 0)  // uh oh
                {
                    state.Output.Fatal("Chunk Size must be either an integer >= 1 or 'auto'", paramBase.Push(P_CHUNK_SIZE), null);
                }
            }
        }
Ejemplo n.º 10
0
        private static void EvolveSimpleProblemCore(IEvolutionState state, bool returnIndividuals,
                                                    BinaryReader dataIn, BinaryWriter dataOut, int numInds, int[] subpops, int[] indsPerSubpop)
        {
            var updateFitness = new bool[numInds];
            var inds          = new Individual[numInds];

            try // load up ALL the individuals
            {
                for (var i = 0; i < numInds; i++)
                {
                    inds[i]          = state.Population.Subpops[subpops[i]].Species.NewIndividual(state, dataIn);
                    updateFitness[i] = dataIn.ReadBoolean();
                }
            }
            catch (IOException e)
            {
                state.Output.Fatal("Unable to read individual from master." + e);
            }

            var stopWatch = Stopwatch.StartNew();

            // Now we need to reset the subpopulations.  They were already set up with the right
            // classes, Species, etc. in state.setup(), so all we need to do is modify the number
            // of individuals in each subpopulation.

            for (var subpop = 0; subpop < state.Population.Subpops.Count; subpop++)
            {
                if (state.Population.Subpops[subpop].Individuals.Count != indsPerSubpop[subpop])
                {
                    state.Population.Subpops[subpop].Individuals = new Individual[indsPerSubpop[subpop]];
                }
            }

            // Disperse into the population
            var counts = new int[state.Population.Subpops.Count];

            for (var i = 0; i < numInds; i++)
            {
                state.Population.Subpops[subpops[i]].Individuals[counts[subpops[i]]++] = inds[i];
            }

            // Evaluate the population until time is up, or the evolution stops
            var result = EvolutionState.R_NOTDONE;

            while (result == EvolutionState.R_NOTDONE)
            {
                result = state.Evolve();
                if (stopWatch.ElapsedMilliseconds > RunTime)
                {
                    break;
                }
            }

            // re-gather from population in the same order
            counts = new int[state.Population.Subpops.Count];
            for (var i = 0; i < numInds; i++)
            {
                inds[i] = state.Population.Subpops[subpops[i]].Individuals[counts[subpops[i]]++];
            }

            state.Finish(result);
            Evolve.Cleanup(state);

            // Return the evaluated individual to the master
            try
            {
                ReturnIndividualsToMaster(state, inds, updateFitness, dataOut, returnIndividuals, -1);
                // -1 == write all individuals
                dataOut.Flush();
            }
            catch (IOException e)
            {
                state.Output.Fatal("Caught fatal IOException\n" + e);
            }
        }
Ejemplo n.º 11
0
        public static void EvaluateGroupedProblem(IEvolutionState state, bool returnIndividuals, BinaryReader dataIn, BinaryWriter dataOut)
        {
            var countVictoriesOnly = false;

            // first load the individuals
            var numInds = 1;

            try
            {
                countVictoriesOnly = dataIn.ReadBoolean();
                numInds            = dataIn.ReadInt32();
            }
            catch (IOException e)
            {
                state.Output.Fatal("Unable to read the number of individuals from the master:\n" + e);
            }

            // load the subpops
            var subpops       = new int[numInds];                        // subpops desired by each ind
            var indsPerSubpop = new int[state.Population.Subpops.Count]; // num inds for each subpop

            for (var i = 0; i < numInds; i++)
            {
                try
                {
                    subpops[i] = dataIn.ReadInt32();
                    if (subpops[i] < 0 || subpops[i] >= state.Population.Subpops.Count)
                    {
                        state.Output.Fatal("Bad subpop number for individual #" + i + ": " + subpops[i]);
                    }
                    indsPerSubpop[subpops[i]]++;
                }
                catch (IOException e)
                {
                    state.Output.Fatal("Unable to read the subpop number from the master:\n" + e);
                }
            }

            // Read the individuals from the stream
            var inds          = new Individual[numInds];
            var updateFitness = new bool[numInds];

            try
            {
                for (var i = 0; i < inds.Length; ++i)
                {
                    inds[i]          = state.Population.Subpops[subpops[i]].Species.NewIndividual(state, dataIn);
                    updateFitness[i] = dataIn.ReadBoolean();
                }
            }
            catch (IOException e)
            {
                state.Output.Fatal("Unable to read individual from master.");
            }

            // Evaluate the individuals together
            ((IGroupedProblem)state.Evaluator.p_problem).Evaluate(state, inds, updateFitness, countVictoriesOnly, subpops, 0);

            try
            {
                ReturnIndividualsToMaster(state, inds, updateFitness, dataOut, returnIndividuals, -1);
                // -1 == write all individuals
                dataOut.Flush();
            }
            catch (IOException e)
            {
                state.Output.Fatal("Caught fatal IOException\n" + e);
            }
        }
Ejemplo n.º 12
0
        private static void EvaluateSimpleProblemCore(IEvolutionState state, bool returnIndividuals,
                                                      BinaryReader dataIn, BinaryWriter dataOut, int numInds, int[] subpops)
        {
            // TODO: Refactor this to use TPL DataFlow!

            var problems      = new ISimpleProblem[numInds];
            var updateFitness = new bool[numInds];
            var inds          = new Individual[numInds];
            var indForThread  = new int[numInds];

            try
            {
                // BRS: TPL DataFlow BEGIN
                var maxDegree = Math.Min(Environment.ProcessorCount, state.EvalThreads);
                var options   = new ExecutionDataflowBlockOptions {
                    MaxDegreeOfParallelism = maxDegree
                };
                var block = new TransformBlock <SlaveEvalThread, Individual>(eval =>
                {
                    eval.Run();
                    return(eval.Ind);
                }, options);

                for (var i = 0; i < numInds; i++)
                {
                    var ind = state.Population.Subpops[subpops[i]].Species.NewIndividual(state, dataIn);
                    if (problems[i] == null)
                    {
                        problems[i] = (ISimpleProblem)state.Evaluator.p_problem.Clone();
                    }
                    updateFitness[i] = dataIn.ReadBoolean();

                    var runnable = new SlaveEvalThread
                    {
                        ThreadNum = i,
                        State     = state,
                        Problem   = problems[i],
                        Ind       = ind,
                        Subpop    = subpops[i]
                    };

                    block.Post(runnable);
                }
                for (var i = 0; i < numInds; i++)
                {
                    // This preserves the original block posting order so we can just use the index.
                    var ind = block.Receive();
                    // Return the evaluated Individual by index...
                    ReturnIndividualsToMaster(state, inds, updateFitness, dataOut, returnIndividuals, individualInQuestion: i);  // return just that individual
                }
                // BRS: TPL DataFlow END
            }
            catch (IOException e)
            {
                state.Output.Fatal("Unable to read individual from master." + e);
            }

            // gather everyone
            for (var i = 0; i < numInds; i++)
            {
                ReturnIndividualsToMaster(state, inds, updateFitness, dataOut, returnIndividuals,
                                          indForThread[i]); // return just that individual
            }

            try
            {
                dataOut.Flush();
            }
            catch (IOException e)
            {
                state.Output.Fatal("Caught fatal IOException\n" + e);
            }
        }
Ejemplo n.º 13
0
 public override void Setup(IEvolutionState state, IParameter paramBase)
 {
     // nothing to setup here
 }
Ejemplo n.º 14
0
 /**
  * Reads a Node printed by printNode(...). The default form simply reads a
  * line into a string, and then calls readNodeFromString() on that line.
  */
 public void ReadNode(IEvolutionState state, StreamReader reader)
 {
     // NNode::NNode (const char *argline, std::vector<Trait*> &traits)
     ReadNodeFromString(reader.ReadLine(), state);
 }
Ejemplo n.º 15
0
        public override void Setup(IEvolutionState state, IParameter paramBase)
        {
            // very important, remember this
            base.Setup(state, paramBase);

            // should we load our x parameters from a file, or generate them randomly?
            var training_file = state.Parameters.GetResource(paramBase.Push(P_TRAINING_FILE), null);
            var testing_file  = state.Parameters.GetResource(paramBase.Push(P_TESTING_FILE), null);
            var problem       = state.Parameters.GetString(paramBase.Push(P_PROBLEM_TYPE), null);
            var benchmark     = -1;

            if (problem == null)
            {
                state.Output.Message("Loading benchmark data from files");
                if ((testing_file == null || training_file == null))            // must provide both
                {
                    state.Output.Fatal("If you don't specify a problem type, you must provide a training file and a testing file",
                                       (training_file == null ? paramBase.Push(P_TRAINING_FILE) : paramBase.Push(P_TESTING_FILE)));
                }
                else  // load from files
                {
                    try
                    {
                        int numInputs = 0;

                        // first load the number of input variables
                        var scan = new Scanner(training_file);
                        if (scan.HasNextInt())
                        {
                            numInputs = scan.NextInt();
                        }
                        else
                        {
                            state.Output.Fatal("Number of input variables not provided at beginning of training file ", paramBase.Push(P_TRAINING_FILE), null);
                        }

                        // Load into an array list each element
                        var input  = new List <double[]>();
                        var output = new List <double>();
                        while (scan.HasNextDouble())
                        {
                            var in_  = new double[numInputs];
                            var out_ = 0.0;
                            for (var i = 0; i < numInputs; i++)
                            {
                                if (scan.HasNextDouble())
                                {
                                    in_[i] = scan.NextDouble();
                                }
                                else
                                {
                                    state.Output.Fatal("Non-normal number of data points in training file ", paramBase.Push(P_TRAINING_FILE), null);
                                }
                            }
                            if (scan.HasNextDouble())
                            {
                                out_ = scan.NextDouble();
                            }
                            else
                            {
                                state.Output.Fatal("Non-normal number of data points in training file ", paramBase.Push(P_TRAINING_FILE), null);
                            }
                            input.Add(in_);
                            output.Add(out_);
                        }

                        // dump to arrays
                        var len = input.Count;
                        trainingInputs = new double[len][];
                        for (var i = 0; i < len; i++)
                        {
                            trainingInputs[i] = new double[numInputs];
                        }
                        trainingOutputs = new double[len];
                        for (var i = 0; i < len; i++)
                        {
                            trainingInputs[i]  = input[i];
                            trainingOutputs[i] = output[i];
                        }


                        // same thing for testing


                        scan = new Scanner(testing_file);
                        if (scan.HasNextInt())
                        {
                            numInputs = scan.NextInt();
                        }
                        else
                        {
                            state.Output.Fatal("Number of input variables not provided at beginning of testing file ", paramBase.Push(P_TESTING_FILE), null);
                        }

                        // Load into an array list each element
                        input  = new List <double[]>();
                        output = new List <double>();
                        while (scan.HasNextDouble())
                        {
                            var in_  = new double[numInputs];
                            var out_ = 0.0;
                            for (var i = 0; i < numInputs; i++)
                            {
                                if (scan.HasNextDouble())
                                {
                                    in_[i] = scan.NextDouble();
                                }
                                else
                                {
                                    state.Output.Fatal("Non-normal number of data points in testing file ", paramBase.Push(P_TESTING_FILE), null);
                                }
                            }
                            if (scan.HasNextDouble())
                            {
                                out_ = scan.NextDouble();
                            }
                            else
                            {
                                state.Output.Fatal("Non-normal number of data points in testing file ", paramBase.Push(P_TESTING_FILE), null);
                            }
                            input.Add(in_);
                            output.Add(out_);
                        }

                        // dump to arrays
                        len           = input.Count;
                        testingInputs = new double[len][];
                        for (var i = 0; i < len; i++)
                        {
                            testingInputs[i] = new double[numInputs];
                        }
                        testingOutputs = new double[len];
                        for (var i = 0; i < len; i++)
                        {
                            testingInputs[i]  = input[i];
                            testingOutputs[i] = output[i];
                        }
                    }
                    catch (FormatException e)
                    {
                        state.Output.Fatal("Some tokens in the file were not numbers.");
                    }
                }
            }
            else
            {
                // determine benchmark
                for (var i = 0; i < names.Length; i++)
                {
                    if (names[i].Equals(problem))  // got it
                    {
                        benchmark = i; break;
                    }
                }
                if (benchmark == -1) // uh oh
                {
                    state.Output.Fatal("Could not find benchmark " + problem, paramBase.Push(P_PROBLEM_TYPE), null);
                }

                state.Output.Message("Doing benchmark " + names[benchmark]);

                try
                {
                    trainingInputs  = trainPoints(state, benchmark, 0);
                    trainingOutputs = new double[trainingInputs.Length];
                    for (var i = 0; i < trainingOutputs.Length; i++)
                    {
                        trainingOutputs[i] = func(state, trainingInputs[i], benchmark);
                    }
                }
                catch (ArgumentException e)
                {
                    state.Output.Fatal("Error in generating training data: " + e.Message);
                }

                try
                {
                    testingInputs  = testPoints(state, benchmark, 0, trainingInputs);
                    testingOutputs = new double[testingInputs.Length];
                    for (var i = 0; i < testingOutputs.Length; i++)
                    {
                        testingOutputs[i] = func(state, testingInputs[i], benchmark);
                    }
                }
                catch (ArgumentException e)
                {
                    state.Output.Fatal("Error in generating testing data: " + e.Message);
                }
            }


            var param = new Parameter("gp.tc.0.fset");  // we assume we have a single tree
            var pval  = state.Parameters.GetString(param, null);

            // verify the number of variables match the expected function set
            if (problem == null)  // it's being loaded from file
            {
                var found = false;
                var vars  = fs_vars[trainingInputs[0].Length];
                for (var i = 0; i < vars.Length; i++)
                {
                    if (pval.Equals(vars[i]))
                    {
                        found = true; break;
                    }
                }
                if (!found)
                {
                    state.Output.Warning("The number of variables in your problem data (" + trainingInputs[0].Length +
                                         "does not match the variables found in the function set " + pval + ".  Hope you know what you're doing.",
                                         param);
                }
                else
                {
                    state.Output.Message("Using function set " + pval);
                }
            }
            else
            {
                if (!(pval.Equals(fs[benchmark])))  // uh oh
                {
                    state.Output.Warning("The number of variables for the " + names[benchmark] +
                                         " problem (" + trainingInputs[0].Length +
                                         ") is normally handled by the function set " + fs[benchmark] +
                                         " but you are using " + pval + ".  Hope you know what you're doing.  " +
                                         "To correct this, try adding the parameter gp.tc.0.fset=" + fs[benchmark],
                                         param);
                }
                else
                {
                    state.Output.Message("Using function set " + pval);
                }
            }

            // set up our input -- don't want to use the default base, it's unsafe
            data = (RegressionData)state.Parameters.GetInstanceForParameterEq(paramBase.Push(P_DATA), null, typeof(RegressionData));
            data.Setup(state, paramBase.Push(P_DATA));
        }
Ejemplo n.º 16
0
        private static void DemoteSomethingDirtyWork(GPNode node, IEvolutionState state, int thread, GPFunctionSet funcs)
        {
            var numDemotable = 0;

            GPType t;
            var    initializer = ((GPInitializer)state.Initializer);

            if (node.Parent is GPNode)
            {
                // ugh, expensive
                t = ((GPNode)(node.Parent)).Constraints(initializer).ChildTypes[node.ArgPosition];
            }
            else
            {
                t = ((GPTree)(node.Parent)).Constraints(initializer).TreeType;
            }

            // Now, determine how many nodes we can demote this under --
            // note this doesn't select based on the total population
            // of "available child positions", but on the total population
            // of *nodes* regardless of if they have more than one possible
            // valid "child position".

            for (var x = 0; x < funcs.Nonterminals[t.Type].Length; x++)
            {
                if (funcs.Nonterminals[t.Type][x].Constraints(initializer).ChildTypes
                    .Any(t1 => t1.CompatibleWith(initializer, node.Constraints(initializer).ReturnType)))
                {
                    numDemotable++;
                }
            }

            // pick a random item to demote -- numDemotable is assumed to be > 0
            var demoteItem = state.Random[thread].NextInt(numDemotable);

            numDemotable = 0;
            // find it

            for (var x = 0; x < funcs.Nonterminals[t.Type].Length; x++)
            {
                if (funcs.Nonterminals[t.Type][x].Constraints(initializer).ChildTypes
                    .Any(t1 => t1.CompatibleWith(initializer, node.Constraints(initializer).ReturnType)))
                {
                    if (numDemotable == demoteItem)
                    {
                        // clone the node
                        var cnode = funcs.Nonterminals[t.Type][x].LightClone();

                        // choose a spot to hang the old parent under
                        var retyp  = node.Constraints(initializer).ReturnType;
                        var chityp = cnode.Constraints(initializer).ChildTypes;

                        var numSpots = cnode.Children.Where((t1, z) => chityp[z].CompatibleWith(initializer, retyp)).Count();
                        var choice   = state.Random[thread].NextInt(numSpots);

                        numSpots = 0;
                        for (var z = 0; z < cnode.Children.Length; z++)
                        {
                            if (chityp[z].CompatibleWith(initializer, retyp))
                            {
                                if (numSpots == choice)
                                {
                                    // demote the parent, inserting cnode
                                    cnode.Parent      = node.Parent;
                                    cnode.ArgPosition = node.ArgPosition;
                                    cnode.Children[z] = node;
                                    node.Parent       = cnode;
                                    node.ArgPosition  = (sbyte)z;
                                    if (cnode.Parent is GPNode)
                                    {
                                        ((GPNode)(cnode.Parent)).Children[cnode.ArgPosition] = cnode;
                                    }
                                    else
                                    {
                                        ((GPTree)(cnode.Parent)).Child = cnode;
                                    }

                                    // this is important to ensure that the
                                    // demotion only happens once!  Otherwise
                                    // you'll get really nasty bugs
                                    numSpots++; // notice no break
                                }
                                else
                                {
                                    // hang a randomly-generated terminal off of cnode
                                    var term = funcs.Terminals
                                               [chityp[z].Type][state.Random[thread].NextInt(funcs.Terminals[chityp[z].Type].Length)].LightClone();

                                    cnode.Children[z] = term;
                                    term.Parent       = cnode;     // just in case
                                    term.ArgPosition  = (sbyte)z;  // just in case
                                    term.ResetNode(state, thread); // let it randomize itself if necessary

                                    // increase numSpots
                                    numSpots++; // notice no break
                                }
                            }
                            else
                            {
                                // hang a randomly-generated terminal off of cnode
                                var term = funcs.Terminals
                                           [chityp[z].Type][state.Random[thread].NextInt(funcs.Terminals[chityp[z].Type].Length)].LightClone();

                                cnode.Children[z] = term;
                                term.Parent       = cnode;     // just in case
                                term.ArgPosition  = (sbyte)z;  // just in case
                                term.ResetNode(state, thread); // let it randomize itself if necessary
                            }
                        }
                        return;
                    }
                    numDemotable++;
                }
            }
            // should never reach here
            throw new ApplicationException("Bug in demoteSomething -- should never be able to reach the end of the function");
        }
Ejemplo n.º 17
0
 /// <summary>
 /// I hard-code both Produce(...) methods for efficiency's sake.
 /// </summary>
 public override int Produce(int subpop, IEvolutionState state, int thread)
 {
     return(0);
 }
Ejemplo n.º 18
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;

            // grab n individuals from our source and stick 'em right into inds.
            // we'll modify them from there
            var n = Sources[0].Produce(min, max, subpop, inds, state, thread, misc);

            // should we bother?
            if (!state.Random[thread].NextBoolean(Likelihood))
            {
                return(n);
            }

            var initializer = (GPInitializer)state.Initializer;

            // now let's mutate 'em
            for (var q = start; q < n + start; q++)
            {
                var i = (GPIndividual)inds[q];

                if (Tree != TREE_UNFIXED && (Tree < 0 || Tree >= i.Trees.Length))
                {
                    // uh oh
                    state.Output.Fatal("MutateDemotePipeline 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");
                }

                for (var x = 0; x < NumTries; x++)
                {
                    int t;
                    // pick random tree
                    if (Tree == TREE_UNFIXED)
                    {
                        if (i.Trees.Length > 1)
                        {
                            t = state.Random[thread].NextInt(i.Trees.Length);
                        }
                        else
                        {
                            t = 0;
                        }
                    }
                    else
                    {
                        t = Tree;
                    }

                    // is the tree demotable?
                    int numdemote = NumDemotableNodes(initializer, i.Trees[t].Child, 0, i.Trees[t].Constraints(initializer).FunctionSet);
                    if (numdemote == 0)
                    {
                        continue; // uh oh, try again
                    }
                    // demote the node, or if we're unsuccessful, just leave it alone
                    PickDemotableNode(initializer, i.Trees[t].Child, state.Random[thread].NextInt(numdemote),
                                      i.Trees[t].Constraints(initializer).FunctionSet);

                    // does this node exceed the maximum depth limits?
                    if (!VerifyPoint(_demotableNode))
                    {
                        continue; // uh oh, try again
                    }
                    // demote it
                    DemoteSomething(_demotableNode, state, thread, i.Trees[t].Constraints(initializer).FunctionSet);
                    i.Evaluated = false;
                    break;
                }

                // add the new individual, replacing its previous source
                inds[q] = i;
            }
            return(n);
        }
Ejemplo n.º 19
0
 /// <summary>
 /// I hard-code both Produce(...) methods for efficiency's sake.
 /// </summary>
 public override int Produce(int subpop, IEvolutionState state, int thread)
 {
     return(state.Random[thread].NextInt(state.Population.Subpops[subpop].Individuals.Count));
 }
Ejemplo n.º 20
0
 /* Produce random sample points between min and max, inclusive, in one dimension.  */
 public double[][] generateRandomSamples(IEvolutionState state, double min, double max, int numPoints, int threadnum)
 {
     return(generateRandomSamples(state, new[] { min }, new[] { max }, numPoints, threadnum));
 }
Ejemplo n.º 21
0
        // making sure that we don't have any children is already
        // done in ERC.checkConstraints(), so we don't need to implement that.

        // this will produce numbers from [-1.0, 1.0), which is probably
        // okay but you might want to modify it if you don't like seeing
        // -1.0's occasionally showing up very rarely.
        public override void ResetNode(IEvolutionState state, int thread)
        {
            value = state.Random[thread].NextDouble() * 2 - 1.0;
        }
Ejemplo n.º 22
0
 /* Produce sample points evenly spaced out between min and max in one dimension, with the given spacing interval.  One dimension only. */
 public double[][] generateIntervalSpacedSamples(IEvolutionState state, double min, double max, double interval, int threadnum)
 {
     return(generateIntervalSpacedSamples(state, new[] { min }, new[] { max }, new[] { interval }, threadnum));
 }
Ejemplo n.º 23
0
 public override void WriteNode(IEvolutionState state, BinaryWriter dataOutput) // throws IOException
 {
     dataOutput.Write(value);
 }
Ejemplo n.º 24
0
        /* Produce sample points for a given benchmark problem.  */
        public double[][] trainPoints(IEvolutionState state, int benchmark, int threadnum)
        {
            switch (benchmark)
            {
            case KOZA1:
            case KOZA2:
            case KOZA3:
                return(generateRandomSamples(state, -1, 1, 20, threadnum));

            case NGUYEN1:
            case NGUYEN2:
            case NGUYEN3:
            case NGUYEN4:
            case NGUYEN5:
            case NGUYEN6:
                return(generateRandomSamples(state, -1, 1, 20, threadnum));

            case NGUYEN7:
                return(generateRandomSamples(state, 0, 2, 20, threadnum));

            case NGUYEN8:
                return(generateRandomSamples(state, 0, 4, 20, threadnum));

            case NGUYEN9:
            case NGUYEN10:
            case NGUYEN11:
            case NGUYEN12:
                return(generateRandomSamples(state, new double[] { 0, 0 }, new double[] { 1, 1 }, 100, threadnum));

            case PAGIE1:
                return(generateIntervalSpacedSamples(state, new[] { -5.0, -5.0 }, new[] { 5.0, 5.0 }, new[] { 0.4, 0.4 }, threadnum));

            case PAGIE2:
                return(generateIntervalSpacedSamples(state, new[] { -5.0, -5.0, -5.0 }, new[] { 5.0, 5.0, 5.0 }, new[] { 0.4, 0.4, 0.4 }, threadnum));


            case KORNS1:
            case KORNS2:
            case KORNS3:
            case KORNS4:
            case KORNS5:
            case KORNS6:
            case KORNS7:
            case KORNS8:
            case KORNS9:
            case KORNS10:
            case KORNS11:
            case KORNS12:
            case KORNS13:
            case KORNS14:
            case KORNS15:
                return(generateRandomSamples(state, new double[] { -50, -50, -50, -50, -50 }, new double[] { 50, 50, 50, 50, 50 }, 10000, threadnum));     // 10000 !!

            case KEIJZER1:
                return(generateIntervalSpacedSamples(state, -1, 1, 0.1, threadnum));

            case KEIJZER2:
                return(generateIntervalSpacedSamples(state, -2, 2, 0.1, threadnum));

            case KEIJZER3:
                return(generateIntervalSpacedSamples(state, -3, 3, 0.1, threadnum));

            case KEIJZER4:
                return(generateIntervalSpacedSamples(state, 0, 10, 0.05, threadnum));

            case KEIJZER5:
                return(generateRandomSamples(state, new double[] { -1, -1, 1 }, new double[] { 1, 1, 2 }, 1000, threadnum));

            case KEIJZER6:
                return(generateIntervalSpacedSamples(state, 1, 50, 1, threadnum));

            case KEIJZER7:
                return(generateIntervalSpacedSamples(state, 1, 100, 1, threadnum));

            case KEIJZER8:
            case KEIJZER9:
                return(generateIntervalSpacedSamples(state, 0, 100, 1, threadnum));

            case KEIJZER10:
                return(generateRandomSamples(state, new double[] { 0, 0 }, new double[] { 1, 1 }, 100, threadnum));

            case KEIJZER11:
            case KEIJZER12:
            case KEIJZER13:
            case KEIJZER14:
            case KEIJZER15:
                return(generateRandomSamples(state, new double[] { -3, -3 }, new double[] { 3, 3 }, 20, threadnum));

            case VLADISLAVLEVA1:
                return(generateRandomSamples(state, new[] { 0.3, 0.3 }, new double[] { 4, 4 }, 100, threadnum));

            case VLADISLAVLEVA2:
                return(generateIntervalSpacedSamples(state, 0.05, 10, 0.1, threadnum));

            case VLADISLAVLEVA3:
                return(generateIntervalSpacedSamples(state, new[] { 0.05, 0.05 }, new[] { 10, 10.05 }, new[] { 0.1, 2 }, threadnum));

            case VLADISLAVLEVA4:
                return(generateRandomSamples(state, new[] { 0.05, 0.05, 0.05, 0.05, 0.05 }, new[] { 6.05, 6.05, 6.05, 6.05, 6.05 }, 1024, threadnum));

            case VLADISLAVLEVA5:
                return(generateRandomSamples(state, new[] { 0.05, 1, 0.05 }, new double[] { 2, 2, 2 }, 300, threadnum));

            case VLADISLAVLEVA6:
                return(generateRandomSamples(state, new[] { 0.1, 0.1 }, new[] { 5.9, 5.9 }, 30, threadnum));

            case VLADISLAVLEVA7:
                return(generateRandomSamples(state, new[] { 0.05, 0.05 }, new[] { 6.05, 6.05 }, 300, threadnum));

            case VLADISLAVLEVA8:
                return(generateRandomSamples(state, new[] { 0.05, 0.05 }, new[] { 6.05, 6.05 }, 50, threadnum));

            default:
                return(null);
            }
        }
Ejemplo n.º 25
0
        static void Main(string[] args)
        {
            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;

            ECActivator.AddSourceAssemblies(new[] { Assembly.GetAssembly(typeof(IEvolutionState)), Assembly.GetAssembly(typeof(FeatureExtractionProblem2)) });
            IEvolutionState state = Evolve.Initialize(Evolve.LoadParameterDatabase(new[] { "-file", @"Parameters\problem2.params" }), 11);

            state.Run(EvolutionState.C_STARTED_FRESH);
            var problem = (FeatureExtractionProblem2)state.Evaluator.p_problem;
            // var treeLog1 = state.Output.AddLog(@"F:\Gesty\logs\gvtree_gen1.txt");
            //var treeLog2 = state.Output.AddLog(@"F:\Gesty\logs\gvtree_gen10.txt");
            //var ecjGraph = state.Output.AddLog(@"F:\Gesty\logs\ecjGraph.txt");
            //var bestOfRun = ((SimpleStatistics)state.Statistics).BestOfRun[0];
            //((GPIndividual)bestOfRun).Trees[0].PrintTree(state, ecjGraph);
            //((GPIndividual)bestOfRun).Trees[0].PrintStyle = GPTree.PRINT_STYLE_DOT;
            //((GPIndividual)bestOfRun).Trees[0].PrintTreeForHumans(state, treeLog);

            var input      = problem.Input;
            var stack      = problem.Stack;
            var stats      = (SimpleStatistics)state.Statistics;
            var bestOfRun  = (GPIndividual)stats.BestOfRun[0];
            var treeReader = new StreamReader(@"F:\Gesty\logs\ecjGraph.txt");

            bestOfRun.Trees[0].ReadTree(state, treeReader);
            bestOfRun.Evaluated = false;


            var imageList = new string[] {
                @"F:\Gesty\problem2\grayscale\A\a\color_0_0002.png",
                @"F:\Gesty\problem2\grayscale\A\b\color_1_0002.png",
                @"F:\Gesty\problem2\grayscale\A\c\color_2_0002.png",
                @"F:\Gesty\problem2\grayscale\A\f\color_5_0002.png",
                @"F:\Gesty\problem2\grayscale\A\h\color_7_0002.png"
            };
            int i = 0;

            foreach (string image in imageList)
            {
                var dir     = @"F:\Gesty\testy\examples";
                var tempImg = new Image <Gray, Byte>(image);
                tempImg.CopyTo(problem.currentImage[0]);
                tempImg.CopyTo(problem.originalImage[0]);
                bestOfRun.Trees[0].Child.Eval(state, 0, input, stack, bestOfRun, problem);
                tempImg.Save(dir + @"\" + i + ".png");
                problem.currentImage[0].Save(dir + @"\" + i + "_trans.png");
                ImageTransformer.GetSquareSuperpixelImages(problem.currentImage[0], dir, i.ToString(), 8);
                i++;
            }
            problem.Evaluate(state, bestOfRun, 0, 0);
            Console.WriteLine(bestOfRun.Fitness);

            /*
             * var confMatFile = new StreamWriter(@"F:\Gesty\testy\confmatBest.csv");
             * for (int x = 0; x < 10; x++)
             * {
             *  var line = new StringBuilder();
             *  for (int j = 0; j < 10; j++)
             *  {
             *      line.Append(problem.confMat[x, j].ToString() + ';');
             *  }
             *  confMatFile.WriteLine(line.ToString().Trim(';'));
             * }
             * confMatFile.Close();
             */

            // Console.WriteLine(bestOfRun.Fitness);

            /*
             * var tempImg = new Image<Gray, Byte>(@"F:\Gesty\problem2\grayscale\A\a\color_0_0002.png");
             * tempImg.CopyTo(problem.currentImage[0]);
             * tempImg.CopyTo(problem.originalImage[0]);
             * ((GPIndividual)bestOfRun).Trees[0].Child.Eval(state, 0, input, stack, bestOfRun, problem);
             * problem.currentImage[0].Save(@"F:\Gesty\testy\transformed.png");
             */

            /*
             * var gesty = new string[8] { "piesc", "dlon", "1p", "2p", "3p", "4p", "5p", "kciuk" };
             * var imageIndex = 0;
             * foreach (string gest in gesty)
             * {
             *  var dir = @"F:\Gesty\superpixel\200x200\" + gest;
             *
             *  //oryginalny
             *  var tempImg = new Image<Gray, Byte>(problem.imageList[imageIndex]);
             *  tempImg.CopyTo(problem.currentImage[0]);
             *  tempImg.CopyTo(problem.originalImage[0]);
             *  tempImg.Dispose();
             *  bestOfRun.Trees[0].Child.Eval(state, 0, input, stack, bestOfRun, problem);
             *  problem.imageTransformer.GetSuperpixelImages(problem.currentImage[0], dir, "oryginalny");
             *  imageIndex += 101;
             *
             *  //obrocony
             *  var tempImg2 = new Image<Gray, Byte>(problem.imageList[imageIndex]);
             *  tempImg2.CopyTo(problem.currentImage[0]);
             *  tempImg2.CopyTo(problem.originalImage[0]);
             *  tempImg2.Dispose();
             *  bestOfRun.Trees[0].Child.Eval(state, 0, input, stack, bestOfRun, problem);
             *  problem.imageTransformer.GetSuperpixelImages(problem.currentImage[0], dir, "obrocony");
             *  imageIndex += 101;
             *
             *  //zaklocony
             *  var tempImg3 = new Image<Gray, Byte>(problem.imageList[imageIndex]);
             *  tempImg3.CopyTo(problem.currentImage[0]);
             *  tempImg3.CopyTo(problem.originalImage[0]);
             *  tempImg3.Dispose();
             *  bestOfRun.Trees[0].Child.Eval(state, 0, input, stack, bestOfRun, problem);
             *  problem.imageTransformer.GetSuperpixelImages(problem.currentImage[0], dir, "zaklocony");
             *  imageIndex += 11;
             * }
             */
            Console.ReadKey();
        }
Ejemplo n.º 26
0
        /* Produce test sample points for a given benchmark problem, to test generalization.  */
        public double[][] testPoints(IEvolutionState state, int benchmark, int threadnum, double[][] trainpoints)
        {
            switch (benchmark)
            {
            case KOZA1:
            case KOZA2:
            case KOZA3:
                return(trainpoints);

            case NGUYEN1:
            case NGUYEN2:
            case NGUYEN3:
            case NGUYEN4:
            case NGUYEN5:
            case NGUYEN6:
            case NGUYEN7:
            case NGUYEN8:
            case NGUYEN9:
            case NGUYEN10:
            case NGUYEN11:
            case NGUYEN12:
                return(trainpoints);

            case PAGIE1:
            case PAGIE2:
                return(trainpoints);

            case KORNS1:
            case KORNS2:
            case KORNS3:
            case KORNS4:
            case KORNS5:
            case KORNS6:
            case KORNS7:
            case KORNS8:
            case KORNS9:
            case KORNS10:
            case KORNS11:
            case KORNS12:
            case KORNS13:
            case KORNS14:
            case KORNS15:
                return(generateRandomSamples(state, new double[] { -50, -50, -50, -50, -50 }, new double[] { 50, 50, 50, 50, 50 }, 10000, threadnum));     // 10000 !!

            case KEIJZER1:
                return(generateIntervalSpacedSamples(state, -1, 1, 0.001, threadnum));

            case KEIJZER2:
                return(generateIntervalSpacedSamples(state, -2, 2, 0.001, threadnum));

            case KEIJZER3:
                return(generateIntervalSpacedSamples(state, -3, 3, 0.001, threadnum));

            case KEIJZER4:
                return(generateIntervalSpacedSamples(state, 0.05, 10.05, 0.05, threadnum));

            case KEIJZER5:
                return(generateRandomSamples(state, new double[] { -1, -1, 1 }, new double[] { 1, 1, 2 }, 10000, threadnum));     // 10000 cases for testing, different than for training

            case KEIJZER6:
                return(generateIntervalSpacedSamples(state, 1, 120, 1, threadnum));

            case KEIJZER7:
                return(generateIntervalSpacedSamples(state, 1, 100, 0.1, threadnum));

            case KEIJZER8:
            case KEIJZER9:
                return(generateIntervalSpacedSamples(state, 0, 100, 0.1, threadnum));

            case KEIJZER10:
                return(generateIntervalSpacedSamples(state, new double[] { 0, 0 }, new double[] { 1, 1 }, new[] { 0.01, 0.01 }, threadnum));

            case KEIJZER11:
            case KEIJZER12:
            case KEIJZER13:
            case KEIJZER14:
            case KEIJZER15:
                return(generateIntervalSpacedSamples(state, new[] { -3.0, -3.0 }, new[] { 3.0, 3.0 }, new[] { 0.01, 0.01 }, threadnum));

            case VLADISLAVLEVA1:
                return(generateIntervalSpacedSamples(state, new[] { -0.2, -0.2 }, new[] { 4.2, 4.2 }, new[] { 0.1, 0.1 }, threadnum));

            case VLADISLAVLEVA2:
                return(generateIntervalSpacedSamples(state, -0.5, 10.5, 0.05, threadnum));

            case VLADISLAVLEVA3:
                return(generateIntervalSpacedSamples(state, new[] { -0.5, -0.5 }, new[] { 10.5, 10.5 }, new[] { 0.05, 0.5 }, threadnum));     // note 0.05 and 0.5, and also 10.5 which is different from training

            case VLADISLAVLEVA4:
                return(generateRandomSamples(state, new[] { -0.25, -0.25, -0.25, -0.25, -0.25 }, new[] { 6.35, 6.35, 6.35, 6.35, 6.35 }, 5000, threadnum));

            case VLADISLAVLEVA5:
                return(generateIntervalSpacedSamples(state, new[] { -0.05, 0.95, -0.05 }, new[] { 2.1, 2.05, 2.1 }, new[] { 0.15, 0.15, 0.1 }, threadnum));     // note 0.05 and 0.5, and also 10.5 which is different from training

            case VLADISLAVLEVA6:
                return(generateIntervalSpacedSamples(state, new[] { -0.05, -0.05 }, new[] { 6.05, 6.05 }, new[] { 0.02, 0.02 }, threadnum));     // note 0.05 and 0.5, and also 10.5 which is different from training

            case VLADISLAVLEVA7:
                return(generateRandomSamples(state, new[] { -0.25, -0.25 }, new[] { 6.35, 6.35 }, 1000, threadnum));

            case VLADISLAVLEVA8:
                return(generateIntervalSpacedSamples(state, new[] { -0.25, -0.25 }, new[] { 6.35, 6.35 }, new[] { 0.2, 0.2 }, threadnum));     // note 0.05 and 0.5, and also 10.5 which is different from training

            default:
                return(null);
            }
        }
Ejemplo n.º 27
0
        public void Setup(IEvolutionState state, Parameter paramBase)
        {
            base.Setup(state, paramBase);

            IParameter def = DefaultBase;
            IParameter p   = paramBase.Push(P_NODESELECTOR).Push("0");
            IParameter d   = def.Push(P_NODESELECTOR).Push("0");

            NodeSelect1 = (IGPNodeSelector)state.Parameters.GetInstanceForParameter(p, d, typeof(IGPNodeSelector));
            NodeSelect1.Setup(state, p);

            p = paramBase.Push(P_NODESELECTOR).Push("1");
            d = def.Push(P_NODESELECTOR).Push("1");

            if (state.Parameters.ParameterExists(p, d) && state.Parameters.GetString(p, d).Equals(V_SAME))
            {
                // can't just copy it this time; the selectors
                // use internal caches. So we have to clone it no matter what
                NodeSelect2 = (IGPNodeSelector)NodeSelect1.Clone();
            }
            else
            {
                NodeSelect2 = (IGPNodeSelector)state.Parameters.GetInstanceForParameter(p, d, typeof(IGPNodeSelector));
                NodeSelect2.Setup(state, p);
            }

            NumTries = state.Parameters.GetInt(paramBase.Push(P_NUM_TRIES), def.Push(P_NUM_TRIES), 1);
            if (NumTries == 0)
            {
                state.Output.Fatal("GPCrossover Pipeline has an invalid number of tries (it must be >= 1).",
                                   paramBase.Push(P_NUM_TRIES), def.Push(P_NUM_TRIES));
            }

            MaxDepth = state.Parameters.GetInt(paramBase.Push(P_MAXDEPTH), def.Push(P_MAXDEPTH), 1);
            if (MaxDepth == 0)
            {
                state.Output.Fatal("GPCrossover Pipeline has an invalid maximum depth (it must be >= 1).",
                                   paramBase.Push(P_MAXDEPTH), def.Push(P_MAXDEPTH));
            }

            Tree1 = TREE_UNFIXED;
            if (state.Parameters.ParameterExists(paramBase.Push(P_TREE).Push("" + 0), def.Push(P_TREE).Push("" + 0)))
            {
                Tree1 = state.Parameters.GetInt(paramBase.Push(P_TREE).Push("" + 0), def.Push(P_TREE).Push("" + 0), 0);
                if (Tree1 == -1)
                {
                    state.Output.Fatal("Tree fixed value, if defined, must be >= 0");
                }
            }

            Tree2 = TREE_UNFIXED;
            if (state.Parameters.ParameterExists(paramBase.Push(P_TREE).Push("" + 1), def.Push(P_TREE).Push("" + 1)))
            {
                Tree2 = state.Parameters.GetInt(paramBase.Push(P_TREE).Push("" + 1), def.Push(P_TREE).Push("" + 1), 0);
                if (Tree2 == -1)
                {
                    state.Output.Fatal("Tree fixed value, if defined, must be >= 0");
                }
            }
            TossSecondParent = state.Parameters.GetBoolean(paramBase.Push(P_TOSS), def.Push(P_TOSS), false);
            if (state.Parameters.ParameterExists(paramBase.Push(P_HOMOLOGOUS), null))
            {
                //get the parameter
                Homologous = state.Parameters.GetBoolean(paramBase.Push(P_HOMOLOGOUS), null, false);
            }
        }
Ejemplo n.º 28
0
        /* Return the function applied to the given data by benchmark problem.  */
        public double func(IEvolutionState state, double[] xs, int benchmark) // throws ArgumentException
        {
            var x = xs[0];
            var y = (xs.Length > 1 ? xs[1] : 0);
            var z = (xs.Length > 2 ? xs[2] : 0);

            switch (benchmark)
            {
            case KOZA1:
                return(x * x * x * x + x * x * x + x * x + x);          // traditional

            case KOZA2:
                return(x * x * x * x * x - 2.0 * x * x * x + x);          // Quintic, from  J. R. Koza, GP II, 1994

            case KOZA3:
                return(x * x * x * x * x * x - 2.0 * x * x * x * x + x * x);     // Sextic, from J. R. Koza, GP II, 1994

            case NGUYEN1:
                return(x * x * x + x * x + x);

            case NGUYEN2:                                                       // identical to KOZA1
                return(x * x * x * x + x * x * x + x * x + x);

            case NGUYEN3:
                return(x * x * x * x * x + x * x * x * x + x * x * x + x * x + x);

            case NGUYEN4:
                return(x * x * x * x * x * x + x * x * x * x * x + x * x * x * x + x * x * x + x * x + x);

            case NGUYEN5:
                return(Math.Sin(x * x) * Math.Cos(x) - 1.0);

            case NGUYEN6:
                return(Math.Sin(x) + Math.Sin(x * x + x));

            case NGUYEN7:
                return(Math.Log(x + 1) + Math.Log(x * x + 1.0));

            case NGUYEN8:                               // Note this presumes you don't have sqrt(x) in your function set!
                return(Math.Sqrt(x));

            case NGUYEN9:
                return(Math.Sin(x) + Math.Sin(y * y));

            case NGUYEN10:
                return(2 * Math.Sin(x) * Math.Cos(y));

            case NGUYEN11:
                return(Math.Pow(x, y));

            case NGUYEN12:
                return(x * x * x * x - x * x * x + (y * y) / 2.0 - y);

            case PAGIE1:
                // otherwise known as 1 / (1 + Math.pow(x,-4)) + 1 / (1 + Math.pow(y,-1))
                return(1.0 / (1.0 + 1.0 / (x * x * x * x)) + 1.0 / (1.0 + 1.0 / (y * y * y * y)));

            case PAGIE2:
                // otherwise known as (1 / (1 + Math.pow(x, -4)) + 1 / (1 + Math.pow(y, -4)) + 1 / (1 + Math.pow(z, -4)));
                return(1.0 / (1.0 + 10 / (x * x * x * x)) + 1.0 / (1.0 + 1.0 / (y * y * y * y)) + 1.0 / (1.0 + 1.0 / (z * z * z * z)));

            case KORNS1:
                return(1.57 + (24.3 * xs[3]));

            case KORNS2:
                return(0.23 + (14.2 * ((xs[3] + xs[1]) / (3.0 * xs[4]))));

            case KORNS3:
                return(-5.41 + (4.9 * (((xs[3] - xs[0]) + (xs[1] / xs[4])) / (3 * xs[4]))));

            case KORNS4:
                return(-2.3 + (0.13 * Math.Sin(xs[2])));

            case KORNS5:
                return(3.0 + (2.13 * Math.Log(xs[4])));

            case KORNS6:
                return(1.3 + (0.13 * Math.Sqrt(xs[0])));

            case KORNS7:
                return(213.80940889 - (213.80940889 * Math.Exp(-0.54723748542 * xs[0])));

            case KORNS8:
                return(6.87 + (11.0 * Math.Sqrt(7.23 * xs[0] * xs[3] * xs[4])));

            case KORNS9:
                return(Math.Sqrt(xs[0]) / Math.Log(xs[1]) * Math.Exp(xs[2] / (xs[3] * xs[3])));

            case KORNS10:
                return(0.81 + (24.3 * (((2.0 * xs[1]) + (3.0 * (xs[2] * xs[2]))) / ((4.0 * (xs[3] * xs[3] * xs[3])) + (5.0 * (xs[4] * xs[4] * xs[4] * xs[4]))))));

            case KORNS11:
                return(6.87 + (11.0 * Math.Cos(7.23 * xs[0] * xs[0] * xs[0])));

            case KORNS12:
                return(2.0 - (2.1 * (Math.Cos(9.8 * xs[0]) * Math.Sin(1.3 * xs[4]))));

            case KORNS13:
                return(32.0 - (3.0 * ((Math.Tan(xs[0]) / Math.Tan(xs[1])) * (Math.Tan(xs[2]) / Math.Tan(xs[3])))));

            case KORNS14:
                return(22.0 - (4.2 * ((Math.Cos(xs[0]) - Math.Tan(xs[1])) * (Math.Tanh(xs[2]) / Math.Sin(xs[3])))));

            case KORNS15:
                return(12.0 - (6.0 * ((Math.Tan(xs[0]) / Math.Exp(xs[1])) * (Math.Log(xs[2]) - Math.Tan(xs[3])))));

            case KEIJZER1:      // fall thru
            case KEIJZER2:      // fall thru
            case KEIJZER3:
                return(0.3 * x * Math.Sin(2 * Math.PI * x));

            case KEIJZER4:
                return(x * x * x * Math.Exp(-x) * Math.Cos(x) * Math.Sin(x) * (Math.Sin(x) * Math.Sin(x) * Math.Cos(x) - 1));

            case KEIJZER5:
                return((30.0 * x * z) / ((x - 10.0) * y * y));

            case KEIJZER6:
            {
                var sum = 0.0;
                var fx  = Math.Floor(x);
                for (var i = 1; i < fx + 1; i++)          // up to and including floor(x)
                {
                    sum += (1.0 / i);
                }
                return(sum);
            }

            case KEIJZER7:                              // Note this presumes you don't have log(x) in your function set!
                return(Math.Log(x));

            case KEIJZER8:                              // same as NGUYEN8
                return(Math.Sqrt(x));

            case KEIJZER9:
                return(asinh(x));      // Not a function in Math

            case KEIJZER10:
                return(Math.Pow(x, y));     // same as NGUYEN11

            case KEIJZER11:
                return(x * y + Math.Sin((x - 1.0) * (y - 1.0)));

            case KEIJZER12:
                return(x * x * x * x - x * x * x + y * y / 2.0 - y);

            case KEIJZER13:
                return(6.0 * Math.Sin(x) * Math.Cos(y));

            case KEIJZER14:
                return(8.0 / (2.0 + x * x + y * y));

            case KEIJZER15:
                return(x * x * x / 5.0 + y * y * y / 2.0 - y - x);

            case VLADISLAVLEVA1:
                return(Math.Exp(-(x - 1) * (x - 1)) / (1.2 + (y - 2.5) * (y - 2.5)));

            case VLADISLAVLEVA2:
                return(Math.Exp(-x) * x * x * x * Math.Cos(x) * Math.Sin(x) * (Math.Cos(x) * Math.Sin(x) * Math.Sin(x) - 1));

            case VLADISLAVLEVA3:
                return(Math.Exp(-x) * x * x * x * Math.Cos(x) * Math.Sin(x) * (Math.Cos(x) * Math.Sin(x) * Math.Sin(x) - 1) * (y - 5));

            case VLADISLAVLEVA4:
            {
                double sum = 0;
                for (var i = 0; i < 5; i++)
                {
                    sum += (xs[i] - 3) * (xs[i] - 3);
                }
                return(10.0 / (5.0 + sum));
            }

            case VLADISLAVLEVA5:
                return((30.0 * (x - 1.0) * (z - 1.0)) / (y * y * (x - 10.0)));

            case VLADISLAVLEVA6:
                return(6.0 * Math.Sin(x) * Math.Cos(y));

            case VLADISLAVLEVA7:
                return((x - 3.0) * (y - 3.0) + 2 * Math.Sin((x - 4.0) * (y - 4.0)));

            case VLADISLAVLEVA8:
                return(((x - 3.0) * (x - 3.0) * (x - 3.0) * (x - 3.0) + (y - 3.0) * (y - 3.0) * (y - 3.0) - (y - 3.0)) / ((y - 2.0) * (y - 2.0) * (y - 2.0) * (y - 2.0) + 10.0));

            default:
                throw new ArgumentException("Invalid benchmark value " + benchmark);
            }
            // never reaches here
        }
Ejemplo n.º 29
0
        /**
         * This method finds a node using the logic given in the langdon paper.
         * @param nodeToSubtrees For Tree of Parent2 all precomputed stats about depth,subtrees etc
         * @param sizeToNodes Quick lookup for LinkedList of size to Nodes
         * @param parent1SelectedNode Node selected in parent1
         * @param Tree2 Tree of parent2
         * @param state Evolution State passed for getting access to Random Object of MersenneTwiser
         * @param thread thread number
         */
        protected GPNode FindFairSizeNode(ArrayList nodeToSubtrees,
                                          Hashtable sizeToNodes,
                                          GPNode parent1SelectedNode,
                                          GPTree tree2,
                                          IEvolutionState state,
                                          int thread)
        {
            GPNode selectedNode = null;
            // get the size of subtrees of parent1
            int parent1SubTrees = parent1SelectedNode.NumNodes(GPNode.NODESEARCH_NONTERMINALS);
            // the maximum length in mate we are looking for
            int maxmatesublen = parent1SubTrees == 0 ? 0 : 2 * parent1SubTrees + 1;

            // lets see if for all lengths we have trees corresponding
            bool[] mateSizeAvailable = new bool[maxmatesublen + 1];
            // initialize the array to false
            for (int i = 0; i < maxmatesublen; i++)
            {
                mateSizeAvailable[i] = false;
            }
            // check for ones we have
            for (int i = 0; i < nodeToSubtrees.Count; i++)
            {
                NodeInfo nodeInfo = (NodeInfo)nodeToSubtrees[i];
                // get the length of trees
                int subtree = nodeInfo.NumberOfSubTreesBeneath;
                if (subtree <= maxmatesublen)
                {
                    mateSizeAvailable[subtree] = true;
                }
            }
            // choose matesublen so mean size change=0 if possible
            int countOfPositives = 0;
            int countOfNegatives = 0;
            int sumOfPositives   = 0;
            int sumOfNegatives   = 0;
            int l;

            for (l = 1; l < parent1SubTrees; l++)
            {
                if (mateSizeAvailable[l])
                {
                    countOfNegatives++;
                    sumOfNegatives += parent1SubTrees - l;
                }
            }
            for (l = parent1SubTrees + 1; l <= maxmatesublen; l++)
            {
                if (mateSizeAvailable[l])
                {
                    countOfPositives++;
                    sumOfPositives += l - parent1SubTrees;
                }
            }
            // if they are missing use the same
            int mateSublengthSelected = 0;

            if (sumOfPositives == 0 || sumOfNegatives == 0)
            {
                //if so then check if mate has the length and use that
                if (mateSizeAvailable[parent1SubTrees])
                {
                    mateSublengthSelected = parent1SubTrees;
                }
                //else we go with zero
            }
            else
            {
                // probability of same is dependent on do we find same sub trees
                // else 0.0
                double pzero = (mateSizeAvailable[parent1SubTrees]) ? 1.0 / parent1SubTrees : 0.0;
                // positive probability
                double ppositive = (1.0 - pzero) /
                                   (countOfPositives +
                                    ((double)(countOfNegatives * sumOfPositives) / (sumOfNegatives)));
                // negative probability
                double pnegative = (1.0 - pzero) /
                                   (countOfNegatives +
                                    ((double)(countOfPositives * sumOfNegatives) / (sumOfPositives)));
                // total probability, just for making sure math is right ;-)
                double total = countOfNegatives * pnegative + pzero + countOfPositives * ppositive;
                // putting an assert for floating point calculations, similar to what langdon does
                // assert(total<1.01&&total>.99);
                // now create a Roulette Wheel
                RouletteWheelSelector wheel = new RouletteWheelSelector(maxmatesublen);
                // add probabilities to the wheel
                // all below the length of parent node get pnegative
                // all above get ppositive and one on node gets pzero
                for (l = 1; l < parent1SubTrees; l++)
                {
                    if (mateSizeAvailable[l])
                    {
                        wheel.Add(pnegative, l);
                    }
                }
                if (mateSizeAvailable[parent1SubTrees])
                {
                    wheel.Add(pzero, parent1SubTrees);
                }
                for (l = parent1SubTrees + 1; l <= maxmatesublen; l++)
                {
                    if (mateSizeAvailable[l])
                    {
                        wheel.Add(ppositive, l);
                    }
                }
                // spin the wheel
                mateSublengthSelected = wheel.Roulette(state, thread);
            }
            // now we have length chosen, but there can be many nodes with that
            //
            LinkedList <NodeInfo> listOfNodes = (LinkedList <NodeInfo>)sizeToNodes[mateSublengthSelected];

            if (listOfNodes == null)
            {
                state.Output.Fatal("In SizeFairCrossoverPipeline, nodes for tree length " + mateSublengthSelected + " is null, indicates some serious error");
            }
            // in size fair we choose the elements at random for given length
            int chosenNode = 0;

            // if using fair size get random from the list
            if (!Homologous)
            {
                chosenNode = state.Random[thread].NextInt(listOfNodes.Count);
            }
            // if Homologous
            else
            {
                if (listOfNodes.Count > 1)
                {
                    GPInitializer initializer        = ((GPInitializer)state.Initializer);
                    int           currentMinDistance = int.MaxValue;
                    for (int i = 0; i < listOfNodes.Count; i++)
                    {
                        // get the GP node
                        GPNode selectedMateNode = listOfNodes.ElementAt(i).Node;
                        // now lets traverse selected and parent 1 to see divergence
                        GPNode currentMateNode    = selectedMateNode;
                        GPNode currentParent1Node = parent1SelectedNode;
                        // found a match?
                        bool foundAMatchInAncestor = false;
                        int  distance = 0;
                        while (currentMateNode.Parent != null &&
                               currentMateNode.Parent is GPNode &&
                               currentParent1Node.Parent != null &&
                               currentParent1Node.Parent is GPNode &&
                               !foundAMatchInAncestor)
                        {
                            GPNode parent1 = (GPNode)currentParent1Node.Parent;
                            GPNode parent2 = (GPNode)currentMateNode.Parent;
                            // if there is match between compatibility of Parents break
                            if (parent1.SwapCompatibleWith(initializer, parent2))
                            {
                                foundAMatchInAncestor = true;
                                break;
                            }
                            else
                            {
                                // need to go one level above of both
                                currentMateNode    = parent2;
                                currentParent1Node = parent1;
                                //increment the distance
                                distance = distance + 1;
                            }
                        }
                        // find the one with least distance
                        if (distance < currentMinDistance)
                        {
                            currentMinDistance = distance;
                            chosenNode         = i;
                        }
                    }
                }
                // else take the first node, no choice
            }
            NodeInfo nodeInfoSelected = listOfNodes.ElementAt(chosenNode);

            selectedNode = nodeInfoSelected.Node;

            return(selectedNode);
        }
Ejemplo n.º 30
0
        public override void ReadIndividual(IEvolutionState state, BinaryReader dataInput)
        {
            base.ReadIndividual(state, dataInput);

            // Next, read auxillary arrays.
            if (dataInput.ReadBoolean())
            {
                Velocity = new double[dataInput.ReadInt32()];
                for (int i = 0; i < Velocity.Length; i++)
                {
                    Velocity[i] = dataInput.ReadDouble();
                }
            }
            else
            {
                Velocity = new double[genome.Length];
            }

            if (dataInput.ReadBoolean())
            {
                Neighborhood = new int[dataInput.ReadInt32()];
                for (int i = 0; i < Neighborhood.Length; i++)
                {
                    Neighborhood[i] = dataInput.ReadInt32();
                }
            }
            else
            {
                Neighborhood = null;
            }

            if (dataInput.ReadBoolean())
            {
                NeighborhoodBestGenome = new double[dataInput.ReadInt32()];
                for (int i = 0; i < NeighborhoodBestGenome.Length; i++)
                {
                    NeighborhoodBestGenome[i] = dataInput.ReadDouble();
                }
            }
            else
            {
                NeighborhoodBestGenome = null;
            }

            if (dataInput.ReadBoolean())
            {
                NeighborhoodBestFitness = (Fitness)Fitness.Clone();
                NeighborhoodBestFitness.ReadFitness(state, dataInput);
            }

            if (dataInput.ReadBoolean())
            {
                PersonalBestGenome = new double[dataInput.ReadInt32()];
                for (int i = 0; i < PersonalBestGenome.Length; i++)
                {
                    PersonalBestGenome[i] = dataInput.ReadDouble();
                }
            }
            else
            {
                PersonalBestGenome = null;
            }

            if (dataInput.ReadBoolean())
            {
                PersonalBestFitness = (Fitness)Fitness.Clone();
                PersonalBestFitness.ReadFitness(state, dataInput);
            }
        }