Skip to content

Commit

Permalink
Remove random number generator from CGPGenotype
Browse files Browse the repository at this point in the history
  • Loading branch information
ssitu committed Dec 6, 2023
1 parent c564f50 commit a1732f4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
2 changes: 0 additions & 2 deletions source/Agents/GP/CGPAgent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ namespace cowboys {
genotype = CGPGenotype({INPUT_SIZE, action_map.size(), NUM_LAYERS, NUM_NODES_PER_LAYER, LAYERS_BACK});
}

genotype.SetSeed(rand());

// Mutate the beginning genotype, might not want this.
MutateAgent(0.2);

Expand Down
27 changes: 7 additions & 20 deletions source/Agents/GP/CGPGenotype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,6 @@ namespace cowboys {
/// The node configurations.
std::vector<CGPNodeGene> nodes;

/// The random number generator.
std::mt19937 rng;

private:
/// @brief Encodes the header into a string.
/// @return The encoded header.
Expand Down Expand Up @@ -512,11 +509,8 @@ namespace cowboys {
/// @return Bool value to indicate if any input connections non-zero.
bool HasInputConnections() const {
for (auto it = begin(); it != end(); ++it) {
if (std::any_of(
it->input_connections.begin(),
it->input_connections.end(),
[](char c) { return c != '0'; }
)) return true;
if (std::any_of(it->input_connections.begin(), it->input_connections.end(), [](char c) { return c != '0'; }))
return true;
}
return false;
}
Expand Down Expand Up @@ -563,22 +557,15 @@ namespace cowboys {
return header + HEADER_END + genotype;
}

/// @brief Sets the seed of the random number generator.
CGPGenotype &SetSeed(size_t seed) {
rng.seed(seed);
return *this;
}

/// @brief Mutates the genotype.
/// @param mutation_rate Value between 0 and 1 representing the probability of mutating a value.
/// @param mutation The function to use for mutating the output. The function will receive the node gene as a
/// parameter.
/// @return This genotype.
CGPGenotype &Mutate(double mutation_rate, std::function<void(CGPNodeGene &)> mutation) {
CGPGenotype &Mutate(double mutation_rate, GPAgentBase &agent, std::function<void(CGPNodeGene &)> mutation) {
assert(mutation_rate >= 0.0 && mutation_rate <= 1.0);
std::uniform_real_distribution<double> dist_mutation(0.0, 1.0);
for (CGPNodeGene &node : nodes)
if (dist_mutation(rng) < mutation_rate)
if (agent.GetRandom() < mutation_rate)
mutation(node);
return *this;
}
Expand All @@ -590,7 +577,7 @@ namespace cowboys {
/// @return This genotype.
CGPGenotype &MutateConnections(double mutation_rate, GPAgentBase &agent) {
std::uniform_int_distribution<size_t> dist(0, 1);
Mutate(mutation_rate, [&agent](CGPNodeGene &node) {
Mutate(mutation_rate, agent, [&agent](CGPNodeGene &node) {
for (char &con : node.input_connections) {
con = agent.GetRandomULL(2) == 0 ? '0' : '1';
}
Expand All @@ -603,7 +590,7 @@ namespace cowboys {
/// @param num_functions The number of functions available to the nodes.
/// @return This genotype.
CGPGenotype &MutateFunctions(double mutation_rate, size_t num_functions, GPAgentBase &agent) {
Mutate(mutation_rate,
Mutate(mutation_rate, agent,
[num_functions, &agent](CGPNodeGene &node) { node.function_idx = agent.GetRandomULL(num_functions); });
return *this;
}
Expand All @@ -615,7 +602,7 @@ namespace cowboys {
/// @return This genotype.
CGPGenotype &MutateOutputs(double mutation_rate, double mean, double std, GPAgentBase &agent,
bool additive = true) {
Mutate(mutation_rate, [mean, std, &agent, additive](CGPNodeGene &node) {
Mutate(mutation_rate, agent, [mean, std, &agent, additive](CGPNodeGene &node) {
double mutation = agent.GetRandomNormal(mean, std);
if (additive) {
node.default_output += mutation;
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Agents/GP/GraphBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST_CASE("Cartesian Graph", "[group7][graph][cartesian]") {
size_t iterations = 100;
for (size_t i = 0; i < iterations; ++i) {
CGPGenotype genotype({INPUT_SIZE, NUM_OUTPUTS, NUM_LAYERS, NUM_NODES_PER_LAYER, LAYERS_BACK});
genotype.SetSeed(i).MutateDefault(1, mock_agent, NODE_FUNCTION_SET.size());
genotype.MutateDefault(1, mock_agent, NODE_FUNCTION_SET.size());
auto graph = builder.CartesianGraph(genotype, NODE_FUNCTION_SET);
auto new_action = graph->MakeDecision(inputs, actions);
choose_same_action = choose_same_action && (new_action == action);
Expand All @@ -75,7 +75,7 @@ TEST_CASE("Cartesian Graph", "[group7][graph][cartesian]") {
size_t iterations = 100;
for (size_t i = 0; i < iterations; ++i) {
auto copy = base;
copy.SetSeed(i).MutateHeader(1, mock_agent);
copy.MutateHeader(1, mock_agent);
CHECK_FALSE(copy == base);
auto expanded_graph = builder.CartesianGraph(copy, NODE_FUNCTION_SET);
auto new_action = expanded_graph->MakeDecision(inputs, actions);
Expand Down

0 comments on commit a1732f4

Please sign in to comment.