// [[Rcpp::export]] DataFrame random_impl(DataFrame genome, int length, int n, int seed = 0) { CharacterVector chroms = genome["chrom"] ; NumericVector sizes = genome["size"] ; int nchrom = chroms.size() ; if (seed == 0) seed = round(R::runif(0, RAND_MAX)) ; // seed the generator auto generator = ENG(seed) ; // calculate weights for chrom distribution float mass = sum(sizes) ; NumericVector weights = sizes / mass ; Range chromidx(0, nchrom) ; PDIST chrom_dist(chromidx.begin(), chromidx.end(), weights.begin()) ; // make and store a DIST for each chrom size std::vector< UDIST > size_rngs ; for (int i=0; i<nchrom; ++i) { auto size = sizes[i] ; // sub length to avoid off-chrom coordinates UDIST size_dist(1, size - length) ; size_rngs.push_back(size_dist) ; } CharacterVector rand_chroms(n) ; IntegerVector rand_starts(n) ; for (int i=0; i<n; ++i) { auto chrom_idx = chrom_dist(generator) ; rand_chroms[i] = chroms[chrom_idx] ; UDIST size_dist = size_rngs[chrom_idx] ; auto rand_start = size_dist(generator) ; rand_starts[i] = rand_start ; } IntegerVector rand_ends = rand_starts + length ; return DataFrame::create( Named("chrom") = rand_chroms, Named("start") = rand_starts, Named("end") = rand_ends, Named("stringsAsFactors") = false) ; }
char* packet_gen_socket(int src, int dst, int state, int ctime){ int size; char *payload=NULL; set_ctime(ctime); LOG_I(OTG,"SOCKET :: num_nodes_tx:: %d , seed:: %d \n", g_otg->num_nodes, g_otg->seed); LOG_I(OTG,"SOCKET :: NODE_INFO (Source= %d, Destination= %d,State= %d) ctime %d \n", src, dst, state, otg_info->ctime); LOG_I(OTG,"SOCKET :: INFO_SIM (src=%d, dst=%d, state=%d) application=%d, idt dist =%d, pkts dist= %d\n", src, dst, state, g_otg->application_type[src][dst], g_otg->idt_dist[src][dst][state], g_otg->size_dist[src][dst][state]); LOG_I(OTG,"SOCKET :: Transmission info: idt=%d, simulation time=%d \n", otg_info->idt[src][dst], ctime); // do not generate packet for this pair of src, dst : no app type and/or idt are defined if ((g_otg->application_type[src][dst] == 0) && (g_otg->idt_dist[src][dst][0] == 0)){ LOG_I(OTG,"SOCKET :: Do not generate packet for this pair of src=%d, dst =%d: no app type and/or idt are defined\n", src, dst); return 0; } //pre-config for the standalone if (ctime<otg_info->ptime[src][dst][state]) //it happends when the emulation was finished otg_info->ptime[src][dst][state]=ctime; if (ctime==0) otg_info->idt[src][dst]=0; //for the standalone mode: the emulation is run several times, we need to initialise the idt to 0 when ctime=0 //end pre-config if ((otg_info->idt[src][dst]==(ctime-otg_info->ptime[src][dst][state])) || (otg_info->idt[src][dst]==0)) { LOG_I(OTG,"SOCKET :: Time To Transmit (Source= %d, Destination= %d,State= %d) , (IDT= %d ,simu time= %d, previous packet time= %d) \n", src, dst, state ,otg_info->idt[src][dst], ctime, otg_info->ptime[src][dst][state]); otg_info->ptime[src][dst][state]=ctime; otg_info->idt[src][dst]=time_dist(src, dst, state); // update the idt for the next otg_tx } else { LOG_I(OTG,"SOCKET :: It is not the time to transmit (ctime= %d, previous time=%d, packet idt=%d), node( %d,%d) \n", ctime,otg_info->ptime[src][dst][state], otg_info->idt[src][dst], src, dst); return 0; // do not generate the packet, and keep the idt } size=size_dist(src, dst, state); LOG_I(OTG,"SOCKET :: Generate Packet for (Source= %d, Destination= %d,State= %d) , pkt size dist= %d, simu time= %d ,packet size=%d \n", src, dst, state, g_otg->size_dist[src][dst][state], otg_info->ctime, size); if (size>(5* sizeof(int))) size=size-(5* sizeof(int)); else size=(5* sizeof(int))+10; payload=payload_pkts(size); return(payload); }
int main() { mt19937 rng; uniform_int_distribution<int> size_dist(0, 40); for(int t = 0; t < 10000; ++t) { int n = size_dist(rng); uniform_int_distribution<int> edgecount_dist(0, 5 * n); int edgecount = edgecount_dist(rng); if(n < 2) edgecount = 0; uniform_int_distribution<int> weight_dist(0, 12); vector<pair<double, pair<int, int> > > edges; for(int i = 0; i < edgecount; ++i) { uniform_int_distribution<int> vertex_dist(0, n - 1); int v1 = vertex_dist(rng); int v2 = v1; while(v1 == v2) v2 = vertex_dist(rng); edges.push_back(pair<double, pair<int, int> >( weight_dist(rng), pair<int, int>(v1, v2) )); } vector<vector<int> > graph(n); for(int i = 0; i < edgecount; ++i) { int v1 = edges[i].second.first; int v2 = edges[i].second.second; graph[v1].push_back(v2); graph[v2].push_back(v1); } pair<vector<vector<int> >, double> p = kruskal(n, edges); if(!sameComponents(graph, p.first)) fail(); for(int v = 0; v < n; ++v) { failIfNotTreeComponent(p.first, v); } double weight = 0.0; for(int x = 0; x < n; ++x) { for(int i = 0; i < p.first[x].size(); ++i) { int y = p.first[x][i]; double best = 1.0 / 0.0; for(int ei = 0; ei < edges.size(); ++ei) { if( (edges[ei].second.first == x && edges[ei].second.second == y) || (edges[ei].second.first == y && edges[ei].second.second == x) ) { best = min(best, edges[ei].first); } } weight += best; } } if(weight != 2.0 * p.second) fail(); } return 0; }
int main(int argc, char* argv[]) { // スコープ長いけれど…… std::string const output_filename = "report_movement"; if(argc != 2 || argc != 4)\ if(argc != 2 && argc != 4) { std::cout << "Usage: " << argv[0] << " 試行回数 [分割数タテ 分割数ヨコ]" << std::endl; //std::quick_exit(-1); return -1; } bool const is_fixed = argc == 4; int const try_num = std::atoi(argv[1]); auto const split_num = is_fixed ? std::make_pair(std::atoi(argv[3]), std::atoi(argv[2])) : std::make_pair(-1, -1); std::mt19937 mt; std::uniform_int_distribution<int> select_dist(1, 20); // 選択可能回数生成器 std::uniform_int_distribution<int> size_dist (2, 16); // サイズ生成器 std::uniform_int_distribution<int> cost_dist (1, 50); // コスト生成器 for(int n=0; n<try_num; ++n) { int const problem_id = 0; // 仮 std::string const player_id = "0"; //仮 std::pair<int,int> const size = is_fixed ? split_num : std::make_pair(size_dist(mt), size_dist(mt)); int const selectable = select_dist(mt); int const cost_select = cost_dist(mt); int const cost_change = cost_dist(mt); std::vector<std::vector<point_type>> block( size.second, std::vector<point_type>(size.first, {-1, -1}) ); std::uniform_int_distribution<int> x_dist(0, size.first - 1); std::uniform_int_distribution<int> y_dist(0, size.second - 1); for(int i=0; i<size.second; ++i) { for(int j=0; j<size.first; ++j) { int x, y; do { x = x_dist(mt); y = y_dist(mt); } while(!(block[y][x] == point_type{-1, -1})); block[y][x] = point_type{j, i}; } } // テストデータ発行 question_data question{ problem_id, player_id, size, selectable, cost_select, cost_change, block }; // 実行から評価まで algorithm algo; algo.reset(question); // テストデータのセット // エミュレータ起動 test_tool::emulator emu(question); // 評価保存先ファイルを開き,問題データを書き込む std::ofstream ofs(output_filename + std::to_string(n) + ".csv"); ofs << "[Question]\n"; for(auto const& line : block) { for(auto const& elem : line) { ofs << R"(")" << elem.x << "," << elem.y << R"(", )"; } ofs << "\n"; } ofs << "\n"; ofs << "[Answer]\n"; ofs << "回数,間違い位置数,コスト\n"; int counter = 0; while(auto first_result = algo.get()) // 解答が受け取れる間ループ { // 評価 auto const evaluate = emu.start(first_result.get()); // 書出 ofs << counter+1 << "," <<evaluate.wrong << "," << evaluate.cost << "\n"; ++counter; // テストなので解答回数をカウントする必要あるかなと } } return 0; }