Changeset 972 for cpp/frams/_demos


Ignore:
Timestamp:
07/03/20 00:32:23 (4 years ago)
Author:
Maciej Komosinski
Message:
  • separate "0" and "0s" formats (for SHAPE_BALL_AND_STICK and SHAPE_SOLIDS, respectively)
  • converting to format list (Geno::F0_FORMAT_LIST = "0,0s")
  • (optional) declaring Model as SHAPE_BALL_AND_STICK or SHAPE_SOLIDS (or SHAPE_UNKNOWN)
Location:
cpp/frams/_demos
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/_demos/f0_variants_test.cpp

    r955 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2016  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1212#include <common/loggers/loggertostdout.h>
    1313
    14 void save_as_f0(SString &gen,Model &m,bool omit_default_values)
     14void save_as_f0(SString &gen, Model &m, bool omit_default_values)
    1515{
    16 // copied from Model::makeGeno() (with small changes)
     16        // copied from Model::makeGeno() (with small changes)
    1717
    18 static Param modelparam(f0_model_paramtab);
    19 static Param partparam(f0_part_paramtab);
    20 static Param jointparam(f0_joint_paramtab);
    21 static Param neuroparam(f0_neuro_paramtab);
    22 static Param connparam(f0_neuroconn_paramtab);
     18        static Param modelparam(f0_model_paramtab);
     19        static Param partparam(f0_part_paramtab);
     20        static Param jointparam(f0_joint_paramtab);
     21        static Param neuroparam(f0_neuro_paramtab);
     22        static Param connparam(f0_neuroconn_paramtab);
    2323
    24 static Part defaultpart;
    25 static Joint defaultjoint;
    26 static Neuro defaultneuro;
    27 static Model defaultmodel;
    28 static NeuroConn defaultconn;
     24        static Part defaultpart;
     25        static Joint defaultjoint;
     26        static Neuro defaultneuro;
     27        static Model defaultmodel;
     28        static NeuroConn defaultconn;
    2929
    30 modelparam.select(&m);
    31 gen+="m:";
    32 modelparam.saveSingleLine(gen,omit_default_values ? &defaultmodel : NULL);
     30        modelparam.select(&m);
     31        gen += "m:";
     32        modelparam.saveSingleLine(gen, omit_default_values ? &defaultmodel : NULL);
    3333
    34 Part *p;
    35 Joint *j;
    36 Neuro *n;
     34        Part *p;
     35        Joint *j;
     36        Neuro *n;
    3737
    38 for (int i=0;p=(Part*)m.getPart(i);i++)
     38        for (int i = 0; p = (Part*)m.getPart(i); i++)
    3939        {
    40         partparam.select(p);
    41         gen+="p:";
    42         partparam.saveSingleLine(gen,omit_default_values ? &defaultpart : NULL);
     40                partparam.select(p);
     41                gen += "p:";
     42                partparam.saveSingleLine(gen, omit_default_values ? &defaultpart : NULL);
    4343        }
    44 for (int i=0;j=(Joint*)m.getJoint(i);i++)
     44        for (int i = 0; j = (Joint*)m.getJoint(i); i++)
    4545        {
    46         jointparam.select(j);
    47         jointparam.setParamTab(j->usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab);
    48         gen+="j:";
    49         jointparam.saveSingleLine(gen,omit_default_values ? &defaultjoint : NULL);
     46                jointparam.select(j);
     47                jointparam.setParamTab(j->usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab);
     48                gen += "j:";
     49                jointparam.saveSingleLine(gen, omit_default_values ? &defaultjoint : NULL);
    5050        }
    51 for (int i=0;n=(Neuro*)m.getNeuro(i);i++)
     51        for (int i = 0; n = (Neuro*)m.getNeuro(i); i++)
    5252        {
    53         neuroparam.select(n);
    54         gen+="n:";
    55         neuroparam.saveSingleLine(gen,omit_default_values ? &defaultneuro : NULL);
     53                neuroparam.select(n);
     54                gen += "n:";
     55                neuroparam.saveSingleLine(gen, omit_default_values ? &defaultneuro : NULL);
    5656        }
    57 for (int a=0;n=(Neuro*)m.getNeuro(a);a++)
     57        for (int a = 0; n = (Neuro*)m.getNeuro(a); a++)
    5858        { // inputs
    59         for (int b=0;b<n->getInputCount();b++)
     59                for (int b = 0; b < n->getInputCount(); b++)
    6060                {
    61                 double w;
    62                 NeuroConn nc;
    63                 Neuro* n2=n->getInput(b,w);
    64                 nc.n1_refno=n->refno; nc.n2_refno=n2->refno;
    65                 nc.weight=w;
    66                 nc.info=n->getInputInfo(b);
    67                 connparam.select(&nc);
    68                 gen+="c:";
    69                 connparam.saveSingleLine(gen,omit_default_values ? &defaultconn : NULL);
     61                        double w;
     62                        NeuroConn nc;
     63                        Neuro* n2 = n->getInput(b, w);
     64                        nc.n1_refno = n->refno; nc.n2_refno = n2->refno;
     65                        nc.weight = w;
     66                        nc.info = n->getInputInfo(b);
     67                        connparam.select(&nc);
     68                        gen += "c:";
     69                        connparam.saveSingleLine(gen, omit_default_values ? &defaultconn : NULL);
    7070                }
    7171        }
    7272}
    7373
    74 int main(int argc,char*argv[])
     74int main(int argc, char*argv[])
    7575{
    76 LoggerToStdout messages_to_stdout(LoggerBase::Enable);
     76        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
    7777
    78 //without converters the application would only handle "format 0" genotypes
    79 DefaultGenoConvManager gcm;
    80 gcm.addDefaultConverters();
    81 Geno::useConverters(&gcm);
     78        //without converters the application would only handle "format 0" genotypes
     79        DefaultGenoConvManager gcm;
     80        gcm.addDefaultConverters();
     81        Geno::useConverters(&gcm);
    8282
    83 Geno::Validators validators;
    84 Geno::useValidators(&validators);
    85 ModelGenoValidator model_validator;
    86 validators+=&model_validator; //This simple validator handles all cases where a converter for a particular format is available but there is no genetic operator. Converters may be less strict in detecting invalid genotypes but it is better than nothing
     83        Geno::Validators validators;
     84        Geno::useValidators(&validators);
     85        ModelGenoValidator model_validator;
     86        validators += &model_validator; //This simple validator handles all cases where a converter for a particular format is available but there is no genetic operator. Converters may be less strict in detecting invalid genotypes but it is better than nothing
    8787
    88 SString gen(argc>1?argv[1]:"X[|G:1.23]");
    89 if (!strcmp(gen.c_str(),"-"))
     88        SString gen(argc > 1 ? argv[1] : "X[|G:1.23]");
     89        if (!strcmp(gen.c_str(), "-"))
    9090        {
    91         gen=0;
    92         StdioFILEDontClose in(stdin);
    93         loadSString(&in,gen);
     91                gen = 0;
     92                StdioFILEDontClose in(stdin);
     93                loadSString(&in, gen);
    9494        }
    95 Geno g(gen);
    96 printf("\nSource genotype: '%s'\n",g.getGenes().c_str());
    97 printf("                  ( format %s %s)\n",
    98        g.getFormat().c_str(), g.getComment().c_str());
     95        Geno g(gen);
     96        printf("\nSource genotype: '%s'\n", g.getGenes().c_str());
     97        printf("                  ( format %s %s)\n",
     98                g.getFormat().c_str(), g.getComment().c_str());
    9999
    100 Model m(g);//.getConverted('0'));
     100        Model m(g, Model::SHAPE_UNKNOWN);//.getConverted('0'));
    101101
    102 if (!m.isValid())
     102        if (!m.isValid())
    103103        {
    104         printf("Cannot build Model from this genotype!\n");
    105         return 2;       
     104                printf("Cannot build Model from this genotype!\n");
     105                return 2;
    106106        }
    107107
    108 printf("\nthis example shows how to save a f0 genotype using low-level ParamInterface::saveSingleLine() calls\n");
     108        printf("\nthis example shows how to save a f0 genotype using low-level ParamInterface::saveSingleLine() calls\n");
    109109
    110 SString f0_skipping_defaults;
    111 SString f0_no_skipping_defaults;
     110        SString f0_skipping_defaults;
     111        SString f0_no_skipping_defaults;
    112112
    113 save_as_f0(f0_skipping_defaults,m,true);
    114 save_as_f0(f0_no_skipping_defaults,m,false);
     113        save_as_f0(f0_skipping_defaults, m, true);
     114        save_as_f0(f0_no_skipping_defaults, m, false);
    115115
    116 printf("\n==== with defdata (skips default values) ======\n%s\n",f0_skipping_defaults.c_str());
    117 printf("\n==== without defdata (saves all fields) ======\n%s\n",f0_no_skipping_defaults.c_str());
     116        printf("\n==== with defdata (skips default values) ======\n%s\n", f0_skipping_defaults.c_str());
     117        printf("\n==== without defdata (saves all fields) ======\n%s\n", f0_no_skipping_defaults.c_str());
    118118
    119 return 0;
     119        return 0;
    120120}
    121121
     
    123123
    124124Source genotype: 'X[|G:1.23]'
    125                   ( format 1 )
     125                                  ( format 1 )
    126126
    127127this example shows how to save a f0 genotype using low-level ParamInterface::saveSingleLine() calls
  • cpp/frams/_demos/full_props.cpp

    r534 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2016  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    4747*/
    4848
    49 int main(int argc,char*argv[])
     49int main(int argc, char*argv[])
    5050{
    51 StdioFILE::setStdio();//setup VirtFILE::Vstdin/out/err
    52 LoggerToStdout messages_to_stderr(LoggerBase::Enable | LoggerBase::DontBlock,VirtFILE::Vstderr); //errors -> stderr, don't interfere with stdout
     51        StdioFILE::setStdio();//setup VirtFILE::Vstdin/out/err
     52        LoggerToStdout messages_to_stderr(LoggerBase::Enable | LoggerBase::DontBlock, VirtFILE::Vstderr); //errors -> stderr, don't interfere with stdout
    5353
    54 PreconfiguredGenetics genetics;
     54        PreconfiguredGenetics genetics;
    5555
    56 bool reverse=false;
    57 char* gen_arg=0;
    58 for(int i=1;i<argc;i++)
     56        bool reverse = false;
     57        char* gen_arg = 0;
     58        for (int i = 1; i < argc; i++)
    5959        {
    60         char* ar=argv[i];
    61         if (ar[0]=='-')
    62                 switch(ar[1])
     60                char* ar = argv[i];
     61                if (ar[0] == '-')
     62                        switch (ar[1])
    6363                        {
    64                         case 'r': reverse=true; break;
     64                        case 'r': reverse = true; break;
    6565                        case 'h': puts("usage: full_props [-r[everse]] [genotype_or_stdin]\n"); break;
    6666                        }
     67                else
     68                        if (!gen_arg)
     69                                gen_arg = ar;
     70        }
     71        SString gen;
     72        if (gen_arg)
     73                gen = gen_arg;
    6774        else
    68                 if (!gen_arg)
    69                         gen_arg=ar;
    70         }
    71 SString gen;
    72 if (gen_arg)
    73         gen=gen_arg;
    74 else
    75         loadSString(VirtFILE::Vstdin,gen);
    76 Geno g(gen);
    77 Model m(g);
     75                loadSString(VirtFILE::Vstdin, gen);
     76        Geno g(gen);
     77        Model m(g, Model::SHAPE_UNKNOWN);
    7878
    79 if (!m.isValid())
     79        if (!m.isValid())
    8080        {
    81         logPrintf("","full_props",LOG_ERROR,"Cannot build Model from the supplied genotype\n");
    82         return 2;       
     81                logPrintf("", "full_props", LOG_ERROR, "Cannot build Model from the supplied genotype\n");
     82                return 2;
    8383        }
    8484
    85 m.open();
    86 for(int i=0;i<m.getNeuroCount();i++)
     85        m.open();
     86        for (int i = 0; i < m.getNeuroCount(); i++)
    8787        {
    88         Neuro *n=m.getNeuro(i);
    89         SyntParam p=n->classProperties(reverse);
    90         p.update();// ...so everyone reading the source can recognize that p was created to update the neuro d field
    91         // but actually, calling update() here is not necessary, because ~SyntParam() would do it anyway
     88                Neuro *n = m.getNeuro(i);
     89                SyntParam p = n->classProperties(reverse);
     90                p.update();// ...so everyone reading the source can recognize that p was created to update the neuro d field
     91                // but actually, calling update() here is not necessary, because ~SyntParam() would do it anyway
    9292        }
    93 m.close();
     93        m.close();
    9494
    95 // normal f0 (omitting default values) would be retrieved using m.getF0Geno()
    96 // the following form allows for more control:
    97 Geno f0_g;
    98 m.makeGeno(f0_g,NULL,reverse);//third arg is "handle_defaults" == whether f0 should omit default property values
    99 puts(f0_g.getGenesAndFormat().c_str());
     95        // normal f0 (omitting default values) would be retrieved using m.getF0Geno()
     96        // the following form allows for more control:
     97        Geno f0_g;
     98        m.makeGeno(f0_g, NULL, reverse);//third arg is "handle_defaults" == whether f0 should omit default property values
     99        puts(f0_g.getGenesAndFormat().c_str());
    100100
    101 return 0;
     101        return 0;
    102102}
    103103
  • cpp/frams/_demos/genoconv_test.cpp

    r955 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1616 */
    1717
    18 /// Sample Geno converter not using Model class.
    19 /// (This converter generates the same output for each input).
    20 /// Such a converter is responsible for doing valid f0 (or other format) output and storing temporary data.
     18 /// Sample Geno converter not using Model class.
     19 /// (This converter generates the same output for each input).
     20 /// Such a converter is responsible for doing valid f0 (or other format) output and storing temporary data.
    2121class GenoConv_Test : public GenoConverter
    2222{
     
    8484        for (t = src; *t; t++)
    8585        {
    86                 if (insideneuron&&*t == ']') insideneuron = 0;
     86                if (insideneuron && *t == ']') insideneuron = 0;
    8787                if (*t == '[') insideneuron = 1;
    8888                if ((!insideneuron) && isdigit(*t) && t[1])
     
    9393                                dst += *t;
    9494                        if (map) // fill in the map only if requested
    95                                 map->add(t - src, t - src, dst.len() - n, dst.len() - 1);
     95                                map->add(t - src, t - src, dst.length() - n, dst.length() - 1);
    9696                        // meaning: source character (t-src) becomes (dst.len()-n ... dst.len()-1)
    9797                }
     
    100100                        dst += *t;
    101101                        if (map)
    102                                 map->add(t - src, t - src, dst.len() - 1, dst.len() - 1);
     102                                map->add(t - src, t - src, dst.length() - 1, dst.length() - 1);
    103103                        // meaning: map single to single character: (t-src) into (dst.len()-1)
    104104                }
     
    113113{
    114114        printf("Genotype:\n%s\nFormat: %s\nValid: %s\nComment: %s\n",
    115                g.getGenes().c_str(), g.getFormat().c_str(), g.isValid() ? "yes" : "no", g.getComment().c_str());
     115                g.getGenes().c_str(), g.getFormat().c_str(), g.isValid() ? "yes" : "no", g.getComment().c_str());
    116116}
    117117
     
    167167        if (using_checkpoints)
    168168        { // using Model with checkpoints
    169                 Model m1(g2, false, true);//true=using_checkpoints
     169                Model m1(g2, Model::SHAPE_UNKNOWN, false, true);//true=using_checkpoints
    170170                printf("\nModel built from the converted f%s genotype has %d checkpoints\n", g2.getFormat().c_str(), m1.getCheckpointCount());
    171                 Model m2(g1, false, true);//true=using_checkpoints
     171                Model m2(g1, Model::SHAPE_UNKNOWN, false, true);//true=using_checkpoints
    172172                printf("Model built from the source f%s genotype has %d checkpoints\n", g1.getFormat().c_str(), m2.getCheckpointCount());
    173173                // accessing individual checkpoint models (if available)
     
    195195                }
    196196
    197                 Model mod1(g1, 1);
     197                Model mod1(g1, Model::SHAPE_UNKNOWN, 1);
    198198                printf("\nModel map for f%s genotype:\n", g1.getFormat().c_str());
    199199                ModelDisplayMap dm1(mod1);
     
    202202                mod1combined.addCombined(mod1.getMap(), dm1.getMap());
    203203                mod1combined.print();
    204                 Model mod2(g2, 1);
     204                Model mod2(g2, Model::SHAPE_UNKNOWN, 1);
    205205                printf("\nModel map for f%s genotype:\n", g2.getFormat().c_str());
    206206                ModelDisplayMap dm2(mod2);
  • cpp/frams/_demos/genomanipulation.cpp

    r955 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    210210                        else
    211211                                printf("(This class does not have its own properties\n"
    212                                 " - Neuro::classProperties().getPropCount()==0)\n");
     212                                        " - Neuro::classProperties().getPropCount()==0)\n");
    213213                }
    214214        }
     
    230230        {
    231231                SyntParam p = n->classProperties();
    232                 if (p.getPropCount()>0)
     232                if (p.getPropCount() > 0)
    233233                {
    234234                        printProperties(p);
     
    284284                g.getFormat().c_str(), g.getComment().c_str());
    285285
    286         Model m(g);//.getConverted('0'));
     286        Model m(g, Model::SHAPE_UNKNOWN);//.getConverted('0'));
    287287
    288288        if (!m.isValid())
  • cpp/frams/_demos/geometry/geometrytestutils.cpp

    r737 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2017  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2424        {
    2525                count++;
    26                 totalSize += genotype->genotype.len();
    27 
    28                 fprintf(stderr, "%d. (%6d chars) %s\n", count, genotype->genotype.len(),
     26                totalSize += genotype->genotype.length();
     27
     28                fprintf(stderr, "%d. (%6d chars) %s\n", count, genotype->genotype.length(),
    2929                        genotype->name.c_str());
    3030        }
     
    6363                if ((genoIndex == count) || (strcmp(genotype->name.c_str(), genoName) == 0))
    6464                {
    65                         Model model(genotype->genotype);
     65                        Model model(genotype->genotype, Model::SHAPE_UNKNOWN);
    6666
    6767                        if (!model.isValid())
  • cpp/frams/_demos/multiline_f0_test.cpp

    r955 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1313#include <common/virtfile/stringfile.h>
    1414
    15 int main(int argc,char*argv[])
     15int main(int argc, char*argv[])
    1616{
    17 LoggerToStdout messages_to_stdout(LoggerBase::Enable);
    18 PreconfiguredGenetics genetics;
     17        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
     18        PreconfiguredGenetics genetics;
    1919
    20 SString gen(argc>1?argv[1]:"X[|G:1.23]");
    21 if (!strcmp(gen.c_str(),"-"))
     20        SString gen(argc > 1 ? argv[1] : "X[|G:1.23]");
     21        if (!strcmp(gen.c_str(), "-"))
    2222        {
    23         gen=0;
    24         StdioFILEDontClose in(stdin);
    25         loadSString(&in,gen);
     23                gen = 0;
     24                StdioFILEDontClose in(stdin);
     25                loadSString(&in, gen);
    2626        }
    27 Geno g(gen);
    28 printf("\nSource genotype: '%s'\n",g.getGenes().c_str());
    29 printf("                  ( format %s %s)\n",
    30        g.getFormat().c_str(), g.getComment().c_str());
     27        Geno g(gen);
     28        printf("\nSource genotype: '%s'\n", g.getGenes().c_str());
     29        printf("                  ( format %s %s)\n",
     30                g.getFormat().c_str(), g.getComment().c_str());
    3131
    32 Model m(g);//.getConverted('0'));
     32        Model m(g, Model::SHAPE_UNKNOWN);//.getConverted('0'));
    3333
    34 if (!m.isValid())
     34        if (!m.isValid())
    3535        {
    36         printf("Cannot build Model from this genotype!\n");
    37         return 2;       
     36                printf("Cannot build Model from this genotype!\n");
     37                return 2;
    3838        }
    39 printf("Converted to f0:\n%s\n",m.getF0Geno().getGenes().c_str());
     39        printf("Converted to f0:\n%s\n", m.getF0Geno().getGenes().c_str());
    4040
    41 printf("\nusing Param::saveMultiLine() to create the \"expanded\" form of the f0 genotype...\n(MultiParamLoader should be able to load this)");
     41        printf("\nusing Param::saveMultiLine() to create the \"expanded\" form of the f0 genotype...\n(MultiParamLoader should be able to load this)");
    4242
    43 StringFILE2 f;
     43        StringFILE2 f;
    4444
    45 static Param modelparam(f0_model_paramtab);
    46 static Param partparam(f0_part_paramtab);
    47 static Param jointparam(f0_joint_paramtab);
    48 static Param neuroparam(f0_neuro_paramtab);
    49 static Param connparam(f0_neuroconn_paramtab);
     45        static Param modelparam(f0_model_paramtab);
     46        static Param partparam(f0_part_paramtab);
     47        static Param jointparam(f0_joint_paramtab);
     48        static Param neuroparam(f0_neuro_paramtab);
     49        static Param connparam(f0_neuroconn_paramtab);
    5050
    51 modelparam.select(&m);
    52 modelparam.saveMultiLine(&f,"m");
     51        modelparam.select(&m);
     52        modelparam.saveMultiLine(&f, "m");
    5353
    54 Part *p;
    55 Joint *j;
    56 Neuro *n;
     54        Part *p;
     55        Joint *j;
     56        Neuro *n;
    5757
    58 for (int i=0;p=(Part*)m.getPart(i);i++)
     58        for (int i = 0; p = (Part*)m.getPart(i); i++)
    5959        {
    60         partparam.select(p);
    61         partparam.saveMultiLine(&f,"p");
     60                partparam.select(p);
     61                partparam.saveMultiLine(&f, "p");
    6262        }
    63 for (int i=0;j=(Joint*)m.getJoint(i);i++)
     63        for (int i = 0; j = (Joint*)m.getJoint(i); i++)
    6464        {
    65         jointparam.select(j);
    66         jointparam.setParamTab(j->usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab);
    67         jointparam.saveMultiLine(&f,"j");
     65                jointparam.select(j);
     66                jointparam.setParamTab(j->usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab);
     67                jointparam.saveMultiLine(&f, "j");
    6868        }
    69 for (int i=0;n=(Neuro*)m.getNeuro(i);i++)
     69        for (int i = 0; n = (Neuro*)m.getNeuro(i); i++)
    7070        {
    71         neuroparam.select(n);
    72         neuroparam.saveMultiLine(&f,"n");
     71                neuroparam.select(n);
     72                neuroparam.saveMultiLine(&f, "n");
    7373        }
    74 for (int a=0;n=(Neuro*)m.getNeuro(a);a++)
     74        for (int a = 0; n = (Neuro*)m.getNeuro(a); a++)
    7575        { // inputs
    76         for (int b=0;b<n->getInputCount();b++)
     76                for (int b = 0; b < n->getInputCount(); b++)
    7777                {
    78                 double w;
    79                 NeuroConn nc;
    80                 Neuro* n2=n->getInput(b,w);
    81                 nc.n1_refno=n->refno; nc.n2_refno=n2->refno;
    82                 nc.weight=w;
    83                 nc.info=n->getInputInfo(b);
    84                 connparam.select(&nc);
    85                 connparam.saveMultiLine(&f,"c");
     78                        double w;
     79                        NeuroConn nc;
     80                        Neuro* n2 = n->getInput(b, w);
     81                        nc.n1_refno = n->refno; nc.n2_refno = n2->refno;
     82                        nc.weight = w;
     83                        nc.info = n->getInputInfo(b);
     84                        connparam.select(&nc);
     85                        connparam.saveMultiLine(&f, "c");
    8686                }
    8787        }
    8888
    89 printf("\n============================\n%s\n",f.getString().c_str());
     89        printf("\n============================\n%s\n", f.getString().c_str());
    9090
    91 return 0;
     91        return 0;
    9292}
    9393
     
    9595
    9696Source genotype: 'X[|G:1.23]'
    97                   ( format 1 )
     97                                  ( format 1 )
    9898Converted to f0:
    9999p:
  • cpp/frams/_demos/neuro_layout_test.cpp

    r408 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2121
    2222// stl is fun? ;-) ForwardIterator implementation for element coordinates (required by min_element/max_element)
    23 template <int MEMBER> struct NNIter: public std::iterator<std::forward_iterator_tag,int> //MEMBER: 0..3=x/y/w/h
     23template <int MEMBER> struct NNIter : public std::iterator<std::forward_iterator_tag, int> //MEMBER: 0..3=x/y/w/h
    2424{
    25 NNLayoutState *nn; int index;
    26 NNIter() {}
    27 NNIter(NNLayoutState *_nn, int _index):nn(_nn),index(_index) {}
    28 int operator*() {return nn->GetXYWH(index)[MEMBER];}
    29 NNIter& operator++() {index++; return *this;}
    30 bool operator!=(const NNIter& it) {return index!=it.index;}
    31 bool operator==(const NNIter& it) {return index==it.index;}
     25        NNLayoutState *nn; int index;
     26        NNIter() {}
     27        NNIter(NNLayoutState *_nn, int _index) :nn(_nn), index(_index) {}
     28        int operator*() { return nn->GetXYWH(index)[MEMBER]; }
     29        NNIter& operator++() { index++; return *this; }
     30        bool operator!=(const NNIter& it) { return index != it.index; }
     31        bool operator==(const NNIter& it) { return index == it.index; }
    3232
    33 static NNIter begin(NNLayoutState *_nn) {return NNIter(_nn,0);}
    34 static NNIter end(NNLayoutState *_nn) {return NNIter(_nn,_nn->GetElements());}
     33        static NNIter begin(NNLayoutState *_nn) { return NNIter(_nn, 0); }
     34        static NNIter end(NNLayoutState *_nn) { return NNIter(_nn, _nn->GetElements()); }
    3535};
    3636
    3737class Screen
    3838{
    39 int min_x,max_x,min_y,max_y,scale_x,scale_y;
    40 int rows,columns;
    41 char* screen;
     39        int min_x, max_x, min_y, max_y, scale_x, scale_y;
     40        int rows, columns;
     41        char* screen;
    4242
    4343public:
    4444
    45 Screen(int _min_x,int _max_x,int _min_y,int _max_y,int _scale_x,int _scale_y)
    46         :min_x(_min_x),max_x(_max_x),min_y(_min_y),max_y(_max_y),scale_x(_scale_x),scale_y(_scale_y)
     45        Screen(int _min_x, int _max_x, int _min_y, int _max_y, int _scale_x, int _scale_y)
     46                :min_x(_min_x), max_x(_max_x), min_y(_min_y), max_y(_max_y), scale_x(_scale_x), scale_y(_scale_y)
     47        {
     48                columns = (max_x - min_x + scale_x - 1) / scale_x;
     49                rows = (max_y - min_y + scale_y - 1) / scale_y;
     50                screen = new char[rows * columns];
     51                memset(screen, ' ', rows * columns);
     52        }
     53
     54        ~Screen()
     55        {
     56                delete[] screen;
     57        }
     58
     59        void put(int x, int y, const char *str)
     60        {
     61                x = (x - min_x) / scale_x;
     62                y = (y - min_y) / scale_y;
     63                if (x < 0) return;
     64                if (y < 0) return;
     65                if (y >= rows) return;
     66                for (; *str; str++, x++)
    4767                {
    48                 columns=(max_x-min_x+scale_x-1)/scale_x;
    49                 rows=(max_y-min_y+scale_y-1)/scale_y;
    50                 screen=new char[rows*columns];
    51                 memset(screen,' ',rows*columns);
    52                 }
    53 
    54 ~Screen()
    55                 {
    56                 delete[] screen;
    57                 }
    58 
    59 void put(int x,int y,const char *str)
    60         {
    61         x=(x-min_x)/scale_x;
    62         y=(y-min_y)/scale_y;
    63         if (x<0) return;
    64         if (y<0) return;
    65         if (y>=rows) return;
    66         for(;*str;str++,x++)
    67                 {
    68                 if (x>=columns) return;
    69                 screen[columns*y+x]=*str;
     68                        if (x >= columns) return;
     69                        screen[columns * y + x] = *str;
    7070                }
    7171        }
    7272
    73 void print()
     73        void print()
    7474        {
    75         for(int y=0;y<rows;y++)
     75                for (int y = 0; y < rows; y++)
    7676                {
    77                 fwrite(&screen[columns*y],1,columns,stdout);
    78                 printf("\n");
     77                        fwrite(&screen[columns * y], 1, columns, stdout);
     78                        printf("\n");
    7979                }
    8080        }
    8181};
    8282
    83 int main(int argc,char*argv[])
     83int main(int argc, char*argv[])
    8484{
    85 LoggerToStdout messages_to_stdout(LoggerBase::Enable);
    86 PreconfiguredGenetics genetics;
     85        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
     86        PreconfiguredGenetics genetics;
    8787
    88 if (argc<=1)
     88        if (argc <= 1)
    8989        {
    9090                puts("Parameters:\n"
    91                      " 1. Genotype (or - character indicating the genotype will be read from stdin)\n"
    92                      " 2. (Optional) layout type (the only useful layout is 2, which is the default, see nn_simple_layout.cpp");
    93           return 10;
     91                        " 1. Genotype (or - character indicating the genotype will be read from stdin)\n"
     92                        " 2. (Optional) layout type (the only useful layout is 2, which is the default, see nn_simple_layout.cpp");
     93                return 10;
    9494        }
    95 SString gen(argv[1]);
    96 if (!strcmp(gen.c_str(),"-"))
     95        SString gen(argv[1]);
     96        if (!strcmp(gen.c_str(), "-"))
    9797        {
    98         gen=0;
    99         StdioFILEDontClose in(stdin);
    100         loadSString(&in,gen);
     98                gen = 0;
     99                StdioFILEDontClose in(stdin);
     100                loadSString(&in, gen);
    101101        }
    102 int layout_type=2;
    103 if (argc>2) layout_type=atol(argv[2]);
    104 Geno g(gen);
    105 if (!g.isValid()) {puts("invalid genotype");return 5;}
    106 Model m(g);
    107 if (!m.getNeuroCount()) {puts("no neural network");return 1;}
    108 printf("%d neurons,",m.getNeuroCount());
     102        int layout_type = 2;
     103        if (argc > 2) layout_type = atol(argv[2]);
     104        Geno g(gen);
     105        if (!g.isValid()) { puts("invalid genotype"); return 5; }
     106        Model m(g, Model::SHAPE_UNKNOWN);
     107        if (!m.getNeuroCount()) { puts("no neural network"); return 1; }
     108        printf("%d neurons,", m.getNeuroCount());
    109109
    110 NNLayoutState_Model nn_layout(&m);
    111 struct NNLayoutFunction &nnfun=nn_layout_functions[layout_type];
    112 printf(" using layout type=%d (%s)\n",layout_type,nnfun.name);
    113 nnfun.doLayout(&nn_layout);
     110        NNLayoutState_Model nn_layout(&m);
     111        struct NNLayoutFunction &nnfun = nn_layout_functions[layout_type];
     112        printf(" using layout type=%d (%s)\n", layout_type, nnfun.name);
     113        nnfun.doLayout(&nn_layout);
    114114
    115 for(int i=0;i<nn_layout.GetElements();i++)
     115        for (int i = 0; i < nn_layout.GetElements(); i++)
    116116        {
    117         int *xywh=nn_layout.GetXYWH(i);
    118         printf("#%-3d %s\t%d,%d\t%dx%d\n",i,m.getNeuro(i)->getClassName().c_str(),
    119                xywh[0],xywh[1],xywh[2],xywh[3]);
     117                int *xywh = nn_layout.GetXYWH(i);
     118                printf("#%-3d %s\t%d,%d\t%dx%d\n", i, m.getNeuro(i)->getClassName().c_str(),
     119                        xywh[0], xywh[1], xywh[2], xywh[3]);
    120120        }
    121121
    122 Screen screen(*std::min_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))-30,
    123               *std::max_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))+70,
    124               *std::min_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout)),
    125               *std::max_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout))+30,
    126               10,35);
     122        Screen screen(*std::min_element(NNIter<0>::begin(&nn_layout), NNIter<0>::end(&nn_layout)) - 30,
     123                *std::max_element(NNIter<0>::begin(&nn_layout), NNIter<0>::end(&nn_layout)) + 70,
     124                *std::min_element(NNIter<1>::begin(&nn_layout), NNIter<1>::end(&nn_layout)),
     125                *std::max_element(NNIter<1>::begin(&nn_layout), NNIter<1>::end(&nn_layout)) + 30,
     126                10, 35);
    127127
    128 printf("===========================================\n");
    129 for(int i=0;i<nn_layout.GetElements();i++)
     128        printf("===========================================\n");
     129        for (int i = 0; i < nn_layout.GetElements(); i++)
    130130        {
    131         int *xywh=nn_layout.GetXYWH(i);
    132         SString label=SString::sprintf("%d:%s",i,m.getNeuro(i)->getClassName().c_str());
    133         screen.put(xywh[0],xywh[1],label.c_str());
     131                int *xywh = nn_layout.GetXYWH(i);
     132                SString label = SString::sprintf("%d:%s", i, m.getNeuro(i)->getClassName().c_str());
     133                screen.put(xywh[0], xywh[1], label.c_str());
    134134        }
    135 screen.print();
    136 printf("===========================================\n");
     135        screen.print();
     136        printf("===========================================\n");
    137137
    138138}
  • cpp/frams/_demos/neuro_test.cpp

    r391 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1818#ifndef SDK_WITHOUT_FRAMS
    1919#include <frams/mech/creatmechobj.h>
    20 int CreatMechObject::modeltags_id=0;
    21 int CreatMechObject::mechtags_id=0;
     20int CreatMechObject::modeltags_id = 0;
     21int CreatMechObject::mechtags_id = 0;
    2222#endif
    2323
    24 ParamEntry creature_paramtab[]={0};
     24ParamEntry creature_paramtab[] = { 0 };
    2525
    2626#ifdef VEYETEST
     
    3333#define LEARNINGSTEPS 50
    3434
    35 void veyeStep(Model &m,int step)
     35void veyeStep(Model &m, int step)
    3636{
    37   static float angle=0;
     37        static float angle = 0;
    3838
    39   NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->setState(step>=LEARNINGSTEPS); //0 (learning) or 1 (normal)
     39        NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->setState(step >= LEARNINGSTEPS); //0 (learning) or 1 (normal)
    4040
    41         NeuroImpl *ni=NeuroNetImpl::getImpl(m.getNeuro(N_VEye));
    42   ((NI_VectorEye*)ni)->relpos.y=0;
    43   ((NI_VectorEye*)ni)->relpos.z=0;
    44   if (NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->getNewState()<0.5)
    45   { //learning
    46     ((NI_VectorEye*)ni)->relpos.x=5.0*sin(2*M_PI*step/LEARNINGSTEPS);
    47   }
    48   else
    49   { //VMotor controls location of VEye
    50     angle+=NeuroNetImpl::getImpl(m.getNeuro(N_VMotor))->getState();
    51     angle=fmod((double)angle,M_PI*2.0);
    52     ((NI_VectorEye*)ni)->relpos.x=5*sin(angle);
    53   }
     41        NeuroImpl *ni = NeuroNetImpl::getImpl(m.getNeuro(N_VEye));
     42        ((NI_VectorEye*)ni)->relpos.y = 0;
     43        ((NI_VectorEye*)ni)->relpos.z = 0;
     44        if (NeuroNetImpl::getImpl(m.getNeuro(N_Mode))->getNewState() < 0.5)
     45        { //learning
     46                ((NI_VectorEye*)ni)->relpos.x = 5.0 * sin(2 * M_PI * step / LEARNINGSTEPS);
     47        }
     48        else
     49        { //VMotor controls location of VEye
     50                angle += NeuroNetImpl::getImpl(m.getNeuro(N_VMotor))->getState();
     51                angle = fmod((double)angle, M_PI * 2.0);
     52                ((NI_VectorEye*)ni)->relpos.x = 5 * sin(angle);
     53        }
    5454
    55   NeuroNetImpl::getImpl(m.getNeuro(N_Fitness))->setState(angle); //wymaga poprawy
    56   //oraz trzeba przemyslec kolejnosc get/set'ow neuronow zeby sygnal sie dobrze propagowal.
     55        NeuroNetImpl::getImpl(m.getNeuro(N_Fitness))->setState(angle); //wymaga poprawy
     56        //oraz trzeba przemyslec kolejnosc get/set'ow neuronow zeby sygnal sie dobrze propagowal.
    5757}
    5858#endif
    5959
    60 int main(int argc,char*argv[])
     60int main(int argc, char*argv[])
    6161{
    62 LoggerToStdout messages_to_stdout(LoggerBase::Enable);
    63 PreconfiguredGenetics genetics;
     62        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
     63        PreconfiguredGenetics genetics;
    6464
    65 if (argc<=1)
     65        if (argc <= 1)
    6666        {
    6767                puts("Parameters: <genotype> [number of simulation steps]");
    68           return 10;
     68                return 10;
    6969        }
    70 SString gen(argv[1]);
    71 if (!strcmp(gen.c_str(),"-"))
     70        SString gen(argv[1]);
     71        if (!strcmp(gen.c_str(), "-"))
    7272        {
    73         gen=0;
    74         StdioFILEDontClose in(stdin);
    75         loadSString(&in,gen);
     73                gen = 0;
     74                StdioFILEDontClose in(stdin);
     75                loadSString(&in, gen);
    7676        }
    77 Geno g(gen);
    78 if (!g.isValid()) {puts("invalid genotype");return 5;}
    79 Model m(g);
    80 if (!m.getNeuroCount()) {puts("no neural network");return 1;}
    81 printf("%d neurons,",m.getNeuroCount());
    82 NeuroFactory neurofac;
    83 neurofac.setStandardImplementation();
    84 NeuroNetConfig nn_config(&neurofac);
    85 NeuroNetImpl *nn=new NeuroNetImpl(m,nn_config);
    86 int i; Neuro *n;
    87 if (!nn->getErrorCount()) printf(" no errors\n");
    88 else
     77        Geno g(gen);
     78        if (!g.isValid()) { puts("invalid genotype"); return 5; }
     79        Model m(g, Model::SHAPE_UNKNOWN);
     80        if (!m.getNeuroCount()) { puts("no neural network"); return 1; }
     81        printf("%d neurons,", m.getNeuroCount());
     82        NeuroFactory neurofac;
     83        neurofac.setStandardImplementation();
     84        NeuroNetConfig nn_config(&neurofac);
     85        NeuroNetImpl *nn = new NeuroNetImpl(m, nn_config);
     86        int i; Neuro *n;
     87        if (!nn->getErrorCount()) printf(" no errors\n");
     88        else
    8989        {
    90         printf(" %d errors:",nn->getErrorCount());
    91         int no_impl=0; SString no_impl_names;
    92         int init_err=0; SString init_err_names;
    93         for(i=0;i<m.getNeuroCount();i++)
     90                printf(" %d errors:", nn->getErrorCount());
     91                int no_impl = 0; SString no_impl_names;
     92                int init_err = 0; SString init_err_names;
     93                for (i = 0; i < m.getNeuroCount(); i++)
    9494                {
    95                 n=m.getNeuro(i);
    96                 NeuroImpl *ni=NeuroNetImpl::getImpl(n);
    97                 if (!ni)
     95                        n = m.getNeuro(i);
     96                        NeuroImpl *ni = NeuroNetImpl::getImpl(n);
     97                        if (!ni)
    9898                        {
    99                         if (no_impl) no_impl_names+=',';
    100                         no_impl_names+=SString::sprintf("#%d.%s",i,n->getClassName().c_str());
    101                         no_impl++;
     99                                if (no_impl) no_impl_names += ',';
     100                                no_impl_names += SString::sprintf("#%d.%s", i, n->getClassName().c_str());
     101                                no_impl++;
    102102                        }
    103                 else if (ni->status==NeuroImpl::InitError)
     103                        else if (ni->status == NeuroImpl::InitError)
    104104                        {
    105                         if (init_err) init_err_names+=',';
    106                         init_err_names+=SString::sprintf("#%d.%s",i,n->getClassName().c_str());
    107                         init_err++;
     105                                if (init_err) init_err_names += ',';
     106                                init_err_names += SString::sprintf("#%d.%s", i, n->getClassName().c_str());
     107                                init_err++;
    108108                        }
    109109                }
     110                printf("\n");
     111                if (no_impl) printf("%d x missing implementation (%s)\n", no_impl, no_impl_names.c_str());
     112                if (init_err) printf("%d x failed initialization (%s)\n", init_err, init_err_names.c_str());
     113        }
     114        int steps = 1;
     115        if (argc > 2) steps = atol(argv[2]);
     116        int st;
     117        printf("step");
     118        for (i = 0; i < m.getNeuroCount(); i++)
     119        {
     120                n = m.getNeuro(i);
     121                printf("\t#%d.%s", i, n->getClassName().c_str());
     122        }
    110123        printf("\n");
    111         if (no_impl) printf("%d x missing implementation (%s)\n",no_impl,no_impl_names.c_str());
    112         if (init_err) printf("%d x failed initialization (%s)\n",init_err,init_err_names.c_str());
    113         }
    114 int steps=1;
    115 if (argc>2) steps=atol(argv[2]);
    116 int st;
    117 printf("step");
    118 for(i=0;i<m.getNeuroCount();i++)
    119         {
    120         n=m.getNeuro(i);
    121         printf("\t#%d.%s",i,n->getClassName().c_str());
    122         }
    123 printf("\n");
    124 for(st=0;st<=steps;st++)
     124        for (st = 0; st <= steps; st++)
    125125        {
    126126#ifdef VEYETEST
    127   veyeStep(m,st);
     127                veyeStep(m, st);
    128128#endif
    129         printf("%d",st);
    130         for(i=0;i<m.getNeuroCount();i++)
     129                printf("%d", st);
     130                for (i = 0; i < m.getNeuroCount(); i++)
    131131                {
    132                 n=m.getNeuro(i);
    133                 printf("\t%g",n->state);
     132                        n = m.getNeuro(i);
     133                        printf("\t%g", n->state);
    134134                }
    135         printf("\n");
    136         nn->simulateNeuroNet();
     135                printf("\n");
     136                nn->simulateNeuroNet();
    137137        }
    138 neurofac.freeImplementation();
     138        neurofac.freeImplementation();
    139139}
  • cpp/frams/_demos/shapeconvert.cpp

    r546 r972  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2016  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    4646                if (ar[0] == '-')
    4747                        switch (ar[1])
    48                 {
     48                        {
    4949                        case 's': shape = (Part::Shape)atol(ar + 2);
    5050                                if ((shape != Part::SHAPE_ELLIPSOID) && (shape != Part::SHAPE_CUBOID) && (shape != Part::SHAPE_CYLINDER))
     
    5656                        case 't': thickness = atof(ar + 2); break;
    5757                        case 'h': puts("Usage: shapeconvert [-sSHAPE] [-tTHICKNESS] [genotype_or_stdin]\n\tSHAPE: 1=ellipsoid, 2=cuboid, 3(default)=cylinder\n\tTHICKNESS: used for Part.sy/sz (default=0.2)"); break;
    58                 }
     58                        }
    5959                else
    6060                        if (!gen_arg)
     
    6767                loadSString(VirtFILE::Vstdin, gen);
    6868        Geno g(gen);
    69         Model m(g);
     69        Model m(g, Model::SHAPE_UNKNOWN);
    7070
    7171        if (!m.isValid())
Note: See TracChangeset for help on using the changeset viewer.