[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 | |
---|
| 29 | Lsystem::Lsystem() { |
---|
| 30 | PrimitiveProduction *pp1 = new PrimitiveProduction(SString("X")); |
---|
| 31 | this->primitiveProductions.insert(make_pair("X", pp1)); |
---|
| 32 | PrimitiveProduction *pp2 = new PrimitiveProduction(SString("R")); |
---|
| 33 | this->primitiveProductions.insert(make_pair("R", pp2)); |
---|
| 34 | PrimitiveProduction *pp3 = new PrimitiveProduction(SString("r")); |
---|
| 35 | this->primitiveProductions.insert(make_pair("r", pp3)); |
---|
| 36 | PrimitiveProduction *pp4 = new PrimitiveProduction(SString("C")); |
---|
| 37 | this->primitiveProductions.insert(make_pair("C", pp4)); |
---|
| 38 | PrimitiveProduction *pp5 = new PrimitiveProduction(SString("c")); |
---|
| 39 | this->primitiveProductions.insert(make_pair("c", pp5)); |
---|
| 40 | PrimitiveProduction *pp6 = new PrimitiveProduction(SString("Q")); |
---|
| 41 | this->primitiveProductions.insert(make_pair("Q", pp6)); |
---|
| 42 | PrimitiveProduction *pp7 = new PrimitiveProduction(SString("q")); |
---|
| 43 | this->primitiveProductions.insert(make_pair("q", pp7)); |
---|
| 44 | PrimitiveProduction *pp8 = new PrimitiveProduction(SString("[")); |
---|
| 45 | this->primitiveProductions.insert(make_pair("[", pp8)); |
---|
| 46 | PrimitiveProduction *pp9 = new PrimitiveProduction(SString("]")); |
---|
| 47 | this->primitiveProductions.insert(make_pair("]", pp9)); |
---|
| 48 | PrimitiveProduction *pp10 = new PrimitiveProduction(SString("^")); |
---|
| 49 | this->primitiveProductions.insert(make_pair("^", pp10)); |
---|
| 50 | |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | Lsystem::~Lsystem() { |
---|
| 54 | for (map<string, Production*>::iterator iter = this->productions.begin(); |
---|
| 55 | iter != this->productions.end(); iter++) { |
---|
| 56 | delete iter->second; |
---|
| 57 | } |
---|
| 58 | for (map<string, PrimitiveProduction*>::iterator iter = this->primitiveProductions.begin(); |
---|
| 59 | iter != this->primitiveProductions.end(); iter++) { |
---|
| 60 | delete iter->second; |
---|
| 61 | } |
---|
| 62 | for (map<string, ParamProduction*>::iterator iter = this->paramProductions.begin(); |
---|
| 63 | iter != this->paramProductions.end(); iter++) { |
---|
| 64 | delete iter->second; |
---|
| 65 | } |
---|
| 66 | for (vector<NeuronProduction*>::iterator iter = this->neuronProductions.begin(); |
---|
| 67 | iter != this->neuronProductions.end(); iter++) { |
---|
| 68 | delete *iter; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | PrimitiveProduction* Lsystem::getPrimitiveProduction(SString name) { |
---|
| 73 | string sname = sstringToString(name); |
---|
| 74 | if (this->primitiveProductions.find(sname) == this->primitiveProductions.end()) { |
---|
| 75 | PrimitiveProduction *pp = new PrimitiveProduction(name); |
---|
| 76 | this->primitiveProductions.insert(make_pair(sname, pp)); |
---|
| 77 | } |
---|
| 78 | return this->primitiveProductions[sname]; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | ParamProduction* Lsystem::getParamProduction(SString name) { |
---|
| 82 | string sname = sstringToString(name); |
---|
| 83 | if (this->paramProductions.find(sname) == this->paramProductions.end()) { |
---|
| 84 | ParamProduction *pp = new ParamProduction(name); |
---|
| 85 | this->paramProductions.insert(make_pair(sname, pp)); |
---|
| 86 | } |
---|
| 87 | return this->paramProductions[sname]; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | SString Lsystem::toString() { |
---|
| 91 | this->removeEmptySubproductionsAndProductions(); |
---|
| 92 | SString result = ""; |
---|
| 93 | result += SString::valueOf(this->iterations) + "\n"; |
---|
| 94 | for (map<string, double>::iterator it = this->startParams.begin(); |
---|
| 95 | it != this->startParams.end(); it++) { |
---|
| 96 | result += stringToSString(it->first) + "=" + SString::valueOf(it->second) + "\n"; |
---|
| 97 | } |
---|
| 98 | result += SString("---") + "\n"; |
---|
| 99 | result += stringToSString(this->firstProductionName) + "\n"; |
---|
| 100 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
| 101 | prodIter != this->productions.end(); prodIter++) { |
---|
| 102 | Production *p = prodIter->second; |
---|
| 103 | result += p->name + "("; |
---|
| 104 | for (int i = 1; i <= p->parameters.size(); i++) { |
---|
| 105 | result += p->parameters.getParameterName(i); |
---|
| 106 | if (i < p->parameters.size()) { |
---|
| 107 | result += ","; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | result += "):"; |
---|
| 111 | for (int subprodIter = 0; subprodIter < p->subproductions.size(); subprodIter++) { |
---|
| 112 | SubProduction *sp = &(p->subproductions[subprodIter]); |
---|
| 113 | for (int condIter = 0; condIter < sp->conditions.size(); condIter++) { |
---|
| 114 | Condition *c = &(sp->conditions[condIter]); |
---|
| 115 | RelationType r = c->relation; |
---|
| 116 | SString op = (r == r_greater) ? ">" : (r == r_greaterEqual) ? ">=" : (r == r_less) ? "<" : |
---|
| 117 | (r == r_lessEqual) ? "<=" : (r == r_equal) ? "==" : (r == r_different) ? "!=" : ""; |
---|
| 118 | result += c->parameter + op + SString::valueOf(c->value); |
---|
| 119 | if (condIter != sp->conditions.size() - 1) { |
---|
| 120 | result += ","; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | if (sp->conditions.size() > 0) { |
---|
| 124 | result += SString("|"); |
---|
| 125 | } |
---|
| 126 | for (vector<ActionStrP>::iterator actionIter = sp->actions.begin(); |
---|
| 127 | actionIter != sp->actions.end(); actionIter++) { |
---|
| 128 | ActionStrP *a = &(*actionIter); |
---|
| 129 | if (a->action == NULL) { |
---|
| 130 | continue; |
---|
| 131 | } |
---|
| 132 | result += a->action->getF8Representation(); |
---|
| 133 | if (!a->action->ignoreParams) { |
---|
| 134 | result += SString("("); |
---|
| 135 | for (int paramIter = 0; paramIter < a->params.size(); paramIter++) { |
---|
| 136 | result += convertReversePolishNotationToNatural(a->params[paramIter]); |
---|
| 137 | if (paramIter != a->params.size() - 1) { |
---|
| 138 | result += ","; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | result += SString(")"); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | if (subprodIter != p->subproductions.size() - 1) { |
---|
| 145 | result += SString(":"); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | result += SString("\n"); |
---|
| 149 | } |
---|
| 150 | return result; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | vector<Action*> Lsystem::getAllActions(bool normal, bool primitives, bool params, bool neurons) { |
---|
| 154 | vector<Action*> actions; |
---|
| 155 | if (normal) { |
---|
| 156 | for (map<string, Production*>::iterator iter = this->productions.begin(); |
---|
| 157 | iter != this->productions.end(); iter++) { |
---|
| 158 | actions.push_back(iter->second); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | if (primitives) { |
---|
| 162 | for (map<string, PrimitiveProduction*>::iterator iter = this->primitiveProductions.begin(); |
---|
| 163 | iter != this->primitiveProductions.end(); iter++) { |
---|
| 164 | actions.push_back(iter->second); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | if (params) { |
---|
| 168 | for (map<string, ParamProduction*>::iterator iter = this->paramProductions.begin(); |
---|
| 169 | iter != this->paramProductions.end(); iter++) { |
---|
| 170 | actions.push_back(iter->second); |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | if (neurons) { |
---|
| 174 | for (vector<NeuronProduction*>::iterator iter = this->neuronProductions.begin(); |
---|
| 175 | iter != this->neuronProductions.end(); iter++) { |
---|
| 176 | actions.push_back(*iter); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | return actions; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | void Lsystem::removeEmptySubproductionsAndProductions() { |
---|
| 183 | /* po wykonaniu moga pozostac puste subprodukcje (usuwanie wywolan do usunietych wczesniej produkcji |
---|
| 184 | * ale i tak znikna one przy kolejnym wywolaniu, tak wiec ilosc smieci nie powinna byc razaca |
---|
| 185 | */ |
---|
| 186 | |
---|
| 187 | //delete empty subproductions |
---|
| 188 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
| 189 | prodIter != this->productions.end(); prodIter++) { |
---|
| 190 | vector<SubProduction> *subproductions = &(prodIter->second->subproductions); |
---|
| 191 | for (int i = 0; i < subproductions->size(); i++) { |
---|
| 192 | if (subproductions->at(i).actions.size() == 0) { |
---|
| 193 | subproductions->erase(subproductions->begin() + i); |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | //find empty productions |
---|
| 199 | vector<SString> emptyProductionNames; |
---|
| 200 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
| 201 | prodIter != this->productions.end(); prodIter++) { |
---|
| 202 | if (prodIter->second->subproductions.size() == 0 && |
---|
| 203 | sstringToString(prodIter->second->name) != this->firstProductionName) { |
---|
| 204 | emptyProductionNames.push_back(prodIter->second->name); |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | //delete calls to empty productions |
---|
| 209 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
| 210 | prodIter != this->productions.end(); prodIter++) { |
---|
| 211 | vector<SubProduction> *subproductions = &(prodIter->second->subproductions); |
---|
| 212 | for (vector<SubProduction>::iterator subProdIter = subproductions->begin(); |
---|
| 213 | subProdIter != subproductions->end(); subProdIter++) { |
---|
| 214 | SubProduction *sp = &(*subProdIter); |
---|
| 215 | for (int actionIter = 0; actionIter < sp->actions.size(); actionIter++) { |
---|
| 216 | ActionStrP *actionstrp = &(sp->actions.at(actionIter)); |
---|
| 217 | if (actionstrp->action != NULL) { |
---|
| 218 | vector<SString>::iterator result = find(emptyProductionNames.begin(), emptyProductionNames.end(), |
---|
| 219 | actionstrp->action->name); |
---|
| 220 | if (result != emptyProductionNames.end()) { //emptyProductionNames contains the action name |
---|
| 221 | sp->actions.erase(sp->actions.begin() + actionIter); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | //delete empty productions |
---|
| 229 | for(int i = 0; i < emptyProductionNames.size(); i++) { |
---|
| 230 | this->productions.erase(sstringToString(emptyProductionNames[i])); |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | ostream& operator<<(ostream& os, const Condition& c) { |
---|
| 235 | RelationType r = c.relation; |
---|
| 236 | SString op = (r == r_greater) ? ">" : (r == r_greaterEqual) ? ">=" : (r == r_less) ? "<" : |
---|
| 237 | (r == r_lessEqual) ? "<=" : (r == r_equal) ? "==" : (r == r_different) ? "!=" : ""; |
---|
| 238 | os << c.parameter << " " << op << " " << c.value; |
---|
| 239 | return os; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | const double ParameterCollection::getValue(int position) { |
---|
| 243 | string name = this->parameters[position - 1]; |
---|
| 244 | return this->paramValues[name]; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | const double ParameterCollection::getValue(SString name) { |
---|
| 248 | return this->paramValues[sstringToString(name)]; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | const SString ParameterCollection::getParameterName(int position) { |
---|
| 252 | return stringToSString(this->parameters[position - 1]); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | const int ParameterCollection::getParameterPosition(SString name) { |
---|
| 256 | for (int i = 0; i < this->parameters.size(); i++) { |
---|
| 257 | string &s = this->parameters[i]; |
---|
| 258 | if (s == sstringToString(name)) { |
---|
| 259 | return i + 1; |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | return -1; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | void ParameterCollection::setValue(int position, double value) { |
---|
| 266 | string name = this->parameters[position - 1]; |
---|
| 267 | this->paramValues[name] = value; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | void ParameterCollection::setValue(SString name, double value) { |
---|
| 271 | this->paramValues[sstringToString(name)] = value; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | void ParameterCollection::addParameter(SString name, int position, double value) { |
---|
| 275 | //TODO sprawdzenie czy position > 0 |
---|
| 276 | string sname = sstringToString(name); |
---|
| 277 | if (position == -1) { |
---|
| 278 | this->parameters.push_back(sname); |
---|
| 279 | this->paramValues[sname] = value; |
---|
| 280 | } else { |
---|
| 281 | this->parameters.reserve(position); |
---|
| 282 | this->parameters.insert(this->parameters.begin() + (position -1), sname); |
---|
| 283 | this->paramValues[sname] = value; |
---|
| 284 | } |
---|
| 285 | //this->paramValues[name] = value; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | const int ParameterCollection::size() { |
---|
| 289 | return this->parameters.size(); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | void ParameterCollection::removeParameter(int position) { |
---|
| 293 | string name = this->parameters[position - 1]; |
---|
| 294 | vector<string>::iterator it = this->parameters.begin() + (position - 1); |
---|
| 295 | this->parameters.erase(it); |
---|
| 296 | this->paramValues.erase(name); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | void ParameterCollection::removeParameter(SString name) { |
---|
| 300 | int idx = getParameterPosition(name); |
---|
| 301 | this->removeParameter(idx); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | bool ParameterCollection::paramExist(SString name) { |
---|
| 305 | string sname = sstringToString(name); |
---|
| 306 | for (vector<string>::iterator it = this->parameters.begin(); it != this->parameters.end(); it++) { |
---|
| 307 | if ((*it) == sname) { |
---|
| 308 | return true; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | return false; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | PrimitiveProduction::PrimitiveProduction(const SString command) { |
---|
| 315 | this->f8command = command; |
---|
| 316 | this->ignoreParams = true; |
---|
| 317 | if (command == SString("[")) { |
---|
| 318 | this->f1command = SString("("); |
---|
| 319 | this->name = SString("_primitive production '[' => '('"); |
---|
| 320 | } else if (command == SString("]")) { |
---|
| 321 | this->f1command = SString(")"); |
---|
| 322 | this->name = SString("_primitive production ']' => ')'"); |
---|
| 323 | } else if (command == SString("^")) { |
---|
| 324 | this->f1command = SString(","); |
---|
| 325 | this->name = SString("_primitive production '^' => ','"); |
---|
| 326 | } else { |
---|
| 327 | this->f1command = command; |
---|
| 328 | this->name = SString("_primitive production '") + command + "'"; |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | const SString PrimitiveProduction::getF1Genotype(const vector<double> params) { |
---|
| 333 | #if CONV_DEBUG > 1 |
---|
| 334 | cout << "@@@@@ Prymityw: " << this->f1command << endl; |
---|
| 335 | #endif |
---|
| 336 | return SString(this->f1command); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | const list<ActionP> PrimitiveProduction::getActionList(const vector<double> param) { |
---|
| 340 | list<ActionP> l; |
---|
| 341 | ActionP ap; |
---|
| 342 | ap.action = this; |
---|
| 343 | ap.params = param; |
---|
| 344 | l.push_back(ap); |
---|
| 345 | return l; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | const SString PrimitiveProduction::getF8Representation() { |
---|
| 349 | return this->f8command; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | ParamProduction::ParamProduction(const SString paramName) { |
---|
| 353 | this->paramName = paramName; |
---|
| 354 | this->name = SString("_param production") + paramName; |
---|
| 355 | this->ignoreParams = true; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | const SString ParamProduction::getF1Genotype(const vector<double> params) { |
---|
| 359 | SString s = SString::valueOf(params[0]); |
---|
| 360 | #if CONV_DEBUG > 1 |
---|
| 361 | cout << "@@@@@ Wartosc parametru: " << this->paramName << ": " << params[0] << endl; |
---|
| 362 | #endif |
---|
| 363 | return s; |
---|
| 364 | |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | const list<ActionP> ParamProduction::getActionList(const vector<double> param) { |
---|
| 368 | list<ActionP> l; |
---|
| 369 | ActionP ap; |
---|
| 370 | ap.action = this; |
---|
| 371 | ap.params = param; |
---|
| 372 | l.push_back(ap); |
---|
| 373 | return l; |
---|
| 374 | |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | const SString ParamProduction::getF8Representation() { |
---|
| 378 | return this->paramName; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | NeuronProduction::NeuronProduction(const SString body) { |
---|
| 382 | this->body = body; |
---|
| 383 | this->name = SString("_neuron production") + body; |
---|
| 384 | this->ignoreParams = true; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | const SString NeuronProduction::getF1Genotype(const vector<double> params) { |
---|
| 388 | return this->body; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | const list<ActionP> NeuronProduction::getActionList(const vector<double> param) { |
---|
| 392 | list<ActionP> l; |
---|
| 393 | ActionP ap; |
---|
| 394 | ap.action = this; |
---|
| 395 | ap.params = param; |
---|
| 396 | l.push_back(ap); |
---|
| 397 | return l; |
---|
| 398 | |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | const SString NeuronProduction::getF8Representation() { |
---|
| 402 | return this->body; |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | Production::Production() { |
---|
| 406 | this->ignoreParams = false; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | /** |
---|
| 410 | * Zwracamy puste, bo co innego mamy zrobić? |
---|
| 411 | */ |
---|
| 412 | const SString Production::getF1Genotype(const vector<double> params) { |
---|
| 413 | return SString(""); |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | const list<ActionP> Production::getActionList(const vector<double> params) { |
---|
| 417 | list<ActionP> l; |
---|
| 418 | #if CONV_DEBUG > 1 |
---|
| 419 | cout << "params.size(): " << params.size() << ", this->parameters.size(): " << this->parameters.size() << endl; |
---|
| 420 | #endif |
---|
| 421 | for (int i = 0; i < params.size() && i < this->parameters.size(); i++) { |
---|
| 422 | this->parameters.setValue(i + 1, params[i]); |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | //jedziemy po podprodukcjach |
---|
| 426 | for (vector<SubProduction>::iterator subProdIter = this->subproductions.begin(); |
---|
| 427 | subProdIter != this->subproductions.end(); subProdIter++) { |
---|
| 428 | #if CONV_DEBUG > 1 |
---|
| 429 | cout << "this->subproductions.size(): " << this->subproductions.size() << endl; |
---|
| 430 | #endif |
---|
| 431 | SubProduction &sp = *subProdIter; |
---|
| 432 | bool conditionsOK = true; |
---|
| 433 | //sprawdzenie warunków dla danej podprodukcji |
---|
| 434 | for (vector<Condition>::iterator condIter = sp.conditions.begin(); |
---|
| 435 | condIter != sp.conditions.end(); condIter++) { |
---|
| 436 | if (conditionsOK == false) { |
---|
| 437 | break; //bo dalsze sprawdzanie i tak nie ma sensu |
---|
| 438 | } |
---|
| 439 | Condition &c = *condIter; |
---|
| 440 | switch (c.relation) { |
---|
| 441 | case r_greater: |
---|
| 442 | if (this->parameters.getValue(c.parameter) <= c.value) { |
---|
| 443 | conditionsOK = false; |
---|
| 444 | } |
---|
| 445 | break; |
---|
| 446 | case r_greaterEqual: |
---|
| 447 | if (this->parameters.getValue(c.parameter) < c.value) { |
---|
| 448 | conditionsOK = false; |
---|
| 449 | } |
---|
| 450 | break; |
---|
| 451 | case r_less: |
---|
| 452 | if (this->parameters.getValue(c.parameter) >= c.value) { |
---|
| 453 | conditionsOK = false; |
---|
| 454 | } |
---|
| 455 | break; |
---|
| 456 | case r_lessEqual: |
---|
| 457 | if (this->parameters.getValue(c.parameter) > c.value) { |
---|
| 458 | conditionsOK = false; |
---|
| 459 | } |
---|
| 460 | break; |
---|
| 461 | case r_equal: |
---|
| 462 | if (this->parameters.getValue(c.parameter) != c.value) { |
---|
| 463 | conditionsOK = false; |
---|
| 464 | } |
---|
| 465 | break; |
---|
| 466 | case r_different: |
---|
| 467 | if (this->parameters.getValue(c.parameter) == c.value) { |
---|
| 468 | conditionsOK = false; |
---|
| 469 | } |
---|
| 470 | break; |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | if (conditionsOK) { |
---|
| 474 | //jedziemy po każdej akcji w danej podprodukcji |
---|
| 475 | for (int i = 0; i < sp.actions.size(); i++) { |
---|
| 476 | #if CONV_DEBUG > 1 |
---|
| 477 | cout << "sp.actions.size(): " << sp.actions.size() << endl; |
---|
| 478 | #endif |
---|
| 479 | Action *action = sp.actions[i].action; |
---|
| 480 | vector<SString> strParams = sp.actions[i].params; |
---|
| 481 | vector<double> params; |
---|
| 482 | //podmieniamy nazwy parametrów na ich wartości |
---|
| 483 | for (vector<SString>::iterator paramIter = strParams.begin(); |
---|
| 484 | paramIter != strParams.end(); paramIter++) { |
---|
| 485 | SString parameter; |
---|
| 486 | SString element; |
---|
| 487 | int pos = 0; |
---|
| 488 | while ((*paramIter).getNextToken(pos, element, ';')) { |
---|
| 489 | if (element[0] == 'n') { |
---|
| 490 | double val = this->parameters.getValue(element); |
---|
| 491 | parameter += SString::valueOf(val) + ";"; |
---|
| 492 | } else { |
---|
| 493 | parameter += element + ";"; |
---|
| 494 | } |
---|
| 495 | } |
---|
| 496 | params.push_back(parseExpression(parameter)); |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | ActionP ap; |
---|
| 500 | ap.action = action; |
---|
| 501 | ap.params = params; |
---|
| 502 | l.push_back(ap); |
---|
| 503 | } |
---|
| 504 | } |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | return l; |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | const SString Production::getF8Representation() { |
---|
| 511 | return this->name; |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | vector<SString> GenoConv_F8ToF1::readProductionNames(const SString &in) { |
---|
| 515 | vector<SString> names; |
---|
| 516 | SString line; |
---|
| 517 | int pos = 0; |
---|
| 518 | bool afterFirstProd = false; |
---|
| 519 | //ParsingStatus status = firstLine; |
---|
| 520 | while (in.getNextToken(pos, line, '\n')) { |
---|
| 521 | #if CONV_DEBUG > 1 |
---|
| 522 | std::cout << "### Linia: " << line << std::endl; |
---|
| 523 | #endif |
---|
| 524 | if (line.startsWith("P") && line.indexOf('(', 0) == -1) { |
---|
| 525 | afterFirstProd = true; |
---|
| 526 | continue; |
---|
| 527 | } |
---|
| 528 | if (!afterFirstProd) { |
---|
| 529 | continue; |
---|
| 530 | } |
---|
| 531 | int lParenIndex = line.indexOf('(', 0); |
---|
| 532 | if (line.startsWith("P") && lParenIndex != -1) { |
---|
| 533 | SString prodName = line.substr(0, lParenIndex); |
---|
| 534 | #if CONV_DEBUG > 1 |
---|
| 535 | std::cout << "###Produkcja: " << prodName << std::endl; |
---|
| 536 | #endif |
---|
| 537 | names.push_back(prodName); |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | return names; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | bool GenoConv_F8ToF1::checkSyntax(const char *geno) { |
---|
| 544 | return this->parseInput(geno, NULL); |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | SString GenoConv_F8ToF1::convert(SString &in, MultiMap *mmap) { |
---|
| 548 | #if CONV_DEBUG > 0 |
---|
| 549 | cout << "convert() start" << endl; |
---|
| 550 | #endif |
---|
| 551 | SString dst = ""; |
---|
| 552 | const char* src = in; |
---|
| 553 | |
---|
| 554 | if (in.len() < 1 && !this->checkSyntax(src)) { |
---|
| 555 | return SString(); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | Lsystem *lsystem = this->createLsystem(in); |
---|
| 559 | if (lsystem == NULL) { |
---|
| 560 | return SString(); |
---|
| 561 | } |
---|
| 562 | if (lsystem->firstProductionName.empty()) { |
---|
| 563 | return SString(); |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | #if CONV_DEBUG > 0 |
---|
| 567 | for (map<string, Production*>::iterator i1 = lsystem->productions.begin(); i1 != lsystem->productions.end(); i1++) { |
---|
| 568 | Production *p = i1->second; |
---|
| 569 | cout << "Production: " << p->name << endl; |
---|
| 570 | for (vector<SubProduction>::iterator i2 = p->subproductions.begin(); i2 != p->subproductions.end(); i2++) { |
---|
| 571 | SubProduction sp = *i2; |
---|
| 572 | cout << "\tConditions" << endl; |
---|
| 573 | for (vector<Condition>::iterator i3 = sp.conditions.begin(); i3 != sp.conditions.end(); i3++) { |
---|
| 574 | cout << "\t\t" << *i3 << endl; |
---|
| 575 | } |
---|
| 576 | cout << "\tAction : params" << endl; |
---|
| 577 | for (int i = 0; i < sp.actions.size(); i++) { |
---|
| 578 | Action *a = sp.actions[i].action; |
---|
| 579 | vector<SString> strParams = sp.actions[i].params; |
---|
| 580 | cout << "\t\t" << a->name << " : "; |
---|
| 581 | for (vector<SString>::iterator i4 = strParams.begin(); i4 != strParams.end(); i4++) { |
---|
| 582 | cout << *i4 << ", "; |
---|
| 583 | } |
---|
| 584 | cout << endl; |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | } |
---|
| 588 | #endif |
---|
| 589 | #if CONV_DEBUG > 1 |
---|
| 590 | cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
| 591 | #endif |
---|
| 592 | |
---|
| 593 | //cout << "convert() 1" << endl; |
---|
| 594 | //ustawienie zmiennych dla początkowej produkcji |
---|
| 595 | Production *firstProduction = lsystem->productions[lsystem->firstProductionName]; |
---|
| 596 | vector<double> params; |
---|
| 597 | params.reserve(lsystem->startParams.size()); |
---|
| 598 | //cout << "startParams->size: " << lsystem->startParams.size() << endl; |
---|
| 599 | for (map<string, double>::iterator iter = lsystem->startParams.begin(); |
---|
| 600 | iter != lsystem->startParams.end(); iter++) { |
---|
| 601 | int position = firstProduction->parameters.getParameterPosition(stringToSString(iter->first)); |
---|
| 602 | //cout << "position of " << iter->first << ": " << position << endl; |
---|
| 603 | //params.insert(params.begin() + (position - 1), iter->second); |
---|
| 604 | params[position - 1] = iter->second; |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | //cout << "convert() 2" << endl; |
---|
| 608 | |
---|
| 609 | ActionP ap; |
---|
| 610 | ap.action = firstProduction; |
---|
| 611 | ap.params = params; |
---|
| 612 | |
---|
| 613 | list<ActionP> actionList; |
---|
| 614 | actionList.push_back(ap); |
---|
| 615 | |
---|
| 616 | //cout << "iterations: " << lsystem->iterations << endl; |
---|
| 617 | for (int i = 0; i < lsystem->iterations; i++) { |
---|
| 618 | //cout << "convert() 2.1" << endl; |
---|
| 619 | list<ActionP> newList; |
---|
| 620 | for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) { |
---|
| 621 | //cout << "convert() 2.1.1" << endl; |
---|
| 622 | Action *a = (*iter).action; |
---|
| 623 | vector<double> p = (*iter).params; |
---|
| 624 | if (a != NULL) { |
---|
| 625 | list<ActionP> tmpList = a->getActionList(p); |
---|
| 626 | newList.insert(newList.end(), tmpList.begin(), tmpList.end()); |
---|
| 627 | } |
---|
| 628 | } |
---|
| 629 | actionList = newList; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | #if CONV_DEBUG > 1 |
---|
| 633 | cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
| 634 | for (list<ActionP>::iterator it = actionList.begin(); it != actionList.end(); it++) { |
---|
| 635 | cout << (*it).action->name << "("; |
---|
| 636 | for (vector<double>::iterator it2 = (*it).params.begin(); it2 != (*it).params.end(); it2++) { |
---|
| 637 | cout << *it2 << ", "; |
---|
| 638 | } |
---|
| 639 | cout << ")" << endl; |
---|
| 640 | } |
---|
| 641 | cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
| 642 | #endif |
---|
| 643 | for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) { |
---|
| 644 | Action *a = (*iter).action; |
---|
| 645 | vector<double> p = (*iter).params; |
---|
| 646 | if (a != NULL) { |
---|
| 647 | dst += a->getF1Genotype(p); |
---|
| 648 | } |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | delete lsystem; |
---|
| 652 | |
---|
| 653 | #if CONV_DEBUG > 0 |
---|
| 654 | cout << "convert() end" << endl; |
---|
| 655 | #endif |
---|
| 656 | return dst; |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | //Lsystem* GenoConv_F8ToF1::createLsystem(const SString &in) { |
---|
| 660 | Lsystem* GenoConv_F8ToF1::createLsystem(SString in) { |
---|
| 661 | #if CONV_DEBUG > 0 |
---|
| 662 | cout << "createLsystem() start" << endl; |
---|
| 663 | #endif |
---|
| 664 | Lsystem *lsys = new Lsystem(); |
---|
| 665 | |
---|
| 666 | //wczytujemy nazwy produkcji i tworzymy wstępnie dla nich obiekty |
---|
| 667 | vector<SString> names = this->readProductionNames(in); |
---|
| 668 | for (vector<SString>::iterator nameIter = names.begin(); nameIter != names.end(); nameIter++) { |
---|
| 669 | Production *production = new Production(); |
---|
| 670 | production->name = *nameIter; |
---|
| 671 | //lsystem->productions.insert(make_pair(*nameIter, production)); |
---|
| 672 | lsys->productions[sstringToString(*nameIter)] = production; |
---|
| 673 | } |
---|
| 674 | |
---|
| 675 | #if CONV_DEBUG > 1 |
---|
| 676 | cout << "lsystemprodsize " << lsys->productions.size() << endl; |
---|
| 677 | for (map<string, Production*>::iterator iii = lsys->productions.begin(); iii != lsys->productions.end(); iii++) { |
---|
| 678 | cout << "PPP '" << iii->first << "' adr " << (long) &(iii->second) << endl; |
---|
| 679 | } |
---|
| 680 | #endif |
---|
| 681 | |
---|
| 682 | const char* src = in; |
---|
| 683 | bool result = this->parseInput(src, lsys); |
---|
| 684 | if (!result) { |
---|
| 685 | delete lsys; |
---|
| 686 | return NULL; |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | #if CONV_DEBUG > 1 |
---|
| 690 | |
---|
| 691 | cout << "@@@@@ Przeparsowane" << endl; |
---|
| 692 | |
---|
| 693 | cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
| 694 | #endif |
---|
| 695 | #if CONV_DEBUG > 0 |
---|
| 696 | cout << "createLsystem() end" << endl; |
---|
| 697 | #endif |
---|
| 698 | return lsys; |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | bool GenoConv_F8ToF1::parseInput(const char* src, Lsystem* lsys) { |
---|
| 702 | //inicjalizacja zmiennych parsera |
---|
| 703 | int yv; |
---|
| 704 | istringstream input; |
---|
| 705 | input.str(string(src)); |
---|
| 706 | ostringstream output; |
---|
| 707 | yyFlexLexer scanner(&input, &output); |
---|
| 708 | bool syntaxOk = false; |
---|
| 709 | void* pParser = prs::ParseAlloc(malloc, lsys, (lsys == NULL), &syntaxOk); |
---|
| 710 | struct prs::Token t0; |
---|
| 711 | //t0.strValue = ""; |
---|
| 712 | memset(t0.strArrValue, 0, 30); |
---|
| 713 | extern YYSTYPE yylval; |
---|
| 714 | |
---|
| 715 | //parsowanie wejścia |
---|
| 716 | // on EOF yylex will return 0 |
---|
| 717 | while((yv = scanner.yylex()) != 0) { |
---|
| 718 | #if CONV_DEBUG > 1 |
---|
| 719 | cout << " yylex() " << yv << " yylval.strVal " << yylval.strVal << endl; |
---|
| 720 | #endif |
---|
| 721 | memset(t0.strArrValue, 0, 30); |
---|
| 722 | sprintf(t0.strArrValue, yylval.strVal); |
---|
| 723 | prs::Parse (pParser, yv, t0); |
---|
| 724 | } |
---|
| 725 | prs::Parse(pParser, 0, t0); |
---|
| 726 | prs::ParseFree(pParser, free); |
---|
| 727 | |
---|
| 728 | return syntaxOk; |
---|
| 729 | } |
---|
| 730 | #undef CONV_DEBUG |
---|