Vector vector_random_area(Seed * seed, Vector const corners[2][2]) { float u = random_seed(seed); float v = random_seed(seed); return vector_interpolate_bilinear(corners, u, v); }
int main () { random_t* random1Ptr; random_t* random2Ptr; random_t* random3Ptr; long i; puts("Starting..."); random1Ptr = random_alloc(); random2Ptr = random_alloc(); random3Ptr = random_alloc(); random_seed(random2Ptr, (RANDOM_DEFAULT_SEED + 1)); random_seed(random3Ptr, (RANDOM_DEFAULT_SEED + 1)); for (i = 0; i < NUM_ITERATIONS; i++) { unsigned long rand1 = random_generate(random1Ptr); unsigned long rand2 = random_generate(random2Ptr); unsigned long rand3 = random_generate(random3Ptr); printf("i = %2li, rand1 = %12lu, rand2 = %12lu, rand2 = %12lu\n", i, rand1, rand2, rand3); assert(rand1 != rand2); assert(rand2 == rand3); } random_free(random1Ptr); random_free(random2Ptr); puts("Done."); return 0; }
static void tester (long geneLength, long segmentLength, long minNumSegment, bool_t doPrint) { gene_t* genePtr; segments_t* segmentsPtr; random_t* randomPtr; bitmap_t* startBitmapPtr; long i; long j; genePtr = gene_alloc(geneLength); segmentsPtr = segments_alloc(segmentLength, minNumSegment); randomPtr = random_alloc(); startBitmapPtr = bitmap_alloc(geneLength); random_seed(randomPtr, 0); gene_create(genePtr, randomPtr); random_seed(randomPtr, 0); segments_create(segmentsPtr, genePtr, randomPtr); assert(segmentsPtr->minNum == minNumSegment); assert(vector_getSize(segmentsPtr->contentsPtr) >= minNumSegment); if (doPrint) { printf("Gene = %s\n", genePtr->contents); } /* Check that each segment occurs in gene */ for (i = 0; i < vector_getSize(segmentsPtr->contentsPtr); i++) { char *charPtr = strstr(genePtr->contents, (char*)vector_at(segmentsPtr->contentsPtr, i)); assert(charPtr != NULL); j = charPtr - genePtr->contents; bitmap_set(startBitmapPtr, j); if (doPrint) { printf("Segment %li (@%li) = %s\n", i, j, (char*)vector_at(segmentsPtr->contentsPtr, i)); } } /* Check that there is complete overlap */ assert(bitmap_isSet(startBitmapPtr, 0)); for (i = 0, j = 0; i < geneLength; i++ ) { if (bitmap_isSet(startBitmapPtr, i)) { assert((i-j-1) < segmentLength); j = i; } } gene_free(genePtr); segments_free(segmentsPtr); random_free(randomPtr); bitmap_free(startBitmapPtr); }
void rgb_timing(Test **test, Rgb_Timing *timing) { double total_time,avg_time; int i,j; unsigned int *rand_uint; MYDEBUG(D_RGB_TIMING){ printf("# Entering rgb_timing(): ps = %u ts = %u\n",test[0]->psamples,test[0]->tsamples); } seed = random_seed(); gsl_rng_set(rng,seed); rand_uint = (uint *)malloc((size_t)test[0]->tsamples*sizeof(uint)); total_time = 0.0; for(i=0;i<test[0]->psamples;i++){ start_timing(); for(j=0;j<test[0]->tsamples;j++){ rand_uint[j] = gsl_rng_get(rng); } stop_timing(); total_time += delta_timing(); } avg_time = total_time/(test[0]->psamples*test[0]->tsamples); timing->avg_time_nsec = avg_time*1.0e+9; timing->rands_per_sec = 1.0/avg_time; free(rand_uint); }
/********************************************************************** * Function: EventLoop * Description: this is the main event loop for the program. It * listens for events for .5 seconds, if none come in, it checks * wheter it should timeout and lock the program. If the unit * is scheduled to sleep within the next five seconds, it locks * the display. * Note: timeout does not work properly if you overclock you * palm's processor. * ********************************************************************/ static void EventLoop (void) { EventType event; UInt16 error; do { // wait a bit for an event EvtGetEvent (&event, 50); /* Dont seed with empty events!*/ if(event.eType != nilEvent) random_seed((byte *) &event, sizeof(event)); // first the system gets the event, then the Menu event handler, then the application // event handler, then finally the form event handler if (!SysHandleEvent (&event)) if (!MenuHandleEvent (0, &event, &error)) if (!ApplicationHandleEvent (&event)) FrmDispatchEvent (&event); } while (event.eType != appStopEvent); }
void cHamiltonianMatrix::randomPotential(gsl_vector* randV){ const gsl_rng_type * T; gsl_rng * r; gsl_rng_env_setup(); T = gsl_rng_default; r = gsl_rng_alloc (T); // gsl_rng_env_setup() is getting a generator type and seed from environment variables. // http://stackoverflow.com/questions/9768519/gsl-uniform-random-number-generator // dev/random solution is very time consuming compared to the gettimeofday(), // the gettimeofday() solution, might be better its level of accuracy is enough: unsigned long int seed_number; if (rank == 0) { // seed_number = 0; // debug purpose. seed_number = random_seed(); // cout << "Before broadcasting, seed number is " << seed_number << endl; } MPI_Bcast(&seed_number, 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD); //replace MPI_INT by MPI_UNSIGNED_LONG if int is not long enough. // cout << "After broadcasting, rank " << rank << " has seed number " << seed_number << endl; gsl_rng_set(r,seed_number); // gsl_rng_set(r,random_seed_devrandom()); for (int i = 0; i < L; i++) { gsl_vector_set(randV, i, -W/2+W*gsl_rng_uniform (r)); // gsl_vector_set(randV,i,1+i-double(L)/2.0);// non-disordered potential for benchmark } gsl_rng_free (r); }
void random_init(void) { // Initialize the memory of the random variables memset(&random_vars, 0, sizeof(random_vars_t)); // Initialize the shift register random_vars.shift_reg = random_seed(); }
int init(int nprt,int ndim,double *box, double **pxyz,double **pvel,double **pacc) { /* allocate & initialize particles */ int i,k; double x; double *xyz=NULL; double *vel=NULL; double *acc=NULL; /* allocate */ xyz=(double*)malloc(ndim*nprt*sizeof(double)); vel=(double*)malloc(ndim*nprt*sizeof(double)); acc=(double*)malloc(ndim*nprt*sizeof(double)); if( !xyz || !vel || !acc ) return 1; /* init */ random_seed(1); for(i=0; i<nprt; i++) { for(k=0; k<ndim; k++) { x=random_number(); xyz[i*ndim+k]=x * (box[2*k+1]-box[2*k]) + box[2*k]; } } /* update pointers */ *(pxyz)=xyz; *(pvel)=vel; *(pacc)=acc; return 0; }
void main() { video_mode = 9; max_entities = 30000; terrain_chunk = 0; level_load(NULL); random_seed(0); mouse_mode = 4; vec_set(camera.x, vector(-1101, -97, 800)); vec_set(camera.pan, vector(4,-29,0)); // Terrain BMAP* bmapHeightMap = generate_random_heightmap_noise(256, 256, 64); BMAP* bmapColorMap = heightmap_to_colormap(bmapHeightMap); ENTITY* entTerrain = terrain_from_heightmap(vector(0,0,-300), bmapHeightMap, 65, 65, 30, 0.4); ent_setskin(entTerrain, bmapColorMap, 2); entTerrain.material = mat_terrain_multi_texture; // Roads proc_city_create_skins(); List *points = roadnetwork_from_rectlangle(entTerrain.min_x + 100, entTerrain.min_y + 100, entTerrain.max_x - 50, entTerrain.max_y - 50, 200, 6); //List *points = roadnetwork_from_voronoi(30, entTerrain.min_x + 100, entTerrain.min_y + 100, entTerrain.max_x - 50, entTerrain.max_y - 50); List *intersections = roadnetwork_calculate(points); roadnetwork_join_near_intersections(intersections, 100); // Delete intersections which are too near to each other List *roadNetwork = roadnetwork_build(intersections, 300, true); // Parcels create_parcels(roadNetwork); }
int main(int argc,char *argv[]) { double x,y,pi; int n_trials,i_trial,n_hits; if (argc != 3) { fprintf(stderr,"%s <N trials> <seed>\n",argv[0]); exit(1); } n_trials = atoi(argv[1]); random_seed(atoi(argv[2])); i_trial = 0; n_hits = 0; while (i_trial < n_trials) { /* Add statements here to generate two random coordinates within the square in the double precision variables x and y, and increment the integer variable n_hits if x and y represent a point within the unit circle Hint: save computation time by NOT calling the sqrt() function! */ x = 2.0*(random_gen() - 0.5); y = 2.0*(random_gen() - 0.5); if (x*x + y*y <= 1) n_hits++; i_trial++; } pi = 4.0 * ((double) n_hits) / ((double) n_trials); printf("%.8f\n",pi); exit(0); }
/* ============================================================================= * client_alloc * -- Returns NULL on failure * ============================================================================= */ client_t* client_alloc (long id, manager_t* managerPtr, long numOperation, long numQueryPerTransaction, long queryRange, long percentUser) { client_t* clientPtr; clientPtr = (client_t*)malloc(sizeof(client_t)); if (clientPtr == NULL) { return NULL; } clientPtr->randomPtr = random_alloc(); if (clientPtr->randomPtr == NULL) { return NULL; } clientPtr->id = id; clientPtr->managerPtr = managerPtr; random_seed(clientPtr->randomPtr, id); clientPtr->numOperation = numOperation; clientPtr->numQueryPerTransaction = numQueryPerTransaction; clientPtr->queryRange = queryRange; clientPtr->percentUser = percentUser; return clientPtr; }
void ebox_init(void) { get_system_clock(&cpu.clock); get_chip_info(); cpu.company[0] = 'S'; cpu.company[1] = 'T'; cpu.company[2] = '\0'; SysTick_Config(cpu.clock.core/1000);// 每隔 1ms产生一次中断 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);//systemticks clock; micro_para = cpu.clock.core/1000000;//减少micros函数计算量 //统计cpu计算能力////////////////// cpu.ability = 0; millis_seconds = 0; do { cpu.ability++;//统计cpu计算能力 } while(millis_seconds < 100); cpu.ability = cpu.ability * 10; //////////////////////////////// ADC1_init(); NVIC_PriorityGroupConfig(NVIC_GROUP_CONFIG); //将pb4默认设置为IO口,禁用jtag RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); set_systick_user_event_per_sec(1000); random_seed(AD_value[0]);//初始化随机数种子 }
int main () { gene_t* gene1Ptr; gene_t* gene2Ptr; gene_t* gene3Ptr; random_t* randomPtr; bool_t status = memory_init(1, 4, 2); assert(status); puts("Starting..."); gene1Ptr = gene_alloc(10); gene2Ptr = gene_alloc(10); gene3Ptr = gene_alloc(9); randomPtr = random_alloc(); random_seed(randomPtr, 0); gene_create(gene1Ptr, randomPtr); random_seed(randomPtr, 1); gene_create(gene2Ptr, randomPtr); random_seed(randomPtr, 0); gene_create(gene3Ptr, randomPtr); assert(gene1Ptr->length == strlen(gene1Ptr->contents)); assert(gene2Ptr->length == strlen(gene2Ptr->contents)); assert(gene3Ptr->length == strlen(gene3Ptr->contents)); assert(gene1Ptr->length == gene2Ptr->length); assert(strcmp(gene1Ptr->contents, gene2Ptr->contents) != 0); assert(gene1Ptr->length == (gene3Ptr->length + 1)); assert(strcmp(gene1Ptr->contents, gene3Ptr->contents) != 0); assert(strncmp(gene1Ptr->contents, gene3Ptr->contents, gene3Ptr->length) == 0); gene_free(gene1Ptr); gene_free(gene2Ptr); gene_free(gene3Ptr); random_free(randomPtr); puts("All tests passed."); return 0; }
int main(int argc, char **argv) { int i; /* Runs. */ population *pop=NULL; /* Population of solutions. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ for (i=0; i<50; i++) { if (pop) ga_extinction(pop); random_seed(424242*i); pop = ga_genesis_integer( 50, /* const int population_size */ 1, /* const int num_chromo */ 25, /* const int len_chromo */ NULL, /*pingpong_ga_callback,*/ /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ pingpong_score, /* GAevaluate evaluate */ pingpong_seed, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_randomrank, /* GAselect_one select_one */ ga_select_two_randomrank, /* GAselect_two select_two */ pingpong_mutate, /* GAmutate mutate */ pingpong_crossover, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_SURVIVE, /* const ga_elitism_type elitism */ 0.5, /* double crossover */ 0.5, /* double mutation */ 0.0 /* double migration */ ); ga_evolution( pop, /* population *pop */ 200 /* const int max_generations */ ); pingpong_ga_callback(i, pop); } printf("The final solution found was:\n"); beststring = ga_chromosome_integer_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); ga_extinction(pop); s_free(beststring); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { population *pop=NULL; /* Population structure. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ random_seed(23091975); pop = ga_genesis_char( 120, /* const int population_size */ 1, /* const int num_chromo */ (int) strlen(target_text), /* const int len_chromo */ struggle_generation_hook, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ ga_seed_printable_random, /* GAseed seed */ struggle_adaptation, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ ga_crossover_char_allele_mixing, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_LAMARCK_CHILDREN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */ 0.8, /* const double crossover */ 0.05, /* const double mutation */ 0.0 /* const double migration */ ); if ( ga_evolution( pop, 1000 )<1000 ) { printf("The evolution was stopped because the termination criteria were met.\n" ); } else { printf("The evolution was stopped because the maximum number of generations were performed.\n" ); } printf( "The final solution with score %f was:\n", ga_get_entity_from_rank(pop,0)->fitness ); beststring = ga_chromosome_char_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); printf( "Total number of fitness evaluations: %ld\n", evaluation_count ); ga_extinction(pop); s_free(beststring); exit(EXIT_SUCCESS); }
/* fill random length array with random data */ void sort_rnd_data_fill(int *data, int length, int min, int max) { int i = 0; random_seed(); while(i < length) { data[i]= random_int(min, max); i++; } }
Vector vector_random_hemi_direction_2(Seed * seed, Vector n) { float u = 0; while (u < 0.001) u = random_seed(seed); float v = random_seed(seed); float r = sqrt(1 - u * u); float phi = 2 * M_PI * v; Vector w = vector_polar(phi, r); w.z = u; Vector x = vector_perpendicular(n); Vector y = cross(n, x); Vector z = n; //return vector_transform(x, y, z, w); return vector_normalize(vector_transform(x, y, z, w)); }
Vector vector_random_hemi_direction_cos(Seed * seed, Vector n) { float u = random_seed(seed); float v1 = random_seed(seed); float r = sqrt(u); float phi = 2 * M_PI * v1; Vector v = vector_polar(phi, r); Vector w = {v.x, v.y, sqrt(fmax(0, 1 - v.x * v.x - v.y * v.y))}; Vector x = vector_perpendicular(n); Vector y = cross(n, x); Vector z = n; //return vector_transform(x, y, z, w); return vector_normalize(vector_transform(x, y, z, w)); }
int main(int argc, char **argv) { int i; /* Loop over runs. */ population *pop=NULL; /* Population of solutions. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ for (i=0; i<50; i++) { random_seed(i); pop = ga_genesis_char( 120, /* const int population_size */ 1, /* const int num_chromo */ (int) strlen(target_text), /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ ga_seed_printable_random, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ ga_crossover_char_allele_mixing, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */ 0.9, /* double crossover */ 0.2, /* double mutation */ 0.0 /* double migration */ ); ga_evolution_threaded( pop, /* population *pop */ 500 /* const int max_generations */ ); printf( "The final solution with seed = %d was:\n", i ); beststring = ga_chromosome_char_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); printf( "With score = %f\n", ga_entity_get_fitness(ga_get_entity_from_rank(pop,0)) ); ga_extinction(pop); } s_free(beststring); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { population *pop=NULL; /* Population of solutions. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ random_seed(20092004); pop = ga_genesis_integer( 200, /* const int population_size */ 1, /* const int num_chromo */ 100, /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ all5s_score, /* GAevaluate evaluate */ ga_seed_integer_random, /* GAseed seed */ NULL, /* GAadapt adapt */ ga_select_one_sus, /* GAselect_one select_one */ ga_select_two_sus, /* GAselect_two select_two */ ga_mutate_integer_singlepoint_drift, /* GAmutate mutate */ ga_crossover_integer_singlepoints, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_allele_min_integer(pop, 0); ga_population_set_allele_max_integer(pop, 10); ga_population_set_parameters( pop, /* population *pop */ GA_SCHEME_DARWIN, /* const ga_scheme_type scheme */ GA_ELITISM_PARENTS_SURVIVE, /* const ga_elitism_type elitism */ 0.8, /* double crossover */ 0.05, /* double mutation */ 0.0 /* double migration */ ); ga_evolution( pop, /* population *pop */ 250 /* const int max_generations */ ); /* Display final solution. */ printf("The final solution was:\n"); beststring = ga_chromosome_integer_to_string(pop, ga_get_entity_from_rank(pop,0), beststring, &beststrlen); printf("%s\n", beststring); printf("With score = %f\n", ga_get_entity_from_rank(pop,0)->fitness); /* Free memory. */ ga_extinction(pop); s_free(beststring); exit(EXIT_SUCCESS); }
GAULFUNC void random_init(void) { random_seed(1); #if RANDOM_DEBUG>0 printf("DEBUG: Random number routines initialised.\n"); #endif return; }
static void op_poke_seed_i16(const void *data, scene_state_t *ss, exec_state_t *NOTUSED(es), command_state_t *cs) { int16_t s = cs_pop(cs); char *base = (char *)ss; size_t offset = (size_t)data; tele_rand_t *ptr = (tele_rand_t *)(base + offset); ptr->seed = s; random_seed(&ptr->rand, ptr->seed); }
static void op_SEED_set(const void *NOTUSED(data), scene_state_t *ss, exec_state_t *NOTUSED(es), command_state_t *cs) { uint16_t s = cs_pop(cs); for (u8 i = 0; i < RAND_STATES_COUNT; i++) { tele_rand_t *r = &ss->rand_states.a[i]; r->seed = s; random_seed(&r->rand, r->seed); } ss->variables.seed = s; }
/* ============================================================================= * threadWait * -- Synchronizes all threads to start/stop parallel section * ============================================================================= */ static void threadWait (void* argPtr) { thread_args_t* args = (thread_args_t*) argPtr; long threadId = args->threadId; commits = &(args->commits); aborts = &(args->aborts); retriesProf = &(args->retries); ucbProf = &(args->ucb); int sz = 100; memoized_blocks = (memoized_choices_t*) malloc(sz * sizeof(memoized_choices_t)); for (sz--; sz >= 0; sz-- ) { memoized_choices_t* block = &(memoized_blocks[sz]); block->runs = 0; block->havingCapacityAborts = 0; block->retries = 0; block->commitsHTM = 1; block->believedCapacity = 1; block->believedTransient = 1; block->believedGiveUp = 1; block->abortsCapacity = 0; block->abortsTransient = 0; block->cyclesCapacity = 100; block->cyclesTransient = 100; block->cyclesGiveUp = 100; block->retries = 5; block->lastCycles = 0; block->lastRetries = 5; block->bestEverCycles = 0; block->bestEverRetries = 5; } randomFallback = random_alloc(); random_seed(randomFallback, time(NULL)); THREAD_LOCAL_SET(global_threadId, (long)threadId); bindThread(threadId); while (1) { THREAD_BARRIER(global_barrierPtr, threadId); /* wait for start parallel */ if (global_doShutdown) { break; } global_funcPtr(global_argPtr); THREAD_BARRIER(global_barrierPtr, threadId); /* wait for end parallel */ if (threadId == 0) { break; } } }
/** * @brief Initialize global hash move data. */ void hash_move_init(void) { int i, j; Random r; random_seed(&r, 0x5DEECE66Dull); for (i = 0; i < 64; ++i) for (j = 0; j < 60; ++j) { do { hash_move[i][j] = random_get(&r); } while (bit_count(hash_move[i][j]) < 8); } }
/** * @brief Initialize global hash code data. */ void hash_code_init(void) { int i, j; Random r; random_seed(&r, 0x5DEECE66Dull); for (i = 0; i < 16; ++i) for (j = 0; j < 256; ++j) { do { hash_rank[i][j] = random_get(&r); } while (bit_count(hash_rank[i][j]) < 8); } }
int main(int argc, char **argv) { population *pop; /* Population of solutions. */ entity *solution; /* Optimised solution. */ random_seed(23091975); pop = ga_genesis_double( 50, /* const int population_size */ 1, /* const int num_chromo */ 4, /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ test_iteration_callback, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ test_score, /* GAevaluate evaluate */ test_seed, /* GAseed seed */ NULL, /* GAadapt adapt */ NULL, /* GAselect_one select_one */ NULL, /* GAselect_two select_two */ ga_mutate_double_singlepoint_drift, /* GAmutate mutate */ NULL, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_simplex_parameters( pop, /* population *pop */ 4, /* const int num_dimensions */ 0.5, /* const double Initial step size. */ test_to_double, /* const GAto_double to_double */ test_from_double /* const GAfrom_double from_double */ ); /* Evaluate and sort the initial population members (i.e. select best of 50 random solutions. */ ga_population_score_and_sort(pop); /* Use the best population member. */ solution = ga_get_entity_from_rank(pop, 0); ga_simplex( pop, /* population *pop */ solution, /* entity *solution */ 10000 /* const int max_iterations */ ); ga_extinction(pop); exit(EXIT_SUCCESS); }
Vector vector_random_disk(Seed * seed) { float u = random_seed(seed) * 2 - 1; float v = random_seed(seed) * 2 - 1; float r, phi; if (u >= -v) { if (u > v) { r = u; if (v > 0) phi = v/r; else phi = 8.0 + v/r; } else { r = v; phi = 2.0 - u/r; } } else { if (u <= v) { r = -u; phi = 4.0 - v/r; } else { r = -v; phi = 6.0 + u/r; } } phi *= M_PI / 4.0; Vector w = {r * cos(phi), r * sin(phi), 0}; return w; }
int main(int argc, char **argv) { population *pop=NULL; /* Population of solutions. */ char *beststring=NULL; /* Human readable form of best solution. */ size_t beststrlen=0; /* Length of beststring. */ entity *solution; /* Solution to problem. */ int num_iterations; /* Number of iterations required. */ random_seed(23091975); pop = ga_genesis_char( 100, /* const int population_size */ 1, /* const int num_chromo */ strlen(target_text), /* const int len_chromo */ NULL, /* GAgeneration_hook generation_hook */ NULL, /* GAiteration_hook iteration_hook */ NULL, /* GAdata_destructor data_destructor */ NULL, /* GAdata_ref_incrementor data_ref_incrementor */ struggle_score, /* GAevaluate evaluate */ struggle_seed, /* GAseed seed */ NULL, /* GAadapt adapt */ NULL, /* GAselect_one select_one */ NULL, /* GAselect_two select_two */ NULL, /* GAmutate mutate */ NULL, /* GAcrossover crossover */ NULL, /* GAreplace replace */ NULL /* vpointer User data */ ); ga_population_set_search_parameters(pop, struggle_scan_chromosome); solution = ga_get_free_entity(pop); num_iterations = ga_search( pop, /* population *pop */ solution /* entity *entity */ ); printf( "The final solution was:\n"); beststring = ga_chromosome_char_to_string(pop, solution, beststring, &beststrlen); printf("%s\n", beststring); printf( "With score = %f\n", ga_entity_get_fitness(solution) ); printf( "This required %d iterations\n", num_iterations); ga_extinction(pop); s_free(beststring); exit(EXIT_SUCCESS); }
void LIBINFONAME(lib_init)(unsigned char loglv,unsigned long rs0,size_t nthread) { unsigned long rs; size_t nth; LOGLV(loglv); random_init(); rs=rs0?rs0:(unsigned long)time(NULL); random_seed(rs); if(nthread) omp_set_num_threads((int)nthread); omp_set_nested(0); nth=(size_t)omp_get_max_threads(); gsl_set_error_handler_off(); LOG(7,"Library started with log level %u, initial random seed %lu, and max thread count "PRINTFSIZET".",loglv,rs,nth) }