📄 mutate.c
字号:
/* 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>typedef struct{ int keep_trying; double internal; double external; double *tree; double treetotal; char *sname; sel_context *sc; int method; int mindepth, maxdepth;} mutate_data;/* operator_mutate_init() * * initialize a breedphase record for mutation. much of this is cut and * paste from the crossover operator; see that for (a few) more comments. */int operator_mutate_init ( char *options, breedphase *bp ){ int errors = 0; mutate_data *md; int i, j, k, m; double r; char **argv, **targv; int internalset = 0, externalset = 0; char *cp; md = (mutate_data *)MALLOC ( sizeof ( mutate_data ) ); /* fill in the breedphase record. */ bp->operator = OPERATOR_MUTATE; bp->data = (void *)md; bp->operator_free = operator_mutate_free; bp->operator_start = operator_mutate_start; bp->operator_end = operator_mutate_end; bp->operator_operate = operator_mutate; /* default values for the mutation-specific data structure. */ md->keep_trying = 0; md->internal = 0.9; md->external = 0.1; md->tree = (double *)MALLOC ( tree_count * sizeof ( double ) ); for ( j = 0; j < tree_count; ++j ) md->tree[j] = 0.0; md->treetotal = 0.0; md->sname = NULL; md->method = GENERATE_HALF_AND_HALF; md->mindepth = -1; md->maxdepth = -1; j = parse_o_rama ( options, &argv ); for ( i = 0; i < j; ++i ) { if ( strcmp ( "keep_trying", argv[i] ) == 0 ) { md->keep_trying = translate_binary ( argv[++i] ); if ( md->keep_trying == -1 ) { ++errors; error ( E_ERROR, "mutation: \"%s\" is not a valid setting for \"keep_trying\".", argv[i] ); } } else if ( strcmp ( "internal", argv[i] ) == 0 ) { internalset = 1; md->internal = strtod ( argv[++i], NULL ); if ( md->internal < 0.0 ) { ++errors; error ( E_ERROR, "mutation: \"internal\" must be nonnegative." ); } } else if ( strcmp ( "external", argv[i] ) == 0 ) { externalset = 1; md->external = strtod ( argv[++i], NULL ); if ( md->external < 0.0 ) { ++errors; error ( E_ERROR, "mutation: \"external\" must be nonnegative." ); } } else if ( strcmp ( "select", argv[i] ) == 0 ) { if ( !exists_select_method ( argv[++i] ) ) { ++errors; error ( E_ERROR, "mutation: \"%s\" is not a known selection method.", argv[i] ); } FREE ( md->sname ); md->sname = (char *)MALLOC ( (strlen(argv[i])+1) * sizeof ( char ) ); strcpy ( md->sname, argv[i] ); } else if ( strcmp ( "method", argv[i] ) == 0 ) { ++i; if ( strcmp ( argv[i], "half_and_half" ) == 0 ) md->method = GENERATE_HALF_AND_HALF; else if ( strcmp ( argv[i], "grow" ) == 0 ) md->method = GENERATE_GROW; else if ( strcmp ( argv[i], "full" ) == 0 ) md->method = GENERATE_FULL; else { ++errors; error ( E_ERROR, "mutation: \"%s\" is not a known generation method.", argv[i] ); } } else if ( strcmp ( "depth", argv[i] ) == 0 ) { md->mindepth = strtol ( argv[++i], &cp, 10 ); if ( *cp == 0 ) md->maxdepth = md->mindepth; else if ( *cp == '-' ) { md->maxdepth = strtol ( cp+1, &cp, 10 ); if ( *cp ) { ++errors; error ( E_ERROR, "mutation: malformed depth string \"%s\".", argv[i] ); } } else { ++errors; error ( E_ERROR, "mutation: malformed depth string \"%s\".", argv[i] ); } } else if ( strcmp ( "tree", argv[i] ) == 0 ) { k = parse_o_rama ( argv[++i], &targv ); if ( k != tree_count ) { ++errors; error ( E_ERROR, "mutation: wrong number of tree fields: \"%s\".", argv[i] ); } else { for ( m = 0; m < k; ++m ) { md->tree[m] = strtod ( targv[m], &cp ); if ( *cp ) { ++errors; error ( E_ERROR, "mutation: \"%s\" is not a number.", targv[m] ); } } } free_o_rama ( k, &targv ); } else if ( strncmp ( "tree", argv[i], 4 ) == 0 ) { k = strtol ( argv[i]+4, &cp, 10 ); if ( *cp ) { ++errors; error ( E_ERROR, "mutation: unknown option \"%s\".", argv[i] ); } if ( k < 0 || k >= tree_count ) { ++errors; error ( E_ERROR, "mutation: \"%s\" is out of range.", argv[i] ); } else { md->tree[k] = strtod ( argv[++i], &cp ); if ( *cp ) { ++errors; error ( E_ERROR, "mutation: \"%s\" is not a number.", argv[i] ); } } } else { ++errors; error ( E_ERROR, "mutation: unknown option \"%s\".", argv[i] ); } } free_o_rama ( j, &argv ); if ( internalset && !externalset ) md->external = 0.0; else if ( !internalset && externalset ) md->internal = 0.0; if ( md->sname == NULL ) { ++errors; error ( E_ERROR, "mutation: no selection method specified." ); } if ( md->mindepth == -1 && md->maxdepth == -1 ) { md->mindepth = 0; md->maxdepth = 4; } if ( md->mindepth < 0 || md->maxdepth < 0 || md->maxdepth < md->mindepth ) { ++errors; error ( E_ERROR, "mutation: bad depth range.\n" ); } for ( j = 0; j < tree_count; ++j ) md->treetotal += md->tree[j]; if ( md->treetotal == 0.0 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -