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

Last change on this file since 346 was 346, checked in by Maciej Komosinski, 9 years ago

Each thread can use its own simulator's GenMan?

  • Property svn:eol-style set to native
File size: 4.3 KB
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
[109]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;
[145]13class GenoConvManager;
[109]14
15class GenoValidator
16{
[150]17public:
18        virtual int testGenoValidity(Geno& g) = 0;/// -1=no information  0=invalid  1=valid
[109]19};
20
[150]21/// Basic GenoValidator that works by building a Model from any Geno (by converting to f0).
22/// Validation fails when the model can't be built or the genotype can't be converted.
23class ModelGenoValidator : public GenoValidator
[145]24{
25public:
26        int testGenoValidity(Geno& g);
27};
28
[109]29/// basic information about a single genotype.
[150]30class Geno : public DestrBase
[109]31{
[150]32        friend class Simulator;//needs to access validators directly
33        SString gen;
34        SString name;
35        char format;
36        SString txt;
37        int isvalid; ///< <0 -> unknown   >=0 -> value for "isValid"
[109]38
[150]39        Geno *f0gen;
[109]40
[150]41        int mapinshift; /// number of characters in the initial comment
42        int mapoutshift; /// number of characters in the output comment
43        int multiline;
[109]44
[150]45        void init(const SString& genstring, char genformat, const SString& genname, const SString& comment);
46        void validate(void);
[109]47
[150]48        void freeF0();
[109]49
[150]50        bool isInvalid() { return isvalid == 0; }
[109]51
[150]52        friend class Model;
53        friend class GenoConvManager;
54
[109]55public:
[346]56        typedef SListTempl<GenoValidator*> Validators;
57
[150]58        /// create a genotype object from primitives
59        /// @param genstring pure genotype, without any comments
60        /// @param genformat genotype format
61        /// @param comment information about genotype (for genetic operators and "history")
62        Geno(const char *genstring = 0, char genformat = -1, const char *genname = 0, const char *comment = 0);
[109]63
[150]64        /// create a genotype object from primitives
65        /// @param genstring pure genotype, wihtout any comments
66        /// @param genformat genotype format
67        /// @param name genotype name, new name will generated if needed
68        /// @param comment information about genotype (for genetic operators and "history")
69        Geno(const SString& genstring, char genformat, const SString& genname, const SString& comment);
[109]70
[150]71        /// create object from full string, containing optional format and comment information
72        Geno(const SString & fullstring);
[109]73
[150]74        /// clone
75        Geno(const Geno& src);
[109]76
[150]77        void operator=(const Geno& src);
[109]78
[150]79        ~Geno();
[109]80
[150]81        void setValid(int v) { isvalid = v; }
82        int getValid() { return isvalid; }
[109]83
[150]84        /// return string representation, with format comment at the beginning
85        SString toString(void) const;
86        SString shortString(void) const;
[109]87
[150]88        void setString(const SString& genewithcomments);
[109]89
[150]90        /** @param newformat=-1 -> don't change */
91        void setGene(const SString& g, char newformat = -1);
92        SString getGene(void) const;
[109]93
[150]94        SString getName(void) const;
95        void setName(const SString&);
96        char getFormat(void) const;
[109]97
[150]98        SString getComment(void) const;
99        void setComment(const SString&);
[109]100
[150]101        /// invalid genotype cannot be used to build a creature
102        bool isValid(void);
[109]103
[150]104        /// make converted version of the genotype.
105        /// @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).
106        Geno getConverted(char otherformat, MultiMap *m = 0, bool *converter_missing = NULL);
[109]107
[150]108        /// @return -1 = before first char in the string
109        /// @return -2 = after last char in the string
110        int mapGenToString(int genpos) const;
111        /// @return -1 = before first char in the genotype
112        /// @return -2 = after last char in the genotype
113        int mapStringToGen(int stringpos) const;
[109]114
[150]115        int operator==(const Geno &g) { return (format == g.format) && (gen == g.gen); }
[109]116
[150]117        void* owner;
[145]118
[150]119        // managing global Geno-related objects (used for validation and conversion)
[346]120        static Validators& getValidators();
121        static void addValidator(GenoValidator* gv,int at_position=9999) { getValidators().insert(at_position,gv); }
122        static void removeValidator(GenoValidator* gv) { getValidators() -= gv; }
[150]123        static void useConverters(GenoConvManager& gcm) { converters = &gcm; }
124        static GenoConvManager &getConverters() { return *converters; }
125protected:
126        static GenoConvManager *converters;
[109]127};
128
129#ifndef NO_GENOCONVMANAGER
130#include "genoconv.h"
131#endif
132
133#endif
Note: See TracBrowser for help on using the repository browser.