├── .gitignore ├── jars ├── cloudsim-3.0.3.jar ├── jswarm-pso_2_08.jar └── commons-math3-3.6.1.jar ├── CO423_Project_2K18_IT_072_073.pdf └── src ├── ACO ├── ACO_FitnessFunction.java ├── ACO_DatacenterBroker.java ├── ACO_Scheduler.java ├── Ant.java └── ACO.java ├── PSO ├── PSO_ParticleUpdate.java ├── PSO_FitnessFunction.java ├── PSO_Particle.java ├── PSO.java ├── PSO_Scheduler.java └── PSO_DatacenterBroker.java ├── utils ├── Constants.java ├── GenerateLengthMatrix.java ├── Calculator.java └── Commons.java ├── SJF ├── SJF_Scheduler.java └── SJF_DatacenterBroker.java ├── FCFS ├── FCFS_Scheduler.java └── FCFS_DatacenterBroker.java ├── RoundRobin ├── RoundRobin_Scheduler.java └── RoundRobin_DatacenterBroker.java └── Scheduler_Comparison.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | out/ 4 | LengthMatrix.txt -------------------------------------------------------------------------------- /jars/cloudsim-3.0.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mgkbadola/Comparative-Study-Of-Different-Cloud-Task-Scheduling-Algorithms/HEAD/jars/cloudsim-3.0.3.jar -------------------------------------------------------------------------------- /jars/jswarm-pso_2_08.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mgkbadola/Comparative-Study-Of-Different-Cloud-Task-Scheduling-Algorithms/HEAD/jars/jswarm-pso_2_08.jar -------------------------------------------------------------------------------- /jars/commons-math3-3.6.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mgkbadola/Comparative-Study-Of-Different-Cloud-Task-Scheduling-Algorithms/HEAD/jars/commons-math3-3.6.1.jar -------------------------------------------------------------------------------- /CO423_Project_2K18_IT_072_073.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mgkbadola/Comparative-Study-Of-Different-Cloud-Task-Scheduling-Algorithms/HEAD/CO423_Project_2K18_IT_072_073.pdf -------------------------------------------------------------------------------- /src/ACO/ACO_FitnessFunction.java: -------------------------------------------------------------------------------- 1 | package ACO; 2 | 3 | import utils.Constants; 4 | import utils.GenerateLengthMatrix; 5 | 6 | public class ACO_FitnessFunction { 7 | 8 | private static double[][] lengthMatrix; 9 | 10 | public ACO_FitnessFunction() 11 | { 12 | lengthMatrix = GenerateLengthMatrix.getlengthMatrix(); 13 | } 14 | 15 | public double calcTotalTime(double[] position) { 16 | double totalCost = 0; 17 | for (int i = 0; i < Constants.NO_OF_TASKS; i++) { 18 | int dcId = (int) position[i]; 19 | totalCost += lengthMatrix[i][dcId]; 20 | } 21 | return totalCost; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/ACO/ACO_DatacenterBroker.java: -------------------------------------------------------------------------------- 1 | package ACO; 2 | 3 | import org.cloudbus.cloudsim.*; 4 | import utils.Constants; 5 | 6 | public class ACO_DatacenterBroker extends DatacenterBroker { 7 | 8 | public ACO_DatacenterBroker(String name) throws Exception { 9 | super(name); 10 | } 11 | 12 | public void RunACO(int antcount, int maxgen){ 13 | ACO aco; 14 | aco = new ACO(); 15 | aco.init(antcount, cloudletList, vmList); 16 | aco.run(maxgen); 17 | aco.ReportResult(); 18 | double[] bestposition = new double[Constants.NO_OF_TASKS]; 19 | for (int i = 0; i < cloudletList.size(); i++) { 20 | cloudletList.get(aco.bestTour[i].task).setVmId(aco.bestTour[i].vm); 21 | bestposition[i] = aco.bestTour[i].vm; 22 | } 23 | ACO_FitnessFunction ff = new ACO_FitnessFunction(); 24 | System.out.println("Best Total Cost: "+ff.calcTotalTime(bestposition)); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/PSO/PSO_ParticleUpdate.java: -------------------------------------------------------------------------------- 1 | package PSO; 2 | 3 | import net.sourceforge.jswarm_pso.Particle; 4 | import net.sourceforge.jswarm_pso.ParticleUpdate; 5 | import net.sourceforge.jswarm_pso.Swarm; 6 | import utils.Constants; 7 | 8 | public class PSO_ParticleUpdate extends ParticleUpdate { 9 | private static final double W = 0.9; 10 | private static final double C = 2.0; 11 | 12 | PSO_ParticleUpdate(Particle particle) { 13 | super(particle); 14 | } 15 | 16 | @Override 17 | public void update(Swarm swarm, Particle particle) { 18 | double[] v = particle.getVelocity(); 19 | double[] x = particle.getPosition(); 20 | double[] pbest = particle.getBestPosition(); 21 | double[] gbest = swarm.getBestPosition(); 22 | 23 | for (int i = 0; i < Constants.NO_OF_TASKS; ++i) { 24 | v[i] = W * v[i] + C * Math.random() * (pbest[i] - x[i]) + C * Math.random() * (gbest[i] - x[i]); 25 | x[i] = (int) (x[i] + v[i]); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /src/utils/Constants.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | public class Constants { 4 | public static int POPULATION_SIZE = 25; 5 | public static int NO_OF_ANTS = 4; 6 | public static int NO_OF_GENERATIONS = 50; 7 | 8 | //Datacenter Parameters 9 | public static int NO_OF_DATACENTERS = 5; 10 | public static String ARCHITECTURE = "x86"; 11 | public static String OS = "Linux"; 12 | public static double TIME_ZONE = 5.5; 13 | public static double COST_PROCESSING = 3.0; 14 | public static double COST_MEMORY = 0.05; 15 | public static double COST_STORAGE = 0.001; 16 | public static double COST_BANDWIDTH = 0.1; 17 | 18 | //Host Parameters 19 | public static int STORAGE = 1000000; 20 | public static int HOST_RAM = 2048; 21 | public static int HOST_BANDWIDTH = 10000; 22 | public static int HOST_MIPS = 1000; 23 | 24 | //VM Parameters 25 | public static int NO_OF_VMS = 5; 26 | public static long VM_IMAGE_SIZE = 10000; 27 | public static int VM_RAM = 512; 28 | public static int VM_MIPS = 250; 29 | public static long VM_BANDWIDTH = 1000; 30 | public static int VM_PES = 1; 31 | public static String VMM_NAME = "Topa"; 32 | 33 | //Cloudlet Parameters 34 | public static int NO_OF_TASKS = 30; 35 | public static long FILE_SIZE = 300; 36 | public static long OUTPUT_SIZE = 300; 37 | public static int TASK_PES = 1; 38 | } 39 | -------------------------------------------------------------------------------- /src/PSO/PSO_FitnessFunction.java: -------------------------------------------------------------------------------- 1 | package PSO; 2 | 3 | import net.sourceforge.jswarm_pso.FitnessFunction; 4 | import utils.Constants; 5 | import utils.GenerateLengthMatrix; 6 | 7 | public class PSO_FitnessFunction extends FitnessFunction { 8 | private static double[][] lengthMatrix; 9 | 10 | PSO_FitnessFunction() { 11 | super(false); 12 | lengthMatrix = GenerateLengthMatrix.getlengthMatrix(); 13 | } 14 | 15 | @Override 16 | public double evaluate(double[] position) { 17 | double alpha = 0.3; 18 | return alpha * calcTotalTime(position) + (1 - alpha) * calcMakespan(position); 19 | } 20 | 21 | private double calcTotalTime(double[] position) { 22 | double totalCost = 0; 23 | for (int i = 0; i < Constants.NO_OF_TASKS; i++) { 24 | int dcId = (int) position[i]; 25 | totalCost += lengthMatrix[i][dcId]; 26 | } 27 | return totalCost; 28 | } 29 | 30 | public double calcMakespan(double[] position) { 31 | double makespan = 0; 32 | double[] dcWorkingTime = new double[Constants.NO_OF_DATACENTERS]; 33 | 34 | for (int i = 0; i < Constants.NO_OF_TASKS; i++) { 35 | int dcId = (int) position[i]; 36 | if(dcWorkingTime[dcId] != 0) --dcWorkingTime[dcId]; 37 | dcWorkingTime[dcId] += lengthMatrix[i][dcId]; 38 | makespan = Math.max(makespan, dcWorkingTime[dcId]); 39 | } 40 | return makespan; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/PSO/PSO_Particle.java: -------------------------------------------------------------------------------- 1 | package PSO; 2 | 3 | import net.sourceforge.jswarm_pso.Particle; 4 | import utils.Constants; 5 | 6 | import java.util.Random; 7 | 8 | public class PSO_Particle extends Particle { 9 | PSO_Particle() { 10 | super(Constants.NO_OF_TASKS); 11 | double[] position = new double[Constants.NO_OF_TASKS]; 12 | double[] velocity = new double[Constants.NO_OF_TASKS]; 13 | 14 | for (int i = 0; i < Constants.NO_OF_TASKS; i++) { 15 | Random randObj = new Random(); 16 | position[i] = randObj.nextInt(Constants.NO_OF_DATACENTERS); 17 | velocity[i] = Math.random(); 18 | } 19 | setPosition(position); 20 | setVelocity(velocity); 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | String output = ""; 26 | for (int i = 0; i < Constants.NO_OF_DATACENTERS; i++) { 27 | String tasks = ""; 28 | int no_of_tasks = 0; 29 | for (int j = 0; j < Constants.NO_OF_TASKS; j++) { 30 | if (i == (int) getPosition()[j]) { 31 | tasks += (tasks.isEmpty() ? "" : " ") + j; 32 | ++no_of_tasks; 33 | } 34 | } 35 | if (tasks.isEmpty()) output += "There is no tasks associated to Data Center " + i + "\n"; 36 | else 37 | output += "There are " + no_of_tasks + " tasks associated to Data Center " + i + " and they are " + tasks + "\n"; 38 | } 39 | return output; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/SJF/SJF_Scheduler.java: -------------------------------------------------------------------------------- 1 | package SJF; 2 | 3 | import org.cloudbus.cloudsim.*; 4 | import org.cloudbus.cloudsim.core.CloudSim; 5 | import utils.*; 6 | import java.util.List; 7 | 8 | public class SJF_Scheduler { 9 | 10 | public static double main(String[] args) { 11 | double finishtime = 0.0; 12 | Log.printLine("Starting SJF Scheduler..."); 13 | 14 | try { 15 | Commons.set_cloudsim_parameters(); 16 | CloudSim.init(Commons.num_user, Commons.calendar, Commons.trace_flag); 17 | 18 | Commons.createDatacenters(); 19 | 20 | SJF_DatacenterBroker broker = createBroker("Broker_0"); 21 | int brokerId = broker.getId(); 22 | 23 | Commons.create_vms_and_cloudlets(brokerId, 2); 24 | 25 | broker.submitVmList(Commons.vmList); 26 | broker.submitCloudletList(Commons.cloudletList); 27 | 28 | CloudSim.startSimulation(); 29 | CloudSim.stopSimulation(); 30 | 31 | List newList = broker.getCloudletReceivedList(); 32 | finishtime = Commons.printCloudletList(newList, 2, null); 33 | 34 | Log.printLine("SJF Scheduler finished!"); 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | Log.printLine("The simulation has been terminated due to an unexpected error"); 38 | } 39 | return finishtime; 40 | } 41 | 42 | private static SJF_DatacenterBroker createBroker(String name) throws Exception { 43 | return new SJF_DatacenterBroker(name); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/FCFS/FCFS_Scheduler.java: -------------------------------------------------------------------------------- 1 | package FCFS; 2 | 3 | import org.cloudbus.cloudsim.*; 4 | import org.cloudbus.cloudsim.core.CloudSim; 5 | import utils.*; 6 | import java.util.List; 7 | 8 | public class FCFS_Scheduler { 9 | 10 | public static double main(String[] args) { 11 | double finishtime = 0.0; 12 | Log.printLine("Starting FCFS Scheduler..."); 13 | 14 | try { 15 | Commons.set_cloudsim_parameters(); 16 | CloudSim.init(Commons.num_user, Commons.calendar, Commons.trace_flag); 17 | 18 | Commons.createDatacenters(); 19 | 20 | FCFS_DatacenterBroker broker = createBroker("Broker_0"); 21 | int brokerId = broker.getId(); 22 | 23 | Commons.create_vms_and_cloudlets(brokerId, 1); 24 | 25 | broker.submitVmList(Commons.vmList); 26 | broker.submitCloudletList(Commons.cloudletList); 27 | 28 | CloudSim.startSimulation(); 29 | CloudSim.stopSimulation(); 30 | 31 | List newList = broker.getCloudletReceivedList(); 32 | finishtime = Commons.printCloudletList(newList, 1, null); 33 | 34 | Log.printLine("FCFS Scheduler finished!"); 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | Log.printLine("The simulation has been terminated due to an unexpected error"); 38 | } 39 | return finishtime; 40 | } 41 | 42 | private static FCFS_DatacenterBroker createBroker(String name) throws Exception { 43 | return new FCFS_DatacenterBroker(name); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/RoundRobin/RoundRobin_Scheduler.java: -------------------------------------------------------------------------------- 1 | package RoundRobin; 2 | 3 | import org.cloudbus.cloudsim.*; 4 | import org.cloudbus.cloudsim.core.CloudSim; 5 | import utils.*; 6 | import java.util.List; 7 | 8 | public class RoundRobin_Scheduler { 9 | 10 | public static double main(String[] args) { 11 | double finishtime = 0.0; 12 | Log.printLine("Starting Round Robin Scheduler..."); 13 | 14 | try { 15 | Commons.set_cloudsim_parameters(); 16 | CloudSim.init(Commons.num_user, Commons.calendar, Commons.trace_flag); 17 | 18 | Commons.createDatacenters(); 19 | 20 | RoundRobin_DatacenterBroker broker = createBroker("Broker_0"); 21 | int brokerId = broker.getId(); 22 | 23 | Commons.create_vms_and_cloudlets(brokerId, 3); 24 | 25 | broker.submitVmList(Commons.vmList); 26 | broker.submitCloudletList(Commons.cloudletList); 27 | 28 | CloudSim.startSimulation(); 29 | CloudSim.stopSimulation(); 30 | 31 | List newList = broker.getCloudletReceivedList(); 32 | finishtime = Commons.printCloudletList(newList, 3, null); 33 | 34 | Log.printLine("Round Robin Scheduler finished!"); 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | Log.printLine("The simulation has been terminated due to an unexpected error"); 38 | } 39 | return finishtime; 40 | } 41 | 42 | private static RoundRobin_DatacenterBroker createBroker(String name) throws Exception { 43 | return new RoundRobin_DatacenterBroker(name); 44 | } 45 | } -------------------------------------------------------------------------------- /src/ACO/ACO_Scheduler.java: -------------------------------------------------------------------------------- 1 | package ACO; 2 | 3 | import org.cloudbus.cloudsim.*; 4 | import org.cloudbus.cloudsim.core.CloudSim; 5 | import utils.Commons; 6 | import utils.Constants; 7 | import java.util.List; 8 | 9 | public class ACO_Scheduler 10 | { 11 | 12 | public static double main(String[] args) 13 | { 14 | double finishtime = 0.0; 15 | Log.printLine("Starting ACO Scheduler..."); 16 | 17 | try { 18 | Commons.set_cloudsim_parameters(); 19 | CloudSim.init(Commons.num_user, Commons.calendar, Commons.trace_flag); 20 | 21 | Commons.createDatacenters(); 22 | 23 | ACO_DatacenterBroker broker = createBroker("Broker_0"); 24 | int brokerId = broker.getId(); 25 | 26 | Commons.create_vms_and_cloudlets(brokerId,5); 27 | 28 | broker.submitVmList(Commons.vmList); 29 | broker.submitCloudletList(Commons.cloudletList); 30 | 31 | broker.RunACO(Constants.NO_OF_ANTS, Constants.NO_OF_GENERATIONS); 32 | 33 | CloudSim.startSimulation(); 34 | CloudSim.stopSimulation(); 35 | 36 | List newList = broker.getCloudletReceivedList(); 37 | finishtime = Commons.printCloudletList(newList, 5, null); 38 | 39 | Log.printLine("ACO Scheduler finished!"); 40 | } catch (Exception e) { 41 | e.printStackTrace(); 42 | Log.printLine("The simulation has been terminated due to an unexpected error"); 43 | } 44 | return finishtime; 45 | } 46 | 47 | 48 | private static ACO_DatacenterBroker createBroker(String name) throws Exception { 49 | return new ACO_DatacenterBroker(name); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Scheduler_Comparison.java: -------------------------------------------------------------------------------- 1 | import ACO.ACO_Scheduler; 2 | import FCFS.FCFS_Scheduler; 3 | import PSO.PSO_Scheduler; 4 | import RoundRobin.RoundRobin_Scheduler; 5 | import SJF.SJF_Scheduler; 6 | import utils.Commons; 7 | import utils.GenerateLengthMatrix; 8 | 9 | import java.util.SortedMap; 10 | import java.util.TreeMap; 11 | import java.util.concurrent.TimeUnit; 12 | 13 | public class Scheduler_Comparison { 14 | public static void main(String[] args) throws InterruptedException { 15 | SortedMap map = new TreeMap<>(); 16 | new GenerateLengthMatrix(); 17 | Commons.lengthMatrix = GenerateLengthMatrix.getlengthMatrix(); 18 | map.put(FCFS_Scheduler.main(args), "First Come-First Serve"); 19 | System.out.println("==========================================="); 20 | TimeUnit.SECONDS.sleep(1); 21 | map.put(SJF_Scheduler.main(args), "Shortest Job First"); 22 | System.out.println("==========================================="); 23 | TimeUnit.SECONDS.sleep(1); 24 | map.put(RoundRobin_Scheduler.main(args), "Round Robin"); 25 | System.out.println("==========================================="); 26 | TimeUnit.SECONDS.sleep(1); 27 | map.put(PSO_Scheduler.main(args), "Particle Swarm Optimisation"); 28 | System.out.println("==========================================="); 29 | TimeUnit.SECONDS.sleep(1); 30 | map.put(ACO_Scheduler.main(args), "Ant Colony Optimisation"); 31 | System.out.println("==========================================="); 32 | TimeUnit.SECONDS.sleep(1); 33 | System.out.println("Sorted list of algorithms (criteria: earliest finish time)"); 34 | for(double i: map.keySet()){ 35 | System.out.printf("%s: %.2f%n", map.get(i), i); 36 | } 37 | System.out.println("==========================================="); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/PSO/PSO.java: -------------------------------------------------------------------------------- 1 | package PSO; 2 | 3 | import net.sourceforge.jswarm_pso.Swarm; 4 | import utils.Constants; 5 | 6 | public class PSO { 7 | private static Swarm swarm; 8 | private static PSO_Particle[] particles; 9 | private static final PSO_FitnessFunction ff = new PSO_FitnessFunction(); 10 | 11 | public PSO() { 12 | initParticles(); 13 | } 14 | 15 | 16 | public double[] run() { 17 | swarm = new Swarm(Constants.POPULATION_SIZE, new PSO_Particle(), ff); 18 | 19 | swarm.setMinPosition(0); 20 | swarm.setMaxPosition(Constants.NO_OF_DATACENTERS - 1); 21 | swarm.setMaxMinVelocity(0.5); 22 | swarm.setParticles(particles); 23 | swarm.setParticleUpdate(new PSO_ParticleUpdate(new PSO_Particle())); 24 | 25 | for (int i = 0; i < 500; i++) { 26 | swarm.evolve(); 27 | if (i % 10 == 0) { 28 | System.out.printf("Global best at iteration (%d): %f\n", i, swarm.getBestFitness()); 29 | } 30 | } 31 | 32 | System.out.println("\nThe best fitness value: " + swarm.getBestFitness() + "\nBest makespan: " + ff.calcMakespan(swarm.getBestParticle().getBestPosition())); 33 | 34 | System.out.println("The best solution is: "); 35 | PSO_Particle bestParticle = (PSO_Particle) swarm.getBestParticle(); 36 | System.out.println(bestParticle.toString()); 37 | 38 | return swarm.getBestPosition(); 39 | } 40 | 41 | private static void initParticles() { 42 | particles = new PSO_Particle[Constants.POPULATION_SIZE]; 43 | for (int i = 0; i < Constants.POPULATION_SIZE; ++i) 44 | particles[i] = new PSO_Particle(); 45 | } 46 | 47 | public void printBestFitness() { 48 | System.out.println("\nBest fitness value: " + swarm.getBestFitness()+ 49 | "\nBest makespan: " + ff.calcMakespan(swarm.getBestParticle().getBestPosition())); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/utils/GenerateLengthMatrix.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | 4 | import java.io.*; 5 | 6 | public class GenerateLengthMatrix { 7 | private static double[][] lengthMatrix; 8 | private final File lengthFile = new File("LengthMatrix.txt"); 9 | 10 | public GenerateLengthMatrix() { 11 | lengthMatrix = new double[Constants.NO_OF_TASKS][Constants.NO_OF_DATACENTERS]; 12 | try { 13 | if (lengthFile.exists()) { 14 | readCostMatrix(); 15 | } else { 16 | initCostMatrix(); 17 | } 18 | } catch (IOException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | 23 | private void initCostMatrix() throws IOException { 24 | System.out.println("Initializing new Length Matrix..."); 25 | BufferedWriter lengthBufferedWriter = new BufferedWriter(new FileWriter(lengthFile)); 26 | 27 | for (int i = 0; i < Constants.NO_OF_TASKS; i++) { 28 | for (int j = 0; j < Constants.NO_OF_DATACENTERS; j++) { 29 | lengthMatrix[i][j] = Math.random() * 700 + 30; 30 | lengthBufferedWriter.write(String.format("%.2f ", lengthMatrix[i][j])); 31 | } 32 | lengthBufferedWriter.write('\n'); 33 | } 34 | lengthBufferedWriter.close(); 35 | } 36 | 37 | private void readCostMatrix() throws IOException { 38 | System.out.println("Reading the Length Matrix..."); 39 | BufferedReader lengthBufferedReader = new BufferedReader(new FileReader(lengthFile)); 40 | int i = 0, j = 0; 41 | do { 42 | String line = lengthBufferedReader.readLine(); 43 | for (String num : line.split(" ")) { 44 | lengthMatrix[i][j++] = Double.parseDouble(num); 45 | } 46 | ++i; 47 | j = 0; 48 | } while (lengthBufferedReader.ready()); 49 | lengthBufferedReader.close(); 50 | } 51 | 52 | public static double[][] getlengthMatrix() { 53 | return lengthMatrix; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/FCFS/FCFS_DatacenterBroker.java: -------------------------------------------------------------------------------- 1 | package FCFS; 2 | 3 | 4 | import org.cloudbus.cloudsim.Cloudlet; 5 | import org.cloudbus.cloudsim.DatacenterBroker; 6 | import org.cloudbus.cloudsim.Log; 7 | import org.cloudbus.cloudsim.core.CloudSim; 8 | import org.cloudbus.cloudsim.core.SimEvent; 9 | 10 | import java.util.ArrayList; 11 | 12 | 13 | public class FCFS_DatacenterBroker extends DatacenterBroker { 14 | 15 | public FCFS_DatacenterBroker(String name) throws Exception { 16 | super(name); 17 | } 18 | 19 | //scheduling function 20 | public void scheduleTaskstoVms() { 21 | 22 | ArrayList clist = new ArrayList(); 23 | 24 | for (Cloudlet cloudlet : getCloudletSubmittedList()) { 25 | clist.add(cloudlet); 26 | } 27 | 28 | setCloudletReceivedList(clist); 29 | } 30 | 31 | @Override 32 | protected void processCloudletReturn(SimEvent ev) { 33 | Cloudlet cloudlet = (Cloudlet) ev.getData(); 34 | getCloudletReceivedList().add(cloudlet); 35 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloudlet " + cloudlet.getCloudletId() 36 | + " received"); 37 | cloudletsSubmitted--; 38 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { 39 | scheduleTaskstoVms(); 40 | cloudletExecution(cloudlet); 41 | } 42 | } 43 | 44 | 45 | protected void cloudletExecution(Cloudlet cloudlet) { 46 | 47 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { // all cloudlets executed 48 | Log.printLine(CloudSim.clock() + ": " + getName() + ": All Cloudlets executed. Finishing..."); 49 | clearDatacenters(); 50 | finishExecution(); 51 | } else { // some cloudlets haven't finished yet 52 | if (getCloudletList().size() > 0 && cloudletsSubmitted == 0) { 53 | // all the cloudlets sent finished. It means that some bount 54 | // cloudlet is waiting its VM be created 55 | clearDatacenters(); 56 | createVmsInDatacenter(0); 57 | } 58 | 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /src/utils/Calculator.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | import java.io.BufferedWriter; 3 | import java.io.FileOutputStream; 4 | import java.io.IOException; 5 | import java.io.OutputStreamWriter; 6 | import java.math.BigDecimal; 7 | import java.math.RoundingMode; 8 | public class Calculator 9 | { 10 | 11 | private static final int DEF_DIV_SCALE = 10; 12 | 13 | private Calculator(){} 14 | 15 | public static double add(double v1, double v2) 16 | { 17 | BigDecimal b1 = BigDecimal.valueOf(v1); 18 | BigDecimal b2 = BigDecimal.valueOf(v2); 19 | return b1.add(b2).doubleValue(); 20 | } 21 | 22 | public static double sub(double v1, double v2) 23 | { 24 | BigDecimal b1 = BigDecimal.valueOf(v1); 25 | BigDecimal b2 = BigDecimal.valueOf(v2); 26 | return b1.subtract(b2).doubleValue(); 27 | } 28 | 29 | public static double mul(double v1, double v2) 30 | { 31 | BigDecimal b1 = BigDecimal.valueOf(v1); 32 | BigDecimal b2 = BigDecimal.valueOf(v2); 33 | return b1.multiply(b2).doubleValue(); 34 | } 35 | 36 | 37 | public static double div(double v1, double v2) 38 | { 39 | BigDecimal b1 = BigDecimal.valueOf(v1); 40 | BigDecimal b2 = BigDecimal.valueOf(v2); 41 | return b1.divide(b2, DEF_DIV_SCALE, RoundingMode.HALF_UP).doubleValue(); 42 | } 43 | 44 | public static double div(double d1,double d2,int len) { 45 | BigDecimal b1 = new BigDecimal(d1); 46 | BigDecimal b2 = new BigDecimal(d2); 47 | return b1.divide(b2, len, RoundingMode.HALF_UP).doubleValue(); 48 | } 49 | 50 | public static void LogResult(String file, String conent) { 51 | BufferedWriter out = null; 52 | try { 53 | out = new BufferedWriter(new OutputStreamWriter( 54 | new FileOutputStream(file, true))); 55 | out.write(conent+"\r\n"); 56 | 57 | } catch (Exception e) { 58 | e.printStackTrace(); 59 | } finally { 60 | try { 61 | out.close(); 62 | } catch (IOException e) { 63 | e.printStackTrace(); 64 | } 65 | } 66 | } 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/RoundRobin/RoundRobin_DatacenterBroker.java: -------------------------------------------------------------------------------- 1 | package RoundRobin; 2 | 3 | 4 | import org.cloudbus.cloudsim.DatacenterBroker; 5 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 6 | import org.cloudbus.cloudsim.Log; 7 | import org.cloudbus.cloudsim.Vm; 8 | import org.cloudbus.cloudsim.core.CloudSim; 9 | import org.cloudbus.cloudsim.core.CloudSimTags; 10 | import org.cloudbus.cloudsim.core.SimEvent; 11 | 12 | import java.util.List; 13 | 14 | public class RoundRobin_DatacenterBroker extends DatacenterBroker { 15 | 16 | public RoundRobin_DatacenterBroker(String name) throws Exception { 17 | super(name); 18 | } 19 | 20 | @Override 21 | protected void processResourceCharacteristics(SimEvent ev) { 22 | DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData(); 23 | getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics); 24 | 25 | if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size()) { 26 | distributeRequestsForNewVmsAcrossDatacentersUsingTheRoundRobinApproach(); 27 | } 28 | } 29 | 30 | /** 31 | * Distributes the VMs across the data centers using the round-robin approach. A VM is allocated to a data center only if there isn't 32 | * a VM in the data center with the same id. 33 | */ 34 | protected void distributeRequestsForNewVmsAcrossDatacentersUsingTheRoundRobinApproach() { 35 | int numberOfVmsAllocated = 0; 36 | int i = 0; 37 | 38 | final List availableDatacenters = getDatacenterIdsList(); 39 | 40 | for (Vm vm : getVmList()) { 41 | int datacenterId = availableDatacenters.get(i++ % availableDatacenters.size()); 42 | String datacenterName = CloudSim.getEntityName(datacenterId); 43 | 44 | if (!getVmsToDatacentersMap().containsKey(vm.getId())) { 45 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() + " in " + datacenterName); 46 | sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); 47 | numberOfVmsAllocated++; 48 | } 49 | } 50 | 51 | setVmsRequested(numberOfVmsAllocated); 52 | setVmsAcks(0); 53 | } 54 | } -------------------------------------------------------------------------------- /src/PSO/PSO_Scheduler.java: -------------------------------------------------------------------------------- 1 | package PSO; 2 | 3 | import org.cloudbus.cloudsim.*; 4 | import org.cloudbus.cloudsim.core.CloudSim; 5 | import utils.*; 6 | import java.util.*; 7 | 8 | public class PSO_Scheduler { 9 | private static PSO PSOSchedulerInstance; 10 | 11 | public static double main(String[] args) { 12 | double finishtime = 0.0; 13 | Log.printLine("Starting PSO Scheduler..."); 14 | 15 | PSOSchedulerInstance = new PSO(); 16 | Commons.mapping = PSOSchedulerInstance.run(); 17 | try { 18 | Commons.set_cloudsim_parameters(); 19 | CloudSim.init(Commons.num_user, Commons.calendar, Commons.trace_flag); 20 | 21 | Commons.createDatacenters(); 22 | 23 | PSO_DatacenterBroker broker = createBroker("Broker_0"); 24 | int brokerId = broker.getId(); 25 | 26 | Commons.create_vms_and_cloudlets(brokerId, 4); 27 | 28 | HashSet dcIds = new HashSet<>(); 29 | HashMap hm = new HashMap<>(); 30 | for (Datacenter dc : Commons.datacenter) { 31 | if (!dcIds.contains(dc.getId())) 32 | dcIds.add(dc.getId()); 33 | } 34 | Iterator it = dcIds.iterator(); 35 | for (int i = 0; i < Commons.mapping.length; i++) { 36 | if (hm.containsKey((int) Commons.mapping[i])) continue; 37 | hm.put((int) Commons.mapping[i], it.next()); 38 | } 39 | for (int i = 0; i < Commons.mapping.length; i++) 40 | Commons.mapping[i] = hm.containsKey((int) Commons.mapping[i]) ? hm.get((int) Commons.mapping[i]) : Commons.mapping[i]; 41 | 42 | broker.submitVmList(Commons.vmList); 43 | broker.setMapping(Commons.mapping); 44 | broker.submitCloudletList(Commons.cloudletList); 45 | 46 | CloudSim.startSimulation(); 47 | List newList = broker.getCloudletReceivedList(); 48 | CloudSim.stopSimulation(); 49 | 50 | finishtime = Commons.printCloudletList(newList, 4, PSOSchedulerInstance); 51 | 52 | Log.printLine("PSO Scheduler finished!"); 53 | } catch (Exception e) { 54 | e.printStackTrace(); 55 | Log.printLine("The simulation has been terminated due to an unexpected error"); 56 | } 57 | return finishtime; 58 | } 59 | 60 | private static PSO_DatacenterBroker createBroker(String name) throws Exception { 61 | return new PSO_DatacenterBroker(name); 62 | } 63 | 64 | } -------------------------------------------------------------------------------- /src/PSO/PSO_DatacenterBroker.java: -------------------------------------------------------------------------------- 1 | package PSO; 2 | 3 | 4 | import org.cloudbus.cloudsim.*; 5 | import org.cloudbus.cloudsim.core.CloudSim; 6 | import org.cloudbus.cloudsim.core.CloudSimTags; 7 | import org.cloudbus.cloudsim.core.SimEvent; 8 | import org.cloudbus.cloudsim.lists.VmList; 9 | 10 | import java.util.List; 11 | 12 | public class PSO_DatacenterBroker extends DatacenterBroker { 13 | 14 | private double[] mapping; 15 | 16 | public PSO_DatacenterBroker(String name) throws Exception { 17 | super(name); 18 | } 19 | 20 | public void setMapping(double[] mapping) { 21 | this.mapping = mapping; 22 | } 23 | 24 | private List assignCloudletsToVms(List cloudlist) { 25 | int idx = 0; 26 | for (Cloudlet cl : cloudlist) { 27 | cl.setVmId((int) mapping[idx++]); 28 | } 29 | return cloudlist; 30 | } 31 | 32 | @Override 33 | protected void submitCloudlets() { 34 | List tasks = assignCloudletsToVms(getCloudletList()); 35 | int vmIndex = 0; 36 | for (Cloudlet cloudlet : tasks) { 37 | Vm vm; 38 | // if user didn't bind this cloudlet and it has not been executed yet 39 | if (cloudlet.getVmId() == -1) { 40 | vm = getVmsCreatedList().get(vmIndex); 41 | } else { // submit to the specific vm 42 | vm = VmList.getById(getVmsCreatedList(), cloudlet.getVmId()); 43 | if (vm == null) { // vm was not created 44 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Postponing execution of cloudlet " 45 | + cloudlet.getCloudletId() + ": bount VM not available"); 46 | continue; 47 | } 48 | } 49 | 50 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Sending cloudlet " 51 | + cloudlet.getCloudletId() + " to VM #" + vm.getId()); 52 | cloudlet.setVmId(vm.getId()); 53 | sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.CLOUDLET_SUBMIT, cloudlet); 54 | cloudletsSubmitted++; 55 | vmIndex = (vmIndex + 1) % getVmsCreatedList().size(); 56 | getCloudletSubmittedList().add(cloudlet); 57 | } 58 | } 59 | 60 | @Override 61 | protected void processResourceCharacteristics(SimEvent ev) { 62 | DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData(); 63 | getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics); 64 | 65 | if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size()) { 66 | distributeRequestsForNewVmsAcrossDatacenters(); 67 | } 68 | } 69 | 70 | protected void distributeRequestsForNewVmsAcrossDatacenters() { 71 | int numberOfVmsAllocated = 0; 72 | int i = 0; 73 | 74 | final List availableDatacenters = getDatacenterIdsList(); 75 | 76 | for (Vm vm : getVmList()) { 77 | int datacenterId = availableDatacenters.get(i++ % availableDatacenters.size()); 78 | String datacenterName = CloudSim.getEntityName(datacenterId); 79 | 80 | if (!getVmsToDatacentersMap().containsKey(vm.getId())) { 81 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() + " in " + datacenterName); 82 | sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); 83 | numberOfVmsAllocated++; 84 | } 85 | } 86 | 87 | setVmsRequested(numberOfVmsAllocated); 88 | setVmsAcks(0); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/ACO/Ant.java: -------------------------------------------------------------------------------- 1 | package ACO; 2 | 3 | import java.util.*; 4 | 5 | import org.cloudbus.cloudsim.Cloudlet; 6 | import org.cloudbus.cloudsim.Vm; 7 | import utils.Calculator; 8 | 9 | public class Ant{ 10 | public static class position{ 11 | public int vm; 12 | public int task; 13 | public position(int a, int b){ 14 | vm = a; 15 | task = b; 16 | } 17 | } 18 | public static double[][] delta; 19 | public int Q = 100; 20 | public List tour; 21 | public double tourLength; 22 | public long[] TL_task; 23 | public List tabu; 24 | private int VMs; 25 | private int tasks; 26 | private List cloudletList; 27 | private List vmList; 28 | 29 | public void RandomSelectVM(List list1, List list2){ 30 | cloudletList = list1; 31 | vmList = list2; 32 | VMs = vmList.size(); 33 | tasks = cloudletList.size(); 34 | delta = new double[VMs][tasks]; 35 | TL_task = new long[VMs]; 36 | for(int i=0; i(); 38 | tour=new ArrayList<>(); 39 | 40 | 41 | int firstVM = (int)(VMs*Math.random()); 42 | int firstExecute = (int)(tasks*Math.random()); 43 | tour.add(new position(firstVM, firstExecute)); 44 | tabu.add(firstExecute); 45 | TL_task[firstVM] += cloudletList.get(firstExecute).getCloudletLength(); 46 | } 47 | 48 | public double Dij(int vm, int task){ 49 | double d; 50 | double s = TL_task[vm]; 51 | double s1 = vmList.get(vm).getMips(); 52 | double s2 = cloudletList.get(task).getCloudletLength(); 53 | double s3 = vmList.get(vm).getBw(); 54 | double r1 = Calculator.div(s,s1,1); 55 | double r2 = Calculator.div(s2,s3,1); 56 | 57 | d = r1+r2; 58 | return d; 59 | } 60 | 61 | public void SelectNextVM(double[][] pheromone){ 62 | double[][] p; 63 | p = new double[VMs][tasks]; 64 | double alpha = 1.0; 65 | double beta = 1.0; 66 | double sum = 0; 67 | 68 | for(int i=0; i=selectp){ 94 | selectVM = i; 95 | selectTask = j; 96 | flag=false; 97 | break; 98 | } 99 | } 100 | } 101 | if (selectVM==-1 | selectTask == -1) 102 | System.out.println("Unable to select a virtual machine!"); 103 | tabu.add(selectTask); 104 | tour.add(new position(selectVM, selectTask)); 105 | TL_task[selectVM] += cloudletList.get(selectTask).getCloudletLength(); 106 | } 107 | 108 | 109 | 110 | public void CalTourLength(){ 111 | double[] max; 112 | max = new double[VMs]; 113 | for (Ant.position position : tour) { 114 | max[position.vm] += cloudletList.get(position.task).getCloudletLength() / vmList.get(position.vm).getMips(); 115 | } 116 | tourLength = max[0]; 117 | for(int i=0; itourLength)tourLength = max[i]; 119 | } 120 | } 121 | 122 | public void CalDelta(){ 123 | for(int i=0; i ants;//Defining ant colony 17 | private int antcount;//Number of ants 18 | private int Q = 100; 19 | private double[][] pheromone;//Pheromone matrix 20 | private double[][] Delta;//Total pheromone increment 21 | private int VMs;//Number of virtual machines 22 | private int tasks; 23 | public position[] bestTour;//Best solution 24 | private double bestLength;//Optimal solution length (time scale) 25 | private List cloudletList; 26 | private List vmList; 27 | 28 | public void init(int antNum, List list1, List list2){ 29 | cloudletList = list1; 30 | vmList = list2; 31 | antcount = antNum; 32 | ants = new ArrayList<>(); 33 | VMs = vmList.size(); 34 | tasks = cloudletList.size(); 35 | pheromone = new double[VMs][tasks]; 36 | Delta = new double[VMs][tasks]; 37 | bestLength = 1000000; 38 | //Initialize the pheromone matrix 39 | for(int i=0; i list = new ArrayList(); 31 | for (Cloudlet cloudlet : getCloudletReceivedList()) { 32 | list.add(cloudlet); 33 | } 34 | 35 | //setCloudletReceivedList(null); 36 | 37 | Cloudlet[] list2 = list.toArray(new Cloudlet[list.size()]); 38 | 39 | //System.out.println("size :"+list.size()); 40 | 41 | Cloudlet temp = null; 42 | 43 | int n = list.size(); 44 | 45 | for (int i = 0; i < n; i++) { 46 | for (int j = 1; j < (n - i); j++) { 47 | if (list2[j - 1].getCloudletLength() / (vm.getMips() * vm.getNumberOfPes()) > list2[j].getCloudletLength() / (vm.getMips() * vm.getNumberOfPes())) { 48 | //swap the elements! 49 | //swap(list2[j-1], list2[j]); 50 | temp = list2[j - 1]; 51 | list2[j - 1] = list2[j]; 52 | list2[j] = temp; 53 | } 54 | // printNumbers(list2); 55 | } 56 | } 57 | 58 | ArrayList list3 = new ArrayList(); 59 | 60 | for (int i = 0; i < list2.length; i++) { 61 | list3.add(list2[i]); 62 | } 63 | //printNumbers(list); 64 | 65 | setCloudletReceivedList(list); 66 | 67 | //System.out.println("\n\tSJFS Broker Schedules\n"); 68 | //System.out.println("\n"); 69 | } 70 | 71 | public void printNumber(Cloudlet[] list) { 72 | for (int i = 0; i < list.length; i++) { 73 | System.out.print(" " + list[i].getCloudletId()); 74 | System.out.println(list[i].getCloudletStatusString()); 75 | } 76 | System.out.println(); 77 | } 78 | 79 | public void printNumbers(ArrayList list) { 80 | for (int i = 0; i < list.size(); i++) { 81 | System.out.print(" " + list.get(i).getCloudletId()); 82 | } 83 | System.out.println(); 84 | } 85 | 86 | @Override 87 | protected void processCloudletReturn(SimEvent ev) { 88 | Cloudlet cloudlet = (Cloudlet) ev.getData(); 89 | getCloudletReceivedList().add(cloudlet); 90 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloudlet " + cloudlet.getCloudletId() 91 | + " received"); 92 | cloudletsSubmitted--; 93 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { 94 | scheduleTaskstoVms(); 95 | cloudletExecution(cloudlet); 96 | } 97 | } 98 | 99 | protected void cloudletExecution(Cloudlet cloudlet) { 100 | 101 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { // all cloudlets executed 102 | Log.printLine(CloudSim.clock() + ": " + getName() + ": All Cloudlets executed. Finishing..."); 103 | clearDatacenters(); 104 | finishExecution(); 105 | } else { // some cloudlets haven't finished yet 106 | if (getCloudletList().size() > 0 && cloudletsSubmitted == 0) { 107 | // all the cloudlets sent finished. It means that some bount 108 | // cloudlet is waiting its VM be created 109 | clearDatacenters(); 110 | createVmsInDatacenter(0); 111 | } 112 | } 113 | } 114 | 115 | @Override 116 | protected void processResourceCharacteristics(SimEvent ev) { 117 | DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData(); 118 | getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics); 119 | 120 | if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size()) { 121 | distributeRequestsForNewVmsAcrossDatacenters(); 122 | } 123 | } 124 | 125 | protected void distributeRequestsForNewVmsAcrossDatacenters() { 126 | int numberOfVmsAllocated = 0; 127 | int i = 0; 128 | 129 | final List availableDatacenters = getDatacenterIdsList(); 130 | 131 | for (Vm vm : getVmList()) { 132 | int datacenterId = availableDatacenters.get(i++ % availableDatacenters.size()); 133 | String datacenterName = CloudSim.getEntityName(datacenterId); 134 | 135 | if (!getVmsToDatacentersMap().containsKey(vm.getId())) { 136 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() + " in " + datacenterName); 137 | sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); 138 | numberOfVmsAllocated++; 139 | } 140 | } 141 | 142 | setVmsRequested(numberOfVmsAllocated); 143 | setVmsAcks(0); 144 | } 145 | } -------------------------------------------------------------------------------- /src/utils/Commons.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import PSO.PSO; 4 | import org.cloudbus.cloudsim.*; 5 | import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 6 | import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 7 | import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 8 | 9 | import java.text.DecimalFormat; 10 | import java.util.ArrayList; 11 | import java.util.Calendar; 12 | import java.util.LinkedList; 13 | import java.util.List; 14 | 15 | public class Commons { 16 | public static List cloudletList; 17 | public static List vmList; 18 | public static Datacenter[] datacenter; 19 | public static double[][] lengthMatrix; 20 | public static double[] mapping; 21 | public static int num_user; 22 | public static Calendar calendar; 23 | public static boolean trace_flag; 24 | 25 | 26 | public static void set_cloudsim_parameters(){ 27 | num_user = 1; 28 | calendar = Calendar.getInstance(); 29 | trace_flag = false; 30 | } 31 | 32 | public static Datacenter createDatacenter(String name) { 33 | List hostList = new ArrayList<>(); 34 | List peList = new ArrayList<>(); 35 | int mips = Constants.HOST_MIPS; 36 | peList.add(new Pe(0, new PeProvisionerSimple(mips))); 37 | int hostId = 0; 38 | int ram = Constants.HOST_RAM; 39 | long storage = Constants.STORAGE; 40 | int bw = Constants.HOST_BANDWIDTH; 41 | hostList.add(new Host(hostId, new RamProvisionerSimple(ram), 42 | new BwProvisionerSimple(bw), storage, 43 | peList, new VmSchedulerTimeShared(peList))); 44 | String arch = Constants.ARCHITECTURE; String os = Constants.OS; 45 | String vmm = Constants.VMM_NAME; double time_zone = Constants.TIME_ZONE; 46 | double cost = Constants.COST_PROCESSING; double costPerMem = Constants.COST_MEMORY; 47 | double costPerStorage = Constants.COST_STORAGE; double costPerBw = Constants.COST_BANDWIDTH; 48 | LinkedList storageList = new LinkedList<>(); 49 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 50 | arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw); 51 | Datacenter datacenter = null; 52 | try { 53 | datacenter = new Datacenter(name, characteristics, 54 | new VmAllocationPolicySimple(hostList), storageList, 0); 55 | } catch (Exception e) { 56 | e.printStackTrace(); 57 | } 58 | return datacenter; 59 | } 60 | public static void createDatacenters() { 61 | datacenter = new Datacenter[Constants.NO_OF_DATACENTERS]; 62 | for (int i = 0; i < Constants.NO_OF_DATACENTERS; i++) { 63 | datacenter[i] = createDatacenter("Datacenter#" + (i+2)); 64 | } 65 | } 66 | 67 | private static List createVMs(int userId, int vms) { 68 | LinkedList list = new LinkedList<>(); 69 | 70 | Vm[] vm = new Vm[Constants.NO_OF_VMS]; 71 | 72 | for (int i = 0; i < vms; i++) { 73 | vm[i] = new Vm(i, userId, Constants.VM_MIPS, Constants.VM_PES, Constants.VM_RAM, 74 | Constants.VM_BANDWIDTH, Constants.VM_IMAGE_SIZE, 75 | Constants.VMM_NAME, new CloudletSchedulerSpaceShared()); 76 | list.add(vm[i]); 77 | } 78 | 79 | return list; 80 | } 81 | 82 | private static List createCloudlets(int userId, int cloudlets, int choice) { 83 | LinkedList list = new LinkedList<>(); 84 | 85 | UtilizationModel umf = new UtilizationModelFull(); 86 | 87 | Cloudlet[] cloudlet = new Cloudlet[cloudlets]; 88 | 89 | for (int i = 0; i < cloudlets; i++) { 90 | int dcId; 91 | if(choice == 4) 92 | dcId = (int) (mapping[i]); 93 | else 94 | dcId = (int) (Math.random() * Constants.NO_OF_DATACENTERS); 95 | long length = (long) (1e3 * lengthMatrix[i][dcId]); 96 | 97 | cloudlet[i] = new Cloudlet(i, length, Constants.TASK_PES, Constants.FILE_SIZE, 98 | Constants.OUTPUT_SIZE, umf, umf, umf); 99 | cloudlet[i].setUserId(userId); 100 | cloudlet[i].setVmId(dcId + 2); 101 | list.add(cloudlet[i]); 102 | } 103 | return list; 104 | } 105 | 106 | public static void create_vms_and_cloudlets(int brokerId, int choice) { 107 | vmList = createVMs(brokerId, Constants.NO_OF_DATACENTERS); 108 | cloudletList = createCloudlets(brokerId, Constants.NO_OF_TASKS, choice); 109 | } 110 | 111 | public static double printCloudletList(List list, int choice, PSO PSOSchedulerInstance) { 112 | Cloudlet cloudlet; 113 | Log.printLine(); 114 | Log.printLine("========== OUTPUT =========="); 115 | Log.printLine("Cloudlet ID\tSTATUS\t" + 116 | "Datacenter ID\t" + 117 | "VM ID\t" + 118 | "Time\t" + 119 | "Start Time\t" + 120 | "Finish Time"); 121 | 122 | double finishTime = 0; 123 | DecimalFormat dft = new DecimalFormat("###.##"); 124 | dft.setMinimumIntegerDigits(2); 125 | for (Cloudlet value : list) { 126 | cloudlet = value; 127 | Log.print('\t' + dft.format(cloudlet.getCloudletId()) + '\t' + '\t'); 128 | 129 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) { 130 | Log.print("SUCCESS"); 131 | 132 | Log.printLine("\t\t" + dft.format(cloudlet.getResourceId()) + 133 | "\t\t\t" + dft.format(cloudlet.getVmId()) + 134 | "\t\t" + dft.format(cloudlet.getActualCPUTime()) + 135 | "\t\t" + dft.format(cloudlet.getExecStartTime()) + 136 | "\t\t" + dft.format(cloudlet.getFinishTime())); 137 | } 138 | finishTime = Math.max(finishTime, cloudlet.getFinishTime()); 139 | } 140 | // if(choice==4) { 141 | // PSOSchedulerInstance.printBestFitness(); 142 | // } 143 | //TODO: makespan 144 | return finishTime; 145 | } 146 | 147 | } 148 | --------------------------------------------------------------------------------