#include "Rule.h"
#include <stdlib.h>
Rule::Rule(void)
{
grow_in = new bool[6];
grow_wgt = new float*[4];
for ( int w = 0 ; w < 4 ; w++ )
grow_wgt[w] = new float[6];
tend_in = new bool[11];
tend_wgt = new float[11];
fit.clumpsize = 0;
fit.perimeter = 0;
fit.value = 0;
}
Rule::~Rule(void)
{
delete[6] grow_in;
for ( int w = 0 ; w < 4 ; w++ )
delete[6] grow_wgt[w];
delete[4] grow_wgt;
delete[11] tend_in;
delete[11] tend_wgt;
}
void Rule::InitBasic()
{
/*
//default grow function is pass-through
for ( int w = 0 ; w < 4 ; w++ )
{
grow_wgt[w][0] = 1.0;
for ( int i = 1 ; i < 6 ; i++ )
{
grow_wgt[w][i] = 0.0;
}
}
//default tend function is Life
for ( int w = 0 ; w < 11 ; w++ )
tend_wgt[w] = -1.0;
tend_wgt[10] = -0.75;
tend_wgt[9] = 0.5;
tend_wgt[2] = 0.5;
tend_wgt[3] = 1.0;
*/
//default grow function is lower-right corner
for ( int w = 0 ; w < 4 ; w++ )
{
for ( int i = 0 ; i < 6 ; i++ )
{
grow_wgt[w][i] = 0;
}
}
grow_wgt[0][0] = 1.0;
//default tend function is pass-through
for ( int w = 0 ; w < 11 ; w++ )
tend_wgt[w] = float(-0.1);
tend_wgt[9] = 5.0;
}
void Rule::Copy( Rule* r )
{
for ( int w = 0 ; w < 4 ; w++ )
{
for ( int i = 0 ; i < 6 ; i++ )
{
grow_wgt[w][i] = r->grow_wgt[w][i];
}
}
for ( int w = 0 ; w < 11 ; w++ )
tend_wgt[w] = r->tend_wgt[w];
fit = r->fit;
}
void Rule::Mutate()
{
//There are 4x6 grow variables, and 1x11 tend variables.
if ( rand() < RAND_MAX / 100 )
{
int w = rand() % 11;
tend_wgt[w] += float(( rand() / ( RAND_MAX * 1.0 ) ) * 20 - 10.0);
}
else
{
int w = rand() % 4;
int i = rand() % 6;
grow_wgt[w][i] += float(( rand() / ( RAND_MAX * 1.0 ) ) * 20 - 10.0);
}
}
Quad Rule::Grow( bool s, bool u, bool r, bool d, bool l )
{
float out[4];
grow_in[0] = s;
grow_in[1] = u;
grow_in[2] = r;
grow_in[3] = d;
grow_in[4] = l;
grow_in[5] = true;
for ( int w = 0 ; w < 4 ; w++ )
{
out[w] = 0.0;
for ( int i = 0 ; i < 6 ; i++ )
{
if ( grow_in[i] ) out[w] += grow_wgt[w][i];
}
}
Quad result;
result.dl = ( out[0] > 0.0 );
result.dr = ( out[1] > 0.0 );
result.ul = ( out[2] > 0.0 );
result.ur = ( out[3] > 0.0 );
return result;
}
bool Rule::Tend( bool state, int ncount )
{
for ( int w = 0 ; w < 9 ; w++ )
tend_in[w] = ( w == ncount );
tend_in[9] = state;
tend_in[10] = true;
float accum = 0.0;
for ( int w = 0 ; w < 11 ; w++ )
if ( tend_in[w] ) accum += tend_wgt[w];
return ( accum > 0.0 );
}
# |
Change |
User |
Description |
Committed |
|
#1
|
4772 |
Sam Stafford |
Something I've been doodling with. |
|
|