void Population::Evolve(size_t elitism_count) { std::vector<Individual> evolved_pop(pop_.size()); std::vector<size_t> elites = Elitism(elitism_count); /* Choosing elite individuals uses raw fitness */ for (size_t j = 0; j < elitism_count; ++j) { evolved_pop[j] = pop_[elites[j]]; } for (size_t j = elitism_count; j < pop_.size(); ++j) { size_t p1 = SelectIndividual(); size_t p2; do { p2 = SelectIndividual(); } while (p2 == p1); Individual parent1(pop_[p1]); Individual parent2(pop_[p2]); Crossover(&parent1, &parent2); evolved_pop[j] = parent1; evolved_pop[j].Mutate(mutation_rate_); } this->pop_ = evolved_pop; CalculateFitness(); }
TEST(EDGE, Hash) { std::vector<ade::DimT> slist = {94, 78, 70, 82, 62, 29, 38}; ade::Opcode mock_code{"MOCK_EDGE", 435}; ade::Opcode mock_code2{"MOCK_EDGE2", 436}; ade::Shape shape(slist); ade::TensptrT parent(new MockTensor(shape)); ade::TensptrT parent2(new MockTensor(shape)); ade::TensptrT child(new MockTensor(shape)); ade::TensptrT child2(new MockTensor(shape)); ade::Edge expired_edge; ade::Edge expired_edge2 = { ade::TensrefT(), ade::TensrefT(), ade::Opcode{"SOMETHING", 123}, }; ASSERT_TRUE(expired_edge.expired()); ASSERT_TRUE(expired_edge2.expired()); std::unordered_set<ade::Edge,ade::EdgeHash> edges = { ade::Edge{parent, child, mock_code}, ade::Edge{parent2, child, mock_code}, ade::Edge{parent, child2, mock_code}, ade::Edge{parent2, child2, mock_code}, ade::Edge{parent, child, mock_code2}, ade::Edge{parent, child, mock_code}, expired_edge, expired_edge2, }; EXPECT_EQ(6, edges.size()); }
TEST(EDGE, Equality) { std::vector<ade::DimT> slist = {94, 78, 70, 82, 62, 29, 38}; ade::Opcode mock_code{"MOCK_EDGE", 435}; ade::Opcode mock_code2{"MOCK_EDGE2", 436}; ade::Shape shape(slist); ade::TensptrT parent(new MockTensor(shape)); ade::TensptrT parent2(new MockTensor(shape)); ade::TensptrT child(new MockTensor(shape)); ade::TensptrT child2(new MockTensor(shape)); ade::Edge orig_edge{parent, child, mock_code}; ade::Edge edge{parent2, child, mock_code}; ade::Edge edge2{parent, child2, mock_code}; ade::Edge edge3{parent2, child2, mock_code}; ade::Edge edge4{parent, child, mock_code2}; ade::Edge expired_edge; ASSERT_TRUE(expired_edge.expired()); ade::Edge edge_eq{parent, child, mock_code}; EXPECT_FALSE(orig_edge == edge); EXPECT_FALSE(orig_edge == edge2); EXPECT_FALSE(orig_edge == edge3); EXPECT_FALSE(orig_edge == edge4); EXPECT_FALSE(orig_edge == expired_edge); EXPECT_TRUE(orig_edge == edge_eq); }
int main () { printf ("Results of node_bind3_test:\n"); Node::Pointer node (Node::Create ()), parent1 (Node::Create ()), parent2 (Node::Create ()); node->SetPosition (1, 2, 3); node->SetOrientation (degree (90.0f), 1, 0, 0); node->SetScale (-1, 2, 3); parent1->SetPosition (-1, -5, 2); parent1->SetOrientation (degree (90.0f), 0, 1, 0); parent1->SetScale (-1, -2, 1); parent1->SetPosition (8, 2, 3); parent1->SetOrientation (degree (90.0f), 0, 0, 1); parent1->SetScale (2, 3, 1); node->BindToParent (*parent1); printf ("before\n position: "); dump_position (*node); printf ("\n orientation: "); dump_orientation (*node); printf ("\n scale: "); dump_scale (*node); printf ("\n"); node->BindToParent (*parent2, NodeBindMode_AddRef, NodeTransformSpace_World); printf ("after rebind\n position: "); dump_position (*node); printf ("\n orientation: "); dump_orientation (*node); printf ("\n scale: "); dump_scale (*node); printf ("\n"); node->Unbind (NodeTransformSpace_World); printf ("after unbind\n position: "); dump_position (*node); printf ("\n orientation: "); dump_orientation (*node); printf ("\n scale: "); dump_scale (*node); printf ("\n"); return 0; }
int main(int argc, char **argv){ mpi::environment env(argc, argv); mpi::communicator world; std::srand(135+2*world.rank()); int nodecount, max_i, max_j, max_k; if(world.rank()==0) { std::cout << "usage: ./individual_test.exe testfilename\n"; FILE* nodefile = fopen(argv[1], "r"); if (nodefile==NULL) { std::cout << "error opening file "<<argv[1] << "\n"; return 1; } char oneline[100]; int line_count=0; while( fgets(oneline,100,nodefile)!=NULL){ if(oneline[0]=='#' || oneline[0]=='\n'){ std::cout << oneline; continue; }else{ line_count+=1; if(line_count==1){ sscanf(oneline,"%d%d%d%d",&nodecount,&max_i,&max_j,&max_k); }else{ gampi_nodelist.push_back((nodeid) atoi(oneline)); } } } } broadcast(world, max_i, 0); broadcast(world, max_j, 0); broadcast(world, max_k, 0); broadcast(world, gampi_nodelist, 0); gampi_domain=Domain(max_i, max_j, max_k); Individual a; if(world.rank()==0) a.show(); Individual b(true); if(world.rank()==0) b.show(); if(world.rank()==0) b.mutate(); if(world.rank()==0) b.show(); broadcast(world, b, 0); char s[16]; sprintf(s,"bcst %d",world.rank()); b.show(s); Individual c(b, true); sprintf(s,"mutt %d",world.rank()); c.show(s); if(world.rank()==0) { std::cout << "Begin crossover test \n"; Individual parent1(true); parent1.show((char*) "parent1"); Individual parent2(true); parent2.show((char*) "parent2"); Individual pt2mind=parent2; pt2mind.mindiff(parent1); pt2mind.show((char*) "pt2mind"); Individual child1, child2; Individual::crossover(parent1, parent2, child1, child2); child1.show((char*) "child1 "); child2.show((char*) "child2 "); } } // end main
int main(int argc, char* argv[]) { typedef double DecisionVariable_t; typedef std::mt19937 RNG_t; unsigned int seed = 1; double eta = 10; double crossover_probability = 1.0; double eps = 0.00001; double proportion_crossed = 1.0; RNG_t rng(seed); DebsSBXCrossover<RNG_t> crossover_tester(rng, eta, crossover_probability, eps, proportion_crossed); int number_dvs = 1; //number of decision variables double min_value = -1.0; double max_value = 8.0; std::vector<double> lower_bounds(number_dvs, min_value); std::vector<double> upper_bounds(number_dvs, max_value); std::vector<int> lower_bounds_i; std::vector<int> upper_bounds_i; std::vector<MinOrMaxType> min_or_max(1, MINIMISATION); // std::vector<DecisionVariable_t> parent1_dv_values {2}; // std::vector<DecisionVariable_t> parent2_dv_values {5}; ProblemDefinitions defs(lower_bounds, upper_bounds,lower_bounds_i, upper_bounds_i, min_or_max, 0); Individual parent1(defs); Individual parent2(defs); std::vector<double> results; int num_samples = 1000000; for (int i = 0; i < num_samples; ++i) { Individual child1(parent1); Individual child2(parent2); crossover_tester.crossover_implementation(child1, child2); results.push_back(child1.getRealDV(0)); results.push_back(child2.getRealDV(0)); // std::cout << child1[0] << std::endl; // std::cout << child2[0] << std::endl; } #ifdef WITH_VTK // plot results. // Set up a 2D scene, add an XY chart to it VTK_CREATE(vtkContextView, view); view->GetRenderer()->SetBackground(1.0, 1.0, 1.0); view->GetRenderWindow()->SetSize(400, 300); VTK_CREATE(vtkChartXY, chart); view->GetScene()->AddItem(chart); // Create a table with some points in it... VTK_CREATE(vtkTable, table); VTK_CREATE(vtkIntArray, arrBin); arrBin->SetName("decision variable value"); table->AddColumn(arrBin); VTK_CREATE(vtkIntArray, arrFrequency); arrFrequency->SetName("Frequency"); table->AddColumn(arrFrequency); int num_bins = 50; table->SetNumberOfRows(num_bins); std::vector<int> frequency_count(num_bins); // std::vector<std::string> bin_names(num_bins); std::vector<int> bin_names(num_bins); for (int i = 0; i < num_samples; ++i) { frequency_count[int((results[i] - min_value) / (max_value - min_value) * num_bins)]++; } for (int i = 0; i < num_bins; ++i) { // bin_names[i] = std::to_string((double(i) / double(num_bins)) * (max_value-min_value) + min_value); bin_names[i] = i; } for (int i = 0; i < num_bins; i++) { table->SetValue(i,0,bin_names[i]); table->SetValue(i,1,frequency_count[i]); } // Add multiple line plots, setting the colors etc vtkPlot *line = 0; line = chart->AddPlot(vtkChart::BAR); #if VTK_MAJOR_VERSION <= 5 line->SetInput(table, 0, 1); #else line->SetInputData(table, 0, 1); #endif line->SetColor(0, 255, 0, 255); //Finally render the scene and compare the image to a reference image view->GetRenderWindow()->SetMultiSamples(0); view->GetInteractor()->Initialize(); view->GetInteractor()->Start(); #endif }