source: js/sdk/js_interface/js_types.h @ 879

Last change on this file since 879 was 879, checked in by Maciej Komosinski, 5 years ago

Fixed freeing memory during mutation and crossing over

File size: 4.5 KB
Line 
1#ifndef JS_TYPES_H_
2#define JS_TYPES_H_
3
4//TODO verify how much of this file is still needed (and why) for the most recent Framsticks SDK and Emscripten
5
6#include <frams/_demos/genotypemini.h>
7#include <frams/_demos/genotypeloader.h>
8#include <common/virtfile/stdiofile.h>
9#include <frams/model/modelparts.h>
10#include <frams/genetics/genooperators.h>
11#include <frams/genetics/preconfigured.h>
12#include <frams/param/paramtree.h>
13#include <frams/param/mutparamlist.h>
14#include <frams/vm/classes/genoobj.h>
15#include <frams/vm/classes/collectionobj.h>
16#include <frams/vm/classes/3dobject.h>
17#include <frams/neuro/neuroimpl.h>
18#include <frams/neuro/neurofactory.h>
19#include <frams/model/geometry/modelgeoclass.h>
20#include <frams/model/modelobj.h>
21#include <string>
22
23#ifdef EMSCRIPTEN
24typedef Model::ShapeType Model_ShapeType;
25typedef Part::Shape Part_Shape;
26typedef Joint::Shape Joint_Shape;
27typedef LoggerBase::LoggerOptions LoggerBase_LoggerOptions;
28typedef LoggerToMemory::Options2 LoggerToMemory_Options2;
29typedef NeuroClass::Hint NeuroClass_Hint;
30
31class SaveFileHelper
32{
33public:
34        VirtFILE *Vfopen(const char* path, const char*mode)
35        {
36                return ::Vfopen(path, mode);
37        }
38
39        ParamEntry* getMinigenotype_paramtab()
40        {
41                return genotypemini_paramtab;
42        }
43};
44
45struct XY
46{
47        XY() {}
48        XY(int x, int y) : x(x), y(y) { }
49        int x;
50        int y;
51};
52
53struct XYWH : public XY
54{
55        XYWH() {}
56        XYWH(int x, int y, int w, int h) : XY(x, y), w(w), h(h) { }
57        int w;
58        int h;
59};
60
61struct NNLayoutState_Model_Fred : public NNLayoutState_Model
62{
63
64        NNLayoutState_Model_Fred(Model *m) : NNLayoutState_Model(m) { }
65
66        XY GetLinkValueXY(int el, int i)
67        {
68                int *ptr = NNLayoutState_Model::GetLinkXY(el, i);
69                return XY(ptr[0], ptr[1]);
70        }
71
72        XYWH GetValueXYWH(int el)
73        {
74                int *ptr = NNLayoutState_Model::GetXYWH(el);
75                return XYWH(ptr[0], ptr[1], ptr[2], ptr[3]);
76        }
77
78        virtual ~NNLayoutState_Model_Fred() { }
79
80};
81
82struct NNLayoutFunctionHelper {
83
84        void doLayout(int layout_type, NNLayoutState *nn_layout)
85        {
86                nn_layout_functions[layout_type].doLayout(nn_layout);
87        }
88
89};
90
91class GlyphLoader {
92public:
93        GlyphLoader() {}
94        const char * getStringifiedGlyph(NeuroClass * cl)
95        {
96                int * glyph = cl->getSymbolGlyph();
97                if (glyph == NULL) return "";
98                std::string res = "";
99                res += std::to_string(glyph[0]);
100                for (int i = 1; i < glyph[0]; i++)
101                {
102                        res += ",";
103                        res += std::to_string(glyph[i]);
104                }
105                return res.c_str();
106        }
107};
108
109class ParamTreeConfigured
110{
111public:
112        ParamTree * tree;
113        MutableParamList paramiface;
114
115        ParamTreeConfigured()
116        {
117                this->tree = generateTree();
118        }
119
120        ~ParamTreeConfigured()
121        {
122                delete tree;
123        }
124
125        ParamTree * generateTree()
126        {
127                //StdioFILE::setStdio(); //setup VirtFILE::Vstdin/out/err
128                PreconfiguredGenetics genetics;
129
130                Param genotypemini_param(genotypemini_paramtab);
131                NeuroFactory neurofac;
132                neurofac.setStandardImplementation();
133                NeuroNetConfig nn_config(&neurofac);
134                ModelGeometry modelgeo;
135
136                //MutableParamList combined;
137                this->paramiface += &genetics.genman.par;
138                this->paramiface += &GenoObj::getStaticParam();
139                this->paramiface += &ModelObj::getStaticParam();
140                this->paramiface += &VectorObject::getStaticParam();
141                this->paramiface += &DictionaryObject::getStaticParam();
142                this->paramiface += &Pt3D_Ext::getStaticParam();
143                this->paramiface += &Orient_Ext::getStaticParam();
144                this->paramiface += &genotypemini_param;
145                this->paramiface += &nn_config.par;
146                this->paramiface += &modelgeo.par;
147
148                ParamTree * t = new ParamTree(&this->paramiface);
149                return t;
150        }
151};
152
153struct GenoOperatorsHelper
154{
155        GenoOperators* genoOper;
156
157        char *lastMutate;
158        char *lastCross1;
159        char *lastCross2;
160
161        GenoOperatorsHelper(GenoOperators *_genoOper)
162        {
163                lastMutate = nullptr;
164                lastCross1 = nullptr;
165                lastCross2 = nullptr;
166                genoOper = _genoOper;
167        }
168
169        int mutate(const char *geno)
170        {
171                float _ch = 0;
172                int _met = 0;
173
174                if (lastMutate != nullptr) free(lastMutate);
175                lastMutate = strdup(geno);
176                return genoOper->mutate(lastMutate, _ch, _met);
177        }
178
179        const char* getLastMutateGeno()
180        {
181                return lastMutate;
182        }
183
184        int crossOver(const char *geno1, const char *geno2)
185        {
186                float _ch1 = 0;
187                float _ch2 = 0;
188
189                if (lastCross1 != nullptr) free(lastCross1);
190                lastCross1 = strdup(geno1);
191
192                if (lastCross2 != nullptr) free(lastCross2);
193                lastCross2 = strdup(geno2);
194                return genoOper->crossOver(lastCross1, lastCross2, _ch1, _ch2);
195        }
196
197        const char* getLastCrossGeno1()
198        {
199                return lastCross1;
200        }
201
202        const char* getLastCrossGeno2()
203        {
204                return lastCross2;
205        }
206
207        ~GenoOperatorsHelper()
208        {
209                if (lastMutate != nullptr) free(lastMutate);
210                if (lastCross1 != nullptr) free(lastCross1);
211                if (lastCross2 != nullptr) free(lastCross2);
212                //delete genoOper;
213        }
214};
215
216#endif
217#endif //JS_TYPES_H_
Note: See TracBrowser for help on using the repository browser.