Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.81 KB | None | 0 0
  1. #include <iostream>
  2. #include<ctime>
  3. #include<cstdlib>
  4. #include<fstream>
  5. using namespace std;
  6.  
  7. #define RIGHE 20
  8. #define COLONNE 20
  9.  
  10. #define n_input 5
  11. #define n_ps 20
  12. #define n_ss 3
  13.  
  14. struct blocco{
  15. int x;
  16. int y;
  17. struct blocco * prossimo;
  18. };
  19.  
  20. struct coordinata{
  21. int x;
  22. int y;
  23. };
  24.  
  25. struct rete{
  26. int griglia[RIGHE][COLONNE];
  27.  
  28. float sinapsi_ps [n_input][n_ps];
  29. float sinapsi_ss [n_ps][n_ss];
  30.  
  31. float neuroni_input[n_input];
  32. float neuroni_ps [n_ps];
  33. float neuroni_ss [n_ss];
  34.  
  35. struct blocco* snake;
  36. struct coordinata velocita;
  37. struct coordinata mela;
  38. bool game_over = false;
  39. int punteggio=0;
  40. int mosse=0;
  41. };
  42.  
  43. struct rete reti[100];
  44.  
  45. float RandomFloat(float a, float b) {
  46.  
  47. float random = ((float) rand()) / (float) RAND_MAX;
  48. float diff = b - a;
  49. float r = random * diff;
  50. return a + r;
  51. }
  52.  
  53. void analizza_input(struct rete& reti){
  54. int x=-1;
  55. int y;
  56.  
  57. //trova la testa
  58. for(int i=0;i<20 && x==-1;i++)
  59. for(int j=0;j<20 && x==-1;j++)
  60. if(reti.griglia[i][j]==9){
  61. x=i;
  62. y=j;
  63. }
  64. //inizializza i neuroni posizione della mela:
  65.  
  66.  
  67. //assegna al neurone 0 cosa si trova davanti al serpente, all 1 a sinstra al 2 a destra. Il neurone 3 e 1 se la mela si trova avanti -1 se dietro, il neurone 4 e 1 se la mela e a sinistra -1
  68. if(reti.velocita.x==-1){
  69. reti.neuroni_input[0] = reti.griglia[x-1][y];
  70. reti.neuroni_input[1] = reti.griglia[x][y-1];
  71. reti.neuroni_input[2] = reti.griglia[x][y+1];
  72.  
  73. if(reti.mela.x < x)
  74. reti.neuroni_input[3] = 1;
  75. else if(reti.mela.x>x)
  76. reti.neuroni_input[3] = -1;
  77.  
  78. if(reti.mela.y > y)
  79. reti.neuroni_input[4] = -1;
  80. if(reti.mela.y < y){
  81. reti.neuroni_input[4] = 1;
  82. }
  83.  
  84. }
  85.  
  86. else if(reti.velocita.x==1){
  87. reti.neuroni_input[0] = reti.griglia[x+1][y];
  88. reti.neuroni_input[1] = reti.griglia[x][y+1];
  89. reti.neuroni_input[2] = reti.griglia[x][y-1];
  90.  
  91. if(reti.mela.x > x)
  92. reti.neuroni_input[3] = 1;
  93. else if(reti.mela.x < x)
  94. reti.neuroni_input[3] = -1;
  95. if(reti.mela.y < y)
  96. reti.neuroni_input[4] = -1;
  97. if(reti.mela.y > y)
  98. reti.neuroni_input[4] = 1;
  99.  
  100. }
  101.  
  102. else if(reti.velocita.y==-1){
  103. reti.neuroni_input[0] = reti.griglia[x][y-1];
  104. reti.neuroni_input[1] = reti.griglia[x+1][y];
  105. reti.neuroni_input[2] = reti.griglia[x-1][y];
  106.  
  107. if(reti.mela.y < y)
  108. reti.neuroni_input[3] = 1;
  109. else if(reti.mela.y > y)
  110. reti.neuroni_input[3] = -1;
  111. if(reti.mela.x < x)
  112. reti.neuroni_input[4] = -1;
  113. if(reti.mela.x > x)
  114. reti.neuroni_input[4] = 1;
  115.  
  116. }
  117.  
  118. else if(reti.velocita.y==+1){
  119. reti.neuroni_input[0] = reti.griglia[x][y+1];
  120. reti.neuroni_input[1] = reti.griglia[x-1][y];
  121. reti.neuroni_input[2] = reti.griglia[x+1][y];
  122.  
  123.  
  124. if(reti.mela.y > y)
  125. reti.neuroni_input[3] = 1;
  126. else if(reti.mela.y < y)
  127. reti.neuroni_input[3] = -1;
  128.  
  129. if(reti.mela.x > x)
  130. reti.neuroni_input[4] = -1;
  131. if(reti.mela.x < x)
  132. reti.neuroni_input[4] = 1;
  133.  
  134. }
  135.  
  136.  
  137. return;
  138. }
  139.  
  140. void iniz_reti_rand(struct rete reti[]){
  141. for(int i=0;i<100;i++){
  142. for(int t=0;t<5;t++)
  143. for(int k=0;k<20;k++)
  144. reti[i].sinapsi_ps[t][k] = RandomFloat(-1,1);
  145.  
  146. for(int r=0;r<20;r++)
  147. for(int j=0;j<3;j++)
  148. reti[i].sinapsi_ss[r][j] = RandomFloat(-1,1);
  149. }
  150. return;
  151. }
  152.  
  153. void iniz_neuroni(struct rete reti){
  154. for(int r=0;r < n_input;r++)
  155. reti.neuroni_input[r]=0;
  156. for(int r=0;r < n_ps;r++)
  157. reti.neuroni_ps[r]=0;
  158. for(int r=0;r < n_ss;r++)
  159. reti.neuroni_ss[r]=0;
  160. return;
  161. }
  162.  
  163. float abs_v(float v){
  164. if(v >=0)
  165. return v;
  166. else return (-v);
  167. }
  168.  
  169. int find_max (float v[]){
  170. if(v[0]>=v[1] && v[0]>=v[2])
  171. return 0;
  172. else if(v[1]>=v[0] && v[1]>=v[2])
  173. return 1;
  174. else if(v[2]>=v[0] && v[2]>=v[1])
  175. return 2;
  176. }
  177.  
  178. int mossa (struct rete reti){
  179. for(int k=0;k<n_ps;k++)
  180. for(int i=0;i<n_input;i++)
  181. reti.neuroni_ps[k]+=reti.neuroni_input[i]*reti.sinapsi_ps[i][k];
  182.  
  183.  
  184. for(int i=0;i<n_ps;i++)
  185. reti.neuroni_ps[i] = reti.neuroni_ps[i]/(abs_v(reti.neuroni_ps[i])+1);
  186.  
  187. for(int i=0;i<n_ss;i++)
  188. for(int j=0;j<n_ps;j++)
  189. reti.neuroni_ss[i]+= reti.neuroni_ps[j]*reti.sinapsi_ss[j][i];
  190.  
  191. for(int i=0;i<n_ss;i++)
  192. reti.neuroni_ss[i] = reti.neuroni_ss[i]/(abs_v(reti.neuroni_ss[i])+1);
  193.  
  194. int max = find_max(reti.neuroni_ss);
  195.  
  196. return max;
  197. }
  198.  
  199. void make_griglia(int griglia[][COLONNE],struct blocco* snake,int mela_x,int mela_y){
  200. griglia[mela_x][mela_y]=0;
  201. struct blocco* p = snake;
  202. while(p != NULL){
  203. if(p->prossimo != NULL)
  204. griglia[p->x][p->y]=4;
  205. else
  206. griglia[p->x][p->y]=9;
  207. p = p-> prossimo;
  208. }
  209. return;
  210. }
  211.  
  212. bool death(struct blocco* snake){
  213. struct blocco* p = snake;
  214. struct blocco* q = snake;
  215.  
  216. while(p != NULL){
  217. q=p;
  218. p = p-> prossimo;
  219. }
  220. //q punta alla testa
  221. if(q->x == 0 || q->y == 0 || q->x == COLONNE - 1 || q->y == RIGHE - 1)
  222. return true;
  223. p=snake;
  224. while(p != q){
  225. if(p->x == q->x && p->y == q->y)
  226. return true;
  227. p=p->prossimo;
  228. }
  229. return false;
  230. }
  231.  
  232. void inizializza(int griglia[RIGHE][COLONNE]){
  233. for(int i=0;i<RIGHE;i++)
  234. for(int k=0;k<COLONNE;k++)
  235. if(k==0 || i == 0 || k==COLONNE-1 || i==COLONNE-1)
  236. griglia[i][k]=2;
  237. else
  238. griglia[i][k]=1;
  239. return;
  240. }
  241.  
  242. void print(int griglia[RIGHE][COLONNE]){
  243. for(int i=0;i<RIGHE;i++){
  244. for(int k=0;k<COLONNE;k++)
  245. if(griglia[i][k]==1)
  246. cout <<" ";
  247. else if(griglia[i][k]==2)
  248. cout <<"x";
  249. else if(griglia[i][k]==0)
  250. cout << "a";
  251. else if(griglia[i][k]==4)
  252. cout << "#";
  253. else if(griglia[i][k]==9)
  254. cout << "O";
  255. cout << endl;
  256. }
  257. cout << endl;
  258. return;
  259. }
  260.  
  261. void reti_migliori(int reti_vincenti[], struct rete reti[]){
  262. for (int k=0;k<25;k++){
  263. int m_score=0;
  264. int i;
  265. for(i=0;i<100;i++)
  266. if(reti[i].punteggio>=m_score){
  267. m_score = reti[i].punteggio;
  268. reti_vincenti[k] = i;
  269. }
  270. reti[reti_vincenti[k]].punteggio = -1000;
  271. }
  272. return;
  273. }
  274.  
  275.  
  276. void iniz_reti_everything(struct rete reti[]){
  277. for(int i=0;i<100;i++){
  278. reti[i].snake = new struct blocco;
  279. reti[i].snake->x = RIGHE/2;
  280. reti[i].snake->y = COLONNE/2;
  281. reti[i].snake->prossimo = NULL;
  282.  
  283. struct blocco* p = new struct blocco;
  284. p->x = RIGHE/2-1;
  285. p->y = COLONNE/2;
  286. p->prossimo = reti[i].snake;
  287. reti[i].snake = p;
  288.  
  289. p = new struct blocco;
  290. p->x = RIGHE/2-2;
  291. p->y = COLONNE/2;
  292. p->prossimo = reti[i].snake;
  293. reti[i].snake = p;
  294.  
  295. reti[i].mela.x = rand() % (RIGHE-2) +1;
  296. reti[i].mela.y = rand() % (COLONNE-2) +1;
  297.  
  298. reti[i].velocita.x=1;
  299. reti[i].velocita.y=0;
  300.  
  301. reti[i].mosse = 0;
  302. reti[i].punteggio = 0;
  303. reti[i].game_over = false;
  304.  
  305. inizializza(reti[i].griglia);
  306. }
  307. return;
  308. }
  309.  
  310. void create_reti_from_seed(struct rete reti[],float seed_sinapsi_ps [25][5][20],float seed_sinapsi_ss [25][20][3]){
  311.  
  312. for(int i=0;i<25;i++){
  313. int r = i;
  314. for(int x=0;x<n_input;x++)
  315. for(int y=0;y<n_ps;y++)
  316. reti[i].sinapsi_ps[x][y] = (seed_sinapsi_ps [r][x][y]);
  317. for(int x=0;x<n_ps;x++)
  318. for(int y=0;y<n_ss;y++)
  319. reti[i].sinapsi_ss[x][y] = seed_sinapsi_ss [r][x][y];
  320. }
  321. for(int i=25; i<75;i++){
  322. int r1 = rand () % 25;
  323. int r2 = rand () % 25;
  324. for(int x=0;x<n_input;x++)
  325. for(int y=0;y<n_ps;y++){
  326. if(rand() % 2 == 0)
  327. reti[i].sinapsi_ps[x][y] = (seed_sinapsi_ps [r1][x][y]);
  328. else
  329. reti[i].sinapsi_ps[x][y] = (seed_sinapsi_ps [r2][x][y]);
  330. }
  331. for(int x=0;x<n_ps;x++)
  332. for(int y=0;y<n_ss;y++){
  333. if(rand()%2 == 0)
  334. reti[i].sinapsi_ss[x][y] = seed_sinapsi_ss [r1][x][y];
  335. else
  336. reti[i].sinapsi_ss[x][y] = seed_sinapsi_ss [r2][x][y];
  337. }
  338. }
  339.  
  340. for(int i=75; i<100;i++){
  341. int r1 = rand () % 25;
  342. int r2 = rand () % 25;
  343. for(int x=0;x<n_input;x++)
  344. for(int y=0;y<n_ps;y++){
  345. if(rand() % 2 == 0)
  346. reti[i].sinapsi_ps[x][y] = (seed_sinapsi_ps [r1][x][y])*RandomFloat(0.5,1.5);
  347. else
  348. reti[i].sinapsi_ps[x][y] = (seed_sinapsi_ps [r2][x][y])*RandomFloat(0.5,1.5);
  349. }
  350. for(int x=0;x<n_ps;x++)
  351. for(int y=0;y<n_ss;y++){
  352. if(rand()%2 == 0)
  353. reti[i].sinapsi_ss[x][y] = seed_sinapsi_ss [r1][x][y]*RandomFloat(0.5,1.5);
  354. else
  355. reti[i].sinapsi_ss[x][y] = seed_sinapsi_ss [r2][x][y]*RandomFloat(0.5,1.5);
  356. }
  357. }
  358.  
  359.  
  360.  
  361.  
  362.  
  363. return;
  364. }
  365.  
  366. void create_seed(float seed_sinapsi_ps [25][5][20],float seed_sinapsi_ss [25][20][3],struct rete reti[],int reti_vincenti[]){
  367. for(int i=0;i<25;i++)
  368. for(int k=0;k<5;k++)
  369. for(int j=0;j<20;j++)
  370. seed_sinapsi_ps[i][k][j] = reti[reti_vincenti[i]].sinapsi_ps[k][j];
  371.  
  372. for(int i=0;i<25;i++)
  373. for(int k=0;k<20;k++)
  374. for(int j=0;j<3;j++)
  375. seed_sinapsi_ss[i][k][j] = reti[reti_vincenti[i]].sinapsi_ss[k][j];
  376. return;
  377. }
  378.  
  379. void print_neural_scheme(struct rete reti){
  380. for(int i=0;i<n_ps;i++)
  381. for (int k=0;k<n_input;k++)
  382. cout << reti.sinapsi_ps[k][i] << " ";
  383. cout << endl;
  384. system("pause");
  385. return;
  386. }
  387.  
  388. void spawn_mela(struct rete reti){
  389. bool ok;
  390. struct coordinata temp_mela;
  391. do{
  392. ok=true;
  393. temp_mela.x = rand() % (RIGHE-2) +1;
  394. temp_mela.y = rand() % (COLONNE-2) +1;
  395. struct blocco* p = reti.snake;
  396. while(p!=NULL){
  397. if(p->x == temp_mela.x && p->y == temp_mela.y)
  398. ok = false;
  399. p = p->prossimo;
  400. }
  401. } while(ok==false);
  402. return;
  403. }
  404.  
  405. void memory_clear(struct blocco* p){
  406. struct blocco* l = p;
  407. while(l!=NULL){
  408. struct blocco* k = l;
  409. l = l->prossimo;
  410. delete k;
  411. }
  412. }
  413.  
  414.  
  415. int main(){
  416. srand(time(NULL));
  417. iniz_reti_rand(reti);
  418. iniz_reti_everything(reti);
  419. int generazione=0;
  420. int reti_vincenti[25];
  421. float seed_sinapsi_ss [25][20][3];
  422. float seed_sinapsi_ps [25][5][20];
  423. int interessato;
  424. cout << "Qual'e' il punteggio a cui sei interessato? le reti con un fit score fino a 20 meno sarannosalvate su un txt!" << endl;
  425. cin >> interessato;
  426. int punti_generazione = 0;
  427. int punti_max = 0;
  428. while(punti_max < interessato){
  429.  
  430. punti_generazione = 0;
  431. punti_max = 0;
  432. for(int i=0;i<100;i++){
  433. //if(generazione > 1)
  434. //print_neural_scheme(reti[i]);
  435. while(reti[i].game_over == false){
  436.  
  437. inizializza(reti[i].griglia);
  438. make_griglia(reti[i].griglia,reti[i].snake,reti[i].mela.x,reti[i].mela.y);
  439.  
  440. iniz_neuroni(reti[i]);
  441. analizza_input(reti[i]);
  442. int move = mossa(reti[i]);
  443.  
  444. if(move == 0){
  445. }
  446. else if(move == 1){
  447. if(reti[i].velocita.x == -1){
  448. reti[i].velocita.x = 0;
  449. reti[i].velocita.y = -1;
  450. }
  451. else if(reti[i].velocita.x == 1){
  452. reti[i].velocita.x = 0;
  453. reti[i].velocita.y = 1;
  454. }
  455. else if(reti[i].velocita.y == -1){
  456. reti[i].velocita.x = 1;
  457. reti[i].velocita.y = 0;
  458. }
  459. else if(reti[i].velocita.y == 1){
  460. reti[i].velocita.x = -1;
  461. reti[i].velocita.y = 0;
  462. }
  463. }
  464. else if(move == 2){
  465. if(reti[i].velocita.x == -1){
  466. reti[i].velocita.x = 0;
  467. reti[i].velocita.y = 1;
  468. }
  469. else if(reti[i].velocita.x == 1){
  470. reti[i].velocita.x = 0;
  471. reti[i].velocita.y = -1;
  472. }
  473. else if(reti[i].velocita.y == -1){
  474. reti[i].velocita.x = -1;
  475. reti[i].velocita.y = 0;
  476. }
  477. else if(reti[i].velocita.y == 1){
  478. reti[i].velocita.x = 1;
  479. reti[i].velocita.y = 0;
  480. }
  481. }
  482.  
  483. struct blocco* old_head = reti[i].snake;
  484. while(old_head -> prossimo != NULL)
  485. old_head = old_head -> prossimo;
  486.  
  487. struct blocco* n_head = new struct blocco;
  488. n_head -> x = old_head -> x + reti[i].velocita.x;
  489. n_head -> y = old_head -> y + reti[i].velocita.y;
  490. n_head -> prossimo = NULL;
  491. old_head -> prossimo = n_head;
  492. if(n_head -> x == reti[i].mela.x && n_head -> y == reti[i].mela.y){
  493. reti[i].mela.x = rand() % (RIGHE-2) +1;
  494. reti[i].mela.y = rand() % (COLONNE-2) +1;
  495. reti[i].punteggio += 1;
  496. //da aggiungere spawn mela
  497.  
  498. }
  499. else{
  500. struct blocco* temp = reti[i].snake;
  501. reti[i].snake = reti[i].snake -> prossimo;
  502. delete temp;
  503. }
  504.  
  505.  
  506.  
  507. reti[i].game_over = death(reti[i].snake);
  508. reti[i].mosse++;
  509.  
  510. if(reti[i].mosse > 30 + reti[i].punteggio*50)
  511. reti[i].game_over = true;
  512. }
  513.  
  514. if(reti[i].punteggio >= interessato-20){
  515. ofstream file;
  516. file.open("reti.txt", ios::app);
  517. file << "Rete " << i << " Punti " << reti[i].punteggio << endl;
  518. for(int t=0;t<5;t++)
  519. for(int k=0;k<20;k++)
  520. file << reti[i].sinapsi_ps[t][k] << " ";
  521.  
  522. for(int r=0;r<20;r++)
  523. for(int j=0;j<3;j++)
  524. file << reti[i].sinapsi_ss[r][j] << " ";
  525. file << endl;
  526. file.close();
  527.  
  528.  
  529. }
  530.  
  531. memory_clear(reti[i].snake);
  532. //reti[i].punteggio += reti[i].mosse;
  533. punti_generazione += reti[i].punteggio;
  534. if(reti[i].punteggio > punti_max)
  535. punti_max = reti[i].punteggio;
  536.  
  537. }
  538. cout << "La generazione " << (int)generazione << " ha realizzato una media di " <<float(punti_generazione)/100 << " punti e il massimo e' stato " << punti_max << endl;
  539. reti_migliori(reti_vincenti,reti);
  540. create_seed(seed_sinapsi_ps,seed_sinapsi_ss,reti,reti_vincenti);
  541. create_reti_from_seed(reti,seed_sinapsi_ps,seed_sinapsi_ss);
  542. iniz_reti_everything(reti);
  543. generazione++;
  544. }
  545. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement