flower pollination algorithm optimization using java and ouput chart

Certainly! Here's an updated version of the Flower Pollination Algorithm in Java that includes generating an output chart using the JFreeChart library:

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartUtils;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.data.xy.XYSeries;

import org.jfree.data.xy.XYSeriesCollection;


import java.io.File;

import java.io.IOException;

import java.util.Random;


public class FlowerPollinationAlgorithm {


    private int populationSize;

    private int maxIterations;

    private double flowerSize;

    private double pa;

    private double beta;

    private double minBound;

    private double maxBound;

    private Random random;

    private XYSeries fitnessSeries;


    public FlowerPollinationAlgorithm(int populationSize, int maxIterations, double flowerSize, double pa, double beta, double minBound, double maxBound) {

        this.populationSize = populationSize;

        this.maxIterations = maxIterations;

        this.flowerSize = flowerSize;

        this.pa = pa;

        this.beta = beta;

        this.minBound = minBound;

        this.maxBound = maxBound;

        this.random = new Random();

        this.fitnessSeries = new XYSeries("Best Fitness");

    }


    public double optimize() {

        // Initialize population

        double[] population = generateRandomSolution();

        double fitness = evaluateSolution(population);


        double bestFitness = fitness;

        double[] bestSolution = population;


        // Main optimization loop

        for (int iteration = 0; iteration < maxIterations; iteration++) {

            double[] newSolution = flowerPollination(population, bestSolution);

            double newFitness = evaluateSolution(newSolution);


            if (newFitness < fitness) {

                population = newSolution;

                fitness = newFitness;


                if (newFitness < bestFitness) {

                    bestFitness = newFitness;

                    bestSolution = newSolution;

                }

            }


            // Update flower size

            flowerSize *= pa;


            fitnessSeries.add(iteration, bestFitness);

        }


        return bestFitness;

    }


    private double[] generateRandomSolution() {

        double[] solution = new double[1];

        solution[0] = minBound + random.nextDouble() * (maxBound - minBound);

        return solution;

    }


    private double[] flowerPollination(double[] solution, double[] bestSolution) {

        double[] newSolution = new double[solution.length];


        double rand = random.nextDouble();

        if (rand > beta) {

            double distance = Math.abs(solution[0] - bestSolution[0]);

            double step = distance * random.nextGaussian();

            newSolution[0] = solution[0] + step;

        } else {

            double min = Math.max(minBound, solution[0] - flowerSize);

            double max = Math.min(maxBound, solution[0] + flowerSize);

            newSolution[0] = min + random.nextDouble() * (max - min);

        }


        return newSolution;

    }


    private double evaluateSolution(double[] solution) {

        // Implement your fitness function here

        // This function should return the fitness value of the given solution

        // The fitness value represents the quality of the solution

        // Lower values indicate better solutions


        // Example: Sphere function

        return solution[0] * solution[0];

    }


    public void createChart(String outputFileName) {

        XYSeriesCollection dataset = new XYSeriesCollection();

        dataset.addSeries(fitnessSeries);


        JFreeChart chart = ChartFactory.createXYLineChart(

                "Flower Pollination Algorithm", "Iteration", "Best Fitness",

                dataset, PlotOrientation.VERTICAL, true, true, false);


        try {

            ChartUtils.saveChartAsPNG(new File(outputFileName), chart, 800, 600);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


    public static void main(String[] args) {

        int populationSize = 1;

        int maxIterations = 100;

        double flowerSize = 0.1;

        double pa = 0.8;

        double beta = 0.5;

        double minBound = -5.0;

        double maxBound = 5.0;


        FlowerPollinationAlgorithm fpa = new FlowerPollinationAlgorithm(populationSize, maxIterations, flowerSize, pa, beta, minBound, maxBound);

        double bestFitness = fpa.optimize();


        System.out.println("Best Fitness: " + bestFitness);

        fpa.createChart("output_chart.png");

    }

}



In this updated version, I've added the ability to generate an output chart using the JFreeChart library. The createChart() method creates an XYLineChart representing the change in the best fitness value over iterations. The resulting chart is saved as a PNG image file.

You can customize the parameters and fitness function as needed. To use the JFreeChart library, you'll need to include the JFreeChart JAR file in your project's dependencies.

Make sure to replace the evaluateSolution() method with your own fitness function implementation.

When you run the program, it will print the best fitness value achieved by the Flower Pollination Algorithm and save the output chart as "output_chart.png" in the current directory.

Feel free to modify the code further and customize it according to your requirements.

Comments

Popular posts from this blog

materi Asking and Giving Attention (Meminta dan Memberi Perhatian) dalam bahasa Rusia

20 Fundamental Spring Boot yang wajib diketahui Pemula Programmer spesialis Java Developer

Daftar Kata Kunci (Keyword) dalam Bahasa Pemrograman Python

Tujuh daftar situs terpercaya untuk melamar kerja ke Austria bagi pelamar dari luar negeri:

percakapan KERETA JARAK JAUH dalam bahasa Rusia

Regression Testing dalam UAT Environment?

TOP 8 Framework Populer menggunakan bahasa .NET

Python Date and Time Manipulation

Program Python yang menyelesaikan sistem persamaan linear menggunakan metode eliminasi Gauss

CRUD SPRING REACTIVE WEBFLUX +Mongo DB