Changeset 1226 for cpp/frams


Ignore:
Timestamp:
04/27/23 03:55:33 (20 months ago)
Author:
Maciej Komosinski
Message:
  • Added ShapeType? constraint to GenoOperators::parseNeuroClass()
  • Introduced GenoOperators::getRandomNeuroClassWithOutputAndWantingNoOrAnyInputs()
Location:
cpp/frams/genetics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/genooperators.cpp

    r1032 r1226  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    313313}
    314314
    315 NeuroClass *GenoOperators::getRandomNeuroClassWithOutputAndNoInputs(Model::ShapeType for_shape_type)
     315NeuroClass *GenoOperators::getRandomNeuroClassWithOutputAndWantingNoInputs(Model::ShapeType for_shape_type)
    316316{
    317317        vector<NeuroClass *> active;
     
    320320                NeuroClass *nc = Neuro::getClass(i);
    321321                if (nc->isShapeTypeSupported(for_shape_type) && nc->genactive && nc->getPreferredOutput() != 0 && nc->getPreferredInputs() == 0)
     322                        active.push_back(nc);
     323        }
     324        if (active.size() == 0) return NULL; else return active[rndUint(active.size())];
     325}
     326
     327NeuroClass *GenoOperators::getRandomNeuroClassWithOutputAndWantingNoOrAnyInputs(Model::ShapeType for_shape_type)
     328{
     329        vector<NeuroClass *> active;
     330        for (int i = 0; i < Neuro::getClassCount(); i++)
     331        {
     332                NeuroClass *nc = Neuro::getClass(i);
     333                if (nc->isShapeTypeSupported(for_shape_type) && nc->genactive && nc->getPreferredOutput() != 0 && nc->getPreferredInputs() <= 0) // getPreferredInputs() should be 0 or -1 (any)
    322334                        active.push_back(nc);
    323335        }
     
    353365        {
    354366                if (!strchrn0(excluded, choices[i])) allowed_count++;
    355                 if (allowed_count == rnd_index) return i;
     367                if (allowed_count == rnd_index) return int(i);
    356368        }
    357369        return -1; //never happens
    358370}
    359371
    360 NeuroClass *GenoOperators::parseNeuroClass(char *&s)
     372NeuroClass *GenoOperators::parseNeuroClass(char *&s, ModelEnum::ShapeType supported_shapetype)
    361373{
    362374        int maxlen = (int)strlen(s);
     
    365377        for (int i = 0; i < Neuro::getClassCount(); i++)
    366378        {
    367                 const char *ncname = Neuro::getClass(i)->name.c_str();
    368                 int ncnamelen = (int)strlen(ncname);
    369                 if (maxlen >= ncnamelen && ncnamelen > NClen && (strncmp(s, ncname, ncnamelen) == 0))
    370                 {
    371                         NC = Neuro::getClass(i);
    372                         NClen = ncnamelen;
     379                NeuroClass *nci = Neuro::getClass(i);
     380                if (!nci->isShapeTypeSupported(supported_shapetype))
     381                        continue;
     382                const char *nciname = nci->name.c_str();
     383                int ncinamelen = (int)strlen(nciname);
     384                if (maxlen >= ncinamelen && ncinamelen > NClen && (strncmp(s, nciname, ncinamelen) == 0))
     385                {
     386                        NC = nci;
     387                        NClen = ncinamelen;
    373388                }
    374389        }
  • cpp/frams/genetics/genooperators.h

    r1032 r1226  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    202202        static NeuroClass* getRandomNeuroClassWithOutput(Model::ShapeType for_shape_type); ///<returns random neuroclass with output or NULL when no active classes.
    203203        static NeuroClass* getRandomNeuroClassWithInput(Model::ShapeType for_shape_type); ///<returns random neuroclass with input or NULL when no active classes.
    204         static NeuroClass* getRandomNeuroClassWithOutputAndNoInputs(Model::ShapeType for_shape_type); ///<returns random sensor or NULL when no active classes.
     204        static NeuroClass* getRandomNeuroClassWithOutputAndWantingNoInputs(Model::ShapeType for_shape_type); ///<returns random sensor or NULL when no active classes. Note: only neuroclasses that prefer 0 inputs are considered, not those that prefer any number of inputs (thus including 0) - see getRandomNeuroClassWithOutputAndWantingNoOrAnyInputs().
     205        static NeuroClass* getRandomNeuroClassWithOutputAndWantingNoOrAnyInputs(Model::ShapeType for_shape_type); ///<returns random neuron or NULL when no active classes. Note: both neuroclasses that prefer 0 inputs and those that prefer any number of inputs (thus including 0) are considered.
    205206        static int getRandomNeuroClassWithOutput(const vector<NeuroClass*>& NClist); ///<returns index of random NeuroClass from the NClist or -1 when no neurons in the list provide output \e NClist list of available neuron classes
    206207        static int getRandomNeuroClassWithInput(const vector<NeuroClass*>& NClist); ///<returns index of random NeuroClass from the NClist or -1 when no neurons in the list want input(s) \e NClist list of available neuron classes
    207         static int getRandomChar(const char *choices, const char *excluded); ///<returns index of a random character from 'choices' excluding 'excluded', or -1 when everything is excluded or 'choices' is empty.
    208         static NeuroClass* parseNeuroClass(char *&s); ///<returns longest matching neuroclass or NULL if the string does not begin with a valid neuroclass name. Advances \e s pointer.
     208        static int getRandomChar(const char *choices, const char *excluded); ///<returns index of a random character from \e choices excluding \e excluded, or -1 when everything is excluded or \e choices is empty.
     209        static NeuroClass* parseNeuroClass(char *&s, ModelEnum::ShapeType supported_shapetype); ///<returns the longest matching neuroclass that satisfies supported_shapetype (ModelEnum::SHAPETYPE_BALL_AND_STICK or ModelEnum::SHAPETYPE_SOLIDS) or NULL if the string does not begin with an appropriate neuroclass name. Advances the \e s pointer if the neuroclass is found.
    209210        static Neuro* findNeuro(const Model *m, const NeuroClass *nc); ///<returns pointer to first Neuro of class \e nc, or NULL if there is no such Neuro.
    210         static int neuroClassProp(char *&s, NeuroClass *nc, bool also_v1_N_props = false); ///<returns 0-based extraproperty of NeuroClass or NEUROCLASS_PROP_OFFSET-based standard property of NeuroClass, or -1 if the string does not begin with a valid property name. Advance \e s pointer if success.
     211        static int neuroClassProp(char *&s, NeuroClass *nc, bool also_v1_N_props = false); ///<returns 0-based extraproperty of NeuroClass or NEUROCLASS_PROP_OFFSET-based standard property of NeuroClass, or -1 if the string does not begin with a valid property name. Advance the \e s pointer if success.
    211212        static bool isWS(const char c); ///<is \e c a whitespace char?
    212213        static void skipWS(char *&s); ///<advances pointer \e s skipping whitespaces.
Note: See TracChangeset for help on using the changeset viewer.