source: cpp/frams/genetics/geneprops.h

Last change on this file was 1313, checked in by Maciej Komosinski, 4 months ago

Color mutations in f1 and f4, and a new syntax for "allowed modifiers" (opposite to previous "excluded modifiers") with optional probabilities for each modifier

File size: 8.4 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#ifndef _GENEPROPS_H
6#define _GENEPROPS_H
7
8#include <common/nonstd_math.h>
9#include <frams/model/model.h>
10
11
12
13
14
15#define F14_MODIFIERS_BASIC "LlRrCcQqFfMm"
16#define F14_MODIFIERS_RARE "EeWwSsAaIi" //An expdef would need to handle many of these properly/specifically to ensure reasonable behavior, and hardly any expdef does. Modifying initial energy of a creature as a result of its genes (Ee) is in general not a good idea, but may make sense in some specific competitive biological simulations. Weight (Ww) works only in water, and in water sinking/going up should usually be caused by real "intentional" activity of a creature, not by its inherited weight. Stamina (Ss) is no longer needed as destructive collisions are not supported, and even if they were, some expdef would need to impose reasonable restrictions on the value of this parameter (e.g. similar to normalizeBiol4()) so there is some cost associated with it, and the specific consequences of destructions should be defined as needed. For assimilation (Aa), there is a dedicated parameter in CreaturesGroup. Ingestion (Ii) influences how fast energy is transferred to Parts of a creature, and optimizing this property may make sense in experiments where energy is present in the environment and can be ingested - if needed, this modifier should be enabled for mutation.
17#define F14_MODIFIERS_COLOR "DdGgBb"
18#define F14_MODIFIERS_COLOR_SPORADIC "D(0.01)d(0.01)G(0.01)g(0.01)B(0.01)b(0.01)" //add a small chance so that color mutations can rarely occur. Functionally they are neutral so we do not want to waste computational resources on creating and evaluating such duplicate individuals, but it is nice to have visually distinct "lineages" by allowing propagated color genes to be extremely rarely modified. Also, if these color modifiers have positive probability, all newly created sticks are assigned a random color immediately on creation.
19
20#define F14_MODIFIERS     F14_MODIFIERS_BASIC  F14_MODIFIERS_RARE  F14_MODIFIERS_COLOR
21
22
23
24class GenePropsOp
25{
26public:
27        virtual ~GenePropsOp() {}
28        virtual double increase(double value) const = 0;
29        virtual double decrease(double value) const = 0;
30        void apply(double &value, char modif) const;
31};
32
33class GenePropsOp_Legacy : public GenePropsOp
34{
35        double minvalue, maxvalue, defvalue, change, revchange;
36public:
37        GenePropsOp_Legacy(double minvalue, double maxvalue, double defvalue, double change, double revchange = -1);
38        double increase(double value) const;
39        double decrease(double value) const;
40        friend class GenePropsOps_AllChange05;
41};
42
43class GenePropsOp_NormalizedAndScaled : public GenePropsOp
44{
45        GenePropsOp_Legacy normalized;
46public:
47        GenePropsOp_NormalizedAndScaled(double change) :normalized(-1, 1, 0, change) {}
48        virtual double scale(double value) const { return value; }
49        virtual double scaleInv(double value) const { return value; }
50        double increase(double value) const { return scale(normalized.increase(scaleInv(value))); }
51        double decrease(double value) const { return scale(normalized.decrease(scaleInv(value))); }
52};
53
54class GenePropsOp_Exponential : public GenePropsOp_NormalizedAndScaled
55{
56        double a, b, c;
57        double log_a;
58        bool linear;
59public:
60        GenePropsOp_Exponential(double minvalue, double maxvalue, double defvalue, double change = 0.5);
61        double scale(double) const;
62        double scaleInv(double) const;
63};
64
65class GenePropsOps
66{
67public:
68        ~GenePropsOps();
69
70        GenePropsOp* length;
71        GenePropsOp* curvedness;
72        GenePropsOp* weight;
73        GenePropsOp* friction;
74        GenePropsOp* muscle_power;
75        GenePropsOp* assimilation;
76        GenePropsOp* stamina;
77        GenePropsOp* ingestion;
78        GenePropsOp* twist;
79        GenePropsOp* energy;
80        GenePropsOp* cred, *cgreen, *cblue;
81        bool use_normalizebiol4;
82};
83
84class GenePropsOps_Legacy : public GenePropsOps
85{
86public:
87        GenePropsOps_Legacy();
88};
89
90/**
91* Just as legacy (before 2023-06), but the influence of each modifier is uniform and simpler (no normalizeBiol4()).
92* This is what is currently used in Framsticks.
93*/
94class GenePropsOps_AllChange05 : public GenePropsOps_Legacy
95{
96public:
97        GenePropsOps_AllChange05();
98};
99
100/**
101* Just an experiment with a different scaling of the influence of individual modifier characters to cover
102* the entire range of parameter values. After investigations we decided that it does not
103* provide any advantages over "AllChange05".
104*/
105class GenePropsOps_Exponential : public GenePropsOps
106{
107public:
108        GenePropsOps_Exponential();
109};
110
111/**
112 * Contains physical, biological and other properties of a Part and a Joint (as handled
113 * by the f1 and f4 encodings), except for rotation. The constructor initializes properties with
114 * default values. In order to change a property, the executeModifier() method
115 * should be called. Modification of length, curvedness and twist properties
116 * usually affects further sticks, so new sticks should have properties of
117 * parents (prop) modified with the prop.propagateAlong() method.
118 * "Biological" properties (assimilation, stamina, muscle strength and
119 * ingestion) can be normalized after modification with normalizeBiol4().
120 * See also geneprops_test.cpp
121 */
122struct GeneProps
123{
124public:
125        double length;            ///<incremented by L, decremented by l. Physical property, length of stick
126        double curvedness;        ///<incremented by C, decremented by c. Curvedness of sticks
127        double weight;            ///<incremented by W, decremented by w. Physical property, weight of stick (in water environment light sticks swim on the surface)
128        double friction;          ///<incremented by F, decremented by f. Physical property, friction of a stick (sticks will slide on the ground or stick to it)
129
130        double muscle_power;      ///<incremented by M, decremented by m. Biological property, muscle strength (muscle speed). Strong muscles act with bigger force, gain higher speed, can resist bigger stress, and use more energy
131        double assimilation;      ///<incremented by A, decremented by a. Biological property, assimilation, photosynthesis (a vertical stick can assimilate twice as much as horizontal one)
132        double stamina;           ///<incremented by S, decremented by s. Biological property, stamina (increases chance of survival during fights)
133        double ingestion;         ///<incremented by I, decremented by i. Biological property, ingestion (ability to gain/transfer energy from food)
134
135        double twist;             ///<incremented by Q, decremented by q. Twist of a stick
136        double energy;            ///<incremented by E, decremented by e. Energy of a creature
137
138        double muscle_bend_range; ///<Used only by conv_f1
139        bool muscle_reset_range;  ///<Used only by conv_f1
140
141        double cred;              ///<incremented by D, decremented by d. Part's red color proportion
142        double cgreen;            ///<incremented by B, decremented by b. Part's green color proportion
143        double cblue;             ///<incremented by G, decremented by g. Part's blue color proportion
144
145        static GeneProps standard_values;
146       
147        static GenePropsOps* getDefaultOps();
148        static void setDefaultOps(GenePropsOps* ops);
149
150        static GenePropsOps* getAllChange05Ops();
151        static GenePropsOps* getLegacyOps();
152       
153        /**
154         * Constructor initializing all properties with default values.
155         */
156        GeneProps();
157
158        /**
159         * Normalizes biological properties (muscle_power, assimilation, stamina, and ingestion).
160         * This method is called in executeModifier() when any of the biological properties is modified
161         * and \a use_normalizebiol4 is true. All values of those properties sum up to 1.
162         */
163        void normalizeBiol4();
164
165        /**
166         * Checks whether the given character is property modifier. If yes, the property
167         * is modified and properties are normalized when needed.
168         * @param modif character that might be a property modifier
169         * @return 0 if the provided character was property modifier, -1 otherwise
170         */
171        int executeModifier(char modif, GenePropsOps* ops = NULL);
172
173        /**
174         * Adjusts current properties for the next stick. In order to set
175         * new properties to the created stick, the copy of the previous stick should be created,
176         * and propagateAlong() should be used for that copy.
177         * @param use_reset_range true if this method should modify muscle_bend_range (used in f1 conversion).
178         */
179        void propagateAlong(bool use_f1_muscle_reset_range, GenePropsOps* ops = NULL);
180};
181
182#endif // _GENEPROPS_H
Note: See TracBrowser for help on using the repository browser.