├── LICENSE ├── README.md ├── classes └── ACOImplement.jar ├── implementation ├── 11.png ├── LBACOvsDEF.png ├── graphImplement.java └── specs.txt ├── sample └── DatacenterBroker.java └── src ├── ACOImplement.java └── LBACO.java /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Siddharth Jain 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ant-Colony-Optimization-Framework 2 | 3 | ## About ACO 4 | Ant Colony Optimization is a member of the ant colony algorithms family, in swarm intelligence methods, 5 | and it constitutes some metaheuristic optimizations. The original algorithm was aiming to search 6 | for an optimal path in a graph, based on the behavior of ants seeking a path between their colony 7 | and a source of food. 8 | 9 | ## Used in Cloud Task Scheduling 10 | This framework aims at optimizing the tasks submitted to a number of Virtual Machines. 11 | The simulator to be used is Cloudsim by Melbourne Clouds Lab. 12 | 13 | ## How to use: 14 | 1. Clone the Repository 15 | 2. Compress src/LBACO.java into jar file and add it in cloudsim/jars directory 16 | 3. Adjust the DatacenterBroker.java file in cloudsim sources. See sample/DatacenterBroker.java 17 | ``` 18 | // For Load Balancing ACO 19 | LBACO lbaco1 = new LBACO(,,,,,); 20 | Map allocated = lbaco1.implement(,,100>); 21 | 22 | // For usual ACO 23 | ACOImplement aco1 = new ACOImplement(,,,,,); 24 | Map allocatedTasks = aco1.allocateTasks(,,); 25 | ``` 26 | 4. The hashmap `allocatedTasks` will represent the cloudlets mapped to VMs. 27 | -------------------------------------------------------------------------------- /classes/ACOImplement.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sidjee/Ant-Colony-Optimization-Framework/77c4f2c7ac6d7af65f6d0a8203dd2b5fc8d2ac85/classes/ACOImplement.jar -------------------------------------------------------------------------------- /implementation/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sidjee/Ant-Colony-Optimization-Framework/77c4f2c7ac6d7af65f6d0a8203dd2b5fc8d2ac85/implementation/11.png -------------------------------------------------------------------------------- /implementation/LBACOvsDEF.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sidjee/Ant-Colony-Optimization-Framework/77c4f2c7ac6d7af65f6d0a8203dd2b5fc8d2ac85/implementation/LBACOvsDEF.png -------------------------------------------------------------------------------- /implementation/graphImplement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Title: CloudSim Toolkit 3 | * Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation 4 | * of Clouds 5 | * Licence: GPL - http://www.gnu.org/copyleft/gpl.html 6 | * 7 | * Copyright (c) 2009, The University of Melbourne, Australia 8 | */ 9 | 10 | 11 | package org.cloudbus.cloudsim.examples; 12 | 13 | import java.text.DecimalFormat; 14 | import java.util.ArrayList; 15 | import java.util.Calendar; 16 | import java.util.LinkedList; 17 | import java.util.List; 18 | import java.util.Random; 19 | import java.io.*; 20 | 21 | import org.cloudbus.cloudsim.Cloudlet; 22 | import org.cloudbus.cloudsim.CloudletSchedulerTimeShared; 23 | import org.cloudbus.cloudsim.Datacenter; 24 | import org.cloudbus.cloudsim.DatacenterBroker; 25 | import org.cloudbus.cloudsim.DatacenterBroker1; 26 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 27 | import org.cloudbus.cloudsim.Host; 28 | import org.cloudbus.cloudsim.Log; 29 | import org.cloudbus.cloudsim.Pe; 30 | import org.cloudbus.cloudsim.Storage; 31 | import org.cloudbus.cloudsim.UtilizationModel; 32 | import org.cloudbus.cloudsim.UtilizationModelFull; 33 | import org.cloudbus.cloudsim.Vm; 34 | import org.cloudbus.cloudsim.VmAllocationPolicySimple; 35 | import org.cloudbus.cloudsim.VmSchedulerSpaceShared; 36 | import org.cloudbus.cloudsim.core.CloudSim; 37 | import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 38 | import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 39 | import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 40 | 41 | import org.cloudbus.cloudsim.examples.LineChart_AWT; 42 | import org.jfree.data.category.DefaultCategoryDataset; 43 | 44 | public class graphImplement { 45 | 46 | /** The cloudlet list. */ 47 | private static List cloudletList; 48 | private static List cloudletList1; 49 | 50 | /** The vmlist. */ 51 | private static List vmlist; 52 | private static List vmlist1; 53 | 54 | private static List createVM(int userId, int vms) { 55 | 56 | //Creates a container to store VMs. This list is passed to the broker later 57 | LinkedList list = new LinkedList(); 58 | 59 | //VM Parameters 60 | long size = 10000; //image size (MB) 61 | int ram = 512; //vm memory (MB) 62 | int mips = 1000; 63 | long bw = 1000; 64 | int pesNumber = 1; //number of cpus 65 | String vmm = "Xen"; //VMM name 66 | Random r = new Random(1); 67 | //create VMs 68 | Vm[] vm = new Vm[vms]; 69 | 70 | for(int i=0;i createCloudlet(int userId, int cloudlets){ 84 | // Creates a container to store Cloudlets 85 | LinkedList list = new LinkedList(); 86 | 87 | //cloudlet parameters 88 | long length ; 89 | long fileSize = 300; 90 | long outputSize = 300; 91 | int pesNumber = 1; 92 | UtilizationModel utilizationModel = new UtilizationModelFull(); 93 | Random r1 = new Random(1); 94 | // Random r2 = new Random(2); 95 | Cloudlet[] cloudlet = new Cloudlet[cloudlets]; 96 | 97 | for(int i=0;i newList = broker.getCloudletReceivedList(); 162 | List newList1 = broker1.getCloudletReceivedList(); 163 | 164 | CloudSim.stopSimulation(); 165 | 166 | 167 | double max = 0; 168 | for (int j=0;j<100*k;j++){ 169 | if(max < newList.get(j).getFinishTime()) 170 | max = newList.get(j).getFinishTime(); 171 | } 172 | double min = 10000000; 173 | for (int j=0;j<100*k;j++){ 174 | if(min > newList.get(j).getExecStartTime()) 175 | min = newList.get(j).getExecStartTime(); 176 | } 177 | 178 | double max1 = 0; 179 | for (int j=0;j<100*k;j++){ 180 | if(max1 < newList1.get(j).getFinishTime()) 181 | max1 = newList1.get(j).getFinishTime(); 182 | } 183 | double min1 = 10000000; 184 | for (int j=0;j<100*k;j++){ 185 | if(min1 > newList1.get(j).getExecStartTime()) 186 | min1 = newList1.get(j).getExecStartTime(); 187 | } 188 | 189 | dataset.addValue((Number)(max-min),"LBACO Algo",String.valueOf(100*k)); 190 | dataset.addValue((Number)(max1-min1),"Default Algo",String.valueOf(100*k)); 191 | } 192 | // } 193 | // FileOutputStream fos = new FileOutputStream("ACO.ser"); 194 | // ObjectOutputStream oos = new ObjectOutputStream(fos); 195 | // oos.writeObject(dataset); 196 | // oos.close(); 197 | 198 | LineChart_AWT.implement("Response Time Vs. Cloudlet","No. of cloudlets","Total response time",dataset); 199 | 200 | Log.printLine("CloudSimExample6 finished!"); 201 | } 202 | catch (Exception e) 203 | { 204 | e.printStackTrace(); 205 | Log.printLine("The simulation has been terminated due to an unexpected error"); 206 | } 207 | } 208 | 209 | private static Datacenter createDatacenter(String name, int elements){ 210 | 211 | // Here are the steps needed to create a PowerDatacenter: 212 | // 1. We need to create a list to store one or more 213 | // Machines 214 | List hostList = new ArrayList(); 215 | 216 | // 2. A Machine contains one or more PEs or CPUs/Cores. Therefore, should 217 | // create a list to store these PEs before creating 218 | // a Machine. 219 | List peList1 = new ArrayList(); 220 | 221 | int mips = 1000; 222 | 223 | // 3. Create PEs and add these into the list. 224 | //for a quad-core machine, a list of 4 PEs is required: 225 | for (int i=0;i storageList = new LinkedList(); //we are not adding SAN devices by now 285 | 286 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 287 | arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw); 288 | 289 | 290 | // 6. Finally, we need to create a PowerDatacenter object. 291 | Datacenter datacenter = null; 292 | try { 293 | datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), storageList, 0); 294 | } catch (Exception e) { 295 | e.printStackTrace(); 296 | } 297 | 298 | return datacenter; 299 | } 300 | 301 | //We strongly encourage users to develop their own broker policies, to submit vms and cloudlets according 302 | //to the specific rules of the simulated scenario 303 | private static DatacenterBroker createBroker(){ 304 | 305 | DatacenterBroker broker = null; 306 | try { 307 | //int m, double Q, double alpha, double beta, double gamma, double rho 308 | broker = new DatacenterBroker("Broker",37,1,2,1,4,0.05); 309 | } catch (Exception e) { 310 | e.printStackTrace(); 311 | return null; 312 | } 313 | return broker; 314 | } 315 | 316 | private static DatacenterBroker1 createBroker1(){ 317 | 318 | DatacenterBroker1 broker = null; 319 | try { 320 | broker = new DatacenterBroker1("Broker1"); 321 | } catch (Exception e) { 322 | e.printStackTrace(); 323 | return null; 324 | } 325 | return broker; 326 | } 327 | 328 | /** 329 | * Prints the Cloudlet objects 330 | * @param list list of Cloudlets 331 | */ 332 | // private static void printCloudletList(List list) { 333 | // int size = list.size(); 334 | // Cloudlet cloudlet; 335 | 336 | // String indent = " "; 337 | // Log.printLine(); 338 | // Log.printLine("========== OUTPUT =========="); 339 | // Log.printLine("Cloudlet ID" + indent + "STATUS" + indent + 340 | // "Data center ID" + indent + "VM ID" + indent + indent + "Time" + indent + "Start Time" + indent + "Finish Time"); 341 | 342 | // DecimalFormat dft = new DecimalFormat("###.##"); 343 | // for (int i = 0; i < size; i++) { 344 | // cloudlet = list.get(i); 345 | // Log.print(indent + cloudlet.getCloudletId() + indent + indent); 346 | 347 | // if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){ 348 | // Log.print("SUCCESS"); 349 | 350 | // Log.printLine( indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId() + 351 | // indent + indent + indent + dft.format(cloudlet.getActualCPUTime()) + 352 | // indent + indent + dft.format(cloudlet.getExecStartTime())+ indent + indent + indent + dft.format(cloudlet.getFinishTime())); 353 | // } 354 | // } 355 | 356 | // } 357 | } 358 | -------------------------------------------------------------------------------- /implementation/specs.txt: -------------------------------------------------------------------------------- 1 | Parameters used for Ant Colony Algorithm:- 2 | ----------------------------------------- 3 | Q = 1 -------- multiplier for pheromone update 4 | 5 | m = 37 --------- No of ants used 6 | 7 | vms = 20 ---------- No of VMs used 8 | 9 | alpha = 2 ----------- Exponent of pheromones 10 | 11 | beta = 1 ----------- Exponent of computing capacity 12 | 13 | gamma = 4 ------------ Exponent of Load balancing factor 14 | 15 | rho = 0.05 ----------- vapourization constant for pheromone trail 16 | 17 | 18 | Configurations for VMs :- 19 | ----------------------- 20 | size = 10000; //image size (MB) 21 | 22 | ram = 512; //vm memory (MB) 23 | 24 | mips = random (500 to 1000) 25 | 26 | bw = 1000(can be kept random) 27 | 28 | pesNumber = 1(can be kept random) //number of cpus 29 | 30 | vmm = "Xen"//VMM name 31 | 32 | 33 | Configurations for cloudlets :- 34 | ----------------------------- 35 | length = Random (100 to 1000) 36 | 37 | fileSize = 300 (can be kept random) 38 | 39 | outputSize = 300 40 | 41 | pesNumber = 1 (can be kept random) 42 | -------------------------------------------------------------------------------- /sample/DatacenterBroker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Title: CloudSim Toolkit 3 | * Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds 4 | * Licence: GPL - http://www.gnu.org/copyleft/gpl.html 5 | * 6 | * Copyright (c) 2009-2012, The University of Melbourne, Australia 7 | */ 8 | 9 | package org.cloudbus.cloudsim; 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 | import java.lang.*; 17 | import java.util.Random; 18 | 19 | import org.cloudbus.cloudsim.core.CloudSim; 20 | import org.cloudbus.cloudsim.core.CloudSimTags; 21 | import org.cloudbus.cloudsim.core.SimEntity; 22 | import org.cloudbus.cloudsim.core.SimEvent; 23 | import org.cloudbus.cloudsim.lists.CloudletList; 24 | import org.cloudbus.cloudsim.lists.VmList; 25 | import org.cloudbus.cloudsim.LBACO; 26 | 27 | /** 28 | * DatacentreBroker represents a broker acting on behalf of a user. It hides VM management, as vm 29 | * creation, sumbission of cloudlets to this VMs and destruction of VMs. 30 | * 31 | * @author Rodrigo N. Calheiros 32 | * @author Anton Beloglazov 33 | * @since CloudSim Toolkit 1.0 34 | */ 35 | public class DatacenterBroker extends SimEntity { 36 | 37 | /** The vm list. */ 38 | protected List vmList; 39 | 40 | /** The vms created list. */ 41 | protected List vmsCreatedList; 42 | 43 | /** The cloudlet list. */ 44 | protected List cloudletList; 45 | 46 | /** The cloudlet submitted list. */ 47 | protected List cloudletSubmittedList; 48 | 49 | /** The cloudlet received list. */ 50 | protected List cloudletReceivedList; 51 | 52 | /** The cloudlets submitted. */ 53 | protected int cloudletsSubmitted; 54 | 55 | /** The vms requested. */ 56 | protected int vmsRequested; 57 | 58 | /** The vms acks. */ 59 | protected int vmsAcks; 60 | 61 | /** The vms destroyed. */ 62 | protected int vmsDestroyed; 63 | 64 | /** The datacenter ids list. */ 65 | protected List datacenterIdsList; 66 | 67 | /** The datacenter requested ids list. */ 68 | protected List datacenterRequestedIdsList; 69 | 70 | /** The vms to datacenters map. */ 71 | protected Map vmsToDatacentersMap; 72 | 73 | /** The datacenter characteristics list. */ 74 | protected Map datacenterCharacteristicsList; 75 | 76 | protected double Q; 77 | protected double alpha; 78 | protected double beta; 79 | protected double gamma; 80 | protected double rho; 81 | protected int m; 82 | 83 | /** 84 | * Created a new DatacenterBroker object. 85 | * 86 | * @param name name to be associated with this entity (as required by Sim_entity class from 87 | * simjava package) 88 | * @throws Exception the exception 89 | * @pre name != null 90 | * @post $none 91 | */ 92 | public DatacenterBroker(String name, int m, double Q, double alpha, double beta, double gamma, double rho) throws Exception { 93 | super(name); 94 | 95 | setVmList(new ArrayList()); 96 | setVmsCreatedList(new ArrayList()); 97 | setCloudletList(new ArrayList()); 98 | setCloudletSubmittedList(new ArrayList()); 99 | setCloudletReceivedList(new ArrayList()); 100 | 101 | cloudletsSubmitted = 0; 102 | setVmsRequested(0); 103 | setVmsAcks(0); 104 | setVmsDestroyed(0); 105 | 106 | setDatacenterIdsList(new LinkedList()); 107 | setDatacenterRequestedIdsList(new ArrayList()); 108 | setVmsToDatacentersMap(new HashMap()); 109 | setDatacenterCharacteristicsList(new HashMap()); 110 | 111 | this.m = m; 112 | this.Q = Q; 113 | this.alpha = alpha; 114 | this.beta = beta; 115 | this.gamma = gamma; 116 | this.rho = rho; 117 | } 118 | 119 | /** 120 | * This method is used to send to the broker the list with virtual machines that must be 121 | * created. 122 | * 123 | * @param list the list 124 | * @pre list !=null 125 | * @post $none 126 | */ 127 | public void submitVmList(List list) { 128 | getVmList().addAll(list); 129 | } 130 | 131 | /** 132 | * This method is used to send to the broker the list of cloudlets. 133 | * 134 | * @param list the list 135 | * @pre list !=null 136 | * @post $none 137 | */ 138 | public void submitCloudletList(List list) { 139 | getCloudletList().addAll(list); 140 | } 141 | 142 | /** 143 | * Specifies that a given cloudlet must run in a specific virtual machine. 144 | * 145 | * @param cloudletId ID of the cloudlet being bount to a vm 146 | * @param vmId the vm id 147 | * @pre cloudletId > 0 148 | * @pre id > 0 149 | * @post $none 150 | */ 151 | public void bindCloudletToVm(int cloudletId, int vmId) { 152 | CloudletList.getById(getCloudletList(), cloudletId).setVmId(vmId); 153 | } 154 | 155 | /** 156 | * Processes events available for this Broker. 157 | * 158 | * @param ev a SimEvent object 159 | * @pre ev != null 160 | * @post $none 161 | */ 162 | @Override 163 | public void processEvent(SimEvent ev) { 164 | switch (ev.getTag()) { 165 | // Resource characteristics request 166 | case CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST: 167 | processResourceCharacteristicsRequest(ev); 168 | break; 169 | // Resource characteristics answer 170 | case CloudSimTags.RESOURCE_CHARACTERISTICS: 171 | processResourceCharacteristics(ev); 172 | break; 173 | // VM Creation answer 174 | case CloudSimTags.VM_CREATE_ACK: 175 | processVmCreate(ev); 176 | break; 177 | // A finished cloudlet returned 178 | case CloudSimTags.CLOUDLET_RETURN: 179 | processCloudletReturn(ev); 180 | break; 181 | // if the simulation finishes 182 | case CloudSimTags.END_OF_SIMULATION: 183 | shutdownEntity(); 184 | break; 185 | // other unknown tags are processed by this method 186 | default: 187 | processOtherEvent(ev); 188 | break; 189 | } 190 | } 191 | 192 | /** 193 | * Process the return of a request for the characteristics of a PowerDatacenter. 194 | * 195 | * @param ev a SimEvent object 196 | * @pre ev != $null 197 | * @post $none 198 | */ 199 | protected void processResourceCharacteristics(SimEvent ev) { 200 | DatacenterCharacteristics characteristics = (DatacenterCharacteristics) ev.getData(); 201 | getDatacenterCharacteristicsList().put(characteristics.getId(), characteristics); 202 | 203 | if (getDatacenterCharacteristicsList().size() == getDatacenterIdsList().size()) { 204 | setDatacenterRequestedIdsList(new ArrayList()); 205 | createVmsInDatacenter(getDatacenterIdsList().get(0)); 206 | } 207 | } 208 | 209 | /** 210 | * Process a request for the characteristics of a PowerDatacenter. 211 | * 212 | * @param ev a SimEvent object 213 | * @pre ev != $null 214 | * @post $none 215 | */ 216 | protected void processResourceCharacteristicsRequest(SimEvent ev) { 217 | setDatacenterIdsList(CloudSim.getCloudResourceList()); 218 | setDatacenterCharacteristicsList(new HashMap()); 219 | 220 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloud Resource List received with " 221 | + getDatacenterIdsList().size() + " resource(s)"); 222 | 223 | for (Integer datacenterId : getDatacenterIdsList()) { 224 | sendNow(datacenterId, CloudSimTags.RESOURCE_CHARACTERISTICS, getId()); 225 | } 226 | } 227 | 228 | /** 229 | * Process the ack received due to a request for VM creation. 230 | * 231 | * @param ev a SimEvent object 232 | * @pre ev != null 233 | * @post $none 234 | */ 235 | protected void processVmCreate(SimEvent ev) { 236 | int[] data = (int[]) ev.getData(); 237 | int datacenterId = data[0]; 238 | int vmId = data[1]; 239 | int result = data[2]; 240 | 241 | if (result == CloudSimTags.TRUE) { 242 | getVmsToDatacentersMap().put(vmId, datacenterId); 243 | getVmsCreatedList().add(VmList.getById(getVmList(), vmId)); 244 | Log.printLine(CloudSim.clock() + ": " + getName() + ": VM #" + vmId 245 | + " has been created in Datacenter #" + datacenterId + ", Host #" 246 | + VmList.getById(getVmsCreatedList(), vmId).getHost().getId()); 247 | } else { 248 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Creation of VM #" + vmId 249 | + " failed in Datacenter #" + datacenterId); 250 | } 251 | 252 | incrementVmsAcks(); 253 | 254 | // all the requested VMs have been created 255 | if (getVmsCreatedList().size() == getVmList().size() - getVmsDestroyed()) { 256 | submitCloudlets(); 257 | } else { 258 | // all the acks received, but some VMs were not created 259 | if (getVmsRequested() == getVmsAcks()) { 260 | // find id of the next datacenter that has not been tried 261 | for (int nextDatacenterId : getDatacenterIdsList()) { 262 | if (!getDatacenterRequestedIdsList().contains(nextDatacenterId)) { 263 | createVmsInDatacenter(nextDatacenterId); 264 | return; 265 | } 266 | } 267 | 268 | // all datacenters already queried 269 | if (getVmsCreatedList().size() > 0) { // if some vm were created 270 | submitCloudlets(); 271 | } else { // no vms created. abort 272 | Log.printLine(CloudSim.clock() + ": " + getName() 273 | + ": none of the required VMs could be created. Aborting"); 274 | finishExecution(); 275 | } 276 | } 277 | } 278 | } 279 | 280 | /** 281 | * Process a cloudlet return event. 282 | * 283 | * @param ev a SimEvent object 284 | * @pre ev != $null 285 | * @post $none 286 | */ 287 | protected void processCloudletReturn(SimEvent ev) { 288 | Cloudlet cloudlet = (Cloudlet) ev.getData(); 289 | getCloudletReceivedList().add(cloudlet); 290 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Cloudlet " + cloudlet.getCloudletId() 291 | + " received"); 292 | 293 | cloudletsSubmitted--; 294 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { // all cloudlets executed 295 | Log.printLine(CloudSim.clock() + ": " + getName() + ": All Cloudlets executed. Finishing..."); 296 | clearDatacenters(); 297 | finishExecution(); 298 | } else { // some cloudlets haven't finished yet 299 | if (getCloudletList().size() > 0 && cloudletsSubmitted == 0) { 300 | // all the cloudlets sent finished. It means that some bount 301 | // cloudlet is waiting its VM be created 302 | clearDatacenters(); 303 | createVmsInDatacenter(0); 304 | } 305 | 306 | } 307 | } 308 | 309 | /** 310 | * Overrides this method when making a new and different type of Broker. This method is called 311 | * by {@link #body()} for incoming unknown tags. 312 | * 313 | * @param ev a SimEvent object 314 | * @pre ev != null 315 | * @post $none 316 | */ 317 | protected void processOtherEvent(SimEvent ev) { 318 | if (ev == null) { 319 | Log.printLine(getName() + ".processOtherEvent(): " + "Error - an event is null."); 320 | return; 321 | } 322 | 323 | Log.printLine(getName() + ".processOtherEvent(): " 324 | + "Error - event unknown by this DatacenterBroker."); 325 | } 326 | 327 | /** 328 | * Create the virtual machines in a datacenter. 329 | * 330 | * @param datacenterId Id of the chosen PowerDatacenter 331 | * @pre $none 332 | * @post $none 333 | */ 334 | protected void createVmsInDatacenter(int datacenterId) { 335 | // send as much vms as possible for this datacenter before trying the next one 336 | int requestedVms = 0; 337 | String datacenterName = CloudSim.getEntityName(datacenterId); 338 | for (Vm vm : getVmList()) { 339 | if (!getVmsToDatacentersMap().containsKey(vm.getId())) { 340 | Log.printLine(CloudSim.clock() + ": " + getName() + ": Trying to Create VM #" + vm.getId() 341 | + " in " + datacenterName); 342 | sendNow(datacenterId, CloudSimTags.VM_CREATE_ACK, vm); 343 | requestedVms++; 344 | } 345 | } 346 | 347 | getDatacenterRequestedIdsList().add(datacenterId); 348 | 349 | setVmsRequested(requestedVms); 350 | setVmsAcks(0); 351 | } 352 | 353 | /** 354 | * Submit cloudlets to the created VMs. 355 | * 356 | * @pre $none 357 | * @post $none 358 | */ 359 | // protected void submitCloudlets() { 360 | // int vmIndex = 0; 361 | // for (Cloudlet cloudlet : getCloudletList()) { 362 | // Vm vm; 363 | // // if user didn't bind this cloudlet and it has not been executed yet 364 | // if (cloudlet.getVmId() == -1) { 365 | // vm = getVmsCreatedList().get(vmIndex); 366 | // } else { // submit to the specific vm 367 | // vm = VmList.getById(getVmsCreatedList(), cloudlet.getVmId()); 368 | // if (vm == null) { // vm was not created 369 | // Log.printLine(CloudSim.clock() + ": " + getName() + ": Postponing execution of cloudlet " 370 | // + cloudlet.getCloudletId() + ": bount VM not available"); 371 | // continue; 372 | // } 373 | // } 374 | 375 | // Log.printLine(CloudSim.clock() + ": " + getName() + ": Sending cloudlet " 376 | // + cloudlet.getCloudletId() + " to VM #" + vm.getId()); 377 | // cloudlet.setVmId(vm.getId()); 378 | // sendNow(getVmsToDatacentersMap().get(vm.getId()), CloudSimTags.CLOUDLET_SUBMIT, cloudlet); 379 | // cloudletsSubmitted++; 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 | protected void submitCloudlets() { 391 | // int vmIndex = 0; 392 | List clList = getCloudletList(); 393 | List vm_list = getVmsCreatedList(); 394 | // Random r = new Random(); 395 | // int m = (vm_list.size()/4 + r.nextInt(vm_list.size()/2+1))%vm_list.size(); 396 | 397 | LBACO lbaco1 = new LBACO(m,Q,alpha,beta,gamma,rho); 398 | Map allocated = lbaco1.implement(clList,vm_list,100); 399 | 400 | for (int i=0;i the generic type 478 | * @return the vm list 479 | */ 480 | @SuppressWarnings("unchecked") 481 | public List getVmList() { 482 | return (List) vmList; 483 | } 484 | 485 | /** 486 | * Sets the vm list. 487 | * 488 | * @param the generic type 489 | * @param vmList the new vm list 490 | */ 491 | protected void setVmList(List vmList) { 492 | this.vmList = vmList; 493 | } 494 | 495 | /** 496 | * Gets the cloudlet list. 497 | * 498 | * @param the generic type 499 | * @return the cloudlet list 500 | */ 501 | @SuppressWarnings("unchecked") 502 | public List getCloudletList() { 503 | return (List) cloudletList; 504 | } 505 | 506 | /** 507 | * Sets the cloudlet list. 508 | * 509 | * @param the generic type 510 | * @param cloudletList the new cloudlet list 511 | */ 512 | protected void setCloudletList(List cloudletList) { 513 | this.cloudletList = cloudletList; 514 | } 515 | 516 | /** 517 | * Gets the cloudlet submitted list. 518 | * 519 | * @param the generic type 520 | * @return the cloudlet submitted list 521 | */ 522 | @SuppressWarnings("unchecked") 523 | public List getCloudletSubmittedList() { 524 | return (List) cloudletSubmittedList; 525 | } 526 | 527 | /** 528 | * Sets the cloudlet submitted list. 529 | * 530 | * @param the generic type 531 | * @param cloudletSubmittedList the new cloudlet submitted list 532 | */ 533 | protected void setCloudletSubmittedList(List cloudletSubmittedList) { 534 | this.cloudletSubmittedList = cloudletSubmittedList; 535 | } 536 | 537 | /** 538 | * Gets the cloudlet received list. 539 | * 540 | * @param the generic type 541 | * @return the cloudlet received list 542 | */ 543 | @SuppressWarnings("unchecked") 544 | public List getCloudletReceivedList() { 545 | return (List) cloudletReceivedList; 546 | } 547 | 548 | /** 549 | * Sets the cloudlet received list. 550 | * 551 | * @param the generic type 552 | * @param cloudletReceivedList the new cloudlet received list 553 | */ 554 | protected void setCloudletReceivedList(List cloudletReceivedList) { 555 | this.cloudletReceivedList = cloudletReceivedList; 556 | } 557 | 558 | /** 559 | * Gets the vm list. 560 | * 561 | * @param the generic type 562 | * @return the vm list 563 | */ 564 | @SuppressWarnings("unchecked") 565 | public List getVmsCreatedList() { 566 | return (List) vmsCreatedList; 567 | } 568 | 569 | /** 570 | * Sets the vm list. 571 | * 572 | * @param the generic type 573 | * @param vmsCreatedList the vms created list 574 | */ 575 | protected void setVmsCreatedList(List vmsCreatedList) { 576 | this.vmsCreatedList = vmsCreatedList; 577 | } 578 | 579 | /** 580 | * Gets the vms requested. 581 | * 582 | * @return the vms requested 583 | */ 584 | protected int getVmsRequested() { 585 | return vmsRequested; 586 | } 587 | 588 | /** 589 | * Sets the vms requested. 590 | * 591 | * @param vmsRequested the new vms requested 592 | */ 593 | protected void setVmsRequested(int vmsRequested) { 594 | this.vmsRequested = vmsRequested; 595 | } 596 | 597 | /** 598 | * Gets the vms acks. 599 | * 600 | * @return the vms acks 601 | */ 602 | protected int getVmsAcks() { 603 | return vmsAcks; 604 | } 605 | 606 | /** 607 | * Sets the vms acks. 608 | * 609 | * @param vmsAcks the new vms acks 610 | */ 611 | protected void setVmsAcks(int vmsAcks) { 612 | this.vmsAcks = vmsAcks; 613 | } 614 | 615 | /** 616 | * Increment vms acks. 617 | */ 618 | protected void incrementVmsAcks() { 619 | vmsAcks++; 620 | } 621 | 622 | /** 623 | * Gets the vms destroyed. 624 | * 625 | * @return the vms destroyed 626 | */ 627 | protected int getVmsDestroyed() { 628 | return vmsDestroyed; 629 | } 630 | 631 | /** 632 | * Sets the vms destroyed. 633 | * 634 | * @param vmsDestroyed the new vms destroyed 635 | */ 636 | protected void setVmsDestroyed(int vmsDestroyed) { 637 | this.vmsDestroyed = vmsDestroyed; 638 | } 639 | 640 | /** 641 | * Gets the datacenter ids list. 642 | * 643 | * @return the datacenter ids list 644 | */ 645 | protected List getDatacenterIdsList() { 646 | return datacenterIdsList; 647 | } 648 | 649 | /** 650 | * Sets the datacenter ids list. 651 | * 652 | * @param datacenterIdsList the new datacenter ids list 653 | */ 654 | protected void setDatacenterIdsList(List datacenterIdsList) { 655 | this.datacenterIdsList = datacenterIdsList; 656 | } 657 | 658 | /** 659 | * Gets the vms to datacenters map. 660 | * 661 | * @return the vms to datacenters map 662 | */ 663 | protected Map getVmsToDatacentersMap() { 664 | return vmsToDatacentersMap; 665 | } 666 | 667 | /** 668 | * Sets the vms to datacenters map. 669 | * 670 | * @param vmsToDatacentersMap the vms to datacenters map 671 | */ 672 | protected void setVmsToDatacentersMap(Map vmsToDatacentersMap) { 673 | this.vmsToDatacentersMap = vmsToDatacentersMap; 674 | } 675 | 676 | /** 677 | * Gets the datacenter characteristics list. 678 | * 679 | * @return the datacenter characteristics list 680 | */ 681 | protected Map getDatacenterCharacteristicsList() { 682 | return datacenterCharacteristicsList; 683 | } 684 | 685 | /** 686 | * Sets the datacenter characteristics list. 687 | * 688 | * @param datacenterCharacteristicsList the datacenter characteristics list 689 | */ 690 | protected void setDatacenterCharacteristicsList( 691 | Map datacenterCharacteristicsList) { 692 | this.datacenterCharacteristicsList = datacenterCharacteristicsList; 693 | } 694 | 695 | /** 696 | * Gets the datacenter requested ids list. 697 | * 698 | * @return the datacenter requested ids list 699 | */ 700 | protected List getDatacenterRequestedIdsList() { 701 | return datacenterRequestedIdsList; 702 | } 703 | 704 | /** 705 | * Sets the datacenter requested ids list. 706 | * 707 | * @param datacenterRequestedIdsList the new datacenter requested ids list 708 | */ 709 | protected void setDatacenterRequestedIdsList(List datacenterRequestedIdsList) { 710 | this.datacenterRequestedIdsList = datacenterRequestedIdsList; 711 | } 712 | 713 | } 714 | -------------------------------------------------------------------------------- /src/ACOImplement.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim; 2 | import java.text.DecimalFormat; 3 | import java.util.*; 4 | import java.util.stream.*; 5 | import java.lang.*; 6 | // import java.util.Calendar; 7 | // import java.util.LinkedList; 8 | // import java.util.List; 9 | 10 | import org.cloudbus.cloudsim.Cloudlet; 11 | import org.cloudbus.cloudsim.CloudletSchedulerTimeShared; 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 | 30 | public class ACOImplement{ 31 | protected double initialPheromone; 32 | protected double Q; 33 | protected double alpha; 34 | protected double beta; 35 | protected double rho; 36 | protected int m; 37 | 38 | public Map allocateTasks(List taskList,List vmList,int tmax){ 39 | int n = vmList.size(); 40 | Map allocatedtasks = new HashMap<>(); 41 | 42 | for(int i=0;i<(int)taskList.size()/(n-1);i++){ 43 | Map at = implement(taskList.subList(i*(n-1),(i+1)*(n-1)),vmList,tmax); 44 | for(int j=0;j at = implement(taskList.subList((taskList.size()/(n-1))*(n-1),taskList.size()),vmList,tmax); 50 | 51 | // allocatedtasks.putAll(), 52 | // vmList,tmax)); 53 | for(int j=0;j implement(List taskList,List vmList,int tmax){ 60 | int tasks = taskList.size(); 61 | int vms = vmList.size(); 62 | List newVmList = IntStream.range(0,vms).boxed().collect(Collectors.toList()); 63 | // Map []edges = new HashMap()[tasks]; 64 | List lengths = new ArrayList<>(); 65 | List> tabu = new ArrayList<>(); 66 | Map > execTimes; 67 | execTimes = new HashMap<>(); 68 | 69 | for(int i=0;i x = new HashMap<>(); 71 | for (int j=0; j > pheromones = initializePheromone(tasks,vms); 79 | int kmin=0; 80 | for(int t=1;t<=tmax;t++){ 81 | tabu = new ArrayList<>(); 82 | 83 | Collections.shuffle(newVmList); 84 | 85 | for(int k=0;k()); 87 | tabu.get(k).put(-1,newVmList.get(k)); 88 | double max = 0; 89 | 90 | for(int task=0;tasklengths.get(k))?lengths.get(k):min; 105 | kmin = (min>lengths.get(k))?k:kmin; 106 | } 107 | 108 | updatePheromones(pheromones,lengths,tabu); 109 | globalUpdatePheromones(pheromones,min,tabu.get(kmin)); 110 | } 111 | return tabu.get(kmin); 112 | } 113 | 114 | public ACOImplement(int m, double initialPheromone, double Q, double alpha, double beta, double rho){ 115 | this.m = m; 116 | this.initialPheromone = initialPheromone; 117 | this.Q = Q; 118 | this.alpha = alpha; 119 | this.beta = beta; 120 | this.rho = rho; 121 | } 122 | 123 | protected int 124 | chooseVM(Map execTimes, Map pheromones, Map tabu){ 125 | 126 | Map probab = new HashMap<>(); 127 | double denominator = 0; 128 | 129 | for(int i=0;i > initializePheromone(int tasks, int vms){ 154 | Map > pheromones = new HashMap<>(); 155 | for(int i=0;i x = new HashMap<>(); 157 | for (int j=0; j > pheromones, List length, 166 | List> tabu){ 167 | Map > updatep = new HashMap<>(); 168 | 169 | for(int i=0;i v = new HashMap<>(); 171 | for(int j=0;j tour = new HashMap<>(); 180 | tour.putAll(tabu.get(k)); 181 | tour.remove(-1); 182 | // for(int i=0;i v = new HashMap<>(); 184 | // v.put(tabu.get(k).get(i), updateValue); 185 | // updatep.put(i,v); 186 | // } 187 | for(int i=0;i v = new HashMap<>(); 189 | for(int j=0;j x = pheromones.get(i); 201 | 202 | for (int j=0; j > pheromones, double length, Map tabu){ 210 | double updateValue = Q/length; 211 | for(int i=0;i v = pheromones.get(i); 213 | v.put(tabu.get(i),v.get(tabu.get(i))+updateValue); 214 | pheromones.put(i,v); 215 | } 216 | } 217 | 218 | protected double getExecutionTime(Vm VM, Cloudlet cloudlet){ 219 | return (cloudlet.getCloudletLength()/(VM.getNumberOfPes()*VM.getMips()) + cloudlet.getCloudletFileSize()/VM.getBw()); 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /src/LBACO.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim; 2 | import java.text.DecimalFormat; 3 | import java.util.*; 4 | import java.util.stream.*; 5 | import java.lang.*; 6 | import java.io.*; 7 | 8 | import org.cloudbus.cloudsim.Cloudlet; 9 | import org.cloudbus.cloudsim.CloudletSchedulerTimeShared; 10 | import org.cloudbus.cloudsim.Datacenter; 11 | import org.cloudbus.cloudsim.DatacenterBroker; 12 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 13 | import org.cloudbus.cloudsim.Host; 14 | import org.cloudbus.cloudsim.Log; 15 | import org.cloudbus.cloudsim.Pe; 16 | import org.cloudbus.cloudsim.Storage; 17 | import org.cloudbus.cloudsim.UtilizationModel; 18 | import org.cloudbus.cloudsim.UtilizationModelFull; 19 | import org.cloudbus.cloudsim.Vm; 20 | import org.cloudbus.cloudsim.VmAllocationPolicySimple; 21 | import org.cloudbus.cloudsim.VmSchedulerTimeShared; 22 | import org.cloudbus.cloudsim.core.CloudSim; 23 | import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 24 | import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 25 | import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 26 | 27 | 28 | public class LBACO{ 29 | protected double Q; 30 | protected double alpha; 31 | protected double beta; 32 | protected double gamma; 33 | protected double rho; 34 | protected int m; 35 | protected Random r; 36 | 37 | 38 | public Map implement(List taskList,List vmList,int tmax) throws FileNotFoundException{ 39 | int tasks = taskList.size(); 40 | int vms = vmList.size(); 41 | Map allocatedtasks = new HashMap<>(); 42 | Map > execTimes; 43 | Map cc, pheromones; 44 | 45 | execTimes = new HashMap<>(); 46 | cc = new HashMap<>(); 47 | 48 | for(int i=0;i x = new HashMap<>(); 50 | for (int j=0; j eet = new HashMap<>(); 67 | 68 | for(int i=0;i probab = new HashMap<>(); 74 | Map eetTemp = new HashMap<>(); 75 | Map lbfValues = new HashMap<>(); 76 | for(int i=0;i probab){ 126 | int []freq = new int[vms]; 127 | int sum = 0; 128 | 129 | for(int i=0;ifreq[i] && n<= freq[i+1]){ 142 | return i+1; 143 | } 144 | } 145 | return 0; 146 | } 147 | 148 | public LBACO(int m, double Q, double alpha, double beta, double gamma, double rho){ 149 | this.m = m; 150 | this.Q = Q; 151 | this.alpha = alpha; 152 | this.beta = beta; 153 | this.gamma = gamma; 154 | this.rho = rho; 155 | r = new Random(); 156 | } 157 | 158 | 159 | protected Map initializePheromone(Map cc){ 160 | Map pheromones = new HashMap<>(); 161 | 162 | for (int j=0; j