Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- //public static int[,] weight = new int[3, 3] { { 0, -1, -3 },{ -1, 0, 2 },{ -3, 2, 0 } };
- public static int[,] weight = new int[3, 3] { { 0, 1, -1 }, { 1, 0, 1 }, { -1, 1, 0 } };
- public static int[] vectorIn = new int[3];
- private static int[][] vectors = new int[100][];
- private static bool sync = false;
- private static bool async = false;
- private static bool hopfieldB = false;
- private static int step = 0;
- public static int[] wIn = new int[24];
- static void Main(string[] args)
- {
- ChooseSyncOrAsync(); // wybór trybu
- if(sync)
- {
- Console.Clear();
- SetVectorIn();//wprowadź wektor
- ShowVector();//pokaż wektor
- int[] newVector = new int[3];
- int[] oldVector = new int[3];
- int e=0, e1=0, e2=0;
- while (sync)
- {
- Console.WriteLine("U" + step.ToString());
- if (step == 0)
- {
- LiczU(vectorIn,oldVector);//vectorIn - v(0) a oldVector V(1) po obliczeniu u(1)
- ShowBothVectros(vectorIn, oldVector);
- vectors[step] = new int[3] { vectorIn[0], vectorIn[1], vectorIn[2] };
- step++;
- e = countEnergy(oldVector, vectorIn);
- Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(oldVector, vectorIn));
- }
- else if(step > 0 && step < 8)
- {
- if (step == 1)
- {
- ShowBothVectros(oldVector, newVector);
- }
- else if (step > 1)
- {
- ShowBothVectros(oldVector, newVector);
- }
- LiczU(oldVector, newVector);
- Console.WriteLine("Energia E(" + step.ToString() +") = " + countEnergy(newVector, oldVector));
- if (compareArrays(newVector, oldVector))//pkt stały
- {
- Console.WriteLine("Wektor wyjściowy V(" + (step + 1).ToString() + ") jest taki sam co wektory wejściowy V(" + (step).ToString() + "). Jest to punkt stały.");
- Console.ReadKey();
- break;
- }
- if(!compareArrays(newVector, oldVector))
- {
- Console.WriteLine("Wektor [" + oldVector[0] + ", " + oldVector[1] + ", " + oldVector[2] + "] jest zbieżny do [" + newVector[0] + ", " + newVector[1] + ", " + newVector[2] + "]");
- Console.ReadKey();
- break;
- }
- if(step % 2 != 0)//e1
- {
- e1 = countEnergy(newVector, oldVector);
- }
- else //e2
- {
- e2 = countEnergy(newVector, oldVector);
- }
- if (step == 1)
- {
- if((e1 - e) == 0)
- {
- Console.WriteLine("Wystąpiła oscylacja ponieważ E(t) - E(t-1) = 0");
- }
- }
- else
- {
- if(step % 2 == 0)
- {
- if((e2 - e1) == 0)
- Console.WriteLine("Występuje oscylacja ponieważ E(t) - E(t-1) = " + (e2 - e1));
- }
- else
- {
- if((e1-e2) == 0)
- Console.WriteLine("Występuje oscylacja ponieważ E(t) - E(t-1) = " + (e1 - e2));
- }
- }
- oldVector [0] = newVector [0];
- oldVector [1] = newVector [1];
- oldVector [2] = newVector [2];
- newVector [0] = 0;
- newVector [1] = 0;
- newVector [2] = 0;
- step++;
- }
- else
- {
- sync = false;
- Console.WriteLine("Przekroczono maksymalną liczę kroków");
- break;
- }
- }
- }
- else if(async)
- {
- Console.Clear();
- SetVectorIn();//wprowadź wektor
- ShowVector();//pokaż wektor
- int[] newVector = new int[3];
- int[] oldVector = new int[3];
- int[] tmp = new int[3];
- int count = 0;
- while(async)
- {
- Console.WriteLine("U" + step.ToString());
- if (step == 0)
- {
- tmp[0] = vectorIn[0]; tmp[1] = vectorIn[1]; tmp[2] = vectorIn[2];
- LiczUAsyncFirst(tmp, newVector);
- ShowBothVectros(tmp, newVector);
- Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
- tmp[0] = newVector[0];
- LiczUAsyncSecond(tmp, newVector);
- ShowBothVectros(tmp, newVector);
- Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
- tmp[1] = newVector[1];
- LiczUAsyncThird(tmp, newVector);
- ShowBothVectros(tmp, newVector);
- Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
- tmp[2] = newVector[2];
- oldVector[0] = newVector[0]; oldVector[1] = newVector[1]; oldVector[2] = newVector[2];
- vectors[step] = new int[3]{ newVector[0], newVector[1], newVector[2]};
- tmp[0] = 0; tmp[1] = 0; tmp[2] = 0;
- step++;
- }
- else if(step > 0 && step < 24)
- {
- LiczUAsyncFirst(oldVector, tmp);
- ShowBothVectros(tmp, newVector);
- Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
- oldVector[0] = tmp[0];
- LiczUAsyncSecond(oldVector, tmp);
- ShowBothVectros(tmp, newVector);
- Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
- oldVector[1] = tmp[1];
- LiczUAsyncThird(oldVector, tmp);
- ShowBothVectros(tmp, newVector);
- Console.WriteLine("Energia E(" + step.ToString() + ") = " + countEnergy(tmp, newVector));
- oldVector[2] = tmp[2];
- vectors[step] = new int[3] { tmp[0], tmp[1], tmp[2]};
- if (compareArrays(tmp,oldVector))
- {
- count++;
- }
- else
- {
- count = 0;
- }
- if(count == 3)
- {
- Console.WriteLine("Sieć podczas działania wyprodukowała taki sam wektor jaki trafił na wejście w 3 kolejnych krokach");
- Console.ReadKey();
- break;
- }
- oldVector[0] = tmp[0]; oldVector[1] = tmp[1]; oldVector[2] = tmp[2];
- tmp[0] = 0; tmp[1] = 0; tmp[2] = 0;
- step++;
- }
- else
- {
- async = false;
- Console.WriteLine("Przekroczono maksymalną liczę 24 kroków");
- break;
- }
- }
- }
- else if(hopfieldB)
- {
- Start:
- //public static int[,] weight = new int[3, 3] { { 0, 1, -1 }, { 1, 0, 1 }, { -1, 1, 0 } };
- SetwIn();//wprowadź wektor
- ShowVectorWIn(wIn);//pokaż wektor
- int[,] hopfield = new int[24,24];//macierz W
- int[,] wzorzec = new int[24,1]; //p
- int[,] wzorzec1 = new int[1,24];//p transponowane
- int[,] tmp = new int[24, 24];
- 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};
- 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 };
- 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 };
- //uczenie sieci
- for (int t = 0; t < 24; t++)
- {
- wzorzec[t, 0] = w1[t];
- wzorzec1[0, t] = w1[t];
- }//do obliczenia u
- //liczenie U
- for (int o = 0; o < 24; o++)//wiersz
- {
- for (int p = 0; p < 24; p++)//kolumna
- {
- tmp[o, p] = wzorzec[p, 0] * wzorzec1[0, p];
- }
- }
- //wstawianie na przekątnej 0
- for (int u = 0; u < 24; u++)
- {
- tmp[u, u] -= 1;
- }
- for (int q = 0; q < 24; q++)//dodanie W+=U
- {
- for (int w = 0; w < 24; w++)
- {
- hopfield[q, w] += tmp[q, w];
- }
- }
- for (int t = 0; t < 24; t++)
- {
- wzorzec[t, 0] = w2[t];
- wzorzec1[0, t] = w2[t];
- }//do obliczenia u
- //liczenie U
- for (int o = 0; o < 24; o++)//wiersz
- {
- for (int p = 0; p < 24; p++)//kolumna
- {
- tmp[o, p] = wzorzec[p, 0] * wzorzec1[0, p];
- }
- }
- //wstawianie na przekątnej 0
- for (int u = 0; u < 24; u++)
- {
- tmp[u, u] -= 1;
- }
- for (int q = 0; q < 24; q++)//dodanie W+=U
- {
- for (int w = 0; w < 24; w++)
- {
- hopfield[q, w] += tmp[q, w];
- }
- }
- for (int t = 0; t < 24; t++)
- {
- wzorzec[t, 0] = w3[t];
- wzorzec1[0, t] = w3[t];
- }//do obliczenia u
- //liczenie U
- for (int o = 0; o < 24; o++)//wiersz
- {
- for (int p = 0; p < 24; p++)//kolumna
- {
- tmp[o, p] = wzorzec[p, 0] * wzorzec1[0, p];
- }
- }
- //wstawianie na przekątnej 0
- for (int u = 0; u < 24; u++)
- {
- tmp[u, u] -= 1;
- }
- for (int q = 0; q < 24; q++)//dodanie W+=U
- {
- for (int w = 0; w < 24; w++)
- {
- hopfield[q, w] += tmp[q, w];
- }
- }
- //
- for (int t = 0; t < 24; t++)
- {
- wzorzec[t, 0] = wIn[t];
- wzorzec1[0, t] = wIn[t];
- }//do obliczenia u
- //liczenie zasranego U
- for(int o = 0; o < 24; o++)//wiersz
- {
- for(int p = 0; p < 24; p++)//kolumna
- {
- tmp[o, p] = wzorzec[p, 0] * wzorzec1[0,p];
- }
- }
- //wstawianie na przekątnej 0
- for(int u = 0; u < 24; u++)
- {
- tmp[u, u] -= 1;
- }
- for(int q = 0; q < 24; q++)//dodanie W+=U
- {
- for(int w = 0; w < 24; w++)
- {
- hopfield[q, w] += tmp[q, w];
- }
- }
- Console.WriteLine("");
- for(int h = 0; h < 24; h++)
- {
- for(int g = 0; g < 24; g++)
- {
- Console.Write(hopfield[h, g]);
- }
- Console.Write("\n");
- }
- Console.WriteLine("Czy chcesz dodać kolejny wzorzec? T/N");
- string lastAns = Console.ReadLine().ToUpper();
- if(lastAns == "T")
- {
- goto Start;
- }
- else
- {
- hopfieldB = false;
- }
- }
- else if(!sync && !async && !hopfieldB)
- {
- Console.WriteLine("Coś poszło nie tak");
- }
- Console.ReadKey();
- }
- private static void SetVectorIn()
- {
- int[] tmp = new int[3] { 0, 0, 0 };
- Console.WriteLine("Wprowadź wektor wejściowy V(0):");
- for( int i = 0; i < 3; i++)
- {
- Console.WriteLine("{" + tmp[0] + ", " + tmp[1] + ", " + tmp[2] + "}");
- int inputTmp = Convert.ToInt32(Console.ReadLine());
- tmp[i] = inputTmp;
- vectorIn[i] = inputTmp;
- }
- }
- private static void SetwIn()
- {
- Console.WriteLine("Wprowadź wektor wejściowy V(0):");
- for (int i = 0; i < 24; i++)
- {
- int inputTmp = Convert.ToInt32(Console.ReadLine());
- wIn[i] = inputTmp;
- }
- for(int j =0; j< 24; j++)
- {
- Console.Write(wIn[j] +", ");
- }
- }
- private static void ShowVector()
- {
- Console.WriteLine("Wektor V(0) = {" + vectorIn[0] + ", " + vectorIn[1] + ", " + vectorIn[2] + "}");
- }
- private static void ShowVectorWIn (int[] s)
- {
- for (int j = 0; j < 24; j++)
- {
- Console.Write(s[j] + ", ");
- }
- }
- private static void ShowBothVectros (int[] first, int[] second)
- {
- Console.WriteLine("\nWektor wejściowy V("+step.ToString()+") = {" + first[0] + ", " + first[1] + ", " + first[2] + "}");
- Console.WriteLine("Wektor wyjściowy V("+(step+1).ToString()+") = {" + second[0] + ", " + second[1] + ", " + second[2] + "}");
- }
- private static bool compareArrays(int[] x, int[] y)
- {
- if( x[0] == y[0] &&
- x[2] == y[1] &&
- x[1] == y[2])
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private static void ChooseSyncOrAsync()
- {
- 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");
- int tmp = Convert.ToInt32(Console.ReadLine());
- if(tmp == 1)
- {
- sync = true;
- }
- else if(tmp == 2 )
- {
- async = true;
- }
- else if(tmp == 3)
- {
- hopfieldB = true;
- }
- else
- {
- Console.WriteLine("Cos poszło nie tak...");
- }
- //Console.ReadKey();
- }
- private static int Bipolarna( int x) // potrzebna do funkcji aktywacji
- {
- if (x <= 0)
- {
- return -1;
- }
- else
- {
- return 1;
- }
- }
- private static int[] LiczU(int[] vs, int[] wyn)
- {
- wyn[0] = Bipolarna(weight[0,0] * vs[0] + weight[0,1] * vs[1] + weight[0,2] * vs[2]);
- wyn[1] = Bipolarna(weight[1, 0] * vs[0] + weight[1, 1] * vs[1] + weight[1, 2] * vs[2]);
- wyn[2] = Bipolarna(weight[2,0] * vs[0] + weight[2,1] * vs[1] + weight[2,2] * vs[2]);
- vectors[step] = new int[3] { wyn[0], wyn[1], wyn[2] };
- return wyn;
- }
- private static int[] LiczUAsyncFirst(int[] input, int[] output)
- {
- output[0] = Bipolarna(weight[0, 0] * input[0] + weight[0, 1] * input[1] + weight[0, 2] * input[2]);
- return output;
- }
- private static int[] LiczUAsyncSecond(int[] input, int[] output)
- {
- output[1] = Bipolarna(weight[1, 0] * input[0] + weight[1, 1] * input[1] + weight[1, 2] * input[2]);
- return output;
- }
- private static int[] LiczUAsyncThird(int[] input, int[] output)
- {
- output[2] = Bipolarna(weight[2, 0] * input[0] + weight[2, 1] * input[1] + weight[2, 2] * input[2]);
- return output;
- }
- private static int countEnergy(int[] t1, int[] t2)
- {
- int e = 0;
- 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]);
- return e;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement