source: cpp/frams/genetics/fn/conv_fn.cpp @ 747

Last change on this file since 747 was 747, checked in by Maciej Komosinski, 2 years ago

Added a new "fn" genetic representation (a vector of real numbers for custom numerical optimization)

File size: 2.2 KB
Line 
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 "conv_fn.h"
6#include <frams/vm/classes/collectionobj.h>
7
8GenoConv_fn0::GenoConv_fn0()
9{
10        name = "Vector of real values, no phenotype"; //for numerical optimization; custom fitness function must be provided in script
11        in_format = 'n';
12        out_format = '0';
13        mapsupport = 0;
14}
15
16
17
18SString 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#original genotype: ") + in;
25}
26
27
28
29vector<double> GenoConv_fn0::stringToVector(const char *input) //returns empty vector on error
30{
31        vector<double> output;
32        ExtValue val;
33        const char* after_des = val.deserialize(input);
34        if (after_des == NULL) // deserialization failed
35        {
36                logPrintf("fn", "stringToVector", LOG_ERROR, "Unable to deserialize - expecting a vector of real values, got '%s'", input);
37                return output;
38        }
39        if (after_des[0] != '\0') // not everything was consumed
40        {
41                logPrintf("fn", "stringToVector", LOG_ERROR, "Extra characters after deserialized '%s'", input);
42                return output;
43        }
44
45        VectorObject *vec = VectorObject::fromObject(val.getObject(), false);
46        if (vec)
47        {
48                for (int i = 0; i < vec->data.size(); i++)
49                {
50                        ExtValue* val = (ExtValue*)vec->data(i);
51                        if (val == NULL)
52                        {
53                                logPrintf("fn", "stringToVector", LOG_ERROR, "Expecting a real value in a vector, got NULL");
54                        }
55                        else
56                                output.push_back(val->getDouble());
57                }
58        }
59        else
60        {
61                logPrintf("fn", "stringToVector", LOG_ERROR, "Expecting a vector of real values, got '%s'", input);
62        }
63        return output;
64}
65
66
67string GenoConv_fn0::vectorToString(const vector<double> vec)
68{
69        char buffer[32];
70        string out = "[";
71        for (unsigned int i = 0; i < vec.size(); i++)
72        {
73                if (i > 0)
74                        out += ", ";
75                snprintf(buffer, sizeof(buffer), "%g", vec[i]);
76                out += buffer; //+= std::to_string(1.23) not available in embarcadero 10.2
77        }
78        return out + "]";
79}
Note: See TracBrowser for help on using the repository browser.