source: cpp/frams/genetics/fL/fL_oper.h @ 920

Last change on this file since 920 was 797, checked in by Maciej Komosinski, 7 years ago

A more complete implementation of fB, fH, fL

File size: 6.3 KB
Line 
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
5#ifndef _FL_OPER_H_
6#define _FL_OPER_H_
7
8#include "../genooperators.h"
9#include "fL_general.h"
10
11/** @name Codes for general fL mutation types */
12//@{
13#define FL_ADD_WORD 0 ///<probability of adding word to axiom or rule successor
14#define FL_ADD_WDEF 1 ///<probability of adding word definition
15#define FL_ADD_RULE 2 ///<probability of adding new rule
16#define FL_CHG_COND 3 ///<probability of modification or rule condition
17#define FL_CHG_WORD 4 ///<probability of changing word name or formula in axiom or rule successor
18#define FL_CHG_ITER 5 ///<probability of changing iteration of genotype
19#define FL_DEL_WORD 6 ///<probability of deleting word from axiom or rule. Deleting all occurrences of word deletes word definition. Deleting all words in rule deletes this rule
20#define FL_OPCOUNT 7 ///<count of mutation operators
21//@}
22
23/** @name Codes for probabilities of mutating axiom words or rule words */
24//@{
25#define FL_AXM_WORD_MUT_PROB 0 ///<probability of performing addition, substitution or deletion of word in axiom
26#define FL_RUL_WORD_MUT_PROB 1 ///<probability of performing addition, substitution or deletion of word in rule's successor
27#define FL_MUTGROUPSCOUNT 2 ///<count of possible groups for mutations
28//@}
29
30/** @name Codes for probabilities of mutating word names or formula during change */
31//@{
32#define FL_CHG_WORD_FORMULA 0 ///<probability of changing formula of one of parameters
33#define FL_CHG_WORD_NAME 1 ///<probability of changing word name to other word name with <= number of parameters
34#define FL_CHG_COUNT 2 ///<count of possible changes in words
35//@}
36
37/** @name Codes for probabilities of choosing one of word types for addition */
38//@{
39#define FL_ADD_STICK  0 ///<probability of adding stick
40#define FL_ADD_NEURO  1 ///<probability of adding neuron
41#define FL_ADD_CONN   2 ///<probability of adding connection
42#define FL_ADD_ROT    3 ///<probability of adding one of rotation words
43#define FL_ADD_OTHER  4 ///<probability of adding word defined in the genotype
44#define FL_ADD_BRANCH 5 ///<probability of adding a branch
45#define FL_ADD_COUNT  6 ///<count of possible additions
46//@}
47
48class Geno_fL : public GenoOperators
49{
50private:
51
52        /**
53         * Adds word with a given definition to the list in place pointed by an iterator.
54         * @param list list, to which new word will be added
55         * @param definition temporal object that will act as pattern for newly created word
56         * @param it the iterator pointing to addition point
57         * @return true
58         */
59        bool addWord(std::list<fL_Word *>* list, fL_Word *definition, std::list<fL_Word *>::iterator it);
60
61        /**
62         * Selects axiom or one of rule's successors.
63         * @param creature the object with defined axiom and rules
64         * @param numparams reference holding the number of parameters that are available for this list, 0 for axiom
65         * @param ruleid the index of the rule in rules structure or -1 if selected sequence is the axiom
66         * @return pointer to a selected sequence
67         */
68        std::list<fL_Word *>* selectRandomSequence(fL_Builder *creature, int &numparams, int &ruleid);
69
70        /**
71         * Selects word definition according to a method. Method is one of
72         * values FL_ADD_STICK, FL_ADD_NEURO, FL_ADD_CONN, FL_ADD_ROT, FL_ADD_OTHER
73         * etc. If FL_ADD_OTHER is chosen, then one of defined words is chosen. If
74         * there are no defined words, then one of built-in words is chosen.
75         * If FL_ADD_BRANCH is selected, then method returns NULL.
76         * @param creature current genotype
77         * @param method one of methods of addition
78         * @return object defining one of genotype words, or NULL if branching method is used
79         */
80        fL_Word* randomWordDefinition(fL_Builder *creature, int method);
81
82        /**
83         * Tries to find appropriate word in second creature that matches the word in first creature.
84         * Firstly, it tries to check if some word is not already assigned to a word in second creature.
85         * If the searching is successful, then appropriate word is used. Otherwise, method tries to find
86         * word that matches by name and number of parameters, or at least by number of parameters and hasn't
87         * been used already for other translation. If this is impossible, method creates new word definition
88         * for the second creature.
89         * @param from creature, from rule is taken
90         * @param to creature, which takes the rule
91         * @param fromword word from the first creature that needs to be translated
92         * @param map hashmap for current assignments
93         * @return word instance that need to be used by generated rule for the second genotype
94         */
95        fL_Word* getAppropriateWord(fL_Builder *from, fL_Builder *to, fL_Word *fromword, std::unordered_map<std::string, std::string> &map);
96
97        /**
98         * Migrates random rule from one creature to the other creature.
99         * @param from creature, from rule is taken
100         * @param to creature, which takes the rule
101         * @param numselrules number of rules that need to be moved
102         */
103        void migrateRandomRules(fL_Builder *from, fL_Builder *to, int numselrules);
104
105        /**
106         * Deletes branch from a given sequence starting from iterator. The deletion
107         * removes only braces, not whole branch.
108         * @param list current list that needs to be modified
109         * @param openbranchposition the iterator pointing to the open branch word
110         */
111        void deleteBranch(std::list<fL_Word *> *list, std::list<fL_Word *>::iterator openbranchposition);
112public:
113        double operations[FL_OPCOUNT]; ///<Relative probabilities of mutation types
114        double groupprobabilities[FL_MUTGROUPSCOUNT]; ///<Relative probabilities of changing elements in rules or axioms
115        double chgoperations[FL_CHG_COUNT]; ///<Relative probabilities of changing word names or parameters of word during change mutation
116        double addtypes[FL_ADD_COUNT]; ///<Relative probabilities of selecting special word types
117
118        double iterchangestep; ///<minimal value, by which time of development is modified
119        int maxdefinedwords; ///<maximal number of defined words for single fL genotype
120
121        Geno_fL();
122
123        int checkValidity(const char *geno, const char *genoname);
124
125        int validate(char *&geno, const char *genoname);
126
127        int mutate(char *&geno, float& chg, int &method);
128
129        int crossOver(char *&g1, char *&g2, float& chg1, float& chg2);
130
131        const char* getSimplest() { return "i:axiom=\"S()\", time=0, maxwords=300"; }
132
133        uint32_t style(const char *geno, int pos);
134};
135
136#endif // _FL_OPER_H_
Note: See TracBrowser for help on using the repository browser.