www.pudn.com > dgpc.rar > gpshared.c


/*======================================================================+
| PGPC: Parallel Genetic Programming in C                               |
| (c) 1994 Genetic Algorithm Technology Corp. all rights reserved       |
|   written by David Andre                                              |
+======================================================================*/
/*======================================================================+
| FILE: gpshared.c                                                      |
| DESCRIPTION: Contains code required both in the boss node and the     |
| breeder nodes, such as randomizers and the like.  May disappear when  |
| out-of sample fittnes cases are allowed.                              |
|                                                                       |
| REVISIONS:                                                            |
| Jan 24, 1995:  Works as of today, no known bugs.                      |
| Mar 21, 1995:  Changed and added Randu, PM randomizers, added         |
|                fitness case randomizers                               |
+======================================================================*/


#include 
#include 
#include 
#include "gp.h"
#include 
#include "proto.h"
#include "time.h"
#ifdef __BORLANDC__
#include "gpi_stub.h"
#else
/*#include 
#include 
#include 
#include 
#include "mncommon.h"
#include "mnproto.h"
#include "gpi.h"*/
#endif
#ifdef _ICC
	 #include 
#endif

#define RANDU_MODER     2147483648  /*  2^32   */
#define PM_MODER        2147483647  /*  2^32-1 */
#define RANDU_CONSTANT  65539
#define PM_CONSTANT     16807

#define MY_MODER    2147483647

static unsigned long      g_my_state;
static unsigned long      g_constant;
static unsigned long      g_mod_num;

static unsigned long      randu_constant;
static unsigned long      pm_constant;

static  char g_str[256];

unsigned long MyRand(void)/*;*/ /*funcdef*/
{
    return g_my_state = ((g_my_state * g_constant) % (MY_MODER));
}

unsigned long MyMaxRand(void)/*;*/ /*funcdef*/
{
	 return (unsigned long) (MY_MODER);
}

unsigned long MySrand(unsigned long my_seed)/*;*/ /*funcdef*/
{
    g_constant = (unsigned long int)pow(7.0,5.0);
    return (g_my_state = my_seed);
}

unsigned long GetMyRand(void)/*;*/ /*funcdef*/
{
    return(g_my_state);
}

unsigned long PM_Rand(void)/*;*/ /*funcdef*/
{
    g_my_state = ((g_my_state * PM_CONSTANT) % (PM_MODER));
    return (g_my_state);
}

unsigned long PM_MaxRand(void)/*;*/ /*funcdef*/
{
    return (unsigned long) (PM_MODER);
}

unsigned long PM_Srand(unsigned long my_seed)/*;*/ /*funcdef*/
{
    g_my_state = my_seed;
    return (g_my_state);
}

unsigned long GetPMRand(void)
{
    return(g_my_state);
}

unsigned long Randu_Rand(void)/*;*/ /*funcdef*/
{
    return g_my_state = ((g_my_state * RANDU_CONSTANT) % (RANDU_MODER));
}

unsigned long Randu_MaxRand(void)/*;*/ /*funcdef*/
{
    return (unsigned long) (RANDU_MODER);
}

unsigned long Randu_Srand(unsigned long my_seed)/*;*/ /*funcdef*/
{
    return (g_my_state = my_seed);
}

unsigned long GetRanduRand(void)
{
    return(g_my_state);
}



/*---------------------------------------------*/


void SetSeed(Population *pop,  unsigned long s  )/*;*/ /*funcdef - dgpc - SetSeed*/
{
  ((*pop).pop_startup_info.seed) = (long)s;
  /* new code */
  SRAND((signed long)s);
}

unsigned long GetSeed(Population * pop)/*;*/ /*funcdef - dgpc - GetSeed*/
{
  return ((*pop).pop_startup_info.seed);
}

unsigned long GetSystemSeed(void)
{
	long temp;
	temp = rand();
	srand(temp);
	return(temp);
}


float GaussianNoise(float mean, float sigma )/*;*/ /*funcdef - dgpc - GaussianNoise*/
{
  float		gauss;
  static int gaussian_noise_toggle;
  static float  gaussian_noise_uniform1, gaussian_noise_uniform2;
  static float  gaussian_noise_temp;


  if (gaussian_noise_toggle) {
    gaussian_noise_uniform1 = (RandomFloat( (float) 1.0));
    gaussian_noise_uniform2 = (RandomFloat( (float) 1.0));
    gaussian_noise_temp =
		  (float) sqrt(-2.0 * log((double)gaussian_noise_uniform1));
    gauss = gaussian_noise_temp *
      (float)cos((float)(2.0*3.14159*gaussian_noise_uniform2));
  }
  else {
    gauss = gaussian_noise_temp *
      (float)sin((float)(2.0*3.14159*gaussian_noise_uniform2));
  }
  gaussian_noise_toggle = ! gaussian_noise_toggle;
  return mean + (sigma * gauss);
}

float ParkMillerRandomizer(void)/*;*/ /*funcdef - dgpc - ParkMillerRandomizer*/
{
  float retval;


  retval = (float) (((float)(double) RAND()) / ((float)(double) DRAND_MAX));
  return retval;
}

float RandomFloat( float f  )/*;*/  /*	 0 <=  RandomFloat() < f  */ /*funcdef - dgpc - RandomFloat*/
{
  float	retval;

  retval = (f*((float) 1.0-ParkMillerRandomizer()));

  return retval;

}

int RandomInt(   int i  )/*;*/    /* 0 <= RandomInt() < i   */ /*funcdef - dgpc - RandomInt*/
{
  int	retval;

  if (i==0) return(0);

 retval = ((int) fabs(((double)i *  (double)RandomFloat((float) 1.0)))) % i;

  return retval;
}

float gpRandomFloat( float f  )/*;*/  /*	 0 <=  RandomFloat() < f  */ /*funcdef - dgpc - RandomFloat*/
{
  float	retval;

  retval = (f*((float) 1.0-ParkMillerRandomizer()));

  return retval;

}

int gpRandomInt(   int i  )/*;*/    /* 0 <= RandomInt() < i   */ /*funcdef - dgpc - gpRandomInt*/
{
  int	retval;

  if (i==0) return(0);

 retval = ((int) fabs(((double)i *  (double)gpRandomFloat((float) 1.0)))) % i;

  return retval;
}


float FitParkMillerRandomizer(void)/*;*/ /*funcdef - dgpc - ParkMillerRandomizer*/
{
  float retval;


  retval = (float) (((float)(double) FRAND()) / ((float)(double) FDRAND_MAX));
  return retval;
}

float FitRandomFloat( float f  )/*;*/  /*	 0 <=  RandomFloat() < f  */ /*funcdef - dgpc - RandomFloat*/
{
  float	retval;

  retval = (f*((float) 1.0-FitParkMillerRandomizer()));

  return retval;

}

int FitRandomInt(   int i  )/*;*/    /* 0 <= RandomInt() < i   */ /*funcdef - dgpc - RandomInt*/
{
  int	retval;

  if (i==0) return(0);

 retval = ((int) fabs(((double)i *  (double)FitRandomFloat((float) 1.0)))) % i;

  return retval;
}



void CreateIndividual(Individual * ind)/*;*/ /*funcdef - dgpc - CreateIndividual*/
{
	int i,j;
	int bnum;
	int codep;
        int bsize;
		
	i=0;bnum=0;codep=0;               	
	for (j=0;jrpbs[j].branchnum = bnum;
		ind->rpbs[j].num_nodes  = bsize;
		ind->rpbs[j].tree = &(ind->code[codep]);
		ind->rpbs[j].ind  = ind;
		bnum++;
                codep +=bsize;
	}
	for (j=0;jadfs[j].branchnum = bnum;
		ind->adfs[j].num_nodes  = bsize;
		ind->adfs[j].tree = &(ind->code[codep]);
                ind->adfs[j].ind  = ind;
		bnum++;
                codep +=bsize;
	}
	i=i;
}


void CopyIndividual(Individual * ind1, Individual * ind2)/*;*/ /*funcdef - dgpc - CopyIndividual*/
{
	int i,j;

		
        CreateIndividual(ind2);
        for (i=0;icode[i].opcode = ind1->code[i].opcode;
            ind2->code[i].jump   = ind1->code[i].jump;
        }
	for (i=0;i< NUM_RPBS; i++)
	{
		for (j=0;jrpbs[i].function_vector[j] = ind1->rpbs[i].function_vector[j];
	}
	for (i=0;i< MAX_NUM_ADFS; i++)
	{
		for (j=0;jadfs[i].function_vector[j] = ind1->adfs[i].function_vector[j];
	}
	ind2->s_fitness = ind1->s_fitness;
	ind2->hits = ind1->hits;
        ind2->beauty = ind1->beauty;
	ind2->current_number_of_adfs = ind1->current_number_of_adfs;
	for (i=0;iadf_arity[i] = ind1->adf_arity[i];
       #if (USE_MULTI_TYPED_ADFS)
        for (i=0;iadf_type[i] = ind1->adf_type[i];
       #endif
#if (DO_POOR_WHITE_TRASH_AUDIT)
        for (i=0;icy_num_of_ops[i] = ind1->cy_num_of_ops[i];
        for (i=0;iop_list[i] = ind1->op_list[i];
        for (i=0;ifit_at_op[i] = ind1->fit_at_op[i];
        ind2->num_ops_done = ind1->num_ops_done;

#endif

}


/*-----------------------------------------------------------------------------*/		

void CopyTree(Branch * br1, Branch * br2)/*;*/ /*copies br1 into br2*/ /*funcdef - dgpc - CopyTree*/
{
int i;
	 for (i=0;inum_nodes;i++)	
    {
    br2->tree[i].opcode = br1->tree[i].opcode;
    br2->tree[i].jump   = br1->tree[i].jump;
    }
}


int ChooseRandomFunction(Population * pop,int kind,Branch *br) /*funcdef*/
{
/* kind == 0 means leaf node*/
/* kind == 1 means internal node*/
/* kind == 2 means any node */
    int i,temp;
    int sum;
    sum=0;
    for (i=0;i 0)
                sum+= (*pop).func_table[i].weight;
        }
        else
                sum+= (*pop).func_table[i].weight;
    }
    temp = RandomInt(sum);
    i=0;
    sum=0;
    while(i < TOTAL_NUMBER_OF_FUNCTIONS)
    {
        if (kind == 0)
        {
            if (_function_arity(i) == 0)
                sum+= (*pop).func_table[i].weight;
        }
        else if (kind == 1)
        {
            if (_function_arity(i) > 0)
                sum+= (*pop).func_table[i].weight;
        }
        else
                sum+= (*pop).func_table[i].weight;

        if (temp < sum)
            return(i);
        i++;
    }
	 gpi_SendError("Error in choose random function\n");
	 return(-1);
}


int ChooseRandomCSSFunction(Population * pop,int kind,Branch *br,int * fvector) /*funcdef*/
{
/* kind == 0 means leaf node*/
/* kind == 1 means internal node*/
/* kind == 2 means any node */
    int i,temp;
    int sum;
    sum=0;
    for (i=0;i 0)
                sum+= (*pop).func_table[i].weight;
        }
        else if (fvector[i] > -1)
                sum+= (*pop).func_table[i].weight;
    }
    temp = RandomInt(sum);
    i=0;
    sum=0;
    while(i < TOTAL_NUMBER_OF_FUNCTIONS)
    {
        if (kind == 0)
        {
            if (fvector[i] == 0)
                sum+= (*pop).func_table[i].weight;
        }
        else if (kind == 1)
        {
            if (fvector[i] > 0)
                sum+= (*pop).func_table[i].weight;
        }
        else if (fvector[i] > -1)
                sum+= (*pop).func_table[i].weight;

        if (temp < sum)
            return(i);
        i++;
    }
	 gpi_SendError("Error in choose random css function\n");
	 return(-1);
}



     		


void init_macro(Branch *br)/*;*/ /*funcdef*/
{
(br->ind->index_ptr)++;
}



void skip_subtree(Branch *br)/*;*//*funcdef*/
{
(br->ind->index_ptr) = TraverseSubtree(br,(br->ind->index_ptr));
(br->ind->index_ptr)++;
}


int GetFuncNumber(char * str,Population * pop)
{
int i;
    for (i=0;iind->index_ptr;
br->ind->index_ptr  =0;
temp2 = _eval_subtree(br,pop);
br->ind->index_ptr  =temp;
return(temp2);

}


/*----------------------------------------------------*/
void CopyFVector(int * fvector_from, int * fvector_to)
{
    int i;
    for (i=0;irpbs[bnum]) );
	else
		return( &(dude->adfs[bnum - NUM_RPBS]) );
}