mio_de_pso.MainForm.java Source code

Java tutorial

Introduction

Here is the source code for mio_de_pso.MainForm.java

Source

/*
 * 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 mio_de_pso;

import algorithms.DE.DEAlgorithm;
import algorithms.PSO.Swarm;
import algorithms.common.Dimension;
import algorithms.common.IEvolutionaryAlgorithm;
import algorithms.common.Individual;
import algorithms.common.Operation;
import functionParsing.RPNEvaluator;
import functionParsing.ShuntingYard;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.util.HashMap;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.LookupPaintScale;
import org.jfree.chart.renderer.xy.XYBlockRenderer;
import org.jfree.data.xy.DefaultXYZDataset;

/**
 *
 * @author User
 */
public class MainForm extends javax.swing.JFrame {

    public double x1Min;
    public double x1Max;
    public double x2Min;
    public double x2Max;

    public String functionRPN;

    public IEvolutionaryAlgorithm DE;
    public IEvolutionaryAlgorithm PSO;

    public NumberAxis domainAxis;
    public NumberAxis rangeAxis;

    public XYPlot deMainPlot;
    public JFreeChart deChart;
    public ChartPanel deChartPanel;

    public XYPlot psoMainPlot;
    public JFreeChart psoChart;
    public ChartPanel psoChartPanel;

    public double resolution;
    public DefaultXYZDataset xyzSet;

    public XYBlockRenderer Renderer;

    /**
     * Creates new form MainForm
     */
    public MainForm() {
        initComponents();
        RPNEvaluator.initDecimalformat();

        domainAxis = new NumberAxis("x1");
        rangeAxis = new NumberAxis("x2");

        Renderer = new XYBlockRenderer();
        xyzSet = new DefaultXYZDataset();

        DEPanel.setLayout(new java.awt.BorderLayout());
        PSOPanel.setLayout(new java.awt.BorderLayout());

    }

    /**
     * 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() {

        jSplitPane2 = new javax.swing.JSplitPane();
        jTextField1 = new javax.swing.JTextField();
        jSplitPane1 = new javax.swing.JSplitPane();
        jSplitPane3 = new javax.swing.JSplitPane();
        PSOPanel = new javax.swing.JPanel();
        DEPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        funcField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        PopulationField = new javax.swing.JFormattedTextField();
        PSOC1Field = new javax.swing.JFormattedTextField();
        PSOC2Field = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        DEFField = new javax.swing.JFormattedTextField();
        DECRField = new javax.swing.JFormattedTextField();
        IterationSpinner = new javax.swing.JSpinner();
        MakeButton = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        ParseButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        x1MinField = new javax.swing.JFormattedTextField();
        x1MaxField = new javax.swing.JFormattedTextField();
        x2MinField = new javax.swing.JFormattedTextField();
        x2MaxField = new javax.swing.JFormattedTextField();
        jLabel13 = new javax.swing.JLabel();
        ResolutionField = new javax.swing.JFormattedTextField();
        jLabel14 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        BestValuesTextArea = new javax.swing.JTextArea();

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jSplitPane3.setResizeWeight(0.5);

        javax.swing.GroupLayout PSOPanelLayout = new javax.swing.GroupLayout(PSOPanel);
        PSOPanel.setLayout(PSOPanelLayout);
        PSOPanelLayout.setHorizontalGroup(PSOPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        PSOPanelLayout.setVerticalGroup(PSOPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 385, Short.MAX_VALUE));

        jSplitPane3.setRightComponent(PSOPanel);

        javax.swing.GroupLayout DEPanelLayout = new javax.swing.GroupLayout(DEPanel);
        DEPanel.setLayout(DEPanelLayout);
        DEPanelLayout.setHorizontalGroup(DEPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 945, Short.MAX_VALUE));
        DEPanelLayout.setVerticalGroup(DEPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 385, Short.MAX_VALUE));

        jSplitPane3.setLeftComponent(DEPanel);

        jSplitPane1.setBottomComponent(jSplitPane3);

        funcField.setText(
                "sin ( x1 ) * e ^ ( 1 - cos ( x2 ) ) ^ 2 + cos ( x2 ) * e ^ ( 1 - sin ( x1 ) ) ^ 2 + ( x1 - x2 ) ^ 2");

        jLabel1.setText("f(x1, x2) =");

        jLabel2.setText("Populacja:");

        jLabel3.setText("Differential evolution:");

        jLabel4.setText("Particle Swarm Optimization");

        PopulationField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("###0"))));
        PopulationField.setText("50");

        PSOC1Field.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        PSOC1Field.setText("0,05");

        PSOC2Field.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        PSOC2Field.setText("0,05");

        jLabel5.setText("C1:");

        jLabel6.setText("C2:");

        jLabel7.setText("F:");

        jLabel8.setText("CR");

        DEFField.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        DEFField.setText("1");

        DECRField.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        DECRField.setText("0,5");

        IterationSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10000, 1));
        IterationSpinner.setValue(1);

        MakeButton.setText("Wykonaj");
        MakeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MakeButtonActionPerformed(evt);
            }
        });

        jLabel9.setText("iteracji");

        ParseButton.setText("Parse");
        ParseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ParseButtonActionPerformed(evt);
            }
        });

        jLabel10.setText("Search bounds:");

        jLabel11.setText("x1");

        jLabel12.setText("x2");

        x1MinField.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        x1MinField.setText("-6");
        x1MinField.setNextFocusableComponent(x1MaxField);
        x1MinField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                x1MinFieldFocusGained(evt);
            }
        });

        x1MaxField.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        x1MaxField.setText("6");
        x1MaxField.setNextFocusableComponent(x2MinField);
        x1MaxField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                x1MaxFieldFocusGained(evt);
            }
        });

        x2MinField.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        x2MinField.setText("-6");
        x2MinField.setNextFocusableComponent(x2MaxField);
        x2MinField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                x2MinFieldFocusGained(evt);
            }
        });

        x2MaxField.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        x2MaxField.setText("6");
        x2MaxField.setNextFocusableComponent(ResolutionField);
        x2MaxField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                x2MaxFieldFocusGained(evt);
            }
        });

        jLabel13.setText("Resolution:");

        ResolutionField.setFormatterFactory(
                new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        ResolutionField.setText("0,1");

        jLabel14.setText("Najlepsze wartoci:");

        BestValuesTextArea.setEditable(false);
        BestValuesTextArea.setColumns(20);
        BestValuesTextArea.setRows(5);
        jScrollPane1.setViewportView(BestValuesTextArea);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel1).addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(PopulationField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 74,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(62, 62, 62)
                                                .addGroup(jPanel1Layout.createParallelGroup(
                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addGroup(jPanel1Layout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jLabel7,
                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                        .addComponent(jLabel8,
                                                                                javax.swing.GroupLayout.Alignment.TRAILING))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(jPanel1Layout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                        false).addComponent(DECRField)
                                                                        .addComponent(DEFField,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                100,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                        .addComponent(jLabel3))
                                                .addGap(18, 18, 18)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel4)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addGroup(jPanel1Layout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jLabel5)
                                                                        .addComponent(jLabel6))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(jPanel1Layout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                        false).addComponent(PSOC1Field)
                                                                        .addComponent(PSOC2Field,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                100,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))))
                                                .addGap(18, 18, 18)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addComponent(jLabel12)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(x2MinField,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(x2MaxField,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                                .addGroup(jPanel1Layout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addGroup(jPanel1Layout
                                                                                .createSequentialGroup()
                                                                                .addComponent(jLabel11)
                                                                                .addPreferredGap(
                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                .addComponent(x1MinField,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        75,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addPreferredGap(
                                                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                                .addComponent(x1MaxField,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        75,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                        .addComponent(jLabel10))
                                                                .addGap(18, 18, 18)
                                                                .addGroup(jPanel1Layout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                        false).addComponent(jLabel13)
                                                                        .addComponent(ResolutionField,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                75,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(funcField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        678, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(ParseButton)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25,
                                        Short.MAX_VALUE)
                                .addGroup(
                                        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel14).addComponent(jScrollPane1,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createSequentialGroup().addComponent(MakeButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(IterationSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 50,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel9)))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(funcField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ParseButton).addComponent(jLabel14))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(12, 12, 12)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                        .addComponent(jLabel4)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(jPanel1Layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(PSOC1Field,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(jLabel5))
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(jPanel1Layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(PSOC2Field,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(jLabel6)))
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                        .addGroup(jPanel1Layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                                        .addComponent(jLabel10)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addGroup(jPanel1Layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(jLabel11)
                                                                                .addComponent(x1MinField,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addComponent(x1MaxField,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                                        .addComponent(jLabel13)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(ResolutionField,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(jPanel1Layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(jLabel12)
                                                                .addComponent(x2MinField,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(x2MaxField,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout
                                        .createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel2).addComponent(PopulationField,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel7).addComponent(DEFField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel8)
                                                .addComponent(DECRField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(MakeButton)
                                                .addComponent(IterationSpinner,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel9))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        jPanel1Layout.createSequentialGroup().addGap(46, 46, 46).addComponent(jScrollPane1)));

        jSplitPane1.setLeftComponent(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jSplitPane1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ParseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ParseButtonActionPerformed
        int populationSize;
        double F;
        double CR;
        double C1;
        double C2;

        this.BestValuesTextArea.setText("");

        //Odczyt danych
        try {
            x1Min = Double.parseDouble(this.x1MinField.getText().replace(',', '.'));
            x1Max = Double.parseDouble(this.x1MaxField.getText().replace(',', '.'));

            x2Min = Double.parseDouble(this.x2MinField.getText().replace(',', '.'));
            x2Max = Double.parseDouble(this.x2MaxField.getText().replace(',', '.'));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Bdnie podane zakresy przeszukiwania");
            return;
        }
        try {
            this.resolution = Double.parseDouble(this.ResolutionField.getText().replace(',', '.'));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Bdna rozdzielczo");
            return;
        }
        try {
            populationSize = Integer.parseInt(this.PopulationField.getText().replace(',', '.'));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Bdny rozmiar populacji");
            return;
        }

        try {
            F = Double.parseDouble(this.DEFField.getText().replace(',', '.'));
            CR = Double.parseDouble(this.DECRField.getText().replace(',', '.'));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Bdne paramtery ewolucji rnicowej");
            return;
        }
        try {
            C1 = Double.parseDouble(this.PSOC1Field.getText().replace(',', '.'));
            C2 = Double.parseDouble(this.PSOC2Field.getText().replace(',', '.'));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Bdne parametry optymalizacji stadnej czsteczek");
            return;
        }

        double[][] data;
        try {
            rangeAxis.setRange(x1Min, x1Max);
            domainAxis.setRange(x2Min, x2Max);

            Renderer.setBlockHeight(resolution);
            Renderer.setBlockWidth(resolution);

            this.functionRPN = ShuntingYard.infixToPostfix(this.funcField.getText());

            data = this.getFunctionValues(x1Min, x1Max, x2Min, x2Max, resolution);
        } catch (IllegalArgumentException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            return;
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,
                    "Bd podczas tworzenia parsowania funkcji!\nWszystkie elementy rwnania musz by oddzielone spacj.");
            return;
        }

        xyzSet.removeSeries("function");
        xyzSet.addSeries("function", data);

        this.createPlots();
        Renderer.setPaintScale(this.createLookupPaintScale(data[2]));

        try {
            HashMap<String, Dimension> dimensions = new HashMap<String, Dimension>();
            dimensions.put("x1", new Dimension(x1Min, x1Max));
            dimensions.put("x2", new Dimension(x2Min, x2Max));

            this.DE = new DEAlgorithm(this.functionRPN, populationSize, dimensions, Operation.Minimize, F, CR);
            this.viewIndividuals(this.DE.getPopulation(), deMainPlot);

            this.PSO = new Swarm(this.functionRPN, populationSize, dimensions, Operation.Minimize, C1, C2);
            this.viewIndividuals(this.PSO.getPopulation(), psoMainPlot);

            this.validate();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Bd podczas inicjalizacji populacji");
            return;
        }
    }//GEN-LAST:event_ParseButtonActionPerformed

    private void MakeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MakeButtonActionPerformed
        int liczbaIteracji = Integer.valueOf(this.IterationSpinner.getValue().toString());
        for (int i = 0; i < liczbaIteracji; ++i) {
            this.DE.makeIteration();
            this.PSO.makeIteration();
        }

        this.createPlots();
        this.viewIndividuals(this.DE.getPopulation(), deMainPlot);
        this.viewIndividuals(this.PSO.getPopulation(), psoMainPlot);
        this.viewBestValues();
        this.validate();
    }//GEN-LAST:event_MakeButtonActionPerformed

    private void x1MinFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_x1MinFieldFocusGained
        this.x1MinField.selectAll();
    }//GEN-LAST:event_x1MinFieldFocusGained

    private void x1MaxFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_x1MaxFieldFocusGained
        this.x1MaxField.selectAll();
    }//GEN-LAST:event_x1MaxFieldFocusGained

    private void x2MinFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_x2MinFieldFocusGained
        this.x2MinField.selectAll();
    }//GEN-LAST:event_x2MinFieldFocusGained

    private void x2MaxFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_x2MaxFieldFocusGained
        this.x2MaxField.selectAll();
    }//GEN-LAST:event_x2MaxFieldFocusGained

    private void viewBestValues() {

        HashMap<String, Double> deBestPosition = this.DE.getBestPosition();
        HashMap<String, Double> psoBestPosition = this.PSO.getBestPosition();

        this.BestValuesTextArea.setText(String.format("DE: %f\n\tx1: %f\n\tx2: %f\nPSO: %f\n\tx1: %f\n\tx2: %f",
                this.DE.getBestValue(), deBestPosition.get("x1"), deBestPosition.get("x2"), this.PSO.getBestValue(),
                psoBestPosition.get("x1"), psoBestPosition.get("x2")));
    }

    private void createPlots() {
        deMainPlot = new XYPlot(null, domainAxis, rangeAxis, Renderer);
        deMainPlot.setDataset(xyzSet);
        deChart = new JFreeChart("Differential Evolution", null, deMainPlot, false);
        deChartPanel = new ChartPanel(deChart);
        DEPanel.removeAll();
        DEPanel.add(deChartPanel, java.awt.BorderLayout.CENTER);

        psoMainPlot = new XYPlot(null, domainAxis, rangeAxis, Renderer);
        psoMainPlot.setDataset(xyzSet);
        psoChart = new JFreeChart("Particle Swarm optimization", null, psoMainPlot, false);
        psoChartPanel = new ChartPanel(psoChart);
        PSOPanel.removeAll();
        PSOPanel.add(psoChartPanel, java.awt.BorderLayout.CENTER);

        this.validate();
    }

    private void viewIndividuals(Individual[] population, XYPlot plot) {

        double annotationSize = (((x1Max - x1Min) * 0.02) + ((x2Max - x2Min) * 0.02)) / 2;

        for (int i = 0; i < population.length; ++i) {
            XYShapeAnnotation annotation = new XYShapeAnnotation(
                    new Ellipse2D.Double(population[i].getPosition("x1"), population[i].getPosition("x2"),
                            annotationSize, annotationSize),
                    new BasicStroke(1.0f), Color.LIGHT_GRAY, Color.LIGHT_GRAY);

            plot.addAnnotation(annotation);
        }
    }

    private LookupPaintScale createLookupPaintScale(double[] values) {
        double minVal = values[0];
        double maxVal = values[0];
        for (int i = 0; i < values.length; ++i) {
            if (values[i] < minVal)
                minVal = values[i];
            if (values[i] > maxVal)
                maxVal = values[i];
        }

        LookupPaintScale ps = new LookupPaintScale(minVal, maxVal + 0.0000001, Color.WHITE);

        Color startColor = Color.BLACK;
        Color secondColor = Color.BLUE;
        Color thirdColor = Color.GREEN;
        Color fourthColor = Color.YELLOW;
        Color endColor = Color.RED;

        int iterations = 80;
        int iterationPerColor = iterations / 4;
        double res = Math.round(((maxVal - minVal) / iterations) * 100.0) / 100.0;
        minVal -= res;

        for (int i = 0; i < iterationPerColor; ++i) {
            ps.add(minVal += res, new Color(
                    startColor.getRed() + (((secondColor.getRed() - startColor.getRed()) * i) / iterationPerColor),
                    startColor.getGreen()
                            + (((secondColor.getGreen()) - startColor.getGreen()) * i) / iterationPerColor,
                    startColor.getBlue()
                            + (((secondColor.getBlue() - startColor.getBlue()) * i) / iterationPerColor)));
        }
        for (int i = 0; i < iterationPerColor; ++i) {
            ps.add(minVal += res, new Color(
                    secondColor.getRed() + (((thirdColor.getRed() - secondColor.getRed()) * i) / iterationPerColor),
                    secondColor.getGreen()
                            + (((thirdColor.getGreen()) - secondColor.getGreen()) * i) / iterationPerColor,
                    secondColor.getBlue()
                            + (((thirdColor.getBlue() - secondColor.getBlue()) * i) / iterationPerColor)));
        }
        for (int i = 0; i < iterationPerColor; ++i) {
            ps.add(minVal += res, new Color(
                    thirdColor.getRed() + (((fourthColor.getRed() - thirdColor.getRed()) * i) / iterationPerColor),
                    thirdColor.getGreen()
                            + (((fourthColor.getGreen()) - thirdColor.getGreen()) * i) / iterationPerColor,
                    thirdColor.getBlue()
                            + (((fourthColor.getBlue() - thirdColor.getBlue()) * i) / iterationPerColor)));
        }
        for (int i = 0; i < iterationPerColor; ++i) {
            ps.add(minVal += res, new Color(
                    fourthColor.getRed() + (((endColor.getRed() - fourthColor.getRed()) * i) / iterationPerColor),
                    fourthColor.getGreen()
                            + (((endColor.getGreen()) - fourthColor.getGreen()) * i) / iterationPerColor,
                    fourthColor.getBlue()
                            + (((endColor.getBlue() - fourthColor.getBlue()) * i) / iterationPerColor)));
        }
        return ps;
    }

    public double[][] getFunctionValues(double x1Min, double x1Max, double x2Min, double x2Max, double resolution)
            throws Exception {
        int x1Len = (int) ((x1Max - x1Min) / resolution);
        int x2Len = (int) ((x2Max - x2Min) / resolution);
        int len = x1Len * x2Len;

        double[][] data = new double[3][len];

        int val = 0;
        for (int i = 0; i < x1Len; ++i) {
            double x1 = x1Min + resolution * i;

            for (int j = 0; j < x2Len; ++j) {
                data[0][val] = x1;
                data[1][val] = x2Min + resolution * j;
                data[2][val] = RPNEvaluator
                        .evalRPN(this.functionRPN.replaceAll("x1", RPNEvaluator.df.format(data[0][val]))
                                .replaceAll("x2", RPNEvaluator.df.format(data[1][val])));
                ++val;
            }
        }
        return data;
    }

    /**
     * @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(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainForm.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 MainForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea BestValuesTextArea;
    private javax.swing.JFormattedTextField DECRField;
    private javax.swing.JFormattedTextField DEFField;
    public javax.swing.JPanel DEPanel;
    private javax.swing.JSpinner IterationSpinner;
    private javax.swing.JButton MakeButton;
    private javax.swing.JFormattedTextField PSOC1Field;
    private javax.swing.JFormattedTextField PSOC2Field;
    public javax.swing.JPanel PSOPanel;
    private javax.swing.JButton ParseButton;
    public javax.swing.JFormattedTextField PopulationField;
    private javax.swing.JFormattedTextField ResolutionField;
    public javax.swing.JTextField funcField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    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.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JFormattedTextField x1MaxField;
    private javax.swing.JFormattedTextField x1MinField;
    private javax.swing.JFormattedTextField x2MaxField;
    private javax.swing.JFormattedTextField x2MinField;
    // End of variables declaration//GEN-END:variables
}