Tengo un programa que está en un JFrame, cuando ejecuto la función Parender();
con el botón aprender
, el programa se detiene y no hace más nada.
package neurona2.org;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Funciona extends JFrame {
private JLabel lx1, lx2, ly1, cambio, x1l, x2l, r1;
private JTextField textx1, textx2, texty1, textx1a, textx2a, texty1a, textx1b, textx2b, texty1b, textx1c, textx2c,
texty1c, t1, t2;
public double x1, x1a, x1b, x1c, x2, x2a, x2b, x2c, y1, y1a, y1b, y1c;
private JButton button1, aceptar, aprender;
double w1 = new Random().nextDouble();
double w2 = new Random().nextDouble();
double umbral = new Random().nextDouble();
double yr = 0;
double error = 0;
double wx = 0;
int i = 0;
double tv[][] = {{ x1, x2, y1 }, { x1a, x2a, y1a }, { x1b, x2b, y1b }, { x1c, x2c, y1c } };
public Funciona() {
this.setLayout(null);
this.setSize(450, 450);
this.setTitle("Aprendizaje");
this.setDefaultCloseOperation(this.HIDE_ON_CLOSE);
this.setVisible(true);
this.setLocationRelativeTo(null);
// Instanciando Cuadros de Texto
lx1 = new JLabel("X1");
this.add(lx1);
lx1.setBounds(25, 15, 25, 20);
textx1 = new JTextField();
this.add(textx1);
textx1.setBounds(25, 30, 30, 30);
lx2 = new JLabel("X2");
this.add(lx2);
lx2.setBounds(75, 15, 25, 20);
textx2 = new JTextField();
this.add(textx2);
textx2.setBounds(75, 30, 30, 30);
ly1 = new JLabel("Y");
this.add(ly1);
ly1.setBounds(125, 10, 25, 30);
texty1 = new JTextField();
this.add(texty1);
texty1.setBounds(125, 30, 30, 30);
textx1a = new JTextField();
this.add(textx1a);
textx1a.setBounds(25, 90, 30, 30);
textx2a = new JTextField();
this.add(textx2a);
textx2a.setBounds(75, 90, 30, 30);
texty1a = new JTextField();
this.add(texty1a);
texty1a.setBounds(125, 90, 30, 30);
textx1b = new JTextField();
this.add(textx1b);
textx1b.setBounds(25, 145, 30, 30);
textx2b = new JTextField();
this.add(textx2b);
textx2b.setBounds(75, 145, 30, 30);
texty1b = new JTextField();
this.add(texty1b);
texty1b.setBounds(125, 145, 30, 30);
textx1c = new JTextField();
this.add(textx1c);
textx1c.setBounds(25, 195, 30, 30);
textx2c = new JTextField();
this.add(textx2c);
textx2c.setBounds(75, 195, 30, 30);
texty1c = new JTextField();
this.add(texty1c);
texty1c.setBounds(125, 195, 30, 30);
cambio = new JLabel();
this.add(cambio);
cambio.setBounds(200, 320, 200, 30);
t1 = new JTextField();
this.add(t1);
t1.setBounds(200, 60, 40, 35);
x1l = new JLabel("X1");
this.add(x1l);
x1l.setBounds(200, 30, 40, 35);
t2 = new JTextField();
this.add(t2);
t2.setBounds(250, 60, 40, 35);
x2l = new JLabel("X2");
this.add(x2l);
x2l.setBounds(250, 30, 40, 35);
r1 = new JLabel("Resultado");
this.add(r1);
r1.setBounds(350, 50, 150, 35);
// Fin cuadros de texto
// Botones
button1 = new JButton("Aceptar");
this.add(button1);
button1.setBounds(25, 300, 100, 20);
aceptar = new JButton("Aceptar");
this.add(aceptar);
aceptar.setBounds(205, 150, 100, 35);
aprender = new JButton("Entrenar");
this.add(aprender);
aprender.setBounds(200, 250, 100, 20);
//Fin botones
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
double x1 = Double.parseDouble(textx1.getText());
double x1a = Double.parseDouble(textx1a.getText());
double x1b = Double.parseDouble(textx1b.getText());
double x1c = Double.parseDouble(textx1c.getText());
double x2 = Double.parseDouble(textx2.getText());
double x2a = Double.parseDouble(textx2a.getText());
double x2b = Double.parseDouble(textx2b.getText());
double x2c = Double.parseDouble(textx2c.getText());
double y1 = Double.parseDouble(texty1.getText());
double y1a = Double.parseDouble(texty1a.getText());
double y1b = Double.parseDouble(texty1b.getText());
double y1c = Double.parseDouble(texty1c.getText());
cambio.setText("Valores aprendidos");
}
});
aprender.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
Aprender();
}
});
}
public void Aprender() {
while (i < tv.length) {
wx = (tv[i][0] * w1) + (tv[i][1] * w2) - umbral;
yr = (wx == umbral) ? 1 : -1;
if (yr == tv[i][2]) {
i++;
} else {
error = tv[i][2] - wx;
w1 = w1 + error * tv[i][0];
w2 = w2 + error * tv[i][1];
if (umbral < 0) {
umbral = (umbral + error) * -1;
} else {
umbral = umbral + error;
}
i = 0;
} // Final Else
} // Final While
}// Final funcion
}
// Final While
, apuesto a que se queda indefinidamente en el bucle debido a quei
nunca se incrementa la suficiente.