[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[193] | 4 | |
---|
[196] | 5 | // Copyright (C) 1999,2000 Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL |
---|
| 6 | |
---|
[193] | 7 | #ifndef _F4_GENERAL_H_ |
---|
| 8 | #define _F4_GENERAL_H_ |
---|
| 9 | |
---|
[196] | 10 | //#include "f4_orientmat.h" |
---|
| 11 | #include <frams/util/3d.h> |
---|
| 12 | #include <frams/util/sstring.h> |
---|
| 13 | #include <frams/util/multirange.h> |
---|
[193] | 14 | |
---|
| 15 | #ifdef DMALLOC |
---|
| 16 | #include <dmalloc.h> |
---|
| 17 | #endif |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | class f4_Props |
---|
| 21 | { |
---|
[196] | 22 | public: |
---|
| 23 | // fill with default values |
---|
| 24 | f4_Props(); |
---|
| 25 | // must sum to 1 |
---|
| 26 | void normalizeBiol4(); |
---|
| 27 | void executeModifier(char modif); |
---|
| 28 | void adjust(); |
---|
[193] | 29 | |
---|
[196] | 30 | double len; // length (dlug) |
---|
| 31 | double curv; // curvedness (skr) |
---|
| 32 | double mass; |
---|
| 33 | double friction; |
---|
| 34 | double ruch; |
---|
| 35 | double assim; |
---|
| 36 | double odpor; |
---|
| 37 | double ingest; // ingestion (wchl) |
---|
| 38 | double twist; |
---|
| 39 | double energ; |
---|
[193] | 40 | }; |
---|
| 41 | |
---|
| 42 | extern f4_Props stdProps; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | // rolling (one-time) |
---|
| 46 | void rolling_dec(double * v); |
---|
| 47 | void rolling_inc(double * v); |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | class f4_node; // later |
---|
| 51 | class f4_Cell; // later |
---|
| 52 | class f4_Cells; // later |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | // cell types |
---|
| 56 | #define T_UNDIFF4 40 |
---|
| 57 | #define T_STICK4 41 |
---|
| 58 | #define T_NEURON4 42 |
---|
| 59 | |
---|
| 60 | int scanrec(const char * s, unsigned int slen, char stopchar); |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | class f4_CellLink; |
---|
| 64 | #define MAXINPUTS 100 |
---|
| 65 | |
---|
| 66 | // an abstract cell type, extension of part/stick -- for developmental encoding |
---|
| 67 | class f4_Cell |
---|
| 68 | { |
---|
| 69 | public: |
---|
[196] | 70 | class repeat_ptr |
---|
| 71 | { |
---|
| 72 | public: |
---|
| 73 | repeat_ptr() : node(NULL), count(-1) { }; |
---|
| 74 | repeat_ptr(f4_node * a, int b) : node(a), count(b) { }; |
---|
| 75 | inline void null() { node = NULL; count = -1; }; |
---|
| 76 | inline bool isNull() const { return ((node == NULL) || (count <= 0)); }; |
---|
| 77 | inline void dec() { count--; }; |
---|
| 78 | f4_node * node; // ptr to repetition code |
---|
| 79 | char count; // repetition counter |
---|
| 80 | }; |
---|
[193] | 81 | |
---|
[196] | 82 | class repeat_stack // a stack of repet_ptr's |
---|
| 83 | { |
---|
| 84 | public: |
---|
| 85 | repeat_stack() { top = 0; }; |
---|
| 86 | inline void null() { top = 0; }; |
---|
| 87 | inline void push(repeat_ptr A) { if (top >= stackSize) return; ptr[top] = A; top++; }; |
---|
| 88 | inline void pop() { if (top > 0) top--; }; |
---|
| 89 | inline repeat_ptr * first() { return &(ptr[top - (top > 0)]); }; |
---|
| 90 | static const int stackSize = 4; // max 4 nested levels |
---|
| 91 | repeat_ptr ptr[stackSize]; |
---|
| 92 | short int top; // top of the stack |
---|
| 93 | }; |
---|
[193] | 94 | |
---|
[196] | 95 | f4_Cell(int nname, |
---|
| 96 | f4_Cell * ndad, int nangle, f4_Props newP); |
---|
| 97 | f4_Cell(f4_Cells * nO, int nname, f4_node * ngeno, f4_node * ngcur, |
---|
| 98 | f4_Cell * ndad, int nangle, f4_Props newP); |
---|
| 99 | ~f4_Cell(); |
---|
[193] | 100 | |
---|
[196] | 101 | int onestep(); // execute one simulation step (till a division) |
---|
[193] | 102 | |
---|
[247] | 103 | int addlink(f4_Cell * nfrom, double nw, int nt); |
---|
[196] | 104 | void adjustRec(); |
---|
[193] | 105 | |
---|
[196] | 106 | int name; // name (number) |
---|
| 107 | int type; // type |
---|
| 108 | f4_Cell * dadlink; |
---|
| 109 | f4_Cells * org; // uplink to organism |
---|
[193] | 110 | |
---|
[196] | 111 | f4_node * genot; // genotype |
---|
| 112 | f4_node * gcur; // current genotype execution pointer |
---|
| 113 | int active; // whether development is still active |
---|
| 114 | repeat_stack repeat; |
---|
| 115 | int recProcessedFlag; // used during recursive traverse |
---|
| 116 | // remember the genotype codes affecting this cell so far |
---|
| 117 | MultiRange genoRange; |
---|
[193] | 118 | |
---|
[196] | 119 | f4_Props P; // properties |
---|
| 120 | int anglepos; // number of position within dad's children (,) |
---|
| 121 | int childcount; // number of children |
---|
| 122 | int commacount; // number of postitions at lastend (>=childcount) |
---|
| 123 | double rolling; // rolling angle ('R') (around x) |
---|
| 124 | double xrot; |
---|
| 125 | double zrot; // horizontal rotation angle due to |
---|
| 126 | // branching (around z) |
---|
| 127 | //Pt3D firstend; // coord.s of first end (connects to parent) |
---|
| 128 | //Pt3D lastend; // last end |
---|
| 129 | //f4_OrientMat OM; |
---|
| 130 | double mz; // freedom in z |
---|
[247] | 131 | int p2_refno; // number of last end part object, used in f0 |
---|
| 132 | int joint_refno; // number of the joint object, used in f0 |
---|
| 133 | int neuro_refno; // number of the neuro object, used in f0 |
---|
[193] | 134 | |
---|
[247] | 135 | int ctrl; // neuron type |
---|
[196] | 136 | double state; |
---|
| 137 | double inertia; |
---|
| 138 | double force; |
---|
| 139 | double sigmo; |
---|
| 140 | f4_CellLink* links[MAXINPUTS]; |
---|
| 141 | int nolink; |
---|
[193] | 142 | }; |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | // an input link to a neuron |
---|
| 146 | class f4_CellLink |
---|
| 147 | { |
---|
| 148 | public: |
---|
[247] | 149 | f4_CellLink(f4_Cell * nfrom, double nw, int nt); |
---|
[196] | 150 | f4_Cell * from; |
---|
| 151 | // type: 0: input, 1 '*', 2 'G', 3 'T', 4 'S' |
---|
[247] | 152 | int t; |
---|
[196] | 153 | double w; |
---|
[193] | 154 | }; |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | // a collection of cells, like Organism, for developmental encoding |
---|
| 158 | #define MAX4CELLS 100 |
---|
| 159 | class f4_Cells |
---|
| 160 | { |
---|
[196] | 161 | public: |
---|
| 162 | f4_Cells(f4_node * genome, int nrepair); |
---|
| 163 | f4_Cells(SString &genome, int nrepair); |
---|
| 164 | ~f4_Cells(); |
---|
| 165 | void addCell(f4_Cell * newcell); |
---|
| 166 | void toF1Geno(SString &out); // output to f1 format, approximation |
---|
| 167 | int onestep(); // simulate all parts for one step |
---|
| 168 | int simulate(); // simulate development, return error (0 for ok) |
---|
| 169 | // for error reporting / genotype fixing |
---|
| 170 | int geterror() { return error; }; |
---|
| 171 | int geterrorpos() { return errorpos; }; |
---|
| 172 | void setError(int nerrpos); |
---|
| 173 | void setRepairRemove(int nerrpos, f4_node * rem); |
---|
| 174 | int setRepairInsert(int nerrpos, f4_node * parent, f4_node * insert); |
---|
| 175 | void repairGeno(f4_node * geno, int whichchild); |
---|
[193] | 176 | |
---|
[196] | 177 | // the cells |
---|
| 178 | f4_Cell * C[MAX4CELLS]; |
---|
| 179 | int nc; |
---|
[193] | 180 | |
---|
| 181 | private: |
---|
[196] | 182 | // for error reporting / genotype fixing |
---|
| 183 | int repair; |
---|
| 184 | int error; |
---|
| 185 | int errorpos; |
---|
| 186 | f4_node * repair_remove; |
---|
| 187 | f4_node * repair_parent; |
---|
| 188 | f4_node * repair_insert; |
---|
| 189 | void toF1GenoRec(int curc, SString &out); |
---|
| 190 | f4_Cell * tmpcel; // needed by toF1Geno |
---|
| 191 | f4_node * f4rootnode; // used by constructor |
---|
[193] | 192 | }; |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * Class to organize a f4 genotype in a tree structure. |
---|
| 197 | */ |
---|
| 198 | class f4_node |
---|
| 199 | { |
---|
| 200 | public: |
---|
[196] | 201 | char name; // one-letter 'name' |
---|
| 202 | f4_node * parent; // parent link, or NULL |
---|
| 203 | f4_node * child; // child, or NULL |
---|
| 204 | f4_node * child2; // second child, or NULL |
---|
| 205 | int pos; // original position in string |
---|
| 206 | int i1; // internal int parameter1 |
---|
[247] | 207 | int l1; // internal long parameter1 |
---|
[196] | 208 | double f1; // internal double parameter1 |
---|
[193] | 209 | |
---|
[196] | 210 | f4_node(); |
---|
| 211 | f4_node(char nname, f4_node * nparent, int npos); |
---|
| 212 | ~f4_node(); |
---|
| 213 | int addChild(f4_node * nchi); |
---|
| 214 | int removeChild(f4_node * nchi); |
---|
| 215 | int childCount(); // return no of children, 0, 1, or 2 |
---|
| 216 | int count(); // return no of nodes (recursive) |
---|
| 217 | f4_node * ordNode(int n); // returns the nth subnode (0-) |
---|
| 218 | f4_node * randomNode(); // returns a random subnode |
---|
| 219 | f4_node * randomNodeWithSize(int min, int max); // returns a random subnode with given size |
---|
| 220 | void sprintAdj(char *& buf); // print recursively |
---|
| 221 | f4_node * duplicate(); // create duplicate copy. recursive. |
---|
| 222 | void destroy(); // release memory. recursive. |
---|
[193] | 223 | private: |
---|
[196] | 224 | void sprint(SString & out); // print recursively |
---|
[193] | 225 | }; |
---|
| 226 | |
---|
| 227 | // convert f4 geno string to tree structure (internal) |
---|
| 228 | f4_node * f4_processtree(const char * geno); |
---|
| 229 | int f4_processrec(const char * genot, unsigned pos0, f4_node * parent); |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | #endif |
---|