diff --git a/source/Agents/GP/CGPAgent.hpp b/source/Agents/GP/CGPAgent.hpp index 26e1f661..b97881e7 100644 --- a/source/Agents/GP/CGPAgent.hpp +++ b/source/Agents/GP/CGPAgent.hpp @@ -127,6 +127,21 @@ namespace cowboys { assert(dynamic_cast(&other) != nullptr); Configure(dynamic_cast(other)); } + + /// @brief The complexity of this agent. Used for fitness. + /// @return The complexity of this agent. + double GetComplexity() const { + double connection_complexity = + static_cast(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 diff --git a/source/Agents/GP/GPTrainingLoop.hpp b/source/Agents/GP/GPTrainingLoop.hpp index ff9d3618..896ddfe3 100644 --- a/source/Agents/GP/GPTrainingLoop.hpp +++ b/source/Agents/GP/GPTrainingLoop.hpp @@ -283,15 +283,7 @@ namespace cowboys { // Agent complexity, temporarily doing this in a bad way if (auto *cgp = dynamic_cast(&agent)) { - auto genotype = cgp->GetGenotype(); - double connection_complexity = - static_cast(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; @@ -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(&agent)){ - auto genotype = cgp->GetGenotype(); - double connection_complexity = - static_cast(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;