double operator()(Individual& ind, RNG& rng, EA& ea) { // get the phenotype (markov network): typename EA::phenotype_type &N = ealib::phenotype(ind, ea); // probably want to reset the RNG for the markov network: N.reset(rng.seed()); // now, set the values of the bits in the input vector: double f=0.0; for(std::size_t i=0; i<128; ++i) { // allocate space for the inputs: std::vector<int> inputs;//(net.ninput_states(), 0); inputs.push_back(rng.bit()); inputs.push_back(rng.bit()); // update the network n times: N.clear(); N.update(inputs); if(*N.begin_output() == (inputs[0] ^ inputs[1])) { ++f; } } // and return some measure of fitness: return f; }
Status GetRandomMatrix(int n, int ix, SpdMatrixT& orig, SpdMatrixT& reduced) { typedef std::mt19937 RNG; // the Mersenne Twister with a popular choice of parameters // http://stackoverflow.com/questions/7114043/random-number-generation-in-c11-how-to-generate-how-do-they-work std::uniform_int_distribution<int> dist(-5, 5); Matrix2D tmp(n); RNG rng; rng.seed(); for( int i = 0; i < n; ++i ) { tmp[i].resize(2*n); for( int j = 0; j < 2*n; ++j ) { tmp[i][j] = dist(rng); } } Size_T ij = 0; for( int i = 0; i < n; ++i ) { for( int j = 0; j <= i; ++j ) { double s = 0.0; for( int k = 0; k < 2*n; ++k ) { s += tmp[i][k] * tmp[j][k]; } orig[ij++] = s; } } Compress(n, ix, orig, reduced); return Status::Success; }
double operator()(Individual& ind, RNG& rng, EA& ea) { typedef sequence_matrix<data::db_type::record_type::vector_type> matrix_type; typedef retina2_iterator<matrix_type> iterator_type; // lazy load of the mnist data (so we don't have to wait unless we // absolutely have to). data::instance()->initialize(get<EVOCADX_TRAIN_FILE>(ea), get<EVOCADX_TEST_FILE>(ea)); // get a markov network: typename EA::phenotype_type &N = ealib::phenotype(ind, ea); int seed = rng.seed(); // save the seed // don't let empty networks play: if(N.ngates() == 0) { return 0.0; } double w=0.0; // accumulated fitness // analyze the lidx records: for(int i=0; i<get<EVOCADX_EXAMINE_N>(ea); ++i) { N.reset(seed); N.clear(); // build a matrix facade for the lix record we're looking at: data::db_type::record_type& R=data::instance()->training[i]; matrix_type M(R.data, data::instance()->training.dim(0), data::instance()->training.dim(1)); // now build a retina iterator over this matrix: iterator_type ci(M, get<EVOCADX_FOVEA_SIZE>(ea), get<EVOCADX_RETINA_SIZE>(ea)); ci.position(M.size1()/2, M.size2()/2); int updates = get<mkv::MKV_UPDATE_N>(ea); for(int j=0; j<updates; ++j) { N.update(ci); ci.move(algorithm::bits2ternary(N.begin_output()), algorithm::bits2ternary(N.begin_output()+2)); } std::vector<int> D; algorithm::range_pair2indices(N.begin_output()+4, N.end_output(), std::back_inserter(D)); if((D.size() == 1) && (D[0] == R.label)) { w += 1.0; } } return w; }
int main( int argc, char** argv ) { RNG rng; std::vector<uint32_t> seeds; seeds.push_back( 0x01234567 ); seeds.push_back( 0xFACEFEED ); seeds.push_back( 0x666DEAD0 ); seeds.push_back( 0xDEADC0DE ); TSP<130> ch130; ch130.LoadFromFile( "data/ch130.tsp" ); printf("===GREEDY===\n"); ch130.Greedy().Debug(); printf("************\n"); TSP<279> a279; a279.LoadFromFile( "data/a280.tsp" ); printf("===GREEDY===\n"); a279.Greedy().Debug(); printf("************\n"); TSP<654> p654; p654.LoadFromFile( "data/p654.tsp" ); printf("===GREEDY===\n"); p654.Greedy().Debug(); printf("************\n"); rng.seed( 0xFACEFEED ); MH::Hormigas::SH<TSP<130>> TSP_sh130( ch130, rng, 5, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1 // factor de evaporación ); MH::Hormigas::SH<TSP<279>> TSP_sh279( a279, rng, 10, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1 // factor de evaporación ); MH::Hormigas::SH<TSP<654>> TSP_sh654( p654, rng, 15, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1 // factor de evaporación ); MH::Hormigas::SHE<TSP<130>> TSP_she130( ch130, rng, 5, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1 // factor de evaporación ); MH::Hormigas::SHE<TSP<279>> TSP_she279( a279, rng, 10, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1 // factor de evaporación ); MH::Hormigas::SHE<TSP<654>> TSP_she654( p654, rng, 15, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1 // factor de evaporación ); MH::Hormigas::SCH<TSP<130>> TSP_sch130( ch130, rng, 5, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1f, // factor de evaporación 0.1f, 0.98f ); MH::Hormigas::SCH<TSP<279>> TSP_sch279( a279, rng, 10, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1f, // factor de evaporación 0.1f, 0.98f ); MH::Hormigas::SCH<TSP<654>> TSP_sch654( p654, rng, 15, // minutos de funcionamiento 20, // numero de hormigas 1, // exponente de feromonas 2, // exponente de heuristica 0.1f, // factor de evaporación 0.1f, 0.98f ); printf("HORMIGAS BASICO\n==================\n"); test( ch130, TSP_sh130, 5, true ); test( a279, TSP_sh279, 5, true ); test( p654, TSP_sh654, 5, true ); printf("HORMIGAS ELITE\n==================\n"); test( ch130, TSP_she130, 5, true ); test( a279, TSP_she279, 5, true ); test( p654, TSP_she654, 5, true ); printf("HORMIGAS COLONIA\n==================\n"); test( ch130, TSP_sch130, 5, true ); test( a279, TSP_sch279, 5, true ); test( p654, TSP_sch654, 5, true ); return 0; }
RNG* JKISS32::split() { RNG* k = new JKISS32(); k->seed(this); return k; }
double operator()(Individual& ind, RNG& rng, EA& ea) { int max_x = get<X_SIZE>(ea,10); int max_y = get<Y_SIZE>(ea,10); int grid_size = max_x * max_y; vector<int> agent_pos (grid_size, -1); vector<int> exec_order (grid_size); double f=0.0; // get the "prototype" phenotype (markov network): typename EA::phenotype_type &N = ealib::phenotype(ind, ea); // start with one agent... vector<typename EA::phenotype_type> as; // for (int q=0; q<get<NUM_START_AGENTS>(ea,1); q++) { as.push_back(N); //grid_size, N); // my agents or networks agent_pos[q] = q; as[q].reset(rng.seed()); } for (int i=0; i<grid_size; ++i) { exec_order[i] = i; } std::vector< std::vector<int> > cell_color(grid_size, std::vector<int>(2, 0)); update_world_stigmergic_communication_N(get<WORLD_UPDATES>(ea,10), agent_pos, exec_order, as, cell_color, ind, rng, ea); double f1_01 = 1.0; double f2_11 = 1.0; double f3_10 = 1.0; // Compute fitness. for (int xy = 0; xy<grid_size; xy++) { // set the input states... int agent_x = floor(xy / max_x); int agent_y = xy % max_x; int p = agent_pos[xy]; if (p == -1) { continue; } if (agent_x < (floor(max_x) / 2 )) { if (((as[p]).output(0) == 1) && ((as[p]).output(1) == 0)) { ++f3_10; } } else if (agent_x >= (floor(max_x) / 2 )) { if ((agent_x ==0) || (agent_y == 0) || (agent_x == (max_x -1)) || (agent_y == (max_y -1))) { if (((as[p]).output(0) == 0) && ((as[p]).output(1) == 1)) { ++f1_01; } } else if (((as[p]).output(0) == 1) && ((as[p]).output(1) == 1)) { ++f2_11; } } } //f = f1_01 * f2_11 * f3_10; double e = f1_01 + f2_11 + f3_10 - 3; f = pow(1.5, e); return f; }
double operator()(Individual& ind, RNG& rng, EA& ea) { int max_x = get<X_SIZE>(ea,10); int max_y = get<Y_SIZE>(ea,10); int grid_size = max_x * max_y; vector<int> agent_pos (grid_size, -1); vector<int> exec_order (grid_size); double f=0.0; // get the "prototype" phenotype (markov network): typename EA::phenotype_type &N = ealib::phenotype(ind, ea); vector<typename EA::phenotype_type> as; // for (int q=0; q<get<NUM_START_AGENTS>(ea,1); q++) { as.push_back(N); //grid_size, N); // my agents or networks agent_pos[q] = q; as[q].reset(rng.seed()); } for (int i=0; i<grid_size; ++i) { exec_order[i] = i; } update_world_N(get<WORLD_UPDATES>(ea,10), agent_pos, exec_order, as, ind, rng, ea); double f1_10 = 1.0; double f1_01 = 1.0; double f1_11 = 1.0; double f2_10 = 1.0; double f2_01 = 1.0; double f2_11 = 1.0; double f3_10 = 1.0; double f3_01 = 1.0; double f3_11 = 1.0; // Compute fitness. for (int xy = 0; xy<grid_size; xy++) { // set the input states... int agent_x = floor(xy / max_x); int agent_y = xy % max_x; int p = agent_pos[xy]; if (p == -1) { continue; } if (agent_x <= (floor(max_x) / 3 )) { if (((as[p]).output(0) == 1) && ((as[p]).output(1) == 0)){ ++f1_10; } else if (((as[p]).output(0) == 0) && ((as[p]).output(1) == 1)){ ++f1_01; } else if (((as[p]).output(0) == 1) && ((as[p]).output(1) == 1)){ ++f1_11; } } else if ((agent_x > (floor(max_x) / 3)) && (agent_x < ((floor(max_x) / 3 * 2)))) { if (((as[p]).output(0) == 1) && ((as[p]).output(1) == 0)){ ++f2_10; } else if (((as[p]).output(0) == 0) && ((as[p]).output(1) == 1)){ ++f2_01; } else if (((as[p]).output(0) == 1) && ((as[p]).output(1) == 1)){ ++f2_11; } } else if (agent_x >= ((floor(max_x) / 3 * 2))) { if (((as[p]).output(0) == 1) && ((as[p]).output(1) == 0)){ ++f3_10; } else if (((as[p]).output(0) == 0) && ((as[p]).output(1) == 1)){ ++f3_01; } else if (((as[p]).output(0) == 1) && ((as[p]).output(1) == 1)){ ++f3_11; } } } accumulator_set<double, stats<tag::max> > fs; double fx = f1_10 * f2_01 * f3_11; fs(fx); fx = f1_10 * f2_11 * f3_01; fs(fx); fx = f1_01 * f2_10 * f3_11; fs(fx); fx = f1_01 * f2_11 * f3_10; fs(fx); fx = f1_11 * f2_01 * f3_10; fs(fx); fx = f1_11 * f2_10 * f3_01; fs(fx); f = boost::accumulators::max(fs); return f; }
double operator()(Individual& ind, RNG& rng, EA& ea) { int max_x = get<X_SIZE>(ea,10); int max_y = get<Y_SIZE>(ea,10); int grid_size = max_x * max_y; vector<int> agent_pos (grid_size, -1); vector<int> exec_order (grid_size); double f=0.0; // get the "prototype" phenotype (markov network): typename EA::phenotype_type &N = ealib::phenotype(ind, ea); vector<typename EA::phenotype_type> as; // for (int q=0; q<get<NUM_START_AGENTS>(ea,1); q++) { as.push_back(N); //grid_size, N); // my agents or networks agent_pos[q] = q; as[q].reset(rng.seed()); } for (int i=0; i<grid_size; ++i) { exec_order[i] = i; } std::vector< std::vector<int> > cell_color(grid_size, std::vector<int>(2, 0)); std::vector<int> apop_count(grid_size,0); update_world_stigmergic_communication_apop_N(get<WORLD_UPDATES>(ea,10), agent_pos, exec_order, as, cell_color, apop_count, ind, rng, ea); int fit_func = get<BODYPLAN>(ea,0) ; switch(fit_func) { case 0: f = body_plan0(grid_size, max_x, max_y, agent_pos, as, ea); break; case 1: f = body_plan1(grid_size, max_x, max_y, agent_pos, as, ea); break; case 2: f = body_plan2(grid_size, max_x, max_y, agent_pos, as, ea); break; case 3: f = body_plan3(grid_size, max_x, max_y, agent_pos, as, ea); break; case 4: f = body_plan4(grid_size, max_x, max_y, agent_pos, as, ea); break; case 5: f = body_plan5(grid_size, max_x, max_y, agent_pos, as, ea); break; case 6: f = body_plan6(grid_size, max_x, max_y, agent_pos, as, ea); break; case 7: f = body_plan7(grid_size, max_x, max_y, agent_pos, as, ea); break; case 8: f = body_plan8(grid_size, max_x, max_y, agent_pos, as, ea); break; case 9: f = body_plan9(grid_size, max_x, max_y, agent_pos, as, ea); break; case 10: f = body_plan10(grid_size, max_x, max_y, agent_pos, as, ea); break; case 11: f = body_plan11(grid_size, max_x, max_y, agent_pos, as, ea); break; case 12: f = body_plan12(grid_size, max_x, max_y, agent_pos, as, ea); break; case 13: f = body_plan13(grid_size, max_x, max_y, agent_pos, as, ea); break; case 14: f = body_plan14(grid_size, max_x, max_y, agent_pos, as, ea); break; case 15: f = body_plan15(grid_size, max_x, max_y, agent_pos, as, ea); break; case 16: f = body_plan16(grid_size, max_x, max_y, agent_pos, as, ea); break; case 17: f = body_plan17(grid_size, max_x, max_y, agent_pos, as, ea); break; case 18: f = body_plan18(grid_size, max_x, max_y, agent_pos, as, ea); break; case 19: f = body_plan19(grid_size, max_x, max_y, agent_pos, as, ea); break; } if (f == 0) { f = 1; } return (f); }