static float evaluate_features(int trials, gprcm_population * population, int individual_index, int custom_command) { int i,j,itt,n; float error,diff=0,v,reference,fitness; float dropout_rate = 0.2f; gprcm_function * f = &population->individual[individual_index]; if (custom_command!=0) dropout_rate=0; for (i = 0; i < trials; i++) { /* clear the state */ gprcm_clear_state(f, population->rows, population->columns, population->sensors, population->actuators); /* Randomly pick an example. This discourages any ordering bias */ n = rand_num(&f->program.random_seed)%trials; for (j=1;j<fields_per_example-3;j++) { gprcm_set_sensor(f,j-1, current_data_set[n*fields_per_example+j]); } for (itt = 0; itt < RUN_STEPS; itt++) { /* run the program */ gprcm_run(f, population, dropout_rate, 0, 0); } /* how close is the output to the actual slump? */ for (j=0;j<3;j++) { reference = 0.01f + fabs(current_data_set[n*fields_per_example+ fields_per_example-3+j]); v = 0.01f + fabs(gprcm_get_actuator(f,j, population->rows, population->columns, population->sensors)); error = fabs(v - reference)/reference; diff += error*error; } } diff = (float)sqrt(diff/(float)(trials*3))*100; fitness = 100 - diff; if (fitness<0) fitness=0; return fitness; }
static float evaluate_features(int trials, gprcm_population * population, int individual_index, int mode) { int i,j,itt,n; float diff=0,v,crime_rate,error,fitness; float dropout_rate = 0.0f; gprcm_function * f = &population->individual[individual_index]; for (i = 0; i < trials; i++) { /* clear the state */ gprcm_clear_state(f, population->rows, population->columns, population->sensors, population->actuators); /* randomly choose and example */ n = rand_num(&f->program.random_seed)%trials; /* set the sensor values */ for (j=INITIAL_FIELDS;j<fields_per_example-1;j++) { gprcm_set_sensor(f,j-INITIAL_FIELDS, current_data_set[n*fields_per_example+j]); } for (itt = 0; itt < RUN_STEPS; itt++) { /* run the program */ gprcm_run(f, population, dropout_rate, 0, 0); } /* how close is the output to the actual crime level? */ crime_rate = 0.0001f + current_data_set[n*fields_per_example+fields_per_example-1]; v = fabs(gprcm_get_actuator(f,0,population->rows, population->columns, population->sensors)); error = (v - crime_rate)/crime_rate; diff += error*error; } diff = (float)sqrt(diff/trials)*100; fitness = 100 - diff; if (fitness < 0) fitness=0; return fitness; }
static float evaluate_features(int trials, gprcm_population * population, int individual_index, int custom_command) { int i,j,n,itt; float diff=0,v,quality,error,fitness; float dropout_rate = 0.1f; gprcm_function * f = &population->individual[individual_index]; if (custom_command!=0) dropout_rate=0; for (i = 0; i < trials; i++) { /* clear the state */ gprcm_clear_state(f, population->rows, population->columns, population->sensors, population->actuators); n = i; for (j = 0; j < fields_per_example - 1; j++) { gprcm_set_sensor(f, j, current_data_set[n* fields_per_example+j]); } for (itt=0;itt<RUN_STEPS;itt++) { /* run the program */ gprcm_run(f, population, dropout_rate, 0, 0); } /* how close is the output to the actual quality? */ quality = 0.01f + current_data_set[n*fields_per_example+ fields_per_example-1]; v = 0.01f + fabs(gprcm_get_actuator(f,0, population->rows, population->columns, population->sensors)); error = (v - quality)/quality; diff += error*error; } diff = (float)sqrt(diff/trials)*100; fitness = 100.0f - diff; if (fitness < 0) fitness = 0; return fitness; }
/* an individual produces an artwork */ void paintatron::produce_art(int index, unsigned char * img, int img_width, int img_height, QImage * source_images, int no_of_source_images, QString * source_texts, int no_of_source_texts, char * filename) { int x,y,x2,y2,n=0,itt,c, source_x, source_y, source_index=0,R,G,B,R2=0,G2=0,B2=0,actuator_offset=0,sensor_offset; int source_char_index; float scale_x, scale_y, angle, radius, radius_scale=img_width; int cx = img_width/2; int cy = img_height/2; const char * texts[128]; //const int sensor_inputs = sensor_image_inputs; gprcm_population * pop = &sys.island[0]; gprcm_function * f = &pop->individual[index]; gprcm_clear_state(f, pop->rows, pop->columns, pop->sensors, pop->actuators); for (itt = 0; itt < no_of_source_texts; itt++) { texts[itt] = source_texts[itt].toStdString().c_str(); } /* for every image pixel */ for (y = 0; y < img_height; y++) { for (x = 0; x < img_width; x++, n+=3) { if (no_of_source_images > 0) { source_index = 0; for (itt = 0; itt < image_itterations; itt++) { actuator_offset = itt*actuator_image_inputs; sensor_offset = itt*sensor_image_inputs; /* source image to use */ source_index++; if (source_index > no_of_source_images) source_index -= no_of_source_images; cx += (int)((fmod(fabs(gprcm_get_actuator(f, initial_actuator+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)-0.5f)*img_width/16); if (cx > img_width) cx -= img_width; if (cx < 0) cx += img_width; cy += (int)((fmod(fabs(gprcm_get_actuator(f, initial_actuator+1+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)-0.5f)*img_height/16); if (cy > img_height) cy -= img_height; if (cy < 0) cy += img_height; if ((source_images[source_index].width() > 0) && (source_images[source_index].height() > 0)) { angle = fmod(fabs(gprcm_get_actuator(f, initial_actuator+2+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)*2*3.1415927f; radius = fmod(fabs(gprcm_get_actuator(f, initial_actuator+3+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)*radius_scale; scale_x = ((fmod(fabs(gprcm_get_actuator(f, initial_actuator+4+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)-0.5f)*8); scale_y = ((fmod(fabs(gprcm_get_actuator(f, initial_actuator+5+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)-0.5f)*8); x2 = cx + (int)(radius*sin(angle)); y2 = cy + (int)(radius*cos(angle)); source_x = abs(x2*(int)(source_images[source_index].width())*scale_x/img_width) % source_images[source_index].width(); source_y = abs(y2*(int)(source_images[source_index].height())*scale_y/img_height) % source_images[source_index].height(); /* get the colour at this location */ QColor col = QColor::fromRgb (source_images[source_index].pixel(source_x,source_y)); col.getRgb(&R,&G,&B); /* set sensors to the colour values */ gprcm_set_sensor_complex(f, 5+sensor_offset, R/255.0f, G/255.0f, pop->sensors, pop->actuators, pop->rows, pop->columns); gprcm_set_sensor_complex(f, 6+sensor_offset, G/255.0f, B/255.0f, pop->sensors, pop->actuators, pop->rows, pop->columns); gprcm_set_sensor_complex(f, 7+sensor_offset, B/255.0f, R/255.0f, pop->sensors, pop->actuators, pop->rows, pop->columns); col = QColor::fromRgb (source_images[source_index].pixel((source_x+5) % source_images[source_index].width(),source_y)); col.getRgb(&R2,&G2,&B2); gprcm_set_sensor(f, 8+sensor_offset, (abs(R-R2) + abs(G-G2) + abs(B-B2))/255.0f); col = QColor::fromRgb (source_images[source_index].pixel(source_x,(source_y+5) % source_images[source_index].height())); col.getRgb(&R2,&G2,&B2); gprcm_set_sensor(f, 9+sensor_offset, (abs(R-R2) + abs(G-G2) + abs(B-B2))/255.0f); } } } if (no_of_source_texts > 0) { sensor_offset = image_itterations*sensor_image_inputs; actuator_offset = image_itterations*actuator_image_inputs; source_index = (int)(fmod(fabs(gprcm_get_actuator(f, initial_actuator+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)*no_of_source_texts); source_char_index = (int)(fmod(fabs(gprcm_get_actuator(f, initial_actuator+1+actuator_offset, pop->rows, pop->columns, pop->sensors)),1.0f)*(source_texts[source_index].length()-1)); gprcm_set_sensor(f, 10+sensor_offset, texts[source_index][source_char_index]/128.0f); } gprcm_set_sensor(f, 0, (x*2/(float)img_width)-1.0f); gprcm_set_sensor(f, 1, (y*2/(float)img_height)-1.0f); gprcm_set_sensor(f, 2, n/(float)(img_width*img_height*3)); gprcm_set_sensor(f, 3, (float)sqrt(((x-cx)*(x-cx)) + ((y-cy)*(y-cy)))/img_width); for (itt = 0; itt < run_steps; itt++) { /* run the program */ gprcm_run(f, pop, dropout_rate, 0, 0); } for (c = 0; c < 3; c++) { img[n + c] = (unsigned char)(fmod(fabs(gprcm_get_actuator(f, c, pop->rows, pop->columns, pop->sensors)),1.0f)*255); } radius_scale = img_width - (fmod(fabs(gprcm_get_actuator(f, 3, pop->rows, pop->columns, pop->sensors)),1.0f)*img_width); gprcm_set_sensor(f, 4,radius_scale/img_width); } } write_png_file(filename, img_width, img_height, img); }