Ignore:
Timestamp:
06/06/18 01:45:18 (7 years ago)
Author:
Maciej Komosinski
Message:

A more complete implementation of fB, fH, fL

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/fL/fL_general.h

    r780 r797  
     1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
     2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
     3// See LICENSE.txt for details.
     4
    15#ifndef _FL_GENERAL_
    2 
    36#define _FL_GENERAL_
    47
     
    2225/** @name Constants used in fL methods */
    2326//@{
    24 #define PART_PROPS_COUNT   4 ///<Count of part properties
    25 #define JOINT_PROPS_COUNT  3 ///<Count of joint properties
    26 #define PE_NEURO_DET       "d" ///<Id of details type definition in f0_neuro_paramtab
    27 #define PE_CONN_WEIGHT     "w" ///<Id of weight type definition in f0_neuroconn_paramtab
     27#define FL_PART_PROPS_COUNT   4 ///<Count of part properties
     28#define FL_JOINT_PROPS_COUNT  3 ///<Count of joint properties
     29#define FL_PE_NEURO_DET       "d" ///<Id of details type definition in f0_neuro_paramtab
     30#define FL_PE_CONN_WEIGHT     "w" ///<Id of weight type definition in f0_neuroconn_paramtab
     31#define FL_PE_CONN_ATTR       "attr" ///<Id of attractor of neural connection
     32#define FL_DEFAULT_LENGTH     1.0 ///<Default length of a stick in fL encoding
     33#define FL_MINIMAL_LENGTH     0.0 ///<Minimal length of a stick in fL encoding
     34#define FL_MAXIMAL_LENGTH     2.0 ///<Maximal length of a stick in fL encoding
     35#define FL_MAXITER           "100.0" ///<Maximal iteration available in fL
     36#define FL_MAXPARAMS          "3" ///<Maximal number of user-parameters
    2837//@}
    2938
    3039extern const char *fL_part_names[];
    3140extern const char *fL_joint_names[];
    32 extern const char *fL_joint_fullnames[JOINT_PROPS_COUNT];
    33 extern const char *fL_part_fullnames[PART_PROPS_COUNT];
     41extern const char *fL_joint_fullnames[FL_JOINT_PROPS_COUNT];
     42extern const char *fL_part_fullnames[FL_PART_PROPS_COUNT];
    3443
    3544#define LSYSTEM_PARAM_TYPE "s" ///< standard type of L-System elements
     
    4655        fLElementType type; ///< type of fL_Element
    4756
    48         fL_Element(fLElementType type) : type(type) {}
     57        int begin; ///<beginning of the element definition in genotype
     58        int end; ///<end of the element definition in genotype
     59
     60
     61        fL_Element(fLElementType type) : type(type) { begin = end = 0; }
    4962        virtual ~fL_Element() { }
    5063
     
    7891        void *data; ///<pointer to properties of word
    7992        bool builtin; ///<determines if word is built-in (true) or not (false).
     93        PartBase *bodyelementpointer; ///<helper field for built-in words
    8094        std::vector<MathEvaluation *> parevals; ///<stores MathEvaluation objects with parameter functions
    8195        double creationiter; ///<this helper field is used during L-System iterations and determines when word was created
    82         fL_Word(bool builtin = false) :
    83                 fL_Element(fLElementType::TERM), mut("Word", "Properties"), //TODO change Word to Term everywhere
     96        fL_Word(bool builtin = false, int begin = 0, int end = 0) :
     97                fL_Element(fLElementType::TERM), mut("Word", "Properties"),
    8498                builtin(builtin)
    8599        {
     
    89103                data = NULL;
    90104                creationiter = 0;
     105                this->begin = begin;
     106                this->end = end;
     107                bodyelementpointer = NULL;
    91108        }
    92109
     
    102119                for (MathEvaluation *ev : parevals)
    103120                {
    104                         delete ev;
     121                        if (ev) delete ev;
    105122                }
    106123                parevals.clear();
     
    125142
    126143        /**
    127          * TODO
    128          */
    129         bool operator==(const fL_Word& other) const;
    130 
    131         /**
    132144         * Returns 'w:' line defining word in genotype.
    133145         * @return string representation of word definition
     
    150162         */
    151163        int saveEvals(bool keepformulas);
     164
     165        /**
     166         * Computes distance between two L-System words in genotype. Distance has sense
     167         * only when two words have the same name. Otherwise, returned value equals -1.
     168         * The distance is computed as Euclidean distance between words arguments.
     169         * @param right the second word
     170         * @return Euclidean distance between words in genotype or -1 if words have different name
     171         */
     172        double distance(fL_Word *right);
    152173};
    153174
     
    164185        };
    165186        BranchType btype; ///< branch parenthesis type
    166         fL_Branch(BranchType branchtype) : fL_Word()
     187        fL_Branch(BranchType branchtype, int begin, int end) : fL_Word(true, begin, end)
    167188        {
    168189                type = fLElementType::BRANCH;
     
    192213        std::list<fL_Word *> objsucc; ///<objec representation of successors
    193214
    194         int start; ///<beginning of the rule definition in genotype
    195         int end; ///<end of the rule definition in genotype
    196 
    197         fL_Rule(int start, int end) : fL_Element(fLElementType::RULE),
    198                 start(start), end(end)
     215        fL_Rule(int begin, int end)  : fL_Element(fLElementType::RULE)
    199216        {
    200217                predecessor = "";
     
    203220                objpred = NULL;
    204221                condeval = NULL;
     222                this->begin = begin;
     223                this->end = end;
    205224        }
    206225
     
    231250         * pointer and iterator of list and inserting successor sequence into list.
    232251         * Final iterator value points to the first word after processed word.
     252         * @param builder builder containing current genotype
    233253         * @param in word that is currently processed
    234254         * @param it iterator of genotype list which determines position of current analysis
    235          * @param genotype current genotype. Words on the left of genotype are processed and possibly replaced by previous rules
    236255         * @param currtime value representing continuous time value for rule
    237256         * @return 0 if rule processed current word, 1 if rule is no applicable
    238257         */
    239         int deploy(fL_Word *in, std::list<fL_Word *>::iterator &it, std::list<fL_Word *> &genotype, double currtime);
    240 };
    241 
     258        int deploy(fL_Builder *builder, fL_Word *in, std::list<fL_Word *>::iterator &it, double currtime);
     259};
     260
     261/**
     262 * Structure for holding current Turtle state.
     263 */
    242264struct fL_State
    243265{
     
    245267        Part *currpart; ///<Latest created part
    246268        Neuro *currneuron; ///<Latest created neuron
    247         fL_State() : direction(1, 0, 0), currpart(NULL), currneuron(NULL)
     269        fL_State() : direction(1,0,0), currpart(NULL), currneuron(NULL)
    248270        { }
    249271};
     
    266288        double time; ///<time of creature development
    267289        int numckp; ///<number of checkpoints per growth step - checkpoints are always created after rules deployment in integer steps, numckp determines how many checkpoints should be created within single step (to watch changes of parameters)
     290        int maxwords; ///<maximum number of words that can
     291        bool using_checkpoints; ///<true if checkpoints should be created, or false
     292        bool using_mapping; ///<true if mappings should be created, or false
     293
     294        int builtincount; ///<number of built-in words
    268295
    269296        std::list<fL_Word *> genotype; ///<list of current words of genotype
    270297        std::unordered_map<std::string, fL_Word *> words; ///<map from string to words existing in L-System
     298        std::vector<std::string> wordnames;
    271299        std::vector<fL_Rule *> rules; ///<rules available in system
    272300
    273         fL_Builder() : fL_Element(fLElementType::INFO)
     301        fL_Builder(bool using_mapping = false, bool using_checkpoints = false) : fL_Element(fLElementType::INFO),
     302                        using_checkpoints(using_checkpoints), using_mapping(using_mapping)
    274303        {
    275304                axiom = "";
    276305                time = 1.0;
    277306                numckp = 1;
     307                builtincount = 0;
     308                maxwords = -1;
    278309        }
    279310        ~fL_Builder();
     
    337368
    338369        /**
     370         * Creates fL_Word object for a given token.
     371         * @param worddef string representing single word object in sequence
     372         * @param word a reference to a pointer of created word by this method
     373         * @param numparams number of parameters for a given sequence (usually number of rule predecessor's arguments)
     374         * @param begin the begin of word definition in genotype
     375         * @param end the end of word definition in genotype
     376         * @return 0 if conversion went successfully, 1 when there is a problem with parsing
     377         */
     378        int createWord(SString worddef, fL_Word *&word, int numparams, int begin, int end);
     379
     380        /**
    339381         * Helper function that converts input sequence into list of L-System words.
    340382         * @param sequence input sequence of stringified word objects
     
    343385         * @return 0 if tokenizing finished successfully, 1 otherwise
    344386         */
    345         int tokenize(SString sequence, std::list<fL_Word *> &result, int numparams);
     387        int tokenize(SString sequence, std::list<fL_Word *> &result, int numparams, int begin, int end);
    346388
    347389        /**
     
    363405        /**
    364406         * Developes L-System from given genotype and builds Framsticks Model from it.
    365          */
    366         int develop();
     407         * When using_checkpoints is enabled, method generates checkpoint for each
     408         * step defined in timestamp.
     409         * @param neededtime reference to a time value after stopping development (usually it will be equal to time specified in the time field, unless the number of allowed words will be exceeded earlier)
     410         * @return final model from a fL genotype
     411         */
     412        Model* developModel(double &neededtime);
    367413
    368414        /**
     
    371417         * @return 0 if developing went successfully, 1 otherwise
    372418         */
    373         int developModel(Model &model);
     419        int buildModelFromSequence(Model *model);
    374420
    375421        /**
     
    386432
    387433        /**
    388          * TODO
     434         * Alters part properties according to informations stored in stickword.
     435         * Method takes current values of part's properties computed from previous
     436         * calls of the alterPartProperties and computes mean according to upcoming
     437         * values of properties.
     438         * @param part analysed part
     439         * @param stickword the L-System word that affects current part
     440         * @param alterationcount the number of times of part modifications - used to compute mean of every word properties
     441         * @return always 0
    389442         */
    390443        int alterPartProperties(Part *part, fL_Word *stickword, double &alterationcount);
    391444
    392445        /**
    393          * TODO
    394          */
    395         Neuro *findInputNeuron(std::list<fL_Word *> genotype, std::list<fL_Word *>::iterator it, fL_Word *attractor);
    396 
    397         /**
    398          * TODO
    399          */
    400         void findNext(fL_Word *word, std::list<fL_Word *> genotype, std::list<fL_Word *>::iterator &it);
     446         * Finds input neuron that is nearest to attractor or connection definition.
     447         * When attractor object is given, then the word with name matching attractor and
     448         * with nearest values of parameters is chosen as the point, from which input
     449         * neuron is looked for. The searching goes both sides.
     450         * @param currneu object storing informations about connection word iterator and current neuron
     451         * @param attractor pointer to an attractor definition presented in connection word
     452         * @return pointer to the input neuron, or NULL if no neuron could be found
     453         */
     454        Neuro *findInputNeuron(std::pair<std::list<fL_Word *>::iterator, Neuro *> currneu, fL_Word *attractor);
     455
     456        /**
     457         * Removes joints, parts and neurons with its connections from current model, without
     458         * removing checkpoint data.
     459         * @param m model to clear
     460         */
     461        void clearModelElements(Model *m);
     462
     463        /**
     464         * Converts parameters defined in built-in words into desired
     465         * range with use of sigmoid function, which ensures staying
     466         * within min and max value.
     467         * @param input the value from evaluation of parameter
     468         * @param min minimal value of property
     469         * @param max maximal value of property
     470         * @return value of body element property
     471         */
     472        double sigmoidTransform(double input, double min, double max);
     473
     474        /**
     475         * Counts words defined in the genotype.
     476         * @return number of defined words
     477         */
     478        int countDefinedWords();
     479
     480        /**
     481         * Counts number of sticks in a given sequence
     482         * @return number of sticks in sequence
     483         */
     484        int countSticksInSequence(std::list<fL_Word *> sequence);
     485
     486        /**
     487         * Counts all definitions of words, all words in axiom and rule successors in a genotype.
     488         * Used for computing change between original genotype and mutation.
     489         * @return number of words in definitions, axiom and rule successors
     490         */
     491        int countWordsInLSystem();
     492
     493        /**
     494         * Sorts rules and removes rules that will not be used. The "sorting" is
     495         * done in such way that all rules with conditions are first, and rules
     496         * without conditions go to the end. If there are more than one rule
     497         * for the same word with no condition or same condition, than the
     498         * second one is removed.
     499         */
     500        void removeRedundantRules();
    401501};
    402502
Note: See TracChangeset for help on using the changeset viewer.