Advertisement
Guest User

Untitled

a guest
Nov 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package redeneuralkohonen;
  2.  
  3. public class RedeNeuralKohonen {
  4.  
  5.     private static Double input[][] = {{1.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 1.0}};
  6.     private static Double taxa;
  7.     private static Double peso[][] = {{0.2, 0.8}, {0.6, 0.4}, {0.5, 0.7}, {0.9, 0.3}};
  8.     private static Double raio;
  9.  
  10.     public static void main(String[] args) {
  11.         iniciaPeso(0);
  12.         System.out.println("iteracao 0");
  13.         mostraPeso();
  14.         System.out.println("\n");
  15.         for (int i = 1; i <= 100; i++) {
  16.             treinamento();
  17.             if (i == 10 || i == 20 || i == 50 || i == 80 || i == 100) {
  18.                 System.out.println("iteracao " + i);
  19.                 mostraPeso();
  20.                 System.out.println("\n");
  21.             }
  22.         }
  23.         System.out.println("a matriz de peso eh aproximadamente");
  24.         mostraPesoArred();
  25.     }
  26.  
  27.     private static void updTaxa() {
  28.         taxa = taxa * 0.5;
  29.         if (taxa < 0.1) {
  30.             taxa = 0.1;
  31.         }
  32.     }
  33.  
  34.     private static void iniciaPeso(int x) {
  35.         if (x == 1) {
  36.             for (int i = 0; i < peso.length; i++) {
  37.                 for (int j = 0; j < peso[0].length; j++) {
  38.                     peso[i][j] = Math.random();
  39.                 }
  40.             }
  41.         }
  42.         raio = 0.0;
  43.         taxa = 0.6;
  44.     }
  45.  
  46.     private static void treinamento() {
  47.         for (int i = 0; i < input.length; i++) {
  48.             Double d1 = funcaoD(i, 0);
  49.             Double d2 = funcaoD(i, 1);
  50.             int j = calcProx(d1, d2);
  51.             updVenc(i, j);
  52.         }
  53.         updTaxa();
  54.     }
  55.  
  56.     private static Double funcaoD(int vet, int x) {
  57.         Double soma = 0.0;
  58.         for (int i = 0; i < input.length; i++) {
  59.             soma += Math.pow((peso[i][x] - input[vet][i]), 2);
  60.         }
  61.         return soma;
  62.     }
  63.  
  64.     private static int calcProx(Double d1, Double d2) {
  65.         if (d1 < d2) {
  66.             return 0;
  67.         } else {
  68.             return 1;
  69.         }
  70.     }
  71.  
  72.     private static void updVenc(int vet, int j) {
  73.         for (int i = 0; i < peso.length; i++) {
  74.             peso[i][j] = ((1 - taxa) * peso[i][j]) + (taxa * (input[vet][i]));
  75.         }
  76.     }
  77.  
  78.     private static void mostraPeso() {
  79.         for (int i = 0; i < peso.length; i++) {
  80.             for (int j = 0; j < peso[i].length; j++) {
  81.                 System.out.print(peso[i][j]);
  82.                 if (j != peso[i].length - 1) {
  83.                     System.out.print(" | ");
  84.                 }
  85.             }
  86.             System.out.println("");
  87.         }
  88.     }
  89.  
  90.     private static void mostraPesoArred() {
  91.         for (int i = 0; i < peso.length; i++) {
  92.             for (int j = 0; j < peso[i].length; j++) {
  93.                 System.out.printf("%.2f", peso[i][j]);
  94.                 if (j != peso[i].length - 1) {
  95.                     System.out.print(" | ");
  96.                 }
  97.             }
  98.             System.out.println("");
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement