Changeset 259 for cpp/frams/genetics


Ignore:
Timestamp:
12/05/14 08:09:47 (9 years ago)
Author:
Maciej Komosinski
Message:

Added support for genetic mapping in f9

Location:
cpp/frams/genetics/f9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/f9/conv_f9.cpp

    r197 r259  
    88#include <common/nonstd_stl.h> //ARRAY_LENGTH
    99
    10 #define APPLY_DETERMINISTIC_BODY_NOISE //this representation easily produces perfectly vertical sticks that would stay upright forever. In most cases such infinite perfection is not desired, so we make the construct less perfect by perturbing its coordinates.
     10#define APPLY_DETERMINISTIC_BODY_NOISE //this genetic representation easily produces perfectly vertical sticks that would stay upright forever in simulation. In most cases such infinite perfection is not desired, so we make the construct less perfect by perturbing its coordinates.
    1111
    1212GenoConv_f90::GenoConv_f90()
     
    1515        in_format = '9';
    1616        out_format = '0';
    17         mapsupport = 0; //would be easy and nice to add!
     17        mapsupport = 1;
    1818}
    1919
     
    3131        Model m;
    3232        m.open();
    33         int recently_added = addSegment(m, vertices, current, 0xDead);
     33        int recently_added = addSegment(m, 0, vertices, current, 0xDead);
    3434        for (int i = 0; i < in.len(); i++)
    3535        {
     
    4444                        (*(delta + axis)) += dir * 2 - 1; //+1 or -1 in the given axis
    4545                        current.add(delta);
    46                         recently_added = addSegment(m, vertices, current, recently_added);
     46                        recently_added = addSegment(m, i, vertices, current, recently_added);
    4747                }
    4848        }
     
    5454        if (m.getPartCount() < 2) //only one part <=> there were no valid turtle commands in the input genotype
    5555                return ""; //so we return an invalid f0 genotype
     56        if (map != NULL)
     57                m.getCurrentToF0Map(*map);
    5658        return m.getF0Geno().getGene();
    5759}
    5860
    59 int GenoConv_f90::addSegment(Model &m, vector<XYZ_LOC> &vertices, const XYZ_LOC &new_vertex, int recently_added)
     61int GenoConv_f90::addSegment(Model &m, int genenr, vector<XYZ_LOC> &vertices, const XYZ_LOC &new_vertex, int recently_added)
    6062{
    6163        if (vertices.size() < 1) //empty model?
     
    7274                Part *p1 = m.getPart(recently_added);
    7375                Part *p2 = m.getPart(vertex_here);
    74                 if (m.findJoint(p1, p2) < 0 && m.findJoint(p2, p1) < 0) //new Joint needed? should always be true if we just created a new Part (vertex_here was <0)
    75                 {
    76                         m.addNewJoint(p1, p2);
    77                 }
     76                p1->addMapping(MultiRange(genenr, genenr));
     77                p2->addMapping(MultiRange(genenr, genenr));
     78
     79                int j12 = m.findJoint(p1, p2);
     80                int j21 = m.findJoint(p2, p1);
     81                if (j12 >= 0)
     82                        m.getJoint(j12)->addMapping(MultiRange(genenr, genenr));
     83                else if (j21 >= 0)
     84                        m.getJoint(j21)->addMapping(MultiRange(genenr, genenr));
     85                else //both j12<0 and j21<0. New Joint needed. Should always happen if we just created a new Part (vertex_here was <0)
     86                        m.addNewJoint(p1, p2)->addMapping(MultiRange(genenr, genenr));
    7887                return vertex_here;
    7988        }
     
    134143        int parts_count = m.getPartCount();
    135144        SList jlist;
    136         for (int i = 0; i<parts_count; i++)
     145        for (int i = 0; i < parts_count; i++)
    137146        {
    138147                Part *p = m.getPart(i);
  • cpp/frams/genetics/f9/conv_f9.h

    r197 r259  
    2121struct XYZ_LOC
    2222{
    23         int x,y,z; //coordinates xyz of a vertex - represented as int's so that it is easy and safe to check identity. Could also be done using lists of Model's Parts, but that would involve comparing floats
    24         XYZ_LOC() {x=y=z=0;}
    25         void add(int delta[3]) {x+=delta[0]; y+=delta[1]; z+=delta[2];}
    26         bool same_coordinates(const XYZ_LOC &loc) {return x==loc.x && y==loc.y && z==loc.z;}
     23        int x, y, z; //coordinates xyz of a vertex - represented as int's so that it is easy and safe to check identity. Could also be done using lists of Model's Parts, but that would involve comparing floats
     24        XYZ_LOC() { x = y = z = 0; }
     25        void add(int delta[3]) { x += delta[0]; y += delta[1]; z += delta[2]; }
     26        bool same_coordinates(const XYZ_LOC &loc) { return x == loc.x && y == loc.y && z == loc.z; }
    2727};
    2828
    2929
    3030// The f9->f0 converter
    31 class GenoConv_f90: public GenoConverter
     31class GenoConv_f90 : public GenoConverter
    3232{
    3333public:
     
    3939protected:
    4040        //auxiliary methods
    41         int addSegment(Model &m,vector<XYZ_LOC> &vertices,const XYZ_LOC &new_vertex,int recently_added);
    42         int findVertexAt(vector<XYZ_LOC> &vertices,const XYZ_LOC &new_vertex);
    43         int addNewVertex(Model &m,vector<XYZ_LOC> &punkty,const XYZ_LOC &nowypunkt);
     41        int addSegment(Model &m, int genenr, vector<XYZ_LOC> &vertices, const XYZ_LOC &new_vertex, int recently_added);
     42        int findVertexAt(vector<XYZ_LOC> &vertices, const XYZ_LOC &new_vertex);
     43        int addNewVertex(Model &m, vector<XYZ_LOC> &punkty, const XYZ_LOC &nowypunkt);
    4444        void setColors(Model &m); //sets fixed (independent from genes) colors and widths on a model, purely for aesthetic purposes
    4545        void perturbPartLocations(Model &m); //deterministic "body noise", see APPLY_DETERMINISTIC_BODY_NOISE
Note: See TracChangeset for help on using the changeset viewer.