Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Windows.Forms;
  10. using System.Collections;
  11. using System.Media;
  12.  
  13. namespace MakeLand
  14. {
  15. public class Population
  16. {
  17. public int generation = 0;
  18. public int bestScore = 0;
  19. public int bestIndex = 0;
  20. public int numInPop = 0;
  21.  
  22. public int[] listOfLiving;
  23. public int countOfLiving;
  24.  
  25. public Phenotype[] maps = null;
  26.  
  27. public Population(int numInPopZ, Random r)
  28. {
  29. numInPop = numInPopZ;
  30. maps = new Phenotype[numInPop];
  31. for (int i = 0; i < numInPop; i++)
  32. {
  33. Genotype g = new Genotype(r);
  34. Phenotype p = new Phenotype(g,0);
  35. p.createPheno();
  36. p.setScore();
  37. maps[i] = p;
  38. }
  39. }
  40.  
  41. /// <summary>
  42. /// Returns the index of the best individual and updates bestScore
  43. /// </summary>
  44. /// <returns></returns>
  45. public int findBest()
  46. {
  47. Phenotype p = maps[0];
  48. bestScore = p.score;
  49. bestIndex = 0;
  50. for (int i = 1; i < numInPop; i++)
  51. {
  52. p = maps[i];
  53. if (p.score > bestScore)
  54. {
  55. bestIndex = i;
  56. bestScore = p.score;
  57. }
  58. }
  59. return bestIndex;
  60. }
  61.  
  62.  
  63. /// <summary>
  64. /// Finds the worst individual thats actually alive
  65. /// </summary>
  66. /// <returns></returns>
  67. public int findWorstAlive()
  68. {
  69. bool first = true;
  70. int worstScore = 0;
  71. int worstIndex = 0;
  72.  
  73. for (int i = 1; i < numInPop; i++)
  74. {
  75. Phenotype p = maps[i];
  76. if (p.alive && first)
  77. {
  78. first = false;
  79. worstScore = p.score;
  80. worstIndex = i;
  81. continue;
  82. }
  83.  
  84. if (p.alive && p.score < worstScore)
  85. {
  86. worstScore = p.score;
  87. worstIndex = i;
  88. }
  89. }
  90. return worstIndex;
  91. }
  92.  
  93. /// <summary>
  94. /// Just a standard getter
  95. /// </summary>
  96. /// <param name="i"></param>
  97. /// <returns></returns>
  98. public Phenotype getPhenotype(int i)
  99. {
  100. return maps[i];
  101. }
  102.  
  103. /// <summary>
  104. /// Unsets the newborn flag for the entire population
  105. /// </summary>
  106. public void unsetNewborn()
  107. {
  108. for (int i = 1; i < numInPop; i++)
  109. {
  110. getPhenotype(i).newborn = false;
  111. }
  112. }
  113.  
  114. /// <summary>
  115. /// Kills the weakest
  116. /// </summary>
  117. /// <param name="n"></param>
  118. public void killThisMany(int n)
  119. {
  120.  
  121. for (int i = 0; i <n; i++)
  122. {
  123. int k = findWorstAlive();
  124. getPhenotype(k).alive = false;
  125. }
  126. }
  127.  
  128.  
  129.  
  130. /// <summary>
  131. /// Search for dead individuals - replace them with living newborn ones
  132. /// </summary>
  133. public void breedPopulation(Random r)
  134. {
  135. listOfLiving = new int[Params.populationCnt];
  136. countOfLiving=0;
  137. for (int i = 0; i < Params.populationCnt; i++)
  138. {
  139. if (getPhenotype(i).alive && (!getPhenotype(i).newborn))
  140. {
  141. listOfLiving[i] = i;
  142. countOfLiving++;
  143. }
  144. }
  145.  
  146. for (int i = 0; i < Params.populationCnt; i++)
  147. {
  148. if (!getPhenotype(i).alive)
  149. {
  150. int mum = r.Next(0, countOfLiving);
  151. int dad = r.Next(0, countOfLiving);
  152. mum = listOfLiving[mum];
  153. dad = listOfLiving[dad];
  154. Phenotype mumP = getPhenotype(mum);
  155. Phenotype dadP = getPhenotype(dad);
  156. Genotype ggg = makeGenome(mumP.genotype,dadP.genotype);
  157. if (Params.mutationPercent > r.Next(0,100)) mutate(ggg, r);
  158. //int b = generation;
  159. maps[i] = new Phenotype(ggg, G.pop.generation);
  160.  
  161. }
  162. }
  163.  
  164. if (generation % 20 == 0)
  165. checkDuplicateGenotypes();
  166. }
  167.  
  168.  
  169. public bool checkDuplicateGenes(Genotype ggg)
  170. {
  171. bool retv = false;
  172. for (int i = 0; i < Params.genotypeSize; i++)
  173. for (int k = i+1; k < Params.genotypeSize; k++)
  174. {
  175. if(ggg.genes[i].equal(ggg.genes[k]))
  176. {
  177. G.dupGeneCount++;
  178. ggg.genes[i] = new Gene(G.rnd);
  179. retv = true;
  180. }
  181. }
  182. return retv;
  183. }
  184.  
  185. public void mutate(Genotype g, Random r)
  186. {
  187. G.mutationCount++;
  188. //g.genes[0] = new Gene();
  189.  
  190. int mutationType = 0;
  191. int numAttributes = 5;
  192. int rndPercent = r.Next(0, 100);
  193.  
  194. if (rndPercent < 25)
  195. mutationType = 0;
  196. else if (rndPercent < 50)
  197. mutationType = 1;
  198. else if (rndPercent < 75)
  199. mutationType = 2;
  200. else
  201. mutationType = 3;
  202.  
  203. int a, b;
  204. int creepVal = 80; ;
  205.  
  206. switch (mutationType) {
  207.  
  208. case 0:
  209.  
  210. // Mutate ng new genes
  211. int ng = r.Next(0, Params.genotypeSize);
  212. for (int i = 0; i < ng; i++)
  213. {
  214. a = r.Next(0, Params.genotypeSize);
  215. g.genes[a] = new Gene(r);
  216. }
  217. break;
  218. case 1:
  219. // Swap Genes
  220. a = r.Next(0, Params.genotypeSize);
  221. b = r.Next(0, Params.genotypeSize);
  222. Gene temp = g.genes[a];
  223. g.genes[a] = g.genes[b];
  224. g.genes[b] = temp;
  225.  
  226. break;
  227. case 2:
  228. // Copy one gene randomly and mutate it
  229. a = r.Next(0, Params.genotypeSize);
  230. Gene newGene = g.genes[a];
  231. a = r.Next(0, Params.genotypeSize);
  232. g.genes[a] = newGene;
  233.  
  234. // Now mutate it
  235. int attributeMutate = r.Next(0, numAttributes);
  236. for (int i = 0; i < attributeMutate; i++)
  237. {
  238. // Creep the value
  239. int mutVal = 0;
  240. switch (attributeMutate)
  241. {
  242. case 0:
  243. mutVal = (r.Next(0, creepVal * 2 + 1) - creepVal) + g.genes[a].repeatX;
  244. mutVal = clamp(mutVal, 0, Params.maxRepeat);
  245. g.genes[a].repeatX = mutVal;
  246. break;
  247. case 1:
  248. mutVal = (r.Next(0, creepVal * 2 + 1) - creepVal) + g.genes[a].repeatY;
  249. mutVal = clamp(mutVal, 0, Params.maxRepeat);
  250. g.genes[a].repeatY = mutVal;
  251. break;
  252. case 2:
  253. mutVal = (r.Next(0, creepVal * 2 + 1) - creepVal) + g.genes[a].x;
  254. mutVal = clamp(mutVal, 0, Params.dimX);
  255. g.genes[a].x = mutVal;
  256. break;
  257. case 3:
  258. mutVal = (r.Next(0, creepVal * 2 + 1) - creepVal) + g.genes[a].y;
  259. mutVal = clamp(mutVal, 0, Params.dimY);
  260. g.genes[a].y = mutVal;
  261. break;
  262. case 4:
  263. mutVal = (r.Next(0, creepVal * 2 + 1) - creepVal) + g.genes[a].terrain;
  264. if (mutVal < 0)
  265. mutVal = 0;
  266. else if (mutVal > 3)
  267. mutVal = 1;
  268. g.genes[a].terrain = mutVal;
  269. break;
  270. }
  271. }
  272. break;
  273. case 3:
  274.  
  275. break;
  276.  
  277. }
  278.  
  279.  
  280. }
  281.  
  282. private int clamp(int val, int min, int max)
  283. {
  284. if (val < min)
  285. return min;
  286. else if (val > max)
  287. return max;
  288. else
  289. return val;
  290. }
  291.  
  292. /// <summary>
  293. /// create a new geneome from mum and dad
  294. /// </summary>
  295. /// <param name="g1"></param>
  296. /// <param name="g2"></param>
  297. /// <returns></returns>
  298. public Genotype makeGenome(Genotype g1, Genotype g2)
  299. {
  300. Genotype retv = new Genotype();
  301. for (int i = 0; i < Params.genotypeSize; i++)
  302. {
  303. if (G.rnd.NextDouble()<0.5)
  304. {
  305. retv.genes[i] = new Gene(g1.genes[i]);
  306. }
  307. else
  308. {
  309. retv.genes[i] = new Gene(g2.genes[i]);
  310. }
  311. }
  312. return retv;
  313. }
  314.  
  315. public void checkDuplicateGenotypes()
  316. {
  317. for (int i = 0; i < Params.populationCnt; i++)
  318. {
  319. Genotype g = getPhenotype(i).genotype;
  320. if (checkDuplicateGenes(g)) continue;
  321. for (int k = i + 1; k < Params.populationCnt; k++)
  322. {
  323. Genotype kk = getPhenotype(k).genotype;
  324. if (kk.equal(g))
  325. {
  326. mutate(g, G.rnd);
  327. G.dupGeneomeCount++;
  328. }
  329. }
  330. }
  331. }
  332.  
  333.  
  334. /// <summary>
  335. /// what it sounds like
  336. /// </summary>
  337. public void do1Generation()
  338. {
  339. G.pop.generation++;
  340. unsetNewborn();
  341. killThisMany(Params.populationCnt / 2);
  342. breedPopulation(G.rnd);
  343. //if (Params.checkDuplicateGenomes != -1 && G.pop.generation % Params.checkDuplicateGenomes == 0) checkDuplicateGenotypes();
  344. Application.DoEvents();
  345. }
  346.  
  347. }
  348.  
  349. public class Genotype
  350. {
  351. public Gene[] genes = new Gene[Params.genotypeSize];
  352.  
  353. public Genotype(Random r)
  354. {
  355. for (int i = 0; i < Params.genotypeSize; i++)
  356. genes[i] = new Gene(r);
  357. }
  358.  
  359. public Genotype()
  360. {
  361. for (int i = 0; i < Params.genotypeSize; i++)
  362. genes[i] = new Gene();
  363. }
  364.  
  365. public bool equal(Genotype gg)
  366. {
  367. for (int i = 0; i < Params.genotypeSize; i++)
  368. {
  369. if (!(gg.genes[i].equal(genes[i]))) return false;
  370. }
  371. return true;
  372. }
  373. }
  374.  
  375.  
  376.  
  377. public class Gene
  378. {
  379. public int terrain=0;
  380. public int x=0;
  381. public int y=0;
  382. public int repeatY = 0;
  383. public int repeatX = 0;
  384.  
  385. public Gene()
  386. {
  387.  
  388. }
  389.  
  390. public Gene(int ter, int xx, int yy, int rptX, int rptY)
  391. {
  392. terrain = ter;
  393. x = xx;
  394. y = yy;
  395. repeatX = rptX;
  396. repeatY = rptY;
  397. }
  398.  
  399. /// <summary>
  400. /// New Random Gene
  401. /// </summary>
  402. /// <param name="r"></param>
  403. public Gene(Random r)
  404. {
  405. terrain = r.Next(0,3);
  406. x = r.Next(0, Params.dimX);
  407. y = r.Next(0, Params.dimY);
  408. repeatX = r.Next(0, Params.maxRepeat);
  409. repeatY = r.Next(0, Params.maxRepeat);
  410. }
  411.  
  412. /// <summary>
  413. /// Copy constructor
  414. /// </summary>
  415. /// <param name="gg"></param>
  416. public Gene(Gene gg) // copy constructor
  417. {
  418. terrain = gg.terrain;
  419. x = gg.x;
  420. y = gg.y;
  421. repeatX = gg.repeatX;
  422. repeatY = gg.repeatY;
  423. }
  424.  
  425. public bool equal(Gene g)
  426. {
  427. if (g.x != x) return false;
  428. if (g.y != y) return false;
  429. if (g.repeatX != repeatX) return false;
  430. if (g.repeatY != repeatY) return false;
  431. if (g.terrain != terrain) return false;
  432. return true;
  433. }
  434.  
  435. }
  436.  
  437. public class Phenotype
  438. {
  439. public Genotype genotype=null; // reference class - this is a pointer not a copy
  440. int[,] pheno = null;
  441. Bitmap bitm = null;
  442. public int score = 0;
  443. public bool alive = true;
  444. public bool newborn = true;
  445. public int gen = 0;
  446.  
  447. /// <summary>
  448. /// Default constructor probably not helpfull
  449. /// </summary>
  450. public Phenotype()
  451. {
  452. // default is all null - no need for code yet
  453. }
  454.  
  455. /// <summary>
  456. /// This is the critical constructor it creates the pheno array for scoring
  457. /// </summary>
  458. /// <param name="gg"></param>
  459. public Phenotype(Genotype gg, int generationCount)
  460. {
  461. genotype = gg;
  462. createPheno();
  463. setScore();
  464. gen = generationCount;
  465.  
  466. }
  467.  
  468. /// <summary>
  469. /// create the pheno array
  470. /// </summary>
  471. public void createPheno()
  472. {
  473. pheno = new int[Params.dimX, Params.dimY];
  474. for (int x=0; x< Params.dimX;x++)
  475. for (int y=0; y< Params.dimY;y++) { pheno[x,y] = 0; } // initialise to 0
  476.  
  477. for (int i = 0; i < Params.genotypeSize; i++)
  478. {
  479. Gene g = genotype.genes[i];
  480. for (int kx = 0; kx < g.repeatX; kx++)
  481. for (int ky = 0; ky < g.repeatY; ky++)
  482. {
  483. int x = g.x+kx;
  484. int y = g.y+ky;
  485. if (y< Params.dimY && x< Params.dimX) pheno[x, y] = g.terrain;
  486. }
  487.  
  488. }
  489. }
  490.  
  491. public int getTerrainSafe(int x, int y)
  492. {
  493. if (x < 0 || y < 0 || x >= Params.dimX || y >= Params.dimY) return 0;
  494. return pheno[x, y];
  495. }
  496.  
  497. /// <summary>
  498. /// returns the score for selection - also stores it in Phenotype
  499. /// </summary>
  500. /// <returns></returns>
  501. public int setScore()
  502. {
  503. score = 0;
  504.  
  505. float seaCount = 0;
  506. float landCount = 0;
  507. float mountainCount = 0;
  508.  
  509. int yChange = 0;
  510. int xChange = 0;
  511. int xyChange = 0;
  512. int every = 1;
  513.  
  514. for (int x = 0; x < Params.dimX; x = x + every) {
  515. for (int y = 0; y < Params.dimY; y = y + every) {
  516. switch (pheno[x, y]) {
  517. case 0:
  518. score += waterTerrain(x, y);
  519. seaCount++;
  520. break;
  521. case 1:
  522. score += landTerrain(x, y);
  523. landCount++;
  524. break;
  525. case 2:
  526. score += mountainTerrain(x, y);
  527. mountainCount++;
  528. break;
  529. }
  530.  
  531. if (y < Params.dimY - 1)
  532. {
  533. if (pheno[x, y] == pheno[x, y + 1])
  534. yChange++;
  535. }
  536.  
  537. if (x < Params.dimY - 1)
  538. {
  539. if (pheno[x, y] == pheno[x + 1, y])
  540. xChange++;
  541. }
  542.  
  543. if (x < Params.dimY - 1 && y < Params.dimY - 1)
  544. {
  545. if (pheno[x, y] == pheno[x + 1, y + 1])
  546. xyChange++;
  547. }
  548. }
  549.  
  550. if (yChange >= 7)
  551. score -= 500;
  552. else
  553. score += 500;
  554.  
  555. if (xChange >= 7)
  556. score -= 500;
  557. else
  558. score += 500;
  559.  
  560. if (xyChange >= 14)
  561. score -= 900;
  562. else
  563. score += 500;
  564.  
  565. if (xyChange <= 7)
  566. score += 1500;
  567.  
  568. xyChange = 0;
  569. xChange = 0;
  570. yChange = 0;
  571. }
  572.  
  573.  
  574. if (landCount / (Params.dimX * Params.dimY) <= Params.percentLand)
  575. score -= 100;
  576. else
  577. score += 100;
  578.  
  579. if (mountainCount/(Params.dimX * Params.dimY) <= Params.percentFresh)
  580. score -= 100;
  581. else
  582. score += 100;
  583.  
  584.  
  585.  
  586. return score;
  587. }
  588.  
  589. private int waterTerrain(int x, int y) {
  590. int score = 0;
  591. if (x <= 3 || x >= Params.dimX - 3 || y <= 3 || y >= Params.dimY - 3)
  592. score += 5;
  593. else if (pheno[x, y + 1] == 0 && pheno[x, y - 1] == 0 &&
  594. pheno[x + 1, y] == 0 && pheno[x - 1, y] == 0 &&
  595. pheno[x + 1, y - 1] == 0 && pheno[x - 1, y + 1] == 0)
  596. {
  597. score += 5;
  598. } else
  599. {
  600. score -= 5;
  601. }
  602.  
  603.  
  604. return score;
  605. }
  606.  
  607. private int landTerrain(int x, int y) {
  608. int score = 0;
  609. if (!(x <= 3 || x >= Params.dimX - 3 || y <= 3 || y >= Params.dimY - 3))
  610. {
  611. // If we are surrounded by more terrain
  612. if (pheno[x, y + 1] == 1 && pheno[x, y - 1] == 1 &&
  613. pheno[x + 1, y] == 1 && pheno[x - 1, y] == 1 &&
  614. pheno[x + 1, y - 1] == 1 && pheno[x - 1, y + 1] == 1)
  615. {
  616. score += 15;
  617. }
  618.  
  619. // If we are surrounded by even more terrain
  620. if (pheno[x, y + 2] == 1 && pheno[x, y - 2] == 1 &&
  621. pheno[x + 2, y] == 1 && pheno[x - 2, y] == 1 &&
  622. pheno[x + 2, y - 2] == 1 && pheno[x - 2, y + 2] == 1)
  623. {
  624. score += 30;
  625. }
  626.  
  627. // If we are surrounded by water
  628. if (pheno[x, y + 1] == 0 && pheno[x, y - 1] == 0 &&
  629. pheno[x + 1, y] == 0 && pheno[x - 1, y] == 0 &&
  630. pheno[x + 1, y - 1] == 0 && pheno[x - 1, y + 1] == 0)
  631. {
  632. score -= 15;
  633. }
  634.  
  635. } else
  636. {
  637. //if (x <= 1 || x >= Params.dimX - 1 || y <= 1 || y >= Params.dimY - 31)
  638. score -= 350;
  639. }
  640.  
  641.  
  642. score += 5;
  643.  
  644. return score;
  645. }
  646.  
  647. private int mountainTerrain(int x, int y) {
  648. int score = 0;
  649. if (!(x <= 3 || x >= Params.dimX - 3 || y <= 3 || y >= Params.dimY - 3))
  650. {
  651.  
  652. if (pheno[x, y + 1] == 2 && pheno[x, y - 1] == 2 &&
  653. pheno[x + 1, y] == 2 && pheno[x - 1, y] == 2 &&
  654. pheno[x + 1, y - 1] == 2 && pheno[x - 1, y + 1] == 2)
  655. {
  656. score += 5;
  657. }
  658. // If we are surrounded by even more terrain
  659. else if (pheno[x, y + 2] == 2 && pheno[x, y - 2] == 2 &&
  660. pheno[x + 2, y] == 2 && pheno[x - 2, y] == 2 &&
  661. pheno[x + 2, y - 2] == 2 && pheno[x - 2, y + 2] == 2)
  662. {
  663. score += 10;
  664. }
  665.  
  666. if (pheno[x, y + 1] == 1 && pheno[x, y - 1] == 1 &&
  667. pheno[x + 1, y] == 1 && pheno[x - 1, y] == 1 &&
  668. pheno[x + 1, y - 1] == 1 && pheno[x - 1, y + 1] == 1)
  669. {
  670. score += 15;
  671. }
  672. // If we are surrounded by even more terrain
  673. else if (pheno[x, y + 2] == 1 && pheno[x, y - 2] == 1 &&
  674. pheno[x + 2, y] == 1 && pheno[x - 2, y] == 1 &&
  675. pheno[x + 2, y - 2] == 1 && pheno[x - 2, y + 2] == 1)
  676. {
  677. score += 30;
  678. }
  679. // If we are surrounded by water
  680. else if (pheno[x, y + 1] == 0 && pheno[x, y - 1] == 0 &&
  681. pheno[x + 1, y] == 0 && pheno[x - 1, y] == 0 &&
  682. pheno[x + 1, y - 1] == 0 && pheno[x - 1, y + 1] == 0)
  683. {
  684. score -= 90;
  685. }
  686. else
  687. score -= 15;
  688. } else
  689. {
  690. //if (x <= 1 || x >= Params.dimX - 1 || y <= 1 || y >= Params.dimY - 31)
  691. score -= 350;
  692. }
  693.  
  694. return score;
  695. }
  696.  
  697. /// <summary>
  698. /// Display the map in a picturebox
  699. /// </summary>
  700. public void show(PictureBox pb)
  701. {
  702. System.Drawing.SolidBrush myBrush;
  703. if (bitm == null)
  704. {
  705. bitm = new Bitmap(Params.dimX, Params.dimY);
  706. myBrush = new System.Drawing.SolidBrush(G.ca[0]);
  707. Graphics gra = Graphics.FromImage(bitm);
  708.  
  709. gra.FillRectangle(myBrush,0,0, Params.dimX, Params.dimY); //this is your code for drawing rectangles
  710.  
  711. for (int x=0; x< Params.dimX; x++)
  712. {
  713. for (int y = 0; y < Params.dimY; y++)
  714. {
  715. if (pheno[x,y] > 0)
  716. {
  717. bitm.SetPixel(x, y, G.ca[pheno[x,y]]);
  718. }
  719. }
  720. }
  721. }
  722. pb.Image = bitm;
  723. }
  724. }
  725. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement