TRAIN *TrainCreateNelderMead(SCORE_FUNCTION score_function, void *x0, int position_size, int konvge, int kcount, double reqmin, double stepValue, void *params) { TRAIN *result = NULL; TRAIN_NELDER_MEAD *trainNM = NULL; unsigned int n; trainNM = (TRAIN_NELDER_MEAD*)calloc(1,sizeof(TRAIN_GREEDY)); result = (TRAIN*)trainNM; result->type = TYPE_TRAIN_NELDER_MEAD; result->current_position = (unsigned char*)calloc(position_size,1); result->trial_position = (unsigned char*)calloc(position_size,1); result->best_position = (unsigned char*)calloc(position_size,1); result->score_function = score_function; result->random = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); result->params = params; result->position_size = position_size; result->max_iterations = 0; result->should_minimize = 1; memcpy(result->current_position,x0,position_size); memcpy(result->best_position,x0,position_size); result->best_score = score_function(result->best_position,result); n = result->position_size/sizeof(double); trainNM->konvge = konvge; trainNM->kcount = kcount; trainNM->reqmin = reqmin; trainNM->stepValue = stepValue; trainNM->step = (double*)calloc(n,sizeof(double)); return result; }
TRAIN *TrainCreateAnneal(SCORE_FUNCTION score_function, void *x0, int position_size, double start_temperature, double stop_temperature, unsigned int cycles, unsigned int iterations, void *params) { TRAIN *result = NULL; TRAIN_ANNEAL *trainAnneal = NULL; trainAnneal = (TRAIN_ANNEAL*)calloc(1,sizeof(TRAIN_ANNEAL)); result = (TRAIN*)trainAnneal; result->type = TYPE_TRAIN_ANNEAL; result->current_position = (unsigned char*)calloc(position_size,1); result->trial_position = (unsigned char*)calloc(position_size,1); result->best_position = (unsigned char*)calloc(position_size,1); result->score_function = score_function; result->random = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); result->params = params; result->position_size = position_size; result->max_iterations = iterations; result->should_minimize = 1; memcpy(result->current_position,x0,position_size); memcpy(result->best_position,x0,position_size); result->best_score = score_function(result->best_position,result); trainAnneal->current_temperature = start_temperature; trainAnneal->starting_temperature = start_temperature; trainAnneal->ending_temperature = stop_temperature; trainAnneal->k = 0; trainAnneal->anneal_cooling = AnnealCoolingSchedule; trainAnneal->anneal_probability = AnnealCalcProbability; trainAnneal->anneal_randomize = AnnealDoubleRandomize; trainAnneal->current_score = result->best_score; trainAnneal->cycles = cycles; return result; }
void RandomFill(unsigned char *items) { int i; RANDOM_GENERATE *prng; prng = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); for(i=0;i<NUM_ITEMS_TO_CHOOSE;i++) { items[i] = RandNextDouble(prng)>0.5; } }
void ExampleTest(int argIndex, int argc, char **argv) { RANDOM_GENERATE *prng; int i; prng = RandCreate(TYPE_RANDOM_MT,(unsigned)time(NULL)); for(i=0;i<10;i++) { printf("%ld\n",RandNextInt(prng)); } }
void RBFNetworkReset(RBF_NETWORK *network) { unsigned int i; RANDOM_GENERATE *prng; prng = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); for(i=0;i<network->ltm_size;i++) { network->long_term_memory[i] = RandNextDoubleRange(prng,-1,1); } RandDelete(prng); }
static KNAPSACK_PARAMS *CreateKnapsack() { KNAPSACK_PARAMS *result; int n; result = (KNAPSACK_PARAMS *)calloc(1,sizeof(KNAPSACK_PARAMS)); result->random = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); /* Generate a random set of items. */ for (n = 0; n < NUM_ITEMS_TO_CHOOSE; n++) { result->profit[n] = RandNextIntRange(result->random,0, ITEM_MAX_VALUE); result->weight[n] = RandNextIntRange(result->random,0, ITEM_MAX_WEIGHT); } return result; }
CLUSTER_ALOG *CreateKMeans(int k,int featureCount) { CLUSTER_ALOG *result; int i; result = (CLUSTER_ALOG *)calloc(1,sizeof(CLUSTER_ALOG)); result->k = k; result->featureCount = featureCount; result->clusters = (CLUSTER*)calloc(k,sizeof(CLUSTER)); result->rnd = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); result->dist = &DistanceEuclidean; /* allocate centroids */ for(i=0;i<k;i++) { result->clusters[i].centroid = (double*)calloc(featureCount,sizeof(double)); } return result; }
/* Create an instance of the knapsack problem */ static KNAPSACK_PARAMS *CreateKnapsack() { KNAPSACK_PARAMS *result; int n; /* Create the KNAPSACK_PARAMS struct and a random number generator. */ result = (KNAPSACK_PARAMS *)calloc(1,sizeof(KNAPSACK_PARAMS)); result->random = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); /* Generate a random set of items. Random weights and profits. */ result->max_profit = 0; for (n = 0; n < NUM_ITEMS_TO_CHOOSE; n++) { result->profit[n] = RandNextIntRange(result->random,0, ITEM_MAX_VALUE); result->weight[n] = RandNextIntRange(result->random,0, ITEM_MAX_WEIGHT); result->max_profit+=result->profit[n]; } return result; }
TRAIN *TrainCreateHillClimb(SCORE_FUNCTION score_function, int should_minimize, void *x0, int position_size, double acceleration, double stepSize, void *params) { TRAIN *result = NULL; TRAIN_HILL_CLIMB *trainHill = NULL; int dimensions,i; trainHill = (TRAIN_HILL_CLIMB*)calloc(1,sizeof(TRAIN_HILL_CLIMB)); result = (TRAIN*)trainHill; result->type = TYPE_TRAIN_HILL_CLIMB; result->current_position = (unsigned char*)calloc(position_size,1); result->trial_position = (unsigned char*)calloc(position_size,1); result->best_position = (unsigned char*)calloc(position_size,1); result->score_function = score_function; result->random = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); result->params = params; result->position_size = position_size; result->max_iterations = 0; result->should_minimize = should_minimize; memcpy(result->current_position,x0,position_size); memcpy(result->best_position,x0,position_size); result->best_score = score_function(result->best_position,result); /* Build canidate array */ trainHill->candidate[0] = -acceleration; trainHill->candidate[1] = -1 / acceleration; trainHill->candidate[2] = 0; trainHill->candidate[3] = 1 / acceleration; trainHill->candidate[4] = acceleration; /* Build step sizes */ dimensions = position_size / sizeof(double); trainHill->step_size = (double*)calloc(dimensions,sizeof(double)); for(i=0;i<dimensions;i++) { trainHill->step_size[i] = stepSize; } return result; }
TRAIN *TrainCreateGreedyRandom(SCORE_FUNCTION score_function, int should_minimize, void *x0, int position_size, void *params,double low, double high) { TRAIN *result = NULL; TRAIN_GREEDY *trainGreedy = NULL; trainGreedy = (TRAIN_GREEDY*)calloc(1,sizeof(TRAIN_GREEDY)); result = (TRAIN*)trainGreedy; trainGreedy->low = low; trainGreedy->high = high; result->type = TYPE_TRAIN_GREEDY_RANDOM; result->current_position = (unsigned char*)calloc(position_size,1); result->trial_position = (unsigned char*)calloc(position_size,1); result->best_position = (unsigned char*)calloc(position_size,1); result->score_function = score_function; result->random = RandCreate(TYPE_RANDOM_MT,(long)time(NULL)); result->params = params; result->position_size = position_size; result->max_iterations = 0; result->should_minimize = should_minimize; memcpy(result->current_position,x0,position_size); memcpy(result->best_position,x0,position_size); result->best_score = score_function(result->best_position,result); return result; }