card.ez 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*_________________________________________________________

Test functions
log normal adaptive mutation
Selection operator: Tournament
__________________________________________________________*/


\User functions:

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
float evaluation_fonction(int k, IndividualImpl* Genome,struct base** k_tables,unsigned* packets_size){
  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 ; i<k ; i++ ){
    k_tmp_tables[i] = table_from_genome(Genome->x,k_tables[i],t2,GENOME_SIZE,GENE_SIZE);
  }
  
  // cross validation
  for( unsigned i=0 ; i<k ; i++ ){
    
    struct base* tmp_learning_table= (struct base*)malloc(sizeof(*tmp_learning_table));
    struct base* tmp_test_table = (struct base*)malloc(sizeof(*tmp_test_table));
    
    tmp_learning_table->instances = 
      (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 ; j<k ; j++ ){
      if(j==i)continue;
      memcpy( (tmp_learning_table->instances)+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 ; j<t1->hdr->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 ; j<t1->hdr->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 ; j<tmp_test_table->no_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 ; i<tmp_learning_table->no_instances ; i++ ){tmp_learning_table->instances[i] = NULL;}
    for( unsigned i=0 ; i<tmp_test_table->no_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);
    printf("f at this packet %d\n",error);  
  }

  fitness_value = (((float)error) / t1->no_instances)*100;// + (((float)tree_size)/k)
  
  for( unsigned i=0 ; i<k ; i++ ){
    ba_delete(k_tmp_tables[i]);
  }
  return fitness_value;
}

115 116 117 118
\end

\At the beginning of each generation function:
{
119
  generate_k_fold(K,packets_size,t1,k_tables,t2);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}
\end

\At the end of each generation function:
\end

\At each generation before reduce function:
\end

\GenomeClass::display:
\end


\User declarations :
#include <base.h>
#include <genome.h>
#include <tree.h>
#include <omp.h>
#include <assert.h>


#define K 5
#define GENE_SIZE 4
Ogier Maitre's avatar
Ogier Maitre committed
143
#define GENOME_SIZE 128
144 145

 
146
float pMutPerGene=0.05;
147 148 149 150 151 152 153
float pMutDesCard = 0.05;
float pMutDesThre = 0.5;

struct base* t1 = NULL;
struct base* t2 = NULL;

float* uniq_instances[2];
154
float* mutation_step;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
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());

177 178
  t1 = ba_postgres_load_ilot(4);
  t2 = ba_postgres_load_batiment(4);
Ogier Maitre's avatar
Ogier Maitre committed
179
  ba_sort_instances(t1,0,0);
180
  ba_set_links(t1,t2);
181

182 183 184 185 186
  for( unsigned i=0 ; i<t1->hdr->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);
Ogier Maitre's avatar
Ogier Maitre committed
187
  
188 189 190
  printf("%d %d\n",uniq_cnt[0],uniq_cnt[1]);

  // allocating K packets 
191
  generate_k_fold(K,packets_size,t1,k_tables,t2);
Ogier Maitre's avatar
Ogier Maitre committed
192
  
193

Ogier Maitre's avatar
Ogier Maitre committed
194
#if 0
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  // 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 ; j<GENE_SIZE-1 ; j++ )
    i->x[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
Ogier Maitre's avatar
Ogier Maitre committed
212
  //exit(-1);
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

  // here we can compute the extremum of the annex table
  float* mins = new float[t2->hdr->no_attributes-2];
  float* maxs = new float[t2->hdr->no_attributes-2];

  unsigned no_usefull_attributs = t2->hdr->no_attributes-2;

  mutation_step = new float[no_usefull_attributs];

  for( unsigned i=1 ; i<t2->hdr->no_attributes-1 ; i++ ){
    mins[i-1] = INFINITY;
    maxs[i-1] = -INFINITY;
    for( unsigned j=0 ; j<t2->no_instances ; j++ ){
      if( t2->instances[j][i] < mins[i-1] )
	mins[i-1] = t2->instances[j][i];
      if( t2->instances[j][i] > maxs[i-1] )
	maxs[i-1] = t2->instances[j][i];

    }
  }

  for( unsigned i=0 ; i< no_usefull_attributs ; i++ ){
    mutation_step[i] = (maxs[i]-mins[i])/50;
    printf("%d : M : %f m : %f ms %f\n",i,maxs[i],mins[i],mutation_step[i]);
  }

  delete mins;
  delete maxs;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
}
\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 ; i<GENOME_SIZE ; i+=GENE_SIZE ){
    for( unsigned j=0 ; j<GENE_SIZE-1 ; j++ ){
      printf("a_%d <= %3.0f ,",j,best->x[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);

Ogier Maitre's avatar
Ogier Maitre committed
266 267
  ba_to_arff(tmp_table,"best_table.arff");

268 269 270 271 272 273 274 275 276 277 278 279 280
  printf("depth of resulting tree %d\n",root->tree_depth());

#if 1
  unsigned error = 0;
  for( unsigned i=0 ; i<tmp_table->no_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

281 282 283 284 285 286 287 288 289 290


  struct base* k_tables_f[10];
  unsigned packets_size_f[10];
  

  generate_k_fold(10,packets_size_f,t1,k_tables_f,t2);
  //evaluation_fonction(10,
  printf("validation on 10 folds %f\n",evaluation_fonction(10, best,k_tables_f,packets_size_f));

291 292 293 294 295 296 297 298 299
  delete root;
  ba_delete( tmp_table );
}
\end

\GenomeClass::initialiser : 
{
  for( unsigned i=0; i<GENOME_SIZE ; i+=GENE_SIZE ) {
    
Ogier Maitre's avatar
Ogier Maitre committed
300 301 302
    Genome.x[i] = random(9.,11364.);
    Genome.x[i+1] = random(77.,999.);
    Genome.x[i+2] = random(569.,1000.);
303
    
Ogier Maitre's avatar
Ogier Maitre committed
304
    Genome.x[i+GENE_SIZE-1] = roundf(random(0.,10.));
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
  }
}
\end

\GenomeClass::crossover :
{
  /*
  for (int i=0; i<GENOME_SIZE; i+=GENE_SIZE){
    if( tossCoin(0.5) )
      for( unsigned j=0 ; j<GENE_SIZE ; j++ )
	child.x[i+j] = parent1.x[i+j];
    else
      for( unsigned j=0 ; j<GENE_SIZE ; j++ )
	child.x[i+j] = parent2.x[i+j];
	}*/
  for (int i=0; i<GENOME_SIZE; i+=GENE_SIZE){
    for( unsigned j=0 ; j<GENE_SIZE ; j++ ){
      if( tossCoin(0.5) ){
	child.x[i+j] = parent1.x[i+j];
      }
    }
  }

}
\end

\GenomeClass::mutator : // Must return the number of mutations
{
  int NbMut=0;
  for (int i=0; i<GENOME_SIZE; i+=GENE_SIZE){
    // mutate the cardinality
    if( tossCoin(pMutPerGene) ){
      if( tossCoin(pMutDesCard) ){
	Genome.x[i+GENE_SIZE-1] = 0;
	NbMut++;
      }
      else{
	float value = random_gauss(Genome.x[i+GENE_SIZE-1],2);
	value = (value<0 ? 0 : value); //if value less than 0, then value is 0
	value = (value>10 ? 10 :value); // if value grether than 10 then value is 10
	Genome.x[i+GENE_SIZE-1] = roundf(value);
	NbMut++;
      }
    }

    for( unsigned j=0 ; j<GENE_SIZE-1 ; j++ ){
      if( tossCoin(pMutPerGene) ){
	if( tossCoin(pMutDesThre) ){
Ogier Maitre's avatar
Ogier Maitre committed
353
	  	  Genome.x[i+j] = INFINITY;
354 355
	}
	else{
Ogier Maitre's avatar
Ogier Maitre committed
356
	  float value;
357
	  if( __isinf(Genome.x[i+j]) ) Genome.x[i+j] = random(0,1000);
358 359 360 361
	  value = random_gauss(Genome.x[i+j],mutation_step[i]);
	  Genome.x[i+j] = roundf(value);
	  Genome.x[i+j] = ba_nearest_table_value(t2,j+1,value);
	  
Ogier Maitre's avatar
Ogier Maitre committed
362
	  NbMut++;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	}
      }
    } // for each threshold
    
  }// for each gene
  return NbMut;
}
\end

// The population evaluation.
\Instead evaluation function: 
{
#pragma omp parallel for
  for( int i=0 ; i<popSize ; i++ ){
    population[i]->evaluate();
  }
}
\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 ; i<K ; i++ ){
    k_tmp_tables[i] = table_from_genome(Genome.x,k_tables[i],t2,GENOME_SIZE,GENE_SIZE);
  }
  
  // cross validation
  for( unsigned i=0 ; i<K ; i++ ){
    
    struct base* tmp_learning_table= (struct base*)malloc(sizeof(*tmp_learning_table));
    struct base* tmp_test_table = (struct base*)malloc(sizeof(*tmp_test_table));
    
    tmp_learning_table->instances = 
      (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 ; j<K ; j++ ){
      if(j==i)continue;
      memcpy( (tmp_learning_table->instances)+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 ; j<t1->hdr->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 ; j<t1->hdr->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 ; j<tmp_test_table->no_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 ; i<tmp_learning_table->no_instances ; i++ ){tmp_learning_table->instances[i] = NULL;}
    for( unsigned i=0 ; i<tmp_test_table->no_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);
    
  }

Ogier Maitre's avatar
Ogier Maitre committed
480
  fitness_value = (((float)error) / t1->no_instances)*100;// + (((float)tree_size)/K);
481 482 483 484 485 486 487 488 489
  
  for( unsigned i=0 ; i<K ; i++ ){
    ba_delete(k_tmp_tables[i]);
  }
  return fitness_value;
}
\end

\User Makefile options: 
490
LIBANTLR_DIR = ~/lib/libantlr3c-3.3-SNAPSHOT/
491
CXXFLAGS+=-I/home/maitre/sources/c4.5/include/ -fopenmp 
492
LDFLAGS+= -lpq  -lm  -fopenmp ../c4.5_common/libc45.a $(LIBANTLR_DIR)/.libs/libantlr3c.a
493 494 495
\end

\Default run parameters :        // Please let the parameters appear in this order
Ogier Maitre's avatar
Ogier Maitre committed
496
  Number of generations : 200     	// NB_GEN
497
  Time limit: 0 			// In seconds, 0 to deactivate
498
  Population size : 200			//POP_SIZE
499 500 501 502
  Offspring size : 100 // 40% 
  Mutation probability : 1       // MUT_PROB
  Crossover probability : 1      // XOVER_PROB
  Evaluator goal : minimise      // Maximise
503
  Selection operator: Tournament 2.0
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
  Surviving parents: 100%//percentage or absolute  
  Surviving offspring: 100%
  Reduce parents operator: Tournament 2
  Reduce offspring operator: Tournament 2
  Final reduce operator: Tournament 2
  
  Elitism: Strong			//Weak or Strong
  Elite: 1
  Print stats:1				//Default: 1
  Generate csv stats file:0			
  Generate gnuplot script:0
  Generate R script:0
  Plot stats:0				//Default: 0
\end