⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ga_core.c

📁 http://gaul.sourceforge.net/ 这里大部分人讨论的是在Matlab里实现GA的toolbox.以上为一个GA的C语言的软件包.如果你想利用GA做优化算法,非常有用.而且有
💻 C
📖 第 1 页 / 共 5 页
字号:
 **********************************************************************/population *ga_population_clone_empty(population *pop)  {  int		i;		/* Loop variable. */  population	*newpop=NULL;	/* New population structure. */  unsigned int	pop_id;		/* Handle for new population structure. */  /* Checks */  if ( !pop ) die("Null pointer to population structure passed.");/* * Allocate new structure. */  newpop = s_malloc(sizeof(population));/* * Clone parameters. */  newpop->size = 0;  newpop->stable_size = pop->stable_size;  newpop->max_size = pop->max_size;  newpop->orig_size = 0;  newpop->num_chromosomes = pop->num_chromosomes;  newpop->len_chromosomes = pop->len_chromosomes;  newpop->data = pop->data;  newpop->free_index = pop->max_size-1;  newpop->crossover_ratio = pop->crossover_ratio;  newpop->mutation_ratio = pop->mutation_ratio;  newpop->migration_ratio = pop->migration_ratio;  newpop->scheme = pop->scheme;  newpop->elitism = pop->elitism;  newpop->allele_mutation_prob = pop->allele_mutation_prob;  newpop->allele_min_integer = newpop->allele_min_integer;  newpop->allele_max_integer = newpop->allele_max_integer;  newpop->allele_min_double = newpop->allele_min_double;  newpop->allele_max_double = newpop->allele_max_double;  THREAD_LOCK_NEW(newpop->lock);#if USE_CHROMO_CHUNKS == 1  THREAD_LOCK_NEW(newpop->chromo_chunk_lock);#endif/* * Clone the callback functions. */  newpop->generation_hook = pop->generation_hook;  newpop->iteration_hook = pop->iteration_hook;  newpop->data_destructor = pop->data_destructor;  newpop->data_ref_incrementor = pop->data_ref_incrementor;  newpop->chromosome_constructor = pop->chromosome_constructor;  newpop->chromosome_destructor = pop->chromosome_destructor;  newpop->chromosome_replicate = pop->chromosome_replicate;  newpop->chromosome_to_bytes = pop->chromosome_to_bytes;  newpop->chromosome_from_bytes = pop->chromosome_from_bytes;  newpop->chromosome_to_string = pop->chromosome_to_string;  newpop->evaluate = pop->evaluate;  newpop->seed = pop->seed;  newpop->adapt = pop->adapt;  newpop->select_one = pop->select_one;  newpop->select_two = pop->select_two;  newpop->mutate = pop->mutate;  newpop->crossover = pop->crossover;  newpop->replace = pop->replace;  newpop->rank = pop->rank;/* * Copy optional parameter data. */  if (pop->tabu_params == NULL)    {    newpop->tabu_params = NULL;    }  else    {    newpop->tabu_params = s_malloc(sizeof(ga_tabu_t));    newpop->tabu_params->tabu_accept = pop->tabu_params->tabu_accept;    newpop->tabu_params->list_length = pop->tabu_params->list_length;    newpop->tabu_params->search_count = pop->tabu_params->search_count;    }  if (pop->sa_params == NULL)    {    newpop->sa_params = NULL;    }  else    {    newpop->sa_params = s_malloc(sizeof(ga_sa_t));    newpop->sa_params->sa_accept = pop->sa_params->sa_accept;    newpop->sa_params->initial_temp = pop->sa_params->initial_temp;    newpop->sa_params->final_temp = pop->sa_params->final_temp;    newpop->sa_params->temp_step = pop->sa_params->temp_step;    newpop->sa_params->temp_freq = pop->sa_params->temp_freq;    newpop->sa_params->temperature = pop->sa_params->temperature;    }  if (pop->climbing_params == NULL)    {    newpop->climbing_params = NULL;    }  else    {    newpop->climbing_params = s_malloc(sizeof(ga_climbing_t));    newpop->climbing_params->mutate_allele = pop->climbing_params->mutate_allele;    }  if (pop->simplex_params == NULL)    {    newpop->simplex_params = NULL;    }  else    {    newpop->simplex_params = s_malloc(sizeof(ga_simplex_t));    newpop->climbing_params->mutate_allele = pop->climbing_params->mutate_allele;    newpop->simplex_params->to_double = pop->simplex_params->to_double;    newpop->simplex_params->from_double = pop->simplex_params->from_double;    newpop->simplex_params->dimensions = pop->simplex_params->dimensions;    }  if (pop->dc_params == NULL)    {    newpop->dc_params = NULL;    }  else    {    newpop->dc_params = s_malloc(sizeof(ga_dc_t));    newpop->dc_params->compare = pop->dc_params->compare;    }  if (pop->gradient_params == NULL)    {    newpop->gradient_params = NULL;    }  else    {    newpop->gradient_params = s_malloc(sizeof(ga_gradient_t));    newpop->gradient_params->to_double = newpop->gradient_params->to_double;    newpop->gradient_params->from_double = newpop->gradient_params->from_double;    newpop->gradient_params->gradient = newpop->gradient_params->gradient;    newpop->gradient_params->step_size = newpop->gradient_params->step_size;    newpop->gradient_params->dimensions = newpop->gradient_params->dimensions;    }  if (pop->search_params == NULL)    {    newpop->search_params = NULL;    }  else    {    newpop->search_params = s_malloc(sizeof(ga_search_t));    newpop->search_params->scan_chromosome = pop->search_params->scan_chromosome;    newpop->search_params->chromosome_state = 0;    newpop->search_params->allele_state = 0;    }  if (newpop->sampling_params == NULL)    {    newpop->sampling_params = NULL;    }  else    {    newpop->sampling_params = NULL;    plog(LOG_FIXME, "Probabilistic sampling parameters not copied.");    }/* * Allocate arrays etc. */  newpop->entity_array = s_malloc(newpop->max_size*sizeof(entity*));  newpop->entity_iarray = s_malloc(newpop->max_size*sizeof(entity*));  newpop->entity_chunk = mem_chunk_new(sizeof(entity), 512);  /* * Wipe the the entity arrays. */  for (i=0; i<newpop->max_size; i++)    {    newpop->entity_array[i] = NULL;    newpop->entity_iarray[i] = NULL;    }/* * Add this new population into the population table. */  THREAD_LOCK(pop_table_lock);  if ( !pop_table ) pop_table=table_new();  pop_id = table_add(pop_table, (vpointer) newpop);  THREAD_UNLOCK(pop_table_lock);  plog( LOG_DEBUG, "New pop = %p id = %d (cloned from %p)",        newpop, pop_id, pop );  return newpop;  }/**********************************************************************  ga_population_clone()  synopsis:	Allocates and initialises a new population structure,		and fills it with an exact copy of the data from an		existing population, including the individual		entity data.  The population's user data		field is referenced.		Entity id's between the populations will _NOT_		correspond.  parameters:	population *	original population structure.  return:	population *	new population structure.  last updated: 24 May 2002 **********************************************************************/population *ga_population_clone(population *pop)  {  int		i;		/* Loop variable. */  population	*newpop=NULL;	/* New population structure. */  entity	*newentity;	/* Used for cloning entities. *//* Note that checks are performed in the ga_population_clone_empty() function. *//* * Clone the population data. */  newpop = ga_population_clone_empty(pop);/* * Clone each of the constituent entities. */#pragma omp parallel for \   shared(pop,newpop) private(i,newentity) \   schedule(static)  for (i=0; i<pop->size; i++)    {    newentity = ga_get_free_entity(newpop);    ga_entity_copy(newpop, newentity, pop->entity_iarray[i]);    }  return newpop;  }/**********************************************************************  ga_get_num_populations()  synopsis:	Gets the number of populations.  parameters:	none  return:	int	number of populations, -1 for undefined table.  last updated: 15 Aug 2002 **********************************************************************/int ga_get_num_populations(void)  {  int	num=-1;  THREAD_LOCK(pop_table_lock);  if (pop_table)    {    num = table_count_items(pop_table);    }  THREAD_UNLOCK(pop_table_lock);  return num;  }/**********************************************************************  ga_get_population_from_id()  synopsis:	Get population pointer from its internal id.  parameters:	unsigned int	id for population.  return:	int  last updated: 15 Aug 2002 **********************************************************************/population *ga_get_population_from_id(unsigned int id)  {  population	*pop=NULL;	/* The population pointer to return. */  THREAD_LOCK(pop_table_lock);  if (pop_table)    {    pop = (population *) table_get_data(pop_table, id);    }  THREAD_UNLOCK(pop_table_lock);  return pop;  }/**********************************************************************  ga_get_population_id()  synopsis:	Get population's internal id from its pointer.  parameters:	population	population pointer to lookup.  return:	unsigned int	internal id for population (or -1 for no match).  last updated: 15 Aug 2002 **********************************************************************/unsigned int ga_get_population_id(population *pop)  {  unsigned int	id=TABLE_ERROR_INDEX;	/* Internal population id. */  THREAD_LOCK(pop_table_lock);  if (pop_table && pop)    {    id = table_lookup_index(pop_table, (vpointer) pop);    }  THREAD_UNLOCK(pop_table_lock);  return id;  }/**********************************************************************  ga_get_all_population_ids()  synopsis:	Get array of internal ids for all currently		allocated populations.  The returned array needs to		be deallocated by the caller.  parameters:	none  return:	unsigned int*	array of population ids (or NULL)  last updated: 15 Aug 2002 **********************************************************************/unsigned int *ga_get_all_population_ids(void)  {  unsigned int	*ids=NULL;	/* Array of ids. */  THREAD_LOCK(pop_table_lock);  if (pop_table)    {    ids = table_get_index_all(pop_table);    }  THREAD_UNLOCK(pop_table_lock);  return ids;  }/**********************************************************************  ga_get_all_populations()  synopsis:	Get array of all currently allocated populations.  The		returned array needs to be deallocated by the caller.  parameters:	none  return:	population**	array of population pointers  last updated: 15 Aug 2002 **********************************************************************/population **ga_get_all_populations(void)  {  population	**pops=NULL;	/* Array of all population pointers. */  THREAD_LOCK(pop_table_lock);  if (pop_table)    {    pops = (population **) table_get_data_all(pop_table);    }  THREAD_UNLOCK(pop_table_lock);  return pops;  }/**********************************************************************  ga_entity_seed()  synopsis:	Fills a population structure with genes.  Defined in		a user-specified function.  parameters:	population *	The entity's population.		entity *	The entity.  return:	boolean success.  last updated:	28/02/01 **********************************************************************/boolean ga_entity_seed(population *pop, entity *adam)  {  if ( !pop ) die("Null pointer to population structure passed.");  if ( !pop->seed ) die("Population seeding function is not defined.");  return pop->seed(pop, adam);  }/**********************************************************************  gaul_population_fill()  synopsis:	Fills all entities in a population structure with		genes from a user-specified function.  parameters:	population *pop		int num			Number of entities to seed.  return:	boolean success.  last updated: 17 Feb 2005 **********************************************************************/boolean gaul_population_fill(population *pop, int num)  {  int		i;		/* Loop variables. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -