Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #ifdef __WIN32
- #include <windows.h>
- #else
- #include <unistd.h>
- void Sleep(long ms) {
- timespec t;
- t.tv_sec = ms/1000;
- t.tv_nsec = (ms%1000) * 1000000;
- nanosleep(&t,NULL);
- }
- #endif
- using namespace std;
- /*
- char labirynt[][64]={
- "##########################",
- "# ################",
- "# ####### ## ###########",
- "# ###### # #### #",
- "# ####### ### ######### #",
- "# ##### # ### #",
- "######### # # ## ########",
- "########## # #######",
- "########### ### ########",
- "##########################",
- "##########################"
- };
- */
- char labirynt[][64]={
- "##########################",
- "# # ################",
- "### # ### ## ###########",
- "## # ## # #### #",
- "## ## ### ### ######### #",
- "## ## # ### #",
- "######### # # ## ########",
- "########## # #######",
- "########### ### ########",
- "##########################",
- "##########################"
- };
- short di[] = {0, 1, 0,-1};
- short dj[] = {1, 0,-1, 0};
- int muta_rate = 18; // (0, 100)
- typedef pair<pair<int,int>,int> situa;
- set<situa> odwiedzone;
- const bool gumowe_sciany = false;
- int test(const char* genom, int i, int j, int df) {
- situa syt(make_pair(i,j),df*2+(labirynt[i][j]=='*'));
- if (odwiedzone.find(syt)!=odwiedzone.end()) return 0;
- odwiedzone.insert(syt);
- int reward = 1;
- if (labirynt[i][j]=='#') {
- if (gumowe_sciany) {
- df ^= 2; // odbij
- reward = -2; // -2: nadal bardziej oplaca sie nie wchodzic w sciany XD; -1: bez różnicy
- return reward + test(genom, i+di[df], j+dj[df], df);
- }
- return 0;
- }
- df ^= 2;
- int sytuac=(labirynt[i][j]=='*')*2 + (labirynt[i+di[df&3]][j+dj[df&3]]=='*');
- for (int d=1; d<4; d++) {
- #ifdef KUPA
- sytuac *= 3;
- #else
- sytuac *= 2;
- #endif
- switch (labirynt[i+di[(d+df)&3]][j+dj[(d+df)&3]]) {
- case ' ':
- sytuac += 0;
- break;
- case '#':
- sytuac += 1;
- break;
- case '*':
- sytuac += 2;
- break;
- default:
- break;
- }
- }
- sytuac = genom[sytuac]-'0';
- sytuac += sytuac&4;
- df += sytuac;
- df &= 11;
- df |= (df&8)>>1;
- if (labirynt[i][j] == '-' || labirynt[i][j] == '*')
- labirynt[i][j] = '+';
- else if (labirynt[i][j] == '+')
- labirynt[i][j] = '%';
- else if (labirynt[i][j] == '%')
- labirynt[i][j] = '&';
- else {
- labirynt[i][j] = '-';
- reward += 60;
- }
- if (df&4)
- labirynt[i][j] = '*';
- return reward+test(genom, i+di[df&3], j+dj[df&3], df&7);
- }
- char *oldgenome, *newgenome;
- #ifdef KUPA
- const int genomesiz = 2*2 * 3*3*3;
- #else
- const int genomesiz = 2*2 * 2*2*2;
- #endif
- int main(int argc, char* argv[]) {
- oldgenome = (char*)malloc(genomesiz+3);
- newgenome = (char*)malloc(genomesiz+3);
- int maxfitness=1, fitness=0, newfitness, i, j, d, ile=0, last_ile=0;
- srand(33);
- if (argc==2) {
- ifstream ifile(argv[1]);
- if (!ifile)
- return!puts("Nie można otworzyć pliku");
- else {
- ifile.getline(oldgenome, genomesiz+3);
- i=0;
- while (i < sizeof(labirynt)/sizeof(*labirynt)-1 && !ifile.eof()) {
- ifile.getline(newgenome, genomesiz+3);
- if (newgenome[0]=='#')
- strcpy(labirynt[i++], newgenome);
- }
- }
- for (i=0; oldgenome[i]>=' '; i++);
- }
- else
- for (i=0; i<genomesiz; i++)
- oldgenome[i] = '0';
- strcpy(oldgenome+i, "\r\n");
- // calculate max fitness and fix the box here
- for (i=0; i<sizeof(labirynt)/sizeof(*labirynt); i++) {
- for (j=0; labirynt[i][j]>=' '; j++)
- if (labirynt[i][j] == ' ') {
- for (d=0; d<4; d++)
- if (labirynt[i+di[d]][j+dj[d]] == ' ')
- maxfitness++;
- maxfitness += 60;
- }
- strcpy(labirynt[i]+j, "\r\n");
- }
- // end: calculate max fitness
- for (int iter=0; fitness<maxfitness; iter++,last_ile++) {
- #ifdef __WIN32
- system("cls");
- #else
- fputs("\x1b[H\x1b[J",stdout);
- #endif
- // XXX kinda cheating here:
- if (last_ile>200) { muta_rate = rand()%(20-2) + 1; last_ile = 0; }
- strcpy(newgenome, oldgenome);
- // mutation here
- if (iter) // ensure we do not modify initial genome
- for (i=0; i<genomesiz; i++)
- if (rand()%100 < muta_rate)
- newgenome[i] = '0' + rand()%4; // %8 dla robienia kupy
- // end: mutation
- odwiedzone.clear();
- newfitness = test(newgenome, 1, 1, 0);
- if (newfitness > fitness) {
- ile++;
- swap(oldgenome, newgenome);
- fitness = newfitness;
- last_ile = 0;
- }
- printf("iteracja %d (%d mutacji ok, fitness=%d/%d[%d%%]); ostatnio=%d; muta_rate=%d%%\n",
- iter, ile, fitness, maxfitness, fitness*100/maxfitness, last_ile, muta_rate);
- for (i=0; i<sizeof(labirynt)/sizeof(*labirynt); i++) {
- fputs(labirynt[i], stdout);
- for (j=0; labirynt[i][j]; j++)
- if (labirynt[i][j] != '#' && labirynt[i][j] > ' ')
- labirynt[i][j] = ' ';
- }
- printf("\n%s%s", oldgenome, newgenome);
- Sleep(200);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment