source: cpp/frams/genetics/geno.h @ 1156

Last change on this file since 1156 was 988, checked in by Maciej Komosinski, 4 years ago

Building a Model from Geno now fails when Model.shape is incompatible with Geno.format; renamed enum constants for genetic formats: xxx_FORMAT => FORMAT_xxx

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _GENO_H_
6#define _GENO_H_
7
8#include <frams/util/sstring.h>
9#include <frams/util/extvalue.h>
10
11class MultiMap;
12class Geno;
13class GenoConvManager;
14
15class GenoValidator
16{
17public:
18        virtual int testGenoValidity(Geno& g) = 0;/// -1=no information  0=invalid  1=valid
19        virtual ~GenoValidator() { };
20};
21
22/// Basic GenoValidator that works by building a Model from any Geno (by converting to f0).
23/// Validation fails when the model can't be built or the genotype can't be converted.
24class ModelGenoValidator : public GenoValidator
25{
26public:
27        int testGenoValidity(Geno& g);
28};
29
30/// basic information about a single genotype.
31class Geno : public DestrBase
32{
33        friend class Simulator;//needs to access validators directly
34        SString gen;
35        SString name;
36        SString format;
37        SString txt;
38        int isvalid; ///< <0 -> unknown   >=0 -> value for "isValid"
39
40        Geno *f0gen;
41
42        int mapinshift; /// number of characters in the initial comment
43        int mapoutshift; /// number of characters in the output comment
44        int multiline;
45
46        void init(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment);
47        void validate(void);
48
49        void freeF0();
50
51        bool isInvalid() { return isvalid == 0; }
52
53        friend class Model;
54        friend class GenoConvManager;
55
56public:
57        static const SString FORMAT_INVALID;
58        static const SString FORMAT_UNKNOWN;
59        typedef SListTempl<GenoValidator*> Validators;
60
61        /// create a genotype object from primitives
62        /// @param genstring pure genotype, without any comments
63        /// @param genformat genotype format
64        /// @param comment information about genotype (for genetic operators and "history")
65        Geno(const char *genstring = 0, const char* genformat = 0, const char *genname = 0, const char *comment = 0);
66
67        Geno(const char *genstring, char genformat, const char *genname = 0, const char *comment = 0); //old style Geno, accepts char genoformat
68
69        /// create a genotype object from primitives
70        /// @param genstring pure genotype, wihtout any comments
71        /// @param genformat genotype format
72        /// @param name genotype name, new name will generated if needed
73        /// @param comment information about genotype (for genetic operators and "history")
74        Geno(const SString& genstring, const SString& genformat, const SString& genname, const SString& comment);
75
76        /// create object from full string, containing optional format and comment information
77        Geno(const SString & fullstring);
78
79        /// clone
80        Geno(const Geno& src);
81
82        void operator=(const Geno& src);
83
84        ~Geno();
85
86        void setValid(int v) { isvalid = v; }
87        int getValid() { return isvalid; }
88
89        /// return string representation, with format comment at the beginning
90        SString getGenesAndFormat(void) const;
91
92        void setString(const SString& genewithcomments);
93
94        /** @param genformat="" -> detect genotype format from genstring comment (like the constructor does), else specify the valid format in genformat and pure genes in genstring. */
95        void setGenesAndFormat(const SString& genstring, const SString& genformat = FORMAT_UNKNOWN);
96        /** g must be pure genes, without format. For the standard behavior use setGenesAndFormat() */
97        void setGenesAssumingSameFormat(const SString& g);
98        SString getGenes(void) const;
99
100        SString getName(void) const;
101        void setName(const SString&);
102        SString getFormat(void) const;
103
104        SString getComment(void) const;
105        void setComment(const SString&);
106
107        /// invalid genotype cannot be used to build a creature
108        bool isValid(void);
109
110        /// make converted version of the genotype.
111        /// @param converter_missing optional output parameter (ignored when NULL). Receives true if the conversion fails because of the lack of appropriate converter(s) (the returned Geno is always invalid in this case). Receives false if the genotype was converted by a converter or a converter chain (the returned Geno can be valid or invalid, depending on the converter's decision).
112        Geno getConverted(SString otherformat, MultiMap *m = 0, bool using_checkpoints = false, bool *converter_missing = NULL);
113
114        /// @return -1 = before first char in the string
115        /// @return -2 = after last char in the string
116        int mapGenToString(int genpos) const;
117        /// @return -1 = before first char in the genotype
118        /// @return -2 = after last char in the genotype
119        int mapStringToGen(int stringpos) const;
120
121        int operator==(const Geno &g) { return (format == g.format) && (gen == g.gen); }
122
123        void* owner;
124
125        // managing global Geno-related objects (used for validation and conversion)
126        static Validators* useValidators(Validators* val);
127        static Validators* getValidators();
128
129        static GenoConvManager* useConverters(GenoConvManager* gcm);
130        static GenoConvManager* getConverters();
131
132        static bool formatIsOneOf(const SString& format, const SString& format_list);
133        static bool isF0Format(const SString& format_list);
134        static const SString F0_FORMAT_LIST;
135};
136
137#ifndef NO_GENOCONVMANAGER
138#include "genoconv.h"
139#endif
140
141#endif
Note: See TracBrowser for help on using the repository browser.