Changeset 1237 for cpp/frams/genetics/f4


Ignore:
Timestamp:
05/07/23 02:40:10 (18 months ago)
Author:
Maciej Komosinski
Message:
  • Got rid of a redundant source of information: cells development stops when a cell sets an error code during its development, not when a function returns an error code
  • Fix whitespace repair being potentially overwritten by further repairs
Location:
cpp/frams/genetics/f4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f4/f4_general.cpp

    r1235 r1237  
    44
    55// Copyright (C) 1999,2000  Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL
    6 // 2018, Grzegorz Latosinski, added support for new API for neuron types and development checkpoints
     6// 2018, Grzegorz Latosinski, added development checkpoints and support for new API for neuron types
    77
    88#include "f4_general.h"
     
    145145}
    146146
    147 
    148 /* return codes:
    149         1 error at pos
    150         0  halt development (yield) for a cycle
    151         */
    152 int f4_Cell::oneStep()
     147void f4_Cell::oneStep()
    153148{
    154149        while (gcur != NULL)
     
    184179                                        // cannot fix
    185180                                        org->setError(gcur->pos);
    186                                         return 1;  // stop
     181                                        return;  // error code set -> stop further cells development
    187182                                }
    188183
     
    224219                                // adjustments for this cell
    225220                                gcur = gcur->child;
    226                                 // halt development
    227                                 return 0;
     221                                return;  // error code not set -> halt this development and yield to other cells to develop
    228222                        }
    229223                        case '>':
     
    271265                                                if (org->setRepairInsert(gcur->pos, gcur, insertnode)) // not in repair mode, release
    272266                                                        delete insertnode;
    273                                                 return 1;
     267                                                return;  // error code set -> stop further cells development
    274268                                        }
    275269                                        repeat.clear();
     
    280274                                                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...
    281275                                        gcur = NULL;
    282                                         return 0;
     276                                        return;  // done development
    283277                                }
    284278                        }
     
    294288                                        // fix: delete it
    295289                                        org->setRepairRemove(gcur->pos, gcur);
    296                                         return 1;  // stop
     290                                        return;  // error code set -> stop further cells development
    297291                                }
    298292                                repeat.push(repeat_ptr(gcur, gcur->reps));
     
    314308                                        // fix: delete it
    315309                                        org->setRepairRemove(gcur->pos, gcur);
    316                                         return 1;  // stop
     310                                        return;  // error code set -> stop further cells development
    317311                                }
    318312                                switch (name)
     
    345339                                        // fix: delete it
    346340                                        org->setRepairRemove(gcur->pos, gcur);
    347                                         return 1;  // stop
     341                                        return;  // error code set -> stop further cells development
    348342                                }
    349343                                P.executeModifier(name);
     
    359353                                        // fix: delete this node
    360354                                        org->setRepairRemove(gcur->pos, gcur);
    361                                         return 1;  // stop
     355                                        return;  // error code set -> stop further cells development
    362356                                }
    363357                                type = CELL_STICK;
    364358                                // fix dad commacount and own anglepos
    365                                 if (NULL != dadlink)
     359                                if (dadlink != NULL)
    366360                                {
    367361                                        dadlink->commacount++;
     
    370364                                // change of type halts developments, see comment at 'neuclasshandler' below
    371365                                gcur = gcur->child;
    372                                 return 0;
     366                                return;  // error code not set -> halt this development and yield to other cells to develop
    373367                        }
    374368                        case '[':
     
    380374                                        // fix: delete it
    381375                                        org->setRepairRemove(gcur->pos, gcur);
    382                                         return 1;  // stop
     376                                        return;  // error code set -> stop further cells development
    383377                                }
    384378                                // input [%d:%g]
     
    419413                                        // cannot fix
    420414                                        org->setError(gcur->pos);
    421                                         return 1;  // stop
     415                                        return;  // error code set -> stop further cells development
    422416                                }
    423417                                gcur = gcur->child;
     
    434428                                active = false; // next time when we reach wait_conn, we will not count ourselves as active (if we were the last active cell, we got a chance to continue development for one development step only)
    435429                                if (active_count > 0)
    436                                         return 0;  // there is at least one active (including ourselves), halt, try again
     430                                        // there is at least one active (including ourselves), halt, try again
     431                                        return;  // error code not set -> halt this development and yield to other cells to develop
    437432
    438433#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.
     
    450445                                        active = true; //not sure if needed, but let this cell have the chance to continue for as long as many children in the tree are left
    451446                                        gcur = gcur->child;
    452                                         return 0;
     447                                        return;  // error code not set -> halt this development and yield to other cells to develop
    453448                                }
    454449                                else
     
    456451                                        //org->setError(gcur->pos); //in case setRepairRemove() would not always produce reasonable results
    457452                                        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?)
    458                                         return 1;  // stop
     453                                        return;  // error code set -> stop further cells development
    459454                                }
    460455#else
     
    472467                                        // fix: delete it
    473468                                        org->setRepairRemove(gcur->pos, gcur);
    474                                         return 1;  // stop
     469                                        return;  // error code set -> stop further cells development
    475470                                }
    476471                                switch (gcur->prop_symbol)
     
    496491                                default:
    497492                                        org->setRepairRemove(gcur->pos, gcur);
    498                                         return 1;  // stop
     493                                        return;  // error code set -> stop further cells development
    499494                                }
    500495                                gcur = gcur->child;
     
    502497                        }
    503498                        case ' ':
    504                         {
    505                                 // space has no effect, should not occur
     499                        case '\t':
     500                        case '\n':
     501                        case '\r':
     502                        {
     503                                // whitespace has no effect, should not occur
    506504                                // fix: delete it
    507505                                org->setRepairRemove(gcur->pos, gcur);
    508                                 gcur = gcur->child;
    509                                 break;
     506                                return;  // error code set -> stop further cells development
    510507                        }
    511508                        default:
     
    515512                                logMessage("f4_Cell", "oneStep", LOG_ERROR, buf.c_str());
    516513                                org->setRepairRemove(gcur->pos, gcur);
    517                                 return 1;
     514                                return;  // error code set -> stop further cells development
    518515                        }
    519516                        }
     
    526523                                // fix: delete this node
    527524                                org->setRepairRemove(gcur->pos, gcur);
    528                                 return 1;  // stop
     525                                return;  // error code set -> stop further cells development
    529526                        }
    530527                        // error: if no previous
     
    533530                                // fix: delete it
    534531                                org->setRepairRemove(gcur->pos, gcur);
    535                                 return 1;  // stop
     532                                return;  // error code set -> stop further cells development
    536533                        }
    537534                        neuclass = gcur->neuclass;
     
    541538                        // to wait for other cells to turn to neurons before adding connections
    542539                        gcur = gcur->child;
    543                         return 0; //stop
     540                        return;  // error code not set -> halt this development and yield to other cells to develop
    544541                }
    545542        }
    546543        active = false;  // done
    547         return 0;
    548544}
    549545
     
    675671        for (int i = 0; i < old_cell_count; i++)
    676672        {
    677                 int cellstep_ret = C[i]->oneStep(); //keeps calling independently of C[i]->active
    678                 if (cellstep_ret > 0)
     673                C[i]->oneStep(); //keeps calling independently of C[i]->active
     674                if (errorcode != GENOPER_OK)
    679675                {
    680676                        // error
     
    685681        for (int i = 0; i < cell_count; i++) //we check all cells, including newly created ones
    686682                if (C[i]->active)
    687                         return true; //at least one cell is still active. TODO maybe the development should stop NOT because of the "active" field (there is this strange "yielding" state too), but by observing the progress of all cells and continuing the development while (errorcode==0 AND (gcur of at least one cell changed OR cell_count changed)) ? Also get rid of return 1/return 0, just observe error.
     683                        return true; //at least one cell is still active. TODO maybe the development should stop NOT because of the "active" field (there is this strange "yielding" state too), but by observing the progress of all cells and continuing the development while (errorcode==GENOPER_OK AND (gcur of at least one cell changed OR cell_count changed)) ?
    688684        return false;
    689685}
  • cpp/frams/genetics/f4/f4_general.h

    r1234 r1237  
    150150         * differentiation or modification is performed on the cell. If current node is
    151151         * creating a connection between two neuron nodes and the input node is not
    152          * yet developed, the simulation of the development of the current cell waits until
    153          * the input node is created. The onestep method is deployed for every cell
    154          * at least once. If one cell requires another one to develop, onestep
    155          * should be deployed again on this cell. This method, unlike genotype tree
    156          * creation, checks semantics. This means that this function will fail if:
     152         * yet developed, the simulation of the development of the current cell returns
     153         * to wait until the input node is created. The oneStep method is deployed for every cell
     154         * at least once. If one cell requires another one to develop, oneStep
     155         * should be deployed again on this cell.
     156         *
     157         * This method, unlike genotype tree creation, checks semantics. This means that
     158         * this function will fail (set error code) if:
    157159         *  - the cell differentiated as a stick will have branching node '<',
    158160         *  - the undifferentiated cell will have termination node '>' (end of cell development without differentiation),
     
    163165         *  - the neuron class is not valid.
    164166         *
    165          * @return 0 if development was successful, 1 if there was an error in genotype tree
    166          */
    167         int oneStep();
     167         * This function returns either because the development of this cell was completed,
     168         * or it was halted (yielding to other cells), or the error code was set in the f4_Cells object in the org attribute.
     169         */
     170        void oneStep();
    168171
    169172        /**
Note: See TracChangeset for help on using the changeset viewer.