[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[1212] | 2 | // Copyright (C) 1999-2023 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[193] | 4 | |
---|
[196] | 5 | // Copyright (C) 1999,2000 Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL |
---|
[760] | 6 | // Copyright (C) since 2001 Maciej Komosinski |
---|
| 7 | // 2018, Grzegorz Latosinski, added support for new API for neuron types and their properties |
---|
[196] | 8 | |
---|
[779] | 9 | #include "f4_conv.h" |
---|
[375] | 10 | #include <common/log.h> |
---|
[779] | 11 | #include "../genooperators.h" //for GENOPER_OK constant |
---|
[193] | 12 | |
---|
| 13 | #ifdef DMALLOC |
---|
| 14 | #include <dmalloc.h> |
---|
| 15 | #endif |
---|
| 16 | |
---|
| 17 | |
---|
[196] | 18 | GenoConv_f40::GenoConv_f40() |
---|
[193] | 19 | { |
---|
[196] | 20 | name = "Developmental encoding"; |
---|
| 21 | in_format = '4'; |
---|
| 22 | out_format = '0'; |
---|
| 23 | mapsupport = 1; |
---|
[193] | 24 | } |
---|
| 25 | |
---|
| 26 | |
---|
[774] | 27 | SString GenoConv_f40::convert(SString &in, MultiMap *map, bool using_checkpoints) |
---|
[193] | 28 | { |
---|
[196] | 29 | int res; |
---|
[774] | 30 | f4_Model *model = new f4_Model(); |
---|
[760] | 31 | res = model->buildFromF4(in, using_checkpoints); |
---|
[804] | 32 | if (GENOPER_OK != res) |
---|
| 33 | { |
---|
| 34 | delete model; |
---|
| 35 | return SString(); // oops |
---|
| 36 | } |
---|
[196] | 37 | if (NULL != map) |
---|
| 38 | // generate to-f0 conversion map |
---|
| 39 | model->getCurrentToF0Map(*map); |
---|
[534] | 40 | SString out = model->getF0Geno().getGenes(); |
---|
[196] | 41 | delete model; |
---|
| 42 | return out; |
---|
[193] | 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | GenoConv_F41_TestOnly::GenoConv_F41_TestOnly() |
---|
| 47 | { |
---|
[783] | 48 | name = "Only for testing, approximate f4->f1 converter"; //Do not use in production! (adam) |
---|
[196] | 49 | in_format = '4'; |
---|
| 50 | out_format = '1'; |
---|
| 51 | mapsupport = 0; |
---|
[193] | 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
[774] | 55 | SString GenoConv_F41_TestOnly::convert(SString &in, MultiMap *map, bool using_checkpoints) |
---|
[193] | 56 | { |
---|
[196] | 57 | int res; |
---|
[774] | 58 | f4_Model *model = new f4_Model(); |
---|
[760] | 59 | res = model->buildFromF4(in, using_checkpoints); |
---|
[804] | 60 | if (GENOPER_OK != res) |
---|
| 61 | { |
---|
| 62 | delete model; |
---|
| 63 | return SString(); // oops |
---|
| 64 | } |
---|
[196] | 65 | SString out; |
---|
| 66 | model->toF1Geno(out); |
---|
| 67 | delete model; |
---|
| 68 | return out; |
---|
[193] | 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | f4_Model::f4_Model() : Model() |
---|
| 73 | { |
---|
[196] | 74 | cells = NULL; |
---|
[193] | 75 | } |
---|
| 76 | |
---|
| 77 | f4_Model::~f4_Model() |
---|
| 78 | { |
---|
[196] | 79 | if (cells) delete cells; |
---|
[193] | 80 | } |
---|
| 81 | |
---|
[760] | 82 | int f4_Model::buildFromF4(SString &geno, bool using_checkpoints) |
---|
[193] | 83 | { |
---|
[196] | 84 | int i; |
---|
[193] | 85 | |
---|
[196] | 86 | error = GENOPER_OK; |
---|
| 87 | errorpos = -1; |
---|
[193] | 88 | |
---|
[196] | 89 | // build cells, and simulate |
---|
| 90 | if (cells) delete cells; |
---|
| 91 | cells = new f4_Cells(geno, 0); |
---|
[1227] | 92 | if (GENOPER_OK != cells->getErrorCode()) |
---|
[196] | 93 | { |
---|
[1227] | 94 | error = cells->getErrorCode(); |
---|
| 95 | errorpos = cells->getErrorPos(); |
---|
[196] | 96 | //delete cells; |
---|
| 97 | return error; |
---|
| 98 | } |
---|
[193] | 99 | |
---|
[196] | 100 | cells->simulate(); |
---|
[1227] | 101 | if (GENOPER_OK != cells->getErrorCode()) |
---|
[196] | 102 | { |
---|
[1227] | 103 | error = cells->getErrorCode(); |
---|
| 104 | errorpos = cells->getErrorPos(); |
---|
[196] | 105 | return error; |
---|
| 106 | } |
---|
[193] | 107 | |
---|
[196] | 108 | // reset recursive traverse flags |
---|
[1227] | 109 | for (i = 0; i < cells->cell_count; i++) |
---|
[196] | 110 | cells->C[i]->recProcessedFlag = 0; |
---|
[193] | 111 | |
---|
[760] | 112 | open(using_checkpoints); // begin model build |
---|
[193] | 113 | |
---|
[196] | 114 | // process every cell |
---|
| 115 | int res; |
---|
[1227] | 116 | for (i = 0; i < cells->cell_count; i++) |
---|
[196] | 117 | { |
---|
| 118 | res = buildModelRec(cells->C[i]); |
---|
| 119 | if (res) |
---|
| 120 | { |
---|
[1227] | 121 | logMessage("f4_Model", "buildFromF4", LOG_ERROR, "Error in building a Model"); |
---|
[196] | 122 | error = res; |
---|
| 123 | break; |
---|
| 124 | } |
---|
| 125 | } |
---|
[193] | 126 | |
---|
[196] | 127 | res = close(); |
---|
| 128 | if (0 == res) // invalid |
---|
| 129 | error = -10; |
---|
[193] | 130 | |
---|
[196] | 131 | return error; |
---|
[193] | 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
[774] | 135 | f4_Cell* f4_Model::getStick(f4_Cell *C) |
---|
[193] | 136 | { |
---|
[1227] | 137 | if (C->type == CELL_STICK) return C; |
---|
[196] | 138 | if (NULL != C->dadlink) |
---|
| 139 | return getStick(C->dadlink); |
---|
| 140 | // we have no more dadlinks, find any stick |
---|
[1227] | 141 | for (int i = 0; i < cells->cell_count; i++) |
---|
| 142 | if (cells->C[i]->type == CELL_STICK) |
---|
[196] | 143 | return cells->C[i]; |
---|
| 144 | // none! |
---|
[1227] | 145 | logMessage("f4_Model", "getStick", LOG_ERROR, "Not a single stick"); |
---|
[196] | 146 | return NULL; |
---|
[193] | 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
[774] | 150 | int f4_Model::buildModelRec(f4_Cell *C) |
---|
[193] | 151 | { |
---|
[196] | 152 | int partidx; |
---|
| 153 | int j, res; |
---|
| 154 | MultiRange range; |
---|
[193] | 155 | |
---|
[196] | 156 | if (C->recProcessedFlag) |
---|
| 157 | // already processed |
---|
| 158 | return 0; |
---|
[193] | 159 | |
---|
[196] | 160 | // mark it processed |
---|
| 161 | C->recProcessedFlag = 1; |
---|
[193] | 162 | |
---|
[196] | 163 | // make sure parent is a stick |
---|
| 164 | if (NULL != C->dadlink) |
---|
[1227] | 165 | if (C->dadlink->type != CELL_STICK) |
---|
[196] | 166 | { |
---|
[1227] | 167 | C->dadlink = getStick(C->dadlink); |
---|
[196] | 168 | } |
---|
[193] | 169 | |
---|
[196] | 170 | // make sure its parent is processed first |
---|
| 171 | if (NULL != C->dadlink) |
---|
| 172 | { |
---|
| 173 | res = buildModelRec(C->dadlink); |
---|
| 174 | if (res) return res; |
---|
| 175 | } |
---|
[193] | 176 | |
---|
[196] | 177 | char tmpLine[100]; |
---|
[193] | 178 | |
---|
[196] | 179 | range = C->genoRange; |
---|
[1227] | 180 | if (C->type == CELL_STICK) |
---|
[196] | 181 | { |
---|
| 182 | int jj_p1_refno; // save for later |
---|
| 183 | // first end is connected to dad, or new |
---|
| 184 | if (C->dadlink == NULL) |
---|
| 185 | { |
---|
| 186 | // new part object for firstend |
---|
| 187 | // coordinates are left to be computed by Model |
---|
[726] | 188 | sprintf(tmpLine, "fr=%g,ing=%g,as=%g", |
---|
[671] | 189 | /*1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation |
---|
[196] | 190 | //C->firstend.x, C->firstend.y, C->firstend.z |
---|
[1227] | 191 | ); |
---|
[726] | 192 | partidx = addFromString(PartType, tmpLine, &range); |
---|
[196] | 193 | if (partidx < 0) return -1; |
---|
[760] | 194 | this->checkpoint(); |
---|
[196] | 195 | jj_p1_refno = partidx; |
---|
| 196 | } |
---|
| 197 | else { |
---|
| 198 | // adjust mass/vol of first endpoint |
---|
| 199 | jj_p1_refno = C->dadlink->p2_refno; |
---|
[774] | 200 | Part *p1 = getPart(jj_p1_refno); |
---|
[196] | 201 | p1->mass += 1.0; |
---|
| 202 | // p1->volume += 1.0/C->P.mass; |
---|
| 203 | } |
---|
| 204 | // new part object for lastend |
---|
[726] | 205 | sprintf(tmpLine, "fr=%g,ing=%g,as=%g", |
---|
[196] | 206 | //C->lastend.x, C->lastend.y, C->lastend.z |
---|
[671] | 207 | /*"vol=" 1.0/C->P.mass,*/ C->P.friction, C->P.ingestion, C->P.assimilation |
---|
[1227] | 208 | ); |
---|
[726] | 209 | partidx = addFromString(PartType, tmpLine, &range); |
---|
[196] | 210 | if (partidx < 0) return -2; |
---|
| 211 | C->p2_refno = partidx; |
---|
[193] | 212 | |
---|
[196] | 213 | // new joint object |
---|
| 214 | // check that the part references are valid |
---|
| 215 | int jj_p2_refno = C->p2_refno; |
---|
| 216 | if ((jj_p1_refno < 0) || (jj_p1_refno >= getPartCount())) return -11; |
---|
| 217 | if ((jj_p2_refno < 0) || (jj_p2_refno >= getPartCount())) return -12; |
---|
[830] | 218 | sprintf(tmpLine, "p1=%d,p2=%d,dx=%g,dy=0,dz=0,rx=%g,ry=0,rz=%g"\ |
---|
[196] | 219 | ",stam=%g", |
---|
| 220 | jj_p1_refno, jj_p2_refno, |
---|
| 221 | // relative position -- always (len, 0, 0), along the stick |
---|
| 222 | // this is optional! |
---|
[671] | 223 | C->P.length, |
---|
[196] | 224 | // relative rotation |
---|
| 225 | C->xrot, C->zrot, |
---|
| 226 | //C->P.ruch, // rotstif |
---|
[671] | 227 | C->P.stamina |
---|
[1227] | 228 | ); |
---|
[726] | 229 | partidx = addFromString(JointType, tmpLine, &range); |
---|
[196] | 230 | if (partidx < 0) return -13; |
---|
[760] | 231 | this->checkpoint(); |
---|
[196] | 232 | C->joint_refno = partidx; |
---|
| 233 | } |
---|
[193] | 234 | |
---|
[1227] | 235 | if (C->type == CELL_NEURON) |
---|
[196] | 236 | { |
---|
[774] | 237 | const char* nclass = C->neuclass->name.c_str(); |
---|
[760] | 238 | int partno, jointno; |
---|
| 239 | if (C->neuclass->getPreferredLocation() == 0) |
---|
| 240 | { |
---|
| 241 | if (strcmp(nclass, "N") == 0) |
---|
| 242 | { |
---|
| 243 | partno = C->dadlink->p2_refno; |
---|
| 244 | if ((partno < 0) || (partno >= getPartCount())) return -21; |
---|
[830] | 245 | else sprintf(tmpLine, "p=%d,d=\"N:in=%g,fo=%g,si=%g\"", partno, C->inertia, C->force, C->sigmo); |
---|
[760] | 246 | } |
---|
| 247 | else |
---|
| 248 | { |
---|
| 249 | sprintf(tmpLine, "d=\"%s\"", nclass); |
---|
| 250 | } |
---|
| 251 | partidx = addFromString(NeuronType, tmpLine, &range); |
---|
| 252 | if (partidx < 0) return -22; |
---|
| 253 | this->checkpoint(); |
---|
| 254 | C->neuro_refno = partidx; |
---|
| 255 | } |
---|
| 256 | else if (C->neuclass->getPreferredLocation() == 1) // attached to Part or have no required attachment - also part |
---|
| 257 | { |
---|
| 258 | partno = C->dadlink->p2_refno; |
---|
| 259 | if ((partno < 0) || (partno >= getPartCount())) return -21; |
---|
[1227] | 260 | |
---|
[760] | 261 | if (strcmp(nclass, "N") == 0) |
---|
[831] | 262 | sprintf(tmpLine, "p=%d,d=\"N:in=%g,fo=%g,si=%g\"", partno, C->inertia, C->force, C->sigmo); |
---|
[760] | 263 | else |
---|
[830] | 264 | sprintf(tmpLine, "p=%d,d=\"%s\"", partno, nclass); |
---|
[1227] | 265 | |
---|
[760] | 266 | partidx = addFromString(NeuronType, tmpLine, &range); |
---|
| 267 | if (partidx < 0) return -22; |
---|
| 268 | this->checkpoint(); |
---|
| 269 | C->neuro_refno = partidx; |
---|
| 270 | } |
---|
[1212] | 271 | else // attached to Joint, assume there are only three possibilities of getPreferredLocation() |
---|
[760] | 272 | { |
---|
| 273 | jointno = C->dadlink->joint_refno; |
---|
[1227] | 274 | |
---|
| 275 | if (strcmp(nclass, "@") == 0) |
---|
| 276 | sprintf(tmpLine, "j=%d,d=\"@:p=%g\"", jointno, C->P.muscle_power); |
---|
| 277 | else if (strcmp(nclass, "|") == 0) |
---|
| 278 | sprintf(tmpLine, "j=%d,d=\"|:p=%g,r=%g\"", jointno, C->P.muscle_power, C->mz); |
---|
| 279 | else |
---|
| 280 | sprintf(tmpLine, "j=%d,d=\"%s\"", jointno, nclass); |
---|
| 281 | |
---|
[760] | 282 | partidx = addFromString(NeuronType, tmpLine, &range); |
---|
| 283 | if (partidx < 0) return -32; |
---|
| 284 | this->checkpoint(); |
---|
| 285 | } |
---|
[196] | 286 | C->neuro_refno = partidx; |
---|
| 287 | int n_refno = C->neuro_refno; |
---|
[193] | 288 | |
---|
[1227] | 289 | for (j = 0; j < C->conns_count; j++) |
---|
[196] | 290 | { |
---|
[1227] | 291 | if (C->conns[j]->from != NULL) |
---|
| 292 | buildModelRec(C->conns[j]->from); |
---|
[193] | 293 | |
---|
[196] | 294 | tmpLine[0] = 0; |
---|
[1227] | 295 | if (C->conns[j]->from == NULL) |
---|
[760] | 296 | { |
---|
[1227] | 297 | logMessage("f4_Model", "buildModelRec", LOG_ERROR, "Old code for sensors as inputs embedded in [connection]: C->conns[j]->from == NULL"); |
---|
[760] | 298 | } |
---|
[196] | 299 | int from = -1; |
---|
[1227] | 300 | if (C->conns[j]->from != NULL) // input from another neuron |
---|
| 301 | from = C->conns[j]->from->neuro_refno; |
---|
[196] | 302 | if (from >= 0) |
---|
| 303 | { |
---|
[1227] | 304 | sprintf(tmpLine, "%d,%d,%g", n_refno, from, C->conns[j]->weight); |
---|
[726] | 305 | if (addFromString(NeuronConnectionType, tmpLine, &range) < 0) return -35; |
---|
[760] | 306 | this->checkpoint(); |
---|
[196] | 307 | } |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | return 0; |
---|
[193] | 311 | } |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | void f4_Model::toF1Geno(SString &out) |
---|
| 315 | { |
---|
[196] | 316 | cells->toF1Geno(out); |
---|
[193] | 317 | } |
---|