source: cpp/frams/genetics/f4/f4_general.cpp @ 1241

Last change on this file since 1241 was 1241, checked in by Maciej Komosinski, 11 months ago

No longer sort modifiers and cancel out antagonistic modifiers in f1 and f4; simplifying modifier sequences is now much less intrusive to allow for 2N distinct values of properties instead of only 2*N that resulted from earlier forced ordering (N is the number of same-letter upper- and lower-case characters in a modifier sequence)

  • Property svn:eol-style set to native
File size: 44.6 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[1213]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
[1237]6// 2018, Grzegorz Latosinski, added development checkpoints and support for new API for neuron types
[196]7
[193]8#include "f4_general.h"
[1234]9#include "../genooperators.h" // for GENOPER_ constants
[196]10#include <common/nonstd_stl.h>
[375]11#include <common/log.h>
[196]12#include <frams/model/model.h> // for min and max attributes
13#include <common/nonstd_math.h>
[193]14
15#ifdef DMALLOC
16#include <dmalloc.h>
17#endif
18
[1227]19
20#define BREAK_WHEN_REP_COUNTER_NULL //see comments where it is used
21#define TREAT_BAD_CONNECTIONS_AS_INVALID_GENO //see comments where it is used
22
23
[774]24void rolling_dec(double *v)
[671]25{
[1229]26        *v -= 0.7853;  // 0.7853981  45 degrees = pi/4 like in f1
[193]27}
[671]28
[774]29void rolling_inc(double *v)
[671]30{
[196]31        *v += 0.7853;  // 0.7853981  45 degrees
[193]32}
33
34
[1227]35f4_Cell::f4_Cell(int nnr, f4_Cell *ndad, int nangle, GeneProps newP)
[193]36{
[1227]37        nr = nnr;
38        type = CELL_UNDIFF;
[196]39        dadlink = ndad;
40        org = NULL;
41        genot = NULL;
[1239]42        gcur = old_gcur = NULL;
[760]43        repeat.clear();
[196]44        //genoRange.clear(); -- implicit
[193]45
[196]46        anglepos = nangle;
47        commacount = 0;
48        childcount = 0;
49        P = newP;
50        rolling = 0;
51        xrot = 0;
52        zrot = 0;
53        //OM = Orient_1;
54        inertia = 0.8;
55        force = 0.04;
56        sigmo = 2;
[1227]57        conns_count = 0;
[193]58
[196]59        // adjust firstend and OM if there is a stick dad
60        if (ndad != NULL)
61        {
62                // make sure it is a stick (and not a stick f4_Cell!)
[1227]63                if (ndad->type == CELL_STICK)
[196]64                {
65                        //firstend = ndad->lastend;
66                        //OM = ndad->OM;
67                        ndad->childcount++;
68                }
[1227]69                if (ndad->type == CELL_NEURON)
[196]70                {
71                        inertia = ndad->inertia;
72                        force = ndad->force;
73                        sigmo = ndad->sigmo;
74                }
75        }
76        // adjust lastend
77        //lastend = firstend + ((Orient)OM * (Pt3D(1,0,0) * P.len));
78        mz = 1;
[193]79}
80
81
[1227]82f4_Cell::f4_Cell(f4_Cells *nO, int nnr, f4_Node *ngeno, f4_Node *ngcur, f4_Cell *ndad, int nangle, GeneProps newP)
[193]83{
[1227]84        nr = nnr;
85        type = CELL_UNDIFF;
[196]86        dadlink = ndad;
87        org = nO;
88        genot = ngeno;
[1239]89        gcur = old_gcur = ngcur;
[760]90        repeat.clear();
[196]91        //genoRange.clear(); -- implicit
92        // preserve geno range of parent cell
93        if (NULL != ndad)
94                genoRange.add(ndad->genoRange);
[193]95
[196]96        anglepos = nangle;
97        commacount = 0;
98        childcount = 0;
99        P = newP;
100        rolling = 0;
101        xrot = 0;
102        zrot = 0;
103        //OM = Orient_1;
104        inertia = 0.8;
105        force = 0.04;
106        sigmo = 2;
[1227]107        conns_count = 0;
[193]108
[196]109        // adjust firstend and OM if there is a stick dad
110        if (ndad != NULL)
111        {
112                // make sure it is a stick (and not a stick f4_Cell!)
[1227]113                if (ndad->type == CELL_STICK)
[671]114                {
[196]115                        //firstend = ndad->lastend;
116                        //OM = ndad->OM;
117                        ndad->childcount++;
118                }
[1227]119                if (ndad->type == CELL_NEURON)
[196]120                {
121                        inertia = ndad->inertia;
122                        force = ndad->force;
123                        sigmo = ndad->sigmo;
124                }
125        }
126        // adjust lastend
127        //lastend = firstend + ((Orient)OM * (Pt3D(1,0,0) * P.len));
128        mz = 1;
[193]129}
130
131
132f4_Cell::~f4_Cell()
133{
[1227]134        // remove connections
135        if (conns_count)
[196]136        {
137                int i;
[1227]138                for (i = conns_count - 1; i >= 0; i--)
139                        delete conns[i];
140                conns_count = 0;
[196]141        }
[193]142}
143
[1237]144void f4_Cell::oneStep()
[193]145{
[1227]146        while (gcur != NULL)
[196]147        {
148                //DB( printf("  %d (%d) executing '%c' %d\n", name, type, gcur->name, gcur->pos); )
149                // currently this is the last one processed
150                // the current genotype code is processed
[760]151                //genoRange.add(gcur->pos,gcur->pos+gcur->name.length()-1);
[1227]152
153                // To detect what genes are valid neuroclass names, but do NOT have is_neuroclass==true
[1229]154                // (just as a curiosity to ensure we properly distinguish between, for example, the "G" neuron and the "G" modifier):
[1227]155                //char *TMP = (char*)gcur->name.c_str();
[1229]156                //if (gcur->is_neuroclass==false && GenoOperators::parseNeuroClass(TMP, ModelEnum::SHAPETYPE_BALL_AND_STICK))
[1227]157                //      printf("Could be a valid neuroclass, but is_neuroclass==false: %s\n", gcur->name.c_str());
158
[1229]159                if (gcur->neuclass == NULL) //not a neuron
[196]160                {
[1229]161                        if (gcur->name.length() > 1)
162                                logPrintf("f4_Cell", "oneStep", LOG_WARN, "Multiple-character code that is not a neuron class name: '%s'", gcur->name.c_str()); //let's see an example of such a code...
163
[760]164                        genoRange.add(gcur->pos, gcur->pos);
165                        char name = gcur->name[0];
166                        switch (name)
[671]167                        {
[760]168                        case '<':
169                        {
170                                // cell division!
171                                //DB( printf("  div! %d\n", name); )
[193]172
[760]173                                // error: sticks cannot divide
[1227]174                                if (type == CELL_STICK)
[671]175                                {
[760]176                                        // cannot fix
177                                        org->setError(gcur->pos);
[1237]178                                        return;  // error code set -> stop further cells development
[196]179                                }
[193]180
[760]181                                // undiff divides
[1227]182                                if (type == CELL_UNDIFF)
[760]183                                {
184                                        // commacount is set only when daughter turns into X
185                                        // daughter cell
186                                        // adjust new len
187                                        GeneProps newP = P;
188                                        newP.propagateAlong(false);
[1227]189                                        f4_Cell *tmp = new f4_Cell(org, org->cell_count, genot, gcur->child2, this, commacount, newP);
[760]190                                        tmp->repeat = repeat;
191                                        repeat.clear();
192                                        org->addCell(tmp);
193                                }
[1227]194                                // a neuron divides: create a new, duplicate connections
195                                if (type == CELL_NEURON)
196                                {
[760]197                                        // daughter cell
[1227]198                                        f4_Cell *tmp = new f4_Cell(org, org->cell_count, genot, gcur->child2,
[760]199                                                // has the same dadlink
200                                                this->dadlink, commacount, P);
201                                        tmp->repeat = repeat;
202                                        repeat.clear();
203                                        // it is a neuron from start
[1227]204                                        tmp->type = CELL_NEURON;
[760]205                                        // it has the same type as the parent neuron
206                                        tmp->neuclass = neuclass;
[1227]207                                        // duplicate connections
208                                        f4_CellConn *conn;
209                                        for (int i = 0; i < conns_count; i++)
[196]210                                        {
[1227]211                                                conn = conns[i];
212                                                tmp->addConnection(conn->from, conn->weight);
[196]213                                        }
[760]214                                        org->addCell(tmp);
215                                }
216                                // adjustments for this cell
217                                gcur = gcur->child;
[1237]218                                return;  // error code not set -> halt this development and yield to other cells to develop
[760]219                        }
220                        case '>':
221                        {
222                                // finish
[1227]223                                // see if there is a repeat count
[760]224                                if (repeat.top > 0)
225                                { // there is a repeat counter
226                                        if (!repeat.first()->isNull())
227                                        { // repeat counter is not null
228                                                repeat.first()->dec();
229                                                if (repeat.first()->count > 0)
230                                                {
231                                                        // return to repeat
232                                                        gcur = repeat.first()->node->child;
233                                                }
234                                                else
235                                                {
236                                                        // continue
237                                                        gcur = repeat.first()->node->child2;
238                                                        repeat.pop();
239                                                }
240                                                break;
241                                        }
[671]242                                        else
243                                        {
[196]244                                                repeat.pop();
[1227]245                                                // MacKo 2023-04: originally, there was no "break" nor "return" here (hence [[fallthrough]]; needed below for modern compilers) - not sure if this was intentional or overlooking.
246                                                // This case can be tested with "#0" in the genotype. Anyway, there seems to be no difference in outcomes with and without "break".
247                                                // However, falling through [[fallthrough]] below for count==0 causes performing repeat.push(repeat_ptr(gcur, 0)) while the very reason
248                                                // we are here is that repeat count==0 (one of the conditions for isNull()), so I opted to add "break", but marked this tentative decision using #define.
249                                                // The ultimate informed decision would require understanding all the logic and testing all the edge cases.
250#ifdef BREAK_WHEN_REP_COUNTER_NULL
251                                                break;
252#endif
[196]253                                        }
254                                }
[671]255                                else
256                                {
[760]257                                        // error: still undiff
[1227]258                                        if (type == CELL_UNDIFF)
[760]259                                        {
260                                                // fix it: insert an 'X'
[1227]261                                                f4_Node *insertnode = new f4_Node("X", NULL, gcur->pos);
[760]262                                                if (org->setRepairInsert(gcur->pos, gcur, insertnode)) // not in repair mode, release
263                                                        delete insertnode;
[1237]264                                                return;  // error code set -> stop further cells development
[760]265                                        }
266                                        repeat.clear();
267                                        // eat up rest
[1227]268                                        int remaining_nodes = gcur->count() - 1;
269                                        if (remaining_nodes > 0)
270                                                logPrintf("f4_Cell", "oneStep", LOG_WARN, "Ignoring junk genetic code: %d node(s) at position %d", remaining_nodes, gcur->child->pos); //let's see an example of such a genotype...
[760]271                                        gcur = NULL;
[1237]272                                        return;  // done development
[196]273                                }
274                        }
[1227]275#ifndef BREAK_WHEN_REP_COUNTER_NULL
[853]276                        [[fallthrough]];
[1227]277#endif
[760]278                        case '#':
[671]279                        {
[760]280                                // repetition marker
281                                if (repeat.top >= repeat_stack::stackSize)
[196]282                                {
[760]283                                        // repeat pointer stack is full, cannot remember this one.
284                                        // fix: delete it
285                                        org->setRepairRemove(gcur->pos, gcur);
[1237]286                                        return;  // error code set -> stop further cells development
[196]287                                }
[1227]288                                repeat.push(repeat_ptr(gcur, gcur->reps));
[760]289                                gcur = gcur->child;
290                                break;
[196]291                        }
[760]292                        case ',':
[196]293                        {
[760]294                                commacount++;
295                                gcur = gcur->child;
296                                break;
[196]297                        }
[1234]298                        case 'r':
299                        case 'R':
[671]300                        {
[760]301                                // error: if neuron
[1227]302                                if (type == CELL_NEURON)
[760]303                                {
304                                        // fix: delete it
305                                        org->setRepairRemove(gcur->pos, gcur);
[1237]306                                        return;  // error code set -> stop further cells development
[760]307                                }
308                                switch (name)
309                                {
310                                case 'r':   rolling_dec(&rolling); break;
311                                case 'R':   rolling_inc(&rolling); break;
312                                }
313                                gcur = gcur->child;
314                                break;
[196]315                        }
[760]316                        case 'l':  case 'L':
317                        case 'c':  case 'C':
318                        case 'q':  case 'Q':
319                        case 'a':  case 'A':
320                        case 'i':  case 'I':
321                        case 's':  case 'S':
322                        case 'm':  case 'M':
323                        case 'f':  case 'F':
324                        case 'w':  case 'W':
325                        case 'e':  case 'E':
326                        case 'd':  case 'D':
327                        case 'g':  case 'G':
328                        case 'b':  case 'B':
329                        case 'h':  case 'H':
[671]330                        {
[760]331                                // error: if neuron
[1227]332                                if (type == CELL_NEURON) //some neurons have the same single-letter names as modifiers (for example G,S,D), but they are supposed to have is_neuroclass==true so they should indeed not be handled here
[1234]333                                {//however, what we see here is actually modifiers such as IdqEbWL (so not valid neuroclasses) that occurred within an already differentiated cell of type==CELL_NEURON.
[1227]334                                        //printf("Handled as a modifier, but type==CELL_NEURON: '%c'\n", name);
[760]335                                        // fix: delete it
336                                        org->setRepairRemove(gcur->pos, gcur);
[1237]337                                        return;  // error code set -> stop further cells development
[760]338                                }
339                                P.executeModifier(name);
340                                gcur = gcur->child;
341                                break;
[196]342                        }
[760]343                        case 'X':
[671]344                        {
[760]345                                // turn undiff. cell into a stick
346                                // error: already differentiated
[1227]347                                if (type != CELL_UNDIFF)
[760]348                                {
349                                        // fix: delete this node
350                                        org->setRepairRemove(gcur->pos, gcur);
[1237]351                                        return;  // error code set -> stop further cells development
[760]352                                }
[1227]353                                type = CELL_STICK;
[760]354                                // fix dad commacount and own anglepos
[1237]355                                if (dadlink != NULL)
[760]356                                {
357                                        dadlink->commacount++;
358                                        anglepos = dadlink->commacount;
359                                }
[1227]360                                // change of type halts developments, see comment at 'neuclasshandler' below
[760]361                                gcur = gcur->child;
[1237]362                                return;  // error code not set -> halt this development and yield to other cells to develop
[196]363                        }
[1227]364                        case '[':
[671]365                        {
[1227]366                                // connection to neuron
367                                // error: not a neuron
368                                if (type != CELL_NEURON)
[760]369                                {
370                                        // fix: delete it
371                                        org->setRepairRemove(gcur->pos, gcur);
[1237]372                                        return;  // error code set -> stop further cells development
[760]373                                }
[1227]374                                // input [%d:%g]
375                                int relfrom = gcur->conn_from;
376                                double weight = gcur->conn_weight;
377                                f4_Cell *neu_from = NULL;
[193]378
[1227]379                                // input from other neuron
380                                // find neuron at relative i
381                                // find own index
382                                int this_index = 0, neu_counter = 0;
383                                for (int i = 0; i < org->cell_count; i++)
[760]384                                {
[1227]385                                        if (org->C[i]->type == CELL_NEURON) neu_counter++;
386                                        if (org->C[i] == this) { this_index = neu_counter - 1; break; }
[760]387                                }
[1227]388                                // find index of incoming
389                                int from_index = this_index + relfrom;
[193]390
[1227]391                                if (from_index < 0) goto wait_conn;
392                                if (from_index >= org->cell_count) goto wait_conn;
393
394                                // find that neuron
395                                neu_counter = 0;
396                                int from;
397                                for (from = 0; from < org->cell_count; from++)
[760]398                                {
[1227]399                                        if (org->C[from]->type == CELL_NEURON) neu_counter++;
400                                        if (from_index == (neu_counter - 1)) break;
[760]401                                }
[1227]402                                if (from >= org->cell_count) goto wait_conn;
403                                neu_from = org->C[from];
404
405                                // add connection
406                                // error: could not add connection (too many?)
407                                if (addConnection(neu_from, weight))
[760]408                                {
409                                        // cannot fix
410                                        org->setError(gcur->pos);
[1237]411                                        return;  // error code set -> stop further cells development
[760]412                                }
413                                gcur = gcur->child;
414                                break;
[196]415                        }
[1227]416                wait_conn:
[671]417                        {
[760]418                                // wait for other neurons to develop
[1227]419
[1239]420                                if (!org->development_stagnation) // other cells are developing, the situation is changing, we may continue waiting...
[1237]421                                        return;  // error code not set -> halt this development and yield to other cells to develop
[1227]422
[1239]423                                //no cells are developing and we are waiting, but there is no chance other cells will create neurons we are waiting for, so we are forced to move on.
424
[1227]425#ifdef TREAT_BAD_CONNECTIONS_AS_INVALID_GENO // MacKo 2023-04: there were so many invalid connections accumulating in the genotype (and stopping processing of the chain of gcur->child) that it looks like treating them as errors is better... in 2000's, Framsticks neurons were flexible when it comes to inputs and outputs (for example, when asked, muscles would provide an output too, and neurons that ignored inputs would still accept them when connected) so f4 could create connections pretty randomly, but after 2000's we attempt to respect neurons' getPreferredInputs() and getPreferredOutput() so the network of connections has more constraints.
426                                if (gcur->parent->name == "#")
[760]427                                {
[1227]428                                        // MacKo 2023-04: Unfortunately the logic of multiplicating connections is not ideal...
429                                        //TREAT_BAD_CONNECTIONS_AS_INVALID_GENO without this "#" exception would break /*4*/<X>N:N#5<[1:1]>
430                                        // because every neuron wants to get an input from the neuron that will be created next
431                                        // and all is fine until the last created neuron, which wants to get an input from another one which will not be created
432                                        // (3 gets from 4, 4 gets from 5, 5 wants to get from 6 (relative connection offset for each of them is 1),
433                                        // but 6 will not get created and if we want to TREAT_BAD_CONNECTIONS_AS_INVALID_GENO, we produce an error...
434                                        // We would like to have this multiplication working, but OTAH we don't want to accept bad connections because then they tend to multiply as junk genes and bloat the genotype also causing more and more neutral mutations...
435                                        //so this condition and checking for "#" is a simple way to be kind to some, but not all, bad connections, and not raise errors. Perhaps too kind and we open the door for too many cases with invalid connections.
436                                        //Maybe it would be better to perform this check before addConnection(), seeing that for example we process the last iteration of the repetition counter? But how would we know that the (needed here) input neuron will not be developed later by other dividing cells...
[1239]437
[1227]438                                        gcur = gcur->child;
[1239]439                                        org->development_stagnation = false; //do not force other potentially waiting cells to hurry and act in this development cycle (which would be the last cycle if development_stagnation stayed true); we just acted and because of this the situation may change, so they can wait until another development_stagnation is detected
[1237]440                                        return;  // error code not set -> halt this development and yield to other cells to develop
[760]441                                }
[1227]442                                else
443                                {
444                                        //org->setError(gcur->pos); //in case setRepairRemove() would not always produce reasonable results
445                                        org->setRepairRemove(gcur->pos, gcur); //produces unexpected results? or NOT? TODO verify, some genotypes earlier produced strange outcomes of this repair (produced a valid genotype, but some neurons were multiplied/copied after repair - maybe because when a branch of '<' (or something else) is missing, the other branch is copied?)
[1237]446                                        return;  // error code set -> stop further cells development
[1227]447                                }
448#else
449                                // no more actives, cannot add connection, ignore, but treat not as an error - before 2023-04
[760]450                                gcur = gcur->child;
[1227]451#endif
[196]452                        }
[853]453                        break;
[760]454                        case ':':
[671]455                        {
[760]456                                // neuron parameter
457                                // error: not a neuron
[1227]458                                if (type != CELL_NEURON)
[671]459                                {
[760]460                                        // fix: delete it
461                                        org->setRepairRemove(gcur->pos, gcur);
[1237]462                                        return;  // error code set -> stop further cells development
[196]463                                }
[1227]464                                switch (gcur->prop_symbol)
[671]465                                {
[760]466                                case '!':
[1227]467                                        if (gcur->prop_increase)
[760]468                                                force += (1.0 - force) * 0.2;
469                                        else
470                                                force -= force * 0.2;
471                                        break;
472                                case '=':
[1227]473                                        if (gcur->prop_increase)
[760]474                                                inertia += (1.0 - inertia) * 0.2;
475                                        else
476                                                inertia -= inertia * 0.2;
477                                        break;
478                                case '/':
[1227]479                                        if (gcur->prop_increase)
[760]480                                                sigmo *= 1.4;
481                                        else
482                                                sigmo /= 1.4;
483                                        break;
484                                default:
485                                        org->setRepairRemove(gcur->pos, gcur);
[1237]486                                        return;  // error code set -> stop further cells development
[196]487                                }
[760]488                                gcur = gcur->child;
489                                break;
[196]490                        }
[760]491                        case ' ':
[1237]492                        case '\t':
493                        case '\n':
494                        case '\r':
[671]495                        {
[1237]496                                // whitespace has no effect, should not occur
[760]497                                // fix: delete it
498                                org->setRepairRemove(gcur->pos, gcur);
[1237]499                                return;  // error code set -> stop further cells development
[196]500                        }
[760]501                        default:
[671]502                        {
[1229]503                                // error: unknown code
504                                string buf = "Unknown code '" + gcur->name + "'";
505                                logMessage("f4_Cell", "oneStep", LOG_ERROR, buf.c_str());
506                                org->setRepairRemove(gcur->pos, gcur);
[1237]507                                return;  // error code set -> stop further cells development
[196]508                        }
[760]509                        }
510                }
511                else
512                {
[1227]513                        genoRange.add(gcur->pos, gcur->pos + int(gcur->name.length()) + 2 - 1); // +2 for N:
514                        if (type != CELL_UNDIFF)
[671]515                        {
[760]516                                // fix: delete this node
[196]517                                org->setRepairRemove(gcur->pos, gcur);
[1237]518                                return;  // error code set -> stop further cells development
[196]519                        }
[760]520                        // error: if no previous
[1227]521                        if (dadlink == NULL)
[671]522                        {
[760]523                                // fix: delete it
[196]524                                org->setRepairRemove(gcur->pos, gcur);
[1237]525                                return;  // error code set -> stop further cells development
[196]526                        }
[1229]527                        neuclass = gcur->neuclass;
[1227]528                        type = CELL_NEURON;
529                        // change of type also halts development, to give other
530                        // cells a chance for adjustment.  Namely, it is important
531                        // to wait for other cells to turn to neurons before adding connections
[196]532                        gcur = gcur->child;
[1237]533                        return;  // error code not set -> halt this development and yield to other cells to develop
[196]534                }
535        }
[193]536}
537
538
[1227]539int f4_Cell::addConnection(f4_Cell *nfrom, double nweight)
[193]540{
[1227]541        if (nfrom->neuclass->getPreferredOutput() == 0) return -1; // if incoming neuron does not produce output, return error
542        if (neuclass->getPreferredInputs() != -1 && conns_count >= neuclass->getPreferredInputs()) return -1; //cannot add more inputs to this neuron
543        if (conns_count >= F4_MAX_CELL_INPUTS - 1) return -1; // over hardcoded limit
544        conns[conns_count] = new f4_CellConn(nfrom, nweight);
545        conns_count++;
[196]546        return 0;
[193]547}
548
549
550void f4_Cell::adjustRec()
551{
[196]552        //f4_OrientMat rot;
[671]553        int i;
[193]554
[196]555        if (recProcessedFlag)
556                // already processed
557                return;
[193]558
[196]559        // mark it processed
560        recProcessedFlag = 1;
[193]561
[196]562        // make sure its parent is processed first
[1227]563        if (dadlink != NULL)
[196]564                dadlink->adjustRec();
[193]565
[196]566        // count children
567        childcount = 0;
[1227]568        for (i = 0; i < org->cell_count; i++)
[196]569        {
570                if (org->C[i]->dadlink == this)
[1227]571                        if (org->C[i]->type == CELL_STICK)
[196]572                                childcount++;
573        }
[193]574
[1227]575        if (type == CELL_STICK)
[196]576        {
[1227]577                if (dadlink == NULL)
[196]578                {
579                        //firstend = Pt3D_0;
580                        // rotation due to rolling
581                        xrot = rolling;
582                        mz = 1;
583                }
[671]584                else
585                {
[196]586                        //firstend = dadlink->lastend;
[760]587                        GeneProps Pdad = dadlink->P;
588                        GeneProps Padj = Pdad;
589                        Padj.propagateAlong(false);
[193]590
[196]591                        //rot = Orient_1;
[193]592
[196]593                        // rotation due to rolling
594                        xrot = rolling +
595                                // rotation due to twist
596                                Pdad.twist;
597                        if (dadlink->commacount <= 1)
598                        {
599                                // rotation due to curvedness
[671]600                                zrot = Padj.curvedness;
[196]601                        }
[671]602                        else
603                        {
604                                zrot = Padj.curvedness + (anglepos * 1.0 / (dadlink->commacount + 1) - 0.5) * M_PI * 2.0;
[196]605                        }
[193]606
[196]607                        //rot = rot * f4_OrientMat(yOz, xrot);
608                        //rot = rot * f4_OrientMat(xOy, zrot);
609                        // rotation relative to parent stick
610                        //OM = rot * OM;
[193]611
[196]612                        // rotation in world coordinates
613                        //OM =  ((f4_OrientMat)dadlink->OM) * OM;
614                        mz = dadlink->mz / dadlink->childcount;
615                }
616                //Pt3D lastoffset = (Orient)OM * (Pt3D(1,0,0)*P.len);
617                //lastend = firstend + lastoffset;
618        }
[193]619}
620
621
622
[1227]623f4_CellConn::f4_CellConn(f4_Cell *nfrom, double nweight)
[193]624{
[196]625        from = nfrom;
[1227]626        weight = nweight;
[193]627}
628
629
630
[1231]631f4_Cells::f4_Cells(f4_Node *genome, bool nrepair)
[193]632{
[196]633        repair = nrepair;
[1227]634        errorcode = GENOPER_OK;
[196]635        errorpos = -1;
636        repair_remove = NULL;
637        repair_parent = NULL;
638        repair_insert = NULL;
639        tmpcel = NULL;
[1231]640
641        // create ancestor cell
[760]642        C[0] = new f4_Cell(this, 0, genome, genome, NULL, 0, GeneProps::standard_values);
[1227]643        cell_count = 1;
[1239]644        development_stagnation = false;
[193]645}
646
647
648
649f4_Cells::~f4_Cells()
650{
[196]651        // release cells
[1227]652        if (cell_count)
[196]653        {
[1231]654                for (int i = cell_count - 1; i >= 0; i--)
[196]655                        delete C[i];
[1227]656                cell_count = 0;
[196]657        }
[193]658}
659
660
[1227]661bool f4_Cells::oneStep()
[193]662{
[1227]663        int old_cell_count = cell_count; //cell_count may change in the loop as new cells may be appended because cells may be dividing
664        for (int i = 0; i < old_cell_count; i++)
[1239]665                C[i]->old_gcur = C[i]->gcur;
666
667        for (int i = 0; i < old_cell_count; i++)
[671]668        {
[1239]669                C[i]->oneStep();
[1237]670                if (errorcode != GENOPER_OK)
[1239]671                        return false; // error -> end development
[1227]672        }
[1239]673
674        if (cell_count != old_cell_count) //the number of cells changed - something is going on!
675                return true; //so continue development!
676
677        for (int i = 0; i < old_cell_count; i++)
678                if (C[i]->old_gcur != C[i]->gcur) // genotype execution pointer changed - something is going on!
679                        return true; //so continue development!
680
[1240]681        //the same number of cells, no progress in development in any cell -> stagnation!
682
683        if (development_stagnation) // stagnation was already detected in the previous step, so end development!
684        {
685                for (int i = 0; i < cell_count; i++)
686                        if (C[i]->gcur != NULL) // genotype execution pointer did not reach the end
687                                logPrintf("f4_Cells", "oneStep", LOG_WARN, "Finishing the development of cells due to stagnation, but cell %d did not reach the end of development", i); //let's see an example of such a genotype and investigate...
688                return false; //end development
689        }
[1239]690        else
691        {
692                development_stagnation = true; //signal (force) f4_Cell's that wait for neural connection development to make a step, because all cells stagnated and waiting cells cannot hope for new neurons to be created
[1240]693                return true; //one grace step. If there are some waiting cells, they must move on in the next step and set development_stagnation=false or set error. If development_stagnation is not set to false, we will finish development in the next step. This grace step may be unnecessary if there are no waiting cells, but we have no easy way to check this from here (although we could check if all cells' gcur==NULL... would this be always equivalent? Maybe some cells may stagnate with gcur!=NULL and they are not waiting for neural connections to develop and this does not mean an error? Added LOG_WARN above to detect such cases. Anyway, for gcur==NULL, f4_Cell.oneStep() exits immediately, so one grace step is not a big overhead.)
[1239]694        }
[193]695}
696
697
698int f4_Cells::simulate()
699{
[1234]700        const bool PRINT_CELLS_DEVELOPMENT = false; //print the state of cells
[1227]701        errorcode = GENOPER_OK;
[1239]702        development_stagnation = false; //will be detected by oneStep()
[193]703
[1234]704        if (PRINT_CELLS_DEVELOPMENT) f4_Node::print_tree(C[0]->genot, 0);
705        if (PRINT_CELLS_DEVELOPMENT) print_cells("Initialization");
[193]706
[1227]707        // execute oneStep() in a cycle
[1234]708        while (oneStep()) if (PRINT_CELLS_DEVELOPMENT) print_cells("Development step");
709        if (PRINT_CELLS_DEVELOPMENT) print_cells("After last development step");
[193]710
[1227]711        if (errorcode != GENOPER_OK) return errorcode;
712
[196]713        // fix neuron attachements
[1227]714        for (int i = 0; i < cell_count; i++)
[671]715        {
[1227]716                if (C[i]->type == CELL_NEURON)
[671]717                {
[1227]718                        while (C[i]->dadlink->type == CELL_NEURON)
[671]719                        {
[196]720                                C[i]->dadlink = C[i]->dadlink->dadlink;
721                        }
722                }
[671]723        }
[193]724
[196]725        // there should be no undiff. cells
726        // make undifferentiated cells sticks
[1227]727        for (int i = 0; i < cell_count; i++)
[671]728        {
[1227]729                if (C[i]->type == CELL_UNDIFF)
[671]730                {
[1227]731                        C[i]->type = CELL_STICK;
732                        //setError();
[196]733                }
[671]734        }
[193]735
[196]736        // recursive adjust
737        // reset recursive traverse flags
[1227]738        for (int i = 0; i < cell_count; i++)
[196]739                C[i]->recProcessedFlag = 0;
740        // process every cell
[1227]741        for (int i = 0; i < cell_count; i++)
[196]742                C[i]->adjustRec();
[193]743
[196]744        //DB( printf("Cell simulation done, %d cells. \n", nc); )
[193]745
[1234]746        if (PRINT_CELLS_DEVELOPMENT) print_cells("Final");
[1227]747
748        return errorcode;
[193]749}
750
751
[1227]752void f4_Cells::print_cells(const char* description)
753{
754        printf("------ %-55s ------ errorcode=%d, errorpos=%d\n", description, getErrorCode(), getErrorPos());
755        for (int i = 0; i < cell_count; i++)
756        {
757                f4_Cell *c = C[i];
758                string type;
759                switch (c->type)
760                {
[1228]761                case CELL_UNDIFF: type = "undiff"; break;
[1227]762                case CELL_STICK:  type = "STICK"; break;
763                case CELL_NEURON: type = string("NEURON:") + c->neuclass->name.c_str(); break;
764                default: type = std::to_string(c->type);
765                }
[1239]766                const char *status = c->gcur == c->old_gcur ? (c->gcur != NULL ? "no progress" : "") : (c->gcur != NULL ? "progress" : "finished"); //progress or no progress means the cell is yielding = not finished but decided to halt development and wait for other cells. New cells may be created in case of "no progress" status.
[1227]767                printf("%2d(%-8s)  nr=%d \t type=%-15s \t genot=%s \t gcurrent=%s", i, status, c->nr, type.c_str(), c->genot->name.c_str(), c->gcur ? c->gcur->name.c_str() : "null");
768                if (c->gcur && c->gcur->name == "[")
769                        printf("\tfrom=%d  weight=%g", c->gcur->conn_from, c->gcur->conn_weight);
770                printf("\n");
771                for (int l = 0; l < c->conns_count; l++)
772                        printf("\tconn:%d from=%d weight=%g\n", l, c->conns[l]->from->nr, c->conns[l]->weight);
773        }
774        printf("\n");
775}
776
777
[774]778void f4_Cells::addCell(f4_Cell *newcell)
[193]779{
[1227]780        if (cell_count >= F4_MAX_CELLS - 1)
[671]781        {
[196]782                delete newcell;
783                return;
784        }
[1227]785        C[cell_count] = newcell;
786        cell_count++;
[193]787}
788
789
790void f4_Cells::setError(int nerrpos)
791{
[1227]792        errorcode = GENOPER_OPFAIL;
[196]793        errorpos = nerrpos;
[193]794}
795
[1232]796void f4_Cells::setRepairRemove(int nerrpos, f4_Node *to_remove)
[193]797{
[1232]798        errorcode = GENOPER_REPAIR;
799        errorpos = nerrpos;
[671]800        if (!repair)
801        {
[196]802                // not in repair mode, treat as repairable error
803        }
[671]804        else
805        {
[1232]806                repair_remove = to_remove;
[196]807        }
[193]808}
809
[1232]810int f4_Cells::setRepairInsert(int nerrpos, f4_Node *parent, f4_Node *to_insert)
[193]811{
[1232]812        errorcode = GENOPER_REPAIR;
813        errorpos = nerrpos;
[671]814        if (!repair)
815        {
[196]816                // not in repair mode, treat as repairable error
817                return -1;
818        }
[671]819        else
820        {
[196]821                repair_parent = parent;
[1232]822                repair_insert = to_insert;
[196]823                return 0;
824        }
[193]825}
826
[1227]827void f4_Cells::repairGeno(f4_Node *geno, int whichchild)
[193]828{
[196]829        // assemble repaired geno, if the case
830        if (!repair) return;
[1227]831        if ((repair_remove == NULL) && (repair_insert == NULL)) return;
[196]832        // traverse genotype tree, remove / insert node
[1227]833        f4_Node *g2;
834        if (whichchild == 1)
835                g2 = geno->child;
836        else
837                g2 = geno->child2;
838        if (g2 == NULL)
[196]839                return;
[671]840        if (g2 == repair_remove)
841        {
[1227]842                f4_Node *oldgeno;
[196]843                geno->removeChild(g2);
[671]844                if (g2->child)
845                {
[196]846                        // add g2->child as child to geno
[1227]847                        if (whichchild == 1)
848                                geno->child = g2->child;
849                        else
850                                geno->child2 = g2->child;
[196]851                        g2->child->parent = geno;
852                }
853                oldgeno = g2;
854                oldgeno->child = NULL;
855                delete oldgeno;
[1227]856                if (geno->child == NULL) return;
[196]857                // check this new
858                repairGeno(geno, whichchild);
859                return;
860        }
[671]861        if (g2 == repair_parent)
862        {
[196]863                geno->removeChild(g2);
864                geno->addChild(repair_insert);
865                repair_insert->parent = geno;
866                repair_insert->child = g2;
867                repair_insert->child2 = NULL;
868                g2->parent = repair_insert;
869        }
870        // recurse
871        if (g2->child)  repairGeno(g2, 1);
872        if (g2->child2) repairGeno(g2, 2);
[193]873}
874
875
876void f4_Cells::toF1Geno(SString &out)
877{
[196]878        if (tmpcel) delete tmpcel;
[760]879        tmpcel = new f4_Cell(-1, NULL, 0, GeneProps::standard_values);
[196]880        out = "";
881        toF1GenoRec(0, out);
882        delete tmpcel;
[193]883}
884
885
886void f4_Cells::toF1GenoRec(int curc, SString &out)
887{
[1227]888        if (curc >= cell_count) return;
[193]889
[1227]890        if (C[curc]->type != CELL_STICK) return;
[193]891
[1240]892        f4_Cell *thisti = C[curc];
[1227]893        if (thisti->dadlink != NULL)
[196]894                *tmpcel = *(thisti->dadlink);
[193]895
[196]896        // adjust length, curvedness, etc.
[760]897        tmpcel->P.propagateAlong(false);
[671]898        while (tmpcel->P.length > thisti->P.length)
[196]899        {
900                tmpcel->P.executeModifier('l');
901                out += "l";
902        }
[671]903        while (tmpcel->P.length < thisti->P.length)
[196]904        {
905                tmpcel->P.executeModifier('L');
906                out += "L";
907        }
[671]908        while (tmpcel->P.curvedness > thisti->P.curvedness)
[196]909        {
910                tmpcel->P.executeModifier('c');
911                out += "c";
912        }
[671]913        while (tmpcel->P.curvedness < thisti->P.curvedness)
[196]914        {
915                tmpcel->P.executeModifier('C');
916                out += "C";
917        }
[671]918        while (thisti->rolling > 0.0f)
919        {
[196]920                rolling_dec(&(thisti->rolling));
921                out += "R";
922        }
[671]923        while (thisti->rolling < 0.0f)
924        {
[196]925                rolling_inc(&(thisti->rolling));
926                out += "r";
927        }
[193]928
[196]929        // output X for this stick
930        out += "X";
[193]931
[196]932        // neurons attached to it
[1240]933        for (int i = 0; i < cell_count; i++)
[671]934        {
[1227]935                if (C[i]->type == CELL_NEURON)
[671]936                {
937                        if (C[i]->dadlink == thisti)
938                        {
[1240]939                                f4_Cell *thneu = C[i];
[196]940                                out += "[";
[1240]941                                out += thneu->neuclass->name.c_str();
942                                if (thneu->conns_count > 0)
943                                        out += ", ";
[1227]944                                // connections
[1240]945                                for (int j = 0; j < thneu->conns_count; j++)
[196]946                                {
[1240]947                                        if (j > 0) out += ", ";
948                                        char buf[100];
[1227]949                                        sprintf(buf, "%d", thneu->conns[j]->from->nr - thneu->nr);
950                                        out += buf;
[196]951                                        out += ":";
[671]952                                        // connection weight
[1227]953                                        sprintf(buf, "%g", thneu->conns[j]->weight);
[196]954                                        out += buf;
955                                }
956                                out += "]";
957                        }
958                }
[671]959        }
[193]960
[196]961        // sticks connected to it
962        if (thisti->commacount >= 2)
963                out += "(";
[193]964
[1240]965        int ccount = 1;
966        for (int i = 0; i < cell_count; i++)
[671]967        {
[1227]968                if (C[i]->type == CELL_STICK)
[671]969                {
970                        if (C[i]->dadlink == thisti)
971                        {
972                                while (ccount < (C[i])->anglepos)
973                                {
[196]974                                        ccount++;
975                                        out += ",";
976                                }
977                                toF1GenoRec(i, out);
978                        }
[671]979                }
980        }
981
982        while (ccount < thisti->commacount)
983        {
[196]984                ccount++;
985                out += ",";
986        }
[193]987
[196]988        if (thisti->commacount >= 2)
989                out += ")";
[193]990}
991
992
993
[671]994// to organize an f4 genotype in a tree structure
[193]995
[1227]996f4_Node::f4_Node()
[193]997{
[760]998        name = "?";
[196]999        parent = NULL;
1000        child = NULL;
1001        child2 = NULL;
1002        pos = -1;
[1227]1003
1004        reps = 0;
1005        prop_symbol = '\0';
1006        prop_increase = false;
1007        conn_from = 0;
1008        conn_weight = 0.0;
1009        neuclass = NULL;
[193]1010}
1011
[1227]1012f4_Node::f4_Node(string nname, f4_Node *nparent, int npos)
[760]1013{
1014        name = nname;
1015        parent = nparent;
1016        child = NULL;
1017        child2 = NULL;
1018        pos = npos;
1019        if (parent) parent->addChild(this);
[1227]1020
1021        reps = 0;
1022        prop_symbol = '\0';
1023        prop_increase = false;
1024        conn_from = 0;
1025        conn_weight = 0.0;
1026        neuclass = NULL;
[760]1027}
1028
[1227]1029f4_Node::f4_Node(char nname, f4_Node *nparent, int npos)
[193]1030{
[196]1031        name = nname;
1032        parent = nparent;
1033        child = NULL;
1034        child2 = NULL;
1035        pos = npos;
1036        if (parent) parent->addChild(this);
[1227]1037
1038        reps = 0;
1039        prop_symbol = '\0';
1040        prop_increase = false;
1041        conn_from = 0;
1042        conn_weight = 0.0;
1043        neuclass = NULL;
[193]1044}
1045
[1227]1046f4_Node::~f4_Node()
[193]1047{
[1227]1048        destroy();
[193]1049}
1050
[1227]1051void f4_Node::print_tree(const f4_Node *root, int indent)
[193]1052{
[1227]1053        for (int i = 0; i < indent; i++) printf(" ");
[1234]1054        printf("%s%s%s (%d)", root->neuclass != NULL ? "N:" : "", root->name.c_str(), root->name == "#" ? std::to_string(root->reps).c_str() : "", root->count() - 1);
[1227]1055        if (root->name == "[")
1056                printf("     from=%-3d  weight=%g", root->conn_from, root->conn_weight);
1057        printf("\n");
1058        if (root->child) print_tree(root->child, indent + 1);
1059        if (root->child2) print_tree(root->child2, indent + 1);
1060}
1061
1062int f4_Node::addChild(f4_Node *nchi)
1063{
1064        if (child == NULL)
[671]1065        {
[196]1066                child = nchi;
1067                return 0;
1068        }
[1227]1069        if (child2 == NULL)
[671]1070        {
[196]1071                child2 = nchi;
1072                return 0;
1073        }
1074        return -1;
[193]1075}
1076
[1227]1077int f4_Node::removeChild(f4_Node *nchi)
[193]1078{
[671]1079        if (nchi == child2)
1080        {
[196]1081                child2 = NULL;
1082                return 0;
1083        }
[671]1084        if (nchi == child)
1085        {
[196]1086                child = NULL;
1087                return 0;
1088        }
1089        return -1;
[193]1090}
1091
[1227]1092int f4_Node::childCount()
[193]1093{
[1231]1094        return int(child != NULL) + int(child2 != NULL); //0, 1 or 2
[193]1095}
1096
[1227]1097int f4_Node::count() const
[193]1098{
[196]1099        int c = 1;
[1227]1100        if (child != NULL)  c += child->count();
1101        if (child2 != NULL) c += child2->count();
[196]1102        return c;
[193]1103}
1104
[1227]1105f4_Node* f4_Node::ordNode(int n)
[193]1106{
[196]1107        int n1;
[1227]1108        if (n == 0) return this;
[196]1109        n--;
[1227]1110        if (child != NULL)
[671]1111        {
[196]1112                n1 = child->count();
1113                if (n < n1) return child->ordNode(n);
1114                n -= n1;
1115        }
[1227]1116        if (child2 != NULL)
[671]1117        {
[196]1118                n1 = child2->count();
1119                if (n < n1) return child2->ordNode(n);
1120                n -= n1;
1121        }
1122        return NULL;
[193]1123}
1124
[1227]1125f4_Node* f4_Node::randomNode()
[193]1126{
[196]1127        int n = count();
[1228]1128        // pick a random node between 0 and n-1
[896]1129        return ordNode(rndUint(n));
[193]1130}
1131
[1227]1132f4_Node* f4_Node::randomNodeWithSize(int mn, int mx)
[193]1133{
[196]1134        // try random nodes, and accept if size in range
1135        // limit to maxlim tries
1136        int i, n, maxlim;
[1227]1137        f4_Node *nod = NULL;
[196]1138        maxlim = count();
[671]1139        for (i = 0; i < maxlim; i++)
1140        {
[196]1141                nod = randomNode();
1142                n = nod->count();
[770]1143                if ((n >= mn) && (n <= mx)) return nod;
[196]1144        }
1145        // failed, doesn't matter
1146        return nod;
[193]1147}
1148
[1227]1149void f4_Node::sprint(SString& out)
[193]1150{
[767]1151        char buf2[20];
[196]1152        // special case: repetition code
[760]1153        if (name == "#")
[196]1154        {
1155                out += "#";
[1227]1156                sprintf(buf2, "%d", reps);
1157                out += buf2;
[196]1158        }
1159        else {
[1227]1160                // special case: neuron connection
[760]1161                if (name == "[")
[671]1162                {
[196]1163                        out += "[";
[1227]1164                        sprintf(buf2, "%d", conn_from);
[767]1165                        out += buf2;
[1227]1166                        sprintf(buf2, ":%g]", conn_weight);
1167                        out += buf2;
[196]1168                }
[760]1169                else if (name == ":")
[671]1170                {
[1227]1171                        sprintf(buf2, ":%c%c:", prop_increase ? '+' : '-', prop_symbol);
[767]1172                        out += buf2;
[196]1173                }
[1227]1174                else if (neuclass != NULL)
[760]1175                {
[1227]1176                        out += "N:";
1177                        out += neuclass->name.c_str();
[760]1178                }
[671]1179                else
1180                {
[767]1181                        out += name.c_str();
[196]1182                }
1183        }
[1227]1184
1185        if (child != NULL)
1186                child->sprint(out);
[196]1187        // if two children, make sure last char is a '>'
[1227]1188        if (childCount() == 2)
1189                if (out[0] == 0) out += ">"; else
1190                        if (out[out.length() - 1] != '>') out += ">";
1191
1192        if (child2 != NULL)
1193                child2->sprint(out);
[196]1194        // make sure last char is a '>'
[1227]1195        if (out[0] == 0) out += ">"; else
1196                if (out[out.length() - 1] != '>') out += ">";
[193]1197}
1198
[1227]1199void f4_Node::sprintAdj(char *& buf)
[193]1200{
[196]1201        unsigned int len;
1202        // build in a SString, with initial size
[955]1203        SString out;
1204        out.reserve(int(strlen(buf)) + 2000);
[193]1205
[196]1206        sprint(out);
[1235]1207        len = out.length();
[193]1208
[196]1209        // very last '>' can be omitted
[1235]1210        // MacKo 2023-05: after tightening parsing and removing a silent repair for missing '>' after '#', this is no longer always the case.
1211        // For genotypes using '#', removing trailing >'s makes them invalid: /*4*/<X><N:N>X#1>> or /*4*/<X><N:N>X#1#2>>> or /*4*/<X><N:N>X#1#2#3>>>> etc.
1212        // Such invalid genotypes with missing >'s would then require silently adding >'s, but now stricter parsing and clear information about invalid syntax is preferred.
1213        // See also comments in f4_processRecur() case '#'.
1214        //if (len > 1)
1215        //      if (out[len - 1] == '>') { (out.directWrite())[len - 1] = 0; out.endWrite(); }; //Macko 2023-04 "can be omitted" => was always removed in generated genotypes.
1216
[196]1217        // copy back to string
1218        // if new is longer, reallocate buf
[671]1219        if (len + 1 > strlen(buf))
1220        {
[196]1221                buf = (char*)realloc(buf, len + 1);
1222        }
[348]1223        strcpy(buf, out.c_str());
[193]1224}
1225
[1227]1226f4_Node* f4_Node::duplicate()
[193]1227{
[1227]1228        f4_Node *copy;
1229        copy = new f4_Node(*this);
[196]1230        copy->parent = NULL;  // set later
1231        copy->child = NULL;
1232        copy->child2 = NULL;
[1227]1233        if (child != NULL)
[671]1234        {
[196]1235                copy->child = child->duplicate();
1236                copy->child->parent = copy;
1237        }
[1227]1238        if (child2 != NULL)
[671]1239        {
[196]1240                copy->child2 = child2->duplicate();
1241                copy->child2->parent = copy;
1242        }
1243        return copy;
[193]1244}
1245
1246
[1227]1247void f4_Node::destroy()
[193]1248{
[196]1249        // children are destroyed (recursively) through the destructor
[1227]1250        if (child2 != NULL) delete child2;
1251        if (child != NULL) delete child;
[193]1252}
1253
[1234]1254// scan genotype string and build a tree
[193]1255// return >1 for error (errorpos)
[1234]1256int f4_processRecur(const char* genot, const int genot_len, int &pos_inout, f4_Node *parent)
[193]1257{
[1234]1258        static const char *all_modifiers_no_comma = F14_MODIFIERS; //I did experiments with added comma (see all_modifiers_for_simplify below) which had the advantage of commas not breaking sequences of modifiers, thus longer sequences of modifiers (including commas) could be simplified and genetic bloat was further reduced. But since we impose a limit on the number of modifier chars in GenoOperators::simplifiedModifiers(), it would also influence commas (e.g. no more than 8 commas per sequence), so in order to leave commas entirely unlimited let's exclude them from simplification. Note that currently 'X' or any other non-F14_MODIFIERS char also separates the sequence to be simplified, so if we wanted a really intensive simplification, it should occur during development, when we know precisely which genes influence each f4_Cell.
1259        //const char *Geno_f4::all_modifiers_for_simplify = F14_MODIFIERS ",\1"; //'\1' added to keep the number of chars even, avoid exceptions in logic and save the simple rule that the sequence is made of pairs (gene,contradictory gene), where a comma has no contradictory gene and \1 is unlikely to occur in the f4 genotype (and not allowed), so no risk it will cancel out a comma during simplification.
1260
1261
[1229]1262        f4_Node *par = parent;
[193]1263
[1234]1264        if (pos_inout >= genot_len)
1265                return genot_len + 1;
[1229]1266
[1234]1267        while (pos_inout < genot_len)
[671]1268        {
[1234]1269                const bool PRINT_PARSING_LOCATION = false;
1270                if (PRINT_PARSING_LOCATION)
1271                {
1272                        printf("%s\n", genot);
1273                        for (int i = 0; i < pos_inout; i++) printf(" ");
1274                        printf("^\n");
1275                }
[1230]1276                switch (genot[pos_inout])
[671]1277                {
[196]1278                case '<':
[760]1279                {
[1230]1280                        f4_Node *node = new f4_Node("<", par, pos_inout);
[1228]1281                        par = node;
[1230]1282                        pos_inout++; //move after '<'
[1234]1283                        int res = f4_processRecur(genot, genot_len, pos_inout, par);
[196]1284                        if (res) return res;
[1234]1285                        if (pos_inout < genot_len)
[671]1286                        {
[1234]1287                                res = f4_processRecur(genot, genot_len, pos_inout, par);
[196]1288                                if (res) return res;
1289                        }
[671]1290                        else // ran out
1291                        {
[1229]1292                                //MacKo 2023-04, more strict behavior: instead of silent repair (no visible effect to the user, genotype stays invalid but is interpreted and reported as valid), we now point out where the error is. For example <X> or <X><X or <X><N:N>
[1234]1293                                return genot_len + 1;
[1229]1294                                //old silent repair:
[1234]1295                                //node = new f4_Node(">", par, genot_len - 1);
[196]1296                        }
1297                        return 0;  // OK
[760]1298                }
[196]1299                case '>':
[760]1300                {
[1230]1301                        new f4_Node(">", par, pos_inout);
1302                        pos_inout++; //move after '>'
[196]1303                        return 0;  // OK
[760]1304                }
[196]1305                case '#':
[760]1306                {
[1231]1307                        // repetition marker
1308                        ExtValue reps;
1309                        const char* end = reps.parseNumber(genot + pos_inout + 1, ExtPType::TInt);
1310                        if (end == NULL)
1311                                return pos_inout + 1; //error
[1235]1312                        f4_Node *node = new f4_Node("#", par, pos_inout); //TODO here or elsewhere: gene mapping seems to map '#' but not the following number
[1231]1313                        node->reps = reps.getInt();
[196]1314                        // skip number
[1230]1315                        pos_inout += end - (genot + pos_inout);
[1234]1316                        int res = f4_processRecur(genot, genot_len, pos_inout, node);
[196]1317                        if (res) return res;
[1234]1318                        if (pos_inout < genot_len)
[671]1319                        {
[1234]1320                                res = f4_processRecur(genot, genot_len, pos_inout, node);
[196]1321                                if (res) return res;
1322                        }
[671]1323                        else // ran out
1324                        {
[1234]1325                                return genot_len + 1; //MacKo 2023-04: report an error, better to be more strict instead of a silent repair (genotype stays invalid but is interpreted and reported as valid) with non-obvious consequences?
[1235]1326                                //earlier approach - silently treating this problem (we don't ever see where the error is because it gets corrected in some way here, while parsing the genotype, and error location in the genotype is never reported):
1327                                //node = new f4_Node(">", par, genot_len - 1); // Maybe TODO: check if this was needed and if this was really the best repair operation; could happen many times in succession for some genotypes even though they were only a result of f4 operators, not manually created... and the operators should not generate invalid genotypes, right? Or maybe crossover does? Seemed like too many #n's for closing >'s; removing #n or adding > helped. Examples (remove trailing >'s to make invalid): /*4*/<X><N:N>X#1>> or /*4*/<X><N:N>X#1#2>>> or /*4*/<X><N:N>X#1#2#3>>>> etc.
1328                                // So operators somehow don't do it properly sometimes? But F4_ADD_REP adds '>'... Maybe the rule to always remove final trailing '>' was responsible? (now commented out). Since the proper syntax for # is #n ...repcode... > ...endcode..., perhaps endcode also needs '>' as the final delimiter. If we have many #'s in the genotype and the final >'s are missing, in the earlier approach we would keep adding them here as needed to ensure the syntax is valid. If we don't add '>' here silently, they must be explicitly added or else the genotype is invalid. BUT this earlier approach here only handled the situation where the genotype ended prematurely; what about cases where '>' may be needed as delimiters for # in the middle of the genotype? Or does # always concern all genes until the end, unless explicitly delimited earlier? Perhaps, if the '>' endcode delimiters are not present in the middle of the genotype, we don't know where they should be so the earlier approach would always add them only at the end of the genotype?
[196]1329                        }
1330                        return 0;  // OK
[760]1331                }
1332                case ' ':
1333                case '\n':
1334                case '\r':
1335                case '\t':
1336                {
1337                        // whitespace: ignore
[1230]1338                        pos_inout++;
[760]1339                        break;
1340                }
1341                case 'N':
1342                {
[1230]1343                        int forgenorange = pos_inout;
1344                        if (genot[pos_inout + 1] != ':')
1345                                return pos_inout + 1; //error
1346                        pos_inout += 2; //skipping "N:"
1347                        unsigned int neuroclass_begin = pos_inout;
1348                        char* neuroclass_end = (char*)genot + neuroclass_begin;
1349                        NeuroClass *neuclass = GenoOperators::parseNeuroClass(neuroclass_end, ModelEnum::SHAPETYPE_BALL_AND_STICK); //advances neuroclass_end
[1227]1350                        if (neuclass == NULL)
[1230]1351                                return pos_inout + 1; //error
1352                        pos_inout += neuroclass_end - genot - neuroclass_begin;
1353                        string neutype = string(genot + neuroclass_begin, genot + pos_inout);
[1228]1354                        f4_Node *node = new f4_Node(neutype, par, forgenorange);
1355                        node->neuclass = neuclass;
1356                        par = node;
[1227]1357                        // if it continues with a colon that determines a neuron parameter (e.g. N:N:+=: ), then let the switch case for colon handle this
[196]1358                        break;
[760]1359                }
[196]1360                case ':':
[760]1361                {
[196]1362                        // neuron parameter  +! -! += -= +/ or -/
[1227]1363                        // in the future this could be generalized to all neuron properties, for example N:|:power:0.6:range:1.4, or can even use '=' or ',' instead of ':' if no ambiguity
1364                        char prop_dir, prop_symbol, prop_end[2]; // prop_end is only to ensure that neuron parameter definition is completed
[1241]1365                        if (sscanf(genot + pos_inout, ":%c%c%1[:]", &prop_dir, &prop_symbol, prop_end) != 3)
[196]1366                                // error: incorrect format
[1230]1367                                return pos_inout + 1 + 1;
[1227]1368                        if (prop_dir != '-' && prop_dir != '+')
[1230]1369                                return pos_inout + 1 + 1; //error
[1227]1370                        switch (prop_symbol)
[671]1371                        {
[196]1372                        case '!':  case '=':  case '/':  break;
1373                        default:
[1230]1374                                return pos_inout + 1 + 1; //error
[196]1375                        }
[1230]1376                        f4_Node *node = new f4_Node(":", par, pos_inout);
[1228]1377                        node->prop_symbol = prop_symbol;
1378                        node->prop_increase = prop_dir == '+' ? true : false; // + or -
1379                        par = node;
[1230]1380                        pos_inout += 4; //skipping :ds:
[196]1381                        break;
[760]1382                }
1383                case '[':
1384                {
[1227]1385                        double weight = 0;
1386                        int relfrom;
[1230]1387                        const char *end = parseConnection(genot + pos_inout, relfrom, weight);
[760]1388                        if (end == NULL)
[1230]1389                                return pos_inout + 1; //error
[1227]1390
[1230]1391                        f4_Node *node = new f4_Node("[", par, pos_inout);
[1228]1392                        node->conn_from = relfrom;
1393                        node->conn_weight = weight;
1394                        par = node;
[1230]1395                        pos_inout += end - (genot + pos_inout);
[196]1396                        break;
[760]1397                }
[1234]1398                default: // 'X' and ',' and all modifiers and also invalid symbols - add a node. For symbols that are not valid in f4, the cell development process will give the error or repair
[760]1399                {
[1230]1400                        //printf("any regular character '%c'\n", genot[pos_inout]);
[1240]1401#define F4_SIMPLIFY_MODIFIERS //avoid long, redundant sequences like ...<X>llmlIilImmimiimmimifmfl<fifmmimilimmmiimiliffmfliIfififlliflimfliffififmiffmflllfflimlififfiiffifIr<r<...
[1234]1402#ifdef F4_SIMPLIFY_MODIFIERS
1403                        char *ptr = (char*)(genot + pos_inout);
[1241]1404                        string original = "";
1405                        while (GenoOperators::strchrn0(all_modifiers_no_comma, *ptr)) //only processes a section of chars known in all_modifiers_no_comma, other characters will exit the loop
[1234]1406                        {
[1241]1407                                original += *ptr;
[1234]1408                                GenoOperators::skipWS(++ptr); //advance and ignore whitespace
1409                        }
1410                        int advanced = ptr - (genot + pos_inout);
1411                        if (advanced > 0) //found modifiers
1412                        {
[1241]1413                                string simplified = GenoOperators::simplifiedModifiers(original);
[1234]1414                                // add a node for each char in "simplified"
1415                                for (size_t i = 0; i < simplified.length(); i++)
1416                                {
1417                                        int pos = GenoOperators::strchrn0(genot + pos_inout, simplified[i]) - genot; //unnecessarily finding the same char, if it occurrs multiple times in simplified
1418                                        f4_Node *node = new f4_Node(simplified[i], par, pos); //location is approximate. In the simplification process we don't trace where the origin(s) of the simplified[i] gene were. We provide 'pos' as the first occurrence of simplified[i] (for example, all 'L' will have the same location assigned, but at least this is where 'L' occurred in the genotype, so in case of any modification of a node (repair, removal, whatever... even mapping of genes) the indicated gene will be one of the responsible ones)
1419                                        par = node;
1420                                }
1421                                pos_inout += advanced;
1422                        }
1423                        else // genot[pos_inout] is a character not present in all_modifiers_no_comma, so treat it as a regular individual char just as it would be without simplification
1424                        {
1425                                f4_Node *node = new f4_Node(genot[pos_inout], par, pos_inout);
1426                                par = node;
1427                                pos_inout++;
1428                        }
1429#else
[1230]1430                        f4_Node *node = new f4_Node(genot[pos_inout], par, pos_inout);
[1228]1431                        par = node;
[1230]1432                        pos_inout++;
[1234]1433#endif // F4_SIMPLIFY_MODIFIERS
[196]1434                        break;
1435                }
[760]1436                }
[196]1437        }
[760]1438
[196]1439        // should end with a '>'
[1229]1440        if (par && par->name != ">")
[671]1441        {
[1234]1442                //happens when pos_inout == genot_len
[1230]1443                //return pos_inout; //MacKo 2023-04: could report an error instead of silent repair, but repair operators only work in Cells (i.e., after the f4_Node tree has been parsed without errors and Cells can start developing) so we don't want to make a fatal error because of missing '>' here. Also after conversions from Cells to text, trailing '>' is deliberately removed... and also the simplest genotype is officially X, not X>.
[1234]1444                new f4_Node('>', par, genot_len - 1);
[671]1445        }
1446
[1230]1447        return 0;  // OK
[193]1448}
1449
[1231]1450int f4_process(const char *genot, f4_Node *root)
1451{
1452        int pos = 0;
[1234]1453        int res = f4_processRecur(genot, (int)strlen(genot), pos, root);
[1231]1454        if (res > 0)
[1232]1455                return res; //parsing error
1456        else if (genot[pos] != 0)
1457                return pos + 1; //parsing OK but junk, unparsed genes left, for example /*4*/<X>N:N>whatever or /*4*/<X>X>>>
1458        else
1459                return 0; //parsing OK and parsed until the end
[1231]1460}
1461
[760]1462const char* parseConnection(const char *fragm, int& relfrom, double &weight)
1463{
1464        const char *parser = fragm;
1465        if (*parser != '[') return NULL;
1466        parser++;
1467        ExtValue val;
1468        parser = val.parseNumber(parser, ExtPType::TInt);
1469        if (parser == NULL) return NULL;
1470        relfrom = val.getInt();
1471        if (*parser != ':') return NULL;
1472        parser++;
1473        parser = val.parseNumber(parser, ExtPType::TDouble);
1474        if (parser == NULL) return NULL;
1475        weight = val.getDouble();
1476        if (*parser != ']') return NULL;
1477        parser++;
1478        return parser;
1479}
[193]1480
[1228]1481/*
1482f4_Node* f4_processTree(const char* geno)
[760]1483{
[1227]1484        f4_Node *root = new f4_Node();
[1228]1485        int res = f4_processRecur(geno, 0, root);
[196]1486        if (res) return NULL;
1487        //DB( printf("test f4  "); )
1488        DB(
[671]1489                if (root->child)
1490                {
[853]1491                        char* buf = (char*)malloc(300);
1492                        DB(printf("(%d) ", root->child->count());)
1493                                buf[0] = 0;
1494                        root->child->sprintAdj(buf);
1495                        DB(printf("%s\n", buf);)
1496                                free(buf);
[196]1497                }
1498        )
1499                return root->child;
[193]1500}
[1228]1501*/
Note: See TracBrowser for help on using the repository browser.