1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2017 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | // Copyright (C) 1999,2000 Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL |
---|
6 | // Copyright (C) since 2001 Maciej Komosinski |
---|
7 | |
---|
8 | #ifndef _GENO_F4_H_ |
---|
9 | #define _GENO_F4_H_ |
---|
10 | |
---|
11 | #include <stdio.h> |
---|
12 | #include "f4_general.h" |
---|
13 | #include "common/nonstd.h" |
---|
14 | #include "../oper_fx.h" |
---|
15 | #include <frams/param/param.h> |
---|
16 | |
---|
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 | //@} |
---|
24 | |
---|
25 | /** @name Codes for specific F4_ADD mutation subtypes */ |
---|
26 | //@{ |
---|
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 |
---|
33 | //@} |
---|
34 | |
---|
35 | class Geno_f4 : public GenoOperators |
---|
36 | { |
---|
37 | public: |
---|
38 | Geno_f4(); |
---|
39 | void setDefaults(); |
---|
40 | |
---|
41 | int checkValidity(const char *, const char *genoname); |
---|
42 | int validate(char *&, const char *genoname); |
---|
43 | int mutate(char *& g, float & chg, int &method); |
---|
44 | int crossOver(char *&g1, char *&g2, float& chg1, float& chg2); |
---|
45 | const char* getSimplest() { return "X"; } |
---|
46 | uint32_t style(const char *g, int pos); |
---|
47 | |
---|
48 | // mutation probabilities |
---|
49 | double prob[F4_COUNT]; ///<relative probabilities of selecting mutation types in f4 genotype |
---|
50 | double probadd[F4_ADD_COUNT]; ///<relative probabilities of selecting mutation addition subtypes |
---|
51 | |
---|
52 | SString excluded_modifiers; ///<Modifiers that are excluded in mutation process |
---|
53 | static const char *all_modifiers; |
---|
54 | |
---|
55 | protected: |
---|
56 | |
---|
57 | /** |
---|
58 | * Validates a f4 genotype. If the genotype is invalid, the genotype is repaired |
---|
59 | * and the validation is repeated. Validation is performed as long as repairing |
---|
60 | * is not effective, or the number of retries exceeded the given limit. |
---|
61 | * @param geno genotype tree |
---|
62 | * @param retrycount maximum amount of repair retries |
---|
63 | * @return GENOOPER_OK if genotype is valid, GENOPER_REPAIR if genotype can be repaired, GENOPER_OPFAIL if genotype can't be repaired |
---|
64 | */ |
---|
65 | int ValidateRec(f4_node *geno, int retrycount) const; |
---|
66 | |
---|
67 | /** |
---|
68 | * Performs mutation of an f4 genotype. The mutation is performed on a random node |
---|
69 | * from a given tree. The method of mutation is chosen via the roulette selection, |
---|
70 | * where probabilities of choosing each mutation type are given in the 'prob' |
---|
71 | * array. Possible mutation types are: |
---|
72 | * - F4_ADD - adds new element to the genotype by: |
---|
73 | * - 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), |
---|
74 | * - F4_ADD_CONN - adding connection for an existing neuron, |
---|
75 | * - F4_ADD_NEUPAR - adding neuron property to the selected node, if it is a neuron node, |
---|
76 | * - F4_ADD_REP - adding a repetition node before a randomly selected node (the repetition node has 2 repetitions), |
---|
77 | * - F4_ADD_SIMP - adding a simple node before a selected node, |
---|
78 | * - F4_DEL - removes a randomly selected node (the node needs to have a parent and at least one child, otherwise returns GENOPER_OPFAIL), |
---|
79 | * - F4_MOD - modifies one of simple nodes by: |
---|
80 | * - '<' - swapping children in division |
---|
81 | * - '[' - modifying connection of a neuron |
---|
82 | * - '#' - incrementing or decrementing repetition count |
---|
83 | * |
---|
84 | * @param g input genotype; the result of mutation will be stored in this parameter |
---|
85 | * @param method reference to the variable that will get the selected method of mutation |
---|
86 | * @return GENOPER_OK if mutation was performed successfully, GENOPER_FAIL otherwise |
---|
87 | */ |
---|
88 | int MutateOne(f4_node *& g, int &method) const; |
---|
89 | |
---|
90 | /** |
---|
91 | * Creates a random connection to an existing neuron or creates an additional |
---|
92 | * sensor for a neuron. |
---|
93 | * @param nn neuron class node |
---|
94 | * @param neuid id of a neuron |
---|
95 | * @param neulist list of genotype neuron classes |
---|
96 | */ |
---|
97 | void linkNodeMakeRandom(f4_node *nn, int neuid, std::vector<NeuroClass*> neulist) const; |
---|
98 | |
---|
99 | /** |
---|
100 | * Changes connection to an existing neuron or creates an additional |
---|
101 | * sensor for neuron. |
---|
102 | * @param nn neuron connection node |
---|
103 | * @param neuid id of a neuron |
---|
104 | * @param neulist list of genotype neuron classes |
---|
105 | */ |
---|
106 | void linkNodeChangeRandom(f4_node *nn, int neuid, std::vector<NeuroClass*> neulist) const; |
---|
107 | |
---|
108 | /** |
---|
109 | * Introduces a random modification to the neuron node. |
---|
110 | * @param nn neuron node |
---|
111 | */ |
---|
112 | void nparNodeMakeRandom(f4_node *nn) const; |
---|
113 | |
---|
114 | /** |
---|
115 | * Increases or decreases the amount of repetitions in the repetition node. |
---|
116 | * @param nn repetition node |
---|
117 | */ |
---|
118 | void repeatNodeChangeRandom(f4_node *nn) const; |
---|
119 | |
---|
120 | /** |
---|
121 | * Tries to perform a mutation until success. There is a maximum of 20 tries. Returns GENOPER_OK or GENOPER_OPFAIL. |
---|
122 | * @param g genotype tree |
---|
123 | * @param method reference to the variable that will get the selected method of mutation |
---|
124 | * @return GENOPER_OK if performed successful mutation, GENOPER_FAIL otherwise |
---|
125 | */ |
---|
126 | int MutateOneValid(f4_node *&g, int &method) const; |
---|
127 | |
---|
128 | /** |
---|
129 | * Performs crossover of two creatures. The 'chg' parameter determines approximately what |
---|
130 | * percentage of the first creature should form the offspring. '1-chg' is the percentage |
---|
131 | * of the second creature in the offspring. |
---|
132 | * @param g1 first parent |
---|
133 | * @param g2 second parent |
---|
134 | * @param chg percentage of the first parent in offspring (the second parent has the rest) |
---|
135 | */ |
---|
136 | int CrossOverOne(f4_node *g1, f4_node *g2, float chg) const; |
---|
137 | }; |
---|
138 | |
---|
139 | |
---|
140 | #endif |
---|
141 | |
---|