[747] | 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 |
|
---|
[779] | 5 | #include "fn_conv.h"
|
---|
[747] | 6 | #include <frams/vm/classes/collectionobj.h>
|
---|
| 7 |
|
---|
| 8 | GenoConv_fn0::GenoConv_fn0()
|
---|
| 9 | {
|
---|
[763] | 10 | name = "Vector of real values, no phenotype"; //for numerical optimization; custom fitness function must be provided in script. See oper_fn.cpp for more details.
|
---|
[747] | 11 | in_format = 'n';
|
---|
| 12 | out_format = '0';
|
---|
| 13 | mapsupport = 0;
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | SString GenoConv_fn0::convert(SString &in, MultiMap *map, bool using_checkpoints)
|
---|
| 19 | {
|
---|
| 20 | vector<double> values = stringToVector(in.c_str());
|
---|
| 21 | if (values.size() == 0) //invalid input genotype?
|
---|
| 22 | return ""; //so we return an invalid f0 genotype
|
---|
| 23 |
|
---|
[757] | 24 | return SString("p:\n"); //phenotype not relevant for this genetic encoding
|
---|
[747] | 25 | }
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | vector<double> GenoConv_fn0::stringToVector(const char *input) //returns empty vector on error
|
---|
| 30 | {
|
---|
[752] | 31 | vector<double> empty;
|
---|
[747] | 32 | ExtValue val;
|
---|
| 33 | const char* after_des = val.deserialize(input);
|
---|
[752] | 34 | if (after_des == NULL) //deserialization failed
|
---|
[747] | 35 | {
|
---|
[752] | 36 | logPrintf("GenoConv_fn0", "stringToVector", LOG_ERROR, "Unable to deserialize - expecting a vector of real values, got '%s'", input);
|
---|
| 37 | return empty;
|
---|
[747] | 38 | }
|
---|
[752] | 39 | if (after_des[0] != '\0') //not everything was consumed
|
---|
[747] | 40 | {
|
---|
[752] | 41 | logPrintf("GenoConv_fn0", "stringToVector", LOG_ERROR, "Extra characters after deserialized '%s'", input);
|
---|
| 42 | return empty;
|
---|
[747] | 43 | }
|
---|
| 44 |
|
---|
| 45 | VectorObject *vec = VectorObject::fromObject(val.getObject(), false);
|
---|
| 46 | if (vec)
|
---|
| 47 | {
|
---|
[752] | 48 | vector<double> output;
|
---|
[747] | 49 | for (int i = 0; i < vec->data.size(); i++)
|
---|
| 50 | {
|
---|
| 51 | ExtValue* val = (ExtValue*)vec->data(i);
|
---|
| 52 | if (val == NULL)
|
---|
| 53 | {
|
---|
[752] | 54 | logPrintf("GenoConv_fn0", "stringToVector", LOG_ERROR, "Expecting a real value in a vector, got NULL");
|
---|
| 55 | return empty;
|
---|
[747] | 56 | }
|
---|
| 57 | else
|
---|
| 58 | output.push_back(val->getDouble());
|
---|
| 59 | }
|
---|
[752] | 60 | return output;
|
---|
[747] | 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
[752] | 64 | logPrintf("GenoConv_fn0", "stringToVector", LOG_ERROR, "Expecting a vector of real values, got '%s'", input);
|
---|
| 65 | return empty;
|
---|
[747] | 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | string GenoConv_fn0::vectorToString(const vector<double> vec)
|
---|
| 71 | {
|
---|
| 72 | char buffer[32];
|
---|
| 73 | string out = "[";
|
---|
| 74 | for (unsigned int i = 0; i < vec.size(); i++)
|
---|
| 75 | {
|
---|
| 76 | if (i > 0)
|
---|
| 77 | out += ", ";
|
---|
[763] | 78 | snprintf(buffer, sizeof(buffer), "%.8g", vec[i]);
|
---|
[787] | 79 | out += buffer;
|
---|
[747] | 80 | }
|
---|
| 81 | return out + "]";
|
---|
| 82 | }
|
---|