COSC-4P82-Final-Project/lib/lilgp/kernel_c/genspace.c

183 lines
5.1 KiB
C
Raw Normal View History

2024-04-01 00:01:49 -04:00
/* lil-gp Genetic Programming System, version 1.0, 11 July 1995
* Copyright (C) 1995 Michigan State University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Douglas Zongker (zongker@isl.cps.msu.edu)
* Dr. Bill Punch (punch@isl.cps.msu.edu)
*
* Computer Science Department
* A-714 Wells Hall
* Michigan State University
* East Lansing, Michigan 48824
* USA
*
*/
#include <lilgp.h>
/* initialize_genspace()
*
* allocates each genspace with GENSPACE_START lnodes.
*/
void initialize_genspace ( void )
{
int i;
oputs ( OUT_SYS, 30, " generation spaces.\n" );
for ( i = 0; i < GENSPACE_COUNT; ++i )
{
gensp[i].size = GENSPACE_START;
gensp[i].data = (lnode *)MALLOC ( gensp[i].size * sizeof ( lnode ) );
memset ( gensp[i].data, 0, gensp[i].size * sizeof ( lnode ) );
gensp[i].used = 0;
#ifdef DEBUG
printf ( "genspace %d initialized with %d nodes.\n",
i, gensp[i].size );
#endif
}
}
/* free_genspace()
*
* frees all the genspaces.
*/
void free_genspace ( void )
{
int i;
for ( i = 0; i < GENSPACE_COUNT; ++i )
{
FREE ( gensp[i].data );
gensp[i].data = NULL;
}
}
/* gensp_next()
*
* returns the address of the next free lnode in the given generation
* space. enlarges the generation space by GENSPACE_GROW lnodes if
* there is no free space.
*/
lnode * gensp_next ( int space )
{
while ( gensp[space].used >= gensp[space].size )
{
int oldsize = gensp[space].size;
gensp[space].size += GENSPACE_GROW;
gensp[space].data = (lnode *)REALLOC ( gensp[space].data,
gensp[space].size *
sizeof ( lnode ) );
memset ( gensp[space].data+oldsize, 0,
(gensp[space].size-oldsize) * sizeof ( lnode ) );
#ifdef DEBUG
printf ( "next: genspace %d grown to %d nodes.\n",
space, gensp[space].size );
#endif
}
return gensp[space].data+(gensp[space].used++);
}
/* gensp_next_int()
*
* like gensp_next(), but returns the position (not the address) of the
* next free lnode.
*/
int gensp_next_int ( int space )
{
while ( gensp[space].used >= gensp[space].size )
{
int oldsize = gensp[space].size;
gensp[space].size += GENSPACE_GROW;
gensp[space].data = (lnode *)REALLOC ( gensp[space].data,
gensp[space].size *
sizeof ( lnode ) );
memset ( gensp[space].data+oldsize, 0,
(gensp[space].size-oldsize) * sizeof ( lnode ) );
#ifdef DEBUG
printf ( "next_int: genspace %d grown to %d nodes from %d.\n",
space, gensp[space].size, oldsize );
#endif
}
return gensp[space].used++;
}
/* gensp_dup_tree()
*
* copies a completed tree out of a generation space into the tree
* pointer passed.
*/
void gensp_dup_tree ( int space, tree *t )
{
t->size = gensp[space].used;
t->nodes = tree_nodes ( gensp[space].data );
t->data = (lnode *)MALLOC ( t->size * sizeof ( lnode ) );
memcpy ( t->data, gensp[space].data, t->size * sizeof ( lnode ) );
}
/* gensp_reset()
*
* marks a genspace as being empty.
*/
void gensp_reset ( int space )
{
gensp[space].used = 0;
memset ( gensp[space].data, 0, gensp[space].size * sizeof ( lnode ) );
}
/* gensp_print()
*
* prints out a section of the genspace from [i..j)
*/
void gensp_print ( int space, int i, int j, FILE *out )
{
int u;
function *f;
/* remove problems when used wants to print last x nodes */
if ( i < 0 )
i = 0;
for ( u = i; u < j; ++u )
{
/* print value of skip node */
if ( gensp[space].data[u].s < 1000 )
fprintf(out, "<%d: %d> ", u, gensp[space].data[u].s);
else if ( gensp[space].data[u].f->arity == 0 )
{
/* print out a terminal */
f = gensp[space].data[u].f;
if ( f->ephem_gen )
{
fprintf(out, "[%d: %s] [%d: ERC] ", u, (f->ephem_str)(gensp[space].data[u+1].d->d), u+1);
++u;
}
else
fprintf(out, "[%d: %s] ", u, f->string);
}
else
fprintf(out, "(%d: %s) ", u, gensp[space].data[u].f->string);
}
fprintf(out, "\n");
}