Changeset 797 for cpp/frams/genetics/fL/fL_general.h
- Timestamp:
- 06/06/18 01:45:18 (7 years ago)
- 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 1 5 #ifndef _FL_GENERAL_ 2 3 6 #define _FL_GENERAL_ 4 7 … … 22 25 /** @name Constants used in fL methods */ 23 26 //@{ 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 28 37 //@} 29 38 30 39 extern const char *fL_part_names[]; 31 40 extern 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];41 extern const char *fL_joint_fullnames[FL_JOINT_PROPS_COUNT]; 42 extern const char *fL_part_fullnames[FL_PART_PROPS_COUNT]; 34 43 35 44 #define LSYSTEM_PARAM_TYPE "s" ///< standard type of L-System elements … … 46 55 fLElementType type; ///< type of fL_Element 47 56 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; } 49 62 virtual ~fL_Element() { } 50 63 … … 78 91 void *data; ///<pointer to properties of word 79 92 bool builtin; ///<determines if word is built-in (true) or not (false). 93 PartBase *bodyelementpointer; ///<helper field for built-in words 80 94 std::vector<MathEvaluation *> parevals; ///<stores MathEvaluation objects with parameter functions 81 95 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 everywhere96 fL_Word(bool builtin = false, int begin = 0, int end = 0) : 97 fL_Element(fLElementType::TERM), mut("Word", "Properties"), 84 98 builtin(builtin) 85 99 { … … 89 103 data = NULL; 90 104 creationiter = 0; 105 this->begin = begin; 106 this->end = end; 107 bodyelementpointer = NULL; 91 108 } 92 109 … … 102 119 for (MathEvaluation *ev : parevals) 103 120 { 104 delete ev;121 if (ev) delete ev; 105 122 } 106 123 parevals.clear(); … … 125 142 126 143 /** 127 * TODO128 */129 bool operator==(const fL_Word& other) const;130 131 /**132 144 * Returns 'w:' line defining word in genotype. 133 145 * @return string representation of word definition … … 150 162 */ 151 163 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); 152 173 }; 153 174 … … 164 185 }; 165 186 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) 167 188 { 168 189 type = fLElementType::BRANCH; … … 192 213 std::list<fL_Word *> objsucc; ///<objec representation of successors 193 214 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) 199 216 { 200 217 predecessor = ""; … … 203 220 objpred = NULL; 204 221 condeval = NULL; 222 this->begin = begin; 223 this->end = end; 205 224 } 206 225 … … 231 250 * pointer and iterator of list and inserting successor sequence into list. 232 251 * Final iterator value points to the first word after processed word. 252 * @param builder builder containing current genotype 233 253 * @param in word that is currently processed 234 254 * @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 rules236 255 * @param currtime value representing continuous time value for rule 237 256 * @return 0 if rule processed current word, 1 if rule is no applicable 238 257 */ 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 */ 242 264 struct fL_State 243 265 { … … 245 267 Part *currpart; ///<Latest created part 246 268 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) 248 270 { } 249 271 }; … … 266 288 double time; ///<time of creature development 267 289 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 268 295 269 296 std::list<fL_Word *> genotype; ///<list of current words of genotype 270 297 std::unordered_map<std::string, fL_Word *> words; ///<map from string to words existing in L-System 298 std::vector<std::string> wordnames; 271 299 std::vector<fL_Rule *> rules; ///<rules available in system 272 300 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) 274 303 { 275 304 axiom = ""; 276 305 time = 1.0; 277 306 numckp = 1; 307 builtincount = 0; 308 maxwords = -1; 278 309 } 279 310 ~fL_Builder(); … … 337 368 338 369 /** 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 /** 339 381 * Helper function that converts input sequence into list of L-System words. 340 382 * @param sequence input sequence of stringified word objects … … 343 385 * @return 0 if tokenizing finished successfully, 1 otherwise 344 386 */ 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); 346 388 347 389 /** … … 363 405 /** 364 406 * 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); 367 413 368 414 /** … … 371 417 * @return 0 if developing went successfully, 1 otherwise 372 418 */ 373 int developModel(Model &model);419 int buildModelFromSequence(Model *model); 374 420 375 421 /** … … 386 432 387 433 /** 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 389 442 */ 390 443 int alterPartProperties(Part *part, fL_Word *stickword, double &alterationcount); 391 444 392 445 /** 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(); 401 501 }; 402 502
Note: See TracChangeset
for help on using the changeset viewer.