CUDAANNMP::CUDAANNMP(int nbLayers_ , int * neuronPerLayer_ ,int nbData_, double **dataIn_, double **dataOut_, int maxPop_, double mutRate_, double crossRate_, double *wRange_,bool printBestChromosome_, int nbThreads_, unsigned int seed) :GA(){ initGA(nbLayers_, neuronPerLayer_, nbData_, dataIn_, dataOut_, maxPop_, mutRate_, crossRate_, wRange_, printBestChromosome_, nbThreads_, seed); init( nbLayers_,neuronPerLayer_,maxPop_, mutRate_, crossRate_, nbData_, dataIn_, dataOut_, wRange_ ); }
/* ******************** * SRCP commands */ int setGA(bus_t busnumber, int addr, ga_state_t a) { int number_ga = get_number_ga(busnumber); if ((addr > 0) && (addr <= number_ga)) { char msg[1000]; if (!isInitializedGA(busnumber, addr)) initGA(busnumber, addr, 'P', 0); ga[busnumber].gastate[addr].id = a.id; ga[busnumber].gastate[addr].action = a.action; ga[busnumber].gastate[addr].port = a.port; gettimeofday(&ga[busnumber].gastate[addr]. tv[ga[busnumber].gastate[addr].port], NULL); infoGA(busnumber, addr, a.port, msg); enqueueInfoMessage(msg); return SRCP_OK; } else { return SRCP_NODATA; } }
int main(){ //printf(" *** Real time multiprocessor allocator***\n"); int i; int maxGen = MAXGEN+1; int nrGen = 0; // Number of generations ga * ga = (struct ga *) malloc(sizeof(struct ga)); FILE * fitnessFile; FILE * chromosomeFile; remove("chromosome0mut0crossElitism.txt"); remove("fitness0mut0crossElitism.txt"); fitnessFile = fopen("fitness0mut0crossElitism.txt", "w"); chromosomeFile = fopen("chromosome0mut0crossElitism.txt", "w"); fprintf(fitnessFile, "%s", "Number of generations, Best Machine, Best fitness, Average fitness \n"); fprintf(chromosomeFile, "%s", "Machine number, TDF, PHYSICAL CORE id, U, Vcore id, Slice, Period, Pcore\n"); srand(time(NULL)); //printf("Initializing GA\n"); initGA(ga); //printf("Evaluating fitness\n"); evaluateFitness(ga); fprintf(fitnessFile, "%d, %d, %f, %f\n", nrGen, ga->bestMachineIndex,ga->bestFitness, ga->avgFitness); fprintf(chromosomeFile, "%s", "Generation 0\n"); for (i = 0; i < ga->populationSize; i++) printChromosome(&(ga->population[i]), chromosomeFile); nrGen++; //for (i = 0; i < ga->populationSize; i++) // printMachineParameters(&(ga->population[i])); //while(nrGen < maxGen && ga->bestFitness<1.0 ){ while(nrGen < maxGen && ga->bestFitness<1.0 ){ printf("Selection for gen %d ...\n", nrGen); selection(ga); printf("Mutation for gen %d ...\n", nrGen); mutation(ga); printf("Crossover for gen %d ...\n", nrGen); crossover(ga); printf("Evaluating fitness for gen %d ...\n", nrGen); evaluateFitness(ga); fprintf(fitnessFile, "%d, %d, %f, %f\n", nrGen, ga->bestMachineIndex, ga->bestFitness, ga->avgFitness); fprintf(chromosomeFile, "Generation %d", nrGen); for (i = 0; i < ga->populationSize; i++) printChromosome(&(ga->population[i]), chromosomeFile); /* printf("Number of generations %d, ", nrGen); printf("Best fitness: %f, ", ga->bestFitness); printf("Average fitness: %f \n", ga->avgFitness); //for (i = 0; i < ga->populationSize; i++) // printMachineParameters(&(ga->population[i])); */ nrGen++; } // end while fclose(chromosomeFile); fclose(fitnessFile); freeGA(ga); return 0; }
/************************************************************************** * The main function. **************************************************************************/ int main(int argc, char** argv) { uint i = 0; float timeSum = 0.0; /* used to maintain state of the genetic algorithm between iterations */ GenAlgState gaState; /* for timing purposes */ struct PcaCArrayFloat timing; float* m_timing; pca_timer_t timer; /* the filenames needed for input/output */ char *m_paramfile, *m_scorefile, *m_elitefile, *m_timingfile; /* check arguments */ if (argc != 2) { fprintf(stderr, "Usage: %s [dataset id]\n", argv[0]); exit(-1); } /* generate needed filenames */ m_paramfile = (char*) malloc(strlen("data/") + strlen(argv[1]) + strlen("-genalg-param.dat") + 1); sprintf(m_paramfile, "data/%s-genalg-param.dat", argv[1]); m_scorefile = (char*) malloc(strlen("data/") + strlen(argv[1]) + strlen("-genalg-score.dat") + 1); sprintf(m_scorefile, "data/%s-genalg-score.dat", argv[1]); m_elitefile = (char*) malloc(strlen("data/") + strlen(argv[1]) + strlen("-genalg-elite.dat") + 1); sprintf(m_elitefile, "data/%s-genalg-elite.dat", argv[1]); m_timingfile = (char*) malloc(strlen("data/") + strlen(argv[1]) + strlen("-genalg-timing.dat") + 1); sprintf(m_timingfile, "data/%s-genalg-timing.dat", argv[1]); #ifdef VERBOSE /* print informative message */ printf("[Running dataset %s]\n", argv[1]); #endif /* initialize seed */ hpec_srand(SEED); /* load data */ initGA(&gaState, m_paramfile, m_scorefile); /* run the genetic algorithm a specified number of times */ m_timing = (float*) malloc(sizeof(float) * gaState.max_gen); while (gaState.num_gen < gaState.max_gen && gaState.num_elite < gaState.max_elite) { /* run one generation of the genetic algorithm */ timer = startTimer(); genalg(&gaState); /* increment variables */ gaState.num_gen++; gaState.num_elite = !(gaState.best_idx) ? gaState.num_elite+1 : 0; /* timing */ m_timing[gaState.num_gen - 1] = stopTimer(timer); timeSum += m_timing[gaState.num_gen - 1]; } /* done; output the elite chromosome */ outputEliteChromosome(m_elitefile, &gaState); #ifdef VERBOSE printf("DONE: Generations = %d, elite score = %f, elite count = %d\n", gaState.num_gen, gaState.max_score, gaState.num_elite); #endif printf("Done. Latency: %f s.\n", timeSum/((float)gaState.num_gen)); /* output timing */ pca_create_carray_1d(float, timing, gaState.num_gen, PCA_REAL); for (i = 0; i < gaState.num_gen; i++) timing.data[i] = m_timing[i]; writeToFile(float, m_timingfile, timing); /* clean up */ cleanupGA(&gaState); clean_mem(float, timing); free(m_timing); free(m_paramfile); free(m_scorefile); free(m_elitefile); free(m_timingfile); return 0; }
JNIEXPORT jint JNICALL Java_Server_runGA ( JNIEnv * env, jobject object, jintArray subjectlist, jintArray teacherlist, jint popsize, jint ngen, jdouble pmut, jdouble pcross, jdoubleArray best_fitness, jintArray teacher_ids, jintArray room_ids, jintArray class_ids, jintArray subject_ids){ jint *sl = (*env)->GetIntArrayElements(env, subjectlist, 0); jint *tl = (*env)->GetIntArrayElements(env, teacherlist, 0); jdouble *bstfit = (*env)->GetDoubleArrayElements(env, best_fitness, 0); jint *t_id = (*env)->GetIntArrayElements(env, teacher_ids, 0); jint *r_id = (*env)->GetIntArrayElements(env, room_ids, 0); jint *c_id = (*env)->GetIntArrayElements(env, class_ids, 0); jint *s_id = (*env)->GetIntArrayElements(env, subject_ids, 0); srand(time(0)); struct TimetableGA ga; int i,j; initSubjectList(&subject_list); for(i = 0; i < SUBJECTS; ++i){ subject_list.hours[i] = sl[i]; } initTeacherList(&teacher_list); for(i = 0; i < TEACHERS; ++i){ for(j = 0; j < MAX_SUBJECTS_PER_TEACHER; ++j){ teacher_list.subjects[i][j] = tl[i*MAX_SUBJECTS_PER_TEACHER + j]; } } initClassList(&class_list); initGA(&ga, popsize,ngen,pcross,pmut); setSelection(&ga, &tournamentSelection); setCrossover(&ga, &singlePointCrossover); int gen; double prevfit=0,nextfit=0; for(gen = 0; gen < ga.ngen; ++gen){ nextGen(&ga); nextfit = ga.population[0].fitness; if(nextfit == MAX_FIT) break; prevfit = nextfit; } fitness(&(ga.population[0])); *bstfit = ga.population[0].fitness; for(i = 0; i < DAYS*PERIODS_PER_DAY; ++i){ for(j = 0; j < CLASSROOMS; ++j){ t_id[i*CLASSROOMS + j] = ga.population[0].genotype[i][j].teacher_id; r_id[i*CLASSROOMS + j] = ga.population[0].genotype[i][j].room_id; c_id[i*CLASSROOMS + j] = ga.population[0].genotype[i][j].class_id; s_id[i*CLASSROOMS + j] = ga.population[0].genotype[i][j].subject_id; } } finalizeGA(&ga); freeClassList(&class_list); freesl(&subject_list); freetl(&teacher_list); (*env)->ReleaseIntArrayElements( env, subjectlist, sl,0); (*env)->ReleaseIntArrayElements(env, teacherlist, tl, 0); (*env)->ReleaseDoubleArrayElements(env, best_fitness, bstfit, 0); (*env)->ReleaseIntArrayElements(env, teacher_ids, t_id, 0); (*env)->ReleaseIntArrayElements(env, room_ids, r_id, 0); (*env)->ReleaseIntArrayElements(env, class_ids, c_id, 0); (*env)->ReleaseIntArrayElements(env, subject_ids, s_id, 0); }
/** * INIT */ int handleINIT(sessionid_t sessionid, bus_t bus, char *device, char *parameter, char *reply) { struct timeval time; int rc = SRCP_UNSUPPORTEDDEVICEGROUP; /*INIT <bus> GL "<addr> <protocol> <optional further parameters>" */ if (bus_has_devicegroup(bus, DG_GL) && strncasecmp(device, "GL", 2) == 0) { long addr, protversion, n_fs, n_func, nelem; char prot; nelem = sscanf(parameter, "%ld %c %ld %ld %ld", &addr, &prot, &protversion, &n_fs, &n_func); if (nelem >= 5) { sessionid_t lockid = 0; /* Only if not locked !! */ cacheGetLockGL(bus, addr, &lockid); if (lockid == 0 || lockid == sessionid) { rc = cacheInitGL(bus, addr, prot, protversion, n_fs, n_func); } else { rc = SRCP_DEVICELOCKED; } } else { rc = SRCP_LISTTOOSHORT; } } else if (bus_has_devicegroup(bus, DG_GA) && strncasecmp(device, "GA", 2) == 0) { long addr, nelem; char prot; nelem = sscanf(parameter, "%ld %c", &addr, &prot); if (nelem >= 2) { rc = initGA(bus, addr, prot); } else { rc = SRCP_LISTTOOSHORT; } } else if (bus_has_devicegroup(bus, DG_FB) && strncasecmp(device, "FB", 2) == 0) { long addr, index, nelem; char prot; nelem = sscanf(parameter, "%ld %c %ld", &addr, &prot, &index); if (nelem >= 3) { rc = initFB(bus, addr, prot, index); } else { rc = SRCP_LISTTOOSHORT; } } else if (bus_has_devicegroup(bus, DG_POWER) && strncasecmp(device, "POWER", 5) == 0) { rc = initPower(bus); } else if (bus_has_devicegroup(bus, DG_TIME) && strncasecmp(device, "TIME", 4) == 0) { int nelem; long rx, ry; nelem = sscanf(parameter, "%ld %ld", &rx, &ry); if (nelem >= 2) { rc = initTIME(rx, ry); /* checks also values! */ } else { rc = SRCP_LISTTOOSHORT; } } /* INIT <bus> SM "<protocol>" */ else if (bus_has_devicegroup(bus, DG_SM) && strncasecmp(device, "SM", 2) == 0) { int result; char protocol[MAXSRCPLINELEN]; result = sscanf(parameter, "%s", protocol); if (result < 1) rc = SRCP_LISTTOOSHORT; else if (strncasecmp(protocol, "NMRA", 4) == 0) rc = infoSM(bus, INIT, 0, -1, NMRA, 0, 0, reply); else rc = SRCP_WRONGVALUE; } gettimeofday(&time, NULL); srcp_fmt_msg(rc, reply, time); return rc; }