card.ez 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*_________________________________________________________

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


\User functions:

\end

\At the beginning of each generation function:
{
15
  generate_k_fold(K,packets_size,t1,k_tables,t2);
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
}
\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 3
#define GENOME_SIZE 32

 
float pMutPerGene=0.1;
float pMutDesCard = 0.05;
Ogier Maitre's avatar
Ogier Maitre committed
44
float pMutDesThre = 0.05;
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

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_train();
  t2 = ba_postgres_load_car();
  ba_set_links(t1,t2);
  
  //printf("+ %d - %d\n",t1->class_repartition[1],t1->class_repartition[0]);
  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);

  printf("%d %d\n",uniq_cnt[0],uniq_cnt[1]);

  // allocating K packets 
86
  generate_k_fold(K,packets_size,t1,k_tables,t2);
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

#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 ; 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
  //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 ; 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);

  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

  
  delete root;
  ba_delete( tmp_table );
}
\end

\GenomeClass::initialiser : 
{
  for( unsigned i=0; i<GENOME_SIZE ; i+=GENE_SIZE ) {
    for( unsigned j=0 ; j<GENE_SIZE-1 ; j++ ){
      Genome.x[i+j] = random(0,100);
    }
    Genome.x[i+GENE_SIZE-1] = random(0,5);
  }
}
\end

\GenomeClass::crossover :
{
Ogier Maitre's avatar
Ogier Maitre committed
164 165
  
#if 0
166 167 168 169 170 171 172
  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];
Ogier Maitre's avatar
Ogier Maitre committed
173 174 175 176 177 178 179 180
    
  }
#else
  for (int i=0; i<GENOME_SIZE; i+=1){
    if( tossCoin(0.5) )
	child.x[i] = parent1.x[i];
    else
	child.x[i] = parent2.x[i];
181
  }
Ogier Maitre's avatar
Ogier Maitre committed
182
#endif
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 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
}
\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) ){
	  Genome.x[i+j] = INFINITY;
	}
	else{
	  if( __isinf(Genome.x[i+j]) ) Genome.x[i+j] = random(0,100);
	  float value = random_gauss(Genome.x[i+j],2);
	  Genome.x[i+j] = roundf(value);
	}
	NbMut++;
      }
    } // 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
331
  fitness_value = (((float)error) / t1->no_instances)*100;// + ((float)tree_size)/K;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

  for( unsigned i=0 ; i<K ; i++ ){
    ba_delete(k_tmp_tables[i]);
  }
  //exit(-1);
  return fitness_value;
}
\end

\User Makefile options: 
CXXFLAGS+=-I/home/maitre/sources/c4.5/include/ -fopenmp 
LDFLAGS+= -lpq  -lm  -fopenmp ../c4.5_common/libc45.a 
\end

\Default run parameters :        // Please let the parameters appear in this order
  Number of generations : 100     	// NB_GEN
  Time limit: 0 			// In seconds, 0 to deactivate
  Population size : 200			//POP_SIZE
  Offspring size : 100 // 40% 
  Mutation probability : 1       // MUT_PROB
  Crossover probability : 1      // XOVER_PROB
  Evaluator goal : minimise      // Maximise
  Selection operator: Tournament 6.0
  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