Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <iostream>
  3. #include<ctime>
  4. #include<cstdlib>
  5. using namespace std;
  6.  
  7. #define RIGHE 20
  8. #define COLONNE 20
  9.  
  10. struct blocco{
  11. int x;
  12. int y;
  13. struct blocco * prossimo;
  14. };
  15.  
  16. struct coordinata{
  17. int x;
  18. int y;
  19. };
  20.  
  21. struct rete{
  22. int griglia[20][20];
  23. int punteggio=0;
  24. int mosse=0;
  25. float sinapsi_ps [20][20][20];
  26. float sinapsi_ss [20][4];
  27. float neuroni_ps [20];
  28. float neuroni_ss [4];
  29. struct blocco* snake;
  30. struct coordinata velocita;
  31. struct coordinata mela;
  32. bool game_over = false;
  33.  
  34. };
  35.  
  36. struct rete reti[100];
  37.  
  38. float RandomFloat(float a, float b) {
  39.  
  40. float random = ((float) rand()) / (float) RAND_MAX;
  41. float diff = b - a;
  42. float r = random * diff;
  43. return a + r;
  44. }
  45.  
  46. void iniz_reti_rand(struct rete reti[]){
  47. for(int i=0;i<100;i++){
  48. for(int t=0;t<20;t++)
  49. for(int k=0;k<20;k++)
  50. for(int j=0;j<20;j++)
  51. reti[i].sinapsi_ps[t][k][j] = RandomFloat(-1,1);
  52.  
  53. for(int r=0;r<20;r++)
  54. for(int j=0;j<4;j++)
  55. reti[i].sinapsi_ss[r][j] = RandomFloat(-1,1);
  56. }
  57. return;
  58. }
  59.  
  60. void iniz_neuroni(struct rete reti){
  61. for(int r=0;r<20;r++)
  62. reti.neuroni_ps[r]=0;
  63. for(int r=0;r<20;r++)
  64. reti.neuroni_ss[r]=0;
  65. return;
  66. }
  67.  
  68. float abs_v(float v){
  69. if(v >=0)
  70. return v;
  71. else return (-v);
  72. }
  73.  
  74. int find_max (float v[]){
  75. if(v[0]>=v[1] && v[0]>=v[2] && v[0]>=v[3])
  76. return 0;
  77. else if(v[1]>=v[0] && v[1]>=v[2] && v[1]>=v[3])
  78. return 1;
  79. else if(v[2]>=v[0] && v[2]>=v[1] && v[2]>=v[3])
  80. return 2;
  81. else if(v[3]>=v[0] && v[3]>=v[1] && v[3]>=v[2])
  82. return 3;
  83. }
  84.  
  85. char mossa (struct rete reti){
  86. for(int i=0;i<20;i++)
  87. for(int k=0;k<20;k++)
  88. for(int j=0;j<20;j++)
  89. reti.neuroni_ps[j]+=reti.griglia[i][k]*reti.sinapsi_ps[i][k][j];
  90. for(int i=0;i<20;i++)
  91. if(reti.neuroni_ps[i]<0)
  92. reti.neuroni_ps[i] = reti.neuroni_ps[i]/(abs_v(reti.neuroni_ps[i])+1);
  93.  
  94. for(int i=0;i<20;i++)
  95. for(int j=0;j<4;j++)
  96. reti.neuroni_ss[j]+= reti.neuroni_ps[i]*reti.sinapsi_ss[i][j];
  97.  
  98. for(int i=0;i<4;i++)
  99. reti.neuroni_ss[i] = reti.neuroni_ss[i]/(abs_v(reti.neuroni_ss[i])+1);
  100. int max = find_max(reti.neuroni_ss);
  101. if(max==0)
  102. return 'w';
  103. else if(max==1)
  104. return 's';
  105. else if(max==2)
  106. return 'a';
  107. else if(max==3)
  108. return 'd';
  109.  
  110. }
  111.  
  112. void make_griglia(int griglia[][COLONNE],struct blocco* snake,int mela_x,int mela_y){
  113. griglia[mela_x][mela_y]=2;
  114. struct blocco* p = snake;
  115. while(p != NULL){
  116. if(p->prossimo != NULL)
  117. griglia[p->x][p->y]=1;
  118. else
  119. griglia[p->x][p->y]=3;
  120. p = p-> prossimo;
  121. }
  122. return;
  123. }
  124.  
  125. bool death(struct blocco* snake){
  126. struct blocco* p = snake;
  127. struct blocco* q = snake;
  128.  
  129. while(p != NULL){
  130. q=p;
  131. p = p-> prossimo;
  132. }
  133. //q punta alla testa
  134. if(q->x == 0 || q->y == 0 || q->x == COLONNE - 1 || q->y == RIGHE - 1)
  135. return true;
  136. p=snake;
  137. while(p != q){
  138. if(p->x == q->x && p->y == q->y)
  139. return true;
  140. p=p->prossimo;
  141. }
  142. return false;
  143. }
  144.  
  145. void inizializza(int griglia[RIGHE][COLONNE]){
  146. for(int i=0;i<RIGHE;i++)
  147. for(int k=0;k<COLONNE;k++)
  148. griglia[i][k]=0;
  149. return;
  150. }
  151. void print(int griglia[RIGHE][COLONNE]){
  152. for(int i=0;i<RIGHE;i++){
  153. for(int k=0;k<COLONNE;k++)
  154. cout << griglia[i][k];
  155. cout << endl;
  156. }
  157. cout << endl;
  158. return;
  159. }
  160.  
  161. void reti_migliori(int reti_vincenti[], struct rete reti[]){
  162. for (int k=0;k<10;k++){
  163. int m_score=0;
  164. int i;
  165. for(i=0;i<100;i++)
  166. if(reti[i].punteggio>=m_score){
  167. m_score = reti[i].punteggio;
  168. reti_vincenti[k] = i;
  169. }
  170. reti[reti_vincenti[k]].punteggio = -1000;
  171. }
  172. return;
  173. }
  174.  
  175. void create_seed(float seed_sinapsi_ps [10][20][20][20],float seed_sinapsi_ss [10][20][4],struct rete reti[],int reti_vincenti[]){
  176. for(int i=0;i<10;i++)
  177. for(int k=0;k<20;k++)
  178. for(int j=0;j<20;j++)
  179. for(int y=0;y<20;y++)
  180. seed_sinapsi_ps[i][k][j][y] = reti[reti_vincenti[i]].sinapsi_ps[k][j][y];
  181.  
  182. for(int i=0;i<10;i++)
  183. for(int k=0;k<20;k++)
  184. for(int j=0;j<4;j++)
  185. seed_sinapsi_ss[i][k][j] = reti[reti_vincenti[i]].sinapsi_ss[k][j];
  186. return;
  187. }
  188.  
  189. void iniz_reti_everything(struct rete reti[]){
  190. for(int i=0;i<100;i++){
  191. reti[i].snake = new struct blocco;
  192. reti[i].snake->x = RIGHE/2;
  193. reti[i].snake->y = COLONNE/2;
  194. reti[i].snake->prossimo = NULL;
  195.  
  196. struct blocco* p = new struct blocco;
  197. p->x = RIGHE/2-1;
  198. p->y = COLONNE/2;
  199. p->prossimo = reti[i].snake;
  200. reti[i].snake = p;
  201.  
  202. p = new struct blocco;
  203. p->x = RIGHE/2-2;
  204. p->y = COLONNE/2;
  205. p->prossimo = reti[i].snake;
  206. reti[i].snake = p;
  207.  
  208. reti[i].mela.x = rand() % (RIGHE-2) +1;
  209. reti[i].mela.y = rand() % (COLONNE-2) +1;
  210.  
  211. reti[i].velocita.x=1;
  212. reti[i].velocita.y=0;
  213.  
  214. reti[i].mosse = 0;
  215. reti[i].punteggio = 0;
  216. reti[i].game_over = false;
  217.  
  218. inizializza(reti[i].griglia);
  219. }
  220. return;
  221. }
  222.  
  223. void create_reti_from_seed(struct rete reti[],float seed_sinapsi_ps [10][20][20][20],float seed_sinapsi_ss [10][20][4]){
  224. for(int i=0;i<100;i++){
  225. int r = i%10;
  226.  
  227. for(int x=0;x<20;x++)
  228. for(int y=0;y<20;y++)
  229. for(int z=0;z<20;z++)
  230. reti[i].sinapsi_ps[x][y][z] = (seed_sinapsi_ps [r][x][y][z])*RandomFloat(0.99,1.01);
  231. for(int x=0;x<20;x++)
  232. for(int y=0;y<4;y++)
  233. reti[i].sinapsi_ss[x][y] = seed_sinapsi_ss [r][x][y]*RandomFloat(0.99,1.01);
  234. }
  235. return;
  236. }
  237.  
  238. int main(){
  239. iniz_reti_rand(reti);
  240. iniz_reti_everything(reti);
  241. int reti_vincenti[10];
  242. float seed_sinapsi_ss [10][20][4];
  243. float seed_sinapsi_ps [10][20][20][20];
  244.  
  245. while(1){
  246. int punti_generazione = 0;
  247. for(int i=0;i<100;i++){
  248. while(reti[i].game_over == false){
  249. make_griglia(reti[i].griglia,reti[i].snake,reti[i].mela.x,reti[i].mela.y);
  250. char move;
  251. iniz_neuroni(reti[i]);
  252. move = mossa(reti[i]);
  253. if(move == 's'){
  254. if(reti[i].velocita.x != -1)
  255. reti[i].velocita.x = 1;
  256. reti[i].velocita.y = 0;
  257. }
  258. if(move == 'w'){
  259. if(reti[i].velocita.x != 1)
  260. reti[i].velocita.x = -1;
  261. reti[i].velocita.y = 0;
  262. }
  263. if(move == 'a'){
  264. reti[i].velocita.x = 0;
  265. if(reti[i].velocita.y != 1)
  266. reti[i].velocita.y = -1;
  267. }
  268. if(move == 'd'){
  269. reti[i].velocita.x = 0;
  270. if(reti[i].velocita.y != -1)
  271. reti[i].velocita.y = 1;
  272. }
  273.  
  274. struct blocco* old_head = reti[i].snake;
  275. while(old_head -> prossimo != NULL)
  276. old_head = old_head -> prossimo;
  277.  
  278. struct blocco* n_head = new struct blocco;
  279. n_head -> x = old_head -> x + reti[i].velocita.x;
  280. n_head -> y = old_head -> y + reti[i].velocita.y;
  281. n_head -> prossimo = NULL;
  282.  
  283. old_head -> prossimo = n_head;
  284. if(n_head -> x == reti[i].mela.x && n_head -> y == reti[i].mela.y){
  285. reti[i].mela.x = rand() % (RIGHE-2) +1;
  286. reti[i].mela.y = rand() % (COLONNE-2) +1;
  287. reti[i].punteggio += 10;
  288. }
  289. else{
  290. struct blocco* temp = reti[i].snake;
  291. reti[i].snake = reti[i].snake -> prossimo;
  292. delete temp;
  293. }
  294.  
  295. reti[i].game_over = death(reti[i].snake);
  296. inizializza(reti[i].griglia);
  297.  
  298. reti[i].mosse++;
  299. if(reti[i].mosse > 1000 + reti[i].punteggio*100)
  300. reti[i].game_over = true;
  301. }
  302. punti_generazione += reti[i].punteggio;
  303.  
  304. }
  305. cout << "La generazione " << " ha realizzato " << punti_generazione << " punti" << endl;
  306. reti_migliori(reti_vincenti,reti);
  307. create_seed(seed_sinapsi_ps,seed_sinapsi_ss,reti,reti_vincenti);
  308. create_reti_from_seed(reti,seed_sinapsi_ps,seed_sinapsi_ss);
  309. iniz_reti_everything(reti);
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement