├── Chromosomes.java ├── CloudSimExample6.java ├── Constant.java ├── DatacenterBroker.java ├── Gene.java ├── GeneticAlgorithm.java ├── Individual.java ├── Main.java ├── PSO.java ├── Particle.java ├── Population.java ├── PsoScheduling.java ├── README.md ├── Scheduler.java ├── SchedulerParticle.java ├── SchedulerParticleUpdate.java ├── Swarm.java ├── Vector.java ├── cloudlet3.java └── vm3.java /Chromosomes.java: -------------------------------------------------------------------------------- 1 | 2 | package org.cloudbus.cloudsim.HSGA; 3 | 4 | import java.util.List; 5 | import java.util.ArrayList; 6 | import org.apache.commons.math3.genetics.AbstractListChromosome; 7 | import org.apache.commons.math3.genetics.InvalidRepresentationException; 8 | 9 | public class Chromosomes{ 10 | 11 | protected ArrayList geneList; 12 | 13 | public Chromosomes(ArrayList geneList){ 14 | this.geneList=geneList; 15 | } 16 | 17 | public ArrayList getGeneList(){ 18 | return this.geneList; 19 | } 20 | 21 | public void updateGene(int index,Vm vm){ 22 | Gene gene=this.geneList.get(index); 23 | gene.setVmForGene(vm); 24 | this.geneList.set(index, gene); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /CloudSimExample6.java: -------------------------------------------------------------------------------- 1 | 2 | package org.cloudbus.cloudsim.HSGA; 3 | 4 | import java.text.DecimalFormat; 5 | import java.util.ArrayList; 6 | import java.util.Random; 7 | import java.util.Calendar; 8 | import java.util.LinkedList; 9 | import java.util.List; 10 | 11 | import org.cloudbus.cloudsim.Chromosomes; 12 | import org.cloudbus.cloudsim.Cloudlet; 13 | import org.cloudbus.cloudsim.CloudletSchedulerSpaceShared; 14 | import org.cloudbus.cloudsim.Datacenter; 15 | import org.cloudbus.cloudsim.DatacenterBroker; 16 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 17 | import org.cloudbus.cloudsim.Gene; 18 | import org.cloudbus.cloudsim.Host; 19 | import org.cloudbus.cloudsim.Log; 20 | import org.cloudbus.cloudsim.Pe; 21 | import org.cloudbus.cloudsim.Storage; 22 | import org.cloudbus.cloudsim.UtilizationModel; 23 | import org.cloudbus.cloudsim.UtilizationModelFull; 24 | import org.cloudbus.cloudsim.Vm; 25 | import org.cloudbus.cloudsim.VmAllocationPolicySimple; 26 | import org.cloudbus.cloudsim.VmSchedulerTimeShared; 27 | import org.cloudbus.cloudsim.core.CloudSim; 28 | import org.cloudbus.cloudsim.core.CloudSimTags; 29 | import org.cloudbus.cloudsim.lists.VmList; 30 | import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 31 | import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 32 | import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 33 | 34 | public class CloudSimExample6 { 35 | 36 | private static List cloudletList; 37 | private static List vmlist; 38 | private static List finalcloudletList; 39 | private static List finalvmlist; 40 | 41 | private static List createVM(int userId, int vms) { 42 | 43 | LinkedList list = new LinkedList(); 44 | 45 | 46 | long size = 10000; // image size (MB) 47 | int ram = 512; // vm memory (MB) 48 | int mips = 500; 49 | long bw = 1000; 50 | int pesNumber = 1;// number of cpus 51 | String vmm = "Xen"; // VMM name 52 | Random rOb = new Random(); 53 | Vm[] vm = new Vm[vms]; 54 | 55 | for (int i = 0; i < vms; i++) { 56 | vm[i] = new Vm(i, userId, mips + rOb.nextInt(500), pesNumber, ram, 57 | bw, size, vmm, 58 | new CloudletSchedulerSpaceShared()); 59 | 60 | list.add(vm[i]); 61 | } 62 | return list; 63 | } 64 | 65 | private static List createCloudlet(int userId, int cloudlets) { 66 | LinkedList list = new LinkedList(); 67 | 68 | // cloudlet parameters 69 | long length = 1000; 70 | long fileSize = 1000; 71 | long outputSize = 1000; 72 | int pesNumber = 1; 73 | UtilizationModel utilizationModel = new UtilizationModelFull(); 74 | 75 | Cloudlet[] cloudlet = new Cloudlet[cloudlets]; 76 | Random randomObj = new Random(); 77 | 78 | for (int i = 0; i < cloudlets; i++) { 79 | int x = (int) (Math.random() * ((2000 - 1) + 1)) + 1; 80 | cloudlet[i] = new Cloudlet(i, (length + x), pesNumber, fileSize, 81 | outputSize, utilizationModel, utilizationModel, 82 | utilizationModel); 83 | cloudlet[i].setUserId(userId); 84 | list.add(cloudlet[i]); 85 | } 86 | 87 | return list; 88 | } 89 | 90 | // //////////////////////// STATIC METHODS /////////////////////// 91 | 92 | public static void main(String[] args) { 93 | Log.printLine("Starting CloudSimExample6..."); 94 | 95 | try { 96 | int num_user = 2; // number of grid users 97 | Calendar calendar = Calendar.getInstance(); 98 | boolean trace_flag = false; // mean trace events 99 | CloudSim.init(num_user, calendar, trace_flag); 100 | 101 | @SuppressWarnings("unused") 102 | Datacenter datacenter0 = createDatacenter("Datacenter_0"); 103 | @SuppressWarnings("unused") 104 | 105 | DatacenterBroker broker = createBroker(); 106 | int brokerId = broker.getId(); 107 | 108 | vmlist = createVM(brokerId, 5); // creating 5 vms 109 | cloudletList = createCloudlet(brokerId, 200); // creating 200 cloudlets 110 | 111 | List sortedList = new ArrayList(); 112 | for(Cloudlet cloudlet:cloudletList){ 113 | sortedList.add(cloudlet); 114 | } 115 | int numCloudlets=sortedList.size(); 116 | for(int i=0;i sortedListVm = new ArrayList(); 133 | ArrayList toBeUsedVm = new ArrayList(); 134 | ArrayList leftOutVm = new ArrayList(); 135 | for(Vm vm:vmlist){ 136 | sortedListVm.add(vm); 137 | } 138 | int numVms=sortedListVm.size(); 139 | 140 | for(int i=0;itmp.getMips()) 150 | { 151 | idx=j; 152 | tmp=sortedListVm.get(j); 153 | } 154 | } 155 | Vm tmp2 = sortedListVm.get(i); 156 | sortedListVm.set(i, tmp); 157 | sortedListVm.set(idx,tmp2); 158 | } 159 | ArrayList initialPopulation = new ArrayList(); 160 | for(int j=0;j firstChromosome = new ArrayList(); 163 | 164 | for(int i=0;i l1= new ArrayList(); 184 | l1=initialPopulation.get(index1).getGeneList(); 185 | Chromosomes chromosome1 = new Chromosomes(l1); 186 | ArrayList l2= new ArrayList(); 187 | l2=initialPopulation.get(index2).getGeneList(); 188 | Chromosomes chromosome2 = new Chromosomes(l2); 189 | double rangeMin = 0.0f; 190 | double rangeMax = 1.0f; 191 | Random r = new Random(); 192 | double crossProb = rangeMin + (rangeMax - rangeMin) * r.nextDouble(); 193 | if(crossProb<0.5) 194 | { 195 | int i,j; 196 | i=random.nextInt(numCloudlets) % numCloudlets; 197 | j=random.nextInt(numCloudlets) % numCloudlets; 198 | Vm vm1 = l1.get(i).getVmFromGene(); 199 | Vm vm2 = l2.get(j).getVmFromGene(); 200 | chromosome1.updateGene(i, vm2); 201 | chromosome2.updateGene(j, vm1); 202 | initialPopulation.set(index1, chromosome1); 203 | initialPopulation.set(index2, chromosome2); 204 | } 205 | double mutProb = rangeMin + (rangeMax - rangeMin) * r.nextDouble(); 206 | if(mutProb<0.5) 207 | { 208 | int i; 209 | i=random.nextInt(populationSize) % populationSize; 210 | ArrayList l= new ArrayList(); 211 | l=initialPopulation.get(i).getGeneList(); 212 | Chromosomes mutchromosome = new Chromosomes(l); 213 | int j; 214 | j=random.nextInt(numCloudlets) % numCloudlets; 215 | Vm vm1 = sortedListVm.get(0); 216 | mutchromosome.updateGene(j,vm1); 217 | } 218 | } 219 | int fittestIndex=0; 220 | double time=1000000; 221 | 222 | for(int i=0;i l= new ArrayList(); 225 | l=initialPopulation.get(i).getGeneList(); 226 | double sum=0; 227 | for(int j=0;j result = new ArrayList(); 243 | result = initialPopulation.get(fittestIndex).getGeneList(); 244 | 245 | List finalcloudletList = new ArrayList(); 246 | List finalvmlist = new ArrayList(); 247 | 248 | 249 | 250 | 251 | for(int i=0;i newList = broker.getCloudletReceivedList(); 264 | 265 | CloudSim.stopSimulation(); 266 | 267 | printCloudletList(newList); 268 | 269 | Log.printLine("CloudSimExample6 finished!"); 270 | } catch (Exception e) { 271 | e.printStackTrace(); 272 | Log.printLine("The simulation has been terminated due to an unexpected error"); 273 | } 274 | } 275 | 276 | private static Datacenter createDatacenter(String name) { 277 | 278 | List hostList = new ArrayList(); 279 | 280 | List peList = new ArrayList(); 281 | 282 | int mips = 5000; 283 | peList.add(new Pe(0, new PeProvisionerSimple(mips))); 284 | 285 | mips = 2500; 286 | peList.add(new Pe(1, new PeProvisionerSimple(mips))); 287 | 288 | mips = 2500; 289 | peList.add(new Pe(2, new PeProvisionerSimple(mips))); 290 | 291 | mips = 1500; 292 | peList.add(new Pe(3, new PeProvisionerSimple(mips))); 293 | 294 | mips = 1000; 295 | peList.add(new Pe(4, new PeProvisionerSimple(mips))); 296 | 297 | int hostId = 0; 298 | int ram = 4096; // host memory (MB) 299 | long storage = 10000000; // host storage 300 | int bw = 10000; 301 | 302 | hostList.add(new Host(hostId, new RamProvisionerSimple(ram), 303 | new BwProvisionerSimple(bw), storage, peList, 304 | new VmSchedulerTimeShared(peList))); 305 | String arch = "x86"; // system architecture 306 | String os = "Linux"; // operating system 307 | String vmm = "Xen"; 308 | double time_zone = 5.30; 309 | double cost = 3.0; 310 | double costPerMem = 0.05; 311 | double costPerStorage = 0.001; 312 | 313 | double costPerBw = 0.001; 314 | LinkedList storageList = new LinkedList(); 315 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 316 | arch, os, vmm, hostList, time_zone, cost, costPerMem, 317 | costPerStorage, costPerBw); 318 | 319 | Datacenter datacenter = null; 320 | try { 321 | datacenter = new Datacenter(name, characteristics, 322 | new VmAllocationPolicySimple(hostList), storageList, 0); 323 | } catch (Exception e) { 324 | e.printStackTrace(); 325 | } 326 | 327 | return datacenter; 328 | } 329 | 330 | private static DatacenterBroker createBroker() { 331 | 332 | DatacenterBroker broker = null; 333 | try { 334 | broker = new DatacenterBroker("Broker"); 335 | } catch (Exception e) { 336 | e.printStackTrace(); 337 | return null; 338 | } 339 | return broker; 340 | } 341 | 342 | private static void printCloudletList(List list) { 343 | int size = list.size(); 344 | Cloudlet cloudlet; 345 | double value1=0; 346 | 347 | String indent = " "; 348 | Log.printLine(); 349 | Log.printLine("========== OUTPUT =========="); 350 | Log.printLine("Cloudlet ID" + indent + "STATUS" + indent 351 | + "Data center ID" + indent + "VM ID" + indent + indent 352 | + "Time" + indent + "Start Time" + indent + "Finish Time"); 353 | 354 | DecimalFormat dft = new DecimalFormat("###.##"); 355 | for (int i = 0; i < size; i++) { 356 | cloudlet = list.get(i); 357 | Log.print(indent + cloudlet.getCloudletId() + indent + indent); 358 | 359 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) { 360 | Log.print("SUCCESS"); 361 | 362 | Log.printLine(indent + indent + cloudlet.getResourceId() 363 | + indent + indent + indent + cloudlet.getVmId() 364 | + indent + indent + indent 365 | + dft.format(cloudlet.getActualCPUTime()) + indent 366 | + indent + dft.format(cloudlet.getExecStartTime()) 367 | + indent + indent + indent 368 | + dft.format(cloudlet.getFinishTime())); 369 | } 370 | value1= value1+Double.parseDouble(dft.format(cloudlet.getActualCPUTime())); 371 | } 372 | Log.printLine("================ Execution Result Ends here =================="); 373 | System.out.println("This (HSGA) schedule plan takes "+value1/10+" ms to finish execution."); 374 | 375 | } 376 | } 377 | -------------------------------------------------------------------------------- /Constant.java: -------------------------------------------------------------------------------- 1 | package Fitness_Function; 2 | 3 | public class Constant { 4 | 5 | 6 | public static final int NO_OF_TASKS = 1000 ; // number of Cloudlets; 7 | 8 | public static int NO_OF_DATA_CENTERS =1; // number of Datacenters; 9 | 10 | public static final int POPULATION_SIZE = 300; // Number of Particles. 11 | 12 | } 13 | -------------------------------------------------------------------------------- /DatacenterBroker.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.HSGA; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Random; 9 | 10 | import org.cloudbus.cloudsim.core.CloudSim; 11 | import org.cloudbus.cloudsim.core.CloudSimTags; 12 | import org.cloudbus.cloudsim.core.SimEntity; 13 | import org.cloudbus.cloudsim.core.SimEvent; 14 | import org.cloudbus.cloudsim.lists.CloudletList; 15 | import org.cloudbus.cloudsim.lists.VmList; 16 | 17 | import org.apache.commons.math3.genetics.Chromosome; 18 | import org.apache.commons.math3.genetics.ElitisticListPopulation; 19 | import org.apache.commons.math3.genetics.FixedElapsedTime; 20 | import org.apache.commons.math3.genetics.GeneticAlgorithm; 21 | import org.apache.commons.math3.genetics.Population; 22 | import org.apache.commons.math3.genetics.TournamentSelection; 23 | import org.apache.commons.math3.genetics.UniformCrossover; 24 | 25 | 26 | public class DatacenterBroker extends SimEntity { 27 | 28 | 29 | protected List vmList; 30 | 31 | 32 | protected List vmsCreatedList; 33 | 34 | 35 | protected List cloudletList; 36 | 37 | 38 | protected List cloudletSubmittedList; 39 | 40 | 41 | protected List cloudletReceivedList; 42 | 43 | 44 | protected int cloudletsSubmitted; 45 | 46 | 47 | protected int vmsRequested; 48 | 49 | 50 | protected int vmsAcks; 51 | 52 | 53 | protected int vmsDestroyed; 54 | 55 | 56 | protected List datacenterIdsList; 57 | 58 | 59 | protected List datacenterRequestedIdsList; 60 | 61 | 62 | protected Map vmsToDatacentersMap; 63 | 64 | 65 | protected Map datacenterCharacteristicsList; 66 | 67 | public DatacenterBroker(String name) throws Exception { 68 | super(name); 69 | 70 | setVmList(new ArrayList()); 71 | setVmsCreatedList(new ArrayList()); 72 | setCloudletList(new ArrayList()); 73 | setCloudletSubmittedList(new ArrayList()); 74 | setCloudletReceivedList(new ArrayList()); 75 | 76 | cloudletsSubmitted = 0; 77 | setVmsRequested(0); 78 | setVmsAcks(0); 79 | setVmsDestroyed(0); 80 | 81 | setDatacenterIdsList(new LinkedList()); 82 | setDatacenterRequestedIdsList(new ArrayList()); 83 | setVmsToDatacentersMap(new HashMap()); 84 | setDatacenterCharacteristicsList(new HashMap()); 85 | } 86 | 87 | public void submitVmList(List list) { 88 | getVmList().addAll(list); 89 | } 90 | 91 | public void submitCloudletList(List list) { 92 | getCloudletList().addAll(list); 93 | } 94 | 95 | public void bindCloudletToVm(int cloudletId, int vmId) { 96 | CloudletList.getById(getCloudletList(), cloudletId).setVmId(vmId); 97 | } 98 | 99 | @Override 100 | public void processEvent(SimEvent ev) { 101 | switch (ev.getTag()) { 102 | // Resource characteristics request 103 | case CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST: 104 | processResourceCharacteristicsRequest(ev); 105 | break; 106 | // Resource characteristics answer 107 | case CloudSimTags.RESOURCE_CHARACTERISTICS: 108 | processResourceCharacteristics(ev); 109 | break; 110 | // VM Creation answer 111 | case CloudSimTags.VM_CREATE_ACK: 112 | processVmCreate(ev); 113 | break; 114 | // A finished cloudlet returned 115 | case CloudSimTags.CLOUDLET_RETURN: 116 | processCloudletReturn(ev); 117 | break; 118 | // if the simulation finishes 119 | case CloudSimTags.END_OF_SIMULATION: 120 | shutdownEntity(); 121 | break; 122 | // other unknown tags are processed by this method 123 | default: 124 | processOtherEvent(ev); 125 | break; 126 | } 127 | } 128 | 129 | protected void processResourceCharacteristics(SimEvent ev) { 130 | DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData(); 131 | getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics); 132 | 133 | if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size()) { 134 | setDatacenterRequestedIdsList(new ArrayList()); 135 | createVmsInDatacenter(getDatacenterIdsList().get(0)); 136 | } 137 | } 138 | 139 | protected void processResourceCharacteristicsRequest(SimEvent ev) { 140 | setDatacenterIdsList(CloudSim.getCloudResourceList()); 141 | setDatacenterCharacteristicsList(new HashMap()); 142 | 143 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloud Resource List received with " 144 | + getDatacenterIdsList().size() + " resource(s)"); 145 | 146 | for (Integer datacenterId : getDatacenterIdsList()) { 147 | sendNow(datacenterId, CloudSimTags.RESOURCE_CHARACTERISTICS, getId()); 148 | } 149 | } 150 | 151 | protected void processVmCreate(SimEvent ev) { 152 | int[] data = (int[]) ev.getData(); 153 | int datacenterId = data[0]; 154 | int vmId = data[1]; 155 | int result = data[2]; 156 | 157 | if (result == CloudSimTags.TRUE) { 158 | getVmsToDatacentersMap().put(vmId, datacenterId); 159 | getVmsCreatedList().add(VmList.getById(getVmList(), vmId)); 160 | Log.printLine(CloudSim.clock() + ": " + getName() + ": VM #" + vmId 161 | + " has been created in Datacenter #" + datacenterId + ", Host #" 162 | + VmList.getById(getVmsCreatedList(), vmId).getHost().getId()); 163 | } else { 164 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Creation of VM #" + vmId 165 | + " failed in Datacenter #" + datacenterId); 166 | } 167 | 168 | incrementVmsAcks(); 169 | if (getVmsCreatedList().size() == getVmList().size() - getVmsDestroyed()) { 170 | submitCloudlets(); 171 | } else { 172 | 173 | if (getVmsRequested() == getVmsAcks()) { 174 | 175 | for (int nextDatacenterId : getDatacenterIdsList()) { 176 | if (!getDatacenterRequestedIdsList().contains(nextDatacenterId)) { 177 | createVmsInDatacenter(nextDatacenterId); 178 | return; 179 | } 180 | } 181 | 182 | if (getVmsCreatedList().size() > 0) { // if some vm were created 183 | submitCloudlets(); 184 | } else { // no vms created. abort 185 | Log.printLine(CloudSim.clock() + ": " + getName() 186 | + ": none of the required VMs could be created. Aborting"); 187 | finishExecution(); 188 | } 189 | } 190 | } 191 | } 192 | 193 | protected void processCloudletReturn(SimEvent ev) { 194 | Cloudlet cloudlet = (Cloudlet) ev.getData(); 195 | getCloudletReceivedList().add(cloudlet); 196 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloudlet " + cloudlet.getCloudletId() 197 | + " received"); 198 | cloudletsSubmitted--; 199 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { // all cloudlets executed 200 | Log.printLine(CloudSim.clock() + ": " + getName() + ": All Cloudlets executed. Finishing..."); 201 | clearDatacenters(); 202 | finishExecution(); 203 | } else { 204 | if (getCloudletList().size() > 0 && cloudletsSubmitted == 0) { 205 | clearDatacenters(); 206 | createVmsInDatacenter(0); 207 | } 208 | 209 | } 210 | } 211 | 212 | protected void processOtherEvent(SimEvent ev) { 213 | if (ev == null) { 214 | Log.printLine(getName() + ".processOtherEvent(): " + "Error - an event is null."); 215 | return; 216 | } 217 | 218 | Log.printLine(getName() + ".processOtherEvent(): " 219 | + "Error - event unknown by this DatacenterBroker."); 220 | } 221 | 222 | protected void createVmsInDatacenter(int datacenterId) { 223 | int requestedVms = 0; 224 | String datacenterName = CloudSim.getEntityName(datacenterId); 225 | for (Vm vm : getVmList()) { 226 | if (!getVmsToDatacentersMap().containsKey(vm.getId())) { 227 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() 228 | + " in " + datacenterName); 229 | sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); 230 | requestedVms++; 231 | } 232 | } 233 | 234 | getDatacenterRequestedIdsList().add(datacenterId); 235 | 236 | setVmsRequested(requestedVms); 237 | setVmsAcks(0); 238 | } 239 | 240 | protected void submitCloudlets() { 241 | 242 | int vmIndex = 0; 243 | for (Cloudlet cloudlet : getCloudletList()) { 244 | Vm vm; 245 | if (cloudlet.getVmId() == -1) { 246 | vm = getVmsCreatedList().get(vmIndex); 247 | } else { 248 | vm = VmList.getById(getVmsCreatedList(), cloudlet.getVmId()); 249 | if (vm == null) { // vm was not created 250 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Postponing execution of cloudlet " 251 | + cloudlet.getCloudletId() + ": bount VM not available"); 252 | continue; 253 | } 254 | } 255 | 256 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Sending cloudlet " 257 | + cloudlet.getCloudletId() + " to VM #" + vm.getId()); 258 | cloudlet.setVmId(vm.getId()); 259 | sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.CLOUDLET_SUBMIT, cloudlet); 260 | cloudletsSubmitted++; 261 | vmIndex = (vmIndex + 1) % getVmsCreatedList().size(); 262 | getCloudletSubmittedList().add(cloudlet); 263 | } 264 | for (Cloudlet cloudlet : getCloudletSubmittedList()) { 265 | getCloudletList().remove(cloudlet); 266 | } 267 | 268 | } 269 | 270 | protected void clearDatacenters() { 271 | for (Vm vm : getVmsCreatedList()) { 272 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Destroying VM #" + vm.getId()); 273 | sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.VM_DESTROY, vm); 274 | } 275 | 276 | getVmsCreatedList().clear(); 277 | } 278 | 279 | protected void finishExecution() { 280 | sendNow(getId(), CloudSimTags.END_OF_SIMULATION); 281 | } 282 | 283 | @Override 284 | public void shutdownEntity() { 285 | Log.printLine(getName() + " is shutting down..."); 286 | } 287 | 288 | @Override 289 | public void startEntity() { 290 | Log.printLine(getName() + " is starting..."); 291 | schedule(getId(), 0, CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST); 292 | } 293 | 294 | @SuppressWarnings("unchecked") 295 | public List getVmList() { 296 | return (List) vmList; 297 | } 298 | 299 | protected void setVmList(List vmList) { 300 | this.vmList = vmList; 301 | } 302 | 303 | @SuppressWarnings("unchecked") 304 | public List getCloudletList() { 305 | return (List) cloudletList; 306 | } 307 | 308 | protected void setCloudletList(List cloudletList) { 309 | this.cloudletList = cloudletList; 310 | } 311 | 312 | @SuppressWarnings("unchecked") 313 | public List getCloudletSubmittedList() { 314 | return (List) cloudletSubmittedList; 315 | } 316 | 317 | protected void setCloudletSubmittedList(List cloudletSubmittedList) { 318 | this.cloudletSubmittedList = cloudletSubmittedList; 319 | } 320 | 321 | @SuppressWarnings("unchecked") 322 | public List getCloudletReceivedList() { 323 | return (List) cloudletReceivedList; 324 | } 325 | 326 | protected void setCloudletReceivedList(List cloudletReceivedList) { 327 | this.cloudletReceivedList = cloudletReceivedList; 328 | } 329 | 330 | @SuppressWarnings("unchecked") 331 | public List getVmsCreatedList() { 332 | return (List) vmsCreatedList; 333 | } 334 | 335 | 336 | protected void setVmsCreatedList(List vmsCreatedList) { 337 | this.vmsCreatedList = vmsCreatedList; 338 | } 339 | 340 | 341 | protected int getVmsRequested() { 342 | return vmsRequested; 343 | } 344 | 345 | 346 | protected void setVmsRequested(int vmsRequested) { 347 | this.vmsRequested = vmsRequested; 348 | } 349 | 350 | 351 | protected int getVmsAcks() { 352 | return vmsAcks; 353 | } 354 | 355 | 356 | protected void setVmsAcks(int vmsAcks) { 357 | this.vmsAcks = vmsAcks; 358 | } 359 | 360 | 361 | protected void incrementVmsAcks() { 362 | vmsAcks++; 363 | } 364 | 365 | 366 | protected int getVmsDestroyed() { 367 | return vmsDestroyed; 368 | } 369 | 370 | 371 | protected void setVmsDestroyed(int vmsDestroyed) { 372 | this.vmsDestroyed = vmsDestroyed; 373 | } 374 | 375 | 376 | protected List getDatacenterIdsList() { 377 | return datacenterIdsList; 378 | } 379 | 380 | 381 | protected void setDatacenterIdsList(List datacenterIdsList) { 382 | this.datacenterIdsList = datacenterIdsList; 383 | } 384 | 385 | 386 | protected Map getVmsToDatacentersMap() { 387 | return vmsToDatacentersMap; 388 | } 389 | 390 | protected void setVmsToDatacentersMap(Map vmsToDatacentersMap) { 391 | this.vmsToDatacentersMap = vmsToDatacentersMap; 392 | } 393 | 394 | protected Map getDatacenterCharacteristicsList() { 395 | return datacenterCharacteristicsList; 396 | } 397 | 398 | protected void setDatacenterCharacteristicsList( 399 | Map datacenterCharacteristicsList) { 400 | this.datacenterCharacteristicsList = datacenterCharacteristicsList; 401 | } 402 | 403 | protected List getDatacenterRequestedIdsList() { 404 | return datacenterRequestedIdsList; 405 | } 406 | 407 | protected void setDatacenterRequestedIdsList(List datacenterRequestedIdsList) { 408 | this.datacenterRequestedIdsList = datacenterRequestedIdsList; 409 | } 410 | 411 | } 412 | 413 | -------------------------------------------------------------------------------- /Gene.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.HSGA; 2 | 3 | import org.cloudbus.cloudsim.core.SimEntity; 4 | 5 | public class Gene{ 6 | 7 | private Cloudlet task; 8 | private Vm vm; 9 | public Gene(Cloudlet cl, Vm v) 10 | { 11 | this.task=cl; 12 | this.vm=v; 13 | } 14 | public Cloudlet getCloudletFromGene() 15 | { 16 | return this.task; 17 | } 18 | public Vm getVmFromGene() 19 | { 20 | return this.vm; 21 | } 22 | public void setCloudletForGene(Cloudlet cl) 23 | { 24 | this.task=cl; 25 | } 26 | public void setVmForGene(Vm vm) 27 | { 28 | this.vm=vm; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /GeneticAlgorithm.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | import org.cloudbus.cloudsim.CloudletSchedulerTimeShared; 8 | import org.cloudbus.cloudsim.UtilizationModel; 9 | import org.cloudbus.cloudsim.UtilizationModelFull; 10 | 11 | public class GeneticAlgorithm { 12 | private int populationSize; 13 | public static List cloudletList; 14 | public static List vmlist; 15 | 16 | private double mutationRate; 17 | 18 | private double crossoverRate; 19 | 20 | private int elitismCount; 21 | 22 | public GeneticAlgorithm(int populationSize, double mutationRate, double crossoverRate, int elitismCount, List cloudletList, List vmlist) { 23 | this.populationSize = populationSize; 24 | this.mutationRate = mutationRate; 25 | this.crossoverRate = crossoverRate; 26 | this.elitismCount = elitismCount; 27 | this.cloudletList = cloudletList; 28 | this.vmlist = vmlist; 29 | } 30 | 31 | public Population initPopulation(int chromosomeLength) { 32 | Population population = new Population(this.populationSize, chromosomeLength); 33 | return population; 34 | } 35 | 36 | public double calcFitness(Individual individual) { 37 | 38 | double cost = 0.0; 39 | double computationCost = 0; 40 | for (int geneIndex = 0; geneIndex < individual.getChromosomeLength(); geneIndex++) { 41 | double[] executionCostArray = cloudletList.get(geneIndex).executioncost; 42 | computationCost += executionCostArray[individual.chromosome[geneIndex]]; 43 | } 44 | double communicationCost = 0; 45 | 46 | ArrayList edgesFrom = new ArrayList(); 47 | ArrayList edgesTo = new ArrayList(); 48 | 49 | edgesFrom.add(1); edgesTo.add(2); 50 | edgesFrom.add(1); edgesTo.add(3); 51 | edgesFrom.add(1); edgesTo.add(4); 52 | edgesFrom.add(2); edgesTo.add(5); 53 | edgesFrom.add(3); edgesTo.add(5); 54 | edgesFrom.add(4); edgesTo.add(5); 55 | 56 | for(int i=0; i< edgesFrom.size();i++) { 57 | int edgeFrom = edgesFrom.get(i), edgeTo = edgesTo.get(i); 58 | int processorI = individual.chromosome[edgeFrom - 1]; 59 | int processorJ = individual.chromosome[edgeTo - 1]; 60 | 61 | int dataSize = 10; 62 | double[] communicationCostArray = vmlist.get(processorI).comcost; 63 | communicationCost += dataSize * communicationCostArray[processorJ]; 64 | } 65 | cost = computationCost + communicationCost; 66 | 67 | double fitness = cost; 68 | individual.setFitness(fitness); 69 | 70 | return fitness; 71 | } 72 | 73 | public void evalPopulation(Population population) { 74 | double populationFitness=0; 75 | 76 | for (Individual individual : population.getIndividuals()) { 77 | 78 | double individualFitness = calcFitness(individual); 79 | individual.setFitness(individualFitness); 80 | populationFitness+=individualFitness; 81 | 82 | } 83 | population.setPopulationFitness(populationFitness); 84 | 85 | } 86 | 87 | public Individual selectParent(Population population) { 88 | Individual individuals[] = population.getIndividuals(); 89 | double populationFitness = population.getPopulationFitness(); 90 | double rouletteWheelPosition = Math.random() * populationFitness; 91 | 92 | double spinWheel = 0; 93 | for (Individual individual : individuals) { 94 | spinWheel += individual.getFitness(); 95 | if (spinWheel >= rouletteWheelPosition) { 96 | return individual; 97 | } 98 | } 99 | return individuals[population.size() - 1]; 100 | } 101 | 102 | 103 | public Population crossoverPopulation(Population population) { 104 | Population newPopulation = new Population(population.size()); 105 | 106 | for (int populationIndex = 0; populationIndex < population.size(); populationIndex++) { 107 | Individual parent1 = population.getFittest(populationIndex); 108 | 109 | if (this.crossoverRate > Math.random()&& populationIndex > this.elitismCount ) { 110 | 111 | Individual offspring = new Individual(parent1.getChromosomeLength()); 112 | Individual parent2 = selectParent(population); 113 | 114 | for (int geneIndex = 0; geneIndex < parent1.getChromosomeLength(); geneIndex++) { 115 | if (0.5 > Math.random()) { 116 | offspring.setGene(geneIndex, parent1.getGene(geneIndex)); 117 | } else { 118 | offspring.setGene(geneIndex, parent2.getGene(geneIndex)); 119 | } 120 | } 121 | 122 | newPopulation.setIndividual(populationIndex, offspring); 123 | } else { 124 | newPopulation.setIndividual(populationIndex, parent1); 125 | } 126 | } 127 | 128 | return newPopulation; 129 | } 130 | 131 | public Population mutatePopulation(Population population) { 132 | Population newPopulation = new Population(this.populationSize); 133 | 134 | for (int populationIndex = 0; populationIndex < population.size(); populationIndex++) { 135 | Individual individual = population.getFittest(populationIndex); 136 | for (int geneIndex = 0; geneIndex < individual.getChromosomeLength(); geneIndex++) { 137 | 138 | if (populationIndex > this.elitismCount) { 139 | 140 | if (this.mutationRate > Math.random()) { 141 | 142 | int newGene=0; 143 | if (individual.getGene(geneIndex) == 0) { 144 | double r=Math.random(); 145 | if(r>0.5) 146 | { 147 | newGene=1; 148 | } 149 | else 150 | newGene=2; 151 | } 152 | else if (individual.getGene(geneIndex) == 1) { 153 | double r=Math.random(); 154 | if(r>0.5) 155 | { 156 | newGene=2; 157 | } 158 | else 159 | newGene=0; 160 | } 161 | else if (individual.getGene(geneIndex) == 2) { 162 | double r=Math.random(); 163 | if(r>0.5) 164 | { 165 | newGene=0; 166 | } 167 | else 168 | newGene=1; 169 | } 170 | 171 | individual.setGene(geneIndex, newGene); 172 | } 173 | } 174 | } 175 | 176 | newPopulation.setIndividual(populationIndex, individual); 177 | } 178 | 179 | return newPopulation; 180 | } 181 | 182 | } 183 | -------------------------------------------------------------------------------- /Individual.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | 4 | public class Individual { 5 | public int[] chromosome; 6 | private double fitness = -1; 7 | 8 | public Individual(int[] chromosome) { 9 | 10 | this.chromosome = chromosome; 11 | } 12 | 13 | public Individual(int chromosomeLength) { 14 | 15 | this.chromosome = new int[chromosomeLength]; 16 | for (int gene = 0; gene < chromosomeLength; gene++) { 17 | double r = Math.random(); 18 | if(r < 0.3333) this.setGene(gene, 0); 19 | else if(r < 0.66) this.setGene(gene, 1); 20 | else this.setGene(gene, 2); 21 | } 22 | 23 | } 24 | 25 | public int[] getChromosome() { 26 | return this.chromosome; 27 | } 28 | 29 | public int getChromosomeLength() { 30 | return this.chromosome.length; 31 | } 32 | 33 | public void setGene(int offset, int gene) { 34 | this.chromosome[offset] = gene; 35 | } 36 | 37 | public int getGene(int offset) { 38 | return this.chromosome[offset]; 39 | } 40 | 41 | public void setFitness(double fitness) { 42 | this.fitness = fitness; 43 | } 44 | 45 | public double getFitness() { 46 | return this.fitness; 47 | } 48 | 49 | public String toString() { 50 | String output = ""; 51 | for (int gene = 0; gene < this.chromosome.length; gene++) { 52 | output += this.chromosome[gene]; 53 | } 54 | return output; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Main.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.text.DecimalFormat; 4 | import java.util.ArrayList; 5 | import java.util.Calendar; 6 | import java.util.LinkedList; 7 | import java.util.List; 8 | import java.util.Random; 9 | 10 | import org.cloudbus.cloudsim.Cloudlet; 11 | import org.cloudbus.cloudsim.CloudletSchedulerSpaceShared; 12 | import org.cloudbus.cloudsim.Datacenter; 13 | import org.cloudbus.cloudsim.DatacenterBroker; 14 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 15 | import org.cloudbus.cloudsim.Host; 16 | import org.cloudbus.cloudsim.Log; 17 | import org.cloudbus.cloudsim.Pe; 18 | import org.cloudbus.cloudsim.Storage; 19 | import org.cloudbus.cloudsim.UtilizationModel; 20 | import org.cloudbus.cloudsim.UtilizationModelFull; 21 | import org.cloudbus.cloudsim.Vm; 22 | import org.cloudbus.cloudsim.VmAllocationPolicySimple; 23 | import org.cloudbus.cloudsim.VmSchedulerTimeShared; 24 | import org.cloudbus.cloudsim.core.CloudSim; 25 | import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 26 | import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 27 | import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 28 | 29 | public class Main { 30 | 31 | 32 | public static List cloudletList; 33 | 34 | public static List vmlist; 35 | 36 | public static List createVM(int userId, int vms) { 37 | 38 | LinkedList list = new LinkedList(); 39 | 40 | 41 | long size = 10000; //image size (MB) 42 | int ram = 512; //vm memory (MB) 43 | int mips = 1000; 44 | long bw = 1000; 45 | int pesNumber = 12; //number of cpus 46 | String vmm = "Xen"; //VMM name 47 | 48 | vm3[] vm = new vm3[vms]; 49 | double[][] comcost= {{0.00,0.17,0.21},{0.17,0.00,0.22},{0.21,0.22,0.00},{0.22,0.00,0.23},{0.24,0.21,0.00}}; 50 | 51 | for(int i=0;i createCloudlet(int userId, int cloudlets){ 63 | LinkedList list = new LinkedList(); 64 | Random rand = new Random(); 65 | 66 | long length = 1000; 67 | long fileSize = 1000; 68 | long outputSize = 1000; 69 | int pesNumber = 12; 70 | UtilizationModel utilizationModel = new UtilizationModelFull(); 71 | 72 | cloudlet3[] cloudlet = new cloudlet3[cloudlets]; 73 | double[][] executioncost= new double[1000][3]; 74 | int[][] datasize= new int[1000][2]; 75 | for(int i=0;i<1000;i++) 76 | { 77 | for(int j=0;j<2;j++) 78 | { 79 | datasize[i][j]=rand.nextInt(20); 80 | System.out.println(datasize[i][j]); 81 | } 82 | } 83 | for(int i=0;i<1000;i++) 84 | { 85 | for(int j=0;j<3;j++) 86 | { 87 | executioncost[i][j]=rand.nextDouble(); 88 | } 89 | } 90 | 91 | for(int i=0;i " + fit.getFitness()); 147 | 148 | for(int j=0;j<5;j++) 149 | { 150 | broker.bindCloudletToVm(j, fit.chromosome[j]); 151 | } 152 | 153 | population = ga.crossoverPopulation(population); 154 | 155 | population = ga.mutatePopulation(population); 156 | 157 | ga.evalPopulation(population); 158 | iteration++; 159 | 160 | } 161 | System.out.println("Best solution of GA: " + population.getFittest(0).toString()); 162 | int[][] particles = new int[population.size()][5]; 163 | for(int ind=0;ind newList = broker.getCloudletReceivedList(); 183 | 184 | CloudSim.stopSimulation(); 185 | 186 | printCloudletList(newList); 187 | 188 | Log.printLine("GA-PSO finished!"); 189 | 190 | } 191 | catch (Exception e) 192 | { 193 | e.printStackTrace(); 194 | Log.printLine("The simulation has been terminated due to an unexpected error"); 195 | } 196 | } 197 | 198 | private static Datacenter createDatacenter(String name){ 199 | 200 | List hostList = new ArrayList(); 201 | /*List peList2 = new ArrayList(); 202 | 203 | peList2.add(new Pe(0, new PeProvisionerSimple(mips))); 204 | peList2.add(new Pe(1, new PeProvisionerSimple(mips)));*/ 205 | List peList = new ArrayList(); 206 | 207 | int mips = 5000; 208 | peList.add(new Pe(0, new PeProvisionerSimple(mips))); 209 | 210 | mips = 2500; 211 | peList.add(new Pe(1, new PeProvisionerSimple(mips))); 212 | 213 | mips = 2500; 214 | peList.add(new Pe(2, new PeProvisionerSimple(mips))); 215 | 216 | mips = 1500; 217 | peList.add(new Pe(3, new PeProvisionerSimple(mips))); 218 | 219 | mips = 1000; 220 | peList.add(new Pe(4, new PeProvisionerSimple(mips))); 221 | 222 | int hostId=0; 223 | int ram = 4096; //host memory (MB) 224 | long storage = 10000000; //host storage 225 | int bw = 10000; 226 | 227 | hostList.add( 228 | new Host( 229 | hostId, 230 | new RamProvisionerSimple(ram), 231 | new BwProvisionerSimple(bw), 232 | storage, 233 | peList, 234 | new VmSchedulerTimeShared(peList) 235 | ) 236 | ); // This is our first machine 237 | 238 | String arch = "x86"; // system architecture 239 | String os = "Linux"; // operating system 240 | String vmm = "Xen"; 241 | double time_zone = 5.30; // time zone this resource located 242 | double cost = 3.0; // the cost of using processing in this resource 243 | double costPerMem = 0.05; // the cost of using memory in this resource 244 | double costPerStorage=0.001; 245 | double costPerBw=0.001; // the cost of using bw in this resource 246 | LinkedList storageList = new LinkedList(); //we are not adding SAN devices by now 247 | 248 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 249 | arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw); 250 | 251 | Datacenter datacenter = null; 252 | try { 253 | datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0); 254 | } catch (Exception e) { 255 | e.printStackTrace(); 256 | } 257 | 258 | return datacenter; 259 | } 260 | 261 | private static DatacenterBroker createBroker(){ 262 | 263 | DatacenterBroker broker = null; 264 | try { 265 | broker = new DatacenterBroker("Broker"); 266 | } catch (Exception e) { 267 | e.printStackTrace(); 268 | return null; 269 | } 270 | return broker; 271 | } 272 | 273 | private static void printCloudletList(List list) { 274 | int size = list.size(); 275 | Cloudlet cloudlet; 276 | double value1=0; 277 | String indent = " "; 278 | Log.printLine(); 279 | Log.printLine("========== OUTPUT =========="); 280 | Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + 281 | "Data center ID" + indent + "VM ID" + indent + indent + "Time" + indent + "Start Time" + indent + "Finish Time"); 282 | 283 | DecimalFormat dft = new DecimalFormat("###.##"); 284 | for (int i = 0; i < size; i++) { 285 | cloudlet = list.get(i); 286 | Log.print(indent + cloudlet.getCloudletId() + indent + indent); 287 | 288 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){ 289 | Log.print("SUCCESS"); 290 | 291 | Log.printLine( indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId() + 292 | indent + indent + indent + dft.format(cloudlet.getActualCPUTime()) + 293 | indent + indent + dft.format(cloudlet.getExecStartTime())+ indent + indent + indent + dft.format(cloudlet.getFinishTime())); 294 | } 295 | value1= value1+Double.parseDouble(dft.format(cloudlet.getActualCPUTime())); 296 | } 297 | Log.printLine("================ Execution Result Ends here =================="); 298 | System.out.println("This (GA+PSO) schedule plan takes "+value1/10+" ms to finish execution."); 299 | } 300 | } 301 | -------------------------------------------------------------------------------- /PSO.java: -------------------------------------------------------------------------------- 1 | package Fitness_Function; 2 | 3 | import net.sourceforge.jswarm_pso.Swarm; 4 | 5 | import java.text.DecimalFormat; 6 | import java.util.List; 7 | 8 | import org.cloudbus.cloudsim.Cloudlet; 9 | import org.cloudbus.cloudsim.Log; 10 | 11 | import FitnessFunction.Scheduler; 12 | 13 | public class PSO { 14 | 15 | private static SchedulerParticle particles[]; 16 | 17 | 18 | public static Scheduler ff = new Scheduler(); 19 | 20 | private static Swarm swarm = new Swarm(Constant.POPULATION_SIZE, new SchedulerParticle(), ff); 21 | 22 | public PSO() 23 | { 24 | 25 | initParticles(); 26 | 27 | } 28 | 29 | public double[] run() { 30 | 31 | 32 | 33 | swarm.setMinPosition(0); 34 | 35 | swarm.setMaxPosition(Constant.NO_OF_DATA_CENTERS - 1); 36 | 37 | swarm.setMaxMinVelocity(0.5); 38 | 39 | swarm.setParticles(particles); 40 | 41 | swarm.setParticleUpdate(new SchedulerParticleUpdate(new SchedulerParticle())); 42 | 43 | 44 | 45 | for (int i = 0; i < 500; i++) { 46 | 47 | swarm.evolve(); 48 | 49 | if (i % 10 == 0) { 50 | 51 | System.out.printf("Gloabl best at iteration (%d): %f\n", i, swarm.getBestFitness()); 52 | 53 | } 54 | 55 | } 56 | 57 | 58 | 59 | System.out.println("\nThe best fitness value: " + swarm.getBestFitness() + " Best makespan: " + ff.calcMakespan(swarm.getBestParticle().getBestPosition())); 60 | 61 | 62 | 63 | System.out.println("The best solution is: "); 64 | 65 | SchedulerParticle bestParticle = (SchedulerParticle) swarm.getBestParticle(); 66 | 67 | System.out.println(bestParticle.toString()); 68 | 69 | 70 | 71 | return swarm.getBestPosition(); 72 | 73 | } 74 | 75 | 76 | 77 | public void printBestFitness() { 78 | 79 | System.out.println("\nThe best fitness value: " + swarm.getBestFitness() + " Best makespan: " + ff.calcMakespan(swarm.getBestParticle().getBestPosition())); 80 | 81 | } 82 | 83 | public double[][] getCommunTimeMatrix() { return ff.getCoumnTimeMatrix(); } 84 | 85 | public double[][] getExecTimeMatrix() { return ff.getExecTimeMatrix(); } 86 | 87 | private static void initParticles() { 88 | 89 | particles = new SchedulerParticle[Constant.POPULATION_SIZE]; 90 | 91 | for (int i = 0; i < Constant.POPULATION_SIZE; ++i) 92 | 93 | particles[i] = new SchedulerParticle(); 94 | 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Particle.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | class Particle { 7 | 8 | public Vector position; 9 | public Vector velocity; 10 | public Vector bestPosition; 11 | public double bestEval; 12 | 13 | 14 | 15 | Particle (int a,int b,int c,int d,int e) { 16 | 17 | position = new Vector(a,b,c,d,e); 18 | velocity = new Vector(); 19 | 20 | bestPosition = position.clone(); 21 | bestEval = eval(); 22 | } 23 | 24 | private double eval () { 25 | 26 | double cost = 0.0; 27 | double computationCost = 0; 28 | double[] executionCostArray = Swarm.cloudletList.get(0).executioncost; 29 | computationCost += executionCostArray[position.a]; 30 | executionCostArray = Swarm.cloudletList.get(1).executioncost; 31 | computationCost += executionCostArray[position.b]; 32 | executionCostArray = Swarm.cloudletList.get(2).executioncost; 33 | computationCost += executionCostArray[position.c]; 34 | executionCostArray = Swarm.cloudletList.get(3).executioncost; 35 | computationCost += executionCostArray[position.d]; 36 | executionCostArray = Swarm.cloudletList.get(4).executioncost; 37 | computationCost += executionCostArray[position.e]; 38 | 39 | double communicationCost = 0; 40 | int[] data = Swarm.cloudletList.get(1).datasize; 41 | 42 | double[] communicationCostArray = Swarm.vmlist.get(position.a).comcost; 43 | communicationCost+=data[0]*communicationCostArray[position.b]; 44 | data = Swarm.cloudletList.get(2).datasize; 45 | 46 | communicationCostArray = Swarm.vmlist.get(position.b).comcost; 47 | communicationCost+=data[0]*communicationCostArray[position.c]; 48 | data = Swarm.cloudletList.get(3).datasize; 49 | 50 | communicationCostArray = Swarm.vmlist.get(position.c).comcost; 51 | communicationCost+=data[0]*communicationCostArray[position.d]; 52 | data = Swarm.cloudletList.get(4).datasize; 53 | 54 | communicationCostArray = Swarm.vmlist.get(position.d).comcost; 55 | communicationCost+=data[0]*communicationCostArray[position.e]; 56 | 57 | cost = computationCost + communicationCost; 58 | return cost; 59 | } 60 | 61 | 62 | void updatePersonalBest () { 63 | double eval = eval(); 64 | if (eval < bestEval) { 65 | bestPosition = position.clone(); 66 | bestEval = eval; 67 | } 68 | } 69 | 70 | Vector getPosition () { 71 | return position.clone(); 72 | } 73 | 74 | Vector getVelocity () { 75 | return velocity.clone(); 76 | } 77 | 78 | Vector getBestPosition() { 79 | return bestPosition.clone(); 80 | } 81 | 82 | double getBestEval () { 83 | return bestEval; 84 | } 85 | 86 | void updatePosition () { 87 | this.position.add(velocity); 88 | } 89 | 90 | void setVelocity (Vector velocity) { 91 | this.velocity = velocity.clone(); 92 | } 93 | 94 | 95 | } 96 | -------------------------------------------------------------------------------- /Population.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | import java.util.Random; 6 | 7 | 8 | public class Population { 9 | public Individual population[]; 10 | public double populationFitness = -1; 11 | 12 | public Population(int populationSize) { 13 | this.population = new Individual[populationSize]; 14 | } 15 | 16 | public Population(int populationSize, int chromosomeLength) { 17 | this.population = new Individual[populationSize]; 18 | 19 | for (int individualCount = 0; individualCount < populationSize; individualCount++) { 20 | Individual individual = new Individual(chromosomeLength); 21 | this.population[individualCount] = individual; 22 | } 23 | 24 | System.out.println("Population"); 25 | for(int i=0;i "); 27 | for(int j=0;j<5;j++) { 28 | System.out.print(population[i].chromosome[j] + " "); 29 | } 30 | System.out.println(); 31 | } 32 | 33 | 34 | } 35 | 36 | public Individual[] getIndividuals() { 37 | return this.population; 38 | } 39 | 40 | public Individual getFittest(int offset) { 41 | Arrays.sort(this.population, new Comparator() { 42 | @Override 43 | public int compare(Individual o1, Individual o2) { 44 | if (o1.getFitness() > o2.getFitness()) { 45 | return 1; 46 | } else if (o1.getFitness() < o2.getFitness()) { 47 | return -1; 48 | } 49 | return 0; 50 | } 51 | }); 52 | return this.population[offset]; 53 | } 54 | 55 | public void setPopulationFitness(double fitness) { 56 | this.populationFitness = fitness; 57 | } 58 | 59 | public double getPopulationFitness() { 60 | return this.populationFitness; 61 | } 62 | 63 | public int size() { 64 | return this.population.length; 65 | } 66 | 67 | public Individual setIndividual(int offset, Individual individual) { 68 | return population[offset] = individual; 69 | } 70 | 71 | public Individual getIndividual(int offset) { 72 | return population[offset]; 73 | } 74 | 75 | public void shuffle() { 76 | Random rnd = new Random(); 77 | for (int i = population.length - 1; i > 0; i--) { 78 | int index = rnd.nextInt(i + 1); 79 | Individual a = population[index]; 80 | population[index] = population[i]; 81 | population[i] = a; 82 | } 83 | } 84 | } -------------------------------------------------------------------------------- /PsoScheduling.java: -------------------------------------------------------------------------------- 1 | package Fitness_Function; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileInputStream; 5 | import java.io.IOException; 6 | import java.io.InputStreamReader; 7 | import java.text.DecimalFormat; 8 | import java.util.ArrayList; 9 | import java.util.Calendar; 10 | import java.util.HashMap; 11 | import java.util.HashSet; 12 | import java.util.Iterator; 13 | import java.util.LinkedList; 14 | import java.util.List; 15 | import org.cloudbus.cloudsim.Cloudlet; 16 | import org.cloudbus.cloudsim.CloudletSchedulerSpaceShared; 17 | import org.cloudbus.cloudsim.CloudletSchedulerTimeShared; 18 | import org.cloudbus.cloudsim.Datacenter; 19 | import org.cloudbus.cloudsim.DatacenterBroker; 20 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 21 | import org.cloudbus.cloudsim.Host; 22 | import org.cloudbus.cloudsim.Log; 23 | import org.cloudbus.cloudsim.Pe; 24 | import org.cloudbus.cloudsim.Storage; 25 | import org.cloudbus.cloudsim.UtilizationModel; 26 | import org.cloudbus.cloudsim.UtilizationModelFull; 27 | import org.cloudbus.cloudsim.Vm; 28 | import org.cloudbus.cloudsim.VmAllocationPolicySimple; 29 | import org.cloudbus.cloudsim.VmSchedulerSpaceShared; 30 | import org.cloudbus.cloudsim.VmSchedulerTimeShared; 31 | import org.cloudbus.cloudsim.core.CloudSim; 32 | import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 33 | import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 34 | import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 35 | import FitnessFunction.*; 36 | import FitnessFunction.Constant; 37 | import FitnessFunction.PSO; 38 | import FitnessFunction.SchedulerParticle; 39 | import FitnessFunction.SchedulerParticleUpdate; 40 | 41 | public class PsoScheduling { 42 | static Datacenter[] datacenter; 43 | private static List vmlist; 44 | private static List cloudletList; 45 | private static final PSO PSOSchedularInstance = new PSO();; 46 | public static double mapping[] = PSOSchedularInstance.run(); 47 | public double[] getPsoMapping() { return mapping; } 48 | 49 | private static void createTasks(int brokerId,String filePath, int taskNum) 50 | { 51 | try 52 | { 53 | @SuppressWarnings("resource") 54 | BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(filePath))); 55 | String data = null; 56 | int index = 0; 57 | 58 | //cloudlet properties. 59 | int pesNumber = 1; 60 | long fileSize = 1000; 61 | long outputSize = 1000; 62 | UtilizationModel utilizationModel = new UtilizationModelFull(); 63 | 64 | while ((data = br.readLine()) != null) 65 | { 66 | System.out.println(data); 67 | String[] taskLength=data.split("\t"); 68 | for(int j=0;j<20;j++){ 69 | Cloudlet task=new Cloudlet(index+j, (long) Double.parseDouble(taskLength[j]), pesNumber, fileSize, 70 | outputSize, utilizationModel, utilizationModel, 71 | utilizationModel); 72 | task.setUserId(brokerId); 73 | cloudletList.add(task); 74 | if(cloudletList.size()==taskNum) 75 | { 76 | br.close(); 77 | return; 78 | } 79 | } 80 | index+=20; 81 | } 82 | 83 | } 84 | catch (IOException e) 85 | { 86 | e.printStackTrace(); 87 | } 88 | } 89 | 90 | 91 | public static void main(String[] args) { 92 | double execTimeMatrix[][] = PSOSchedularInstance.getExecTimeMatrix(); 93 | double communTimeMatrix[][] = PSOSchedularInstance.getCommunTimeMatrix(); 94 | Log.printLine("Starting Task Schedular Simulation..."); 95 | try { 96 | int num_user = 2; 97 | Calendar calendar = Calendar.getInstance(); 98 | boolean trace_flag = false; 99 | CloudSim.init(num_user, calendar, trace_flag); 100 | datacenter = new Datacenter[Constant.NO_OF_DATA_CENTERS]; 101 | for(int i = 0; i < Constant.NO_OF_DATA_CENTERS; i++) { 102 | datacenter[i] = createDatacenter("Datacenter_" + i); 103 | } 104 | DatacenterBroker broker = createBroker(); 105 | int brokerId = broker.getId(); 106 | vmlist = new ArrayList(); 107 | //int mips = 1000; 108 | long size = 10000; 109 | int ram = 512; 110 | long bw = 1000; 111 | int pesNumber = 1; 112 | String vmm = "Xen"; 113 | Vm vm1 = new Vm(0, brokerId, 5000, pesNumber, ram, bw, size, 114 | vmm, new CloudletSchedulerSpaceShared()); 115 | Vm vm2 = new Vm(1, brokerId, 2500, pesNumber, ram, bw, size, 116 | vmm,new CloudletSchedulerTimeShared()); 117 | Vm vm3 = new Vm(2, brokerId, 2500, pesNumber, ram, bw, size, 118 | vmm,new CloudletSchedulerTimeShared()); 119 | Vm vm4 = new Vm(3, brokerId, 1500, pesNumber, ram, bw, size, 120 | vmm, new CloudletSchedulerSpaceShared()); 121 | Vm vm5 = new Vm(4, brokerId, 1000, pesNumber, ram, bw, size, 122 | vmm, new CloudletSchedulerSpaceShared()); 123 | 124 | // add the VMs to the vmList 125 | vmlist.add(vm1); 126 | vmlist.add(vm2); 127 | vmlist.add(vm3); 128 | vmlist.add(vm4); 129 | vmlist.add(vm5); 130 | 131 | broker.submitVmList(vmlist); 132 | cloudletList = new ArrayList(); 133 | 134 | createTasks(brokerId,"data\\cloudlets.txt",200); 135 | HashSet dcIds = new HashSet<>(); 136 | HashMap hm = new HashMap<>(); 137 | for(Datacenter dc: datacenter) { 138 | if(!dcIds.contains(dc.getId())) 139 | dcIds.add(dc.getId()); 140 | } 141 | Iterator it = dcIds.iterator(); 142 | for(int i = 0; i < mapping.length; i++) { 143 | if(hm.containsKey((int) mapping[i])) continue; 144 | hm.put((int) mapping[i], (int) it.next()); 145 | } 146 | for(int i = 0; i < mapping.length; i++) 147 | mapping[i] = hm.containsKey((int) mapping[i]) ? hm.get((int) mapping[i]) : mapping[i]; 148 | //broker.submitMapping(mapping); 149 | broker.submitCloudletList(cloudletList); 150 | CloudSim.startSimulation(); 151 | List newList = broker.getCloudletReceivedList(); 152 | CloudSim.stopSimulation(); 153 | printCloudletList(newList); 154 | Log.printLine("simulating PSO finished!"); 155 | }catch(Exception e) { 156 | System.out.println("An error has been occurred!\n" + e.getMessage()); 157 | } 158 | } 159 | private static void printCloudletList(List list) { 160 | int size = list.size(); 161 | double value1=0; 162 | Cloudlet cloudlet; 163 | String indent = " "; 164 | Log.printLine(); 165 | Log.printLine("========== OUTPUT =========="); 166 | Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + 167 | "Data center ID" + indent + "VM ID" + indent + "Time" + indent + "Start Time" + indent + "Finish Time"); 168 | double mxFinishTime = 0; 169 | DecimalFormat dft = new DecimalFormat("###.##"); 170 | for (int i = 0; i < size; i++) { 171 | cloudlet = list.get(i); 172 | Log.print(indent + cloudlet.getCloudletId() + indent + indent); 173 | if (cloudlet.getStatus() == Cloudlet.SUCCESS){ 174 | Log.print("SUCCESS"); 175 | Log.printLine( indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId() + 176 | indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent + indent + dft.format(cloudlet.getExecStartTime())+ 177 | indent + indent + dft.format(cloudlet.getFinishTime())); 178 | mxFinishTime = Math.max(mxFinishTime, cloudlet.getFinishTime()); 179 | } 180 | value1= value1+Double.parseDouble(dft.format(cloudlet.getActualCPUTime())); 181 | } 182 | PSOSchedularInstance.printBestFitness(); 183 | System.out.println(mxFinishTime); 184 | System.out.println("This (G&PSO) schedule plan takes "+value1/10+" ms to finish execution."); 185 | } 186 | private static Datacenter createDatacenter(String name) 187 | { 188 | List hostList = new ArrayList(); 189 | List peList = new ArrayList(); 190 | 191 | int mips = 5000; 192 | peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store MIPS Rating 193 | 194 | mips = 2500; 195 | peList.add(new Pe(1, new PeProvisionerSimple(mips))); 196 | 197 | mips = 2500; 198 | peList.add(new Pe(2, new PeProvisionerSimple(mips))); 199 | 200 | mips = 1500; 201 | peList.add(new Pe(3, new PeProvisionerSimple(mips))); 202 | 203 | mips = 1000; 204 | peList.add(new Pe(4, new PeProvisionerSimple(mips))); 205 | 206 | int hostId = 0; 207 | int ram = 4096; // host memory (MB) 208 | long storage = 10000000; // host storage 209 | int bw = 10000; 210 | 211 | hostList.add(new Host(hostId, new RamProvisionerSimple(ram), 212 | new BwProvisionerSimple(bw), storage, peList, 213 | new VmSchedulerTimeShared(peList))); 214 | String arch = "x86"; // system architecture 215 | String os = "Linux"; // operating system 216 | String vmm = "Xen"; 217 | double time_zone = 5.30; 218 | double cost = 3.0; 219 | double costPerMem = 0.05; 220 | double costPerStorage = 0.001; 221 | 222 | double costPerBw = 0.001; 223 | 224 | 225 | LinkedList storageList = new LinkedList(); 226 | 227 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 228 | arch, os, vmm, hostList, time_zone, cost, costPerMem, 229 | costPerStorage, costPerBw); 230 | 231 | 232 | Datacenter datacenter = null; 233 | try 234 | { 235 | datacenter = new Datacenter(name, characteristics, 236 | new VmAllocationPolicySimple(hostList), storageList, 0); 237 | } catch (Exception e) 238 | { 239 | e.printStackTrace(); 240 | } 241 | 242 | return datacenter; 243 | } 244 | 245 | private static DatacenterBroker createBroker() { 246 | DatacenterBroker broker = null; 247 | try { 248 | broker = new DatacenterBroker("Broker"); 249 | } catch (Exception e) { 250 | e.printStackTrace(); 251 | } 252 | return broker; 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Meta-Heuristics-Algorithms-for-Task-Scheduling-in-Cloud 2 | 3 | # Implementation of Greedy Particle Swarm Optimization,HSGA and Hybrid(GA+PSO) for the purpose of Task Scheduling in cloud computing environment 4 | 5 | Cloud Service providers are facing problem with optimized scheduling of tasks to the virtual machines in cloud computing environment. Scheduling of resources must be done in such a way that it reduces the total resource cost, reduces total time of execution, improves throughtput, reduces failure, balances server load and provides the highest possible QOS. The problem of task scheduling comes under the category of NP-hard problems. Meta-heuristics algorithms find the best or near-best solution in reasonable amount of time by making random choices to find the solution. 6 | 7 | In this work three different meta-heuristic algorithms which were GPSO, HSGA and Hybrid (GA+PSO) were implemented for task scheduling and a comparison was made between them. 8 | 9 | For implementing this project I have used Cloudsim 4.0 which is an open source framework used to simulate the cloud computing infrastructure and services. It is entirely written in JAVA and it enables the modelling and simulation of core features of cloud like: task queues, event processing, cloud entity formation (data centers, data center broker etc.), communication between entities, borker policy implementation and so on. With this I have used Eclipse IDE and JAVA version 11.0.10. 10 | 11 | Comparing the algorithms on the basis of execution time indicates that Hybrid (GA+PSO) performs the best followed by HSGA and then GPSO. 12 | -------------------------------------------------------------------------------- /Scheduler.java: -------------------------------------------------------------------------------- 1 | package Fitness_Function; 2 | 3 | import net.sourceforge.jswarm_pso.FitnessFunction; 4 | 5 | public class Scheduler extends FitnessFunction { 6 | private static double[][] execTimeMatrix, communTimeMatrix; 7 | 8 | Scheduler() { 9 | super(false); 10 | initMatrices(); 11 | } 12 | 13 | @Override 14 | public double evaluate(double[] position) { 15 | return calcMakespan(position); 16 | } 17 | 18 | private double calcTotalTime(double[] position) { 19 | double totalCost = 0; 20 | for (int i = 0; i < Constant.NO_OF_TASKS; i++) { 21 | int dcId = (int) position[i]; 22 | totalCost += execTimeMatrix[i][dcId] + communTimeMatrix[i][dcId]; 23 | } 24 | return totalCost; 25 | } 26 | 27 | public double calcMakespan(double[] position) { 28 | double makespan = 0; 29 | double[] dcWorkingTime = new double[Constant.NO_OF_DATA_CENTERS]; 30 | 31 | for (int i = 0; i < Constant.NO_OF_TASKS; i++) { 32 | int dcId = (int) position[i]; 33 | if(dcWorkingTime[dcId] != 0) --dcWorkingTime[dcId]; 34 | dcWorkingTime[dcId] += execTimeMatrix[i][dcId] + communTimeMatrix[i][dcId]; 35 | makespan = Math.max(makespan, dcWorkingTime[dcId]); 36 | } 37 | return makespan; 38 | } 39 | public double[][] getExecTimeMatrix() { return execTimeMatrix; } 40 | public double[][] getCoumnTimeMatrix() { return communTimeMatrix; } 41 | 42 | private void initMatrices() { 43 | System.out.println("Initializing input matrices (e.g. exec time & communication time matrices"); 44 | execTimeMatrix = new double[Constant.NO_OF_TASKS][Constant.NO_OF_DATA_CENTERS]; 45 | communTimeMatrix = new double[Constant.NO_OF_TASKS][Constant.NO_OF_DATA_CENTERS]; 46 | 47 | for (int i = 0; i < Constant.NO_OF_TASKS; i++) { 48 | for (int j = 0; j < Constant.NO_OF_DATA_CENTERS; j++) { 49 | execTimeMatrix[i][j] = Math.random() * 500; 50 | communTimeMatrix[i][j] = Math.random() * 500 + 20; 51 | } 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /SchedulerParticle.java: -------------------------------------------------------------------------------- 1 | package Fitness_Function; 2 | 3 | import net.sourceforge.jswarm_pso.Particle; 4 | 5 | 6 | 7 | import java.util.Random; 8 | 9 | 10 | 11 | public class SchedulerParticle extends Particle { 12 | 13 | SchedulerParticle() { 14 | 15 | super(Constant.NO_OF_TASKS); 16 | 17 | double[] position = new double[Constant.NO_OF_TASKS]; 18 | 19 | double[] velocity = new double[Constant.NO_OF_TASKS]; 20 | 21 | 22 | 23 | for (int i = 0; i < Constant.NO_OF_TASKS; i++) { 24 | 25 | Random randObj = new Random(); 26 | 27 | position[i] = randObj.nextInt(Constant.NO_OF_DATA_CENTERS); 28 | 29 | velocity[i] = Math.random(); 30 | 31 | } 32 | 33 | setPosition(position); 34 | 35 | setVelocity(velocity); 36 | 37 | } 38 | 39 | 40 | 41 | @Override 42 | 43 | public String toString() { 44 | 45 | String output = ""; 46 | 47 | for (int i = 0; i < Constant.NO_OF_DATA_CENTERS; i++) { 48 | 49 | String tasks = ""; 50 | 51 | int no_of_tasks = 0; 52 | 53 | for (int j = 0; j < Constant.NO_OF_TASKS; j++) { 54 | 55 | if (i == (int) getPosition()[j]) { 56 | 57 | tasks += (tasks.isEmpty() ? "" : " ") + j; 58 | 59 | ++no_of_tasks; 60 | 61 | } 62 | 63 | } 64 | 65 | if (tasks.isEmpty()) output += "There is no tasks associated to Data Center " + i + "\n"; 66 | 67 | else 68 | 69 | output += "There are " + no_of_tasks + " tasks associated to Data Center " + i + " and they are " + tasks + "\n"; 70 | 71 | } 72 | 73 | return output; 74 | 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /SchedulerParticleUpdate.java: -------------------------------------------------------------------------------- 1 | package Fitness_Function; 2 | 3 | import net.sourceforge.jswarm_pso.Particle; 4 | 5 | import net.sourceforge.jswarm_pso.ParticleUpdate; 6 | 7 | import net.sourceforge.jswarm_pso.Swarm; 8 | 9 | public class SchedulerParticleUpdate extends ParticleUpdate { 10 | 11 | private static final double W = 0.9; 12 | 13 | private static final double C = 2.0; 14 | 15 | 16 | 17 | SchedulerParticleUpdate(Particle particle) { 18 | 19 | super(particle); 20 | 21 | } 22 | 23 | 24 | 25 | @Override 26 | 27 | public void update(Swarm swarm, Particle particle) { 28 | 29 | double[] v = particle.getVelocity(); 30 | 31 | double[] x = particle.getPosition(); 32 | 33 | double[] pbest = particle.getBestPosition(); 34 | 35 | double[] gbest = swarm.getBestPosition(); 36 | 37 | 38 | 39 | for (int i = 0; i < Constant.NO_OF_TASKS; ++i) { 40 | 41 | v[i] = W * v[i] + C * Math.random() * (pbest[i] - x[i]) + C * Math.random() * (gbest[i] - x[i]); 42 | 43 | x[i] = (int) (x[i] + v[i]); 44 | 45 | } 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Swarm.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.List; 4 | import java.util.Random; 5 | 6 | public class Swarm { 7 | 8 | private int numOfParticles, epochs; 9 | private double inertia, cognitiveComponent, socialComponent; 10 | public Vector bestPosition; 11 | private double bestEval; 12 | private int[][] particles; 13 | public static final double DEFAULT_INERTIA = 0.729844; 14 | public static final double DEFAULT_COGNITIVE = 1.496180; // Cognitive component. 15 | public static final double DEFAULT_SOCIAL = 1.496180; // Social component. 16 | public static List cloudletList; 17 | public static List vmlist; 18 | 19 | 20 | public Swarm (int[][] particles, int epochs, int numOfParticles, List cloudletList, List vmlist) { 21 | this.numOfParticles = numOfParticles; 22 | this.particles=particles; 23 | this.epochs=epochs; 24 | this.cloudletList = cloudletList; 25 | this.vmlist = vmlist; 26 | bestPosition = new Vector(0, 0, 0, 0, 0); 27 | bestEval = Double.POSITIVE_INFINITY; 28 | 29 | } 30 | 31 | public void run (int[][] individuals) { 32 | Particle[] particle=new Particle[numOfParticles]; 33 | for(int i=0;i" + (particle[i].position).toString()); 42 | } 43 | 44 | double oldEval = bestEval; 45 | System.out.println("--------------------------EXECUTING-------------------------"); 46 | 47 | 48 | for (int i = 0; i < epochs; i++) { 49 | 50 | if (bestEval < oldEval) { 51 | 52 | oldEval = bestEval; 53 | } 54 | 55 | 56 | for (int j=0;j