[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[1227] | 2 | // Copyright (C) 1999-2023 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[193] | 4 | |
---|
[196] | 5 | // Copyright (C) 1999,2000 Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL |
---|
| 6 | // Copyright (C) since 2001 Maciej Komosinski |
---|
| 7 | |
---|
[779] | 8 | #ifndef _F4_OPER_H_ |
---|
| 9 | #define _F4_OPER_H_ |
---|
[193] | 10 | |
---|
[196] | 11 | #include <stdio.h> |
---|
[193] | 12 | #include "f4_general.h" |
---|
[196] | 13 | #include "common/nonstd.h" |
---|
[779] | 14 | #include "../genooperators.h" |
---|
[196] | 15 | #include <frams/param/param.h> |
---|
[193] | 16 | |
---|
[760] | 17 | /** @name Codes for general mutation types */ |
---|
| 18 | //@{ |
---|
| 19 | #define F4_ADD 0 ///<Adding new node |
---|
| 20 | #define F4_DEL 1 ///<Deletion of node |
---|
| 21 | #define F4_MOD 2 ///<Modification of node |
---|
| 22 | #define F4_COUNT 3 ///<Count of mutation types |
---|
| 23 | //@} |
---|
[193] | 24 | |
---|
[760] | 25 | /** @name Codes for specific F4_ADD mutation subtypes */ |
---|
| 26 | //@{ |
---|
[193] | 27 | #define F4_ADD_DIV 0 |
---|
| 28 | #define F4_ADD_CONN 1 |
---|
| 29 | #define F4_ADD_NEUPAR 2 |
---|
| 30 | #define F4_ADD_REP 3 |
---|
| 31 | #define F4_ADD_SIMP 4 |
---|
| 32 | #define F4_ADD_COUNT 5 |
---|
[760] | 33 | //@} |
---|
[193] | 34 | |
---|
[1227] | 35 | /** @name Codes for specific F4_MOD neuron mutation subtypes (not included in mutation_method_names, all are considered F4_MOD there) */ |
---|
| 36 | //@{ |
---|
| 37 | #define F4_MODNEU_CONN 0 |
---|
| 38 | #define F4_MODNEU_WEIGHT 1 |
---|
| 39 | #define F4_MODNEU_COUNT 2 |
---|
| 40 | //@} |
---|
| 41 | |
---|
[196] | 42 | class Geno_f4 : public GenoOperators |
---|
[193] | 43 | { |
---|
| 44 | public: |
---|
[196] | 45 | Geno_f4(); |
---|
[674] | 46 | void setDefaults(); |
---|
| 47 | |
---|
[513] | 48 | int checkValidity(const char *, const char *genoname); |
---|
| 49 | int validate(char *&, const char *genoname); |
---|
[196] | 50 | int mutate(char *& g, float & chg, int &method); |
---|
| 51 | int crossOver(char *&g1, char *&g2, float& chg1, float& chg2); |
---|
| 52 | const char* getSimplest() { return "X"; } |
---|
[247] | 53 | uint32_t style(const char *g, int pos); |
---|
[193] | 54 | |
---|
[196] | 55 | // mutation probabilities |
---|
[777] | 56 | double prob[F4_COUNT]; ///<relative probabilities of selecting mutation types in f4 genotype |
---|
[1227] | 57 | double probadd[F4_ADD_COUNT]; ///<relative probabilities of selecting addition mutation subtypes |
---|
| 58 | double probmodneu[F4_MODNEU_COUNT]; ///<relative probabilities of selecting neuron mutation subtypes |
---|
| 59 | paInt mut_max_rep; ///maximum allowed number of repetitions for the '#' repetition gene |
---|
[193] | 60 | |
---|
[760] | 61 | SString excluded_modifiers; ///<Modifiers that are excluded in mutation process |
---|
[674] | 62 | static const char *all_modifiers; |
---|
| 63 | |
---|
[193] | 64 | protected: |
---|
[760] | 65 | |
---|
| 66 | /** |
---|
| 67 | * Validates a f4 genotype. If the genotype is invalid, the genotype is repaired |
---|
| 68 | * and the validation is repeated. Validation is performed as long as repairing |
---|
| 69 | * is not effective, or the number of retries exceeded the given limit. |
---|
| 70 | * @param geno genotype tree |
---|
| 71 | * @param retrycount maximum amount of repair retries |
---|
| 72 | * @return GENOOPER_OK if genotype is valid, GENOPER_REPAIR if genotype can be repaired, GENOPER_OPFAIL if genotype can't be repaired |
---|
| 73 | */ |
---|
[1227] | 74 | int ValidateRec(f4_Node *geno, int retrycount) const; |
---|
[760] | 75 | |
---|
| 76 | /** |
---|
| 77 | * Performs mutation of an f4 genotype. The mutation is performed on a random node |
---|
| 78 | * from a given tree. The method of mutation is chosen via the roulette selection, |
---|
| 79 | * where probabilities of choosing each mutation type are given in the 'prob' |
---|
| 80 | * array. Possible mutation types are: |
---|
| 81 | * - F4_ADD - adds new element to the genotype by: |
---|
| 82 | * - F4_ADD_DIV - replacing randomly selected node with division node '<', setting this node as a child and creating new stick or neuron sibling of the selected cell (the neuron-type sibling will be connected to a random existing neuron), |
---|
| 83 | * - F4_ADD_CONN - adding connection for an existing neuron, |
---|
| 84 | * - F4_ADD_NEUPAR - adding neuron property to the selected node, if it is a neuron node, |
---|
| 85 | * - F4_ADD_REP - adding a repetition node before a randomly selected node (the repetition node has 2 repetitions), |
---|
| 86 | * - F4_ADD_SIMP - adding a simple node before a selected node, |
---|
| 87 | * - F4_DEL - removes a randomly selected node (the node needs to have a parent and at least one child, otherwise returns GENOPER_OPFAIL), |
---|
| 88 | * - F4_MOD - modifies one of simple nodes by: |
---|
| 89 | * - '<' - swapping children in division |
---|
| 90 | * - '[' - modifying connection of a neuron |
---|
| 91 | * - '#' - incrementing or decrementing repetition count |
---|
| 92 | * |
---|
| 93 | * @param g input genotype; the result of mutation will be stored in this parameter |
---|
| 94 | * @param method reference to the variable that will get the selected method of mutation |
---|
| 95 | * @return GENOPER_OK if mutation was performed successfully, GENOPER_FAIL otherwise |
---|
| 96 | */ |
---|
[1227] | 97 | int MutateOne(f4_Node *& g, int &method) const; |
---|
[760] | 98 | |
---|
| 99 | /** |
---|
[1227] | 100 | * Finds all neurons in g (in the order of ordNode()) and returns their neuroclasses in a vector. |
---|
| 101 | * Additionally, looks for the needle_neuron node and returns its index (in the list of the returned vector) as found_index, |
---|
| 102 | * or -1 if not found (for example, it was not a neuroclass node or not added to the "g" tree). |
---|
| 103 | * @param g root node |
---|
| 104 | * @param needle_neuron neuroclass node to look for in all nodes |
---|
| 105 | * @param found_index returned index of needle |
---|
| 106 | * @return all nodes that are neurons |
---|
[760] | 107 | */ |
---|
[1227] | 108 | static vector<NeuroClass*> findAllNeuronsAndNode(f4_Node * const & g, f4_Node* const &needle_neuron, int &found_index); |
---|
[760] | 109 | |
---|
| 110 | /** |
---|
[1227] | 111 | * Finds indexes of a given neuron and another random (output- or input-providing) neuron in the list of all neurons present in the "g" tree. |
---|
| 112 | * @param g root node |
---|
| 113 | * @param neuron neuroclass node to look for in all nodes in g |
---|
| 114 | * @param other_has_output if true, other neuron will provide output; otherwise, it will accept input(s) |
---|
| 115 | * @param neuron_index returned index of neuron |
---|
| 116 | * @param other_neuron_index returned index of a random neuron that provides an output or accepts inputs |
---|
| 117 | * @return true if succeeded, false otherwise |
---|
[760] | 118 | */ |
---|
[1227] | 119 | static bool findConnectionNeuronIndexes(f4_Node * const &g, f4_Node *neuron, bool other_has_output, int &neuron_index, int &other_neuron_index); |
---|
[760] | 120 | |
---|
| 121 | /** |
---|
[1227] | 122 | * Creates a random connection to an existing neuron and randomizes connection weight |
---|
| 123 | * sensor for a neuron. |
---|
| 124 | * @param nn neuron class node |
---|
| 125 | * @param nn_index index of the nn neuron |
---|
| 126 | * @param other_index index of the neuron providing output, to get input from |
---|
| 127 | */ |
---|
| 128 | void connectionNodeChangeRandom(f4_Node *nn, int nn_index, int other_index) const; |
---|
| 129 | |
---|
| 130 | /** |
---|
[760] | 131 | * Introduces a random modification to the neuron node. |
---|
| 132 | * @param nn neuron node |
---|
| 133 | */ |
---|
[1227] | 134 | void nparNodeMakeRandom(f4_Node *nn) const; |
---|
[760] | 135 | |
---|
| 136 | /** |
---|
| 137 | * Increases or decreases the amount of repetitions in the repetition node. |
---|
| 138 | * @param nn repetition node |
---|
| 139 | */ |
---|
[1227] | 140 | void repeatNodeChangeRandom(f4_Node *nn) const; |
---|
[760] | 141 | |
---|
| 142 | /** |
---|
| 143 | * Tries to perform a mutation until success. There is a maximum of 20 tries. Returns GENOPER_OK or GENOPER_OPFAIL. |
---|
| 144 | * @param g genotype tree |
---|
| 145 | * @param method reference to the variable that will get the selected method of mutation |
---|
| 146 | * @return GENOPER_OK if performed successful mutation, GENOPER_FAIL otherwise |
---|
| 147 | */ |
---|
[1227] | 148 | int MutateOneValid(f4_Node *&g, int &method) const; |
---|
[760] | 149 | |
---|
| 150 | /** |
---|
| 151 | * Performs crossover of two creatures. The 'chg' parameter determines approximately what |
---|
| 152 | * percentage of the first creature should form the offspring. '1-chg' is the percentage |
---|
| 153 | * of the second creature in the offspring. |
---|
| 154 | * @param g1 first parent |
---|
| 155 | * @param g2 second parent |
---|
| 156 | * @param chg percentage of the first parent in offspring (the second parent has the rest) |
---|
| 157 | */ |
---|
[1227] | 158 | int CrossOverOne(f4_Node *g1, f4_Node *g2, float chg) const; |
---|
[193] | 159 | }; |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | #endif |
---|
| 163 | |
---|