[1] | 1 | /* |
---|
| 2 | * conv_f8tof1.cpp |
---|
| 3 | * L-systemToF1 |
---|
| 4 | * |
---|
| 5 | * Created by Maciej Wajcht on 08-03-21. |
---|
| 6 | * Copyright 2008 __MyCompanyName__. All rights reserved. |
---|
| 7 | * |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include "conv_f8tof1.h" |
---|
| 11 | #include "conv_f8_utils.h" |
---|
| 12 | #include <sstream> |
---|
| 13 | #include <string> |
---|
| 14 | #include <algorithm> |
---|
| 15 | #include "lexglobal.h" |
---|
| 16 | #include "conv_f8tof1_scanner.h" |
---|
| 17 | namespace prs { |
---|
| 18 | #include "conv_f8tof1_grammar.c" |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | #if CONV_DEBUG > 0 |
---|
| 22 | #include <iostream> |
---|
| 23 | #endif |
---|
| 24 | |
---|
| 25 | #define CONV_DEBUG 0 //0 - off, 1 - minimal, 2 - full |
---|
| 26 | |
---|
| 27 | using namespace std; |
---|
| 28 | |
---|
[35] | 29 | Lsystem::Lsystem() |
---|
| 30 | { |
---|
| 31 | for(int i=0;i<strlen(GenoConv_F8ToF1::simpleprods);i++) |
---|
| 32 | { |
---|
| 33 | string s(1,GenoConv_F8ToF1::simpleprods[i]); |
---|
| 34 | SString ss(s.c_str()); |
---|
| 35 | this->primitiveProductions.insert(make_pair(s, new PrimitiveProduction(ss))); |
---|
| 36 | } |
---|
[1] | 37 | } |
---|
[35] | 38 | |
---|
[1] | 39 | Lsystem::~Lsystem() { |
---|
[35] | 40 | for (map<string, Production*>::iterator iter = this->productions.begin(); |
---|
[1] | 41 | iter != this->productions.end(); iter++) { |
---|
| 42 | delete iter->second; |
---|
| 43 | } |
---|
[35] | 44 | for (map<string, PrimitiveProduction*>::iterator iter = this->primitiveProductions.begin(); |
---|
[1] | 45 | iter != this->primitiveProductions.end(); iter++) { |
---|
| 46 | delete iter->second; |
---|
| 47 | } |
---|
| 48 | for (map<string, ParamProduction*>::iterator iter = this->paramProductions.begin(); |
---|
| 49 | iter != this->paramProductions.end(); iter++) { |
---|
| 50 | delete iter->second; |
---|
| 51 | } |
---|
| 52 | for (vector<NeuronProduction*>::iterator iter = this->neuronProductions.begin(); |
---|
| 53 | iter != this->neuronProductions.end(); iter++) { |
---|
| 54 | delete *iter; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | PrimitiveProduction* Lsystem::getPrimitiveProduction(SString name) { |
---|
| 59 | string sname = sstringToString(name); |
---|
| 60 | if (this->primitiveProductions.find(sname) == this->primitiveProductions.end()) { |
---|
| 61 | PrimitiveProduction *pp = new PrimitiveProduction(name); |
---|
| 62 | this->primitiveProductions.insert(make_pair(sname, pp)); |
---|
| 63 | } |
---|
| 64 | return this->primitiveProductions[sname]; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | ParamProduction* Lsystem::getParamProduction(SString name) { |
---|
| 68 | string sname = sstringToString(name); |
---|
| 69 | if (this->paramProductions.find(sname) == this->paramProductions.end()) { |
---|
| 70 | ParamProduction *pp = new ParamProduction(name); |
---|
| 71 | this->paramProductions.insert(make_pair(sname, pp)); |
---|
| 72 | } |
---|
| 73 | return this->paramProductions[sname]; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | SString Lsystem::toString() { |
---|
| 77 | this->removeEmptySubproductionsAndProductions(); |
---|
| 78 | SString result = ""; |
---|
| 79 | result += SString::valueOf(this->iterations) + "\n"; |
---|
| 80 | for (map<string, double>::iterator it = this->startParams.begin(); |
---|
| 81 | it != this->startParams.end(); it++) { |
---|
| 82 | result += stringToSString(it->first) + "=" + SString::valueOf(it->second) + "\n"; |
---|
| 83 | } |
---|
| 84 | result += SString("---") + "\n"; |
---|
| 85 | result += stringToSString(this->firstProductionName) + "\n"; |
---|
| 86 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
| 87 | prodIter != this->productions.end(); prodIter++) { |
---|
| 88 | Production *p = prodIter->second; |
---|
| 89 | result += p->name + "("; |
---|
| 90 | for (int i = 1; i <= p->parameters.size(); i++) { |
---|
| 91 | result += p->parameters.getParameterName(i); |
---|
| 92 | if (i < p->parameters.size()) { |
---|
| 93 | result += ","; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | result += "):"; |
---|
| 97 | for (int subprodIter = 0; subprodIter < p->subproductions.size(); subprodIter++) { |
---|
| 98 | SubProduction *sp = &(p->subproductions[subprodIter]); |
---|
| 99 | for (int condIter = 0; condIter < sp->conditions.size(); condIter++) { |
---|
| 100 | Condition *c = &(sp->conditions[condIter]); |
---|
| 101 | RelationType r = c->relation; |
---|
| 102 | SString op = (r == r_greater) ? ">" : (r == r_greaterEqual) ? ">=" : (r == r_less) ? "<" : |
---|
| 103 | (r == r_lessEqual) ? "<=" : (r == r_equal) ? "==" : (r == r_different) ? "!=" : ""; |
---|
| 104 | result += c->parameter + op + SString::valueOf(c->value); |
---|
| 105 | if (condIter != sp->conditions.size() - 1) { |
---|
| 106 | result += ","; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | if (sp->conditions.size() > 0) { |
---|
| 110 | result += SString("|"); |
---|
| 111 | } |
---|
| 112 | for (vector<ActionStrP>::iterator actionIter = sp->actions.begin(); |
---|
| 113 | actionIter != sp->actions.end(); actionIter++) { |
---|
| 114 | ActionStrP *a = &(*actionIter); |
---|
| 115 | if (a->action == NULL) { |
---|
| 116 | continue; |
---|
| 117 | } |
---|
| 118 | result += a->action->getF8Representation(); |
---|
| 119 | if (!a->action->ignoreParams) { |
---|
| 120 | result += SString("("); |
---|
| 121 | for (int paramIter = 0; paramIter < a->params.size(); paramIter++) { |
---|
| 122 | result += convertReversePolishNotationToNatural(a->params[paramIter]); |
---|
| 123 | if (paramIter != a->params.size() - 1) { |
---|
| 124 | result += ","; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | result += SString(")"); |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | if (subprodIter != p->subproductions.size() - 1) { |
---|
| 131 | result += SString(":"); |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | result += SString("\n"); |
---|
| 135 | } |
---|
| 136 | return result; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | vector<Action*> Lsystem::getAllActions(bool normal, bool primitives, bool params, bool neurons) { |
---|
| 140 | vector<Action*> actions; |
---|
| 141 | if (normal) { |
---|
| 142 | for (map<string, Production*>::iterator iter = this->productions.begin(); |
---|
| 143 | iter != this->productions.end(); iter++) { |
---|
| 144 | actions.push_back(iter->second); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | if (primitives) { |
---|
| 148 | for (map<string, PrimitiveProduction*>::iterator iter = this->primitiveProductions.begin(); |
---|
| 149 | iter != this->primitiveProductions.end(); iter++) { |
---|
| 150 | actions.push_back(iter->second); |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | if (params) { |
---|
| 154 | for (map<string, ParamProduction*>::iterator iter = this->paramProductions.begin(); |
---|
| 155 | iter != this->paramProductions.end(); iter++) { |
---|
| 156 | actions.push_back(iter->second); |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | if (neurons) { |
---|
[33] | 160 | for (vector<NeuronProduction*>::iterator iter = this->neuronProductions.begin(); |
---|
[1] | 161 | iter != this->neuronProductions.end(); iter++) { |
---|
| 162 | actions.push_back(*iter); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | return actions; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | void Lsystem::removeEmptySubproductionsAndProductions() { |
---|
| 169 | /* po wykonaniu moga pozostac puste subprodukcje (usuwanie wywolan do usunietych wczesniej produkcji |
---|
| 170 | * ale i tak znikna one przy kolejnym wywolaniu, tak wiec ilosc smieci nie powinna byc razaca |
---|
| 171 | */ |
---|
| 172 | |
---|
| 173 | //delete empty subproductions |
---|
| 174 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
| 175 | prodIter != this->productions.end(); prodIter++) { |
---|
| 176 | vector<SubProduction> *subproductions = &(prodIter->second->subproductions); |
---|
[24] | 177 | for (vector<SubProduction>::iterator i = subproductions->begin(); i != subproductions->end(); /*nothing*/) { |
---|
| 178 | if ((*i).actions.size() == 0) { |
---|
| 179 | i = subproductions->erase(i); |
---|
| 180 | } else { |
---|
| 181 | i++; |
---|
[1] | 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | //find empty productions |
---|
| 187 | vector<SString> emptyProductionNames; |
---|
| 188 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
| 189 | prodIter != this->productions.end(); prodIter++) { |
---|
| 190 | if (prodIter->second->subproductions.size() == 0 && |
---|
| 191 | sstringToString(prodIter->second->name) != this->firstProductionName) { |
---|
| 192 | emptyProductionNames.push_back(prodIter->second->name); |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | //delete calls to empty productions |
---|
| 197 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
| 198 | prodIter != this->productions.end(); prodIter++) { |
---|
| 199 | vector<SubProduction> *subproductions = &(prodIter->second->subproductions); |
---|
| 200 | for (vector<SubProduction>::iterator subProdIter = subproductions->begin(); |
---|
| 201 | subProdIter != subproductions->end(); subProdIter++) { |
---|
| 202 | SubProduction *sp = &(*subProdIter); |
---|
[24] | 203 | for (vector<ActionStrP>::iterator actionIter = sp->actions.begin(); |
---|
| 204 | actionIter != sp->actions.end(); /*nothing*/) { |
---|
| 205 | bool deleted = false; |
---|
| 206 | if ((*actionIter).action != NULL) { |
---|
[1] | 207 | vector<SString>::iterator result = find(emptyProductionNames.begin(), emptyProductionNames.end(), |
---|
[24] | 208 | (*actionIter).action->name); |
---|
[1] | 209 | if (result != emptyProductionNames.end()) { //emptyProductionNames contains the action name |
---|
[24] | 210 | actionIter = sp->actions.erase(actionIter); |
---|
| 211 | deleted = true; |
---|
[1] | 212 | } |
---|
| 213 | } |
---|
[24] | 214 | if (!deleted) { |
---|
| 215 | actionIter++; |
---|
| 216 | } |
---|
[1] | 217 | } |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | //delete empty productions |
---|
| 222 | for(int i = 0; i < emptyProductionNames.size(); i++) { |
---|
| 223 | this->productions.erase(sstringToString(emptyProductionNames[i])); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | ostream& operator<<(ostream& os, const Condition& c) { |
---|
| 228 | RelationType r = c.relation; |
---|
| 229 | SString op = (r == r_greater) ? ">" : (r == r_greaterEqual) ? ">=" : (r == r_less) ? "<" : |
---|
| 230 | (r == r_lessEqual) ? "<=" : (r == r_equal) ? "==" : (r == r_different) ? "!=" : ""; |
---|
| 231 | os << c.parameter << " " << op << " " << c.value; |
---|
| 232 | return os; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | const double ParameterCollection::getValue(int position) { |
---|
| 236 | string name = this->parameters[position - 1]; |
---|
| 237 | return this->paramValues[name]; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | const double ParameterCollection::getValue(SString name) { |
---|
| 241 | return this->paramValues[sstringToString(name)]; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | const SString ParameterCollection::getParameterName(int position) { |
---|
| 245 | return stringToSString(this->parameters[position - 1]); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | const int ParameterCollection::getParameterPosition(SString name) { |
---|
| 249 | for (int i = 0; i < this->parameters.size(); i++) { |
---|
| 250 | string &s = this->parameters[i]; |
---|
| 251 | if (s == sstringToString(name)) { |
---|
| 252 | return i + 1; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | return -1; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | void ParameterCollection::setValue(int position, double value) { |
---|
| 259 | string name = this->parameters[position - 1]; |
---|
| 260 | this->paramValues[name] = value; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | void ParameterCollection::setValue(SString name, double value) { |
---|
| 264 | this->paramValues[sstringToString(name)] = value; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | void ParameterCollection::addParameter(SString name, int position, double value) { |
---|
| 268 | //TODO sprawdzenie czy position > 0 |
---|
| 269 | string sname = sstringToString(name); |
---|
| 270 | if (position == -1) { |
---|
[33] | 271 | this->parameters.push_back(sname); |
---|
[1] | 272 | this->paramValues[sname] = value; |
---|
| 273 | } else { |
---|
| 274 | this->parameters.reserve(position); |
---|
| 275 | this->parameters.insert(this->parameters.begin() + (position -1), sname); |
---|
| 276 | this->paramValues[sname] = value; |
---|
| 277 | } |
---|
| 278 | //this->paramValues[name] = value; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | const int ParameterCollection::size() { |
---|
| 282 | return this->parameters.size(); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | void ParameterCollection::removeParameter(int position) { |
---|
| 286 | string name = this->parameters[position - 1]; |
---|
| 287 | vector<string>::iterator it = this->parameters.begin() + (position - 1); |
---|
| 288 | this->parameters.erase(it); |
---|
| 289 | this->paramValues.erase(name); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | void ParameterCollection::removeParameter(SString name) { |
---|
| 293 | int idx = getParameterPosition(name); |
---|
| 294 | this->removeParameter(idx); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | bool ParameterCollection::paramExist(SString name) { |
---|
| 298 | string sname = sstringToString(name); |
---|
| 299 | for (vector<string>::iterator it = this->parameters.begin(); it != this->parameters.end(); it++) { |
---|
| 300 | if ((*it) == sname) { |
---|
| 301 | return true; |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | return false; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | PrimitiveProduction::PrimitiveProduction(const SString command) { |
---|
| 308 | this->f8command = command; |
---|
| 309 | this->ignoreParams = true; |
---|
| 310 | if (command == SString("[")) { |
---|
| 311 | this->f1command = SString("("); |
---|
| 312 | this->name = SString("_primitive production '[' => '('"); |
---|
| 313 | } else if (command == SString("]")) { |
---|
| 314 | this->f1command = SString(")"); |
---|
| 315 | this->name = SString("_primitive production ']' => ')'"); |
---|
| 316 | } else if (command == SString("^")) { |
---|
| 317 | this->f1command = SString(","); |
---|
| 318 | this->name = SString("_primitive production '^' => ','"); |
---|
| 319 | } else { |
---|
| 320 | this->f1command = command; |
---|
| 321 | this->name = SString("_primitive production '") + command + "'"; |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | const SString PrimitiveProduction::getF1Genotype(const vector<double> params) { |
---|
| 326 | #if CONV_DEBUG > 1 |
---|
| 327 | cout << "@@@@@ Prymityw: " << this->f1command << endl; |
---|
| 328 | #endif |
---|
| 329 | return SString(this->f1command); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | const list<ActionP> PrimitiveProduction::getActionList(const vector<double> param) { |
---|
| 333 | list<ActionP> l; |
---|
| 334 | ActionP ap; |
---|
| 335 | ap.action = this; |
---|
| 336 | ap.params = param; |
---|
| 337 | l.push_back(ap); |
---|
| 338 | return l; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | const SString PrimitiveProduction::getF8Representation() { |
---|
| 342 | return this->f8command; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | ParamProduction::ParamProduction(const SString paramName) { |
---|
| 346 | this->paramName = paramName; |
---|
| 347 | this->name = SString("_param production") + paramName; |
---|
| 348 | this->ignoreParams = true; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | const SString ParamProduction::getF1Genotype(const vector<double> params) { |
---|
| 352 | SString s = SString::valueOf(params[0]); |
---|
| 353 | #if CONV_DEBUG > 1 |
---|
[26] | 354 | cout << "@@@@@ Param value: " << this->paramName << ": " << params[0] << endl; |
---|
[1] | 355 | #endif |
---|
| 356 | return s; |
---|
| 357 | |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | const list<ActionP> ParamProduction::getActionList(const vector<double> param) { |
---|
| 361 | list<ActionP> l; |
---|
| 362 | ActionP ap; |
---|
| 363 | ap.action = this; |
---|
| 364 | ap.params = param; |
---|
| 365 | l.push_back(ap); |
---|
| 366 | return l; |
---|
| 367 | |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | const SString ParamProduction::getF8Representation() { |
---|
| 371 | return this->paramName; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | NeuronProduction::NeuronProduction(const SString body) { |
---|
| 375 | this->body = body; |
---|
| 376 | this->name = SString("_neuron production") + body; |
---|
| 377 | this->ignoreParams = true; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | const SString NeuronProduction::getF1Genotype(const vector<double> params) { |
---|
| 381 | return this->body; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | const list<ActionP> NeuronProduction::getActionList(const vector<double> param) { |
---|
| 385 | list<ActionP> l; |
---|
| 386 | ActionP ap; |
---|
| 387 | ap.action = this; |
---|
| 388 | ap.params = param; |
---|
| 389 | l.push_back(ap); |
---|
| 390 | return l; |
---|
| 391 | |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | const SString NeuronProduction::getF8Representation() { |
---|
| 395 | return this->body; |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | Production::Production() { |
---|
| 399 | this->ignoreParams = false; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | const SString Production::getF1Genotype(const vector<double> params) { |
---|
[26] | 403 | return SString(); //return empty |
---|
[1] | 404 | } |
---|
| 405 | |
---|
| 406 | const list<ActionP> Production::getActionList(const vector<double> params) { |
---|
| 407 | list<ActionP> l; |
---|
| 408 | #if CONV_DEBUG > 1 |
---|
| 409 | cout << "params.size(): " << params.size() << ", this->parameters.size(): " << this->parameters.size() << endl; |
---|
| 410 | #endif |
---|
| 411 | for (int i = 0; i < params.size() && i < this->parameters.size(); i++) { |
---|
| 412 | this->parameters.setValue(i + 1, params[i]); |
---|
| 413 | } |
---|
| 414 | |
---|
[26] | 415 | //iterate through subproductions |
---|
[1] | 416 | for (vector<SubProduction>::iterator subProdIter = this->subproductions.begin(); |
---|
| 417 | subProdIter != this->subproductions.end(); subProdIter++) { |
---|
| 418 | #if CONV_DEBUG > 1 |
---|
| 419 | cout << "this->subproductions.size(): " << this->subproductions.size() << endl; |
---|
| 420 | #endif |
---|
| 421 | SubProduction &sp = *subProdIter; |
---|
| 422 | bool conditionsOK = true; |
---|
[26] | 423 | //check conditions of subproduction |
---|
[1] | 424 | for (vector<Condition>::iterator condIter = sp.conditions.begin(); |
---|
| 425 | condIter != sp.conditions.end(); condIter++) { |
---|
| 426 | if (conditionsOK == false) { |
---|
[26] | 427 | break; //because it's no use checking further |
---|
[1] | 428 | } |
---|
| 429 | Condition &c = *condIter; |
---|
| 430 | switch (c.relation) { |
---|
| 431 | case r_greater: |
---|
| 432 | if (this->parameters.getValue(c.parameter) <= c.value) { |
---|
| 433 | conditionsOK = false; |
---|
| 434 | } |
---|
| 435 | break; |
---|
| 436 | case r_greaterEqual: |
---|
| 437 | if (this->parameters.getValue(c.parameter) < c.value) { |
---|
| 438 | conditionsOK = false; |
---|
| 439 | } |
---|
| 440 | break; |
---|
| 441 | case r_less: |
---|
| 442 | if (this->parameters.getValue(c.parameter) >= c.value) { |
---|
| 443 | conditionsOK = false; |
---|
| 444 | } |
---|
| 445 | break; |
---|
| 446 | case r_lessEqual: |
---|
| 447 | if (this->parameters.getValue(c.parameter) > c.value) { |
---|
| 448 | conditionsOK = false; |
---|
| 449 | } |
---|
| 450 | break; |
---|
| 451 | case r_equal: |
---|
| 452 | if (this->parameters.getValue(c.parameter) != c.value) { |
---|
| 453 | conditionsOK = false; |
---|
| 454 | } |
---|
| 455 | break; |
---|
| 456 | case r_different: |
---|
| 457 | if (this->parameters.getValue(c.parameter) == c.value) { |
---|
| 458 | conditionsOK = false; |
---|
| 459 | } |
---|
| 460 | break; |
---|
| 461 | } |
---|
| 462 | } |
---|
| 463 | if (conditionsOK) { |
---|
[26] | 464 | //iterate through each action in subproduction |
---|
[1] | 465 | for (int i = 0; i < sp.actions.size(); i++) { |
---|
| 466 | #if CONV_DEBUG > 1 |
---|
| 467 | cout << "sp.actions.size(): " << sp.actions.size() << endl; |
---|
| 468 | #endif |
---|
| 469 | Action *action = sp.actions[i].action; |
---|
| 470 | vector<SString> strParams = sp.actions[i].params; |
---|
| 471 | vector<double> params; |
---|
[26] | 472 | //replace parameter names with values |
---|
[1] | 473 | for (vector<SString>::iterator paramIter = strParams.begin(); |
---|
| 474 | paramIter != strParams.end(); paramIter++) { |
---|
| 475 | SString parameter; |
---|
| 476 | SString element; |
---|
| 477 | int pos = 0; |
---|
| 478 | while ((*paramIter).getNextToken(pos, element, ';')) { |
---|
| 479 | if (element[0] == 'n') { |
---|
| 480 | double val = this->parameters.getValue(element); |
---|
| 481 | parameter += SString::valueOf(val) + ";"; |
---|
| 482 | } else { |
---|
| 483 | parameter += element + ";"; |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | params.push_back(parseExpression(parameter)); |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | ActionP ap; |
---|
| 490 | ap.action = action; |
---|
| 491 | ap.params = params; |
---|
| 492 | l.push_back(ap); |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | return l; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | const SString Production::getF8Representation() { |
---|
| 501 | return this->name; |
---|
| 502 | } |
---|
| 503 | |
---|
[33] | 504 | |
---|
| 505 | |
---|
| 506 | |
---|
| 507 | |
---|
| 508 | |
---|
| 509 | |
---|
| 510 | |
---|
| 511 | |
---|
[35] | 512 | const char* GenoConv_F8ToF1::simpleprods="X[]^RrLlAaCcFfMmSsIiQqWw"; //Ee skipped |
---|
| 513 | |
---|
| 514 | |
---|
[33] | 515 | #define FIELDSTRUCT GenoConv_F8ToF1 |
---|
| 516 | |
---|
| 517 | static ParamEntry GENOCONVf8param_tab[]= |
---|
| 518 | { |
---|
[36] | 519 | {"Genetics: f8: Converter",1,1,}, |
---|
[33] | 520 | {"f8conv_maxlen", 0, 0, "Maximal genotype length", "d 10 10000 500", FIELD(maxF1Length),"Maximal length of the resulting f1 genotype, in characters. If the f8 L-system produces longer f1 genotype, it will be considered incorrect.", }, |
---|
| 521 | {0,}, |
---|
| 522 | }; |
---|
| 523 |
|
---|
| 524 | #undef FIELDSTRUCT
|
---|
[36] | 525 |
|
---|
| 526 | Param GenoConv_F8ToF1::staticpar;
|
---|
| 527 |
|
---|
[33] | 528 | |
---|
| 529 | GenoConv_F8ToF1::GenoConv_F8ToF1() |
---|
| 530 | { |
---|
[41] | 531 | name = "Generative encoding"; |
---|
[33] | 532 | in_format = '8'; |
---|
| 533 | out_format = '1'; |
---|
| 534 | mapsupport = 0; |
---|
| 535 | maxF1Length = 500; |
---|
| 536 | par.setParamTab(GENOCONVf8param_tab); |
---|
| 537 | par.select(this); |
---|
| 538 | par.setDefault(); |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | |
---|
[1] | 542 | vector<SString> GenoConv_F8ToF1::readProductionNames(const SString &in) { |
---|
| 543 | vector<SString> names; |
---|
| 544 | SString line; |
---|
| 545 | int pos = 0; |
---|
| 546 | bool afterFirstProd = false; |
---|
| 547 | //ParsingStatus status = firstLine; |
---|
| 548 | while (in.getNextToken(pos, line, '\n')) { |
---|
| 549 | #if CONV_DEBUG > 1 |
---|
[26] | 550 | std::cout << "### Line: " << line << std::endl; |
---|
[1] | 551 | #endif |
---|
| 552 | if (line.startsWith("P") && line.indexOf('(', 0) == -1) { |
---|
| 553 | afterFirstProd = true; |
---|
| 554 | continue; |
---|
| 555 | } |
---|
| 556 | if (!afterFirstProd) { |
---|
| 557 | continue; |
---|
| 558 | } |
---|
| 559 | int lParenIndex = line.indexOf('(', 0); |
---|
| 560 | if (line.startsWith("P") && lParenIndex != -1) { |
---|
| 561 | SString prodName = line.substr(0, lParenIndex); |
---|
| 562 | #if CONV_DEBUG > 1 |
---|
[26] | 563 | std::cout << "###Production: " << prodName << std::endl; |
---|
[1] | 564 | #endif |
---|
| 565 | names.push_back(prodName); |
---|
| 566 | } |
---|
| 567 | } |
---|
| 568 | return names; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | bool GenoConv_F8ToF1::checkSyntax(const char *geno) { |
---|
| 572 | return this->parseInput(geno, NULL); |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | SString GenoConv_F8ToF1::convert(SString &in, MultiMap *mmap) { |
---|
| 576 | #if CONV_DEBUG > 0 |
---|
| 577 | cout << "convert() start" << endl; |
---|
| 578 | #endif |
---|
| 579 | SString dst = ""; |
---|
| 580 | const char* src = in; |
---|
| 581 | |
---|
[32] | 582 | if (in.len() < 1 || !this->checkSyntax(src)) { |
---|
[1] | 583 | return SString(); |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | Lsystem *lsystem = this->createLsystem(in); |
---|
| 587 | if (lsystem == NULL) { |
---|
| 588 | return SString(); |
---|
| 589 | } |
---|
| 590 | if (lsystem->firstProductionName.empty()) { |
---|
[32] | 591 | delete lsystem; |
---|
[1] | 592 | return SString(); |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | #if CONV_DEBUG > 0 |
---|
| 596 | for (map<string, Production*>::iterator i1 = lsystem->productions.begin(); i1 != lsystem->productions.end(); i1++) { |
---|
| 597 | Production *p = i1->second; |
---|
| 598 | cout << "Production: " << p->name << endl; |
---|
| 599 | for (vector<SubProduction>::iterator i2 = p->subproductions.begin(); i2 != p->subproductions.end(); i2++) { |
---|
| 600 | SubProduction sp = *i2; |
---|
| 601 | cout << "\tConditions" << endl; |
---|
| 602 | for (vector<Condition>::iterator i3 = sp.conditions.begin(); i3 != sp.conditions.end(); i3++) { |
---|
| 603 | cout << "\t\t" << *i3 << endl; |
---|
| 604 | } |
---|
| 605 | cout << "\tAction : params" << endl; |
---|
| 606 | for (int i = 0; i < sp.actions.size(); i++) { |
---|
| 607 | Action *a = sp.actions[i].action; |
---|
| 608 | vector<SString> strParams = sp.actions[i].params; |
---|
| 609 | cout << "\t\t" << a->name << " : "; |
---|
| 610 | for (vector<SString>::iterator i4 = strParams.begin(); i4 != strParams.end(); i4++) { |
---|
| 611 | cout << *i4 << ", "; |
---|
| 612 | } |
---|
| 613 | cout << endl; |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | } |
---|
| 617 | #endif |
---|
| 618 | #if CONV_DEBUG > 1 |
---|
| 619 | cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
| 620 | #endif |
---|
| 621 | |
---|
| 622 | //cout << "convert() 1" << endl; |
---|
[26] | 623 | //set parameters for start production |
---|
[1] | 624 | Production *firstProduction = lsystem->productions[lsystem->firstProductionName]; |
---|
[32] | 625 | if (firstProduction==NULL) { |
---|
| 626 | delete lsystem; |
---|
| 627 | return SString(""); //should never happen because of syntax validation |
---|
| 628 | } |
---|
| 629 | |
---|
[1] | 630 | vector<double> params; |
---|
[9] | 631 | params.assign(lsystem->startParams.size(), 0.0); |
---|
[1] | 632 | //cout << "startParams->size: " << lsystem->startParams.size() << endl; |
---|
[32] | 633 | for (map<string, double>::iterator iter = lsystem->startParams.begin(); |
---|
| 634 | iter != lsystem->startParams.end(); iter++) |
---|
| 635 | { |
---|
[1] | 636 | int position = firstProduction->parameters.getParameterPosition(stringToSString(iter->first)); |
---|
| 637 | //cout << "position of " << iter->first << ": " << position << endl; |
---|
[32] | 638 | //params.insert(params.begin() + (position - 1), iter->second); //no need because the vector has required length (assign above) |
---|
| 639 | if (position>params.size()) { |
---|
| 640 | delete lsystem; |
---|
| 641 | return SString(""); |
---|
| 642 | } |
---|
| 643 | params[position - 1] = iter->second; |
---|
[1] | 644 | } |
---|
| 645 | |
---|
| 646 | //cout << "convert() 2" << endl; |
---|
| 647 | |
---|
| 648 | ActionP ap; |
---|
| 649 | ap.action = firstProduction; |
---|
| 650 | ap.params = params; |
---|
| 651 | |
---|
| 652 | list<ActionP> actionList; |
---|
| 653 | actionList.push_back(ap); |
---|
| 654 | |
---|
| 655 | //cout << "iterations: " << lsystem->iterations << endl; |
---|
| 656 | for (int i = 0; i < lsystem->iterations; i++) { |
---|
| 657 | //cout << "convert() 2.1" << endl; |
---|
| 658 | list<ActionP> newList; |
---|
| 659 | for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) { |
---|
| 660 | //cout << "convert() 2.1.1" << endl; |
---|
| 661 | Action *a = (*iter).action; |
---|
| 662 | vector<double> p = (*iter).params; |
---|
| 663 | if (a != NULL) { |
---|
| 664 | list<ActionP> tmpList = a->getActionList(p); |
---|
| 665 | newList.insert(newList.end(), tmpList.begin(), tmpList.end()); |
---|
| 666 | } |
---|
| 667 | } |
---|
| 668 | actionList = newList; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | #if CONV_DEBUG > 1 |
---|
| 672 | cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
| 673 | for (list<ActionP>::iterator it = actionList.begin(); it != actionList.end(); it++) { |
---|
| 674 | cout << (*it).action->name << "("; |
---|
| 675 | for (vector<double>::iterator it2 = (*it).params.begin(); it2 != (*it).params.end(); it2++) { |
---|
| 676 | cout << *it2 << ", "; |
---|
| 677 | } |
---|
| 678 | cout << ")" << endl; |
---|
| 679 | } |
---|
| 680 | cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
| 681 | #endif |
---|
| 682 | for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) { |
---|
| 683 | Action *a = (*iter).action; |
---|
| 684 | vector<double> p = (*iter).params; |
---|
| 685 | if (a != NULL) { |
---|
| 686 | dst += a->getF1Genotype(p); |
---|
[25] | 687 | if (dst.len() > maxF1Length) { |
---|
[32] | 688 | delete lsystem; |
---|
[25] | 689 | return SString(); //genotype becomes too long so we abort conversion |
---|
[7] | 690 | } |
---|
[1] | 691 | } |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | delete lsystem; |
---|
| 695 | |
---|
| 696 | #if CONV_DEBUG > 0 |
---|
| 697 | cout << "convert() end" << endl; |
---|
| 698 | #endif |
---|
| 699 | return dst; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | //Lsystem* GenoConv_F8ToF1::createLsystem(const SString &in) { |
---|
| 703 | Lsystem* GenoConv_F8ToF1::createLsystem(SString in) { |
---|
| 704 | #if CONV_DEBUG > 0 |
---|
| 705 | cout << "createLsystem() start" << endl; |
---|
| 706 | #endif |
---|
| 707 | Lsystem *lsys = new Lsystem(); |
---|
[32] | 708 | |
---|
[26] | 709 | //read production names and create objects for them |
---|
[1] | 710 | vector<SString> names = this->readProductionNames(in); |
---|
| 711 | for (vector<SString>::iterator nameIter = names.begin(); nameIter != names.end(); nameIter++) { |
---|
| 712 | Production *production = new Production(); |
---|
| 713 | production->name = *nameIter; |
---|
| 714 | //lsystem->productions.insert(make_pair(*nameIter, production)); |
---|
| 715 | lsys->productions[sstringToString(*nameIter)] = production; |
---|
| 716 | } |
---|
| 717 | |
---|
| 718 | #if CONV_DEBUG > 1 |
---|
| 719 | cout << "lsystemprodsize " << lsys->productions.size() << endl; |
---|
| 720 | for (map<string, Production*>::iterator iii = lsys->productions.begin(); iii != lsys->productions.end(); iii++) { |
---|
| 721 | cout << "PPP '" << iii->first << "' adr " << (long) &(iii->second) << endl; |
---|
| 722 | } |
---|
| 723 | #endif |
---|
| 724 | |
---|
| 725 | const char* src = in; |
---|
| 726 | bool result = this->parseInput(src, lsys); |
---|
| 727 | if (!result) { |
---|
| 728 | delete lsys; |
---|
| 729 | return NULL; |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | #if CONV_DEBUG > 1 |
---|
| 733 | |
---|
[26] | 734 | cout << "@@@@@ Parsed" << endl; |
---|
[1] | 735 | |
---|
| 736 | cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
| 737 | #endif |
---|
[11] | 738 | //final check |
---|
| 739 | for (map<string, Production*>::iterator prodIter = lsys->productions.begin(); |
---|
| 740 | prodIter != lsys->productions.end(); prodIter++) { |
---|
| 741 | for (vector<SubProduction>::iterator subProdIter = prodIter->second->subproductions.begin(); |
---|
| 742 | subProdIter != prodIter->second->subproductions.end(); subProdIter++) { |
---|
| 743 | SubProduction subProduction = *subProdIter; |
---|
[24] | 744 | for (vector<ActionStrP>::iterator i = subProduction.actions.begin(); |
---|
| 745 | i != subProduction.actions.end(); /*nothing*/) { |
---|
| 746 | if ((*i).action == NULL) { |
---|
| 747 | i = subProduction.actions.erase(i); |
---|
| 748 | } else { |
---|
| 749 | i++; |
---|
[11] | 750 | } |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | } |
---|
[1] | 754 | #if CONV_DEBUG > 0 |
---|
| 755 | cout << "createLsystem() end" << endl; |
---|
| 756 | #endif |
---|
| 757 | return lsys; |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | bool GenoConv_F8ToF1::parseInput(const char* src, Lsystem* lsys) { |
---|
[26] | 761 | //initialize parser |
---|
[1] | 762 | int yv; |
---|
| 763 | istringstream input; |
---|
| 764 | input.str(string(src)); |
---|
| 765 | ostringstream output; |
---|
| 766 | yyFlexLexer scanner(&input, &output); |
---|
| 767 | bool syntaxOk = false; |
---|
| 768 | void* pParser = prs::ParseAlloc(malloc, lsys, (lsys == NULL), &syntaxOk); |
---|
| 769 | struct prs::Token t0; |
---|
| 770 | //t0.strValue = ""; |
---|
| 771 | memset(t0.strArrValue, 0, 30); |
---|
| 772 | extern YYSTYPE yylval; |
---|
| 773 | |
---|
[26] | 774 | //parse input |
---|
[1] | 775 | // on EOF yylex will return 0 |
---|
| 776 | while((yv = scanner.yylex()) != 0) { |
---|
| 777 | #if CONV_DEBUG > 1 |
---|
| 778 | cout << " yylex() " << yv << " yylval.strVal " << yylval.strVal << endl; |
---|
| 779 | #endif |
---|
| 780 | memset(t0.strArrValue, 0, 30); |
---|
| 781 | sprintf(t0.strArrValue, yylval.strVal); |
---|
| 782 | prs::Parse (pParser, yv, t0); |
---|
| 783 | } |
---|
| 784 | prs::Parse(pParser, 0, t0); |
---|
| 785 | prs::ParseFree(pParser, free); |
---|
| 786 | |
---|
| 787 | return syntaxOk; |
---|
| 788 | } |
---|
| 789 | #undef CONV_DEBUG |
---|
[33] | 790 | |
---|