Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.70 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.  
  7. namespace l2
  8. {
  9. public static class Util
  10. {
  11. private static Random rnd = new Random();
  12. public static int GetRandom02()
  13. {
  14. return rnd.Next(0,2);
  15. }
  16. public static double GetRandom02d()
  17. {
  18. return rnd.NextDouble();
  19. }
  20. public static int GetRandomCustom(int val)
  21. {
  22. return rnd.Next(0, val);
  23. }
  24. public static char rev(char har)
  25. {
  26. char h = '0';
  27. if (har == h)
  28. return '1';
  29. else return '0';
  30. }
  31. public static string ReplaceAt(this string input, int index, char newChar)
  32. {
  33. char[] chars = input.ToCharArray();
  34. chars[index] = newChar;
  35. return new string(chars);
  36. }
  37.  
  38. }
  39.  
  40. class Program
  41. {
  42.  
  43. public static string Reverse(string s)
  44. {
  45. char[] charArray = s.ToCharArray();
  46. Array.Reverse(charArray);
  47. return new string(charArray);
  48. }
  49.  
  50.  
  51. public static String genbin(double dlugosc)
  52. {
  53. String liczbastr = "";
  54. for (int i = 0; i < dlugosc; i++)
  55. {
  56. liczbastr = liczbastr.Insert(0, Util.GetRandom02().ToString());
  57. }
  58. return liczbastr;
  59. }
  60.  
  61. public static int bintodec(String bin)
  62. {
  63. int dec = 0;
  64. dec = Convert.ToInt32(bin, 2);
  65. return dec;
  66. }
  67.  
  68. public static double doprzedzialu(int liczba, double a, double b, double m)
  69. {
  70. double wynik = 0;
  71. wynik = a + (b - a) * liczba / (Math.Pow(2, m) - 1);
  72. return wynik;
  73. }
  74.  
  75. public static double rastrigin(double x, double A, double omega, int n)
  76. {
  77. double wynik = 0;
  78. wynik = A * n + Math.Pow(x, 2) - 10 * Math.Cos(omega * x);
  79. return wynik;
  80. }
  81.  
  82. static void Main(string[] args)
  83. {
  84. bool pierwszy = true;
  85. double A = 10;
  86. double omega = 20 * 3.14;
  87. int n = 3;
  88. int d = 6;
  89. double a = -1.0;
  90. double b = 1.0;
  91. double dziedzm = b - a;
  92. double ilosckom = (b - a) * Math.Pow(10, d);
  93. int m = 0;
  94. double dlugwekbin = Math.Pow(2, m);
  95. while (dlugwekbin <= ilosckom)
  96. {
  97. m++;
  98. dlugwekbin = Math.Pow(2, m);
  99. }
  100. int il = 10;
  101. List<String> lancuchy = new List<String>();
  102. for (int i = 0; i < il; i++)
  103. {
  104. lancuchy.Add(genbin(m));
  105. }
  106. List<String> koncowe = new List<String>();
  107. for (int k = 0; k < 10; k++)
  108. {
  109. //CAŁKOWITE ZASTĘPOWANIE
  110. if(pierwszy != true)
  111. lancuchy = koncowe;
  112. //CZĘŚCIOWE ZASTĘPOWANIE
  113. /*if(pierwszy != true)
  114. {
  115. List<String> temp = lancuchy;
  116. lancuchy = new List<String>();
  117. for (int i = 0; i < 5; i++)
  118. lancuchy.Add(koncowe[i]);
  119. for (int i = 0; i < 5; i++)
  120. lancuchy.Add(temp[i]);
  121.  
  122. }
  123. */
  124. //LOSOWE ZASTĘPOWANIE
  125. /*if (pierwszy != true)
  126. {
  127. List<String> temp = lancuchy.Concat(koncowe).ToList();
  128. lancuchy = new List<String>();
  129. for (int i = 0; i < 10; i++)
  130. lancuchy.Add(temp[Util.GetRandomCustom(temp.Count)]);
  131.  
  132. }
  133. */
  134. List<double> odwzorowane = new List<double>();
  135. for (int i = 0; i < il; i++)
  136. {
  137. odwzorowane.Add(doprzedzialu(bintodec(lancuchy[i]), a, b, m));
  138. }
  139. List<double> rastriginlista = new List<double>();
  140. for (int i = 0; i < il; i++)
  141. {
  142. rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n));
  143. }
  144. Console.WriteLine("Dziedzina zmienności = " + dziedzm + " Il kombinacji = " + ilosckom + " m = " + m + " dł wektora bin = " + dlugwekbin);
  145. Console.WriteLine();
  146. Console.WriteLine("Wektory binarne:");
  147. for (int i = 0; i < il; i++)
  148. Console.WriteLine(lancuchy[i]);
  149. Console.WriteLine();
  150. Console.WriteLine("Odwzorowane lancuchy binarne na liczbę X z zakresu [" + a + "," + b + "]:");
  151. for (int i = 0; i < il; i++)
  152. Console.WriteLine(odwzorowane[i]);
  153. Console.WriteLine();
  154. Console.WriteLine("Wartości funkcji Rastrigina:");
  155. for (int i = 0; i < il; i++)
  156. Console.WriteLine(rastriginlista[i]);
  157.  
  158. Console.WriteLine();
  159. Console.WriteLine("RULETKA:");
  160. Console.WriteLine();
  161. double F = 0;
  162. for (int i = 0; i < il; i++)
  163. F += rastriginlista[i];
  164. //Console.WriteLine("Całkowite dopasowanie F = " + F);
  165. List<double> prawdopodobienstwa_wyboru = new List<double>();
  166. for (int i = 0; i < il; i++)
  167. prawdopodobienstwa_wyboru.Add(rastriginlista[i] / F);
  168. /*Console.WriteLine("Prawdopodobienstwa wyboru:");
  169. for (int i = 0; i < il; i++)
  170. Console.WriteLine(prawdopodobienstwa_wyboru[i]);*/
  171. List<double> dystrybuanty = new List<double>();
  172. for (int i = 0; i < il; i++)
  173. {
  174. double dystr = 0;
  175. for (int j = 0; j <= i; j++)
  176. dystr += prawdopodobienstwa_wyboru[j];
  177. dystrybuanty.Add(dystr);
  178. }
  179. /*Console.WriteLine("Dystrybuanty:");
  180. for (int i = 0; i < il; i++)
  181. Console.WriteLine(dystrybuanty[i]);*/
  182. List<String> ruletka = new List<string>();
  183. for (int i = 0; i < il; i++)
  184. {
  185. double r = Util.GetRandom02d();
  186. for (int j = 0; j < il; j++)
  187. {
  188. if (r < dystrybuanty[j])
  189. {
  190. //Console.WriteLine("Wybrany osobnik z indeksem " + j + ": " + lancuchy[j]);
  191. ruletka.Add(lancuchy[j]);
  192. break;
  193. }
  194. }
  195. }
  196. for (int i = 0; i < il; i++)
  197. Console.WriteLine(ruletka[i]);
  198.  
  199. Console.WriteLine();
  200. Console.WriteLine("MUTACJA:");
  201. Console.WriteLine();
  202. double pm = 0.1;
  203. List<String> lancuchymut = new List<String>();
  204. for (int i = 0; i < ruletka.Count; i++)
  205. {
  206. string st = "";
  207. for (int j = 0; j < m; j++)
  208. {
  209. double rn = Util.GetRandom02d();
  210. if (rn < pm)
  211. {
  212. if (ruletka[i][j] == '0')
  213. st = st.Insert(0, "1");
  214. else if (ruletka[i][j] == '1')
  215. st = st.Insert(0, "0");
  216. }
  217. else
  218. {
  219. st = st.Insert(0, ruletka[i][j].ToString());
  220. }
  221. }
  222. lancuchymut.Add(Reverse(st));
  223. }
  224. /* Console.WriteLine("PO MUTACJI:");
  225. for (int i = 0; i < il; i++)
  226. Console.WriteLine(lancuchymut[i]);*/
  227.  
  228. Console.WriteLine();
  229. Console.WriteLine("INWERSJA:");
  230. Console.WriteLine();
  231. List<String> inw = new List<String>();
  232. double pi = 0.1;
  233. for (int i = 0; i < ruletka.Count; i++)
  234. {
  235. double rnd = Util.GetRandom02d();
  236. if(rnd < pi)
  237. {
  238. int p1 = Util.GetRandomCustom(m - 1);
  239. int p2 = Util.GetRandomCustom(m - 1);
  240. int pocz;
  241. int kon;
  242. if (p1 < p2)
  243. {
  244. pocz = p1;
  245. kon = p2;
  246. }
  247. else
  248. {
  249. pocz = p2;
  250. kon = p1;
  251. }
  252. string lancuch = ruletka[i];
  253. for(int j = pocz + 1; j < kon; j++)
  254. {
  255. if (lancuch[j] == '0')
  256. lancuch = lancuch.ReplaceAt(j, '1');
  257. else if (lancuch[j] == '1')
  258. lancuch = lancuch.ReplaceAt(j, '0');
  259. }
  260. inw.Add(lancuch);
  261. }
  262. }
  263. /* for (int i = 0; i < inw.Count; i++)
  264. Console.WriteLine(inw[i]);*/
  265.  
  266. Console.WriteLine();
  267. Console.WriteLine("KRZYŻOWANIE JEDNOPUNKTOWE:");
  268. Console.WriteLine();
  269. List<String> rulkopia = ruletka;
  270. double pk = 0.6;
  271. List<String> ldokrzyz = new List<String>();
  272. for (int i = il - 1; i >= 0; i--)
  273. {
  274. double r = Util.GetRandom02d();
  275. if (r < pk)
  276. {
  277. ldokrzyz.Add(rulkopia[i]);
  278. rulkopia.RemoveAt(i);
  279. }
  280. }
  281. if (ldokrzyz.Count % 2 != 0)
  282. {
  283. int rnd = Util.GetRandomCustom(rulkopia.Count);
  284. ldokrzyz.Add(rulkopia[rnd]);
  285. rulkopia.RemoveAt(rnd);
  286. }
  287. /*for (int i = 0; i < ldokrzyz.Count; i++)
  288. Console.WriteLine(ldokrzyz[i]);*/
  289. List<String> lkrzyzfin = new List<String>();
  290. int ile = ldokrzyz.Count / 2;
  291. for (int i = 0; i < ile; i++)
  292. {
  293. List<String> lkrzyztemp = new List<String>();
  294. int los = Util.GetRandomCustom(ldokrzyz.Count);
  295. lkrzyztemp.Add(ldokrzyz[los]);
  296. ldokrzyz.RemoveAt(los);
  297. los = Util.GetRandomCustom(ldokrzyz.Count);
  298. lkrzyztemp.Add(ldokrzyz[los]);
  299. ldokrzyz.RemoveAt(los);
  300. int punkt = Util.GetRandomCustom(m - 1);
  301. string t1 = lkrzyztemp[0];
  302. string t2 = lkrzyztemp[1];
  303. for (int j = punkt + 1; j < m; j++)
  304. {
  305. lkrzyztemp[0] = lkrzyztemp[0].ReplaceAt(j, t2[j]);
  306. lkrzyztemp[1] = lkrzyztemp[1].ReplaceAt(j, t1[j]);
  307. }
  308. lkrzyzfin.Add(lkrzyztemp[0]);
  309. lkrzyzfin.Add(lkrzyztemp[1]);
  310. }
  311.  
  312. List<String> wszystkie = new List<string>();
  313. wszystkie = lancuchymut.Concat(inw).Concat(rulkopia).ToList();
  314. odwzorowane = new List<double>();
  315. for (int i = 0; i < wszystkie.Count; i++)
  316. {
  317. odwzorowane.Add(doprzedzialu(bintodec(wszystkie[i]), a, b, m));
  318. }
  319. rastriginlista = new List<double>();
  320. for (int i = 0; i < odwzorowane.Count; i++)
  321. {
  322. rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n));
  323. }
  324. List<Tuple<double, string>> polaczone = new List<Tuple<double, string>>();
  325. for (int i = 0; i < wszystkie.Count; i++)
  326. polaczone.Add(new Tuple<double, String>(rastriginlista[i], wszystkie[i]));
  327. polaczone.Sort();
  328. polaczone.Reverse();
  329. Console.WriteLine("WSZYSTKIE:");
  330. for (int i = 0; i < polaczone.Count; i++)
  331. Console.WriteLine(polaczone[i]);
  332. koncowe = new List<String>();
  333. for(int i = 0; i < 10; i++)
  334. {
  335. koncowe.Add(polaczone[i].Item2);
  336. }
  337. odwzorowane = new List<double>();
  338. for (int i = 0; i < koncowe.Count; i++)
  339. {
  340. odwzorowane.Add(doprzedzialu(bintodec(koncowe[i]), a, b, m));
  341. }
  342. rastriginlista = new List<double>();
  343. for (int i = 0; i < odwzorowane.Count; i++)
  344. {
  345. rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n));
  346. }
  347. rastriginlista.Sort();
  348. polaczone = new List<Tuple<double, string>>();
  349. for (int i = 0; i < koncowe.Count; i++)
  350. polaczone.Add(new Tuple<double, String>(rastriginlista[i], koncowe[i]));
  351. polaczone.Sort();
  352. polaczone.Reverse();
  353. pierwszy = false;
  354. Console.WriteLine("WYNIK:");
  355. for (int i = 0; i < polaczone.Count; i++)
  356. Console.WriteLine(polaczone[i]);
  357. }
  358.  
  359. Console.ReadKey();
  360. }
  361. }
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement