Ignore:
Timestamp:
05/02/23 01:36:15 (12 months ago)
Author:
Maciej Komosinski
Message:
  • Thanks to r1230, it is possible to detect (and repair=remove) junk trailing genes that are left after successful parsing (after last '>')
  • The validate() function may attempt to repair a genotype where earlier it would give up
  • Stricter parsing of the '#' gene
File:
1 edited

Legend:

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

    r1230 r1231  
    640640
    641641
    642 f4_Cells::f4_Cells(f4_Node *genome, int nrepair)
    643 {
    644         // create ancestor cell
     642f4_Cells::f4_Cells(f4_Node *genome, bool nrepair)
     643{
    645644        repair = nrepair;
    646645        errorcode = GENOPER_OK;
     
    650649        repair_insert = NULL;
    651650        tmpcel = NULL;
    652         f4rootnode = NULL;
     651
     652        // create ancestor cell
    653653        C[0] = new f4_Cell(this, 0, genome, genome, NULL, 0, GeneProps::standard_values);
    654654        cell_count = 1;
     
    656656
    657657
    658 f4_Cells::f4_Cells(SString & genome, int nrepair)
    659 {
    660         repair = nrepair;
    661         errorcode = GENOPER_OK;
    662         errorpos = -1;
    663         repair_remove = NULL;
    664         repair_parent = NULL;
    665         repair_insert = NULL;
    666         tmpcel = NULL;
    667         f4rootnode = NULL;
    668 
    669         // transform geno from string to nodes
    670         f4rootnode = new f4_Node();
    671         int _ = 0;
    672         int res = f4_processRecur(genome.c_str(), _, f4rootnode);
    673         if (res || (f4rootnode->childCount() != 1))
    674         {
    675                 errorcode = GENOPER_OPFAIL;
    676                 errorpos = -1;
    677         }
    678 
    679         // create ancestor cell
    680         C[0] = new f4_Cell(this, 0, f4rootnode->child, f4rootnode->child, NULL, 0, GeneProps::standard_values);
    681         cell_count = 1;
    682 }
    683658
    684659f4_Cells::~f4_Cells()
    685660{
    686661        // release cells
    687         int i;
    688662        if (cell_count)
    689663        {
    690                 for (i = cell_count - 1; i >= 0; i--)
     664                for (int i = cell_count - 1; i >= 0; i--)
    691665                        delete C[i];
    692666                cell_count = 0;
    693667        }
    694         if (f4rootnode)
    695                 delete f4rootnode;
    696668}
    697669
     
    11301102int f4_Node::childCount()
    11311103{
    1132         if (child != NULL)
    1133         {
    1134                 if (child2 != NULL) return 2;
    1135                 else return 1;
    1136         }
    1137         else
    1138         {
    1139                 if (child2 != NULL) return 1;
    1140                 else return 0;
    1141         }
     1104        return int(child != NULL) + int(child2 != NULL); //0, 1 or 2
    11421105}
    11431106
     
    12561219        len = out.length();
    12571220        if (len > 1)
    1258                 if (out[len - 1] == '>') { (out.directWrite())[len - 1] = 0; out.endWrite(); }; //Macko 2023-04 TODO "can be omitted", but should we remove it as a rule even in generated genotypes? see if I can somehow detect junk characters after top-level '>' ends properly: /*4*/<X>N:N>whatever
     1221                if (out[len - 1] == '>') { (out.directWrite())[len - 1] = 0; out.endWrite(); }; //Macko 2023-04 "can be omitted", but it is removed as a rule even in generated genotypes :)
    12591222        // copy back to string
    12601223        // if new is longer, reallocate buf
     
    13051268        while (pos_inout < (int)strlen(genot))
    13061269        {
    1307                 //#define PRINT_PARSING_LOCATION
     1270//#define PRINT_PARSING_LOCATION
    13081271#ifdef PRINT_PARSING_LOCATION
    13091272                printf("%s\n", genot);
     
    13421305                case '#':
    13431306                {
    1344                         // repetition marker, 1 by default
    1345                         ExtValue val;
    1346                         const char* end = val.parseNumber(genot + pos_inout + 1, ExtPType::TInt);
    1347                         //TODO end==NULL? -> error!
    1348                         int reps = (end == NULL) ? 1 : val.getInt();
     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
    13491312                        f4_Node *node = new f4_Node("#", par, pos_inout);
    1350                         node->reps = reps;
     1313                        node->reps = reps.getInt();
    13511314                        // skip number
    13521315                        pos_inout += end - (genot + pos_inout);
     
    14551418}
    14561419
     1420int f4_process(const char *genot, f4_Node *root)
     1421{
     1422        int pos = 0;
     1423        int res = f4_processRecur(genot, pos, root);
     1424        if (res > 0)
     1425                return res; //error
     1426        else if (genot[pos] == 0) //parsed until the end - OK!
     1427                return 0;
     1428        else return pos + 1; //junk, unparsed genes after successful parsing, for example /*4*/<X>N:N>whatever or /*4*/<X>X>>>
     1429}
     1430
    14571431const char* parseConnection(const char *fragm, int& relfrom, double &weight)
    14581432{
Note: See TracChangeset for help on using the changeset viewer.