1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 44 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 86 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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 331 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 370 371 372 373 374 375 376 377 378
|
#if (defined FDEBUG) && (!defined ONLINE_JUDGE) #define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) #define DWHILE(cnd, blk) \ while (cnd) blk #define DFOR(ini, cnd, itr, blk) \ for (ini; cnd; itr) blk #else #define DEBUG(fmt, ...) #define DWHILE(cnd, blk) #define DFOR(ini, cnd, itr, blk) #endif
#include <bits/stdc++.h> using namespace std; #define ll long long #define pause system("pause") #define IINF 0x3f3f3f3f #define rg register
const int MAP_SIZ = 10; const float CAN_RATE = 0.5;
const int SUC_CLCT_PT = 10; const int ERR_CLCT_PT = -1; const int HIT_WALL_PT = -5;
const int MOV_LIM = 200; const int POP_CNT = 500; const int GEN_CNT = 1000; const float MUT_RATE = 0.005; const int MAP_REP = 50;
const int THREAD_CNT = 10;
enum GRD_DIR { DIRNONE = -1, CUR, UP, DN, RT, LF }; const int DIR_CNT = 5; enum GRD_OBJ { OBJNONE = -1, EPT, WAL, CAN }; const int OBJ_CNT = 3; enum ACTION { ACTNONE = -1, MV_UP, MV_DN, MV_RT, MV_LF, MV_RND, CLCT_CAN, HALT }; const int ACTION_CNT = 7;
typedef vector<vector<bool>> Map_t; Map_t cur_map;
inline bool is_wall(int x, int y, Map_t* mp) { auto [n, m] = make_pair((*mp).size(), (*mp).front().size()); if (x >= n || x < 0 || y >= m || y < 0) return true; else return false; }
inline GRD_OBJ get_obj_inpos(int x, int y, Map_t* mp) { GRD_OBJ obj; int n = mp->size(); int m = mp->front().size(); if (is_wall(x, y, mp)) obj = WAL; else if ((*mp)[x][y]) obj = CAN; else if (!(*mp)[x][y]) obj = EPT;
return obj; }
struct Obj_in_dir { GRD_DIR dir; GRD_OBJ obj; const bool operator<(Obj_in_dir b) const { if (dir != b.dir) return dir < b.dir; return obj < b.obj; } const bool operator==(Obj_in_dir b) const { return dir == b.dir && obj == b.obj; } const bool operator!=(Obj_in_dir b) const { return dir != b.dir || obj != b.obj; } Obj_in_dir(GRD_DIR _dir, GRD_OBJ _obj) : dir(_dir), obj(_obj) {}
Obj_in_dir(int x, int y, GRD_DIR _dir, Map_t* mp) { dir = _dir; switch (dir) { case CUR: obj = get_obj_inpos(x, y, mp); break; case UP: obj = get_obj_inpos(x - 1, y, mp); break; case DN: obj = get_obj_inpos(x + 1, y, mp); break; case RT: obj = get_obj_inpos(x, y + 1, mp); break; case LF: obj = get_obj_inpos(x, y - 1, mp); break; } } Obj_in_dir() { dir = DIRNONE; obj = OBJNONE; } };
struct Srndng { Obj_in_dir objs[5]; const bool operator<(Srndng b) const { for (int i = 0; i < 5; i++) { if (objs[i] != b.objs[i]) return objs[i] < b.objs[i]; } return false; }
Srndng(int x, int y, Map_t* mp) { for (int i = 0; i < 5; i++) { objs[i] = Obj_in_dir(x, y, GRD_DIR(i), mp); } } Srndng() { for (int i = 0; i < 5; i++) objs[i].dir = DIRNONE, objs[i].obj = OBJNONE; } };
typedef map<Srndng, ACTION> Gene_t; typedef pair<Gene_t, float> Gene_res_t; typedef vector<Gene_res_t> Gene_pool_t;
void mp_generator(Map_t* mp, int n = MAP_SIZ, int m = MAP_SIZ) { srand(time(0)); mp->resize(n); for (int i = 0; i < n; i++) { (*mp)[i].resize(m); } for (auto& row : *mp) { for (auto&& unit : row) { unit = (rand() * 1.0 <= CAN_RATE * RAND_MAX); } } }
Map_t* mp_generator(int n = MAP_SIZ, int m = MAP_SIZ) { auto mp = new Map_t(n); mp_generator(mp); return mp; }
void gene_generator_once(Gene_t* ret_gene, Srndng* ret_srndng, GRD_DIR cur_dir) { if (cur_dir >= DIR_CNT) { (*ret_gene)[*ret_srndng] = ACTION(rand() % ACTION_CNT); return; } for (int i = 0; i < OBJ_CNT; i++) { (*ret_srndng).objs[cur_dir] = Obj_in_dir(GRD_DIR(cur_dir), GRD_OBJ(i)); gene_generator_once(ret_gene, ret_srndng, GRD_DIR(cur_dir + 1)); } }
void gene_combine(Gene_t* pa, Gene_t* pb, Gene_t* child) { int cmb_pos = round(double(rand() * 1.0 / RAND_MAX * 1.0) * double(pa->size())); int cur_idx = 0; for (auto [key, val] : *pa) { if (cur_idx > cmb_pos) break; if ((rand() * 1.0 / RAND_MAX * 1.0) <= MUT_RATE) (*child)[key] = ACTION(rand() % (ACTION_CNT)); else (*child)[key] = val; cur_idx++; } cur_idx = 0; for (auto [key, val] : *pb) { if (cur_idx > cmb_pos) { if ((rand() * 1.0 / RAND_MAX * 1.0) <= MUT_RATE) (*child)[key] = ACTION(rand() % (ACTION_CNT)); else (*child)[key] = val; } cur_idx++; } }
Gene_t* gene_combine(Gene_t* pa, Gene_t* pb) { auto child = new Gene_t; gene_combine(pa, pb, child); return child; }
inline pair<int, int> get_pos_after_mv(int x, int y, ACTION mv) { switch (mv) { case MV_UP: return {x - 1, y}; break; case MV_DN: return {x + 1, y}; break; case MV_LF: return {x, y - 1}; break; case MV_RT: return {x, y + 1}; break; case MV_RND: return get_pos_after_mv(x, y, ACTION(rand() % 4)); break; case CLCT_CAN: throw invalid_argument("not a move"); return {x, y}; break; case HALT: throw invalid_argument("not a move"); return {x, y}; break; } }
inline bool is_mov(ACTION act) { return act <= 4; }
int calc_fitness(Gene_t* gene, Map_t* mp) { int cur_x = 0, cur_y = 0; int fit = 0; for (int cur_mov = 1; cur_mov <= MOV_LIM; cur_mov++) { Srndng cur_srnd(cur_x, cur_y, mp); ACTION cur_act = (*gene)[cur_srnd]; if (is_mov(cur_act)) { tie(cur_x, cur_y) = get_pos_after_mv(cur_x, cur_y, cur_act); } if (is_wall(cur_x, cur_y, mp)) { fit += HIT_WALL_PT; auto [n, m] = make_pair((*mp).size(), (*mp).front().size()); if (cur_x < 0) cur_x = 0; if (cur_y < 0) cur_y = 0; if (cur_x >= n) cur_x = n - 1; if (cur_y >= m) cur_y = m - 1; } else if (cur_act == CLCT_CAN) { if ((*mp)[cur_x][cur_y]) { fit += SUC_CLCT_PT; (*mp)[cur_x][cur_y] = false; } else fit += ERR_CLCT_PT; } } return fit; }
void gene_generator(Gene_pool_t* pool, int cnt) { while (cnt--) { Gene_t temp_gene; Srndng temp_srnd; gene_generator_once(&temp_gene, &temp_srnd, GRD_DIR(0)); pool->push_back({temp_gene, 0}); } }
Gene_pool_t cur_pool, temp_pool;
void calc_popfit_mul_th() { thread* calc_fit_th[THREAD_CNT]; const int PER_TH = POP_CNT / THREAD_CNT; for (int i = 0; i < THREAD_CNT; i++) { calc_fit_th[i] = new thread([i]() { for (int j = i * PER_TH; j < (i + 1) * PER_TH; j++) cur_pool[j].second = calc_fitness(&cur_pool[j].first, &cur_map); }); } for (int i = 0; i < THREAD_CNT; i++) { calc_fit_th[i]->join(); } }
vector<int> choose_by_weight(vector<float>& possi, int cnt) { vector<int> ret; ret.reserve(cnt); double tot = 0; for (float cur : possi) { tot += cur; } map<int, int> choose_rg; int lst = 0; for (int i = 0; i < possi.size(); i++) { int len = lround(possi[i] * 1.0 / tot * 1.0 * (RAND_MAX * 1.0)); if (len == 0) continue; choose_rg[lst] = i; lst = lst + len; } choose_rg[IINF] = possi.size(); while (ret.size() < cnt) { int rd = rand(); int rd_idx = (--choose_rg.upper_bound(rd))->second; ret.push_back(rd_idx); } return ret; }
ofstream fileout;
void evolve(int cur_gen) { if (cur_gen != 1) { temp_pool.clear(); }
for_each(cur_pool.begin(), cur_pool.end(), [](Gene_res_t& a) { a.second = 0; });
for (int m = 0; m < MAP_REP; m++) { mp_generator(&cur_map); Map_t temp_map = cur_map; for (int i = 0; i < POP_CNT; i++) { cur_pool[i].second += calc_fitness(&cur_pool[i].first, &cur_map); cur_map = temp_map; } } for (auto& res : cur_pool) { res.second /= (MAP_REP * 1.0); } float tot_fit = 0; float mx_fit = numeric_limits<float>::min(); for (auto cur : cur_pool) { tot_fit += cur.second; mx_fit = max(mx_fit, cur.second); } fileout << mx_fit << ","; cout << cur_gen <<" "<<mx_fit<<"\n";
sort(cur_pool.begin(), cur_pool.end(), [](Gene_res_t& a, Gene_res_t& b) { return a.second < b.second; });
vector<float> possi; const float TOT_ELE = (0.0 + (POP_CNT - 1) * 1.0) * POP_CNT * 1.0 / 2.0; for (int i = 0; i < cur_pool.size(); i++) { possi.push_back(i * 1.0 * sqrt(i * 1.0)); } auto chosen = choose_by_weight(possi, POP_CNT * 2);
temp_pool.clear(); while (chosen.size()) { int fir = chosen.back(); chosen.pop_back(); int sec = chosen.back(); chosen.pop_back(); Gene_t child; gene_combine(&cur_pool[fir].first, &cur_pool[sec].first, &child); DEBUG("fir: %d sec: %d\n", fir, sec); temp_pool.push_back({child, 0}); }
swap(cur_pool, temp_pool); }
int main() { fileout.open("./out"); gene_generator(&cur_pool, POP_CNT); for (int i = 1; i <= GEN_CNT; i++) { evolve(i); } system("python ./plotting.py"); pause; }
|