Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed CO423_Project_2K18_IT_072_073.pdf
Binary file not shown.
30 changes: 30 additions & 0 deletions LengthMatrix copy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
288.69 288.69 288.69 288.69 288.69
389.36 389.36 389.36 389.36 389.36
97.92 97.92 97.92 97.92 97.92
179.37 179.37 179.37 179.37 179.37
197.86 197.86 197.86 197.86 197.86
219.15 219.15 219.15 219.15 219.15
322 322 322 322 322
198 198 198 198 198
456.36 456.36 456.36 456.36 456.36
169.69 169.69 169.69 169.69 169.69
62.44 62.44 62.44 62.44 62.44
111.41 111.41 111.41 111.41 111.41
311.62 311.62 311.62 311.62 311.62
459.59 459.59 459.59 459.59 459.59
471.76 471.76 471.76 471.76 471.76
280.88 280.88 280.88 280.88 280.88
590.20 590.20 590.20 590.20 590.20
62.44 62.44 62.44 62.44 62.44
111.41 111.41 111.41 111.41 111.41
717.92 717.92 717.92 717.92 717.92
200.66 200.66 200.66 200.66 200.66
459.59 459.59 459.59 459.59 459.59
102.83 102.83 102.83 102.83 102.83
533.08 533.08 533.08 533.08 533.08
671.33 671.33 671.33 671.33 671.33
500.66 500.66 500.66 500.66 500.66
136.11 136.11 136.11 136.11 136.11
441.94 441.94 441.94 441.94 441.94
168.68 168.68 168.68 168.68 168.68
505.93 505.93 505.93 505.93 505.93
10 changes: 10 additions & 0 deletions LengthMatrixHetero.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
53.26 667.83 125.88 712.83 388.35
81.92 269.33 109.71 328.33 155.07
719.17 547.04 373.60 442.52 578.01
408.90 231.28 300.43 305.72 726.50
374.48 140.67 427.59 100.29 184.31
280.68 505.28 611.43 443.82 355.33
507.53 655.56 481.48 651.37 719.06
135.66 611.43 414.17 499.46 401.28
289.66 263.36 48.87 419.14 698.83
137.55 254.64 504.53 290.09 168.43
Binary file added jars/cloudsim-4.0.jar
Binary file not shown.
98 changes: 97 additions & 1 deletion src/FCFS/FCFS_Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import utils.*;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class FCFS_Scheduler {
private static double[][] lengthMatrix;

public static double main(String[] args) {
public static double main(String[] args) {
double finishtime = 0.0;
Log.printLine("Starting FCFS Scheduler...");

Expand All @@ -31,6 +37,9 @@ public static double main(String[] args) {
List<Cloudlet> newList = broker.getCloudletReceivedList();
finishtime = Commons.printCloudletList(newList, 1, null);

/***AddED EXTRA */
printCloudletList(newList);

Log.printLine("FCFS Scheduler finished!");
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -42,4 +51,91 @@ public static double main(String[] args) {
private static FCFS_DatacenterBroker createBroker(String name) throws Exception {
return new FCFS_DatacenterBroker(name);
}

/******** ADDED EXTRA PART */
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;

String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("VM ID" + indent + "Load Quality"+ indent + "Time");

DecimalFormat dft = new DecimalFormat("###.##");
dft.setMinimumIntegerDigits(2);

lengthMatrix = GenerateLengthMatrix.getlengthMatrix();

// for (int i = 0; i < size; i++) {
// cloudlet = list.get(i);
// int taskId = i; // Assuming task ID starts from 0
// int dataCenterId = cloudlet.getVmId() % Constants.NO_OF_DATACENTERS;
// double length = lengthMatrix[taskId][dataCenterId];

// Log.printLine(indent + dft.format(taskId) + indent + indent + dft.format(dataCenterId) + indent + indent + dft.format(length));
// }

// CODE TO CHECK UNDERLOADED/OVERLOADED SYSTEM
double tot_summation_time=0;
for(int i=0;i<Constants.NO_OF_TASKS;i++)
{
tot_summation_time+= lengthMatrix[i][0];
}

double avg_sum_time=tot_summation_time/Constants.NO_OF_DATACENTERS;
// System.out.println("avg_sum_time "+ avg_sum_time);

double a = (avg_sum_time*25)/100;
double b = (avg_sum_time*60)/100;
double x = avg_sum_time-a;
double y = avg_sum_time+b;
// System.out.printf(" \n Below are a, b, x, y ");
// System.out.println(a + " "+ b + " " + x + " " + y);
Map<Integer, Double> totalLengths = new HashMap<>();
// First loop to calculate total lengths
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
int taskId = i; // Assuming task ID starts from 0
int dataCenterId = cloudlet.getVmId() % Constants.NO_OF_DATACENTERS;
double length = lengthMatrix[taskId][dataCenterId];

// Update total length for the datacenter
totalLengths.put(dataCenterId, totalLengths.getOrDefault(dataCenterId, 0.0) + length);
}
// Second loop to print load quality using total lengths
for (Map.Entry<Integer, Double> entry : totalLengths.entrySet()) {
int dataCenterId = entry.getKey();
double totalLengthForDatacenter = entry.getValue();
String loadAns = ((totalLengthForDatacenter > y) ? "Overloaded" : (totalLengthForDatacenter < x) ? "Underloaded" : "Ok");
System.out.println(dataCenterId + indent + loadAns + indent + totalLengthForDatacenter);

}

System.out.println("avg_sum_time "+ avg_sum_time);
System.out.printf(" \n Below are a, b, x, y ");
System.out.println(a + " "+ b + " " + x + " " + y);
// CODE FOR LOAD ENDS HERE


double makespan = calcMakespan(list);
Log.printLine("Makespan using FCFS: " + makespan);
}

private static double calcMakespan(List<Cloudlet> list) {
lengthMatrix = GenerateLengthMatrix.getlengthMatrix();
double makespan = 0;
double[] dcWorkingTime = new double[Constants.NO_OF_DATACENTERS];

for (int i = 0; i < list.size(); i++) {
int dcId = list.get(i).getVmId() % Constants.NO_OF_DATACENTERS;
if (dcWorkingTime[dcId] != 0) --dcWorkingTime[dcId];
dcWorkingTime[dcId] += lengthMatrix[i][dcId];
makespan = Math.max(makespan, dcWorkingTime[dcId]);
}
return makespan;
}
/**** */

}

93 changes: 88 additions & 5 deletions src/PSO/PSO.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package PSO;

import net.sourceforge.jswarm_pso.Swarm;
import net.sourceforge.jswarm_pso.Particle;
import utils.GenerateLengthMatrix;
import utils.Constants;

public class PSO {
private static Swarm swarm;
private static PSO_Particle[] particles;
private static final PSO_FitnessFunction ff = new PSO_FitnessFunction();
private static double[][] lengthMatrix;
private static final PSO_Particle pp = new PSO_Particle();


public PSO() {
initParticles();
}


public double[] run() {
swarm = new Swarm(Constants.POPULATION_SIZE, new PSO_Particle(), ff);

Expand All @@ -22,18 +26,24 @@ public double[] run() {
swarm.setParticles(particles);
swarm.setParticleUpdate(new PSO_ParticleUpdate(new PSO_Particle()));

for (int i = 0; i < 500; i++) {
for (int i = 0; i < 20; i++) {
swarm.evolve();
if (i % 10 == 0) {
// if (i % 10 == 0) {
System.out.printf("Global best at iteration (%d): %f\n", i, swarm.getBestFitness());
}
System.out.printf("Makespan at iteration (%d): %f\n" , i , ff.calcMakespan(swarm.getBestParticle().getBestPosition()));
printTaskAllocation(swarm.getBestParticle().getBestPosition());
// printTaskAllocation( (PSO_Particle) swarm.getBestParticle());
// System.out.println(pp.toString());

// }
}

System.out.println("\nThe best fitness value: " + swarm.getBestFitness() + "\nBest makespan: " + ff.calcMakespan(swarm.getBestParticle().getBestPosition()));

System.out.println("The best solution is: ");
PSO_Particle bestParticle = (PSO_Particle) swarm.getBestParticle();
System.out.println(bestParticle.toString());
System.out.println(bestParticle.toString() + "mkc");
// printTaskAllocation(swarm.getBestParticle().getBestPosition());

return swarm.getBestPosition();
}
Expand All @@ -48,4 +58,77 @@ public void printBestFitness() {
System.out.println("\nBest fitness value: " + swarm.getBestFitness()+
"\nBest makespan: " + ff.calcMakespan(swarm.getBestParticle().getBestPosition()));
}


private void printTaskAllocation(double[] position) {
System.out.println("Task Allocation:");
for (int i = 0; i < Constants.NO_OF_DATACENTERS; i++) {
System.out.printf("Data Center %d: ", i);
int totalTaskSum = 0;
for (int j = 0; j < Constants.NO_OF_TASKS; j++) {
if ((int) position[j] == i) {
System.out.printf("%d ", j);
totalTaskSum += j; // Add the task number to the total sum
}
}
double totalLengthSum = calculateTotalLengthSum(position, i);

// Machine State
double Total_Summation_Time = 0.0;
lengthMatrix = GenerateLengthMatrix.getlengthMatrix();
for(int k=0; k< lengthMatrix.length; k++){
Total_Summation_Time += lengthMatrix[k][0];

}

double Average_Summation_Time = Total_Summation_Time/Constants.NO_OF_VMS;
System.out.println(Average_Summation_Time);
double a = (Average_Summation_Time*25)/100;
double b = (Average_Summation_Time*60)/100;
double x = Average_Summation_Time-a;
double y = Average_Summation_Time+b;
System.out.println(a + " "+ b + " " + x + " " + y);
System.out.printf("(Task Sum: %d, Length Sum: %.2f)\n", totalTaskSum, totalLengthSum);
System.out.println("The tl is " + totalLengthSum);
if(totalLengthSum < (Average_Summation_Time-a)) System.out.println("UnderLoaded");
else if(totalLengthSum > (Average_Summation_Time+b)) System.out.println("Overloaded");
System.out.println("=================================================================");
}
}

private double calculateTotalLengthSum(double[] position, int dataCenter) {
lengthMatrix = GenerateLengthMatrix.getlengthMatrix();
double totalLengthSum = 0;
for (int j = 0; j < Constants.NO_OF_TASKS; j++) {
if ((int) position[j] == dataCenter) {
totalLengthSum += lengthMatrix[j][dataCenter];
}
}
return totalLengthSum;
}

// private void printTaskAllocation(PSO_Particle particle) {
// System.out.println("Task Allocation:");
// System.out.println(particle.toString());
// }


// @Override
// public String toString() {
// String output = "";
// for (int i = 0; i < Constants.NO_OF_DATACENTERS; i++) {
// String tasks = "";
// int no_of_tasks = 0;
// for (int j = 0; j < Constants.NO_OF_TASKS; j++) {
// if (i == (int) swarm.getBestParticle().getPosition()[j]) {
// tasks += (tasks.isEmpty() ? "" : " ") + j;
// ++no_of_tasks;
// }
// }
// if (tasks.isEmpty()) output += "There is no tasks associated to Data Center " + i + "\n";
// else
// output += "There are " + no_of_tasks + " tasks associated to Data Center " + i + " and they are " + tasks + "\n";
// }
// return output;
// }
}
11 changes: 10 additions & 1 deletion src/PSO/PSO_DatacenterBroker.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.cloudbus.cloudsim.core.SimEvent;
import org.cloudbus.cloudsim.lists.VmList;

import utils.Constants;

import java.util.Arrays;
import java.util.List;

public class PSO_DatacenterBroker extends DatacenterBroker {
Expand All @@ -24,8 +27,14 @@ public void setMapping(double[] mapping) {
private List<Cloudlet> assignCloudletsToVms(List<Cloudlet> cloudlist) {
int idx = 0;
for (Cloudlet cl : cloudlist) {
cl.setVmId((int) mapping[idx++]);
cl.setVmId((int) mapping[(idx++)]);
System.out.println("VM id to cloudlet is " + cl.getCloudletId() + " " + cl.getVmId());
}

// Debug prints
System.out.println("Mapping array size: " + mapping.length);
System.out.println("Mapping array values: " + Arrays.toString(mapping));

return cloudlist;
}

Expand Down
3 changes: 2 additions & 1 deletion src/PSO/PSO_FitnessFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PSO_FitnessFunction extends FitnessFunction {
@Override
public double evaluate(double[] position) {
double alpha = 0.3;
return alpha * calcTotalTime(position) + (1 - alpha) * calcMakespan(position);
return calcMakespan(position);
}

private double calcTotalTime(double[] position) {
Expand All @@ -24,6 +24,7 @@ private double calcTotalTime(double[] position) {
int dcId = (int) position[i];
totalCost += lengthMatrix[i][dcId];
}
// System.out.println("totalCost "+ totalCost);
return totalCost;
}

Expand Down
7 changes: 6 additions & 1 deletion src/PSO/PSO_Particle.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ public class PSO_Particle extends Particle {

for (int i = 0; i < Constants.NO_OF_TASKS; i++) {
Random randObj = new Random();
position[i] = randObj.nextInt(Constants.NO_OF_DATACENTERS);
position[i] = randObj.nextInt(Constants.NO_OF_DATACENTERS); // This line assigns a random position to the current task.
// The randObj.nextInt(Constants.NO_OF_DATACENTERS) method generates a random integer between
// 0 and Constants.NO_OF_DATACENTERS (exclusive). This random number is then stored
// in the position[i] array element.
velocity[i] = Math.random();
// This line assigns a random velocity to the current task. The Math.random() method generates a random double value between 0.0 and 1.0 (inclusive).
// This random number is then stored in the velocity[i] array element.
}
setPosition(position);
setVelocity(velocity);
Expand Down
Loading