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 |
|
---|
5 | #include "fn_conv.h"
|
---|
6 | #include <frams/vm/classes/collectionobj.h>
|
---|
7 |
|
---|
8 | GenoConv_fn0::GenoConv_fn0()
|
---|
9 | {
|
---|
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.
|
---|
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 |
|
---|
24 | return SString("p:\n"); //phenotype not relevant for this genetic encoding
|
---|
25 | }
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | vector<double> GenoConv_fn0::stringToVector(const char *input) //returns empty vector on error
|
---|
30 | {
|
---|
31 | vector<double> empty;
|
---|
32 | ExtValue val;
|
---|
33 | const char* after_des = val.deserialize(input);
|
---|
34 | if (after_des == NULL) //deserialization failed
|
---|
35 | {
|
---|
36 | logPrintf("GenoConv_fn0", "stringToVector", LOG_ERROR, "Unable to deserialize - expecting a vector of real values, got '%s'", input);
|
---|
37 | return empty;
|
---|
38 | }
|
---|
39 | if (after_des[0] != '\0') //not everything was consumed
|
---|
40 | {
|
---|
41 | logPrintf("GenoConv_fn0", "stringToVector", LOG_ERROR, "Extra characters after deserialized '%s'", input);
|
---|
42 | return empty;
|
---|
43 | }
|
---|
44 |
|
---|
45 | VectorObject *vec = VectorObject::fromObject(val.getObject(), false);
|
---|
46 | if (vec)
|
---|
47 | {
|
---|
48 | vector<double> output;
|
---|
49 | for (int i = 0; i < vec->data.size(); i++)
|
---|
50 | {
|
---|
51 | ExtValue* val = (ExtValue*)vec->data(i);
|
---|
52 | if (val == NULL)
|
---|
53 | {
|
---|
54 | logPrintf("GenoConv_fn0", "stringToVector", LOG_ERROR, "Expecting a real value in a vector, got NULL");
|
---|
55 | return empty;
|
---|
56 | }
|
---|
57 | else
|
---|
58 | output.push_back(val->getDouble());
|
---|
59 | }
|
---|
60 | return output;
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | logPrintf("GenoConv_fn0", "stringToVector", LOG_ERROR, "Expecting a vector of real values, got '%s'", input);
|
---|
65 | return empty;
|
---|
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 += ", ";
|
---|
78 | snprintf(buffer, sizeof(buffer), "%.8g", vec[i]);
|
---|
79 | out += buffer;
|
---|
80 | }
|
---|
81 | return out + "]";
|
---|
82 | }
|
---|