private static void TestCode() { ECActivator.AddSourceAssemblies(new[] { Assembly.GetAssembly(typeof(IEvolutionState)), Assembly.GetAssembly(typeof(ClassificationProblem)) }); var parameters = Evolve.LoadParameterDatabase(new[] { "-file", @"Params\App\Iris\koza.params" }); IEvolutionState state = Evolve.Initialize(parameters, 0); state.Setup(state, new Parameter(new string[] { "a" })); //state.Run(EvolutionState.C_STARTED_FRESH); // var individual = (GPIndividual)((SimpleStatistics)state.Statistics).BestOfRun[0]; var tree = new GPTree(); var reader = new System.IO.StreamReader("single_cl_ecj_graph.txt"); tree.ReadTree(state, reader); var writer = new System.IO.StreamWriter("testcode.txt"); var code = TreeReader.PrintCodeFromTree(tree); writer.Write(code); writer.Close(); }
private static void CheckTreeFromFile() { ECActivator.AddSourceAssemblies(new[] { Assembly.GetAssembly(typeof(IEvolutionState)), Assembly.GetAssembly(typeof(ClassificationProblem)) }); var parameters = Evolve.LoadParameterDatabase(new[] { "-file", @"Params\App\Iris\koza.params" }); IEvolutionState state = Evolve.Initialize(parameters, 0); state.Setup(state, new Parameter(new string[] { "a" })); //state.Run(EvolutionState.C_STARTED_FRESH); var individual = (GPIndividual)((SimpleStatistics)state.Statistics).BestOfRun[0]; var tree = new GPTree(); var reader = new System.IO.StreamReader("single_cl_ecj_graph.txt"); tree.ReadTree(state, reader); // individual.Setup(state, new Parameter(new string[] { "numtrees = 1", "1" })); individual.Trees[0] = tree; // CheckClassifierOnTestData(new[] { individual }, state, (int)ClassifierType.SINGLE_CLASSIFIER, directoryName); }
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"); } } }