/// <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); }
/// <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); }