source: cpp/frams/genetics/f4/f4_oper.h @ 1252

Last change on this file since 1252 was 1234, checked in by Maciej Komosinski, 20 months ago

Simplify sequences of modifier genes, cancelling out antagonistic ones and limiting the number of identical genes

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5// Copyright (C) 1999,2000  Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL
6// Copyright (C) since 2001 Maciej Komosinski
7
8#ifndef _F4_OPER_H_
9#define _F4_OPER_H_
10
11#include <stdio.h>
12#include "f4_general.h"
13#include "common/nonstd.h"
14#include "../genooperators.h"
15#include <frams/param/param.h>
16
17/** @name Codes for general mutation types */
18//@{
19#define F4_ADD           0 ///<Adding new node
20#define F4_DEL           1 ///<Deletion of node
21#define F4_MOD           2 ///<Modification of node
22#define F4_COUNT         3 ///<Count of mutation types
23//@}
24
25/** @name Codes for specific F4_ADD mutation subtypes */
26//@{
27#define F4_ADD_DIV       0
28#define F4_ADD_CONN      1
29#define F4_ADD_NEUPAR    2
30#define F4_ADD_REP       3
31#define F4_ADD_SIMP      4
32#define F4_ADD_COUNT     5
33//@}
34
35/** @name Codes for specific F4_MOD neuron mutation subtypes (not included in mutation_method_names, all are considered F4_MOD there) */
36//@{
37#define F4_MODNEU_CONN     0
38#define F4_MODNEU_WEIGHT   1
39#define F4_MODNEU_COUNT    2
40//@}
41
42class Geno_f4 : public GenoOperators
43{
44public:
45        Geno_f4();
46        void setDefaults();
47
48        int checkValidity(const char *, const char *genoname);
49        int validate(char *&, const char *genoname);
50        int mutate(char *& g, float & chg, int &method);
51        int crossOver(char *&g1, char *&g2, float& chg1, float& chg2);
52        const char* getSimplest() { return "X"; }
53        uint32_t style(const char *g, int pos);
54
55        // mutation probabilities
56        double prob[F4_COUNT];            ///<relative probabilities of selecting mutation types in f4 genotype
57        double probadd[F4_ADD_COUNT];     ///<relative probabilities of selecting addition mutation subtypes
58        double probmodneu[F4_MODNEU_COUNT];     ///<relative probabilities of selecting neuron mutation subtypes
59        paInt mut_max_rep;                ///maximum allowed number of repetitions for the '#' repetition gene
60
61        SString excluded_modifiers;       ///<Modifiers that are excluded in mutation process
62        static const char *all_modifiers;
63
64private:
65
66        /**
67         * Validates a f4 genotype. If the genotype is invalid, the genotype is repaired
68         * and the validation is repeated. Validation is performed as long as repairing
69         * is not effective, or the number of retries exceeded the given limit.
70         * @param geno genotype tree
71         * @param retrycount maximum amount of repair retries
72         * @return GENOOPER_OK if genotype is valid, GENOPER_REPAIR if genotype can be repaired, GENOPER_OPFAIL if genotype can't be repaired
73         */
74        int  ValidateRecur(f4_Node *geno, int retrycount) const;
75
76        /**
77         * Performs mutation of an f4 genotype. The mutation is performed on a random node
78         * from a given tree. The method of mutation is chosen via the roulette selection,
79         * where probabilities of choosing each mutation type are given in the 'prob'
80         * array. Possible mutation types are:
81         *  - F4_ADD - adds new element to the genotype by:
82         *   - F4_ADD_DIV - replacing randomly selected node with division node '<', setting this node as a child and creating new stick or neuron sibling of the selected cell (the neuron-type sibling will be connected to a random existing neuron),
83         *   - F4_ADD_CONN - adding connection for an existing neuron,
84         *   - F4_ADD_NEUPAR - adding neuron property to the selected node, if it is a neuron node,
85         *   - F4_ADD_REP - adding a repetition node before a randomly selected node (the repetition node has 2 repetitions),
86         *   - F4_ADD_SIMP - adding a simple node before a selected node,
87         *  - F4_DEL - removes a randomly selected node (the node needs to have a parent and at least one child, otherwise returns GENOPER_OPFAIL),
88         *  - F4_MOD - modifies one of simple nodes by:
89         *   - '<' - swapping children in division
90         *   - '[' - modifying connection of a neuron
91         *   - '#' - incrementing or decrementing repetition count
92         *
93         * @param g input genotype; the result of mutation will be stored in this parameter
94         * @param method reference to the variable that will get the selected method of mutation
95         * @return GENOPER_OK if mutation was performed successfully, GENOPER_FAIL otherwise
96         */
97        int  MutateOne(f4_Node *& g, int &method) const;
98
99        /**
100         * Finds all neurons in g (in the order of ordNode()) and returns their neuroclasses in a vector.
101         * Additionally, looks for the needle_neuron node and returns its index (in the list of the returned vector) as found_index,
102         * or -1 if not found (for example, it was not a neuroclass node or not added to the "g" tree).
103         * @param g root node
104         * @param needle_neuron neuroclass node to look for in all nodes
105         * @param found_index returned index of needle
106         * @return all nodes that are neurons
107         */
108        static vector<NeuroClass*> findAllNeuronsAndNode(f4_Node * const & g, f4_Node* const &needle_neuron, int &found_index);
109
110        /**
111         * Finds indexes of a given neuron and another random (output- or input-providing) neuron in the list of all neurons present in the "g" tree.
112         * @param g root node
113         * @param neuron neuroclass node to look for in all nodes in g
114         * @param other_has_output if true, other neuron will provide output; otherwise, it will accept input(s)
115         * @param neuron_index returned index of neuron
116         * @param other_neuron_index returned index of a random neuron that provides an output or accepts inputs
117         * @return true if succeeded, false otherwise
118         */
119        static bool findConnectionNeuronIndexes(f4_Node * const &g, f4_Node *neuron, bool other_has_output, int &neuron_index, int &other_neuron_index);
120
121        /**
122         * Creates a random connection to an existing neuron and randomizes connection weight
123         * sensor for a neuron.
124         * @param nn neuron class node
125         * @param nn_index index of the nn neuron
126         * @param other_index index of the neuron providing output, to get input from
127         */
128        void connectionNodeChangeRandom(f4_Node *nn, int nn_index, int other_index) const;
129
130        /**
131         * Introduces a random modification to the neuron node.
132         * @param nn neuron node
133         */
134        void nparNodeMakeRandom(f4_Node *nn) const;
135
136        /**
137         * Increases or decreases the amount of repetitions in the repetition node.
138         * @param nn repetition node
139         */
140        void repeatNodeChangeRandom(f4_Node *nn) const;
141
142        /**
143         * Tries to perform a mutation until success. There is a maximum of 20 tries. Returns GENOPER_OK or GENOPER_OPFAIL.
144         * @param g genotype tree
145         * @param method reference to the variable that will get the selected method of mutation
146         * @return GENOPER_OK if performed successful mutation, GENOPER_FAIL otherwise
147         */
148        int  MutateOneValid(f4_Node *&g, int &method) const;
149
150        /**
151         * Performs crossover of two creatures. The 'chg' parameter determines approximately what
152         * percentage of the first creature should form the offspring. '1-chg' is the percentage
153         * of the second creature in the offspring.
154         * @param g1 first parent
155         * @param g2 second parent
156         * @param chg percentage of the first parent in offspring (the second parent has the rest)
157         */
158        int  CrossOverOne(f4_Node *g1, f4_Node *g2, float chg) const;
159};
160
161
162#endif
163
Note: See TracBrowser for help on using the repository browser.