Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.41 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 ConsoleApp1
  8. {
  9. class Program
  10. {
  11. //public static int[,] weight = new int[3, 3] { { 0, -1, -3 },{ -1, 0, 2 },{ -3, 2, 0 } };
  12. public static int[,] weight = new int[3, 3] { { 0, 1, -1 }, { 1, 0, 1 }, { -1, 1, 0 } };
  13. public static int[] vectorIn = new int[3];
  14. private static int[][] vectors = new int[100][];
  15. private static bool sync = false;
  16. private static bool async = false;
  17. private static bool hopfieldB = false;
  18. private static int step = 0;
  19. public static int[] wIn = new int[24];
  20. static void Main(string[] args)
  21. {
  22. ChooseSyncOrAsync(); // wybór trybu
  23. if(sync)
  24. {
  25. Console.Clear();
  26. SetVectorIn();//wprowadź wektor
  27. ShowVector();//pokaż wektor
  28. int[] newVector = new int[3];
  29. int[] oldVector = new int[3];
  30. int e=0, e1=0, e2=0;
  31. while (sync)
  32. {
  33. Console.WriteLine("U" + step.ToString());
  34. if (step == 0)
  35. {
  36. LiczU(vectorIn,oldVector);//vectorIn - v(0) a oldVector V(1) po obliczeniu u(1)
  37. ShowBothVectros(vectorIn, oldVector);
  38. vectors[step] = new int[3] { vectorIn[0], vectorIn[1], vectorIn[2] };
  39. step++;
  40. e = countEnergy(oldVector, vectorIn);
  41. Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(oldVector, vectorIn));
  42. }
  43. else if(step > 0 && step < 8)
  44. {
  45. if (step == 1)
  46. {
  47. ShowBothVectros(oldVector, newVector);
  48. }
  49. else if (step > 1)
  50. {
  51. ShowBothVectros(oldVector, newVector);
  52. }
  53. LiczU(oldVector, newVector);
  54. Console.WriteLine("Energia E(" + step.ToString() +") = " + countEnergy(newVector, oldVector));
  55.  
  56. if (compareArrays(newVector, oldVector))//pkt stały
  57. {
  58. Console.WriteLine("Wektor wyjściowy V(" + (step + 1).ToString() + ") jest taki sam co wektory wejściowy V(" + (step).ToString() + "). Jest to punkt stały.");
  59. Console.ReadKey();
  60. break;
  61. }
  62. if(!compareArrays(newVector, oldVector))
  63. {
  64. Console.WriteLine("Wektor [" + oldVector[0] + ", " + oldVector[1] + ", " + oldVector[2] + "] jest zbieżny do [" + newVector[0] + ", " + newVector[1] + ", " + newVector[2] + "]");
  65. Console.ReadKey();
  66. break;
  67. }
  68.  
  69. if(step % 2 != 0)//e1
  70. {
  71. e1 = countEnergy(newVector, oldVector);
  72. }
  73. else //e2
  74. {
  75. e2 = countEnergy(newVector, oldVector);
  76. }
  77. if (step == 1)
  78. {
  79. if((e1 - e) == 0)
  80. {
  81. Console.WriteLine("Wystąpiła oscylacja ponieważ E(t) - E(t-1) = 0");
  82. }
  83. }
  84. else
  85. {
  86. if(step % 2 == 0)
  87. {
  88. if((e2 - e1) == 0)
  89. Console.WriteLine("Występuje oscylacja ponieważ E(t) - E(t-1) = " + (e2 - e1));
  90. }
  91. else
  92. {
  93. if((e1-e2) == 0)
  94. Console.WriteLine("Występuje oscylacja ponieważ E(t) - E(t-1) = " + (e1 - e2));
  95. }
  96. }
  97.  
  98. oldVector [0] = newVector [0];
  99. oldVector [1] = newVector [1];
  100. oldVector [2] = newVector [2];
  101. newVector [0] = 0;
  102. newVector [1] = 0;
  103. newVector [2] = 0;
  104. step++;
  105.  
  106. }
  107. else
  108. {
  109. sync = false;
  110. Console.WriteLine("Przekroczono maksymalną liczę kroków");
  111. break;
  112. }
  113. }
  114. }
  115. else if(async)
  116. {
  117. Console.Clear();
  118. SetVectorIn();//wprowadź wektor
  119. ShowVector();//pokaż wektor
  120. int[] newVector = new int[3];
  121. int[] oldVector = new int[3];
  122. int[] tmp = new int[3];
  123. int count = 0;
  124. while(async)
  125. {
  126. Console.WriteLine("U" + step.ToString());
  127.  
  128. if (step == 0)
  129. {
  130. tmp[0] = vectorIn[0]; tmp[1] = vectorIn[1]; tmp[2] = vectorIn[2];
  131. LiczUAsyncFirst(tmp, newVector);
  132. ShowBothVectros(tmp, newVector);
  133. Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
  134. tmp[0] = newVector[0];
  135. LiczUAsyncSecond(tmp, newVector);
  136. ShowBothVectros(tmp, newVector);
  137. Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
  138. tmp[1] = newVector[1];
  139. LiczUAsyncThird(tmp, newVector);
  140. ShowBothVectros(tmp, newVector);
  141. Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
  142. tmp[2] = newVector[2];
  143. oldVector[0] = newVector[0]; oldVector[1] = newVector[1]; oldVector[2] = newVector[2];
  144. vectors[step] = new int[3]{ newVector[0], newVector[1], newVector[2]};
  145. tmp[0] = 0; tmp[1] = 0; tmp[2] = 0;
  146.  
  147. step++;
  148. }
  149. else if(step > 0 && step < 24)
  150. {
  151.  
  152. LiczUAsyncFirst(oldVector, tmp);
  153. ShowBothVectros(tmp, newVector);
  154. Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
  155. oldVector[0] = tmp[0];
  156. LiczUAsyncSecond(oldVector, tmp);
  157. ShowBothVectros(tmp, newVector);
  158. Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
  159. oldVector[1] = tmp[1];
  160. LiczUAsyncThird(oldVector, tmp);
  161. ShowBothVectros(tmp, newVector);
  162. Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
  163. oldVector[2] = tmp[2];
  164. vectors[step] = new int[3] { tmp[0], tmp[1], tmp[2]};
  165.  
  166. if (compareArrays(tmp,oldVector))
  167. {
  168. count++;
  169. }
  170. else
  171. {
  172. count = 0;
  173. }
  174. if(count == 3)
  175. {
  176. Console.WriteLine("Sieć podczas działania wyprodukowała taki sam wektor jaki trafił na wejście w 3 kolejnych krokach");
  177. Console.ReadKey();
  178. break;
  179. }
  180.  
  181. oldVector[0] = tmp[0]; oldVector[1] = tmp[1]; oldVector[2] = tmp[2];
  182. tmp[0] = 0; tmp[1] = 0; tmp[2] = 0;
  183. step++;
  184. }
  185. else
  186. {
  187. async = false;
  188. Console.WriteLine("Przekroczono maksymalną liczę 24 kroków");
  189. break;
  190. }
  191. }
  192. }
  193. else if(hopfieldB)
  194. {
  195. Start:
  196. //public static int[,] weight = new int[3, 3] { { 0, 1, -1 }, { 1, 0, 1 }, { -1, 1, 0 } };
  197. SetwIn();//wprowadź wektor
  198. ShowVectorWIn(wIn);//pokaż wektor
  199. int[,] hopfield = new int[24,24];//macierz W
  200. int[,] wzorzec = new int[24,1]; //p
  201. int[,] wzorzec1 = new int[1,24];//p transponowane
  202. int[,] tmp = new int[24, 24];
  203.  
  204. int[] w1 = new int[24] {1, -1, -1, 1 , -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1};
  205. int[] w2 = new int[24] { -1, 1, 1, 1 , 1, -1, -1, 1 , -1, -1, 1, -1 , -1, 1, -1, -1 , 1, -1, -1, -1 , 1, 1, 1, 1 };
  206. int[] w3 = new int[24] { 1, 1, 1, 1 , 1, -1, -1, -1 , 1, 1, 1, -1 , -1, -1, -1, 1 , -1, -1, -1, 1 , 1, 1, 1, -1 };
  207.  
  208. //uczenie sieci
  209. for (int t = 0; t < 24; t++)
  210. {
  211. wzorzec[t, 0] = w1[t];
  212. wzorzec1[0, t] = w1[t];
  213.  
  214. }//do obliczenia u
  215. //liczenie U
  216. for (int o = 0; o < 24; o++)//wiersz
  217. {
  218. for (int p = 0; p < 24; p++)//kolumna
  219. {
  220. tmp[o, p] = wzorzec[p, 0] * wzorzec1[0, p];
  221. }
  222. }
  223.  
  224. //wstawianie na przekątnej 0
  225. for (int u = 0; u < 24; u++)
  226. {
  227. tmp[u, u] -= 1;
  228. }
  229.  
  230. for (int q = 0; q < 24; q++)//dodanie W+=U
  231. {
  232. for (int w = 0; w < 24; w++)
  233. {
  234. hopfield[q, w] += tmp[q, w];
  235. }
  236. }
  237.  
  238.  
  239. for (int t = 0; t < 24; t++)
  240. {
  241. wzorzec[t, 0] = w2[t];
  242. wzorzec1[0, t] = w2[t];
  243.  
  244. }//do obliczenia u
  245. //liczenie U
  246. for (int o = 0; o < 24; o++)//wiersz
  247. {
  248. for (int p = 0; p < 24; p++)//kolumna
  249. {
  250. tmp[o, p] = wzorzec[p, 0] * wzorzec1[0, p];
  251. }
  252. }
  253.  
  254. //wstawianie na przekątnej 0
  255. for (int u = 0; u < 24; u++)
  256. {
  257. tmp[u, u] -= 1;
  258. }
  259.  
  260. for (int q = 0; q < 24; q++)//dodanie W+=U
  261. {
  262. for (int w = 0; w < 24; w++)
  263. {
  264. hopfield[q, w] += tmp[q, w];
  265. }
  266. }
  267.  
  268. for (int t = 0; t < 24; t++)
  269. {
  270. wzorzec[t, 0] = w3[t];
  271. wzorzec1[0, t] = w3[t];
  272.  
  273. }//do obliczenia u
  274. //liczenie U
  275. for (int o = 0; o < 24; o++)//wiersz
  276. {
  277. for (int p = 0; p < 24; p++)//kolumna
  278. {
  279. tmp[o, p] = wzorzec[p, 0] * wzorzec1[0, p];
  280. }
  281. }
  282.  
  283. //wstawianie na przekątnej 0
  284. for (int u = 0; u < 24; u++)
  285. {
  286. tmp[u, u] -= 1;
  287. }
  288.  
  289. for (int q = 0; q < 24; q++)//dodanie W+=U
  290. {
  291. for (int w = 0; w < 24; w++)
  292. {
  293. hopfield[q, w] += tmp[q, w];
  294. }
  295. }
  296.  
  297.  
  298.  
  299. //
  300.  
  301.  
  302. for (int t = 0; t < 24; t++)
  303. {
  304. wzorzec[t, 0] = wIn[t];
  305. wzorzec1[0, t] = wIn[t];
  306.  
  307. }//do obliczenia u
  308. //liczenie zasranego U
  309. for(int o = 0; o < 24; o++)//wiersz
  310. {
  311. for(int p = 0; p < 24; p++)//kolumna
  312. {
  313. tmp[o, p] = wzorzec[p, 0] * wzorzec1[0,p];
  314. }
  315. }
  316.  
  317. //wstawianie na przekątnej 0
  318. for(int u = 0; u < 24; u++)
  319. {
  320. tmp[u, u] -= 1;
  321. }
  322.  
  323. for(int q = 0; q < 24; q++)//dodanie W+=U
  324. {
  325. for(int w = 0; w < 24; w++)
  326. {
  327. hopfield[q, w] += tmp[q, w];
  328. }
  329. }
  330. Console.WriteLine("");
  331. for(int h = 0; h < 24; h++)
  332. {
  333. for(int g = 0; g < 24; g++)
  334. {
  335. Console.Write(hopfield[h, g]);
  336. }
  337. Console.Write("\n");
  338. }
  339. Console.WriteLine("Czy chcesz dodać kolejny wzorzec? T/N");
  340. string lastAns = Console.ReadLine().ToUpper();
  341. if(lastAns == "T")
  342. {
  343. goto Start;
  344. }
  345. else
  346. {
  347. hopfieldB = false;
  348. }
  349. }
  350. else if(!sync && !async && !hopfieldB)
  351. {
  352. Console.WriteLine("Coś poszło nie tak");
  353. }
  354. Console.ReadKey();
  355. }
  356. private static void SetVectorIn()
  357. {
  358. int[] tmp = new int[3] { 0, 0, 0 };
  359. Console.WriteLine("Wprowadź wektor wejściowy V(0):");
  360. for( int i = 0; i < 3; i++)
  361. {
  362. Console.WriteLine("{" + tmp[0] + ", " + tmp[1] + ", " + tmp[2] + "}");
  363. int inputTmp = Convert.ToInt32(Console.ReadLine());
  364. tmp[i] = inputTmp;
  365. vectorIn[i] = inputTmp;
  366. }
  367. }
  368. private static void SetwIn()
  369. {
  370. Console.WriteLine("Wprowadź wektor wejściowy V(0):");
  371. for (int i = 0; i < 24; i++)
  372. {
  373. int inputTmp = Convert.ToInt32(Console.ReadLine());
  374. wIn[i] = inputTmp;
  375. }
  376. for(int j =0; j< 24; j++)
  377. {
  378. Console.Write(wIn[j] +", ");
  379. }
  380. }
  381.  
  382. private static void ShowVector()
  383. {
  384. Console.WriteLine("Wektor V(0) = {" + vectorIn[0] + ", " + vectorIn[1] + ", " + vectorIn[2] + "}");
  385. }
  386.  
  387. private static void ShowVectorWIn (int[] s)
  388. {
  389. for (int j = 0; j < 24; j++)
  390. {
  391. Console.Write(s[j] + ", ");
  392. }
  393. }
  394.  
  395. private static void ShowBothVectros (int[] first, int[] second)
  396. {
  397. Console.WriteLine("\nWektor wejściowy V("+step.ToString()+") = {" + first[0] + ", " + first[1] + ", " + first[2] + "}");
  398. Console.WriteLine("Wektor wyjściowy V("+(step+1).ToString()+") = {" + second[0] + ", " + second[1] + ", " + second[2] + "}");
  399. }
  400.  
  401. private static bool compareArrays(int[] x, int[] y)
  402. {
  403. if( x[0] == y[0] &&
  404. x[2] == y[1] &&
  405. x[1] == y[2])
  406. {
  407. return true;
  408. }
  409. else
  410. {
  411. return false;
  412. }
  413. }
  414. private static void ChooseSyncOrAsync()
  415. {
  416. Console.WriteLine("W którym trybie program ma zostać uruchomiony? \n1-Synchroniczym \n2-Asynchronicznym \n Jeśli chces uruchomić Hopfielda dla n=24 wybierz 3");
  417. int tmp = Convert.ToInt32(Console.ReadLine());
  418. if(tmp == 1)
  419. {
  420. sync = true;
  421. }
  422. else if(tmp == 2 )
  423. {
  424. async = true;
  425. }
  426. else if(tmp == 3)
  427. {
  428. hopfieldB = true;
  429. }
  430. else
  431. {
  432. Console.WriteLine("Cos poszło nie tak...");
  433. }
  434. //Console.ReadKey();
  435. }
  436. private static int Bipolarna( int x) // potrzebna do funkcji aktywacji
  437. {
  438. if (x <= 0)
  439. {
  440. return -1;
  441. }
  442. else
  443. {
  444. return 1;
  445. }
  446. }
  447. private static int[] LiczU(int[] vs, int[] wyn)
  448. {
  449. wyn[0] = Bipolarna(weight[0,0] * vs[0] + weight[0,1] * vs[1] + weight[0,2] * vs[2]);
  450. wyn[1] = Bipolarna(weight[1, 0] * vs[0] + weight[1, 1] * vs[1] + weight[1, 2] * vs[2]);
  451. wyn[2] = Bipolarna(weight[2,0] * vs[0] + weight[2,1] * vs[1] + weight[2,2] * vs[2]);
  452. vectors[step] = new int[3] { wyn[0], wyn[1], wyn[2] };
  453. return wyn;
  454. }
  455. private static int[] LiczUAsyncFirst(int[] input, int[] output)
  456. {
  457. output[0] = Bipolarna(weight[0, 0] * input[0] + weight[0, 1] * input[1] + weight[0, 2] * input[2]);
  458. return output;
  459. }
  460. private static int[] LiczUAsyncSecond(int[] input, int[] output)
  461. {
  462. output[1] = Bipolarna(weight[1, 0] * input[0] + weight[1, 1] * input[1] + weight[1, 2] * input[2]);
  463. return output;
  464. }
  465. private static int[] LiczUAsyncThird(int[] input, int[] output)
  466. {
  467. output[2] = Bipolarna(weight[2, 0] * input[0] + weight[2, 1] * input[1] + weight[2, 2] * input[2]);
  468. return output;
  469. }
  470. private static int countEnergy(int[] t1, int[] t2)
  471. {
  472. int e = 0;
  473. e = (t1[0] * t2[1]) + 3 * (t1[0] * t2[2]) + (t1[1] * t2[0]) - 2 * (t1[1] * t2[2]) + 3 * (t1[2] * t2[0]) - 2 * (t1[2] * t2[1]);
  474. return e;
  475. }
  476. }
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement