/*_________________________________________________________ Test functions log normal adaptive mutation Selection operator: Tournament __________________________________________________________*/ \User functions: \end \At the beginning of each generation function: { generate_k_fold(K,packets_size,t1,k_tables); } \end \At the end of each generation function: \end \At each generation before reduce function: \end \GenomeClass::display: \end \User declarations : #include #include #include #include #include #define K 5 #define GENE_SIZE 4 #define GENOME_SIZE 32 float pMutPerGene=0.1; float pMutDesCard = 0.05; float pMutDesThre = 0.5; struct base* t1 = NULL; struct base* t2 = NULL; float* uniq_instances[2]; unsigned uniq_cnt[2]; struct base* k_tables[K]; unsigned packets_size[K]; \end \User classes : GenomeClass { float x[GENOME_SIZE]; } \end \Before everything else function: { srand(globalRandomGenerator->get_seed()); INSTEAD_EVAL_STEP = true; cout << "Seed : " << globalRandomGenerator->get_seed() << endl; srand(globalRandomGenerator->get_seed()); t1 = ba_postgres_load_ilot(1); t2 = ba_postgres_load_batiment(1); ba_set_links(t1,t2); //printf("+ %d - %d\n",t1->class_repartition[1],t1->class_repartition[0]); for( unsigned i=0 ; ihdr->attributes[t1->hdr->whichis_class]->no_values; i++ ) printf("%s : %d\n",t1->hdr->attributes[t1->hdr->whichis_class]->values[i],t1->class_repartition[i]); uniq_instances[0] = ba_compute_uniq_values(t2, 1, uniq_cnt+0); uniq_instances[1] = ba_compute_uniq_values(t2, 2, uniq_cnt+1); printf("%d %d\n",uniq_cnt[0],uniq_cnt[1]); // allocating K packets generate_k_fold(K,packets_size,t1,k_tables); #if 1 // try the perfect solution IndividualImpl* i = new IndividualImpl(); i->x[0] = 30; i->x[1] = 39; i->x[GENE_SIZE-1] = 0; for( unsigned j=3 ; jx[j] = INFINITY; float f = i->evaluate(); printf("fitness : %f\n",f); cTreeNode* t = generate_tree(i->x,t1,t2,GENOME_SIZE,GENE_SIZE); struct base* tc = table_from_genome(i->x,t1,t2,GENOME_SIZE,GENE_SIZE); show_tree(tc,t,0); #endif exit(-1); } \end \After everything else function: { EA->population->sortParentPopulation(); IndividualImpl* best = (IndividualImpl*)EA->population->parents[0]; printf("best fitness %f\n",best->evaluate()); for( unsigned i=0 ; ix[i+j]); } printf(" >= %3.0f\n",best->x[i+GENE_SIZE-1]); } printf("\n"); cTreeNode* root = generate_tree(best->x,t1,t2,GENOME_SIZE,GENE_SIZE); struct base* tmp_table = table_from_genome(best->x,t1,t2,GENOME_SIZE,GENE_SIZE); show_tree(tmp_table,root); printf("depth of resulting tree %d\n",root->tree_depth()); #if 1 unsigned error = 0; for( unsigned i=0 ; ino_instances ; i++ ){ unsigned predicted_class = root->classify_instance(tmp_table->instances[i]); unsigned real_class = (unsigned)tmp_table->instances[i][t1->hdr->whichis_class]; if( predicted_class != real_class ) error++; } printf(" error on the whole set : %d\n",error); #endif delete root; ba_delete( tmp_table ); } \end \GenomeClass::initialiser : { for( unsigned i=0; i10 ? 10 :value); // if value grether than 10 then value is 10 Genome.x[i+GENE_SIZE-1] = roundf(value); NbMut++; } } for( unsigned j=0 ; jevaluate(); } } \end \GenomeClass::evaluator : // Returns the score { struct base* k_tmp_tables[K]; unsigned error = 0; unsigned tree_size = 0; // generate tmp tables from genome, for every packets float fitness_value = 0; for( unsigned i=0 ; iinstances = (float**)malloc(sizeof(*tmp_learning_table->instances)*t1->no_instances-k_tables[i]->no_instances); tmp_learning_table->hdr = ba_partial_copy_hdr(k_tmp_tables[i]->hdr); tmp_test_table->hdr = ba_partial_copy_hdr(k_tmp_tables[i]->hdr); // create a learning set with k-1 packets unsigned copied_instances = 0; for( unsigned j=0 ; jinstances)+copied_instances, k_tmp_tables[j]->instances, sizeof(*tmp_learning_table->instances)*(k_tmp_tables[j]->no_instances)); copied_instances += k_tmp_tables[j]->no_instances; } tmp_learning_table->no_instances = copied_instances; // create the test set with 1 packet tmp_test_table->instances = (float**)malloc(sizeof(*tmp_test_table->instances)*k_tmp_tables[i]->no_instances); memcpy(tmp_test_table->instances,k_tmp_tables[i]->instances, sizeof(*tmp_test_table->instances)*k_tmp_tables[i]->no_instances); tmp_test_table->no_instances = k_tmp_tables[i]->no_instances; // set threshold, from t1 for standard attribute for( unsigned j=0 ; jhdr->no_attributes ; j++ ){ tmp_test_table->hdr->attributes[j]->threshold = t1->hdr->attributes[j]->threshold; tmp_test_table->hdr->attributes[j]->no_threshold = t1->hdr->attributes[j]->no_threshold; } // compute threshold for attribute generated by genome ba_compute_threshold_from(tmp_test_table,t1->hdr->no_attributes); // set threshold, from t1 for standard attribute for( unsigned j=0 ; jhdr->no_attributes ; j++ ){ tmp_learning_table->hdr->attributes[j]->threshold = t1->hdr->attributes[j]->threshold; tmp_learning_table->hdr->attributes[j]->no_threshold = t1->hdr->attributes[j]->no_threshold; } // compute threshold for attribute generated by genome ba_compute_threshold_from(tmp_learning_table,t1->hdr->no_attributes); cTreeNode* t = genereate_decision_tree(tmp_learning_table); //show_tree(k_tmp_tables[0],t,0); //DBG_print_instances(tmp_test_table->instances,tmp_test_table->no_instances,tmp_test_table->hdr->no_attributes); for( unsigned j=0 ; jno_instances ; j++ ){ unsigned predicted_class = t->classify_instance(tmp_test_table->instances[j]); unsigned real_class = (unsigned)tmp_test_table->instances[j][t1->hdr->whichis_class]; //printf("%3.0f : %d, %d\n",tmp_test_table->instances[j][1],predicted_class,real_class); // here compute classification error, or any quality measurment if( predicted_class!=real_class ){ error++; } } //printf("err : %f %d\n",fitness_value,t->tree_depth()); tree_size += t->tree_depth(); delete t; // free current sets // first un-assignate instances for( unsigned i=0 ; ino_instances ; i++ ){tmp_learning_table->instances[i] = NULL;} for( unsigned i=0 ; ino_instances ; i++ ){tmp_test_table->instances[i] = NULL;} tmp_learning_table->no_instances = 0; tmp_test_table->no_instances = 0; // then delete tmp tables ba_partial_copy_delete(tmp_learning_table); ba_partial_copy_delete(tmp_test_table); } fitness_value = (((float)error) / t1->no_instances)*100;// + ((float)tree_size)/K; for( unsigned i=0 ; i