bool binomial_ring::remove_content(binomial &f) const { // If the monomials of f have a common monomial factor, remove it from each, and // return true. Otherwise return false. monomial m = f.lead; monomial n = f.tail; bool result = false; for (int i=0; i<nvars; i++) { if (m[i] > 0 && n[i] > 0) { if (m[i] > n[i]) { m[i] -= n[i]; n[i] = 0; } else { n[i] -= m[i]; m[i] = 0; } result = true; } } if (result) { set_weights(m); // This is an inefficient way to do this... set_weights(n); } return result; }
void binomial_ring::intvector_to_binomial(vec f, binomial &result) const // result should be a preallocated binomial { for (int i=0; i<nslots; i++) { result.lead[i] = 0; result.tail[i] = 0; } for ( ; f != NULL; f = f->next) { std::pair<bool,long> res = globalZZ->coerceToLongInteger(f->coeff); M2_ASSERT(res.first); int e = static_cast<int>(res.second); if (e > 0) result.lead[f->comp] = e; else if (e < 0) result.tail[f->comp] = -e; } set_weights(result.lead); set_weights(result.tail); normalize(result); }
int igraph_i_largest_weighted_cliques(const igraph_t *graph, const igraph_vector_t *vertex_weights, igraph_vector_ptr_t *res) { graph_t *g; igraph_integer_t vcount = igraph_vcount(graph); if (vcount == 0) { igraph_vector_ptr_clear(res); return IGRAPH_SUCCESS; } igraph_to_cliquer(graph, &g); IGRAPH_FINALLY(graph_free, g); IGRAPH_CHECK(set_weights(vertex_weights, g)); igraph_vector_ptr_clear(res); igraph_cliquer_opt.user_data = res; igraph_cliquer_opt.user_function = &collect_cliques_callback; IGRAPH_FINALLY(free_clique_list, res); CLIQUER_INTERRUPTABLE(clique_find_all(g, 0, 0, FALSE, &igraph_cliquer_opt)); IGRAPH_FINALLY_CLEAN(1); graph_free(g); IGRAPH_FINALLY_CLEAN(1); return IGRAPH_SUCCESS; }
int igraph_i_weighted_clique_number(const igraph_t *graph, const igraph_vector_t *vertex_weights, igraph_real_t *res) { graph_t *g; igraph_integer_t vcount = igraph_vcount(graph); if (vcount == 0) { *res = 0; return IGRAPH_SUCCESS; } igraph_to_cliquer(graph, &g); IGRAPH_FINALLY(graph_free, g); IGRAPH_CHECK(set_weights(vertex_weights, g)); igraph_cliquer_opt.user_function = NULL; /* we are not using a callback function, thus this is not interruptable */ *res = clique_max_weight(g, &igraph_cliquer_opt); graph_free(g); IGRAPH_FINALLY_CLEAN(1); return IGRAPH_SUCCESS; }
monomial binomial_ring::make_monomial(int *exp) const // Make a monomial from an exponent vector { monomial result = new_monomial(); for (int i=0; i<nvars; i++) result[i] = exp[i]; set_weights(result); return result; }
monomial binomial_ring::monomial_lcm(monomial m, monomial n) const // return lcm(m,n) { monomial result = new_monomial(); for (int i=0; i<nvars; i++) result[i] = (m[i] > n[i] ? m[i] : n[i]); set_weights(result); return result; }
bool binomial_ring::vector_to_binomial(vec f, binomial &result) const // result should already have both monomials allocated // returns false if f is not a binomial, otherwise result is set. { if (f == NULL) return false; Nterm *t = f->coeff; if (t == NULL || t->next == NULL || t->next->next != NULL) return false; R->getMonoid()->to_expvector(t->monom, result.lead); set_weights(result.lead); R->getMonoid()->to_expvector(t->next->monom, result.tail); set_weights(result.tail); normalize(result); return true; }
GeoConnection::GeoConnection(SpikingGroup *source, NeuronGroup *destination, AurynWeight conWeight, AurynWeight lrcWeight, bool isSourceInhibitory, bool isDestInhibitory, unsigned int fieldWidth, double sigma, TransmitterType transmitter, string name) :SparseConnection(source,destination,transmitter) { set_weights(conWeight,lrcWeight); set_params(fieldWidth,fieldWidth/2,5,isSourceInhibitory,isDestInhibitory); set_probability(sigma,0.4); allocateMEM(source,destination); connectLikeInVC((source==destination)); }
void set_weights(t_room *room, int weight) { int i; i = -1; if (room->weight > weight) return ; room->weight = weight; while (room->tubes[++i]) set_weights(room->tubes[i], weight - 1); }
monomial binomial_ring::quotient(monomial m, monomial n) const // return m:n { monomial result = new_monomial(); for (int i=0; i<nslots; i++) { int x = m[i] - n[i]; result[i] = (x > 0 ? x : 0); } set_weights(result); return result; }
void binomial_ring::translate_monomial(const binomial_ring *old_ring, monomial &m) const { int i; if (m == NULL) return; monomial result = new_monomial(); for (i=0; i<old_ring->nvars; i++) result[i] = m[i]; for (i=old_ring->nvars; i<nvars; i++) result[i] = 0; old_ring->remove_monomial(m); set_weights(result); m = result; }
Json json_classification(const vector<string>& sentence, const Mat<R>& probs, const Mat<R>& word_weights) { // store sentence memory & tokens: auto sentence_viz = visualizable::Sentence<R>(sentence); sentence_viz.set_weights(word_weights); // store sentence as input + distribution as output: Json::object json_example = { { "type", "classifier_example"}, { "input", sentence_viz.to_json()}, { "output", utils::json_finite_distribution(probs, SST::label_names) }, }; return json_example; }
int igraph_i_weighted_cliques(const igraph_t *graph, const igraph_vector_t *vertex_weights, igraph_vector_ptr_t *res, igraph_real_t min_weight, igraph_real_t max_weight, igraph_bool_t maximal) { graph_t *g; igraph_integer_t vcount = igraph_vcount(graph); if (vcount == 0) { igraph_vector_ptr_clear(res); return IGRAPH_SUCCESS; } if (min_weight != (int) min_weight) { IGRAPH_WARNING("Only integer vertex weights are supported; the minimum weight will be truncated to its integer part"); min_weight = (int) min_weight; } if (max_weight != (int) max_weight) { IGRAPH_WARNING("Only integer vertex weights are supported; the maximum weight will be truncated to its integer part"); max_weight = (int) max_weight; } if (min_weight <= 0) min_weight = 1; if (max_weight <= 0) max_weight = 0; if (max_weight > 0 && max_weight < min_weight) IGRAPH_ERROR("max_weight must not be smaller than min_weight", IGRAPH_EINVAL); igraph_to_cliquer(graph, &g); IGRAPH_FINALLY(graph_free, g); IGRAPH_CHECK(set_weights(vertex_weights, g)); igraph_vector_ptr_clear(res); igraph_cliquer_opt.user_data = res; igraph_cliquer_opt.user_function = &collect_cliques_callback; IGRAPH_FINALLY(free_clique_list, res); CLIQUER_INTERRUPTABLE(clique_find_all(g, min_weight, max_weight, maximal, &igraph_cliquer_opt)); IGRAPH_FINALLY_CLEAN(1); graph_free(g); IGRAPH_FINALLY_CLEAN(1); return IGRAPH_SUCCESS; }
int main(int argc, char *argv[]) { char arg_string[2000]; int nsub,n_new_sub,real_nsub; float *score,p; int max_cc[2],s,n_non_mendelian; non_mendelian *non_mendelians; char *non_mendelian_report; par_info pi; sa_par_info spi; subject **sub,**new_sub,**real_sub; pi.use_cc=1; printf("%s v%s\n",PROGRAM,SAVERSION); printf("MAX_LOCI=%d\nMAX_SUB=%d\n",MAX_LOCI,MAX_SUB); assert(sub=(subject **)calloc(MAX_SUB,sizeof(subject*))); for (s=0;s<MAX_SUB;++s) assert(sub[s]=(subject *)calloc(1,sizeof(subject))); assert(score=(float *)calloc(MAX_SUB,sizeof(float))); max_cc[0]=max_cc[1]=0; read_all_args(argv,argc, &pi, &spi); // make_arg_string(arg_string,argc,argv); // parse_arg_string(arg_string,&pi,&spi,&pspi); process_options(&pi,&spi); if (spi.df[FILTERFILE].fp) initExclusions(spi.df[FILTERFILE].fp); read_all_data(&pi,&spi,sub,&nsub,names,comments,func_weight); if (spi.use_trios) { if (atoi(comments[0])>22 || toupper(comments[0][0]) == 'X' || toupper(comments[0][0]) == 'Y' || toupper(comments[0][0]) == 'C'&&toupper(comments[0][1]) == 'H'&&toupper(comments[0][2]) == 'R' && (atoi(comments[0] + 3) > 22 || toupper(comments[0][3]) == 'X' || toupper(comments[0][3]) == 'Y')) { error("Cannot at present use trios for genes on X or Y chromosome", ""); return 1; } } if (spi.use_trios) { assert(new_sub=(subject **)calloc(nsub,sizeof(subject*))); for (s=0;s<nsub;++s) assert(new_sub[s]=(subject *)calloc(1,sizeof(subject))); assert(non_mendelians=(non_mendelian *)calloc(MAX_SUB,sizeof(non_mendelian))); if ((n_new_sub=sort_trios(sub,nsub,&pi,new_sub,non_mendelians,&n_non_mendelian,long_line))==0) exit(1); real_sub=sub; sub=new_sub; real_nsub=nsub; nsub=n_new_sub; assert(non_mendelian_report=(char*)malloc(strlen(long_line)+1)); strcpy(non_mendelian_report,long_line); } fprintf(spi.df[OUTFILE].fp,"pscoreassoc output\n" "Locus controls frequency cases frequency frequency allele weight\n" " AA : AB : BB AA : AB : BB \n"); get_freqs(sub,nsub,&pi,&spi,cc_freq,cc_count,cc_genocount); applyExclusions(&pi); set_weights(spi.df[OUTFILE].fp,weight,missing_score,rarer,sub,nsub,&pi,&spi,func_weight,cc_freq,cc_count,max_cc,names,comments); get_scores(score,weight,missing_score,rarer,sub,nsub,&pi,&spi); p=do_score_onetailed_ttest(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer); if (spi.df[SCOREFILE].fp) write_scores(spi.df[SCOREFILE].fp,sub,nsub,score); if (spi.do_recessive_test) { if (atoi(comments[0]+3)>22 || toupper(comments[0][4])=='X' || toupper(comments[0][4])=='Y') // simple trick for now to avoid X and Y genes fprintf(spi.df[OUTFILE].fp,"\nCannot do recessive test for genes on X or Y chromosome.\n"); else if (spi.use_haplotypes) do_recessive_HWE_test_with_haplotypes(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer,names); else do_recessive_HWE_test(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer,names); } stateExclusions(spi.df[OUTFILE].fp); printf("\nProgram run completed OK\n"); return 0; }
int main(int argc, char* argv[]){ if (argc != 1){ fprintf( stderr, "Usage: %s\n", argv[0]); exit(EXIT_FAILURE); } int i; /* data directory */ char data_dir[256]; snprintf(data_dir, 256, "../data/"); /* load the pointing list */ char plist_filename[256]; snprintf(plist_filename, 256, "%stodo_list.ascii.dat", data_dir); int N_plist; POINTING *plist; load_pointing_list(plist_filename, &N_plist, &plist); /* Read star data for each pointing */ for(i = 0; i < N_plist; i++){ char data_filename[256]; snprintf(data_filename, 256, "%sstar_%s.ascii.dat", data_dir, plist[i].ID); load_data(data_filename, &plist[i].N_data, &plist[i].data); } fprintf(stderr, "Star data loaded from %s\n", data_dir); /* Read model data for each pointing, here each file starts with uniformly distributed random */ for(i = 0; i < N_plist; i++){ char model_filename[256]; snprintf(model_filename, 256, "%suniform_%s.ascii.dat", data_dir, plist[i].ID); load_data(model_filename, &plist[i].N_model, &plist[i].model); } fprintf(stderr, "Uniform model data loaded from %s\n", data_dir); /* initialize model parameters */ PARAMETERS params; params.N_parameters = 5; /* Total number of free paramters. */ params.thindisk_type = 1; /* turn on thin disk */ params.thindisk_r0 = 2.475508; params.thindisk_z0 = 0.241209; params.thindisk_n0 = 1.0; params.thickdisk_type = 1; /* turn on thick disk */ params.thickdisk_r0 = 2.417346; params.thickdisk_z0 = 0.694395; params.thickdisk_n0 = 0.106672; params.halo_type = 0; /* turn off halo */ fprintf(stderr, "Set initial parameters..\n"); int mcmc_steps, efficiency_counter; double efficiency; MCMC *mcmc_chain; double chi2, delta_chi2; int dof; mcmc_steps = 500000; mcmc_chain = calloc(mcmc_steps, sizeof(MCMC)); FILE *file_chain_realtime; file_chain_realtime = fopen("./data/mcmc.dat", "a"); fprintf(stderr, "Allocate MCMC chain..Max steps = %d \n", mcmc_steps); /* set the first element as the initial parameters. Then calculate the chi2 for the initial parameters. */ mcmc_chain[0].params = params; set_weights(mcmc_chain[0].params, plist, N_plist); /* allocate space for correlation function calculation */ /* This also does a first time correlation calculation of initial parameters. */ /* DD pairs may only calculated once here but not later. */ correlation_initialize(plist, N_plist); /* load the jackknife fractional errors */ load_errors(plist, N_plist); mcmc_chain[0].dof = degrees_of_freedom(plist, N_plist, params); mcmc_chain[0].chi2 = chi_square(plist, N_plist); fprintf(stderr, "initial: chi2 = %le \n", mcmc_chain[0].chi2); fprintf(stderr, "Start MCMC calculation..\n"); /* Markov chain loop */ efficiency_counter = 1; for(i = 1; i < mcmc_steps; i++){ /* update new positions in paramter space */ params = update_parameters(mcmc_chain[i - 1].params); /* recalculate weights */ set_weights(params, plist, N_plist); /* recalculate correlation functions */ calculate_correlation(plist, N_plist); /* get chi squares */ dof = degrees_of_freedom(plist, N_plist, params); chi2 = chi_square(plist, N_plist); delta_chi2 = chi2 - mcmc_chain[i - 1].chi2; fprintf(stderr, "delta_chi2 = %le \n", delta_chi2); if(delta_chi2 <= 0.0){ /* if delta chisquare is smaller then record*/ mcmc_chain[i].params = params; mcmc_chain[i].chi2 = chi2; mcmc_chain[i].dof = dof; efficiency_counter++; } else{ /* if delta chisquare is bigger then use probability to decide */ /* !!! replace with GSL random generator later !!! */ double tmp = (double)rand() / (double)RAND_MAX; /* a random number in [0,1] */ if (tmp < exp(- delta_chi2 / 2.0)){ mcmc_chain[i].params = params; mcmc_chain[i].chi2 = chi2; mcmc_chain[i].dof = dof; efficiency_counter++; } else{ /* record the old position. */ mcmc_chain[i] = mcmc_chain[i - 1]; } } efficiency = (float) efficiency_counter / i; /* print out progress */ /* if(i % (mcmc_steps / 100) == 0){ */ /* fprintf(stderr, "MCMC... %d / %d: chi2 = %lf \n", i, mcmc_steps, chi2); */ /* } */ fprintf(stderr, "MCMC... %d / %d: efficiency = %lf \n z0_thin = %lf, r0_thin = %lf, z0_thick = %lf, r0_thick = %lf, n0_thick = %lf \n chi2 = %lf, dof = %d, chi2_reduced = %lf \n\n", i, mcmc_steps, efficiency, params.thindisk_z0, params.thindisk_r0, params.thickdisk_z0, params.thickdisk_r0, params.thickdisk_n0, chi2, dof, chi2/dof); MCMC tmp_mc; PARAMETERS tmp_p; tmp_mc = mcmc_chain[i]; tmp_p = tmp_mc.params; // fprintf(stderr, "Made it here\n"); // fprintf(file_chain_realtime, "%d\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%d\t%lf\n", // i+50000, tmp_p.thindisk_r0, tmp_p.thindisk_z0, tmp_p.thickdisk_r0, tmp_p.thickdisk_z0, tmp_p.thickdisk_n0, // tmp_mc.chi2, (int)tmp_mc.dof, tmp_mc.chi2/dof); // fprintf(stderr, "Printed to file.\n"); // if(i % 50 == 0){ // fflush(file_chain_realtime); // } } /* end of mcmc */ output_mcmc(mcmc_chain, mcmc_steps); fprintf(stderr, "End MCMC calculation..\n"); for(i = 0; i < N_plist; i++){ free(plist[i].data); free(plist[i].model); free(plist[i].corr); } free(plist); free(mcmc_chain); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { char arg_string[2000]; int nsub,n_new_sub,real_nsub,nVarTypes; float **varScore,p; int s,n_non_mendelian; non_mendelian *non_mendelians; char *non_mendelian_report; par_info pi; sa_par_info spi; subject **sub,**new_sub,**real_sub; pi.use_cc=1; printf("%s v%s\n",PROGRAM,GVSVERSION); printf("MAX_LOCI=%d\nMAX_SUB=%d\n",MAX_LOCI,MAX_SUB); assert(sub=(subject **)calloc(MAX_SUB,sizeof(subject*))); for (s=0;s<MAX_SUB;++s) assert(sub[s]=(subject *)calloc(1,sizeof(subject))); max_cc[0]=max_cc[1]=0; read_all_args(argv,argc, &pi, &spi); // make_arg_string(arg_string,argc,argv); // parse_arg_string(arg_string,&pi,&spi,&pspi); process_options(&pi,&spi); if (spi.df[FILTERFILE].fp) initExclusions(spi.df[FILTERFILE].fp); read_all_data(&pi,&spi,sub,&nsub,names,comments,func_weight); if (spi.use_trios) { if (atoi(comments[0])>22 || toupper(comments[0][0]) == 'X' || toupper(comments[0][0]) == 'Y' || toupper(comments[0][0]) == 'C'&&toupper(comments[0][1]) == 'H'&&toupper(comments[0][2]) == 'R' && (atoi(comments[0] + 3) > 22 || toupper(comments[0][3]) == 'X' || toupper(comments[0][3]) == 'Y')) { error("Cannot at present use trios for genes on X or Y chromosome", ""); return 1; } } if (spi.use_trios) { assert(new_sub=(subject **)calloc(nsub,sizeof(subject*))); for (s=0;s<nsub;++s) assert(new_sub[s]=(subject *)calloc(1,sizeof(subject))); assert(non_mendelians=(non_mendelian *)calloc(MAX_SUB,sizeof(non_mendelian))); if ((n_new_sub=sort_trios(sub,nsub,&pi,&spi,new_sub,non_mendelians,&n_non_mendelian,long_line))==0) exit(1); real_sub=sub; sub=new_sub; real_nsub=nsub; nsub=n_new_sub; assert(non_mendelian_report=(char*)malloc(strlen(long_line)+1)); strcpy(non_mendelian_report,long_line); } else non_mendelian_report=0; // not used, compiler error otherwise get_freqs(sub,nsub,&pi,&spi,cc_freq,cc_count,cc_genocount); applyExclusions(&pi); spi.use_func_weights=0; // this is a trick to prevent the rarity weight being multiplied by the functional weight set_weights(0,weight,missing_score,rarer,sub,nsub,&pi,&spi,func_weight,cc_freq,cc_count,max_cc,names,comments); nVarTypes=readFlagTable(varFlagTable,&spi); assert(varScore=(float **)malloc(nsub*sizeof(float*))); for (s=0;s<nsub;++s) assert(varScore[s]=(float*)malloc(nVarTypes*sizeof(float))); // allocate a table to hold the subject scores for each variant type, then output fill it and it getVarScores(varFlagTable,nVarTypes,varScore,weight,func_weight,missing_score,rarer,sub,nsub,&pi,&spi); // beware, func_weight is indexed differently // weight[l]*=func_weight[pi->loci_to_use[l]]; // weight and missing score are only calcuated for loci to be used writeVarScores(spi.df[SCOREFILE].fp,sub,nsub,nVarTypes,varScore); fclose(spi.df[SCOREFILE].fp); spi.df[SCOREFILE].fp=0; fprintf(spi.df[OUTFILE].fp,"geneVarAssoc output\n"); fprintf(spi.df[OUTFILE].fp,"Used %d valid variants\n",pi.n_loci_to_use); stateExclusions(spi.df[OUTFILE].fp); fclose(spi.df[OUTFILE].fp); spi.df[OUTFILE].fp=0; // because otherwise the destructor will try to fclose it printf("\nProgram run completed OK\n"); return 0; }
float startGeneticAlgo(void** solvers, float** inputs, float* outputs, int count_row, int count_col, size_t(*get_res)(float* in, float* out, void* solver), void(*set_weights)(float* weights, void* solver), int count_weights, int count_person, int count_epochs, int count_bests, float mutation_percent, float* res_weights) { #ifdef GENETIC_DEBUG ofstream fout; fout.open("Genetic_debug.txt", ios_base::trunc); fout << "count_row = " << count_row << endl; fout << "count_col = " << count_col << endl; fout << "count_weights = " << count_weights << endl; fout << "count_person = " << count_person << endl; fout << "count_epochs = " << count_epochs << endl; fout << "count_bests = " << count_bests << endl; fout.close(); #endif // GENETIC_DEBUG float** new_weights = (float**)malloc(count_person * sizeof(float*)); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created new_weights" << endl; fout.close(); #endif // GENETIC_DEBUG int person_num, epoch_num, best_num, weight_num; float* err = (float*)malloc(count_person * sizeof(float)); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created err" << endl; fout.close(); #endif // GENETIC_DEBUG int* arr_best_nums = (int*)malloc(count_bests * sizeof(int)); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created arr_best_nums" << endl; fout.close(); #endif // GENETIC_DEBUG int* arr_bad_nums = (int*)malloc(count_bests * sizeof(int)); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created arr_bad_nums" << endl; fout.close(); #endif // GENETIC_DEBUG float best_err = 0; float** tmp_res = (float**)malloc(count_person*sizeof(float*)); for (person_num = 0; person_num < count_person; person_num++) { tmp_res[person_num] = (float*)malloc(sizeof(float)); new_weights[person_num] = (float*)malloc(count_weights * sizeof(float)); for (weight_num = 0; weight_num < count_weights; weight_num++) { new_weights[person_num][weight_num] = ((float)rand()) / RAND_MAX; } set_weights(new_weights[person_num], solvers[person_num]); } epoch_num = 0; #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created and set new_weights" << endl; fout.close(); #endif // GENETIC_DEBUG while (epoch_num < count_epochs) { for (person_num = 0; person_num < count_person; person_num++) { int row_num; #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Strat for person_num =" << person_num << endl; fout.close(); #endif // GENETIC_DEBUG err[person_num] = 0; #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Err =" << err[person_num] << endl; fout.close(); #endif // GENETIC_DEBUG for (row_num = 0; row_num < count_row; row_num++) { #ifdef GENETIC_DEBUG int count_col_tmp = 0; fout.open("Genetic_debug.txt", ios::app); fout << "Strat get res for row_num =" << row_num << endl; for (count_col_tmp = 0; count_col_tmp < count_col; count_col_tmp++) { fout << "Inputs " << count_col_tmp<<" = "<<inputs[row_num][count_col_tmp] << endl; } fout.close(); #endif // GENETIC_DEBUG get_res( inputs[row_num], tmp_res[person_num],solvers[person_num]); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Finished" << endl; fout.close(); #endif // GENETIC_DEBUG err[person_num] += (outputs[row_num] - tmp_res[person_num][0]) * (outputs[row_num] - tmp_res[person_num][0]); } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Finished for person_num =" << person_num << endl; fout.close(); #endif // GENETIC_DEBUG err[person_num] /= count_row; } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "epoch_num = "<< epoch_num << ", of " << count_epochs << endl; for (person_num = 0; person_num < count_person; person_num++) { fout << " person_num = " << person_num << ", err = " << err[person_num] << endl; } fout.close(); #endif // GENETIC_DEBUG for (best_num = 0; best_num < count_bests; best_num++) { int tmp_best_person_num; float tmp_best_person_err; person_num = 0; do { tmp_best_person_num = person_num; tmp_best_person_err = err[person_num]; person_num++; } while (err[tmp_best_person_num] < 0); for (; person_num < count_person; person_num++) { if (tmp_best_person_err > err[person_num] && err[person_num] >= 0) { tmp_best_person_err = err[person_num]; tmp_best_person_num = person_num; } } arr_best_nums[best_num] = tmp_best_person_num; err[tmp_best_person_num] = -err[tmp_best_person_num]; } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "epoch_num = " << epoch_num << ", of " << count_epochs << endl; for (best_num = 0; best_num < count_bests; best_num++) { fout << " best_num = " << arr_best_nums[best_num] << ", err = " << -err[arr_best_nums[best_num]] << endl; } fout.close(); #endif // GENETIC_DEBUG best_err = -err[0]; for (best_num = 0; best_num < count_bests; best_num++) { int tmp_bad_person_num; float tmp_bad_person_err; person_num = 0; do { tmp_bad_person_num = person_num; tmp_bad_person_err = err[person_num]; person_num++; } while (err[tmp_bad_person_num] < 0 && person_num < count_person); for (; person_num < count_person; person_num++) { if (tmp_bad_person_err < err[person_num]) { tmp_bad_person_err = err[person_num]; tmp_bad_person_num = person_num; } } arr_bad_nums[best_num] = tmp_bad_person_num; err[tmp_bad_person_num] = -err[tmp_bad_person_num]; } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "epoch_num = " << epoch_num << ", of " << count_epochs << endl; for (best_num = 0; best_num < count_bests; best_num++) { fout << " bad_num = " << arr_bad_nums[best_num] << ", err = " << -err[arr_bad_nums[best_num]] << endl; } fout.close(); #endif // GENETIC_DEBUG for (best_num = 0; best_num < count_bests; best_num += 2) { int tmp_mask, tmp_l, tmp_r, mut_mask, res_tmp; float is_mut; for (weight_num = 0; weight_num < count_weights; weight_num++) { tmp_mask = rand(); tmp_l = (*((int*)(&(new_weights[arr_best_nums[best_num]])))) & tmp_mask; tmp_r = (*((int*)(&(new_weights[arr_best_nums[best_num + 1]])))) & (!tmp_mask); res_tmp = (tmp_l | tmp_r); new_weights[arr_bad_nums[best_num]][weight_num] = (float)(*((float*)(&res_tmp))); is_mut = rand() / RAND_MAX; if (is_mut < mutation_percent) { mut_mask = rand(); new_weights[arr_bad_nums[best_num]][weight_num] = (float)((*((int*)(&(new_weights[arr_bad_nums[best_num]][weight_num])))) ^ mut_mask); } tmp_l = (*((int*)(&(new_weights[arr_best_nums[best_num]])))) & (!tmp_mask); tmp_r = (*((int*)(&(new_weights[arr_best_nums[best_num + 1]])))) & tmp_mask; res_tmp = (tmp_l | tmp_r); new_weights[arr_bad_nums[best_num + 1]][weight_num] = (float)(*((float*)(&res_tmp))); is_mut = rand() / RAND_MAX; if (is_mut < mutation_percent) { mut_mask = rand(); new_weights[arr_bad_nums[best_num + 1]][weight_num] = (float)((*((int*)(&(new_weights[arr_bad_nums[best_num + 1]][weight_num])))) ^ mut_mask); } } set_weights(new_weights[arr_bad_nums[best_num]], solvers[arr_bad_nums[best_num]]); set_weights(new_weights[arr_bad_nums[best_num + 1]], solvers[arr_bad_nums[best_num + 1]]); } epoch_num++; } for (weight_num = 0; weight_num < count_weights; weight_num++) { res_weights[weight_num] = new_weights[arr_best_nums[0]][weight_num]; } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Weights values:" << endl; for (weight_num = 0; weight_num < count_weights; weight_num++) { fout <<res_weights[weight_num] << endl; } fout.close(); #endif // GENETIC_DEBUG #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Was set res weights" << endl; fout.close(); #endif // GENETIC_DEBUG for (person_num = 0; person_num < count_person; person_num++) { free(new_weights[person_num]); } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove new weights" << endl; fout.close(); #endif // GENETIC_DEBUG free(new_weights); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove new weights, again" << endl; fout.close(); #endif // GENETIC_DEBUG free(err); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove err" << endl; fout.close(); #endif // GENETIC_DEBUG free(arr_best_nums); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove arr_best_nums" << endl; fout.close(); #endif // GENETIC_DEBUG free(arr_bad_nums); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove arr_bad_nums" << endl; fout.close(); #endif // GENETIC_DEBUG return best_err; }