Example #1
0
        /// <summary>
        /// Initializes an evolutionary run given the parameters and a random seed adjustment (added to each random seed),
        /// with the Output pre-constructed.
        /// The adjustment offers a convenient way to change the seeds of the random number generators each time you
        /// do a new evolutionary run.  You are of course welcome to replace the random number generators after initialize(...)
        /// but before startFresh(...).
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="randomSeedOffset"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public static EvolutionState Initialize(IParameterDatabase parameters, int randomSeedOffset, Output output)
        {
            var breedthreads = 1;
            var evalthreads  = 1;

            //bool store;
            //int x;

            // output was already created for us.
            output.SystemMessage(ECVersion.Message());

            // 2. set up thread values

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

            var auto = (V_THREADS_AUTO.ToUpper().Equals(parameters.GetString(new Parameter(P_BREEDTHREADS), null).ToUpper())
                        ||
                        V_THREADS_AUTO.ToUpper().Equals(
                            parameters.GetString(new Parameter(P_EVALTHREADS), null).ToUpper()));
            // at least one thread is automatic.  Seeds may need to be dynamic.

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

            var random = new MersenneTwisterFast[1];

            var seedMessage = "Seed: ";

            // Get time in milliseconds
            var time = (int)DateTimeHelper.CurrentTimeMilliseconds;

            var seed = 0;

            seed = DetermineSeed(output, parameters, new Parameter(P_SEED).Push("" + 0),
                                 time + 0, random.Length * randomSeedOffset, auto);

            random[0] = PrimeGenerator(new MersenneTwisterFast(seed));
            // we prime the generator to be more sure of randomness.
            seedMessage = seedMessage + seed + " ";

            // 4.  Start up the evolution

            // what evolution state to use?
            var state = (EvolutionState)parameters.GetInstanceForParameter(new Parameter(P_STATE), null, typeof(IEvolutionState));

            state.Parameters       = parameters;
            state.Random           = random;
            state.Output           = output;
            state.EvalThreads      = evalthreads;
            state.BreedThreads     = breedthreads;
            state.RandomSeedOffset = randomSeedOffset;

            output.SystemMessage($"Threads:  breed/{breedthreads} eval/{evalthreads}");
            output.SystemMessage(seedMessage);

            return(state);
        }
Example #2
0
 /** Optionally prints the help message. */
 public static void CheckForHelp(String[] args)
 {
     for (int x = 0; x < args.Length; x++)
     {
         if (args[x].Equals(A_HELP))
         {
             Trace.WriteLine(ECVersion.Message());
             Trace.WriteLine(
                 "Format:\n\n" +
                 "    java ec.Evolve -file FILE [-p PARAM=VALUE] [-p PARAM=VALUE] ...\n" +
                 "    java ec.Evolve -from FILE [-p PARAM=VALUE] [-p PARAM=VALUE] ...\n" +
                 "    java ec.Evolve -from FILE -at CLASS [-p PARAM=VALUE] [-p PARAM=VALUE] ...\n" +
                 "    java ec.Evolve -checkpoint CHECKPOINT\n" +
                 "    java ec.Evolve -help\n\n" +
                 "-help                   Shows this message and exits.\n\n" +
                 "-file FILE              Launches ECJ using the provided parameter FILE.\n\n" +
                 "-from FILE              Launches ECJ using the provided parameter FILE\n" +
                 "                        which is defined relative to the directory\n" +
                 "                        holding the classfile ec/Evolve.class  If this\n" +
                 "                        class file is found inside a Jar file, then the\n" +
                 "                        FILE will also be assumed to be in that Jar file,\n" +
                 "                        at the proper relative location.\n\n" +
                 "-from FILE -at CLASS    Launches ECJ using the provided parameter FILE\n" +
                 "                        which is defined relative to the directory\n" +
                 "                        holding the classfile CLASS (for example,\n" +
                 "                        ec/ant/ant.class).  If this class file is found\n" +
                 "                        inside a Jar file, then the FILE will also be\n" +
                 "                        assumed to be in that Jar file, at the proper\n" +
                 "                        relative location.\n\n" +
                 "-p PARAM=VALUE          Overrides the parameter PARAM in the parameter\n" +
                 "                        file, setting it to the value VALUE instead.  You\n" +
                 "                        can override as many parameters as you like on\n" +
                 "                        the command line.\n\n" +
                 "-checkpoint CHECKPOINT  Launches ECJ from the provided CHECKPOINT file.\n"
                 );
             Environment.Exit(1);
         }
     }
 }
Example #3
0
        /// <summary>
        /// Initializes an evolutionary run given the parameters and a random seed adjustment (added to each random seed),
        /// with the Output pre-constructed.
        /// The adjustment offers a convenient way to change the seeds of the random number generators each time you
        /// do a new evolutionary run.  You are of course welcome to replace the random number generators after initialize(...)
        /// but before startFresh(...).
        /// </summary>
        public static EvolutionState Initialize(IParameterDatabase parameters, int randomSeedOffset, Output output)
        {
            var breedthreads = 1;
            var evalthreads  = 1;

            // Should we muzzle stdout and stderr?

            if (parameters.ParameterExists(new Parameter(P_MUZZLE), null))
            {
                output.Warning("" + new Parameter(P_MUZZLE) + " has been deprecated.  We suggest you use " +
                               new Parameter(P_SILENT) + " or similar newer options.");
            }

            if (parameters.GetBoolean(new Parameter(P_SILENT), null, false) ||
                parameters.GetBoolean(new Parameter(P_MUZZLE), null, false))
            {
                output.GetLog(0).Silent = true;
                output.GetLog(1).Silent = true;
            }

            //bool store;
            int x;

            // output was already created for us.
            output.SystemMessage(ECVersion.Message());

            // 2. set up thread values

            breedthreads = DetermineThreads(output, parameters, new Parameter(P_BREEDTHREADS));
            evalthreads  = DetermineThreads(output, parameters, new Parameter(P_EVALTHREADS));
            var auto = (V_THREADS_AUTO.ToUpper().Equals(
                            parameters.GetString(new Parameter(P_BREEDTHREADS), null).ToUpper())
                        ||
                        V_THREADS_AUTO.ToUpper().Equals(
                            parameters.GetString(new Parameter(P_EVALTHREADS), null).ToUpper()));
            // at least one thread is automatic.  Seeds may need to be dynamic.

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

            var random = new IMersenneTwister[breedthreads > evalthreads ? breedthreads : evalthreads];
            var seeds  = new int[random.Length];

            var seedMessage = "Seed: ";

            // Get time in milliseconds
            var time = (int)DateTimeHelper.CurrentTimeMilliseconds;

            for (x = 0; x < random.Length; x++)
            {
                seeds[x] = DetermineSeed(output, parameters, new Parameter(P_SEED).Push("" + x),
                                         time + x, random.Length * randomSeedOffset, auto);

                for (var y = 0; y < x; y++)
                {
                    if (seeds[x] == seeds[y])
                    {
                        output.Fatal(P_SEED + "." + x + " (" + seeds[x] + ") and "
                                     + P_SEED + "." + y + " (" + seeds[y] + ") ought not be the same seed.", null, null);
                    }
                }

                random[x] = PrimeGenerator(new MersenneTwisterFast(seeds[x]));
                // we prime the generator to be more sure of randomness.
                seedMessage = seedMessage + seeds[x] + " ";
            }

            // 4.  Start up the evolution

            // what evolution state to use?
            var state = (EvolutionState)parameters.GetInstanceForParameter(new Parameter(P_STATE), null, typeof(IEvolutionState));

            state.Parameters       = parameters;
            state.Random           = random;
            state.Output           = output;
            state.EvalThreads      = evalthreads;
            state.BreedThreads     = breedthreads;
            state.RandomSeedOffset = randomSeedOffset;

            output.SystemMessage($"Threads:  breed/{breedthreads} eval/{evalthreads}");
            output.SystemMessage(seedMessage);

            return(state);
        }
Example #4
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");
                }
            }
        }