Skip to content

Commit

Permalink
Code reuse for complexity and prevent graphs from blowing up in size
Browse files Browse the repository at this point in the history
  • Loading branch information
ssitu committed Dec 7, 2023
1 parent 370f344 commit fcf9a72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
15 changes: 15 additions & 0 deletions source/Agents/GP/CGPAgent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ namespace cowboys {
assert(dynamic_cast<const CGPAgent *>(&other) != nullptr);
Configure(dynamic_cast<const CGPAgent &>(other));
}

/// @brief The complexity of this agent. Used for fitness.
/// @return The complexity of this agent.
double GetComplexity() const {
double connection_complexity =
static_cast<double>(genotype.GetNumConnections()) / genotype.GetNumPossibleConnections();

double functional_nodes = genotype.GetNumFunctionalNodes();

// Just needed some function such that connection_complexity + node_complexity grows as the number of nodes grows, this function makes the increase more gradual.
double node_complexity = std::log(functional_nodes) / 5;

double complexity = connection_complexity + node_complexity;
return complexity;
}
};

} // End of namespace cowboys
20 changes: 2 additions & 18 deletions source/Agents/GP/GPTrainingLoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,7 @@ namespace cowboys {

// Agent complexity, temporarily doing this in a bad way
if (auto *cgp = dynamic_cast<CGPAgent *>(&agent)) {
auto genotype = cgp->GetGenotype();
double connection_complexity =
static_cast<double>(genotype.GetNumConnections()) / genotype.GetNumPossibleConnections();

double functional_nodes = genotype.GetNumFunctionalNodes();
double node_complexity = functional_nodes / (functional_nodes + 1);

double complexity = connection_complexity + node_complexity;
fitness -= complexity;
fitness -= cgp->GetComplexity();
}

return fitness;
Expand All @@ -316,15 +308,7 @@ namespace cowboys {
cse491::AgentBase &agent = *agents[arena][a];
// Agent complexity, temporarily doing this in a bad way
if (auto *cgp = dynamic_cast<CGPAgent *>(&agent)){
auto genotype = cgp->GetGenotype();
double connection_complexity =
static_cast<double>(genotype.GetNumConnections()) / genotype.GetNumPossibleConnections();

double functional_nodes = genotype.GetNumFunctionalNodes();
double node_complexity = functional_nodes / (functional_nodes + 1);

double complexity = connection_complexity + node_complexity;
fitness -= complexity;
fitness -= cgp->GetComplexity();
}

return fitness;
Expand Down

0 comments on commit fcf9a72

Please sign in to comment.