Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Random;
- /**
- * Created by micha on 06.04.2017.
- */
- public class Sieci_Hopfielda {
- private double[] xs;
- private double[] x;
- private double[][] c;
- private double[][] w;
- private double[] theta;
- public Sieci_Hopfielda(){
- initXS();
- initC();
- initW();
- initTheta();
- initX();
- }
- private void initXS(){
- this.xs = new double[]{
- 0,0,0,0,0,
- 0,1,1,0,0,
- 0,0,1,0,0,
- 0,0,1,0,0,
- 0,0,1,0,0};
- }
- private void initC(){
- this.c = new double[25][25];
- for(int i=0;i<25;i++){
- for(int j=0;j<25;j++){
- if(i==j){
- c[i][j]=0;
- }
- else{
- c[i][j]=(xs[i]-0.5)*(xs[j]-0.5);
- }
- }
- }
- }
- private void initW(){
- this.w = new double[25][25];
- for(int i=0;i<25;i++){
- for(int j=0;j<25;j++){
- w[i][j]=2*c[i][j];
- }
- }
- }
- //pytanie czy ma iterować tylko po j w C
- private void initTheta(){
- this.theta = new double[25];
- for(int i=0;i<25;i++){
- for(int j=0;j<25;j++){
- theta[i]=theta[i]+c[i][j];
- }
- }
- }
- private void initX(){
- Random generator = new Random();
- this.x = new double[25];
- for(int i=0;i<25;i++){
- x[i] = Math.floorMod(generator.nextInt(),2);
- }
- }
- private void nextX(){
- double[] u = getU();
- for(int i=0;i<25;i++){
- if(u[i]>0.0){
- x[i]=1.0;}
- if(u[i]<0.0){
- x[i]=0.0;}
- }
- }
- private double[] getU(){
- double[] u = new double[25];
- for(int i=0;i<25;i++){
- for(int j=0;j<25;j++){
- u[i]=u[i]+w[i][j]*x[j];
- }
- u[i]=u[i]-theta[i];
- }
- return u;
- }
- public void algorithm() throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- initX();
- display(x);
- while(!br.readLine().equals("exit")){
- nextX();
- display(this.x);
- }
- }
- private void display(double[] vector){
- for(int i=0;i<25;i++){
- if(i % 5 == 0){
- System.out.print("\n");}
- if(vector[i]==0.0){
- System.out.print("-");
- }
- else if(vector[i]==1.0){
- System.out.print("*");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement