Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pr.ui; import java.awt.Dimension; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JFrame; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.StandardTickUnitSource; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import pr.algorithms.Adaline; import pr.algorithms.Algorithm; import pr.algorithms.PerceptronV2; import pr.util.Utils; /** * * @author Joel de Santos <joeldesantos@gmail.com> */ public class InputForm extends javax.swing.JFrame { /** * Creates new form InputForm */ public InputForm() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel3 = new javax.swing.JLabel(); jMenuItem8 = new javax.swing.JMenuItem(); trainDataTF = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); textDataBtn = new javax.swing.JButton(); trainBtn = new javax.swing.JButton(); jSeparator2 = new javax.swing.JSeparator(); jLabel2 = new javax.swing.JLabel(); learningRateTF = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); epochLimitTF = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); thetaTF = new javax.swing.JTextField(); jSeparator3 = new javax.swing.JSeparator(); jLabel7 = new javax.swing.JLabel(); testDataTF = new javax.swing.JTextField(); testDataBtn = new javax.swing.JButton(); testBtn = new javax.swing.JButton(); manualEntryBtn = new javax.swing.JButton(); showWeightsBtn = new javax.swing.JButton(); jSeparator6 = new javax.swing.JSeparator(); showPlaneBtn = new javax.swing.JButton(); jLabel5 = new javax.swing.JLabel(); showErrorsBtn = new javax.swing.JButton(); showWeigthsLogBtn = new javax.swing.JButton(); showPlaneLogBtn = new javax.swing.JButton(); jLabel8 = new javax.swing.JLabel(); resultTF = new javax.swing.JTextField(); algorithmCB = new javax.swing.JComboBox<>(); showTestDataBtn = new javax.swing.JButton(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); minimuDistanceMI = new javax.swing.JMenuItem(); perceptronMI = new javax.swing.JMenuItem(); adelineMI = new javax.swing.JMenuItem(); jSeparator1 = new javax.swing.JPopupMenu.Separator(); showWeightsMI = new javax.swing.JMenuItem(); showPlaneMI = new javax.swing.JMenuItem(); showErrorsMI = new javax.swing.JMenuItem(); showWeigthsLogMI = new javax.swing.JMenuItem(); showPlaneLogMI = new javax.swing.JMenuItem(); jSeparator5 = new javax.swing.JPopupMenu.Separator(); jMenuItem7 = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); aboutMI = new javax.swing.JMenuItem(); jLabel3.setText("jLabel3"); jMenuItem8.setText("jMenuItem8"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Reconocimiento de Patrones"); trainDataTF.setEditable(false); jLabel1.setText("Datos de entrenamiento:"); textDataBtn.setText("Browse"); textDataBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { textDataBtnActionPerformed(evt); } }); trainBtn.setText("Train"); trainBtn.setEnabled(false); trainBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { trainBtnActionPerformed(evt); } }); jLabel2.setText("Learning Rate:"); learningRateTF.setText("0.1"); jLabel4.setText("Limite de epocas:"); epochLimitTF.setText("100"); jLabel6.setText("Theta:"); thetaTF.setText("1"); jLabel7.setText("Datos de prueba:"); testDataTF.setEditable(false); testDataBtn.setText("Browse"); testDataBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { testDataBtnActionPerformed(evt); } }); testBtn.setText("Prueba"); testBtn.setEnabled(false); testBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { testBtnActionPerformed(evt); } }); manualEntryBtn.setText("Entrada Manual"); manualEntryBtn.setEnabled(false); showWeightsBtn.setText("Mostrar pesos"); showWeightsBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showWeightsBtnActionPerformed(evt); } }); showPlaneBtn.setText("Plano"); showPlaneBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showPlaneBtnActionPerformed(evt); } }); jLabel5.setText("Graficas y logs:"); showErrorsBtn.setText("Errors"); showErrorsBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showErrorsBtnActionPerformed(evt); } }); showWeigthsLogBtn.setText("Log de Pesos"); showWeigthsLogBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showWeigthsLogBtnActionPerformed(evt); } }); showPlaneLogBtn.setText("Log de Planos"); showPlaneLogBtn.setEnabled(false); jLabel8.setText("Resultado:"); resultTF.setEditable(false); algorithmCB.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "Selecciona un algoritmo", "Minima Distancia", "Perceptron", "Adaline" })); algorithmCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { algorithmCBActionPerformed(evt); } }); showTestDataBtn.setText("Mostrar"); showTestDataBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { showTestDataBtnActionPerformed(evt); } }); jMenu1.setText("Algoritmo"); minimuDistanceMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.ALT_MASK)); minimuDistanceMI.setText("Minima Distancia"); minimuDistanceMI.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { minimuDistanceMIActionPerformed(evt); } }); jMenu1.add(minimuDistanceMI); perceptronMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.ALT_MASK)); perceptronMI.setText("Perceptron"); perceptronMI.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { perceptronMIActionPerformed(evt); } }); jMenu1.add(perceptronMI); adelineMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK)); adelineMI.setText("Adaline"); adelineMI.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { adelineMIActionPerformed(evt); } }); jMenu1.add(adelineMI); jMenu1.add(jSeparator1); showWeightsMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_1, java.awt.event.InputEvent.ALT_MASK)); showWeightsMI.setText("Mostrar Pesos"); jMenu1.add(showWeightsMI); showPlaneMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_2, java.awt.event.InputEvent.ALT_MASK)); showPlaneMI.setText("Plano"); jMenu1.add(showPlaneMI); showErrorsMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_3, java.awt.event.InputEvent.ALT_MASK)); showErrorsMI.setText("Errors"); jMenu1.add(showErrorsMI); showWeigthsLogMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_4, java.awt.event.InputEvent.ALT_MASK)); showWeigthsLogMI.setText("Log de pesos"); jMenu1.add(showWeigthsLogMI); showPlaneLogMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_5, java.awt.event.InputEvent.ALT_MASK)); showPlaneLogMI.setText("Log de Planos"); jMenu1.add(showPlaneLogMI); jMenu1.add(jSeparator5); jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem7.setText("Clear"); jMenu1.add(jMenuItem7); jMenuBar1.add(jMenu1); jMenu2.setText("Ayuda"); aboutMI.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0)); aboutMI.setText("Acerca de"); aboutMI.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { aboutMIActionPerformed(evt); } }); jMenu2.add(aboutMI); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(showPlaneBtn) .addGap(18, 18, 18).addComponent(showErrorsBtn).addGap(10, 10, 10) .addComponent(showWeigthsLogBtn) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(showPlaneLogBtn)) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel7) .addComponent(jSeparator6, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(testBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(testDataTF)) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(50, 50, 50) .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel8) .addGap(149, 149, 149)) .addComponent(resultTF, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(testDataBtn) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(manualEntryBtn) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(showTestDataBtn)))) .addGroup(layout.createSequentialGroup() .addComponent(trainDataTF, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(textDataBtn) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(trainBtn)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(learningRateTF)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6).addComponent(thetaTF, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(10, 10, 10) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(epochLimitTF)) .addGap(8, 8, 8).addComponent(showWeightsBtn))) .addComponent(jLabel5)).addGap(8, 8, 8)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(algorithmCB, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addComponent(jLabel1).addGap(0, 0, Short.MAX_VALUE))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(algorithmCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel1) .addGap(1, 1, 1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(trainDataTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(textDataBtn).addComponent(trainBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jLabel4).addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(learningRateTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(epochLimitTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(thetaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(showWeightsBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(testDataTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(testDataBtn).addComponent(manualEntryBtn) .addComponent(showTestDataBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel8) .addGap(8, 8, 8) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(resultTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(testBtn)) .addGap(14, 14, 14) .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(showPlaneBtn).addComponent(showErrorsBtn) .addComponent(showWeigthsLogBtn).addComponent(showPlaneLogBtn)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void minimuDistanceMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minimuDistanceMIActionPerformed reset("Minima Distancia"); // Minima Distancia }//GEN-LAST:event_minimuDistanceMIActionPerformed private void aboutMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMIActionPerformed JFrame about = new About(); about.pack(); about.setVisible(Boolean.TRUE); }//GEN-LAST:event_aboutMIActionPerformed private void testDataBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testDataBtnActionPerformed jfc = new JFileChooser(lastUsedDirectory); int returnVal = jfc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { testBtn.setEnabled(true); File file = jfc.getSelectedFile(); testDataTF.setText("Loaded"); lastUsedDirectory = file.getParent(); try { // Read the data List<String> lines = Files.readAllLines(Paths.get(file.getAbsolutePath())); testData = Utils.getTestDataFromRawDataList(lines); } catch (IOException ex) { Logger.getLogger(InputForm.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_testDataBtnActionPerformed private void showPlaneBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showPlaneBtnActionPerformed try { XYSeries xyseries = new XYSeries("Plane Graph"); xyseries.add(algorithm.getBias(), (algorithm.getBias() - 1) / 2); xyseries.add(algorithm.getBias() + 25, (algorithm.getBias() + 25 - 1) / 2); XYSeriesCollection xyseriescollection = new XYSeriesCollection(xyseries); JFreeChart jfreechart = ChartFactory.createXYLineChart("Plane Graph", "X", "Y", xyseriescollection, PlotOrientation.VERTICAL, true, true, false); XYPlot xyplot = (XYPlot) jfreechart.getPlot(); // NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis(); // numberaxis.setStandardTickUnits(new StandardTickUnitSource()); // NumberAxis numberaxis1 = (NumberAxis) xyplot.getRangeAxis(); // numberaxis1.setStandardTickUnits(new StandardTickUnitSource()); // numberaxis1.setAutoRangeMinimumSize(4.9406564584124654E-324D);r ChartPanel chartpanel = new ChartPanel(jfreechart); chartpanel.setPreferredSize(new Dimension(500, 270)); Utils.showDialoj("Plane Graph", chartpanel, this); } catch (UnsupportedOperationException uoe) { } }//GEN-LAST:event_showPlaneBtnActionPerformed private void perceptronMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_perceptronMIActionPerformed reset("Perceptron"); getPerceptron(); }//GEN-LAST:event_perceptronMIActionPerformed private void adelineMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adelineMIActionPerformed reset("Adaline"); // Adeline }//GEN-LAST:event_adelineMIActionPerformed private void textDataBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textDataBtnActionPerformed jfc = new JFileChooser(lastUsedDirectory); int returnVal = jfc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { trainBtn.setEnabled(true); File file = jfc.getSelectedFile(); trainDataTF.setText(file.getName()); lastUsedDirectory = file.getParent(); try { // Read the data List<String> lines = Files.readAllLines(Paths.get(file.getAbsolutePath())); // Obtain the classes and data from the raw data classes = new ArrayList(); data = new ArrayList(); Utils.getTrainingListsFromRawDataList(lines, data, classes); } catch (IOException ex) { Logger.getLogger(InputForm.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_textDataBtnActionPerformed private void trainBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trainBtnActionPerformed // Train the algorithm if (algorithm != null) { trainDataTF.setText(""); algorithm.train(classes, data); } else { Utils.showDialoj("Error", "No se ha seleccionado un algoritmo.", this); } }//GEN-LAST:event_trainBtnActionPerformed private void algorithmCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_algorithmCBActionPerformed String value = algorithmCB.getSelectedItem().toString(); if ("Minima Distancia".equals(value)) { reset(value); algorithm = new PerceptronV2(); } else if ("Perceptron".equals(value)) { reset(value); algorithm = getPerceptron(); } else if ("Adaline".equals(value)) { reset(value); algorithm = getAdaline(); } }//GEN-LAST:event_algorithmCBActionPerformed private void testBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testBtnActionPerformed // Test the algorithm if (algorithm != null && algorithm.isTrained()) { trainDataTF.setText(""); int classType = algorithm.test(testData); resultTF.setText("Clase " + (classType == 0 ? 1 : 2)); } else { Utils.showDialoj("Error", "No se ha seleccionado/entrenado el algoritmo.", this); } }//GEN-LAST:event_testBtnActionPerformed private void showWeightsBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showWeightsBtnActionPerformed if (algorithm != null && algorithm.isTrained()) { Utils.showDialoj("Pesos", algorithm.getWeights(), this); } else { Utils.showDialoj("Error", "No se ha seleccionado/entrenado el algoritmo.", this); } }//GEN-LAST:event_showWeightsBtnActionPerformed private void showWeigthsLogBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showWeigthsLogBtnActionPerformed if (algorithm != null && algorithm.isTrained()) { Utils.showDialoj("Pesos", algorithm.getWeightsLog(), this); } else { Utils.showDialoj("Error", "No se ha seleccionado/entrenado el algoritmo.", this); } }//GEN-LAST:event_showWeigthsLogBtnActionPerformed private void showTestDataBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showTestDataBtnActionPerformed if (algorithm != null && algorithm.isTrained()) { String displayData = ""; for (int h = 0, i = 20; i < testData.length(); h = i + 1, i += 20) { displayData += testData.substring(h, i); displayData += "\n"; } displayData = displayData.substring(0, displayData.length() - 1); Utils.showDialoj("Test data", displayData, this); } else { Utils.showDialoj("Error", "No se ha seleccionado/entrenado el algoritmo.", this); } }//GEN-LAST:event_showTestDataBtnActionPerformed private void showErrorsBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showErrorsBtnActionPerformed DefaultCategoryDataset categorydataset = new DefaultCategoryDataset(); for (int i = 0; i < algorithm.getErrorLog().size(); i++) { categorydataset.addValue(algorithm.getErrorLog().get(i), "Global error", "" + i); } JFreeChart jfreechart = ChartFactory.createLineChart("Error Graph", null, "Error Graph", categorydataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); categoryplot.setRangePannable(true); categoryplot.setRangeGridlinesVisible(false); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); ChartUtilities.applyCurrentTheme(jfreechart); ChartPanel chartpanel = new ChartPanel(jfreechart); chartpanel.setMouseWheelEnabled(true); Utils.showDialoj("Error Graph", chartpanel, this); }//GEN-LAST:event_showErrorsBtnActionPerformed private void reset(String selectedAlgortihm) { trainBtn.setEnabled(false); testBtn.setEnabled(false); trainDataTF.setText(""); testDataTF.setText(""); resultTF.setText(""); if (Utils.isNullOrEmpty(selectedAlgortihm)) { algorithmCB.setSelectedItem("Selecciona un algoritmo"); } else { algorithmCB.setSelectedItem(selectedAlgortihm); } } private Algorithm getPerceptron() throws NumberFormatException { if (Utils.isNullOrEmpty(learningRateTF.getText())) { Utils.showDialoj("Error", "Por favor especifique un learning rate", this); } if (Utils.isNullOrEmpty(thetaTF.getText())) { Utils.showDialoj("Error", "Por favor especifique un valor para theta", this); } if (Utils.isNullOrEmpty(epochLimitTF.getText())) { Utils.showDialoj("Error", "Por favor especifique un limite de epocas", this); } double learningRate = Double.parseDouble(learningRateTF.getText()); double theta = Double.parseDouble(thetaTF.getText()); int epochLimit = Integer.parseInt(epochLimitTF.getText()); PerceptronV2 algo = new PerceptronV2(); algo.initPerceptron(600, learningRate, theta, epochLimit); return algo; } private Algorithm getAdaline() throws NumberFormatException { if (Utils.isNullOrEmpty(learningRateTF.getText())) { Utils.showDialoj("Error", "Por favor especifique un learning rate", this); } if (Utils.isNullOrEmpty(thetaTF.getText())) { Utils.showDialoj("Error", "Por favor especifique un valor para theta", this); } if (Utils.isNullOrEmpty(epochLimitTF.getText())) { Utils.showDialoj("Error", "Por favor especifique un limite de epocas", this); } double learningRate = Double.parseDouble(learningRateTF.getText()); double theta = Double.parseDouble(thetaTF.getText()); int epochLimit = Integer.parseInt(epochLimitTF.getText()); Adaline algo = new Adaline(); algo.initAdeline(600, learningRate, theta, epochLimit); return algo; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(InputForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(InputForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(InputForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(InputForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new InputForm().setVisible(true); } }); } private Algorithm algorithm; private String testData; private List<Integer> classes; private List<String> data; private String lastUsedDirectory = "c:"; private JFileChooser jfc; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem aboutMI; private javax.swing.JMenuItem adelineMI; private javax.swing.JComboBox<String> algorithmCB; private javax.swing.JTextField epochLimitTF; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem7; private javax.swing.JMenuItem jMenuItem8; private javax.swing.JPopupMenu.Separator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JPopupMenu.Separator jSeparator5; private javax.swing.JSeparator jSeparator6; private javax.swing.JTextField learningRateTF; private javax.swing.JButton manualEntryBtn; private javax.swing.JMenuItem minimuDistanceMI; private javax.swing.JMenuItem perceptronMI; private javax.swing.JTextField resultTF; private javax.swing.JButton showErrorsBtn; private javax.swing.JMenuItem showErrorsMI; private javax.swing.JButton showPlaneBtn; private javax.swing.JButton showPlaneLogBtn; private javax.swing.JMenuItem showPlaneLogMI; private javax.swing.JMenuItem showPlaneMI; private javax.swing.JButton showTestDataBtn; private javax.swing.JButton showWeightsBtn; private javax.swing.JMenuItem showWeightsMI; private javax.swing.JButton showWeigthsLogBtn; private javax.swing.JMenuItem showWeigthsLogMI; private javax.swing.JButton testBtn; private javax.swing.JButton testDataBtn; private javax.swing.JTextField testDataTF; private javax.swing.JButton textDataBtn; private javax.swing.JTextField thetaTF; private javax.swing.JButton trainBtn; private javax.swing.JTextField trainDataTF; // End of variables declaration//GEN-END:variables }