├── MultiSwarm ├── Constants.java ├── FitnessFunctionPSO.java ├── ModifiedPSOUpdate.java ├── PSOSubSwarm.java ├── ParticlePSO.java ├── testfunc.java └── testmain.java ├── MultiSwarmPSOCloudTest ├── Constants.java ├── FitnessFunctionPSO.java ├── ModifiedPSOUpdate.java ├── MyDataCenterBroker.java ├── PSO.java ├── ParticlePSO.java ├── Results.java └── TaskScheduler3.java ├── PSOCEC2013 ├── Constants.java ├── FitnessFunctionPSO.java ├── MYPSO.java ├── ModifiedPSOUpdate.java ├── PSO.java ├── ParticlePSO.java ├── testfunc.java └── testmain.java ├── PSOCloud ├── Constants.java ├── FitnessFunctionPSO.java ├── ModifiedPSOUpdate.java ├── MyDataCenterBroker.java ├── PSO.java ├── ParticlePSO.java ├── ResCalc.java └── TaskScheduler3.java ├── README.md └── Workspace.zip /MultiSwarm/Constants.java: -------------------------------------------------------------------------------- 1 | package MultiSwarm; 2 | /* 3 | * Class Name : Constants 4 | * Purpose : Serves as a reference for changing the parameters for the PSO Algorithm. 5 | * 6 | */ 7 | public class Constants { 8 | /* 9 | * Here number of tasks represents the number of dimensions in a particle 10 | * 11 | */ 12 | public static final int NoOfTasks = 5; 13 | public static final int NoOfSwarms = 6; 14 | public static final int NoOfParticles = 30; 15 | public static final int NoOfIterations = 4000; 16 | } 17 | -------------------------------------------------------------------------------- /MultiSwarm/FitnessFunctionPSO.java: -------------------------------------------------------------------------------- 1 | package MultiSwarm; 2 | 3 | import MultiSwarm.*; 4 | import net.sourceforge.jswarm_pso.*; 5 | 6 | 7 | /* 8 | * Class Name: Fitness Function 9 | * Purpose: It provides the function used to test the convergence of our PSO Algorithm 10 | * 11 | */ 12 | public class FitnessFunctionPSO extends FitnessFunction{ 13 | 14 | /* 15 | * Global Parameters: 16 | * funcno : refers to the function number corresponding to different functions in CEC-2013 17 | * 18 | */ 19 | int funcno; 20 | FitnessFunctionPSO(int funcno) 21 | { 22 | /* 23 | * Here false in super denotes that our objective is to minimize the function. 24 | */ 25 | super(false); 26 | this.funcno=funcno; 27 | } 28 | 29 | /* 30 | * evaluate is the function which returns the fitness value of the position 31 | * the fitness value of the position is returned in f[0] 32 | */ 33 | public double evaluate(double[] position) { 34 | testfunc tf = new testfunc(); 35 | double f[]= {0.0,0.0}; 36 | 37 | try { 38 | tf.test_func(position,f, Constants.NoOfTasks, 1, funcno); 39 | } catch (Exception e) { 40 | e.printStackTrace(); 41 | } 42 | return f[0]; 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /MultiSwarm/ModifiedPSOUpdate.java: -------------------------------------------------------------------------------- 1 | package MultiSwarm; 2 | 3 | import MultiSwarm.Constants; 4 | import MultiSwarm.ParticlePSO; 5 | import net.sourceforge.jswarm_pso.Particle; 6 | import net.sourceforge.jswarm_pso.ParticleUpdate; 7 | import net.sourceforge.jswarm_pso.Swarm; 8 | 9 | /* 10 | * Class Name: PSO Position and Velocity Update 11 | * Purpose: It is used to update the position and velocity of the given particle 12 | * 13 | */ 14 | 15 | public class ModifiedPSOUpdate extends ParticleUpdate{ 16 | 17 | /* 18 | * Global Parameters: 19 | * obj : is the object used to store the particle as an object. 20 | * 21 | */ 22 | ParticlePSO obj; 23 | ModifiedPSOUpdate(ParticlePSO particle){ 24 | super(particle); 25 | this.obj=particle; 26 | } 27 | 28 | /* 29 | * update : provided by JSwarm and used to update the position and velocity 30 | * 31 | */ 32 | public void update(Swarm swarm,Particle particle) { 33 | 34 | double v[]=particle.getVelocity(); 35 | double x[]=particle.getPosition(); 36 | double pbest[]=particle.getBestPosition(); 37 | double gbest[]=swarm.getBestPosition(); 38 | /* 39 | * count : stores the number of iterations of each particle has been updated. 40 | */ 41 | obj.count=obj.count+1; 42 | int it=obj.count; 43 | /* 44 | * w : represents inertia weight which has been calculated based on number of iterations 45 | * k : represents the constriction factor which has been calculated based on number of iterations 46 | * 47 | */ 48 | double w=0.857143+(1-0.857143)*(1-it/Constants.NoOfIterations); 49 | double k=(Math.cos((Math.PI/Constants.NoOfIterations)*it)+2.5)/4.0; 50 | 51 | for(int i=0;i Standard Velocity Update Function 55 | * 2 -> Velocity Update using Modified Inertia Weight 56 | * 3 -> Velocity Update using Constriction Factor 57 | * 4 -> Velocity Update using both Inertia Weight and Constriction Factor 58 | */ 59 | /* 60 | * Uncomment the update function that is required to change the update function. 61 | */ 62 | // 1. 63 | v[i]=0.729844*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i]); 64 | // 2. 65 | // v[i]=w*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i]); 66 | // 3. 67 | // v[i]=k*(v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i])); 68 | // 4. 69 | // v[i]=k*(w*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i])); 70 | 71 | particle.setVelocity(v); 72 | x[i]=(x[i]+v[i]); 73 | particle.setPosition(x); 74 | } 75 | 76 | } 77 | } 78 | 79 | -------------------------------------------------------------------------------- /MultiSwarm/PSOSubSwarm.java: -------------------------------------------------------------------------------- 1 | package MultiSwarm; 2 | 3 | import java.util.Arrays; 4 | import java.io.*; 5 | import net.sourceforge.jswarm_pso.*; 6 | 7 | /* 8 | * Class Name: PSOSubSwarm 9 | * 10 | */ 11 | public class PSOSubSwarm { 12 | 13 | /* 14 | * Global Parameters: 15 | * ff : represents the fitness function used by all particles in the swarm 16 | * swarm : represents the different swarms that are used in multiswarm PSO 17 | * particles[i][j] : represents the particles where i is the swarm id and j is particle id 18 | * error[][][] : stores the error for all 28 fitness function 19 | * ansplot[][][] : stores the fitness value for all 28 fitness function 20 | * ansfitness[] : represents the best possible fitness value of the function (minimum) 21 | * funcno : stores the function number of CEC function to be checked 22 | */ 23 | 24 | FitnessFunctionPSO ff ; 25 | Swarm swarm[] = new Swarm[Constants.NoOfSwarms]; 26 | private static ParticlePSO particles[][]; 27 | static double error[][][] = new double[28][5][11]; 28 | static double ansplot[][][] = new double[28][5][11]; 29 | static double multiply[]={0.01,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}; 30 | static int minimum=0; 31 | double ansfitness[]={-1400,-1300,-1200,-1100,-1000,-900,-800,-700,-600,-500,-400,-300,-200,-100,100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400}; 32 | int funcno; 33 | int counter; 34 | public PSOSubSwarm(int funcno,int counter) 35 | { 36 | this.funcno=funcno; 37 | this.counter=counter; 38 | ff = new FitnessFunctionPSO(funcno); 39 | /* 40 | * Dividing the particles into subswarms 41 | * 42 | */ 43 | for(int q=0;q Standard Velocity Update Function 53 | * 2 -> Velocity Update using Modified Inertia Weight 54 | * 3 -> Velocity Update using Constriction Factor 55 | * 4 -> Velocity Update using both Inertia Weight and Constriction Factor 56 | */ 57 | /* 58 | * Uncomment the update function that is required to change the update function. 59 | */ 60 | // 1. 61 | v[i]=0.729844*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i]); 62 | // 2. 63 | // v[i]=w*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i]); 64 | // 3. 65 | // v[i]=k*(v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i])); 66 | // 4. 67 | // v[i]=k*(w*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i])); 68 | 69 | particle.setVelocity(v); 70 | x[i]=(x[i]+v[i]); 71 | particle.setPosition(x); 72 | } 73 | 74 | } 75 | } -------------------------------------------------------------------------------- /MultiSwarmPSOCloudTest/MyDataCenterBroker.java: -------------------------------------------------------------------------------- 1 | package MultiSwarmPSOCloudTest; 2 | 3 | /* 4 | * Title: CloudSim Toolkit 5 | * Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds 6 | * Licence: GPL - http://www.gnu.org/copyleft/gpl.html 7 | * 8 | * Copyright (c) 2009-2012, The University of Melbourne, Australia 9 | */ 10 | 11 | import java.util.ArrayList; 12 | import java.util.HashMap; 13 | import java.util.LinkedList; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | import org.cloudbus.cloudsim.Cloudlet; 18 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 19 | import org.cloudbus.cloudsim.Vm; 20 | import org.cloudbus.cloudsim.core.CloudSim; 21 | import org.cloudbus.cloudsim.core.CloudSimTags; 22 | import org.cloudbus.cloudsim.core.SimEntity; 23 | import org.cloudbus.cloudsim.core.SimEvent; 24 | import org.cloudbus.cloudsim.lists.CloudletList; 25 | import org.cloudbus.cloudsim.lists.VmList; 26 | import org.cloudbus.cloudsim.Log; 27 | 28 | /** 29 | * DatacentreBroker represents a broker acting on behalf of a user. It hides VM management, as vm 30 | * creation, sumbission of cloudlets to this VMs and destruction of VMs. 31 | * 32 | * @author Rodrigo N. Calheiros 33 | * @author Anton Beloglazov 34 | * @since CloudSim Toolkit 1.0 35 | */ 36 | public class MyDataCenterBroker extends SimEntity { 37 | 38 | private double[] mapping; 39 | private double[] lag; 40 | /** The vm list. */ 41 | protected List vmList; 42 | 43 | /** The vms created list. */ 44 | protected List vmsCreatedList; 45 | 46 | /** The cloudlet list. */ 47 | protected List cloudletList; 48 | 49 | /** The cloudlet submitted list. */ 50 | protected List cloudletSubmittedList; 51 | 52 | /** The cloudlet received list. */ 53 | protected List cloudletReceivedList; 54 | 55 | /** The cloudlets submitted. */ 56 | protected int cloudletsSubmitted; 57 | 58 | /** The vms requested. */ 59 | protected int vmsRequested; 60 | 61 | /** The vms acks. */ 62 | protected int vmsAcks; 63 | 64 | /** The vms destroyed. */ 65 | protected int vmsDestroyed; 66 | 67 | /** The datacenter ids list. */ 68 | protected List datacenterIdsList; 69 | 70 | /** The datacenter requested ids list. */ 71 | protected List datacenterRequestedIdsList; 72 | 73 | /** The vms to datacenters map. */ 74 | protected Map vmsToDatacentersMap; 75 | 76 | /** The datacenter characteristics list. */ 77 | protected Map datacenterCharacteristicsList; 78 | 79 | /** 80 | * Created a new DatacenterBroker object. 81 | * 82 | * @param name name to be associated with this entity (as required by Sim_entity class from 83 | * simjava package) 84 | * @throws Exception the exception 85 | * @pre name != null 86 | * @post $none 87 | */ 88 | public MyDataCenterBroker(String name) throws Exception { 89 | super(name); 90 | 91 | setVmList(new ArrayList()); 92 | setVmsCreatedList(new ArrayList()); 93 | setCloudletList(new ArrayList()); 94 | setCloudletSubmittedList(new ArrayList()); 95 | setCloudletReceivedList(new ArrayList()); 96 | 97 | cloudletsSubmitted = 0; 98 | setVmsRequested(0); 99 | setVmsAcks(0); 100 | setVmsDestroyed(0); 101 | 102 | setDatacenterIdsList(new LinkedList()); 103 | setDatacenterRequestedIdsList(new ArrayList()); 104 | setVmsToDatacentersMap(new HashMap()); 105 | setDatacenterCharacteristicsList(new HashMap()); 106 | } 107 | 108 | /** 109 | * This method is used to send to the broker the list with virtual machines that must be 110 | * created. 111 | * 112 | * @param list the list 113 | * @pre list !=null 114 | * @post $none 115 | */ 116 | public void submitVmList(List list) { 117 | getVmList().addAll(list); 118 | } 119 | 120 | /** 121 | * This method is used to send to the broker the list of cloudlets. 122 | * 123 | * @param list the list 124 | * @pre list !=null 125 | * @post $none 126 | */ 127 | public void submitCloudletList(List list) { 128 | getCloudletList().addAll(list); 129 | } 130 | 131 | /** 132 | * Specifies that a given cloudlet must run in a specific virtual machine. 133 | * 134 | * @param cloudletId ID of the cloudlet being bount to a vm 135 | * @param vmId the vm id 136 | * @pre cloudletId > 0 137 | * @pre id > 0 138 | * @post $none 139 | */ 140 | public void bindCloudletToVm(int cloudletId, int vmId) { 141 | CloudletList.getById(getCloudletList(), cloudletId).setVmId(vmId); 142 | } 143 | 144 | /** 145 | * Processes events available for this Broker. 146 | * 147 | * @param ev a SimEvent object 148 | * @pre ev != null 149 | * @post $none 150 | */ 151 | @Override 152 | public void processEvent(SimEvent ev) { 153 | switch (ev.getTag()) { 154 | // Resource characteristics request 155 | case CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST: 156 | processResourceCharacteristicsRequest(ev); 157 | break; 158 | // Resource characteristics answer 159 | case CloudSimTags.RESOURCE_CHARACTERISTICS: 160 | processResourceCharacteristics(ev); 161 | break; 162 | // VM Creation answer 163 | case CloudSimTags.VM_CREATE_ACK: 164 | processVmCreate(ev); 165 | break; 166 | // A finished cloudlet returned 167 | case CloudSimTags.CLOUDLET_RETURN: 168 | processCloudletReturn(ev); 169 | break; 170 | // if the simulation finishes 171 | case CloudSimTags.END_OF_SIMULATION: 172 | shutdownEntity(); 173 | break; 174 | // other unknown tags are processed by this method 175 | default: 176 | processOtherEvent(ev); 177 | break; 178 | } 179 | } 180 | 181 | /** 182 | * Process the return of a request for the characteristics of a PowerDatacenter. 183 | * 184 | * @param ev a SimEvent object 185 | * @pre ev != $null 186 | * @post $none 187 | */ 188 | protected void processResourceCharacteristics(SimEvent ev) { 189 | DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData(); 190 | getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics); 191 | 192 | if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size()) { 193 | setDatacenterRequestedIdsList(new ArrayList()); 194 | createVmsInDatacenter(getDatacenterIdsList().get(0)); 195 | } 196 | } 197 | 198 | /** 199 | * Process a request for the characteristics of a PowerDatacenter. 200 | * 201 | * @param ev a SimEvent object 202 | * @pre ev != $null 203 | * @post $none 204 | */ 205 | protected void processResourceCharacteristicsRequest(SimEvent ev) { 206 | setDatacenterIdsList(CloudSim.getCloudResourceList()); 207 | setDatacenterCharacteristicsList(new HashMap()); 208 | 209 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloud Resource List received with " 210 | + getDatacenterIdsList().size() + " resource(s)"); 211 | 212 | for (Integer datacenterId : getDatacenterIdsList()) { 213 | sendNow(datacenterId, CloudSimTags.RESOURCE_CHARACTERISTICS, getId()); 214 | } 215 | } 216 | 217 | /** 218 | * Process the ack received due to a request for VM creation. 219 | * 220 | * @param ev a SimEvent object 221 | * @pre ev != null 222 | * @post $none 223 | */ 224 | protected void processVmCreate(SimEvent ev) { 225 | int[] data = (int[]) ev.getData(); 226 | int datacenterId = data[0]; 227 | int vmId = data[1]; 228 | int result = data[2]; 229 | 230 | if (result == CloudSimTags.TRUE) { 231 | getVmsToDatacentersMap().put(vmId, datacenterId); 232 | getVmsCreatedList().add(VmList.getById(getVmList(), vmId)); 233 | Log.printLine(CloudSim.clock() + ": " + getName() + ": VM #" + vmId 234 | + " has been created in Datacenter #" + datacenterId + ", Host #" 235 | + VmList.getById(getVmsCreatedList(), vmId).getHost().getId()); 236 | } else { 237 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Creation of VM #" + vmId 238 | + " failed in Datacenter #" + datacenterId); 239 | } 240 | 241 | incrementVmsAcks(); 242 | 243 | // all the requested VMs have been created 244 | if (getVmsCreatedList().size() == getVmList().size() - getVmsDestroyed()) { 245 | submitCloudlets(); 246 | } else { 247 | // all the acks received, but some VMs were not created 248 | if (getVmsRequested() == getVmsAcks()) { 249 | // find id of the next datacenter that has not been tried 250 | for (int nextDatacenterId : getDatacenterIdsList()) { 251 | if (!getDatacenterRequestedIdsList().contains(nextDatacenterId)) { 252 | createVmsInDatacenter(nextDatacenterId); 253 | return; 254 | } 255 | } 256 | 257 | // all datacenters already queried 258 | if (getVmsCreatedList().size() > 0) { // if some vm were created 259 | submitCloudlets(); 260 | } else { // no vms created. abort 261 | Log.printLine(CloudSim.clock() + ": " + getName() 262 | + ": none of the required VMs could be created. Aborting"); 263 | finishExecution(); 264 | } 265 | } 266 | } 267 | } 268 | 269 | /** 270 | * Process a cloudlet return event. 271 | * 272 | * @param ev a SimEvent object 273 | * @pre ev != $null 274 | * @post $none 275 | */ 276 | protected void processCloudletReturn(SimEvent ev) { 277 | Cloudlet cloudlet = (Cloudlet) ev.getData(); 278 | getCloudletReceivedList().add(cloudlet); 279 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloudlet " + cloudlet.getCloudletId() 280 | + " received"); 281 | cloudletsSubmitted--; 282 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { // all cloudlets executed 283 | Log.printLine(CloudSim.clock() + ": " + getName() + ": All Cloudlets executed. Finishing..."); 284 | clearDatacenters(); 285 | finishExecution(); 286 | } else { // some cloudlets haven't finished yet 287 | if (getCloudletList().size() > 0 && cloudletsSubmitted == 0) { 288 | // all the cloudlets sent finished. It means that some bount 289 | // cloudlet is waiting its VM be created 290 | clearDatacenters(); 291 | createVmsInDatacenter(0); 292 | } 293 | 294 | } 295 | } 296 | 297 | /** 298 | * Overrides this method when making a new and different type of Broker. This method is called 299 | * by {@link #body()} for incoming unknown tags. 300 | * 301 | * @param ev a SimEvent object 302 | * @pre ev != null 303 | * @post $none 304 | */ 305 | protected void processOtherEvent(SimEvent ev) { 306 | if (ev == null) { 307 | Log.printLine(getName() + ".processOtherEvent(): " + "Error - an event is null."); 308 | return; 309 | } 310 | 311 | Log.printLine(getName() + ".processOtherEvent(): " 312 | + "Error - event unknown by this DatacenterBroker."); 313 | } 314 | 315 | /** 316 | * Create the virtual machines in a datacenter. 317 | * 318 | * @param datacenterId Id of the chosen PowerDatacenter 319 | * @pre $none 320 | * @post $none 321 | */ 322 | protected void createVmsInDatacenter(int datacenterId) { 323 | // send as much vms as possible for this datacenter before trying the next one 324 | int requestedVms = 0; 325 | String datacenterName = CloudSim.getEntityName(datacenterId); 326 | for (Vm vm : getVmList()) { 327 | if (!getVmsToDatacentersMap().containsKey(vm.getId())) { 328 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() 329 | + " in " + datacenterName); 330 | sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); 331 | requestedVms++; 332 | } 333 | } 334 | 335 | getDatacenterRequestedIdsList().add(datacenterId); 336 | 337 | setVmsRequested(requestedVms); 338 | setVmsAcks(0); 339 | } 340 | 341 | /** 342 | * Submit cloudlets to the created VMs. 343 | * 344 | * @pre $none 345 | * @post $none 346 | */ 347 | /* 348 | * This function is modified to simulate the working of postponing cloudlets based on the dependency 349 | * 350 | */ 351 | 352 | protected void submitCloudlets() { 353 | int vmIndex = 0; 354 | int idx = 0; 355 | // for(Cloudlet cl: getCloudletList()) { 356 | // cl.setVmId((int) mapping[idx++]); 357 | // } 358 | for (Cloudlet cloudlet : getCloudletList()) { 359 | Vm vm; 360 | // if user didn't bind this cloudlet and it has not been executed yet 361 | if (cloudlet.getVmId() == -1) { 362 | //mapping is used to get vm mapping of cloudlet 363 | vm = getVmsCreatedList().get((int)mapping[idx]); 364 | } else { // submit to the specific vm 365 | vm = VmList.getById(getVmsCreatedList(), cloudlet.getVmId()); 366 | if (vm == null) { // vm was not created 367 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Postponing execution of cloudlet " 368 | + cloudlet.getCloudletId() + ": bount VM not available"); 369 | continue; 370 | } 371 | } 372 | 373 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Sending cloudlet " 374 | + cloudlet.getCloudletId() + " to VM #" + vm.getId()); 375 | cloudlet.setVmId(vm.getId()); 376 | //lag is sent to simulate waiting time of each cloudlet based on mapping 377 | send(getVmsToDatacentersMap().get(vm.getId()),lag[cloudlet.getCloudletId()], CloudSimTags.CLOUDLET_SUBMIT, cloudlet); 378 | //sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.CLOUDLET_SUBMIT, cloudlet); 379 | cloudletsSubmitted++; 380 | idx++; 381 | //vmIndex = (vmIndex + 1) % getVmsCreatedList().size(); 382 | getCloudletSubmittedList().add(cloudlet); 383 | } 384 | 385 | // remove submitted cloudlets from waiting list 386 | for (Cloudlet cloudlet : getCloudletSubmittedList()) { 387 | getCloudletList().remove(cloudlet); 388 | } 389 | } 390 | 391 | /** 392 | * Destroy the virtual machines running in datacenters. 393 | * 394 | * @pre $none 395 | * @post $none 396 | */ 397 | protected void clearDatacenters() { 398 | for (Vm vm : getVmsCreatedList()) { 399 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Destroying VM #" + vm.getId()); 400 | sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.VM_DESTROY, vm); 401 | } 402 | 403 | getVmsCreatedList().clear(); 404 | } 405 | 406 | /** 407 | * Send an internal event communicating the end of the simulation. 408 | * 409 | * @pre $none 410 | * @post $none 411 | */ 412 | protected void finishExecution() { 413 | sendNow(getId(), CloudSimTags.END_OF_SIMULATION); 414 | } 415 | 416 | /* 417 | * (non-Javadoc) 418 | * @see cloudsim.core.SimEntity#shutdownEntity() 419 | */ 420 | @Override 421 | public void shutdownEntity() { 422 | Log.printLine(getName() + " is shutting down..."); 423 | } 424 | 425 | /* 426 | * (non-Javadoc) 427 | * @see cloudsim.core.SimEntity#startEntity() 428 | */ 429 | @Override 430 | public void startEntity() { 431 | Log.printLine(getName() + " is starting..."); 432 | schedule(getId(), 0, CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST); 433 | } 434 | 435 | /** 436 | * Gets the vm list. 437 | * 438 | * @param the generic type 439 | * @return the vm list 440 | */ 441 | @SuppressWarnings("unchecked") 442 | public List getVmList() { 443 | return (List) vmList; 444 | } 445 | 446 | /** 447 | * Sets the vm list. 448 | * 449 | * @param the generic type 450 | * @param vmList the new vm list 451 | */ 452 | protected void setVmList(List vmList) { 453 | this.vmList = vmList; 454 | } 455 | 456 | /** 457 | * Gets the cloudlet list. 458 | * 459 | * @param the generic type 460 | * @return the cloudlet list 461 | */ 462 | @SuppressWarnings("unchecked") 463 | public List getCloudletList() { 464 | return (List) cloudletList; 465 | } 466 | 467 | /** 468 | * Sets the cloudlet list. 469 | * 470 | * @param the generic type 471 | * @param cloudletList the new cloudlet list 472 | */ 473 | protected void setCloudletList(List cloudletList) { 474 | this.cloudletList = cloudletList; 475 | } 476 | 477 | /** 478 | * Gets the cloudlet submitted list. 479 | * 480 | * @param the generic type 481 | * @return the cloudlet submitted list 482 | */ 483 | @SuppressWarnings("unchecked") 484 | public List getCloudletSubmittedList() { 485 | return (List) cloudletSubmittedList; 486 | } 487 | 488 | /** 489 | * Sets the cloudlet submitted list. 490 | * 491 | * @param the generic type 492 | * @param cloudletSubmittedList the new cloudlet submitted list 493 | */ 494 | protected void setCloudletSubmittedList(List cloudletSubmittedList) { 495 | this.cloudletSubmittedList = cloudletSubmittedList; 496 | } 497 | 498 | /** 499 | * Gets the cloudlet received list. 500 | * 501 | * @param the generic type 502 | * @return the cloudlet received list 503 | */ 504 | @SuppressWarnings("unchecked") 505 | public List getCloudletReceivedList() { 506 | return (List) cloudletReceivedList; 507 | } 508 | 509 | /** 510 | * Sets the cloudlet received list. 511 | * 512 | * @param the generic type 513 | * @param cloudletReceivedList the new cloudlet received list 514 | */ 515 | protected void setCloudletReceivedList(List cloudletReceivedList) { 516 | this.cloudletReceivedList = cloudletReceivedList; 517 | } 518 | 519 | /** 520 | * Gets the vm list. 521 | * 522 | * @param the generic type 523 | * @return the vm list 524 | */ 525 | @SuppressWarnings("unchecked") 526 | public List getVmsCreatedList() { 527 | return (List) vmsCreatedList; 528 | } 529 | 530 | /** 531 | * Sets the vm list. 532 | * 533 | * @param the generic type 534 | * @param vmsCreatedList the vms created list 535 | */ 536 | protected void setVmsCreatedList(List vmsCreatedList) { 537 | this.vmsCreatedList = vmsCreatedList; 538 | } 539 | 540 | /** 541 | * Gets the vms requested. 542 | * 543 | * @return the vms requested 544 | */ 545 | protected int getVmsRequested() { 546 | return vmsRequested; 547 | } 548 | 549 | /** 550 | * Sets the vms requested. 551 | * 552 | * @param vmsRequested the new vms requested 553 | */ 554 | protected void setVmsRequested(int vmsRequested) { 555 | this.vmsRequested = vmsRequested; 556 | } 557 | 558 | /** 559 | * Gets the vms acks. 560 | * 561 | * @return the vms acks 562 | */ 563 | protected int getVmsAcks() { 564 | return vmsAcks; 565 | } 566 | 567 | /** 568 | * Sets the vms acks. 569 | * 570 | * @param vmsAcks the new vms acks 571 | */ 572 | protected void setVmsAcks(int vmsAcks) { 573 | this.vmsAcks = vmsAcks; 574 | } 575 | 576 | /** 577 | * Increment vms acks. 578 | */ 579 | protected void incrementVmsAcks() { 580 | vmsAcks++; 581 | } 582 | 583 | /** 584 | * Gets the vms destroyed. 585 | * 586 | * @return the vms destroyed 587 | */ 588 | protected int getVmsDestroyed() { 589 | return vmsDestroyed; 590 | } 591 | 592 | /** 593 | * Sets the vms destroyed. 594 | * 595 | * @param vmsDestroyed the new vms destroyed 596 | */ 597 | protected void setVmsDestroyed(int vmsDestroyed) { 598 | this.vmsDestroyed = vmsDestroyed; 599 | } 600 | 601 | /** 602 | * Gets the datacenter ids list. 603 | * 604 | * @return the datacenter ids list 605 | */ 606 | protected List getDatacenterIdsList() { 607 | return datacenterIdsList; 608 | } 609 | 610 | /** 611 | * Sets the datacenter ids list. 612 | * 613 | * @param datacenterIdsList the new datacenter ids list 614 | */ 615 | protected void setDatacenterIdsList(List datacenterIdsList) { 616 | this.datacenterIdsList = datacenterIdsList; 617 | } 618 | 619 | /** 620 | * Gets the vms to datacenters map. 621 | * 622 | * @return the vms to datacenters map 623 | */ 624 | protected Map getVmsToDatacentersMap() { 625 | return vmsToDatacentersMap; 626 | } 627 | 628 | /** 629 | * Sets the vms to datacenters map. 630 | * 631 | * @param vmsToDatacentersMap the vms to datacenters map 632 | */ 633 | protected void setVmsToDatacentersMap(Map vmsToDatacentersMap) { 634 | this.vmsToDatacentersMap = vmsToDatacentersMap; 635 | } 636 | 637 | /** 638 | * Gets the datacenter characteristics list. 639 | * 640 | * @return the datacenter characteristics list 641 | */ 642 | protected Map getDatacenterCharacteristicsList() { 643 | return datacenterCharacteristicsList; 644 | } 645 | 646 | /** 647 | * Sets the datacenter characteristics list. 648 | * 649 | * @param datacenterCharacteristicsList the datacenter characteristics list 650 | */ 651 | protected void setDatacenterCharacteristicsList( 652 | Map datacenterCharacteristicsList) { 653 | this.datacenterCharacteristicsList = datacenterCharacteristicsList; 654 | } 655 | 656 | /** 657 | * Gets the datacenter requested ids list. 658 | * 659 | * @return the datacenter requested ids list 660 | */ 661 | protected List getDatacenterRequestedIdsList() { 662 | return datacenterRequestedIdsList; 663 | } 664 | 665 | /** 666 | * Sets the datacenter requested ids list. 667 | * 668 | * @param datacenterRequestedIdsList the new datacenter requested ids list 669 | */ 670 | protected void setDatacenterRequestedIdsList(List datacenterRequestedIdsList) { 671 | this.datacenterRequestedIdsList = datacenterRequestedIdsList; 672 | } 673 | 674 | public void submitMapping(double[] psoMapping) { 675 | mapping = psoMapping; 676 | } 677 | public void submitDelay(double[] delay) { 678 | lag=delay; 679 | } 680 | public List assignCloudletsToDC(List cloudlist) { 681 | // double[] mapping = (new PSO()).run(); 682 | int idx = 0; 683 | for(Cloudlet cl: cloudlist) { 684 | cl.setVmId((int) mapping[idx++]); 685 | } 686 | return cloudlist; 687 | } 688 | 689 | } 690 | 691 | -------------------------------------------------------------------------------- /MultiSwarmPSOCloudTest/PSO.java: -------------------------------------------------------------------------------- 1 | package MultiSwarmPSOCloudTest; 2 | 3 | import MultiSwarmPSOCloudTest.*; 4 | import net.sourceforge.jswarm_pso.*; 5 | 6 | public class PSO { 7 | /* 8 | * Global Parameters: 9 | * ff : represents the fitness function used by all particles in the swarm 10 | * swarm : represents the different swarms that are used in multiswarm PSO 11 | * particles[i][j] : represents the particles where i is the swarm id and j is particle id 12 | * other parameters are defined earlier 13 | */ 14 | ParticlePSO particles[][]; 15 | static int minimum=0; 16 | FitnessFunctionPSO ff; 17 | Swarm swarm[] = new Swarm[Constants.NoOfSwarms]; 18 | int[] tasklength;int[] outputfilesize;int[] mips;double[] execcost,waitcost; 19 | int[][] graph; 20 | public PSO(int[] tasklength,int[] outputfilesize,int[] mips,double[] execcost,double[] waitcost,int[][] graph) 21 | { 22 | this.tasklength=tasklength; 23 | this.outputfilesize=outputfilesize; 24 | this.mips=mips; 25 | this.execcost=execcost; 26 | this.graph=graph; 27 | this.waitcost=waitcost; 28 | ff = new FitnessFunctionPSO(execcost,waitcost,mips,outputfilesize,tasklength,graph); 29 | for(int q=0;q0?1.0:-1.0; 26 | } 27 | setPosition(position); 28 | setVelocity(velocity); 29 | } 30 | public String toString() { 31 | String output = ""; 32 | for(int i=0;i cloudletList; 34 | 35 | /** The vmlist. */ 36 | private static List vmlist; 37 | 38 | /* 39 | * mapping: represents the mapping of cloudlets to vm based on MultiSwarmPSO 40 | * mapping2: represents the mapping of cloudlets to vm based on Random Scheduling 41 | * resultcost: stores the cost of cloudlet execution based on the mappings 42 | * other parameters are defined in FitnessFunction 43 | */ 44 | public static double mapping[]; 45 | public static double[][] executiontimematrix; 46 | public static double[][] communicationtimematrix; 47 | public static double[][] datatransfermatrix; 48 | public static double[][] taskoutputfilematrix; 49 | public static double[][] commcost; 50 | public static double[] mapping2 = new double[Constants.NoOfTasks]; 51 | public static int depgraph[][] = new int[Constants.NoOfTasks][Constants.NoOfTasks]; 52 | 53 | 54 | public static double[] resultcost= new double[2]; 55 | 56 | public double[] getPSOMapping() { 57 | return mapping; 58 | } 59 | 60 | /* 61 | * This function is used to create shell script which will run scripts of respective vm 62 | * for both the mappings 63 | * 64 | */ 65 | 66 | public static void createvmrunscript(int vmno)throws Exception{ 67 | FileWriter fw = new FileWriter("C:\\Users\\admin\\Desktop\\psobroker\\pso\\runpsovm-"+vmno+".sh"); 68 | BufferedWriter bw = new BufferedWriter(fw); 69 | PrintWriter pw = new PrintWriter(bw); 70 | pw.println("#!/bin/bash"); 71 | pw.println("sh vm-"+vmno+".sh"); 72 | pw.close(); 73 | bw.close(); 74 | fw.close(); 75 | FileWriter fw1 = new FileWriter("C:\\Users\\admin\\Desktop\\psobroker\\random\\runrandomvm-"+vmno+".sh"); 76 | BufferedWriter bw1 = new BufferedWriter(fw1); 77 | PrintWriter pw1 = new PrintWriter(bw1); 78 | pw1.println("#!bin/bash"); 79 | pw1.println("sh vm-"+vmno+".sh"); 80 | pw1.close(); 81 | bw1.close(); 82 | fw1.close(); 83 | } 84 | /* 85 | * This function creates shell scripts to run java programs if all the dependent output 86 | * files are present. It calculates the time to execute the program for each vm as well. 87 | * It also stores the output in the corresponding output file as well. 88 | * 89 | */ 90 | public static void createscript(double mapping[],String Folder)throws Exception{ 91 | int no_of_vms=Constants.NoOfVMs; 92 | for(int i=0;i> ans_p"+(int)j+".txt"); 112 | } 113 | } 114 | pw.println("stop=$(date +\"%T\")"); 115 | pw.println("echo \"Start Time : $start\""); 116 | pw.println("echo \"Stop Time : $stop\""); 117 | pw.close(); 118 | bw.close(); 119 | fw.close(); 120 | } 121 | } 122 | 123 | /* 124 | * This function is used for the simulation of Cloud Scenarios using CloudSim. 125 | */ 126 | public static double[] func(int[] tasklength,int[] outputfilesize,int[] mips,double[] execcost,double[] waitcost,int[][] graph) throws Exception { 127 | /* 128 | * Depgraph denotes that a task requires output files from which tasks 129 | */ 130 | for(int j=0;j(); 174 | 175 | //VM description 176 | int vmid = 0; 177 | long size = 10000; //image size (MB) 178 | int ram = 256; //vm memory (MB) 179 | long bw = 1000; 180 | int pesNumber = 1; //number of cpus 181 | String vmm = "Xen"; //VMM name 182 | 183 | //create two VMs 184 | for(int i=0;i(); 197 | 198 | //Cloudlet properties 199 | 200 | long fileSize = 300; 201 | UtilizationModel utilizationModel = new UtilizationModelFull(); 202 | for(int id=0;id newList = broker.getCloudletReceivedList(); 232 | 233 | CloudSim.stopSimulation(); 234 | 235 | printCloudletList(newList); 236 | 237 | resultcost[0]=PSOScheduler.printBestFitness(); 238 | Log.printLine("Simulation of Task Scheduler using PSO is finished!"); 239 | for(int i=0;i(); 285 | 286 | //VM description 287 | int vmid = 0; 288 | 289 | long size = 10000; //image size (MB) 290 | int ram = 256; //vm memory (MB) 291 | long bw = 1000; 292 | int pesNumber = 1; //number of cpus 293 | String vmm = "Xen"; //VMM name 294 | 295 | //create two VMs 296 | for(int i=0;i(); 309 | 310 | //Cloudlet properties 311 | //This is the mips rating(Million Instructions per second which are processed) of each VM that is being called 312 | //This is the cost of execution on different VM per unit time. 313 | 314 | UtilizationModel utilizationModel = new UtilizationModelFull(); 315 | for(int i=0;i newList2 = broker.getCloudletReceivedList(); 378 | 379 | CloudSim.stopSimulation(); 380 | 381 | printCloudletList(newList2); 382 | 383 | resultcost[1]=cost; 384 | Log.printLine("Simulation of Task Scheduler using Random scheduling is finished!"); 385 | } 386 | catch (Exception e) { 387 | e.printStackTrace(); 388 | Log.printLine("The simulation has been terminated due to an unexpected error"); 389 | } 390 | 391 | } 392 | 393 | 394 | 395 | private static Datacenter createDatacenter(String name){ 396 | 397 | // Here are the steps needed to create a PowerDatacenter: 398 | // 1. We need to create a list to store 399 | // our machine 400 | List hostList = new ArrayList(); 401 | 402 | // 2. A Machine contains one or more PEs or CPUs/Cores. 403 | // In this example, it will have only one core. 404 | List peList = new ArrayList(); 405 | 406 | int mips = 1000; 407 | 408 | // 3. Create PEs and add these into a list. 409 | peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating 410 | 411 | //4. Create Hosts with its id and list of PEs and add them to the list of machines 412 | int hostId=0; 413 | int ram = 2048; //host memory (MB) 414 | long storage = 1000000; //host storage 415 | int bw = 10000; 416 | 417 | hostList.add( 418 | new Host( 419 | hostId, 420 | new RamProvisionerSimple(ram), 421 | new BwProvisionerSimple(bw), 422 | storage, 423 | peList, 424 | new VmSchedulerTimeShared(peList) 425 | ) 426 | ); // This is our first machine 427 | 428 | //create another machine in the Data center 429 | List peList2 = new ArrayList(); 430 | 431 | peList2.add(new Pe(0, new PeProvisionerSimple(mips))); 432 | 433 | hostId++; 434 | 435 | hostList.add( 436 | new Host( 437 | hostId, 438 | new RamProvisionerSimple(ram), 439 | new BwProvisionerSimple(bw), 440 | storage, 441 | peList2, 442 | new VmSchedulerTimeShared(peList2) 443 | ) 444 | ); // This is our second machine 445 | 446 | 447 | 448 | // 5. Create a DatacenterCharacteristics object that stores the 449 | // properties of a data center: architecture, OS, list of 450 | // Machines, allocation policy: time- or space-shared, time zone 451 | // and its price (G$/Pe time unit). 452 | String arch = "x86"; // system architecture 453 | String os = "Linux"; // operating system 454 | String vmm = "Xen"; 455 | double time_zone = 10.0; // time zone this resource located 456 | double cost = 3.0; // the cost of using processing in this resource 457 | double costPerMem = 0.05; // the cost of using memory in this resource 458 | double costPerStorage = 0.001; // the cost of using storage in this resource 459 | double costPerBw = 0.0; // the cost of using bw in this resource 460 | LinkedList storageList = new LinkedList(); //we are not adding SAN devices by now 461 | 462 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 463 | arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw); 464 | 465 | // 6. Finally, we need to create a PowerDatacenter object. 466 | Datacenter datacenter = null; 467 | try { 468 | datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0); 469 | } catch (Exception e) { 470 | e.printStackTrace(); 471 | } 472 | 473 | return datacenter; 474 | } 475 | 476 | // Broker policy using PSO 477 | private static MyDataCenterBroker createBroker(){ 478 | 479 | MyDataCenterBroker broker = null; 480 | try { 481 | broker = new MyDataCenterBroker("Broker"); 482 | } catch (Exception e) { 483 | e.printStackTrace(); 484 | return null; 485 | } 486 | return broker; 487 | } 488 | /** 489 | * Prints the Cloudlet objects 490 | * @param list list of Cloudlets 491 | */ 492 | private static void printCloudletList(List list) { 493 | int size = list.size(); 494 | Cloudlet cloudlet; 495 | 496 | String indent = " "; 497 | Log.printLine(); 498 | Log.printLine("========== OUTPUT =========="); 499 | Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + 500 | "Data center ID" + indent + "VM ID" + indent + "Time" + indent + "Start Time" + indent + "Finish Time"); 501 | 502 | DecimalFormat dft = new DecimalFormat("###.##"); 503 | for (int i = 0; i < size; i++) { 504 | cloudlet = list.get(i); 505 | Log.print(indent + cloudlet.getCloudletId() + indent + indent); 506 | 507 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){ 508 | Log.print("SUCCESS"); 509 | 510 | Log.printLine( indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId() + 511 | indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent + indent + dft.format(cloudlet.getExecStartTime())+ 512 | indent + indent + dft.format(cloudlet.getFinishTime())); 513 | } 514 | } 515 | 516 | } 517 | } 518 | -------------------------------------------------------------------------------- /PSOCEC2013/Constants.java: -------------------------------------------------------------------------------- 1 | package PSOCEC2013; 2 | 3 | /* 4 | * Class Name : Constants 5 | * Purpose : Serves as a reference for changing the parameters for the PSO Algorithm. 6 | * 7 | */ 8 | public class Constants { 9 | /* 10 | * Here number of tasks represents the number of dimensions in a particle 11 | * 12 | */ 13 | public static final int NoOfTasks = 5; 14 | public static final int NoOfParticles = 30; 15 | public static final int NoOfIterations = 1500; 16 | } 17 | -------------------------------------------------------------------------------- /PSOCEC2013/FitnessFunctionPSO.java: -------------------------------------------------------------------------------- 1 | package PSOCEC2013; 2 | 3 | import net.sourceforge.jswarm_pso.*; 4 | 5 | /* 6 | * Class Name: Fitness Function 7 | * Purpose: It provides the function used to test the convergence of our PSO Algorithm 8 | * 9 | */ 10 | public class FitnessFunctionPSO extends FitnessFunction{ 11 | /* 12 | * Global Parameters: 13 | * funcno : refers to the function number corresponding to different functions in CEC-2013 14 | * 15 | */ 16 | int funcno; 17 | FitnessFunctionPSO(int funcno) 18 | { 19 | /* 20 | * Here false in super denotes that our objective is to minimize the function. 21 | */ 22 | super(false); 23 | this.funcno=funcno; 24 | } 25 | 26 | /* 27 | * evaluate is the function which returns the fitness value of the position 28 | * the fitness value of the position is returned in f[0] 29 | */ 30 | public double evaluate(double[] position) { 31 | testfunc tf = new testfunc(); 32 | double f[]= {0.0,0.0}; 33 | 34 | try { 35 | tf.test_func(position,f, Constants.NoOfTasks, 1, funcno); 36 | } catch (Exception e) { 37 | // TODO Auto-generated catch block 38 | e.printStackTrace(); 39 | } 40 | return f[0]; 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /PSOCEC2013/MYPSO.java: -------------------------------------------------------------------------------- 1 | package PSOCEC2013; 2 | 3 | import java.util.Arrays; 4 | 5 | import net.sourceforge.jswarm_pso.*; 6 | public class MYPSO { 7 | FitnessFunctionPSO ff ; 8 | Swarm swarm; 9 | private static ParticlePSO particles[]; 10 | static double error[][][] = new double[28][51][11]; 11 | static double multiply[]={0.01,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}; 12 | double ansfitness[]={-1400,-1300,-1200,-1100,-1000,-900,-800,-700,-600,-500,-400,-300,-200,-100,100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400}; 13 | 14 | int funcno; 15 | int counter; 16 | public MYPSO(int funcno,int run) 17 | { 18 | this.funcno=funcno; 19 | this.counter=counter; 20 | ff = new FitnessFunctionPSO(funcno); 21 | swarm = new Swarm(Constants.NoOfParticles, new ParticlePSO(), ff); 22 | initializeParticles(); 23 | } 24 | public void initializeParticles() { 25 | particles = new ParticlePSO[Constants.NoOfParticles]; 26 | for(int i=0;i Standard Velocity Update Function 50 | * 2 -> Velocity Update using Modified Inertia Weight 51 | * 3 -> Velocity Update using Constriction Factor 52 | * 4 -> Velocity Update using both Inertia Weight and Constriction Factor 53 | */ 54 | /* 55 | * Uncomment the update function that is required to change the update function. 56 | */ 57 | // 1. 58 | v[i]=0.729844*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i]); 59 | // 2. 60 | // v[i]=w*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i]); 61 | // 3. 62 | // v[i]=k*(v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i])); 63 | // 4. 64 | // v[i]=k*(w*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i])); 65 | 66 | particle.setVelocity(v); 67 | x[i]=(x[i]+v[i]); 68 | particle.setPosition(x); 69 | } 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /PSOCEC2013/PSO.java: -------------------------------------------------------------------------------- 1 | package PSOCEC2013; 2 | 3 | import java.io.*; 4 | /* 5 | * Class Name: PSO 6 | * Purpose : Used for running the PSO simulation 7 | * 8 | */ 9 | import java.util.Arrays; 10 | 11 | import net.sourceforge.jswarm_pso.*; 12 | public class PSO { 13 | /* 14 | * Global Parameters: 15 | * ff : represents the fitness function used by all particles in the swarm 16 | * swarm : represents the swarms that are used in PSO 17 | * particles[i] : represents the particles where i is particle id 18 | * error[][][] : stores the error for all 28 fitness function 19 | * ansplot[][][] : stores the fitness value for all 28 fitness function 20 | * ansfitness[] : represents the best possible fitness value of the function (minimum) 21 | * funcno : stores the function number of CEC function to be checked 22 | */ 23 | FitnessFunctionPSO ff ; 24 | Swarm swarm; 25 | private static ParticlePSO particles[]; 26 | static double error[][][] = new double[28][7][11]; 27 | static double ansplot[][][] = new double[28][7][11]; 28 | static double multiply[]={0.01,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}; 29 | double ansfitness[]={-1400,-1300,-1200,-1100,-1000,-900,-800,-700,-600,-500,-400,-300,-200,-100,100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400}; 30 | 31 | int funcno; 32 | int counter; 33 | 34 | public PSO(int funcno,int counter) 35 | { 36 | this.funcno=funcno; 37 | this.counter=counter; 38 | ff = new FitnessFunctionPSO(funcno); 39 | swarm = new Swarm(Constants.NoOfParticles, new ParticlePSO(), ff); 40 | initializeParticles(); 41 | } 42 | 43 | public void initializeParticles() { 44 | particles = new ParticlePSO[Constants.NoOfParticles]; 45 | for(int i=0;i Standard Velocity Update Function 51 | * 2 -> Velocity Update using Modified Inertia Weight 52 | * 3 -> Velocity Update using Constriction Factor 53 | * 4 -> Velocity Update using both Inertia Weight and Constriction Factor 54 | */ 55 | /* 56 | * Uncomment the update function that is required to change the update function. 57 | */ 58 | // 1. 59 | v[i]=0.729844*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i]); 60 | // 2. 61 | // v[i]=w*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i]); 62 | // 3. 63 | // v[i]=k*(v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i])); 64 | // 4. 65 | // v[i]=k*(w*v[i]+2*Math.random()*(pbest[i]-x[i])+2*Math.random()*(gbest[i]-x[i])); 66 | 67 | particle.setVelocity(v); 68 | x[i]=(x[i]+v[i]); 69 | particle.setPosition(x); 70 | } 71 | 72 | } 73 | } -------------------------------------------------------------------------------- /PSOCloud/MyDataCenterBroker.java: -------------------------------------------------------------------------------- 1 | package PSOCloud; 2 | 3 | /* 4 | * Title: CloudSim Toolkit 5 | * Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds 6 | * Licence: GPL - http://www.gnu.org/copyleft/gpl.html 7 | * 8 | * Copyright (c) 2009-2012, The University of Melbourne, Australia 9 | */ 10 | 11 | import java.util.ArrayList; 12 | import java.util.HashMap; 13 | import java.util.LinkedList; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | import org.cloudbus.cloudsim.Cloudlet; 18 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 19 | import org.cloudbus.cloudsim.Vm; 20 | import org.cloudbus.cloudsim.core.CloudSim; 21 | import org.cloudbus.cloudsim.core.CloudSimTags; 22 | import org.cloudbus.cloudsim.core.SimEntity; 23 | import org.cloudbus.cloudsim.core.SimEvent; 24 | import org.cloudbus.cloudsim.lists.CloudletList; 25 | import org.cloudbus.cloudsim.lists.VmList; 26 | import org.cloudbus.cloudsim.Log; 27 | 28 | /** 29 | * DatacentreBroker represents a broker acting on behalf of a user. It hides VM management, as vm 30 | * creation, sumbission of cloudlets to this VMs and destruction of VMs. 31 | * 32 | * @author Rodrigo N. Calheiros 33 | * @author Anton Beloglazov 34 | * @since CloudSim Toolkit 1.0 35 | */ 36 | public class MyDataCenterBroker extends SimEntity { 37 | 38 | private double[] mapping; 39 | private double[] lag; 40 | /** The vm list. */ 41 | protected List vmList; 42 | 43 | /** The vms created list. */ 44 | protected List vmsCreatedList; 45 | 46 | /** The cloudlet list. */ 47 | protected List cloudletList; 48 | 49 | /** The cloudlet submitted list. */ 50 | protected List cloudletSubmittedList; 51 | 52 | /** The cloudlet received list. */ 53 | protected List cloudletReceivedList; 54 | 55 | /** The cloudlets submitted. */ 56 | protected int cloudletsSubmitted; 57 | 58 | /** The vms requested. */ 59 | protected int vmsRequested; 60 | 61 | /** The vms acks. */ 62 | protected int vmsAcks; 63 | 64 | /** The vms destroyed. */ 65 | protected int vmsDestroyed; 66 | 67 | /** The datacenter ids list. */ 68 | protected List datacenterIdsList; 69 | 70 | /** The datacenter requested ids list. */ 71 | protected List datacenterRequestedIdsList; 72 | 73 | /** The vms to datacenters map. */ 74 | protected Map vmsToDatacentersMap; 75 | 76 | /** The datacenter characteristics list. */ 77 | protected Map datacenterCharacteristicsList; 78 | 79 | /** 80 | * Created a new DatacenterBroker object. 81 | * 82 | * @param name name to be associated with this entity (as required by Sim_entity class from 83 | * simjava package) 84 | * @throws Exception the exception 85 | * @pre name != null 86 | * @post $none 87 | */ 88 | public MyDataCenterBroker(String name) throws Exception { 89 | super(name); 90 | 91 | setVmList(new ArrayList()); 92 | setVmsCreatedList(new ArrayList()); 93 | setCloudletList(new ArrayList()); 94 | setCloudletSubmittedList(new ArrayList()); 95 | setCloudletReceivedList(new ArrayList()); 96 | 97 | cloudletsSubmitted = 0; 98 | setVmsRequested(0); 99 | setVmsAcks(0); 100 | setVmsDestroyed(0); 101 | 102 | setDatacenterIdsList(new LinkedList()); 103 | setDatacenterRequestedIdsList(new ArrayList()); 104 | setVmsToDatacentersMap(new HashMap()); 105 | setDatacenterCharacteristicsList(new HashMap()); 106 | } 107 | 108 | /** 109 | * This method is used to send to the broker the list with virtual machines that must be 110 | * created. 111 | * 112 | * @param list the list 113 | * @pre list !=null 114 | * @post $none 115 | */ 116 | public void submitVmList(List list) { 117 | getVmList().addAll(list); 118 | } 119 | 120 | /** 121 | * This method is used to send to the broker the list of cloudlets. 122 | * 123 | * @param list the list 124 | * @pre list !=null 125 | * @post $none 126 | */ 127 | public void submitCloudletList(List list) { 128 | getCloudletList().addAll(list); 129 | } 130 | 131 | /** 132 | * Specifies that a given cloudlet must run in a specific virtual machine. 133 | * 134 | * @param cloudletId ID of the cloudlet being bount to a vm 135 | * @param vmId the vm id 136 | * @pre cloudletId > 0 137 | * @pre id > 0 138 | * @post $none 139 | */ 140 | public void bindCloudletToVm(int cloudletId, int vmId) { 141 | CloudletList.getById(getCloudletList(), cloudletId).setVmId(vmId); 142 | } 143 | 144 | /** 145 | * Processes events available for this Broker. 146 | * 147 | * @param ev a SimEvent object 148 | * @pre ev != null 149 | * @post $none 150 | */ 151 | @Override 152 | public void processEvent(SimEvent ev) { 153 | switch (ev.getTag()) { 154 | // Resource characteristics request 155 | case CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST: 156 | processResourceCharacteristicsRequest(ev); 157 | break; 158 | // Resource characteristics answer 159 | case CloudSimTags.RESOURCE_CHARACTERISTICS: 160 | processResourceCharacteristics(ev); 161 | break; 162 | // VM Creation answer 163 | case CloudSimTags.VM_CREATE_ACK: 164 | processVmCreate(ev); 165 | break; 166 | // A finished cloudlet returned 167 | case CloudSimTags.CLOUDLET_RETURN: 168 | processCloudletReturn(ev); 169 | break; 170 | // if the simulation finishes 171 | case CloudSimTags.END_OF_SIMULATION: 172 | shutdownEntity(); 173 | break; 174 | // other unknown tags are processed by this method 175 | default: 176 | processOtherEvent(ev); 177 | break; 178 | } 179 | } 180 | 181 | /** 182 | * Process the return of a request for the characteristics of a PowerDatacenter. 183 | * 184 | * @param ev a SimEvent object 185 | * @pre ev != $null 186 | * @post $none 187 | */ 188 | protected void processResourceCharacteristics(SimEvent ev) { 189 | DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData(); 190 | getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics); 191 | 192 | if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size()) { 193 | setDatacenterRequestedIdsList(new ArrayList()); 194 | createVmsInDatacenter(getDatacenterIdsList().get(0)); 195 | } 196 | } 197 | 198 | /** 199 | * Process a request for the characteristics of a PowerDatacenter. 200 | * 201 | * @param ev a SimEvent object 202 | * @pre ev != $null 203 | * @post $none 204 | */ 205 | protected void processResourceCharacteristicsRequest(SimEvent ev) { 206 | setDatacenterIdsList(CloudSim.getCloudResourceList()); 207 | setDatacenterCharacteristicsList(new HashMap()); 208 | 209 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloud Resource List received with " 210 | + getDatacenterIdsList().size() + " resource(s)"); 211 | 212 | for (Integer datacenterId : getDatacenterIdsList()) { 213 | sendNow(datacenterId, CloudSimTags.RESOURCE_CHARACTERISTICS, getId()); 214 | } 215 | } 216 | 217 | /** 218 | * Process the ack received due to a request for VM creation. 219 | * 220 | * @param ev a SimEvent object 221 | * @pre ev != null 222 | * @post $none 223 | */ 224 | protected void processVmCreate(SimEvent ev) { 225 | int[] data = (int[]) ev.getData(); 226 | int datacenterId = data[0]; 227 | int vmId = data[1]; 228 | int result = data[2]; 229 | 230 | if (result == CloudSimTags.TRUE) { 231 | getVmsToDatacentersMap().put(vmId, datacenterId); 232 | getVmsCreatedList().add(VmList.getById(getVmList(), vmId)); 233 | Log.printLine(CloudSim.clock() + ": " + getName() + ": VM #" + vmId 234 | + " has been created in Datacenter #" + datacenterId + ", Host #" 235 | + VmList.getById(getVmsCreatedList(), vmId).getHost().getId()); 236 | } else { 237 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Creation of VM #" + vmId 238 | + " failed in Datacenter #" + datacenterId); 239 | } 240 | 241 | incrementVmsAcks(); 242 | 243 | // all the requested VMs have been created 244 | if (getVmsCreatedList().size() == getVmList().size() - getVmsDestroyed()) { 245 | submitCloudlets(); 246 | } else { 247 | // all the acks received, but some VMs were not created 248 | if (getVmsRequested() == getVmsAcks()) { 249 | // find id of the next datacenter that has not been tried 250 | for (int nextDatacenterId : getDatacenterIdsList()) { 251 | if (!getDatacenterRequestedIdsList().contains(nextDatacenterId)) { 252 | createVmsInDatacenter(nextDatacenterId); 253 | return; 254 | } 255 | } 256 | 257 | // all datacenters already queried 258 | if (getVmsCreatedList().size() > 0) { // if some vm were created 259 | submitCloudlets(); 260 | } else { // no vms created. abort 261 | Log.printLine(CloudSim.clock() + ": " + getName() 262 | + ": none of the required VMs could be created. Aborting"); 263 | finishExecution(); 264 | } 265 | } 266 | } 267 | } 268 | 269 | /** 270 | * Process a cloudlet return event. 271 | * 272 | * @param ev a SimEvent object 273 | * @pre ev != $null 274 | * @post $none 275 | */ 276 | protected void processCloudletReturn(SimEvent ev) { 277 | Cloudlet cloudlet = (Cloudlet) ev.getData(); 278 | getCloudletReceivedList().add(cloudlet); 279 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloudlet " + cloudlet.getCloudletId() 280 | + " received"); 281 | cloudletsSubmitted--; 282 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { // all cloudlets executed 283 | Log.printLine(CloudSim.clock() + ": " + getName() + ": All Cloudlets executed. Finishing..."); 284 | clearDatacenters(); 285 | finishExecution(); 286 | } else { // some cloudlets haven't finished yet 287 | if (getCloudletList().size() > 0 && cloudletsSubmitted == 0) { 288 | // all the cloudlets sent finished. It means that some bount 289 | // cloudlet is waiting its VM be created 290 | clearDatacenters(); 291 | createVmsInDatacenter(0); 292 | } 293 | 294 | } 295 | } 296 | 297 | /** 298 | * Overrides this method when making a new and different type of Broker. This method is called 299 | * by {@link #body()} for incoming unknown tags. 300 | * 301 | * @param ev a SimEvent object 302 | * @pre ev != null 303 | * @post $none 304 | */ 305 | protected void processOtherEvent(SimEvent ev) { 306 | if (ev == null) { 307 | Log.printLine(getName() + ".processOtherEvent(): " + "Error - an event is null."); 308 | return; 309 | } 310 | 311 | Log.printLine(getName() + ".processOtherEvent(): " 312 | + "Error - event unknown by this DatacenterBroker."); 313 | } 314 | 315 | /** 316 | * Create the virtual machines in a datacenter. 317 | * 318 | * @param datacenterId Id of the chosen PowerDatacenter 319 | * @pre $none 320 | * @post $none 321 | */ 322 | protected void createVmsInDatacenter(int datacenterId) { 323 | // send as much vms as possible for this datacenter before trying the next one 324 | int requestedVms = 0; 325 | String datacenterName = CloudSim.getEntityName(datacenterId); 326 | for (Vm vm : getVmList()) { 327 | if (!getVmsToDatacentersMap().containsKey(vm.getId())) { 328 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() 329 | + " in " + datacenterName); 330 | sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); 331 | requestedVms++; 332 | } 333 | } 334 | 335 | getDatacenterRequestedIdsList().add(datacenterId); 336 | 337 | setVmsRequested(requestedVms); 338 | setVmsAcks(0); 339 | } 340 | 341 | /** 342 | * Submit cloudlets to the created VMs. 343 | * 344 | * @pre $none 345 | * @post $none 346 | */ 347 | /* 348 | * This function is modified to simulate the working of postponing cloudlets based on the dependency 349 | * 350 | */ 351 | protected void submitCloudlets() { 352 | int vmIndex = 0; 353 | int idx = 0; 354 | // for(Cloudlet cl: getCloudletList()) { 355 | // cl.setVmId((int) mapping[idx++]); 356 | // } 357 | for (Cloudlet cloudlet : getCloudletList()) { 358 | Vm vm; 359 | // if user didn't bind this cloudlet and it has not been executed yet 360 | if (cloudlet.getVmId() == -1) { 361 | //mapping is used to get vm mapping of cloudlet 362 | vm = getVmsCreatedList().get((int)mapping[idx]); 363 | } else { // submit to the specific vm 364 | vm = VmList.getById(getVmsCreatedList(), cloudlet.getVmId()); 365 | if (vm == null) { // vm was not created 366 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Postponing execution of cloudlet " 367 | + cloudlet.getCloudletId() + ": bount VM not available"); 368 | continue; 369 | } 370 | } 371 | 372 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Sending cloudlet " 373 | + cloudlet.getCloudletId() + " to VM #" + vm.getId()); 374 | cloudlet.setVmId(vm.getId()); 375 | //lag is sent to simulate waiting time of each cloudlet based on mapping 376 | send(getVmsToDatacentersMap().get(vm.getId()),lag[cloudlet.getCloudletId()], CloudSimTags.CLOUDLET_SUBMIT, cloudlet); 377 | //sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.CLOUDLET_SUBMIT, cloudlet); 378 | cloudletsSubmitted++; 379 | idx++; 380 | //vmIndex = (vmIndex + 1) % getVmsCreatedList().size(); 381 | getCloudletSubmittedList().add(cloudlet); 382 | } 383 | 384 | // remove submitted cloudlets from waiting list 385 | for (Cloudlet cloudlet : getCloudletSubmittedList()) { 386 | getCloudletList().remove(cloudlet); 387 | } 388 | } 389 | 390 | /** 391 | * Destroy the virtual machines running in datacenters. 392 | * 393 | * @pre $none 394 | * @post $none 395 | */ 396 | protected void clearDatacenters() { 397 | for (Vm vm : getVmsCreatedList()) { 398 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Destroying VM #" + vm.getId()); 399 | sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.VM_DESTROY, vm); 400 | } 401 | 402 | getVmsCreatedList().clear(); 403 | } 404 | 405 | /** 406 | * Send an internal event communicating the end of the simulation. 407 | * 408 | * @pre $none 409 | * @post $none 410 | */ 411 | protected void finishExecution() { 412 | sendNow(getId(), CloudSimTags.END_OF_SIMULATION); 413 | } 414 | 415 | /* 416 | * (non-Javadoc) 417 | * @see cloudsim.core.SimEntity#shutdownEntity() 418 | */ 419 | @Override 420 | public void shutdownEntity() { 421 | Log.printLine(getName() + " is shutting down..."); 422 | } 423 | 424 | /* 425 | * (non-Javadoc) 426 | * @see cloudsim.core.SimEntity#startEntity() 427 | */ 428 | @Override 429 | public void startEntity() { 430 | Log.printLine(getName() + " is starting..."); 431 | schedule(getId(), 0, CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST); 432 | } 433 | 434 | /** 435 | * Gets the vm list. 436 | * 437 | * @param the generic type 438 | * @return the vm list 439 | */ 440 | @SuppressWarnings("unchecked") 441 | public List getVmList() { 442 | return (List) vmList; 443 | } 444 | 445 | /** 446 | * Sets the vm list. 447 | * 448 | * @param the generic type 449 | * @param vmList the new vm list 450 | */ 451 | protected void setVmList(List vmList) { 452 | this.vmList = vmList; 453 | } 454 | 455 | /** 456 | * Gets the cloudlet list. 457 | * 458 | * @param the generic type 459 | * @return the cloudlet list 460 | */ 461 | @SuppressWarnings("unchecked") 462 | public List getCloudletList() { 463 | return (List) cloudletList; 464 | } 465 | 466 | /** 467 | * Sets the cloudlet list. 468 | * 469 | * @param the generic type 470 | * @param cloudletList the new cloudlet list 471 | */ 472 | protected void setCloudletList(List cloudletList) { 473 | this.cloudletList = cloudletList; 474 | } 475 | 476 | /** 477 | * Gets the cloudlet submitted list. 478 | * 479 | * @param the generic type 480 | * @return the cloudlet submitted list 481 | */ 482 | @SuppressWarnings("unchecked") 483 | public List getCloudletSubmittedList() { 484 | return (List) cloudletSubmittedList; 485 | } 486 | 487 | /** 488 | * Sets the cloudlet submitted list. 489 | * 490 | * @param the generic type 491 | * @param cloudletSubmittedList the new cloudlet submitted list 492 | */ 493 | protected void setCloudletSubmittedList(List cloudletSubmittedList) { 494 | this.cloudletSubmittedList = cloudletSubmittedList; 495 | } 496 | 497 | /** 498 | * Gets the cloudlet received list. 499 | * 500 | * @param the generic type 501 | * @return the cloudlet received list 502 | */ 503 | @SuppressWarnings("unchecked") 504 | public List getCloudletReceivedList() { 505 | return (List) cloudletReceivedList; 506 | } 507 | 508 | /** 509 | * Sets the cloudlet received list. 510 | * 511 | * @param the generic type 512 | * @param cloudletReceivedList the new cloudlet received list 513 | */ 514 | protected void setCloudletReceivedList(List cloudletReceivedList) { 515 | this.cloudletReceivedList = cloudletReceivedList; 516 | } 517 | 518 | /** 519 | * Gets the vm list. 520 | * 521 | * @param the generic type 522 | * @return the vm list 523 | */ 524 | @SuppressWarnings("unchecked") 525 | public List getVmsCreatedList() { 526 | return (List) vmsCreatedList; 527 | } 528 | 529 | /** 530 | * Sets the vm list. 531 | * 532 | * @param the generic type 533 | * @param vmsCreatedList the vms created list 534 | */ 535 | protected void setVmsCreatedList(List vmsCreatedList) { 536 | this.vmsCreatedList = vmsCreatedList; 537 | } 538 | 539 | /** 540 | * Gets the vms requested. 541 | * 542 | * @return the vms requested 543 | */ 544 | protected int getVmsRequested() { 545 | return vmsRequested; 546 | } 547 | 548 | /** 549 | * Sets the vms requested. 550 | * 551 | * @param vmsRequested the new vms requested 552 | */ 553 | protected void setVmsRequested(int vmsRequested) { 554 | this.vmsRequested = vmsRequested; 555 | } 556 | 557 | /** 558 | * Gets the vms acks. 559 | * 560 | * @return the vms acks 561 | */ 562 | protected int getVmsAcks() { 563 | return vmsAcks; 564 | } 565 | 566 | /** 567 | * Sets the vms acks. 568 | * 569 | * @param vmsAcks the new vms acks 570 | */ 571 | protected void setVmsAcks(int vmsAcks) { 572 | this.vmsAcks = vmsAcks; 573 | } 574 | 575 | /** 576 | * Increment vms acks. 577 | */ 578 | protected void incrementVmsAcks() { 579 | vmsAcks++; 580 | } 581 | 582 | /** 583 | * Gets the vms destroyed. 584 | * 585 | * @return the vms destroyed 586 | */ 587 | protected int getVmsDestroyed() { 588 | return vmsDestroyed; 589 | } 590 | 591 | /** 592 | * Sets the vms destroyed. 593 | * 594 | * @param vmsDestroyed the new vms destroyed 595 | */ 596 | protected void setVmsDestroyed(int vmsDestroyed) { 597 | this.vmsDestroyed = vmsDestroyed; 598 | } 599 | 600 | /** 601 | * Gets the datacenter ids list. 602 | * 603 | * @return the datacenter ids list 604 | */ 605 | protected List getDatacenterIdsList() { 606 | return datacenterIdsList; 607 | } 608 | 609 | /** 610 | * Sets the datacenter ids list. 611 | * 612 | * @param datacenterIdsList the new datacenter ids list 613 | */ 614 | protected void setDatacenterIdsList(List datacenterIdsList) { 615 | this.datacenterIdsList = datacenterIdsList; 616 | } 617 | 618 | /** 619 | * Gets the vms to datacenters map. 620 | * 621 | * @return the vms to datacenters map 622 | */ 623 | protected Map getVmsToDatacentersMap() { 624 | return vmsToDatacentersMap; 625 | } 626 | 627 | /** 628 | * Sets the vms to datacenters map. 629 | * 630 | * @param vmsToDatacentersMap the vms to datacenters map 631 | */ 632 | protected void setVmsToDatacentersMap(Map vmsToDatacentersMap) { 633 | this.vmsToDatacentersMap = vmsToDatacentersMap; 634 | } 635 | 636 | /** 637 | * Gets the datacenter characteristics list. 638 | * 639 | * @return the datacenter characteristics list 640 | */ 641 | protected Map getDatacenterCharacteristicsList() { 642 | return datacenterCharacteristicsList; 643 | } 644 | 645 | /** 646 | * Sets the datacenter characteristics list. 647 | * 648 | * @param datacenterCharacteristicsList the datacenter characteristics list 649 | */ 650 | protected void setDatacenterCharacteristicsList( 651 | Map datacenterCharacteristicsList) { 652 | this.datacenterCharacteristicsList = datacenterCharacteristicsList; 653 | } 654 | 655 | /** 656 | * Gets the datacenter requested ids list. 657 | * 658 | * @return the datacenter requested ids list 659 | */ 660 | protected List getDatacenterRequestedIdsList() { 661 | return datacenterRequestedIdsList; 662 | } 663 | 664 | /** 665 | * Sets the datacenter requested ids list. 666 | * 667 | * @param datacenterRequestedIdsList the new datacenter requested ids list 668 | */ 669 | protected void setDatacenterRequestedIdsList(List datacenterRequestedIdsList) { 670 | this.datacenterRequestedIdsList = datacenterRequestedIdsList; 671 | } 672 | 673 | public void submitMapping(double[] psoMapping) { 674 | mapping = psoMapping; 675 | } 676 | public void submitDelay(double[] delay) { 677 | lag=delay; 678 | } 679 | public List assignCloudletsToDC(List cloudlist) { 680 | // double[] mapping = (new PSO()).run(); 681 | int idx = 0; 682 | for(Cloudlet cl: cloudlist) { 683 | cl.setVmId((int) mapping[idx++]); 684 | } 685 | return cloudlist; 686 | } 687 | 688 | } 689 | 690 | -------------------------------------------------------------------------------- /PSOCloud/PSO.java: -------------------------------------------------------------------------------- 1 | package PSOCloud; 2 | 3 | import net.sourceforge.jswarm_pso.*; 4 | public class PSO { 5 | /* 6 | * Global Parameters: 7 | * ff : represents the fitness function used by all particles in the swarm 8 | * swarm : represents the different swarms that are used in multiswarm PSO 9 | * particles[i][j] : represents the particles where i is the swarm id and j is particle id 10 | * other parameters are defined earlier 11 | */ 12 | ParticlePSO particles[]; 13 | FitnessFunctionPSO ff; 14 | Swarm swarm; 15 | int[] tasklength;int[] outputfilesize;int[] mips;double[] execcost,waitcost; 16 | int[][] graph; 17 | public PSO(int[] tasklength,int[] outputfilesize,int[] mips,double[] execcost,double[] waitcost,int[][] graph) 18 | { 19 | this.tasklength=tasklength; 20 | this.outputfilesize=outputfilesize; 21 | this.mips=mips; 22 | this.execcost=execcost; 23 | this.graph=graph; 24 | this.waitcost=waitcost; 25 | ff = new FitnessFunctionPSO(execcost,waitcost,mips,outputfilesize,tasklength,graph); 26 | swarm = new Swarm(Constants.NoOfParticles, new ParticlePSO(), ff); 27 | initializeParticles(); 28 | } 29 | public void initializeParticles() { 30 | particles = new ParticlePSO[Constants.NoOfParticles]; 31 | for(int i=0;i0?1.0:-1.0; 25 | } 26 | setPosition(position); 27 | setVelocity(velocity); 28 | } 29 | public String toString() { 30 | String output = ""; 31 | for(int i=0;i cloudletList; 35 | 36 | /** The vmlist. */ 37 | private static List vmlist; 38 | /* 39 | * mapping: represents the mapping of cloudlets to vm based on MultiSwarmPSO 40 | * mapping2: represents the mapping of cloudlets to vm based on Random Scheduling 41 | * resultcost: stores the cost of cloudlet execution based on the mappings 42 | * other parameters are defined in FitnessFunction 43 | */ 44 | public static double mapping[]; 45 | public static double[][] executiontimematrix; 46 | public static double[][] communicationtimematrix; 47 | public static double[][] datatransfermatrix; 48 | public static double[][] taskoutputfilematrix; 49 | public static double[][] commcost; 50 | public static double[] mapping2 = new double[Constants.NoOfTasks]; 51 | public static int depgraph[][] = new int[Constants.NoOfTasks][Constants.NoOfTasks]; 52 | 53 | 54 | public static double[] resultcost= new double[2]; 55 | 56 | /** 57 | * Creates main() to run this example 58 | */ 59 | public double[] getPSOMapping() { 60 | return mapping; 61 | } 62 | 63 | /* 64 | * This function is used to create shell script which will run scripts of respective vm 65 | * for both the mappings 66 | * 67 | */ 68 | public static void createvmrunscript(int vmno)throws Exception{ 69 | FileWriter fw = new FileWriter("C:\\Users\\admin\\Desktop\\psobroker\\pso\\runpsovm-"+vmno+".sh"); 70 | BufferedWriter bw = new BufferedWriter(fw); 71 | PrintWriter pw = new PrintWriter(bw); 72 | pw.println("#!/bin/bash"); 73 | pw.println("sh vm-"+vmno+".sh"); 74 | pw.close(); 75 | bw.close(); 76 | fw.close(); 77 | FileWriter fw1 = new FileWriter("C:\\Users\\admin\\Desktop\\psobroker\\random\\runrandomvm-"+vmno+".sh"); 78 | BufferedWriter bw1 = new BufferedWriter(fw1); 79 | PrintWriter pw1 = new PrintWriter(bw1); 80 | pw1.println("#!bin/bash"); 81 | pw1.println("sh vm-"+vmno+".sh"); 82 | pw1.close(); 83 | bw1.close(); 84 | fw1.close(); 85 | } 86 | 87 | /* 88 | * This function creates shell scripts to run java programs if all the dependent output 89 | * files are present. It calculates the time to execute the program for each vm as well. 90 | * It also stores the output in the corresponding output file as well. 91 | * 92 | */ 93 | public static void createscript(double mapping[],String Folder)throws Exception{ 94 | int no_of_vms=Constants.NoOfVMs; 95 | for(int i=0;i(); 178 | 179 | //VM description 180 | int vmid = 0; 181 | long size = 10000; //image size (MB) 182 | int ram = 256; //vm memory (MB) 183 | long bw = 1000; 184 | int pesNumber = 1; //number of cpus 185 | String vmm = "Xen"; //VMM name 186 | 187 | //create two VMs 188 | for(int i=0;i(); 201 | 202 | //Cloudlet properties 203 | 204 | long fileSize = 300; 205 | // long outputSize[] = outputfilesize; 206 | UtilizationModel utilizationModel = new UtilizationModelFull(); 207 | for(int id=0;id newList = broker.getCloudletReceivedList(); 238 | 239 | CloudSim.stopSimulation(); 240 | 241 | printCloudletList(newList); 242 | 243 | resultcost[0]=PSOScheduler.printBestFitness(); 244 | Log.printLine("Simulation of Task Scheduler using PSO is finished!"); 245 | for(int i=0;i(); 291 | 292 | //VM description 293 | int vmid = 0; 294 | 295 | //int mips[] = {80, 50, 10, 10, 5, 15, 20, 60}; 296 | long size = 10000; //image size (MB) 297 | int ram = 256; //vm memory (MB) 298 | long bw = 1000; 299 | int pesNumber = 1; //number of cpus 300 | String vmm = "Xen"; //VMM name 301 | 302 | //create two VMs 303 | for(int i=0;i(); 316 | 317 | //Cloudlet properties 318 | //This is the mips rating(Million Instructions per second which are processed) of each VM that is being called 319 | //This is the cost of execution on different VM per unit time. 320 | 321 | UtilizationModel utilizationModel = new UtilizationModelFull(); 322 | for(int i=0;i newList2 = broker.getCloudletReceivedList(); 387 | 388 | CloudSim.stopSimulation(); 389 | 390 | printCloudletList(newList2); 391 | 392 | resultcost[1]=cost; 393 | Log.printLine("Simulation of Task Scheduler using Random scheduling is finished!"); 394 | } 395 | catch (Exception e) { 396 | e.printStackTrace(); 397 | Log.printLine("The simulation has been terminated due to an unexpected error"); 398 | } 399 | 400 | } 401 | 402 | 403 | 404 | private static Datacenter createDatacenter(String name){ 405 | 406 | // Here are the steps needed to create a PowerDatacenter: 407 | // 1. We need to create a list to store 408 | // our machine 409 | List hostList = new ArrayList(); 410 | 411 | // 2. A Machine contains one or more PEs or CPUs/Cores. 412 | // In this example, it will have only one core. 413 | List peList = new ArrayList(); 414 | 415 | int mips = 1000; 416 | 417 | // 3. Create PEs and add these into a list. 418 | peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating 419 | 420 | //4. Create Hosts with its id and list of PEs and add them to the list of machines 421 | int hostId=0; 422 | int ram = 2048; //host memory (MB) 423 | long storage = 1000000; //host storage 424 | int bw = 10000; 425 | 426 | hostList.add( 427 | new Host( 428 | hostId, 429 | new RamProvisionerSimple(ram), 430 | new BwProvisionerSimple(bw), 431 | storage, 432 | peList, 433 | new VmSchedulerTimeShared(peList) 434 | ) 435 | ); // This is our first machine 436 | 437 | //create another machine in the Data center 438 | List peList2 = new ArrayList(); 439 | 440 | peList2.add(new Pe(0, new PeProvisionerSimple(mips))); 441 | 442 | hostId++; 443 | 444 | hostList.add( 445 | new Host( 446 | hostId, 447 | new RamProvisionerSimple(ram), 448 | new BwProvisionerSimple(bw), 449 | storage, 450 | peList2, 451 | new VmSchedulerTimeShared(peList2) 452 | ) 453 | ); // This is our second machine 454 | 455 | 456 | 457 | // 5. Create a DatacenterCharacteristics object that stores the 458 | // properties of a data center: architecture, OS, list of 459 | // Machines, allocation policy: time- or space-shared, time zone 460 | // and its price (G$/Pe time unit). 461 | String arch = "x86"; // system architecture 462 | String os = "Linux"; // operating system 463 | String vmm = "Xen"; 464 | double time_zone = 10.0; // time zone this resource located 465 | double cost = 3.0; // the cost of using processing in this resource 466 | double costPerMem = 0.05; // the cost of using memory in this resource 467 | double costPerStorage = 0.001; // the cost of using storage in this resource 468 | double costPerBw = 0.0; // the cost of using bw in this resource 469 | LinkedList storageList = new LinkedList(); //we are not adding SAN devices by now 470 | 471 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 472 | arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw); 473 | 474 | // 6. Finally, we need to create a PowerDatacenter object. 475 | Datacenter datacenter = null; 476 | try { 477 | datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0); 478 | } catch (Exception e) { 479 | e.printStackTrace(); 480 | } 481 | 482 | return datacenter; 483 | } 484 | 485 | // Broker policy using PSO 486 | private static MyDataCenterBroker createBroker(){ 487 | 488 | MyDataCenterBroker broker = null; 489 | try { 490 | broker = new MyDataCenterBroker("Broker"); 491 | } catch (Exception e) { 492 | e.printStackTrace(); 493 | return null; 494 | } 495 | return broker; 496 | } 497 | /** 498 | * Prints the Cloudlet objects 499 | * @param list list of Cloudlets 500 | */ 501 | private static void printCloudletList(List list) { 502 | int size = list.size(); 503 | Cloudlet cloudlet; 504 | 505 | String indent = " "; 506 | Log.printLine(); 507 | Log.printLine("========== OUTPUT =========="); 508 | Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + 509 | "Data center ID" + indent + "VM ID" + indent + "Time" + indent + "Start Time" + indent + "Finish Time"); 510 | 511 | DecimalFormat dft = new DecimalFormat("###.##"); 512 | for (int i = 0; i < size; i++) { 513 | cloudlet = list.get(i); 514 | Log.print(indent + cloudlet.getCloudletId() + indent + indent); 515 | 516 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){ 517 | Log.print("SUCCESS"); 518 | 519 | Log.printLine( indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId() + 520 | indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent + indent + dft.format(cloudlet.getExecStartTime())+ 521 | indent + indent + dft.format(cloudlet.getFinishTime())); 522 | } 523 | } 524 | 525 | } 526 | } 527 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Particle-Swarm-Optimization-Based-Cost-Effective-Cloudlet-Scheduling-using-CloudSim 2 | 3 | ## Problem Statement 4 | The problem at hand is to minimize the total cost of processing cloudlets by optimal scheduling on given Virtual Machines. Our hypothesis is that we can obtain better cost using Particle Swarm Optimization to schedule our tasks as compared to Random Scheduling algorithm. 5 | 6 | ## Setup Used 7 | 1.) Language used - Java 8 | 9 | 2.) Operating System - Windows 10 10 | 11 | 3.) RAM - 8 GB 12 | 13 | 4.) CloudSim version - 3.0.3 14 | 15 | 5.) JSwarm version - 2.08 16 | 17 | 6.) Oracle VirtualBox version - 5.1.28 18 | 19 | 7.) Ubuntu OS version - 14.04.3 20 | 21 | Link to download CEC-2013 code - http://web.mysites.ntu.edu.sg/epnsugan/PublicSite/Shared%20Documents/CEC2013/cec13%20java%20code.rar 22 | 23 | ## Details 24 | The comparisons were done between Random Scheduling and Particle Swarm Optimization. Also, 4 different variants of PSO were used and tested against **CEC-2013 28 benchmark functions**. 25 | 26 | The 4 variants are namely - 27 | 28 | 1.) Standard PSO heuristic 29 | 30 | 2.) PSO with Linearly Decreasing Inertia Weight 31 | 32 | 3.) PSO with improved Constriction Factor 33 | 34 | 4.) PSO with both Linearly Decreasing Inertia Weight and improved Constriction Factor 35 | 36 | Among them, the best variant was selected for Cloudlet Scheduling. Another heuristic of PSO known as MultiSwarm PSO was also used and tested. In this heuristic instead of using a single swarm, the swarm was divided into a number of smaller swarms with their own respective velocities. The 4 variants stated above were also implemented using MultiSwarm heuristic and tested against **CEC-2013 28 benchmark functions** and also the best variant among them is selected for scheduling cloudlets. Both Single Swarm and MultiSwarm PSO were tested against Random Scheduling and it was observed that both performed a lot better than Random Scheduling. Our work was done keeping in mind both independent and dependent tasks. In our code, there are 4 packages - 37 | 38 | 1.) **PSOCEC2013** : Which contains 4 variants of PSO codes using JSwarm which are tested on CEC functions. 39 | 40 | 2.) **PSOCloud** : Which uses the PSO which are tested on CEC to simulate the cloud scenarios for cost effective scheduling. 41 | 42 | 3.) **MultiSwarm** : Which contains MultiSwarm PSO codes which are tested on CEC functions. 43 | 44 | 4.) **MultiSwarmPSOCloudTest** : Which uses the MultiSwarm PSO to simulate the cloud scenarios for cost effective scheduling. 45 | 46 | 47 | Also we tried to implement the same on virtual machines in Oracle Virtual Box to test and compare our CloudSim reasults on real time environment. We used the CloudSim PSO to obtain the best optimized mapping for in which order and on which virtual machines our cloudlets should run. Shell scripts are generated according to mapping obtained. The shell scripts generated while running the PSOCloud and MultiSwarmPSOCloudTest can be used to run the different corresponding cloudlets on different VMs even in case of independent or dependent tasks. 48 | 49 | ## How to Set up VirtualBox Instances 50 | 1. Create the number of VM instances as specified in the cloud scenario and the number of programs(Cloudlets). 51 | 52 | 2. Install Ubuntu-OS in VMs. 53 | 54 | 3. Create a shared folder between the host OS and the VM instances on VirtualBox. 55 | 56 | 4. After getting a mapping from PSO corresponding shell scripts are generated. 57 | 58 | 5. Run commands in VM. 59 | 60 | 1->cd to the shared folder (/media/psobroker) where all the codes are present and then to pso or random 61 | 62 | 2->javac createconvert.java 63 | 64 | 3->java createconvert (NoofVMs) runpsovm-(VMNo).sh 65 | 66 | 4->sh convert.sh 67 | 68 | Do upto step 4 in all the VMs parallely. 69 | Run step 5 in all the VMs one by one 70 | 71 | 5->sh runpsovm-(VMNo).sh 72 | 73 | 6->corresponding output files will be generated in the shared folder which are accessible by host system as well. 74 | 75 | **Check for dos to unix conversion in case errors in opening the files between host and VMs** 76 | 77 | ## Files to run in each package 78 | 79 | 1.) **PSOCEC2013** : PSO 80 | 81 | 2.) **PSOCloud** : ResCalc 82 | 83 | 3.) **MultiSwarm** : PSOSubSwarm 84 | 85 | 4.) **MultiSwarmPSOCloudTest** : Results 86 | -------------------------------------------------------------------------------- /Workspace.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kshitij1210/Particle-Swarm-Optimization-Based-Cost-Effective-Cloudlet-Scheduling-using-CloudSim/1dbb56c6132408f087a9774e28c2850ee1af2ad1/Workspace.zip --------------------------------------------------------------------------------