vector<int> BL(vector<Data> train, vector<int> solution_initial){ int n = train.at(0).attributes.size(); vector<int> S = solution_initial; vector<int> S_neighbour = vector<int>(S); double new_rate, rate_s; bool better = true; for(int i = 0; i < 15000 && better; ){ better = false; vector<int> order = vector_random(n); for(int j = 0; j < S.size() && !better; j++, i++){ int pos = order.at(j); S_neighbour.at(pos) = (S_neighbour.at(pos) == 1) ? 0 : 1; //flip new_rate = tasa_clas(S_neighbour, train, train); if(rate_s < new_rate){ S = S_neighbour; rate_s = new_rate; better = true; } else S_neighbour.at(pos) = (S_neighbour.at(pos) == 1) ? 0 : 1; // flip back } } return S; }
pair<double, vector<int>> selectBestChromosome(multimap<double,vector<int>> chromosomes){ int n = chromosomes.size(); vector<int> numbers = vector_random(n); int pos_random = Randint(0,n-2); int max = numbers.at(pos_random); if(max < numbers.at(pos_random+1)) max = numbers.at(pos_random+1); multimap<double, vector<int>>::iterator it = chromosomes.begin(); for(int i = 0; i < max; i++) it++; return pair<double, vector<int>>((*it).first, (*it).second); }
/* * Runs benchmark. */ int main(int argc, char **argv) { int i; /* Loop index. */ int *map; /* Map of clusters. */ uint64_t end; /* End time. */ uint64_t start; /* Start time. */ vector_t *data; /* Data points. */ readargs(argc, argv); printf("CAPBench - KM kernel\n"); printf(" # of threads: %d \n", nthreads); timer_init(); srandnum(seed); omp_set_num_threads(nthreads); /* Benchmark initialization. */ start = timer_get(); data = smalloc(p->npoints*sizeof(vector_t)); for (i = 0; i < p->npoints; i++) { data[i] = vector_create(p->dimension); vector_random(data[i]); } end = timer_get(); /* Cluster data. */ printf("Entering in KM ROI - Clustering data...\n"); start = timer_get(); m5_reset_stats(0,0); map = kmeans(data, p->npoints, p->ncentroids, p->mindistance); end = timer_get(); m5_dump_stats(0,0); printf("KM total time: %f\n", timer_diff(start, end)*MICROSEC); /* House keeping. */ free(map); for (i = 0; i < p->npoints; i++) vector_destroy(data[i]); free(data); return (0); }
multimap<double, vector<int>> cross(multimap<double, vector<int>> chromosomes, double probability, vector<Data> train){ int n = chromosomes.size(); vector<int> order = vector_random(n); vector<int> order_cross; pair <pair<double,vector<int>>, pair<double,vector<int>>> chromosomes_songs; multimap<double, vector<int>> chromosomed_crossed; int n_cross = n*probability; for(int i = 0; i < n_cross; i++){ order_cross.push_back(order.at(i)); } for(int i = 0; i < order_cross.size()-1; i+=2) { int c1 = order_cross.at(i); int c2 = order_cross.at(i + 1); multimap<double, vector<int>>::iterator it1 = chromosomes.begin(), it2 = chromosomes.begin(); for (int j = 0; j < c1; j++) it1++; for (int j = 0; j < c2; j++) it2++; chromosomes_songs = cross(*it1, *it2, train); chromosomed_crossed.insert(chromosomes_songs.first); chromosomed_crossed.insert(chromosomes_songs.second); } for(int i = n_cross-1; i < order.size(); i++){ int v = order.at(i); multimap<double, vector<int>>::iterator it = chromosomes.begin(); for (int j = 0; j < v; j++) it++; chromosomed_crossed.insert(*it); } return chromosomed_crossed; }
static void check_print_and_free_new_random_vector_______sent_size_vector_array_____returned(void **state){ vector_t * vc =vector_random(); assert_int_equal(vector_print(vc),1); assert_int_equal(vector_free(vc),1); }
vector<int> AGE(vector<Data> train){ int n = train.at(0).attributes.size(); vector<int> chromosome; multimap <double, vector<int>> chromosomes_initials, chromosomes_pre_cross, chromosomes_crossed; double pm = 0.001; int num_gen_to_mute = pm*2*n; vector<int> gen_to_mute; vector<int> chromosome_to_mute; int evaluates = 0; for (int i = 0; i < 30; i++) { for (int j = 0; j < n; j++) chromosome.push_back(Randint(0, 1)); chromosomes_initials.insert(pair<double, vector<int>>(tasa_clas(chromosome, train, train), chromosome)); chromosome.clear(); evaluates++; } while(evaluates < 5000) { //Selection chromosomes_pre_cross.clear(); for (int i = 0; i < 2; i++) chromosomes_pre_cross.insert(selectBestChromosome(chromosomes_initials)); //Cross multimap<double, vector<int>>::iterator it = chromosomes_pre_cross.begin(); it++; pair <pair<double,vector<int>>, pair<double,vector<int>>> chromosomes_songs = cross(*chromosomes_pre_cross.begin(), *it, train); chromosomes_crossed.insert(chromosomes_songs.first); chromosomes_crossed.insert(chromosomes_songs.second); evaluates+=2; //Mutation if(num_gen_to_mute > 0){ gen_to_mute = vector_random(n); for (int i = n; i > num_gen_to_mute; i--) gen_to_mute.pop_back(); for (int j = 0; j < num_gen_to_mute; j++) chromosome_to_mute.push_back(Randint(0, 1)); for (int i = 0; i < num_gen_to_mute; i++) { multimap<double, vector<int>>::iterator chromosome_selected = chromosomes_crossed.begin(); for (int j = 0; j < chromosome_to_mute.at(i); j++) chromosome_selected++; pair<double, vector<int>> element = *chromosome_selected; chromosomes_crossed.erase(chromosome_selected); element.second.at(gen_to_mute.at(i)) = (element.second.at(gen_to_mute.at(i)) == 1) ? 0 : 1; chromosomes_crossed.insert(pair<double,vector<int>>(tasa_clas(element.second, train, train), element.second)); evaluates++; } } for (multimap<double, vector<int>>::iterator it = chromosomes_crossed.begin(); it != chromosomes_crossed.end(); it++) { if((*chromosomes_initials.begin()).first < (*it).first) { chromosomes_initials.erase(chromosomes_initials.begin()); chromosomes_initials.insert(*it); } } } multimap<double, vector<int>>::iterator best_chromosome = chromosomes_initials.end(); best_chromosome--; pair<double, vector<int>> solution_final = *best_chromosome; return solution_final.second; }
vector<int> AGG(vector<Data> train){ int n = train.at(0).attributes.size(); vector<int> chromosome; pair<double, vector<int>> elitism; multimap <double, vector<int>> chromosomes_initials, chromosomes_pre_cross, chromosomes_crossed, chromosomes_final; double pc = 0.7; double pm = 0.001; int num_gen_to_mute = pm*30*n; vector<int> gen_to_mute; vector<int> chromosome_to_mute; bool elitism_is; int evaluates = 0; for (int i = 0; i < 30; i++) { for (int j = 0; j < n; j++) chromosome.push_back(Randint(0, 1)); chromosomes_final.insert(pair<double, vector<int>>(tasa_clas(chromosome, train, train), chromosome)); chromosome.clear(); evaluates++; } while(evaluates < 5000) { elitism_is = false; gen_to_mute = vector_random(n); chromosome_to_mute = vector_random(30); chromosomes_initials = chromosomes_final; multimap<double, vector<int>>::iterator best = chromosomes_initials.end(); best--; elitism = *best; //Selection chromosomes_pre_cross.clear(); for (int i = 0; i < 30; i++) chromosomes_pre_cross.insert(selectBestChromosome(chromosomes_initials)); //Cross chromosomes_crossed = cross(chromosomes_pre_cross, pc, train); evaluates+=(30*pc); //Mutation for (int i = n; i > num_gen_to_mute; i--) gen_to_mute.pop_back(); for (int j = 30; j > num_gen_to_mute; j--) chromosome_to_mute.pop_back(); for (int i = 0; i < num_gen_to_mute; i++) { multimap<double, vector<int>>::iterator chromosome_selected = chromosomes_crossed.begin(); for (int j = 0; j < chromosome_to_mute.at(i); j++) chromosome_selected++; pair<double, vector<int>> element = *chromosome_selected; chromosomes_crossed.erase(chromosome_selected); element.second.at(gen_to_mute.at(i)) = (element.second.at(gen_to_mute.at(i)) == 1) ? 0 : 1; chromosomes_crossed.insert(pair<double,vector<int>>(tasa_clas(element.second, train, train), element.second)); evaluates++; } //Select elitism for (multimap<double, vector<int>>::iterator it = chromosomes_crossed.begin(); it != chromosomes_crossed.end() && !elitism_is; it++) { pair<double, vector<int>> element = *it; if (element.first == elitism.first && element.second == elitism.second) elitism_is = true; } if (!elitism_is) { chromosomes_crossed.erase(chromosomes_crossed.begin()); chromosomes_crossed.insert(elitism); } chromosomes_final = chromosomes_crossed; } multimap<double, vector<int>>::iterator best_chromosome = chromosomes_final.end(); best_chromosome--; pair<double, vector<int>> solution_final = *best_chromosome; return solution_final.second; }
void test_centrosym(int NREPEAT, int dim) { int res, irepeat; clock_t t1, t2; double tmean_product_full=0, tmean_product_comp=0; double tmean_traceprod_full=0, tmean_traceprod_comp=0; double tmean_traceprodnaive_full=0, tmean_traceprodnaive_comp=0; double tmean_quadform_full=0, tmean_quadform_comp=0; printf("\n==============================================\n"); printf("Testing properties of centrosymmetric matrices\n"); printf("----------------------------------------------\n"); printf("Performing benchmark"); for( irepeat = 0; irepeat < NREPEAT; irepeat++ ){ fflush(NULL); printf("."); // Generate two random centrosymmetric matrices (full form) double *matfull1; square_alloc(&matfull1, dim); centrosym_full_random(matfull1, dim); res = centrosym_isvalid(matfull1, dim); if(res == 0) printf("matfull1 is not centrosymmetric\n"); double *matfull2; square_alloc(&matfull2, dim); centrosym_full_random(matfull2, dim); res = centrosym_isvalid(matfull2, dim); if(res == 0) printf("matfull2 is not centrosymmetric\n"); // Extract compressed forms double *matcomp1; centrosym_alloc(&matcomp1, dim); centrosym_full_extractcomp(matcomp1, matfull1, dim); double *matcomp2; centrosym_alloc(&matcomp2, dim); centrosym_full_extractcomp(matcomp2, matfull2, dim); // Perform the product in full form double *matfull3; square_alloc(&matfull3, dim); fflush(NULL); t1 = clock(); square_product(matfull3, matfull1, matfull2, dim); fflush(NULL); t2 = clock(); tmean_product_full += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; tmean_traceprodnaive_full += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; //printf("Matrix product in full form : %4.4e seconds\n",(t2 - t1) / (double)CLOCKS_PER_SEC); double *matcomp3; centrosym_alloc(&matcomp3, dim); centrosym_full_extractcomp(matcomp3, matfull3, dim); // Check that the full form is still centrosymmetric res = centrosym_isvalid(matfull3, dim); if(res == 0) printf("matfull3 is not centrosymmetric\n"); // Perform the product in compressed fonm double *matcomp4; centrosym_alloc(&matcomp4, dim); fflush(NULL); t1 = clock(); centrosym_product(matcomp4, matcomp1, matcomp2, dim); fflush(NULL); t2 = clock(); tmean_product_comp += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; tmean_traceprodnaive_comp += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; //printf("Matrix product in comp form : %4.4e seconds\n",(t2 - t1) / (double)CLOCKS_PER_SEC); // Extract and compare the two results res = centrosym_assertequal(matcomp4, matcomp3, dim); if(res == 0) printf("matcomp4 is not equal to matcomp3\n"); //centrosym_print(matcomp3, dim); //centrosym_print(matcomp4, dim); // Test the trace of a product fflush(NULL); t1 = clock(); double traceprodfull = square_traceprod(matfull1, matfull2, dim); fflush(NULL); t2 = clock(); tmean_traceprod_full += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; fflush(NULL); t1 = clock(); double traceprodcomp = centrosym_traceprod(matcomp1, matcomp2, dim); fflush(NULL); t2 = clock(); tmean_traceprod_comp += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; if( abs(traceprodfull - traceprodcomp) > 1e-10 ) printf("traceprodcomp is not equal to traceprodfull\n"); fflush(NULL); t1 = clock(); double traceprodfullnaive = square_trace(matfull3, dim); fflush(NULL); t2 = clock(); tmean_traceprodnaive_comp += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; fflush(NULL); t1 = clock(); double traceprodcompnaive = centrosym_trace(matcomp4, dim); fflush(NULL); t2 = clock(); tmean_traceprodnaive_full += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; if( abs(traceprodfullnaive - traceprodcompnaive) > 1e-10 ) printf("traceprodfullnaive is not equal to traceprodcompnaive\n"); // Generate random vectors double *x = (double*)calloc(dim, sizeof(double)); double *y = (double*)calloc(dim, sizeof(double)); vector_random(x, dim); vector_random(y, dim); // Test quadratic forms fflush(NULL);t1 = clock(); double quadform_full = square_quadform(x, matfull3, y, dim); fflush(NULL);t2 = clock(); tmean_quadform_full += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; fflush(NULL);t1 = clock(); double quadform_comp = centrosym_quadform(x, matcomp4, y, dim); fflush(NULL);t2 = clock(); tmean_quadform_comp += (double)(t2 - t1) / (double)CLOCKS_PER_SEC; //printf("\n %f - %f = %2.2e\n",quadform_full,quadform_comp,quadform_full-quadform_comp); if( abs(quadform_full - quadform_comp) > 1e-6 ) printf("quadform_full is not equal to quadform_comp\n"); free(x); free(y); free(matfull1); free(matcomp1); free(matfull2); free(matcomp2); free(matfull3); free(matcomp3); free(matcomp4); } printf("done\n"); printf("> Memory gain due to compressed form : %2.2f \n", ((double)dim*dim)/(dim*(dim+1)/2) ); //printf("Mean time for matrix product in full form : %4.4f seconds\n",tmean_product_full / (double)NREPEAT); //printf("Mean time for matrix product in comp form : %4.4f seconds\n",tmean_product_comp / (double)NREPEAT); printf("> Matrix product acceleration factor : %2.2f \n", (double)tmean_product_full / (double)tmean_product_comp ); //printf("Mean time for trace product in full form : %4.4f seconds\n",tmean_traceprod_full / (double)NREPEAT); //printf("Mean time for trace product in comp form : %4.4f seconds\n",tmean_traceprod_comp / (double)NREPEAT); //printf("Mean time for naive trace product in full form : %4.4f seconds\n",tmean_traceprodnaive_full / (double)NREPEAT); //printf("Mean time for naive trace product in comp form : %4.4f seconds\n",tmean_traceprodnaive_comp / (double)NREPEAT); printf("> Trace-product acceleration factor : %2.2f \n", (double)tmean_traceprod_full / (double)tmean_traceprod_comp ); //printf("Mean time for full quadratic form : %4.4f seconds\n",tmean_quadform_full / (double)NREPEAT); //printf("Mean time for compressed quadratic form : %4.4f seconds\n",tmean_quadform_comp / (double)NREPEAT); printf("> Quadratic form acceleration factor : %2.2f \n", (double)tmean_quadform_full / (double)tmean_quadform_comp ); printf("----------------------------------------------"); }