Ignore:
Timestamp:
03/25/14 03:22:37 (10 years ago)
Author:
Maciej Komosinski
Message:

Updated f4 sources and made f4 available for GDK demo apps

Location:
cpp/frams/genetics/f4
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f4/f4_general.h

    r193 r196  
    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  */
     1// This file is a part of the Framsticks GDK.
     2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
     3// Refer to http://www.framsticks.com/ for further information.
     4
     5// Copyright (C) 1999,2000  Adam Rotaru-Varga (adam_rotaru@yahoo.com), GNU LGPL
    236
    247#ifndef _F4_GENERAL_H_
    258#define _F4_GENERAL_H_
    269
    27 #include "f4_orientmat.h"
    28 #include "3d.h"
    29 #include "sstring.h"
    30 #include "multirange.h"
     10//#include "f4_orientmat.h"
     11#include <frams/util/3d.h>
     12#include <frams/util/sstring.h>
     13#include <frams/util/multirange.h>
    3114
    3215#ifdef DMALLOC
     
    3720class f4_Props
    3821{
    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;
     22public:
     23        // fill with default values
     24        f4_Props();
     25        // must sum to 1
     26        void normalizeBiol4();
     27        void executeModifier(char modif);
     28        void adjust();
     29
     30        double len;      // length (dlug)
     31        double curv;     // curvedness (skr)
     32        double mass;
     33        double friction;
     34        double ruch;
     35        double assim;
     36        double odpor;
     37        double ingest;  // ingestion (wchl)
     38        double twist;
     39        double energ;
    5740};
    5841
     
    8568{
    8669public:
    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;
     70        class repeat_ptr
     71        {
     72        public:
     73                repeat_ptr() : node(NULL), count(-1) { };
     74                repeat_ptr(f4_node * a, int b) : node(a), count(b) { };
     75                inline void null() { node = NULL; count = -1; };
     76                inline bool isNull() const { return ((node == NULL) || (count <= 0)); };
     77                inline void dec() { count--; };
     78                f4_node *  node;  // ptr to repetition code
     79                char       count; // repetition counter
     80        };
     81
     82        class repeat_stack  // a stack of repet_ptr's
     83        {
     84        public:
     85                repeat_stack() { top = 0; };
     86                inline void null() { top = 0; };
     87                inline void push(repeat_ptr A) { if (top >= stackSize) return; ptr[top] = A; top++; };
     88                inline void pop() { if (top > 0) top--; };
     89                inline repeat_ptr * first() { return &(ptr[top - (top > 0)]); };
     90                static const int stackSize = 4;  // max 4 nested levels
     91                repeat_ptr ptr[stackSize];
     92                short int top;  // top of the stack
     93        };
     94
     95        f4_Cell(int nname,
     96                f4_Cell * ndad, int nangle, f4_Props newP);
     97        f4_Cell(f4_Cells * nO, int nname, f4_node * ngeno, f4_node * ngcur,
     98                f4_Cell * ndad, int nangle, f4_Props newP);
     99        ~f4_Cell();
     100
     101        int onestep();  // execute one simulation step (till a division)
     102
     103        int   addlink(f4_Cell * nfrom, double nw, long nt);
     104        void  adjustRec();
     105
     106        int        name;     // name (number)
     107        int        type;     // type
     108        f4_Cell *  dadlink;
     109        f4_Cells * org; // uplink to organism
     110
     111        f4_node *  genot;         // genotype
     112        f4_node *  gcur;        // current genotype execution pointer
     113        int        active;      // whether development is still active
     114        repeat_stack repeat;
     115        int        recProcessedFlag;  // used during recursive traverse
     116        // remember the genotype codes affecting this cell so far
     117        MultiRange genoRange;
     118
     119        f4_Props     P;             // properties
     120        int          anglepos;      // number of position within dad's children (,)
     121        int          childcount;    // number of children
     122        int          commacount;    // number of postitions at lastend (>=childcount)
     123        double       rolling;       // rolling angle ('R') (around x)
     124        double       xrot;
     125        double       zrot;          // horizontal rotation angle due to
     126        // branching (around z)
     127        //Pt3D         firstend;      // coord.s of first end (connects to parent)
     128        //Pt3D         lastend;       // last end
     129        //f4_OrientMat OM;
     130        double       mz;            // freedom in z
     131        long         p2_refno;   // number of last end part object, used in f0
     132        long         joint_refno;   // number of the joint object, used in f0
     133        long         neuro_refno;   // number of the neuro object, used in f0
     134
     135        long         ctrl;  // neuron type
     136        double       state;
     137        double       inertia;
     138        double       force;
     139        double       sigmo;
     140        f4_CellLink* links[MAXINPUTS];
     141        int          nolink;
    159142};
    160143
     
    164147{
    165148public:
    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;
     149        f4_CellLink(f4_Cell * nfrom, double nw, long nt);
     150        f4_Cell *    from;
     151        // type: 0: input, 1 '*', 2 'G', 3 'T', 4 'S'
     152        long         t;
     153        double       w;
    171154};
    172155
     
    176159class f4_Cells
    177160{
    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;
     161public:
     162        f4_Cells(f4_node * genome, int nrepair);
     163        f4_Cells(SString &genome, int nrepair);
     164        ~f4_Cells();
     165        void addCell(f4_Cell * newcell);
     166        void toF1Geno(SString &out);       // output to f1 format, approximation
     167        int  onestep();       // simulate all parts for one step
     168        int  simulate();      // simulate development, return error (0 for ok)
     169        // for error reporting / genotype fixing
     170        int  geterror() { return error; };
     171        int  geterrorpos() { return errorpos; };
     172        void setError(int nerrpos);
     173        void setRepairRemove(int nerrpos, f4_node * rem);
     174        int  setRepairInsert(int nerrpos, f4_node * parent, f4_node * insert);
     175        void repairGeno(f4_node * geno, int whichchild);
     176
     177        // the cells
     178        f4_Cell * C[MAX4CELLS];
     179        int       nc;
    197180
    198181private:
    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
     182        // for error reporting / genotype fixing
     183        int repair;
     184        int error;
     185        int errorpos;
     186        f4_node * repair_remove;
     187        f4_node * repair_parent;
     188        f4_node * repair_insert;
     189        void toF1GenoRec(int curc, SString &out);
     190        f4_Cell * tmpcel;               // needed by toF1Geno
     191        f4_node * f4rootnode;          // used by constructor
    209192};
    210193
     
    216199{
    217200public:
    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.
     201        char      name; // one-letter 'name'
     202        f4_node * parent;       // parent link, or NULL
     203        f4_node * child;        // child, or NULL
     204        f4_node * child2;       // second child, or NULL
     205        int       pos;        // original position in string
     206        int       i1;           // internal int  parameter1
     207        long      l1;           // internal long parameter1
     208        double    f1;           // internal double parameter1
     209
     210        f4_node();
     211        f4_node(char nname, f4_node * nparent, int npos);
     212        ~f4_node();
     213        int       addChild(f4_node * nchi);
     214        int       removeChild(f4_node * nchi);
     215        int       childCount(); // return no of children, 0, 1, or 2
     216        int       count();      // return no of nodes (recursive)
     217        f4_node * ordNode(int n);       // returns the nth subnode (0-)
     218        f4_node * randomNode(); // returns a random subnode
     219        f4_node * randomNodeWithSize(int min, int max); // returns a random subnode with given size
     220        void      sprintAdj(char *& buf);       // print recursively
     221        f4_node * duplicate();         // create duplicate copy. recursive.
     222        void      destroy();    // release memory. recursive.
    240223private:
    241   void     sprint(SString & out);       // print recursively
     224        void     sprint(SString & out); // print recursively
    242225};
    243226
Note: See TracChangeset for help on using the changeset viewer.