source: cpp/f4/f4_general.h @ 109

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

added f4, a genetic representation that describes development of an organism

File size: 7.6 KB
Line 
1/*
2 *  f4_general.h - f4 genotype functions.
3 *
4 *  f4genotype - f4 format genotype conversions for FramSticks
5 *
6 *  Copyright (C) 1999,2000  Adam Rotaru-Varga (adam_rotaru@yahoo.com)
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the GNU Lesser General Public
10 *  License as published by the Free Software Foundation; either
11 *  version 2.1 of the License, or (at your option) any later version.
12 *
13 *  This library is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *  Lesser General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Lesser General Public
19 *  License along with this library; if not, write to the Free Software
20 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#ifndef _F4_GENERAL_H_
25#define _F4_GENERAL_H_
26
27#include "f4_orientmat.h"
28#include "3d.h"
29#include "sstring.h"
30#include "multirange.h"
31
32#ifdef DMALLOC
33#include <dmalloc.h>
34#endif
35
36
37class f4_Props
38{
39 public:
40  // fill with default values
41  f4_Props();
42  // must sum to 1
43  void normalizeBiol4();
44  void executeModifier(char modif);
45  void adjust();
46
47  double len;      // length (dlug)
48  double curv;     // curvedness (skr)
49  double mass;
50  double friction;
51  double ruch;
52  double assim;
53  double odpor;
54  double ingest;  // ingestion (wchl)
55  double twist;
56  double energ;
57};
58
59extern f4_Props stdProps;
60
61
62// rolling (one-time)
63void rolling_dec(double * v);
64void rolling_inc(double * v);
65
66
67class f4_node;   // later
68class f4_Cell;   // later
69class f4_Cells;  // later
70
71
72// cell types
73#define T_UNDIFF4 40
74#define T_STICK4  41
75#define T_NEURON4 42
76
77int scanrec(const char * s, unsigned int slen, char stopchar);
78
79
80class f4_CellLink;
81#define MAXINPUTS 100
82
83// an abstract cell type, extension of part/stick -- for developmental encoding
84class f4_Cell
85{
86public:
87  class repeat_ptr
88  {
89   public:
90    repeat_ptr() : node(NULL), count(-1) { };
91    repeat_ptr(f4_node * a, int b) : node(a), count(b) { };
92    inline void null() { node = NULL; count = -1; };
93    inline bool isNull() const { return ((node == NULL) || (count <= 0)); };
94    inline void dec() { count--; };
95    f4_node *  node;  // ptr to repetition code
96    char       count; // repetition counter
97  };
98
99  class repeat_stack  // a stack of repet_ptr's
100  {
101   public:
102    repeat_stack() { top=0; };
103    inline void null() { top=0; };
104    inline void push(repeat_ptr A) { if (top>=stackSize) return; ptr[top]=A; top++; };
105    inline void pop() { if (top>0) top--; };
106    inline repeat_ptr * first() { return &(ptr[top-(top>0)]); };
107    static const int stackSize = 4;  // max 4 nested levels
108    repeat_ptr ptr[stackSize];
109    short int top;  // top of the stack
110  };
111
112  f4_Cell(int nname,
113    f4_Cell * ndad, int nangle, f4_Props newP);
114  f4_Cell(f4_Cells * nO, int nname, f4_node * ngeno, f4_node * ngcur,
115    f4_Cell * ndad, int nangle, f4_Props newP);
116  ~f4_Cell();
117
118  int onestep();        // execute one simulation step (till a division)
119
120  int   addlink(f4_Cell * nfrom, double nw, long nt);
121  void  adjustRec();
122
123  int        name;     // name (number)
124  int        type;     // type
125  f4_Cell *  dadlink;
126  f4_Cells * org;       // uplink to organism
127
128  f4_node *  genot;       // genotype
129  f4_node *  gcur;        // current genotype execution pointer
130  int        active;      // whether development is still active
131  repeat_stack repeat;
132  int        recProcessedFlag;  // used during recursive traverse
133  // remember the genotype codes affecting this cell so far
134  MultiRange genoRange;
135
136  f4_Props     P;             // properties
137  int          anglepos;      // number of position within dad's children (,)
138  int          childcount;    // number of children
139  int          commacount;    // number of postitions at lastend (>=childcount)
140  double       rolling;       // rolling angle ('R') (around x)
141  double       xrot;
142  double       zrot;          // horizontal rotation angle due to
143                              // branching (around z)
144  //Pt3D         firstend;      // coord.s of first end (connects to parent)
145  //Pt3D         lastend;       // last end
146  //f4_OrientMat OM;
147  double       mz;            // freedom in z
148  long         p2_refno;   // number of last end part object, used in f0
149  long         joint_refno;   // number of the joint object, used in f0
150  long         neuro_refno;   // number of the neuro object, used in f0
151
152  long         ctrl;  // neuron type
153  double       state;
154  double       inertia;
155  double       force;
156  double       sigmo;
157  f4_CellLink* links[MAXINPUTS];
158  int          nolink;
159};
160
161
162// an input link to a neuron
163class f4_CellLink
164{
165public:
166               f4_CellLink(f4_Cell * nfrom, double nw, long nt);
167  f4_Cell *    from;
168  // type: 0: input, 1 '*', 2 'G', 3 'T', 4 'S'
169  long         t;
170  double       w;
171};
172
173
174// a collection of cells, like Organism, for developmental encoding
175#define MAX4CELLS 100
176class f4_Cells
177{
178 public:
179  f4_Cells(f4_node * genome, int nrepair);
180  f4_Cells(SString &genome, int nrepair);
181  ~f4_Cells();
182  void addCell(f4_Cell * newcell);
183  void toF1Geno(SString &out);       // output to f1 format, approximation
184  int  onestep();       // simulate all parts for one step
185  int  simulate();      // simulate development, return error (0 for ok)
186  // for error reporting / genotype fixing
187  int  geterror() { return error;};
188  int  geterrorpos() { return errorpos;};
189  void setError(int nerrpos);
190  void setRepairRemove(int nerrpos, f4_node * rem);
191  int  setRepairInsert(int nerrpos, f4_node * parent, f4_node * insert);
192  void repairGeno(f4_node * geno, int whichchild);
193
194  // the cells
195  f4_Cell * C[MAX4CELLS];
196  int       nc;
197
198private:
199  // for error reporting / genotype fixing
200  int repair;
201  int error;
202  int errorpos;
203  f4_node * repair_remove;
204  f4_node * repair_parent;
205  f4_node * repair_insert;
206  void toF1GenoRec(int curc, SString &out);
207  f4_Cell * tmpcel;             // needed by toF1Geno
208  f4_node * f4rootnode;          // used by constructor
209};
210
211
212/**
213 * Class to organize a f4 genotype in a tree structure.
214 */
215class f4_node
216{
217public:
218  char      name;       // one-letter 'name'
219  f4_node * parent;     // parent link, or NULL
220  f4_node * child;      // child, or NULL
221  f4_node * child2;     // second child, or NULL
222  int       pos;        // original position in string
223  int       i1;         // internal int  parameter1
224  long      l1;         // internal long parameter1
225  double    f1;         // internal double parameter1
226
227            f4_node();
228            f4_node(char nname, f4_node * nparent, int npos);
229            ~f4_node();
230  int       addChild(f4_node * nchi);
231  int       removeChild(f4_node * nchi);
232  int       childCount();       // return no of children, 0, 1, or 2
233  int       count();    // return no of nodes (recursive)
234  f4_node * ordNode(int n);     // returns the nth subnode (0-)
235  f4_node * randomNode();       // returns a random subnode
236  f4_node * randomNodeWithSize(int min, int max);       // returns a random subnode with given size
237  void      sprintAdj(char *& buf);     // print recursively
238  f4_node * duplicate();         // create duplicate copy. recursive.
239  void      destroy();  // release memory. recursive.
240private:
241  void     sprint(SString & out);       // print recursively
242};
243
244// convert f4 geno string to tree structure (internal)
245f4_node * f4_processtree(const char * geno);
246int f4_processrec(const char * genot, unsigned pos0, f4_node * parent);
247
248
249#endif
Note: See TracBrowser for help on using the repository browser.