source: cpp/frams/genetics/f4/f4_oper.cpp @ 1232

Last change on this file since 1232 was 1232, checked in by Maciej Komosinski, 12 months ago

The validate() function may now attempt to repair genotypes without syntax errors (i.e., fully parsed into a correct f4_Node tree), but with semantic errors

  • Property svn:eol-style set to native
File size: 27.4 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2023  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// 2018, Grzegorz Latosinski, added support for new API for neuron types and their properties
8
9
10// This representation has a tendency to bloat - adding a small penalty to fitness such as "this.velocity - 0.000000001*String.len(this.genotype);"
11// may help, but it would be better to improve the source code to make genetic operators neutral in terms of genotype length. Adding such a penalty
12// removes "work in progress" changes in genotypes thus promoting immediate, straightforward improvements while hindering slower, multifaceted progress.
13// TODO getting rid of redundancy (valid genotypes with a lot of "junk code") in this representation looks like a good idea; many improvements to this end have already been done in April & May 2023.
14//
15//
16// TODO the behavior of neuron input indexes during mutation seems badly implemented (see also TREAT_BAD_CONNECTIONS_AS_INVALID_GENO). Are they kept properly maintained when nodes are added and removed? This could be done well because during mutation we operate on the tree structure with cross-references between nodes (so they should not be affected by local changes in the tree), and then convert the tree back to string. Yet, the f4_Node.conn_from is an integer and these fields in nodes do not seem to be maintained on tree node adding/removal... change these integer offsets to references to node objects? But actually, do the offsets that constitute relative connection references concern the f4_Node tree structure (and all these sophisticated calculations of offsets during mutation are useful) or rather they concern the f4_Cells development? verify all situations in f4_Cell::oneStep(), case '['.
17// TODO add simplifying sequences of modifiers (so capital and small letter cancel out, like in f1) - but seems like each single modifier is a separate f4_Node? and perhaps we don't want to use the repair mechanism for this... maybe mutations, when they add/modify/remove a modifier node, should be "cleaning" the tree by removing nodes when they encounter contradictory modifiers on the same subpath, and also limit the number of modifiers of each type just like in f1? To avoid sequences like ...<X>llmlIilImmimiimmimifmfl<fifmmimilimmmiimiliffmfliIfififlliflimfliffififmiffmfliflifmIlimimiflimfiffmllliflmimifllifliliflifmIlimimiflimfiffmllliflmimifllfmIlimimiflimfiffmllliflmimiflliflimimmiflimfliffmiflifmfiffllIlififliffififmiffmfliflifIliflimimflimflfflimimifllfflifllfflimlififfiiffifIr<r<...
18// TODO in mutation, adding the '#' gene does not seem to be effective. The gene is added and genotypes are valid, but hardly ever #n is effective, i.e., it hardly ever multiplicates body or brain parts... investigate!
19// TODO add support for properties of (any class of) neurons - not just sigmoid/force/intertia (':' syntax) for N
20// TODO add mapping genotype character ranges for neural [connections]
21// TODO change the default branching plane (to match f1) so they do not grow perfectly vertical (cheating vertpos) so easily? (so they require Rr or other modifiers)
22
23
24#include "f4_oper.h"
25#include <frams/util/sstring.h>
26#include <common/log.h>
27
28#include <stdio.h>
29#include <stdlib.h>
30#include "common/nonstd_math.h"
31#include <string.h>
32
33
34const char *Geno_f4::all_modifiers = F14_MODIFIERS ","; //comma in f4 is handled the same way (simple node, F4_ADD_SIMP) as modifiers
35
36// codes that can be changed (apart from being added/deleted)
37#define F4_MUT_CHANGE_CODES "<[#"
38
39#define FIELDSTRUCT Geno_f4
40
41static ParamEntry geno_f4_paramtab[] =
42{
43        { "Genetics: f4", 1, F4_COUNT + F4_ADD_COUNT + F4_MODNEU_COUNT + 2, },
44        { "f4_mut_add", 0, 0, "Add node", "f 0 100 50", FIELD(prob[F4_ADD]), "Mutation: probability of adding a node", },
45        { "f4_mut_add_div", 0, 0, "- add division", "f 0 100 20", FIELD(probadd[F4_ADD_DIV]), "Add node mutation: probability of adding a division", },
46        { "f4_mut_add_conn", 0, 0, "- add connection", "f 0 100 15", FIELD(probadd[F4_ADD_CONN]), "Add node mutation: probability of adding a neural connection", },
47        { "f4_mut_add_neupar", 0, 0, "- add neuron property", "f 0 100 5", FIELD(probadd[F4_ADD_NEUPAR]), "Add node mutation: probability of adding a neuron property/modifier", },
48        { "f4_mut_add_rep", 0, 0, "- add repetition '#'", "f 0 100 10", FIELD(probadd[F4_ADD_REP]), "Add node mutation: probability of adding the '#' repetition gene", },
49        { "f4_mut_add_simp", 0, 0, "- add simple node", "f 0 100 50", FIELD(probadd[F4_ADD_SIMP]), "Add node mutation: probability of adding a random, simple gene", },
50
51        { "f4_mut_del", 0, 0, "Delete node", "f 0 100 20", FIELD(prob[F4_DEL]), "Mutation: probability of deleting a node", },
52
53        { "f4_mut_mod", 0, 0, "Modify node", "f 0 100 30", FIELD(prob[F4_MOD]), "Mutation: probability of changing a node", },
54        { "f4_mut_modneu_conn", 0, 0, "- neuron input: modify source", "f 0 100 60", FIELD(probmodneu[F4_MODNEU_CONN]), "Neuron input mutation: probability of changing its source neuron", },
55        { "f4_mut_modneu_weight", 0, 0, "- neuron input: modify weight", "f 0 100 40", FIELD(probmodneu[F4_MODNEU_WEIGHT]), "Neuron input mutation: probability of changing its weight", },
56
57        { "f4_mut_max_rep", 1, 0, "Maximum number for '#' repetitions", "d 2 20 6", FIELD(mut_max_rep), "Maximum allowed number of repetitions for the '#' repetition gene", },
58        { "f4_mut_exmod", 1, 0, "Excluded modifiers", "s 0 30", FIELD(excluded_modifiers), "Modifiers that will not be added nor deleted during mutation\n(all: " F14_MODIFIERS ")", },
59        { 0, },
60};
61
62#undef FIELDSTRUCT
63
64
65Geno_f4::Geno_f4()
66{
67        supported_format = '4';
68        par.setParamTab(geno_f4_paramtab);
69        par.select(this);
70        par.setDefault();
71
72        mutation_method_names = new const char*[F4_COUNT + F4_ADD_COUNT - 1];
73        int index = 0;
74        mutation_method_names[index++] = "added division";
75        mutation_method_names[index++] = "added neural connection";
76        mutation_method_names[index++] = "added neuron property";
77        mutation_method_names[index++] = "added repetition gene";
78        mutation_method_names[index++] = "added a simple node";
79        mutation_method_names[index++] = "deleted a node";
80        mutation_method_names[index++] = "modified a node";
81        if (index != F4_COUNT + F4_ADD_COUNT - 1) logMessage("Geno_f4", "Constructor", LOG_CRITICAL, "Mutation names init error");
82}
83
84void Geno_f4::setDefaults()
85{
86        excluded_modifiers = F14_MODIFIERS_RARE F14_MODIFIERS_VISUAL;
87}
88
89int Geno_f4::ValidateRecur(f4_Node *geno, int retrycount) const
90{
91        // ! the genotype is geno->child (not geno) !
92        // build from it with repair on
93
94        f4_Cells cells(geno->child, true);
95        cells.simulate();  //we should simulate?!
96
97        // errors not fixed:
98        if (cells.getErrorCode() == GENOPER_OPFAIL)
99        {
100                if (cells.getErrorPos() >= 0) return 1 + cells.getErrorPos();
101                return GENOPER_OPFAIL;
102        }
103        // errors can be fixed
104        if (cells.getErrorCode() == GENOPER_REPAIR)
105        {
106                cells.repairGeno(geno, 1);
107                // note: geno might have been fixed
108                // check again
109                int res2 = GENOPER_OK;
110                if (retrycount > 0)
111                        res2 = ValidateRecur(geno, retrycount - 1);
112
113                if (res2 == GENOPER_OK) return GENOPER_REPAIR;
114                return res2;
115        }
116        // no errors:
117        return GENOPER_OK;
118}
119
120
121int Geno_f4::validate(char *& geno, const char *genoname)
122{
123        // convert geno to a tree, then try to validate
124        f4_Node root;
125        int res = f4_process(geno, &root);
126        if (root.childCount() != 1) return GENOPER_OK; // the resulting tree will not be repairable (fatal flaw; root must have exactly one child) - do not even attempt repair
127
128        // here we have a genotype with root.childCount()==1 (meaning some part was successfully parsed into a tree) and either res==0 (syntax was correct, semantics we don't know) or res>0 (for sure has some error)
129        const int VALIDATE_TRIALS = 20;
130        res = ValidateRecur(&root, VALIDATE_TRIALS);
131        if (res != GENOPER_OPFAIL) // if repaired (GENOPER_REPAIR) or had no errors (GENOPER_OK, e.g. the genotype had some errors that were ignored during tree creation or had junk genes appended at the end, so the tree was OK but the genotype was not),
132        {
133                geno[0] = 0;
134                root.child->sprintAdj(geno); //make it back to string
135        }
136        return GENOPER_OK;
137}
138
139
140int Geno_f4::checkValidity(const char* geno, const char *genoname)
141{
142        f4_Node root;
143        int res = f4_process(geno, &root);
144        if (res) return res;  // errorpos, >0
145        if (root.childCount() != 1) return 1; // fatal flaw; root must have exactly one child
146        f4_Cells cells(root.child, false);
147        cells.simulate();
148        if (cells.getErrorCode() == GENOPER_OPFAIL || cells.getErrorCode() == GENOPER_REPAIR)
149        {
150                if (cells.getErrorPos() >= 0) return 1 + cells.getErrorPos();
151                else return 1; //error, no known position
152        }
153        else return GENOPER_OK;
154}
155
156
157int Geno_f4::MutateOne(f4_Node *& g, int &method) const
158{
159        // ! the genotype is g->child (not g) !
160
161        // do the mutation
162        // pick a random node
163        f4_Node *node_mutated = g->child->randomNode();
164        //DB( printf("%c\n", node_mutated->name); )
165
166        switch (roulette(prob, F4_COUNT))
167        {
168        case F4_ADD:
169        {
170                // add a node
171                switch (method = roulette(probadd, F4_ADD_COUNT))
172                {
173                case F4_ADD_DIV:
174                {
175                        // add division ('<')
176                        f4_Node *node_mutated_parent = node_mutated->parent;
177                        node_mutated_parent->removeChild(node_mutated);
178                        f4_Node *node_new_div = new f4_Node('<', node_mutated_parent, node_mutated_parent->pos);
179                        node_new_div->addChild(node_mutated);
180                        // new cell is stick or neuron
181                        // "X>" or "N>"
182                        constexpr double STICK_OR_NEURON = 0.5; // hardcoded probability... could be parametrized, but in a general case (unknown fitness goal) 0.5 makes sense?
183                        f4_Node *node_new = NULL; //stick or neuron or neural connection
184                        if (rndDouble(1) < STICK_OR_NEURON)
185                                node_new = new f4_Node('X', node_new_div, node_new_div->pos);
186                        else
187                        {
188                                // make neuron
189                                NeuroClass *rndclass = GenoOperators::getRandomNeuroClass(Model::SHAPETYPE_BALL_AND_STICK);
190                                if (rndclass == NULL) //no active neurons?
191                                {
192                                        node_new = new f4_Node('X', node_new_div, node_new_div->pos);
193                                }
194                                else
195                                {
196                                        f4_Node *node_new_neuron = new f4_Node(rndclass->getName().c_str(), node_new_div, node_new_div->pos);
197                                        node_new_neuron->neuclass = rndclass;
198                                        node_new = node_new_neuron; //can be changed below if all goes well and we add a new connection too
199                                        if (probadd[F4_ADD_CONN] > 0) //user wants to add connections
200                                        {
201                                                if (rndclass->getPreferredInputs() != 0) //neuron also wants connections?
202                                                {
203                                                        int node_new_neuron_index, other_neuron_index;
204                                                        bool ok = findConnectionNeuronIndexes(g, node_new_neuron, true, node_new_neuron_index, other_neuron_index); //node_new_neuron_index==-1 should never happen, we just added node_new_neuron we are looking for
205                                                        if (ok) //we can create a new connection
206                                                        {
207                                                                node_new = new f4_Node('[', node_new_neuron, node_new_div->pos);
208                                                                connectionNodeChangeRandom(node_new, node_new_neuron_index, other_neuron_index);
209                                                        }
210                                                }
211                                                else if (rndclass->getPreferredOutput() > 0) //neuron also wants connections?
212                                                {
213                                                        // Not so easy: we would need to add a '[' node as a child not of node_new_neuron, but of other neuron that would get an input from node_new_neuron (and need to properly calculate relative connection reference).
214                                                        // The "false" argument in findConnectionNeuronIndexes() below is not suffient, because we also need to access (find) the f4_Node of the other neuron.
215                                                        // A similar logic is implemented in F4_ADD_CONN below, but let's not complicate this F4_ADD_DIV mutation anymore.
216                                                        // The disadvantage is that the node_new_neuron added here which is a neuron that provides output (e.g., a receptor, N, etc.) will not get connected immediately here even when there are already existing neurons wanting inputs (e.g., muscles, N, etc.).
217                                                        //bool ok = findConnectionNeuronIndexes(g, ... , false, ..., ...);
218                                                }
219                                        }
220                                }
221                        }
222                        new f4_Node('>', node_new, node_new->pos); //adds to node_new
223                        node_mutated->parent = node_new_div;
224                        // now, swap children with 50% chance
225                        if (rndUint(2) == 0)
226                        {
227                                node_mutated_parent = node_new_div->child;
228                                node_new_div->child = node_new_div->child2;
229                                node_new_div->child2 = node_mutated_parent;
230                        }
231                }
232                break;
233                case F4_ADD_CONN:
234                {
235                        // add connection
236
237                        // the probability that a randomly selected node will be a neuron and additionally this neuron will accept inputs is low,
238                        // so we disregard randomly picked node_mutated and build a list of all valid candidate nodes here, then randomly select one from them.
239
240                        vector<f4_Node*> candidate_nodes; //neurons that accept input(s)
241                        for (int i = 0; i < g->count(); i++)
242                        {
243                                f4_Node *node = g->ordNode(i);
244                                f4_Node *node_parent = node->parent;
245                                if (node_parent == NULL || node_parent->neuclass == NULL) continue;
246                                int prefinputs = node_parent->neuclass->getPreferredInputs();
247                                if (prefinputs == -1 ||
248                                        prefinputs > 0) //would be nice if we could easily and quickly check if the parent already has its preferred inputs used, so that we do not produce an invalid mutation here... it is possible through the f4_Cell.n_conns field, but only during organism development
249                                        candidate_nodes.push_back(node);
250                        }
251
252                        if (candidate_nodes.size() == 0)
253                                return GENOPER_OPFAIL;
254
255                        node_mutated = candidate_nodes[rndUint((unsigned int)candidate_nodes.size())];
256                        f4_Node *node_mutated_parent = node_mutated->parent;
257
258                        int node_mutated_parent_index, other_neuron_index;
259                        bool ok = findConnectionNeuronIndexes(g, node_mutated_parent, true, node_mutated_parent_index, other_neuron_index); //node_mutated_parent_index==-1 should never happen, we earlier selected the neuron we are now looking for
260                        if (!ok)
261                                return GENOPER_OPFAIL;
262
263                        node_mutated->parent->removeChild(node_mutated); //this subtree will be reconnected below, as a child to node_new_conn
264                        f4_Node *node_new_conn = new f4_Node('[', node_mutated->parent, node_mutated->parent->pos);
265                        node_new_conn->addChild(node_mutated);
266                        node_mutated->parent = node_new_conn; // node_mutated_parent is the neuron, node_mutated->parent is '['
267                        connectionNodeChangeRandom(node_new_conn, node_mutated_parent_index, other_neuron_index);
268                }
269                break;
270                case F4_ADD_NEUPAR:
271                {
272                        // add neuron modifier
273                        node_mutated->parent->removeChild(node_mutated);
274                        f4_Node *n2 = new f4_Node(':', node_mutated->parent, node_mutated->parent->pos);
275                        nparNodeMakeRandom(n2);
276                        n2->addChild(node_mutated);
277                        node_mutated->parent = n2;
278                }
279                break;
280                case F4_ADD_REP:
281                {
282                        // add repetition ('#')
283                        // repeated code (left child) is the original, right child is empty, count is set to 2
284                        f4_Node *n3 = node_mutated->parent;
285                        n3->removeChild(node_mutated);
286                        f4_Node *n2 = new f4_Node('#', n3, n3->pos);
287                        n2->reps = 2;
288                        n2->addChild(node_mutated);
289                        new f4_Node('>', n2, n2->pos);
290                        node_mutated->parent = n2;
291                }
292                break;
293                case F4_ADD_SIMP:
294                {
295                        // add simple node
296                        // choose a simple node from ADD_SIMPLE_CODES
297                        node_mutated->parent->removeChild(node_mutated);
298                        //f4_Node *n2 = new f4_Node(ADD_SIMPLE_CODES[rndUint(strlen(ADD_SIMPLE_CODES))], n1->parent, n1->parent->pos);
299                        int modifierid = GenoOperators::getRandomChar(all_modifiers, excluded_modifiers.c_str());
300                        f4_Node *n2 = new f4_Node(all_modifiers[modifierid], node_mutated->parent, node_mutated->parent->pos);
301                        n2->addChild(node_mutated);
302                        node_mutated->parent = n2;
303                }
304                break;
305                }
306        }
307        break;
308
309        case F4_DEL:
310        {
311                method = F4_ADD_COUNT - 1 + F4_DEL;
312                // delete a node
313                // must pick a node with parent, and at least one child
314                // already picked a node, but repeat may be needed
315                for (int i = 0; i < 10; i++)
316                {
317                        if ((node_mutated->parent != NULL) && (g != node_mutated->parent))
318                                if (node_mutated->child != NULL)
319                                        break;
320                        // try a new one
321                        node_mutated = g->child->randomNode();
322                }
323                if ((node_mutated->parent != NULL) && (g != node_mutated->parent))
324                {
325                        switch (node_mutated->childCount())
326                        {
327                        case 0: break;
328                        case 1:  // one child
329                        {
330                                f4_Node *node_mutated_parent = node_mutated->parent;
331                                node_mutated_parent->removeChild(node_mutated);
332                                if (node_mutated->child != NULL)
333                                {
334                                        node_mutated->child->parent = node_mutated_parent;
335                                        node_mutated_parent->addChild(node_mutated->child);
336                                        node_mutated->child = NULL;
337                                }
338                                if (node_mutated->child2 != NULL)
339                                {
340                                        node_mutated->child2->parent = node_mutated_parent;
341                                        node_mutated_parent->addChild(node_mutated->child2);
342                                        node_mutated->child2 = NULL;
343                                }
344                                // destroy n1
345                                node_mutated->parent = NULL;
346                                delete node_mutated;
347                        }
348                        break;
349
350                        case 2:  // two children
351                        {
352                                // two children
353                                f4_Node *n2 = node_mutated->parent;
354                                n2->removeChild(node_mutated);
355                                // n1 has two children. pick one randomly 50-50, destroy other
356                                if (rndUint(2) == 0)
357                                {
358                                        node_mutated->child->parent = n2;
359                                        n2->addChild(node_mutated->child);
360                                        node_mutated->child = NULL;
361                                        node_mutated->child2->parent = NULL;
362                                }
363                                else
364                                {
365                                        node_mutated->child2->parent = n2;
366                                        n2->addChild(node_mutated->child2);
367                                        node_mutated->child2 = NULL;
368                                        node_mutated->child->parent = NULL;
369                                }
370                                // destroy n1
371                                node_mutated->parent = NULL;
372                                delete node_mutated;
373                        }
374                        break;
375                        }
376                }
377                else return GENOPER_OPFAIL;
378        }
379        break;
380        case F4_MOD:
381        {
382                method = F4_ADD_COUNT - 1 + F4_MOD;
383                // change a node
384                // the only nodes that are modifiable are F4_MUT_CHANGE_CODES
385                // try to get a modifiable node
386                // already picked a node, but repeat may be needed
387                int i = 0;
388                while (1)
389                {
390                        if (strchr(F4_MUT_CHANGE_CODES, node_mutated->name[0])) break;
391                        // try a new one
392                        node_mutated = g->child->randomNode();
393                        i++;
394                        if (i >= 20) return GENOPER_OPFAIL;
395                }
396                switch (node_mutated->name[0])
397                {
398                case '<':
399                {
400                        // swap children
401                        f4_Node *n2 = node_mutated->child;
402                        node_mutated->child = node_mutated->child2;
403                        node_mutated->child2 = n2;
404                }
405                break;
406                case '[':
407                {
408                        switch (roulette(probmodneu, F4_MODNEU_COUNT))
409                        {
410                        case F4_MODNEU_CONN:
411                        {
412                                f4_Node *neuron = node_mutated; //we start in '[' node and follow up parents until we find the neuron with these connections
413                                while (neuron != NULL && neuron->neuclass == NULL) neuron = neuron->parent;
414                                if (neuron == NULL)
415                                        return GENOPER_OPFAIL; //did not find a neuron on the way up tree
416
417
418                                int neuron_index, other_neuron_index;
419                                bool ok = findConnectionNeuronIndexes(g, neuron, true, neuron_index, other_neuron_index); //neuron_index==-1 should never happen, we know the neuron is in the tree
420                                if (!ok)
421                                        return GENOPER_OPFAIL;
422
423                                connectionNodeChangeRandom(node_mutated, neuron_index, other_neuron_index);
424                                break;
425                        }
426                        case F4_MODNEU_WEIGHT:
427                                node_mutated->conn_weight = GenoOperators::getMutatedNeuronConnectionWeight(node_mutated->conn_weight);
428                                break;
429                        }
430                }
431                break;
432
433                case '#':
434                {
435                        repeatNodeChangeRandom(node_mutated);
436                }
437                break;
438                }
439        }
440        break;
441
442        default: //no mutations allowed?
443                return GENOPER_OPFAIL;
444        }
445        return GENOPER_OK;
446}
447
448// find all neurons and the needle
449vector<NeuroClass*> Geno_f4::findAllNeuronsAndNode(f4_Node * const & g, f4_Node* const &needle_neuron, int &found_index)
450{
451        found_index = -1; // not found (for example, needle_neuron is not a neuroclass node or not added to the "g" tree)
452        vector<NeuroClass*> neulist;
453        for (int i = 0; i < g->count(); i++)
454        {
455                f4_Node *node = g->ordNode(i);
456                if (node->neuclass != NULL)
457                {
458                        neulist.push_back(node->neuclass);
459                        if (node == needle_neuron)
460                                found_index = int(neulist.size()) - 1;
461                }
462        }
463        return neulist;
464}
465
466bool Geno_f4::findConnectionNeuronIndexes(f4_Node * const &g, f4_Node *neuron, bool other_has_output, int &neuron_index, int &other_neuron_index)
467{
468        vector<NeuroClass*> neulist = findAllNeuronsAndNode(g, neuron, neuron_index);
469        if (neuron_index == -1)
470                return false;
471
472        other_neuron_index = other_has_output ?
473                GenoOperators::getRandomNeuroClassWithOutput(neulist) //find an existing neuron that provides an output
474                :
475                GenoOperators::getRandomNeuroClassWithInput(neulist); //find an existing neuron that accepts input(s)
476        return other_neuron_index >= 0;
477}
478
479// change a [ node
480void Geno_f4::connectionNodeChangeRandom(f4_Node *nn, int nn_index, int other_index) const
481{
482        // relative input connection to some existing neuron
483        nn->conn_from = nn_index - other_index;
484        //nn->conn_from = (int)(4.0f * (rndDouble(1) - 0.5)); //in very old times - did not care about neuron input/output preferences
485
486        nn->conn_weight = GenoOperators::getMutatedNeuronConnectionWeight(nn->conn_weight);
487}
488
489
490// make a random : node
491void Geno_f4::nparNodeMakeRandom(f4_Node *nn) const
492{
493        unsigned int prop = rndUint(3); //random neuron property
494        nn->prop_symbol = "!=/"[prop];
495        nn->prop_increase = rndUint(2) == 1;
496}
497
498// change a repeat # node
499void Geno_f4::repeatNodeChangeRandom(f4_Node *nn) const
500{
501        if (rndDouble(1) < 0.5) nn->reps++; else nn->reps--; // change count
502        if (nn->reps < 1) nn->reps = 1;
503        if (nn->reps > mut_max_rep) nn->reps = mut_max_rep;
504}
505
506
507int Geno_f4::MutateOneValid(f4_Node *& g, int &method) const
508// mutate one, until a valid genotype is obtained
509{
510        // ! the genotype is g->child (not g) !
511        int i, res;
512        f4_Node *gcopy = NULL;
513        const int TRY_MUTATE = 20;
514        // try this at most TRY_MUTATE times: copy, mutate, then validate
515        for (i = 0; i < TRY_MUTATE; i++)
516        {
517                gcopy = g->duplicate();
518
519                res = MutateOne(gcopy, method);
520
521                if (GENOPER_OK != res)
522                {
523                        // mutation failed, try again
524                        delete gcopy;
525                        continue;  // for
526                }
527                // try to validate it
528                res = ValidateRecur(gcopy, 10);
529                // accept if it is OK, or was repaired
530                if (GENOPER_OK == res)
531                        //(GENOPER_REPAIR == res)
532                {
533                        // destroy the original one
534                        g->destroy();
535                        // make it the new one
536                        *g = *gcopy;
537                        gcopy->child = NULL;
538                        gcopy->child2 = NULL;
539                        delete gcopy;
540                        res = GENOPER_OK;
541                        goto retm1v;
542                }
543                delete gcopy;
544        }
545        // attempts failed
546        res = GENOPER_OPFAIL;
547retm1v:
548        return res;
549}
550
551
552int Geno_f4::mutate(char *& g, float & chg, int &method)
553{
554        f4_Node *root = new f4_Node;
555        if (f4_process(g, root) || root->childCount() != 1)
556        {
557                delete root;
558                return GENOPER_OPFAIL;
559        } // could not convert or bad: fail
560        // mutate one node, set chg as this percent
561        chg = 1.0 / float(root->child->count());
562        if (MutateOneValid(root, method) != GENOPER_OK)
563        {
564                delete root;
565                return GENOPER_OPFAIL;
566        }
567        // OK, convert back to string
568        g[0] = 0;
569        root->child->sprintAdj(g);
570        delete root;
571        return GENOPER_OK;
572}
573
574
575/*
576int Geno_f4::MutateMany(char *& g, float & chg)
577// check if original is valid, then
578// make a number of mutations
579{
580int res, n, i;
581int totNodes = 0;
582int maxToMut = 0;
583
584// convert to tree
585f4_Node *root;
586root = new f4_Node();
587res = f4_processrec(g, 0, root);
588if (res) {
589// could not convert, fail
590goto retm;
591}
592if (1 != root->childCount()) {
593res = GENOPER_OPFAIL;
594goto retm;
595}
596
597// check if original is valid
598res = ValidateRec( root, 20 );
599// might have been repaired!
600if (GENOPER_REPAIR==res) {
601res = GENOPER_OK;
602}
603if (GENOPER_OK != res) {
604goto retm;
605}
606
607// decide number of nodes to mutate
608// decide maximum number of nodes to mutate: 0.25*nodes, min 2
609totNodes = root->child->count();
610maxToMut = (int)( 0.25f * totNodes);
611if (maxToMut<2) maxToMut=2;
612if (maxToMut>totNodes) maxToMut=totNodes;
613
614// decide number of nodes to mutate
615n = (int)( 0.5 + rndDouble(1) * maxToMut );
616if (n<1) n=1;
617if (n>totNodes) n=totNodes;
618// set chg as this percent
619chg = ((float)n) / ((float)totNodes);
620for (i=0; i<n; i++)
621{
622res = MutateOneValid(root);
623if (GENOPER_OK != res)
624{
625res = GENOPER_OPFAIL;
626goto retm;
627}
628}
629// OK, convert back to string
630g[0]=0;
631root->child->sprintAdj(g);
632retm:
633delete root;
634return res;
635}
636*/
637
638
639int Geno_f4::CrossOverOne(f4_Node *g1, f4_Node *g2, float chg) const
640{
641        // ! the genotypes are g1->child and g2->child (not g1 g2) !
642        // single offspring in g1
643        int smin, smax;
644        float size;
645        f4_Node *n1, *n2, *n1p, *n2p;
646
647        // determine desired size
648        size = (1 - chg) * (float)g1->count();
649        smin = (int)(size * 0.9f - 1);
650        smax = (int)(size * 1.1f + 1);
651        // get a random node with desired size
652        n1 = g1->child->randomNodeWithSize(smin, smax);
653
654        // determine desired size
655        size = (1 - chg) * (float)g2->count();
656        smin = (int)(size * 0.9f - 1);
657        smax = (int)(size * 1.1f + 1);
658        // get a random node with desired size
659        n2 = g2->child->randomNodeWithSize(smin, smax);
660
661        // exchange the two nodes:
662        n1p = n1->parent;
663        n2p = n2->parent;
664        n1p->removeChild(n1);
665        n1p->addChild(n2);
666        n2p->removeChild(n2);
667        n2p->addChild(n1);
668        n1->parent = n2p;
669        n2->parent = n1p;
670
671        return GENOPER_OK;
672}
673
674int Geno_f4::crossOver(char *&g1, char *&g2, float &chg1, float &chg2)
675{
676        f4_Node root1, root2, *copy1, *copy2;
677
678        // convert genotype strings into tree structures
679        if (f4_process(g1, &root1) || (root1.childCount() != 1)) return GENOPER_OPFAIL;
680        if (f4_process(g2, &root2) || (root2.childCount() != 1)) return GENOPER_OPFAIL;
681
682        // decide amounts of crossover, 0.1-0.9
683        chg1 = 0.1 + rndDouble(0.8);
684        chg2 = 0.1 + rndDouble(0.8);
685
686        copy1 = root1.duplicate();
687        if (CrossOverOne(copy1, &root2, chg1) != GENOPER_OK) { delete copy1; copy1 = NULL; }
688        copy2 = root2.duplicate();
689        if (CrossOverOne(copy2, &root1, chg2) != GENOPER_OK) { delete copy2; copy2 = NULL; }
690
691        g1[0] = 0;
692        g2[0] = 0;
693        if (copy1) { copy1->child->sprintAdj(g1); delete copy1; }
694        if (copy2) { copy2->child->sprintAdj(g2); delete copy2; }
695        if (g1[0] || g2[0]) return GENOPER_OK; else return GENOPER_OPFAIL;
696}
697
698uint32_t Geno_f4::style(const char *g, int pos)
699{
700        char ch = g[pos];
701
702        // style categories
703#define STYL4CAT_MODIFIC F14_MODIFIERS ","
704#define STYL4CAT_NEUMOD "/!="
705#define STYL4CAT_NEUSPECIAL "|@*"
706#define STYL4CAT_DIGIT "+-0123456789.[]" //'+' is only for adjusting old-style properties "/!="
707#define STYL4CAT_REST ":XN<># "
708
709        if (!isalpha(ch) && !strchr(STYL4CAT_MODIFIC STYL4CAT_NEUMOD STYL4CAT_NEUSPECIAL STYL4CAT_DIGIT STYL4CAT_REST "\t", ch))
710        {
711                return GENSTYLE_CS(0, GENSTYLE_INVALID);
712        }
713        uint32_t style = GENSTYLE_CS(0, GENSTYLE_STRIKEOUT); //default, should be changed below
714        if (strchr("X", ch))                     style = GENSTYLE_CS(0, GENSTYLE_BOLD);
715        else if (strchr(":", ch))                style = GENSTYLE_CS(0, GENSTYLE_NONE);
716        else if (strchr("#", ch))                style = GENSTYLE_RGBS(220, 0, 0, GENSTYLE_BOLD);
717        else if (strchr("/=!", ch))              style = GENSTYLE_RGBS(255, 140, 0, GENSTYLE_BOLD); //property... for now, f4 does not supoprt properties in general for any neuron class, like f1 does
718        else if (strchr("N@|*", ch))             style = GENSTYLE_RGBS(150, 0, 150, GENSTYLE_BOLD); //neuroclass
719        else if (strchr("<", ch))                style = GENSTYLE_RGBS(0, 0, 200, GENSTYLE_BOLD);
720        else if (strchr(">", ch))                style = GENSTYLE_RGBS(0, 0, 100, GENSTYLE_NONE);
721        else if (strchr(STYL4CAT_DIGIT, ch))     style = GENSTYLE_CS(GENCOLOR_NUMBER, GENSTYLE_NONE);
722        else if (strchr(STYL4CAT_MODIFIC, ch))   style = GENSTYLE_RGBS(100, 100, 100, GENSTYLE_NONE);
723        else if (strchr(STYL4CAT_NEUMOD, ch))    style = GENSTYLE_RGBS(0, 150, 0, GENSTYLE_NONE);
724        if (isalpha(ch))
725        {
726                // allowed neuron formats:
727                //   N:CLASSNAME
728                //   N:@
729                //   N:|
730                // old syntax still supported in coloring, but no longer valid:
731                //   [SENSOR, WEIGHT]
732                //   N@
733                //   N|
734                // ...so must have N: or [ before neuroclass name (or just N, but this is handled above - for N@|* only)
735
736                while (pos > 0)
737                {
738                        pos--;
739                        if (!isalpha(g[pos]))
740                        {
741                                if (isupper(g[pos + 1]) && (g[pos] == '[') || (g[pos] == ':' && pos > 0 && g[pos - 1] == 'N')) //we may have sequences like :-/:I (even though they are not valid) - in this example "I" should not be treated as neuron name, hence there must also be a "N" before ":"
742                                        style = GENSTYLE_RGBS(150, 0, 150, GENSTYLE_BOLD); // neuroclass
743                                //(...) else (...)
744                                //      style = GENSTYLE_RGBS(255, 140, 0, GENSTYLE_BOLD); // property - current f4 does not support neuron properties in a general case, only those old-style "/=!" as +! -! += -= +/ -/
745                                break;
746                        }
747                }
748        }
749        return style;
750}
Note: See TracBrowser for help on using the repository browser.