source: cpp/frams/_demos/genotypemini.cpp

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

Added support for "checkpoints" (intermediate phases of development of the Model when converting between genetic encodings). See Model.checkpoint() and conv_f1.cpp for an example.

File size: 4.9 KB
Line 
1#include "genotypemini.h"
2#include <frams/vm/classes/collectionobj.h>
3#include <common/log.h>
4
5#define FIELDSTRUCT GenotypeMini
6ParamEntry genotypemini_paramtab[] =
7{
8        { "Genotype", 1, 31, "org", },
9
10        { "name", 0, 0, "Name", "s 0 40", FIELD(name), },
11        { "genotype", 0, 0, "Genotype", "s 1", FIELD(genotype), "Genes as a string of characters.", },
12
13        { "info_timestamp", 1, 0, "Last modified", "ft 0 -1 0", FIELD(info_timestamp), },
14        { "info_author", 1, 0, "Author name", "s 0 100", FIELD(info_author), },
15        { "info_author_ispublic", 1, 0, "Author name is public", "d 0 1 1", FIELD(info_author_ispublic), },
16        { "info_email", 1, 0, "Author email", "s 0 100", FIELD(info_email), },
17        { "info_email_ispublic", 1, 0, "Author email is public", "d 0 1 0", FIELD(info_email_ispublic), },
18        { "info", 1, 0, "Description", "s 1 1000", FIELD(info), "Short description of key features of this creature.", },
19        { "info_origin", 1, 0, "Origin", "d 0 4 0 ~Unspecified~Designed~Designed and evolved~Evolved under various conditions~Evolved using single, constant setup", FIELD(info_origin), "Declaration of how this genotype originated." },
20        { "info_how_created", 1, 0, "How created", "s 1 1000", FIELD(info_how_created), "Description of the process of designing and/or evolving this genotype." },
21        { "info_performance", 1, 0, "Performance notes", "s 1 1000", FIELD(info_performance), "Description of why this genotype is special/interesting and how it performs." },
22
23        { "energy0", 0, 0, "Starting energy", "f 0 -1 0", FIELD(energy0), },
24        { "numparts", 0, 0, "Body parts", "d", FIELD(numparts), },
25        { "numjoints", 0, 0, "Body joints", "d", FIELD(numjoints), },
26        { "numneurons", 0, 0, "Brain size", "d", FIELD(numneurons), },
27        { "numconnections", 0, 0, "Brain connections", "d", FIELD(numconnections), },
28
29        { "num", 0, 0, "Ordinal number", "d", FIELD(ordnumber), },
30        { "gnum", 0, 0, "Generation", "d", FIELD(generation), },
31
32        { "instances", 0, 0, "Instances", "d", FIELD(instances), "Copies of this genotype", },
33
34        { "lifespan", 0, 0, "Life span", "f", FIELD(lifespan), "Average life span", },
35        { "velocity", 0, 0, "Velocity", "f", FIELD(velocity), "Average velocity", },
36        { "distance", 0, 0, "Distance", "f", FIELD(distance), },
37        { "vertvel", 0, 0, "Vertical velocity", "f", FIELD(vertvel), },
38        { "vertpos", 0, 0, "Vertical position", "f", FIELD(vertpos), },
39
40        { "user1", 0, 0, "User field 1", "x", FIELD(user1), },
41        { "user2", 0, 0, "User field 2", "x", FIELD(user2), },
42        { "user3", 0, 0, "User field 3", "x", FIELD(user3), },
43        { "data", 3, PARAM_OBJECTSET, "Custom fields dictionary", "oDictionary", FIELD(data), },
44
45        { "is_valid", 0, 0, "Validity", "d -1 1 -1", FIELD(is_valid),
46        "0 = invalid genotype\n"
47        "1 = valid genotype\n"
48        "-1 = validity is not known." },
49
50        { "uid", 0, 0, "#", "s", FIELD(uid), "Unique identifier" },
51
52        { "mutate", 0, 0, "Mutate", "p()", PROCEDURE(p_mutate), },
53
54        { 0, 0, 0, },
55};
56#undef FIELDSTRUCT
57
58void GenotypeMini::initData()
59{
60        DictionaryObject *d = new DictionaryObject;
61        data = d->makeObject();
62}
63
64GenotypeMini::GenotypeMini()
65{
66        initData();
67}
68
69GenotypeMini::GenotypeMini(const GenotypeMini &src)
70{
71        initData();
72        name = src.name; genotype = src.genotype; info = src.info; uid = src.uid; info_timestamp = src.info_timestamp; info_author = src.info_author; info_email = src.info_email; info_author_ispublic = src.info_author_ispublic; info_email_ispublic = src.info_email_ispublic; info_origin = src.info_origin; info_how_created = src.info_how_created; info_performance = src.info_performance; energy0 = src.energy0; lifespan = src.lifespan; velocity = src.velocity; distance = src.distance; vertvel = src.vertvel; vertpos = src.vertpos; numparts = src.numparts; numjoints = src.numjoints; numneurons = src.numneurons; numconnections = src.numconnections; ordnumber = src.ordnumber; generation = src.generation; instances = src.instances; is_valid = src.is_valid; user1 = src.user1; user2 = src.user2; user3 = src.user3;
73
74        // special case for 'data' (Dictionary inside an ExtObject)
75        // 'data=src.data' would copy the Dictionary object reference (very wrong!)
76        // performing a shallow copy instead:
77        DictionaryObject *d = DictionaryObject::fromObject(data);
78        DictionaryObject *src_d = DictionaryObject::fromObject(src.data);
79        d->copyFrom(src_d);
80}
81
82void GenotypeMini::clear()
83{
84        Param p(genotypemini_paramtab, this);
85        p.setDefault();
86}
87
88static std::function<Geno(const Geno&)> genman_mutate;
89
90void GenotypeMini::useGenManMutate(std::function<Geno(const Geno&)> gmm)
91{
92        genman_mutate = gmm;
93}
94
95void GenotypeMini::p_mutate(ExtValue *args, ExtValue *ret)
96{
97        if (ret != NULL) ret->setEmpty();
98        if (!genman_mutate)
99        {
100                logPrintf("GenotypeMini", "mutate", LOG_ERROR, "No function for mutation provided. See GENOTYPEMINI_USE_GENMAN().");
101                return;
102        }
103        Geno in(genotype.c_str(), -1, name.c_str(), info.c_str());
104        Geno out = genman_mutate(in);
105        genotype = out.getGenesAndFormat();
106        name = out.getName();
107        info = out.getComment();
108}
Note: See TracBrowser for help on using the repository browser.