18 #include "functions.h" 20 #include <core/darwin.h> 21 #include <core/properties.h> 22 #include <core/stringify.h> 34 enum class MutationStrategy {
39 inline auto customStringify(core::TypeTag<MutationStrategy>) {
41 { MutationStrategy::FixedCount,
"fixed_count" },
42 { MutationStrategy::Probabilistic,
"probabilistic" },
48 PROPERTY(mutation_count,
int, 2,
"Number of mutations per genotype");
52 PROPERTY(connection_mutation_chance,
55 "Probability of mutating a connection");
56 PROPERTY(function_mutation_chance,
59 "Probability of mutating a node's function");
60 PROPERTY(output_mutation_chance,
63 "Probability of mutating an output gene");
64 PROPERTY(constant_mutation_chance,
67 "Probability of mutating an evolvable constant");
71 CASE(MutationStrategy::FixedCount, fixed_count, FixedCountMutation);
72 CASE(MutationStrategy::Probabilistic, probabilistic, ProbabilisticMutation);
75 using IndexType = uint16_t;
79 array<IndexType, kMaxFunctionArity> connections;
81 friend void to_json(json& json_obj,
const FunctionGene& gene);
82 friend void from_json(
const json& json_obj, FunctionGene& gene);
83 friend bool operator==(
const FunctionGene& a,
const FunctionGene& b);
89 friend void to_json(json& json_obj,
const OutputGene& gene);
90 friend void from_json(
const json& json_obj, OutputGene& gene);
91 friend bool operator==(
const OutputGene& a,
const OutputGene& b);
96 explicit Genotype(
const Population* population);
98 unique_ptr<darwin::Brain> grow()
const override;
99 unique_ptr<darwin::Genotype> clone()
const override;
101 json save()
const override;
102 void load(
const json& json_obj)
override;
103 void reset()
override;
105 void createPrimordialSeed();
106 void probabilisticMutation(
const ProbabilisticMutation& config);
107 void fixedCountMutation(
const FixedCountMutation& config);
108 void inherit(
const Genotype& parent1,
const Genotype& parent2,
float preference);
110 const Population* population()
const {
return population_; }
111 const vector<FunctionGene>& functionGenes()
const {
return function_genes_; }
112 const vector<OutputGene>& outputGenes()
const {
return output_genes_; }
114 float getEvolvableConstant(
int function_id)
const;
116 friend bool operator==(
const Genotype& a,
const Genotype& b);
119 template <
class PRED>
120 void mutationHelper(PRED& predicates);
122 pair<IndexType, IndexType> connectionRange(
int layer,
int levels_back)
const;
125 const Population* population_ =
nullptr;
127 vector<FunctionGene> function_genes_;
128 vector<OutputGene> output_genes_;
129 vector<float> constants_genes_;
void reset(std::vector< T > &v)
Reset the values in a vector to 0.
Definition: ann_dynamic.h:32
Cartesian Genetic Programming (CGP)
Definition: brain.cpp:25
The foundation for data structures supporting runtime reflection.
Definition: properties.h:388
A variant type (tagged-union) with PropertySet fields.
Definition: properties.h:194
Handles types with a fixed, known set of values (enumerations for example)
Definition: stringify.h:85
const Stringify< T > * stringify()
Returns the stringifier for type T.
Definition: stringify.h:166
The interface to the population-specific "genetic material", the Genotype
Definition: darwin.h:126