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

Last change on this file since 1314 was 1313, checked in by Maciej Komosinski, 2 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

  • 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-2024  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 allowed_modifiers;        //to be used in mutations
62
63private:
64
65        /**
66         * Validates a f4 genotype. If the genotype is invalid, the genotype is repaired
67         * and the validation is repeated. Validation is performed as long as repairing
68         * is not effective, or the number of retries exceeded the given limit.
69         * @param geno genotype tree
70         * @param retrycount maximum amount of repair retries
71         * @return GENOOPER_OK if genotype is valid, GENOPER_REPAIR if genotype can be repaired, GENOPER_OPFAIL if genotype can't be repaired
72         */
73        int  ValidateRecur(f4_Node *geno, int retrycount) const;
74
75        /**
76         * Performs mutation of an f4 genotype. The mutation is performed on a random node
77         * from a given tree. The method of mutation is chosen via the roulette selection,
78         * where probabilities of choosing each mutation type are given in the 'prob'
79         * array. Possible mutation types are:
80         *  - F4_ADD - adds new element to the genotype by:
81         *   - 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),
82         *   - F4_ADD_CONN - adding connection for an existing neuron,
83         *   - F4_ADD_NEUPAR - adding neuron property to the selected node, if it is a neuron node,
84         *   - F4_ADD_REP - adding a repetition node before a randomly selected node (the repetition node has 2 repetitions),
85         *   - F4_ADD_SIMP - adding a simple node before a selected node,
86         *  - F4_DEL - removes a randomly selected node (the node needs to have a parent and at least one child, otherwise returns GENOPER_OPFAIL),
87         *  - F4_MOD - modifies one of simple nodes by:
88         *   - '<' - swapping children in division
89         *   - '[' - modifying connection of a neuron
90         *   - '#' - incrementing or decrementing repetition count
91         *
92         * @param g input genotype; the result of mutation will be stored in this parameter
93         * @param method reference to the variable that will get the selected method of mutation
94         * @return GENOPER_OK if mutation was performed successfully, GENOPER_FAIL otherwise
95         */
96        int  MutateOne(f4_Node *& g, int &method) const;
97
98        /**
99         * Finds all neurons in g (in the order of ordNode()) and returns their neuroclasses in a vector.
100         * Additionally, looks for the needle_neuron node and returns its index (in the list of the returned vector) as found_index,
101         * or -1 if not found (for example, it was not a neuroclass node or not added to the "g" tree).
102         * @param g root node
103         * @param needle_neuron neuroclass node to look for in all nodes
104         * @param found_index returned index of needle
105         * @return all nodes that are neurons
106         */
107        static vector<NeuroClass*> findAllNeuronsAndNode(f4_Node * const & g, f4_Node* const &needle_neuron, int &found_index);
108
109        /**
110         * 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.
111         * @param g root node
112         * @param neuron neuroclass node to look for in all nodes in g
113         * @param other_has_output if true, other neuron will provide output; otherwise, it will accept input(s)
114         * @param neuron_index returned index of neuron
115         * @param other_neuron_index returned index of a random neuron that provides an output or accepts inputs
116         * @return true if succeeded, false otherwise
117         */
118        static bool findConnectionNeuronIndexes(f4_Node * const &g, f4_Node *neuron, bool other_has_output, int &neuron_index, int &other_neuron_index);
119
120        /**
121         * Creates a random connection to an existing neuron and randomizes connection weight
122         * sensor for a neuron.
123         * @param nn neuron class node
124         * @param nn_index index of the nn neuron
125         * @param other_index index of the neuron providing output, to get input from
126         */
127        void connectionNodeChangeRandom(f4_Node *nn, int nn_index, int other_index) const;
128
129        /**
130         * Introduces a random modification to the neuron node.
131         * @param nn neuron node
132         */
133        void nparNodeMakeRandom(f4_Node *nn) const;
134
135        /**
136         * Increases or decreases the amount of repetitions in the repetition node.
137         * @param nn repetition node
138         */
139        void repeatNodeChangeRandom(f4_Node *nn) const;
140
141        /**
142         * Tries to perform a mutation until success. There is a maximum of 20 tries. Returns GENOPER_OK or GENOPER_OPFAIL.
143         * @param g genotype tree
144         * @param method reference to the variable that will get the selected method of mutation
145         * @return GENOPER_OK if performed successful mutation, GENOPER_FAIL otherwise
146         */
147        int  MutateOneValid(f4_Node *&g, int &method) const;
148
149        /**
150         * Performs crossover of two creatures. The 'chg' parameter determines approximately what
151         * percentage of the first creature should form the offspring. '1-chg' is the percentage
152         * of the second creature in the offspring.
153         * @param g1 first parent
154         * @param g2 second parent
155         * @param chg percentage of the first parent in offspring (the second parent has the rest)
156         */
157        int  CrossOverOne(f4_Node *g1, f4_Node *g2, float chg) const;
158};
159
160
161#endif
162
Note: See TracBrowser for help on using the repository browser.