List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Perform Particle Swarm Optimization Scheduling Algorithm in Cloudsim?

Perform Particle Swarm Optimization Scheduling Algorithm in Cloudsim

Condition for Perform Particle Swarm Optimization Scheduling Algorithm in Cloudsim

  • Description:
    To perform a Particle Swarm Optimization (PSO) scheduling algorithm in CloudSim, the task scheduling problem is treated as an optimization problem, where each particle represents a potential task-to-VM mapping. The PSO algorithm uses a population of particles, each with a position (task schedule) and velocity (changes in the schedule). The fitness of each particle is evaluated based on an objective function like minimizing makespan, energy consumption, or cost. Particles update their positions using their own best-known position and the global best-known position of the swarm. The algorithm iterates through multiple generations to converge on the optimal solution, stopping when a predefined criterion is met.
Sample Code
  • import java.util.Random;
    public class ParticleSwarmOptimization {
    // Define problem parameters
    private static final int DIMENSIONS = 2; // Number of variables
    private static final int SWARM_SIZE = 30; // Number of particles
    private static final int MAX_ITERATIONS = 1000; // Number of iterations
    // Define PSO parameters
    private static final double W = 0.5; // Inertia weight
    private static final double C1 = 1.5; // Cognitive coefficient
    private static final double C2 = 1.5; // Social coefficient
    // Define boundaries for the optimization problem
    private static final double LOWER_BOUND = -10.0;
    private static final double UPPER_BOUND = 10.0;
    public static void main(String[] args) {
    Particle[] particles = new Particle[SWARM_SIZE];
    Particle globalBest = null;
    double globalBestFitness = Double.POSITIVE_INFINITY;
    // Initialize particles
    for (int i = 0; i < SWARM_SIZE; i++) {
    particles[i] = new Particle(DIMENSIONS, LOWER_BOUND, UPPER_BOUND);
    double fitness = fitnessFunction(particles[i].position);
    particles[i].fitness = fitness;
    if (fitness < globalBestFitness) {
    globalBestFitness = fitness;
    globalBest = particles[i];
    }
    }
    // Run PSO iterations
    for (int iteration = 0; iteration < MAX_ITERATIONS; iteration++) {
    for (int i = 0; i < SWARM_SIZE; i++) {
    particles[i].updateVelocity(globalBest);
    particles[i].updatePosition(LOWER_BOUND, UPPER_BOUND);
    particles[i].fitness = fitnessFunction(particles[i].position);
    if (particles[i].fitness < globalBestFitness) {
    globalBestFitness = particles[i].fitness;
    globalBest = particles[i];
    }
    }
    // Print the best solution in this iteration
    System.out.println("Iteration " + iteration + ": Best Fitness = " + globalBestFitness);
    }
    // Final best solution
    System.out.println("\nFinal Best Solution: ");
    System.out.println("Position: " + arrayToString(globalBest.position));
    System.out.println("Fitness: " + globalBestFitness);
    }
    // Sphere function to minimize (you can replace it with any other function)
    private static double fitnessFunction(double[] position) {
    double sum = 0.0;
    for (int i = 0; i < position.length; i++) {
    sum += position[i] * position[i]; // f(x) = sum(x^2)
    }
    return sum;
    }
    // Helper method to convert array to string for display
    private static String arrayToString(double[] array) {
    StringBuilder sb = new StringBuilder();
    for (double v : array) {
    sb.append(String.format("%.4f", v)).append(" ");
    }
    return sb.toString();
    }
    // Particle class representing each particle in the swarm
    static class Particle {
    double[] position;
    double[] velocity;
    double[] bestPosition;
    double fitness;
    // Constructor for the particle
    public Particle(int dimensions, double lowerBound, double upperBound) {
    position = new double[dimensions];
    velocity = new double[dimensions];
    bestPosition = new double[dimensions];
    Random rand = new Random();
    // Initialize position and velocity randomly
    for (int i = 0; i < dimensions; i++) {
    position[i] = lowerBound + (upperBound - lowerBound) * rand.nextDouble();
    velocity[i] = -1 + (1 + 1) * rand.nextDouble();
    }
    // Set initial best position
    System.arraycopy(position, 0, bestPosition, 0, dimensions);
    this.fitness = fitnessFunction(position);
    }
    // Update particle velocity based on its own best and the global best
    public void updateVelocity(Particle globalBest) {
    Random rand = new Random();
    for (int i = 0; i < position.length; i++) {
    double r1 = rand.nextDouble();
    double r2 = rand.nextDouble();
    velocity[i] = W * velocity[i]
    + C1 * r1 * (bestPosition[i] - position[i])
    + C2 * r2 * (globalBest.position[i] - position[i]);
    }
    }
    // Update particle position based on its velocity
    public void updatePosition(double lowerBound, double upperBound) {
    for (int i = 0; i < position.length; i++) {
    position[i] += velocity[i];
    if (position[i] < lowerBound) {
    position[i] = lowerBound;
    } else if (position[i] > upperBound) {
    position[i] = upperBound;
    }
    }
    // Update personal best position
    if (fitnessFunction(position) < fitness) {
    bestPosition = position.clone();
    }
    }
    }
    }
ScreenShots
  • Perform Particle Swarm Optimization Scheduling Algorithm1
  • Perform Particle Swarm Optimization Scheduling Algorithm2