├── GenExcel.java ├── LuncherBaselinesTest.java ├── LuncherMM1.java ├── LuncherMMS.java ├── LuncherQLearning.java ├── QCloudletSchedulerSpaceShared.java ├── QDatacenter.java ├── QDatacenterBroker.java ├── README.md ├── Utilization.java ├── VirtualQueueSize.java ├── VmCloudletAssigner.java ├── VmCloudletAssignerFair.java ├── VmCloudletAssignerGreedy.java ├── VmCloudletAssignerRandom.java ├── VmCloudletAssignerReinforcementLearning.java ├── VmCloudletAssignermm1.java └── VmCloudletAssignermms.java /GenExcel.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | 13 | import org.apache.poi.ss.usermodel.Cell; 14 | import org.apache.poi.ss.usermodel.Row; 15 | import org.apache.poi.ss.usermodel.Workbook; 16 | import org.apache.poi.xssf.usermodel.XSSFRow; 17 | import org.apache.poi.xssf.usermodel.XSSFSheet; 18 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; 19 | 20 | public class GenExcel { 21 | private XSSFSheet sheet; 22 | private XSSFWorkbook hwb; 23 | private XSSFRow row; 24 | 25 | private File path_file = new File("cloudsim-3.0s"); 26 | private String path = path_file.getAbsolutePath(); 27 | 28 | private static String New_List_File="Q.xlsx"; 29 | private static GenExcel instance = new GenExcel(); 30 | Map> QList= new HashMap>(); 31 | public static GenExcel getInstance() { 32 | if (instance == null) 33 | instance = new GenExcel(); 34 | return instance; 35 | } 36 | 37 | public Map> init() 38 | { 39 | 40 | try { 41 | File file = new File(path + "Q.xlsx"); 42 | InputStream inputStream = new FileInputStream(file); 43 | String fileName = file.getName(); 44 | Workbook wb = null; 45 | 46 | hwb = new XSSFWorkbook(inputStream); 47 | //excel sheet 48 | sheet = hwb.getSheetAt(0); 49 | 50 | int firstRowIndex = sheet.getFirstRowNum(); 51 | int lastRowIndex = sheet.getLastRowNum(); 52 | for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){ 53 | Row row = sheet.getRow(rIndex); 54 | if(row != null){ 55 | int firstCellIndex = row.getFirstCellNum(); 56 | int lastCellIndex = row.getLastCellNum(); 57 | 58 | Cell cells=row.getCell(firstCellIndex); 59 | String values=""; 60 | if(cells!=null){ 61 | values=cells.toString(); 62 | QList.put(values, new HashMap()); 63 | 64 | }else{ 65 | //sheet.shiftRows(rIndex, sheet.getLastRowNum()+1,-1); 66 | QList.put("", new HashMap()); 67 | } 68 | 69 | System.out.println(values+"state"); 70 | 71 | for(int cIndex = firstCellIndex+1; cIndex < 400; cIndex ++){ 72 | Cell cell = row.getCell(cIndex); 73 | String value = ""; 74 | if(cell != null){ 75 | value = cell.toString(); 76 | QList.get(values).put(cIndex-1, Double.parseDouble(value)); 77 | System.out.print(value+"\t"+"action"); 78 | } else{ 79 | //sheet.shiftRows(rIndex+1, sheet.getLastRowNum()+1,-1); 80 | QList.get(values).put(cIndex-1, 0.0); 81 | } 82 | } 83 | System.out.println(); 84 | } else{ 85 | } 86 | } 87 | 88 | } catch (FileNotFoundException e) { 89 | hwb = new XSSFWorkbook(); 90 | //excel sheet 91 | sheet = hwb.createSheet(); 92 | 93 | } catch(IOException e){ 94 | e.printStackTrace(); 95 | } 96 | return QList; 97 | } 98 | 99 | public void fillData(Map> QList, String state_idx, int action_idx, double QValue) 100 | { 101 | File file = new File(path + "Q.xlsx"); 102 | int rownumber=sheet.getLastRowNum(); 103 | 104 | for (int i = 1; i <= rownumber; i++) { 105 | Row row = sheet.getRow(i); 106 | if(row!=null){ 107 | sheet.removeRow(row); 108 | } 109 | } 110 | 111 | int rows=0; 112 | int s=0; 113 | 114 | for(Map.Entry > me : QList.entrySet()) 115 | { 116 | Row row = sheet.createRow(rows); 117 | row.createCell(0).setCellValue(me.getKey()); 118 | //record 119 | for (int k = 0; k <= 40; k++) { 120 | if(me.getValue().containsKey(k)){ 121 | row.createCell(k+1).setCellValue(me.getValue().get(k)); 122 | } 123 | } 124 | rows ++; 125 | } 126 | } 127 | 128 | public void genExcel() 129 | { 130 | 131 | try { 132 | File file = new File(path + "Q.xlsx"); 133 | hwb.write(new FileOutputStream(file,false)); 134 | } catch (FileNotFoundException e) { 135 | e.printStackTrace(); 136 | } catch (IOException e) { 137 | e.printStackTrace(); 138 | } 139 | } 140 | 141 | 142 | 143 | 144 | 145 | } 146 | -------------------------------------------------------------------------------- /LuncherBaselinesTest.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.text.DecimalFormat; 4 | import java.util.ArrayList; 5 | import java.util.Calendar; 6 | import java.util.LinkedList; 7 | import java.util.List; 8 | 9 | import org.cloudbus.cloudsim.Cloudlet; 10 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 11 | import org.cloudbus.cloudsim.Host; 12 | import org.cloudbus.cloudsim.Log; 13 | import org.cloudbus.cloudsim.Pe; 14 | import org.cloudbus.cloudsim.Storage; 15 | import org.cloudbus.cloudsim.VmAllocationPolicySimple; 16 | import org.cloudbus.cloudsim.VmSchedulerTimeShared; 17 | import org.cloudbus.cloudsim.core.CloudSim; 18 | import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple; 19 | import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple; 20 | import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple; 21 | 22 | public class LuncherBaselinesTest { 23 | private static final int NUM_VM = 20; 24 | private static final int NUM_CLOUDLET = 1000; 25 | private static final double POISSON_LAMBDA = 10.0; 26 | private static final double LETS_WAVE_INTERVAL = 1.0; 27 | private static final int MAX_LENGTH_WAITING_QUEUE = 1; 28 | 29 | 30 | private static final int VM_MIPS[] = { 100, 150, 200,250,300,350,400,450,500,550, 31 | 600,650,700,750,800,850,900,950,1000,1000 32 | }; 33 | 34 | 35 | public static void main(String[] args) { 36 | Log.printLine("Starting..."); 37 | try { 38 | int num_user = 1; 39 | Calendar calendar = Calendar.getInstance(); 40 | boolean trace_flag = false; 41 | CloudSim.init(num_user, calendar, trace_flag); 42 | if (VM_MIPS.length != NUM_VM) { 43 | System.out.println("virtual machine MIPS number does not match the number of virtual machines, terminate!"); 44 | System.exit(0); 45 | } 46 | int numHost = 6; 47 | int numVm = NUM_VM; 48 | int vmMips[] = VM_MIPS; 49 | QDatacenter datacenter0 = createDatacenter("Datacenter_0", numHost,numVm, vmMips); 50 | 51 | // Three baselines algorithms 52 | 53 | // VmCloudletAssigner vmCloudletAssigner = new VmCloudletAssignerRandom(); 54 | 55 | // VmCloudletAssigner vmCloudletAssigner = new VmCloudletAssignerGreedy(); 56 | 57 | VmCloudletAssigner vmCloudletAssigner = new VmCloudletAssignerFair(); 58 | 59 | int numlets = NUM_CLOUDLET; 60 | int numPe = NUM_VM; 61 | double lambda = POISSON_LAMBDA; 62 | double numletWaveInterval = LETS_WAVE_INTERVAL; 63 | int cloudletWaitingQueueLength = MAX_LENGTH_WAITING_QUEUE; 64 | QDatacenterBroker globalBroker = new QDatacenterBroker( 65 | "QDatacenterBroker", vmCloudletAssigner, numlets, numPe, 66 | lambda, numletWaveInterval, vmMips,cloudletWaitingQueueLength); 67 | 68 | VirtualQueueSize.init(NUM_VM, cloudletWaitingQueueLength); 69 | 70 | CloudSim.startSimulation(); 71 | 72 | List newList = new LinkedList(); 73 | 74 | newList.addAll(globalBroker.getCloudletReceivedList()); 75 | 76 | Log.printLine("Total Cloudlets: " + numlets); 77 | printCloudletList(newList); 78 | 79 | CloudSim.stopSimulation(); 80 | printUtilization(datacenter0); 81 | 82 | Log.printLine("finished!"); 83 | 84 | } catch (Exception e) { 85 | e.printStackTrace(); 86 | Log.printLine("The simulation has been terminated due to an unexpected error"); 87 | 88 | } 89 | 90 | } 91 | 92 | 93 | 94 | private static QDatacenter createDatacenter(String name, int numHost,int numPe, int vmMips[]) { 95 | 96 | List hostList = new ArrayList(); 97 | 98 | int maxMips = 0; 99 | for (int mip : vmMips) 100 | if (mip > maxMips) 101 | maxMips = mip; 102 | 103 | int hostId = 0; 104 | int ram = 16384; 105 | long storage = 1000000; 106 | int bw = 100000; 107 | 108 | int vmtohostnumber=0; 109 | 110 | for (int i = 0; i < numHost; i++) { 111 | List peList = new ArrayList(); 112 | if(i storageList = new LinkedList(); 161 | 162 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 163 | arch, os, vmm, hostList, time_zone, cost, costPerMem,costPerStorage, costPerBw); 164 | 165 | QDatacenter datacenter = null; 166 | try { 167 | datacenter = new QDatacenter(name, characteristics, 168 | new VmAllocationPolicySimple(hostList), storageList, 0); 169 | } catch (Exception e) { 170 | e.printStackTrace(); 171 | } 172 | 173 | return datacenter; 174 | 175 | } 176 | 177 | public static void printUtilization(QDatacenter datacenter0){ 178 | List hosts=datacenter0.getHostList(); 179 | double totalutilization=0; 180 | double aveutilization=0; 181 | double aveu=0; 182 | double totalworkload=0; 183 | double aveworkload=0; 184 | int j=0; 185 | double power=0;double totalpower=0;double avepower=0;double totalpowers=0; 186 | for(Host host:hosts){ 187 | for(int i=0;i0.5){ 190 | power=(-7.79979*host.getStateHistory().get(i).getUtilization()+150.56995)*host.getStateHistory().get(i).getWorkLoad(); 191 | 192 | }else{ 193 | power=(132.47581*host.getStateHistory().get(i).getUtilization()+8.84754)*host.getStateHistory().get(i).getWorkLoad(); 194 | } 195 | totalpower=power+totalpower; 196 | } 197 | totalpowers=totalpower/host.getStateHistory().size(); 198 | aveutilization=totalutilization/host.getStateHistory().size(); 199 | totalutilization=0; 200 | j++; 201 | } 202 | aveu=totalutilization/hosts.size(); 203 | // avepower=totalpower/host.getStateHistory().size(); 204 | System.out.println("utilization:" + aveutilization);//print each host utilization 205 | System.out.println("Energy:" + totalpowers);//print each host energy 206 | } 207 | 208 | 209 | 210 | private static void printCloudletList(List list) { 211 | int size = list.size(); 212 | Cloudlet cloudlet; 213 | List finishTimeEachVm = new ArrayList(); 214 | List totalWaitingTimeEachVm = new ArrayList(); 215 | List totalUtilizingTimeEachVm = new ArrayList(); 216 | List numCloudletsEachVm = new ArrayList(); 217 | double finishtime=0; 218 | double watingtime=0; 219 | double wwatingtime=0; 220 | 221 | for (int i = 0; i < NUM_VM; i++) { 222 | finishTimeEachVm.add(0.0); 223 | totalWaitingTimeEachVm.add(0.0); 224 | totalUtilizingTimeEachVm.add(0.0); 225 | numCloudletsEachVm.add(0); 226 | 227 | } 228 | String indent = " "; 229 | int success = 0; 230 | 231 | DecimalFormat dft = new DecimalFormat("###.##"); 232 | for (int i = 0; i < size; i++) { 233 | cloudlet = list.get(i); 234 | // Log.print(indent + cloudlet.getCloudletId() + indent + indent); 235 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) { 236 | success++; 237 | totalWaitingTimeEachVm.set(cloudlet.getVmId(),totalWaitingTimeEachVm.get(cloudlet.getVmId())+ cloudlet.getWaitingTime()); 238 | watingtime=watingtime+cloudlet.getExecStartTime()-cloudlet.getArrivetime(); 239 | wwatingtime=wwatingtime+cloudlet.getFinishTime()-cloudlet.getArrivetime(); 240 | if (finishTimeEachVm.get(cloudlet.getVmId()) < cloudlet.getExecStartTime()) { 241 | finishTimeEachVm.set(cloudlet.getVmId(),cloudlet.getExecStartTime()); 242 | } 243 | if(finishtime newList = new LinkedList(); 74 | newList.addAll(globalBroker.getCloudletReceivedList()); 75 | 76 | Log.printLine("Total Cloudlets: " + numlets); 77 | printCloudletList(newList); 78 | 79 | CloudSim.stopSimulation(); 80 | 81 | Log.printLine("finished!"); 82 | 83 | } catch (Exception e) { 84 | e.printStackTrace(); 85 | Log.printLine("The simulation has been terminated due to an unexpected error"); 86 | 87 | } 88 | 89 | } 90 | 91 | 92 | 93 | private static QDatacenter createDatacenter(String name, int numHost,int numPe, int vmMips[]) { 94 | 95 | List hostList = new ArrayList(); 96 | 97 | int maxMips = 0; 98 | for (int mip : vmMips) 99 | if (mip > maxMips) 100 | maxMips = mip; 101 | 102 | int hostId = 0; 103 | int ram = 16384; 104 | long storage = 1000000; 105 | int bw = 10000; 106 | 107 | for (int i = 0; i < numHost; i++) { 108 | List peList = new ArrayList(); 109 | peList.add(new Pe(0,new PeProvisionerSimple(vmMips[i]))); 110 | hostList.add(new Host(hostId, new RamProvisionerSimple(ram), 111 | new BwProvisionerSimple(bw), storage, peList,new VmSchedulerTimeShared(peList))); 112 | hostId++; 113 | } 114 | 115 | String arch = "x86"; // system architecture 116 | String os = "Linux"; // operating system 117 | String vmm = "Xen"; 118 | double time_zone = 10.0; // time zone this resource located 119 | double cost = 3.0; // the cost of using processing in this resource 120 | double costPerMem = 0.05; // the cost of using memory in this resource 121 | double costPerStorage = 0.1; // the cost of using storage in this 122 | 123 | double costPerBw = 0.1; // the cost of using bw in this resource 124 | LinkedList storageList = new LinkedList(); 125 | 126 | 127 | 128 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 129 | 130 | arch, os, vmm, hostList, time_zone, cost, costPerMem,costPerStorage, costPerBw); 131 | 132 | 133 | 134 | QDatacenter datacenter = null; 135 | try { 136 | datacenter = new QDatacenter(name, characteristics, 137 | new VmAllocationPolicySimple(hostList), storageList, 0); 138 | } catch (Exception e) { 139 | e.printStackTrace(); 140 | } 141 | 142 | return datacenter; 143 | 144 | } 145 | 146 | 147 | 148 | private static void printCloudletList(List list) { 149 | int size = list.size(); 150 | Cloudlet cloudlet; 151 | List finishTimeEachVm = new ArrayList(); 152 | List totalWaitingTimeEachVm = new ArrayList(); 153 | List totalUtilizingTimeEachVm = new ArrayList(); 154 | List numCloudletsEachVm = new ArrayList(); 155 | double finishtime=0; 156 | double watingtime=0; 157 | double wwatingtime=0; 158 | double wate=0; 159 | 160 | for (int i = 0; i < NUM_VM; i++) { 161 | finishTimeEachVm.add(0.0); 162 | totalWaitingTimeEachVm.add(0.0); 163 | totalUtilizingTimeEachVm.add(0.0); 164 | numCloudletsEachVm.add(0); 165 | 166 | } 167 | String indent = " "; 168 | int success = 0; 169 | 170 | DecimalFormat dft = new DecimalFormat("###.##"); 171 | for (int i = 0; i < size; i++) { 172 | cloudlet = list.get(i); 173 | // Log.print(indent + cloudlet.getCloudletId() + indent + indent); 174 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) { 175 | success++; 176 | totalWaitingTimeEachVm.set(cloudlet.getVmId(),totalWaitingTimeEachVm.get(cloudlet.getVmId())+ cloudlet.getWaitingTime()); 177 | wwatingtime=wwatingtime+cloudlet.getFinishTime()-cloudlet.getArrivetime(); 178 | watingtime=watingtime+cloudlet.getExecStartTime()-cloudlet.getArrivetime(); 179 | 180 | if (finishTimeEachVm.get(cloudlet.getVmId()) < cloudlet.getExecStartTime()) { 181 | finishTimeEachVm.set(cloudlet.getVmId(),cloudlet.getExecStartTime()); 182 | 183 | } 184 | 185 | if(finishtime newList = new LinkedList(); 70 | 71 | newList.addAll(globalBroker.getCloudletReceivedList()); 72 | 73 | Log.printLine("Total Cloudlets: " + numlets); 74 | printCloudletList(newList); 75 | 76 | CloudSim.stopSimulation(); 77 | 78 | Log.printLine("finished!"); 79 | 80 | } catch (Exception e) { 81 | e.printStackTrace(); 82 | Log.printLine("The simulation has been terminated due to an unexpected error"); 83 | 84 | } 85 | 86 | } 87 | 88 | 89 | 90 | private static QDatacenter createDatacenter(String name, int numHost,int numPe, int vmMips[]) { 91 | 92 | List hostList = new ArrayList(); 93 | 94 | int maxMips = 0; 95 | for (int mip : vmMips) 96 | if (mip > maxMips) 97 | maxMips = mip; 98 | 99 | int hostId = 0; 100 | int ram = 16384; 101 | long storage = 1000000; 102 | int bw = 10000; 103 | 104 | for (int i = 0; i < numHost; i++) { 105 | List peList = new ArrayList(); 106 | peList.add(new Pe(0,new PeProvisionerSimple(vmMips[i]))); 107 | hostList.add(new Host(hostId, new RamProvisionerSimple(ram), 108 | new BwProvisionerSimple(bw), storage, peList,new VmSchedulerTimeShared(peList))); 109 | hostId++; 110 | } 111 | 112 | String arch = "x86"; // system architecture 113 | String os = "Linux"; // operating system 114 | String vmm = "Xen"; 115 | double time_zone = 10.0; // time zone this resource located 116 | double cost = 3.0; // the cost of using processing in this resource 117 | double costPerMem = 0.05; // the cost of using memory in this resource 118 | double costPerStorage = 0.1; // the cost of using storage in this 119 | 120 | double costPerBw = 0.1; // the cost of using bw in this resource 121 | LinkedList storageList = new LinkedList(); 122 | 123 | 124 | 125 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 126 | 127 | arch, os, vmm, hostList, time_zone, cost, costPerMem,costPerStorage, costPerBw); 128 | 129 | 130 | 131 | QDatacenter datacenter = null; 132 | try { 133 | datacenter = new QDatacenter(name, characteristics, 134 | new VmAllocationPolicySimple(hostList), storageList, 0); 135 | } catch (Exception e) { 136 | e.printStackTrace(); 137 | } 138 | 139 | return datacenter; 140 | 141 | } 142 | 143 | 144 | 145 | private static void printCloudletList(List list) { 146 | int size = list.size(); 147 | Cloudlet cloudlet; 148 | List finishTimeEachVm = new ArrayList(); 149 | List totalWaitingTimeEachVm = new ArrayList(); 150 | List totalUtilizingTimeEachVm = new ArrayList(); 151 | List numCloudletsEachVm = new ArrayList(); 152 | double finishtime=0; 153 | double watingtime=0; 154 | double wwatingtime=0; 155 | 156 | for (int i = 0; i < NUM_VM; i++) { 157 | finishTimeEachVm.add(0.0); 158 | totalWaitingTimeEachVm.add(0.0); 159 | totalUtilizingTimeEachVm.add(0.0); 160 | numCloudletsEachVm.add(0); 161 | 162 | } 163 | String indent = " "; 164 | int success = 0; 165 | 166 | DecimalFormat dft = new DecimalFormat("###.##"); 167 | for (int i = 0; i < size; i++) { 168 | cloudlet = list.get(i); 169 | // Log.print(indent + cloudlet.getCloudletId() + indent + indent); 170 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) { 171 | success++; 172 | totalWaitingTimeEachVm.set(cloudlet.getVmId(),totalWaitingTimeEachVm.get(cloudlet.getVmId())+ cloudlet.getWaitingTime()); 173 | 174 | wwatingtime=wwatingtime+cloudlet.getFinishTime()-cloudlet.getArrivetime(); 175 | 176 | watingtime=watingtime+cloudlet.getExecStartTime()-cloudlet.getArrivetime(); 177 | if (finishTimeEachVm.get(cloudlet.getVmId()) < cloudlet.getExecStartTime()) { 178 | finishTimeEachVm.set(cloudlet.getVmId(),cloudlet.getExecStartTime()); 179 | 180 | } 181 | if(finishtime newList = new LinkedList(); // Create a list that records the results of running cloud tasks 81 | 82 | newList.addAll(globalBroker.getCloudletReceivedList()); 83 | 84 | Log.printLine("Total Cloudlets: " + numlets); 85 | printCloudletList(newList); 86 | 87 | 88 | CloudSim.stopSimulation(); 89 | printUtilization(datacenter0); 90 | 91 | Log.printLine("finished!"); 92 | 93 | } catch (Exception e) { 94 | e.printStackTrace(); 95 | Log.printLine("The simulation has been terminated due to an unexpected error"); 96 | 97 | } 98 | } 99 | } 100 | 101 | 102 | 103 | private static QDatacenter createDatacenter(String name, int numHost,int numPe, int vmMips[]) { 104 | 105 | List hostList = new ArrayList(); 106 | 107 | int maxMips = 0; 108 | for (int mip : vmMips) 109 | if (mip > maxMips) 110 | maxMips = mip; 111 | 112 | int hostId = 0; // id of hosts 113 | int ram = 16384; // host ram 114 | long storage = 1000000; 115 | int bw = 100000; // host bandwidth 116 | 117 | 118 | 119 | int vmtohostnumber=0; 120 | 121 | for (int i = 0; i < numHost; i++) { // create hosts 122 | List peList = new ArrayList(); 123 | if(i storageList = new LinkedList(); 157 | 158 | 159 | 160 | DatacenterCharacteristics characteristics = new DatacenterCharacteristics( 161 | // set datacenter characteristics 162 | arch, os, vmm, hostList, time_zone, cost, costPerMem,costPerStorage, costPerBw); 163 | QDatacenter datacenter = null; 164 | try { 165 | datacenter = new QDatacenter(name, characteristics, // create datacenter 166 | new VmAllocationPolicySimple(hostList), storageList, 0); 167 | } catch (Exception e) { 168 | e.printStackTrace(); 169 | } 170 | 171 | return datacenter; 172 | 173 | } 174 | 175 | // print different host utilization and energy 176 | public static void printUtilization(QDatacenter datacenter0){ 177 | List hosts=datacenter0.getHostList(); 178 | double totalutilization=0;//utilization of each host 179 | double aveutilization=0;// average utilization of each host 180 | double aveu=0; 181 | double totalworkload=0;//workload of each host 182 | double aveworkload=0;//average workload of each host 183 | int j=0; 184 | double power=0;double totalpower=0;double avepower=0;double totalpowers=0; 185 | 186 | for(Host host:hosts){ 187 | for(int i=0;i0.5){ 192 | power=(-7.79979*host.getStateHistory().get(i).getUtilization()+150.56995)*host.getStateHistory().get(i).getWorkLoad(); 193 | 194 | }else{ 195 | power=(132.47581*host.getStateHistory().get(i).getUtilization()+8.84754)*host.getStateHistory().get(i).getWorkLoad(); 196 | } 197 | totalpower=power+totalpower; 198 | } 199 | totalpowers=totalpower/host.getStateHistory().size(); 200 | aveutilization=totalutilization/host.getStateHistory().size(); 201 | // aveworkload=totalworkload/host.getStateHistory().size(); 202 | totalutilization=0; 203 | j++; 204 | } 205 | aveu=totalutilization/hosts.size(); 206 | // avepower=totalpower/host.getStateHistory().size(); 207 | System.out.println("utilization:" + aveutilization);//print each host utilization 208 | System.out.println("Energy:" + totalpowers);//print each host energy 209 | } 210 | 211 | private static void printCloudletList(List list) { 212 | int size = list.size(); 213 | Cloudlet cloudlet; 214 | List finishTimeEachVm = new ArrayList(); // The time when the last cloud task in each virtual machine left the waiting queue 215 | List totalWaitingTimeEachVm = new ArrayList(); // The total waiting time of all cloud tasks in the waiting queue of each virtual machine 216 | List totalUtilizingTimeEachVm = new ArrayList(); // The total time consumed by all cloud tasks in each virtual machine 217 | List numCloudletsEachVm = new ArrayList(); // The total number of cloud tasks executed in each virtual machine 218 | double finishtime=0; 219 | double watingtime=0; 220 | double wwatingtime=0; 221 | 222 | for (int i = 0; i < NUM_VM; i++) { 223 | finishTimeEachVm.add(0.0); 224 | totalWaitingTimeEachVm.add(0.0); 225 | totalUtilizingTimeEachVm.add(0.0); 226 | numCloudletsEachVm.add(0); 227 | } 228 | 229 | String indent = " "; 230 | int success = 0; 231 | DecimalFormat dft = new DecimalFormat("###.##"); 232 | 233 | for (int i = 0; i < size; i++) { 234 | cloudlet = list.get(i); 235 | // Log.print(indent + cloudlet.getCloudletId() + indent + indent); 236 | if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) { 237 | success++; 238 | // Calculate the total waiting time in the queue of all cloud tasks in each physical machine 239 | totalWaitingTimeEachVm.set(cloudlet.getVmId(),totalWaitingTimeEachVm.get(cloudlet.getVmId())+ cloudlet.getWaitingTime()); 240 | watingtime=watingtime+cloudlet.getExecStartTime()-cloudlet.getArrivetime(); 241 | //waiting time 242 | wwatingtime=wwatingtime+cloudlet.getFinishTime()-cloudlet.getArrivetime(); 243 | if (finishTimeEachVm.get(cloudlet.getVmId()) < cloudlet.getExecStartTime()) { 244 | // Get the time when the last cloud task of each physical machine left the waiting queue 245 | finishTimeEachVm.set(cloudlet.getVmId(),cloudlet.getExecStartTime()); 246 | } 247 | if(finishtime cloudletWaitingQueue; 24 | 25 | private int cloudletWaitingQueueLength; 26 | private int cloudletHasRun; 27 | 28 | public QCloudletSchedulerSpaceShared(int vmId, int maxLength) { 29 | super(); 30 | setAverageWaitingTime(0); 31 | setPreviousAverageWaitingTime(0); 32 | cloudletWaitingQueue = new LinkedList(); 33 | setVmId(vmId); 34 | setCloudletWaitingQueueLength(maxLength); 35 | setVm(vm); 36 | cloudletHasRun = 0; 37 | } 38 | 39 | @Override 40 | public double updateVmProcessing(double currentTime, List mipsShare) { 41 | setCurrentMipsShare(mipsShare); 42 | 43 | 44 | // Log.printLine("updateVmProcessing Vm#" + getVmId()); 45 | double timeSpam = currentTime - getPreviousTime(); 46 | double capacity = 0.0; 47 | int cpus = 0; 48 | 49 | for (Double mips : mipsShare) { // count the CPUs available to the VMM 50 | capacity += mips; 51 | if (mips > 0) { 52 | cpus++; 53 | } 54 | } 55 | currentCpus = cpus; 56 | capacity /= cpus; // average capacity of each cpu 57 | 58 | // each machine in the exec list has the same amount of cpu 59 | for (ResCloudlet rcl : getCloudletExecList()) { 60 | rcl.updateCloudletFinishedSoFar((long) (capacity * timeSpam 61 | * rcl.getNumberOfPes() * 1000000)); 62 | 63 | } 64 | 65 | if (getCloudletExecList().size() == 0 && getCloudletWaitingQueue().size() == 0) { 66 | setPreviousTime(currentTime); 67 | return 0.0; 68 | } 69 | if (!(getCloudletExecList().size() == 0) ) { 70 | setT(currentTime); 71 | for (ResCloudlet rcl : getCloudletExecList()) { 72 | // Log.printLine(rcl.getCloudlet().getCloudletId()+" "+currentTime+" "+vmId); 73 | } 74 | } 75 | 76 | int finished = 0; 77 | List toRemove = new ArrayList(); 78 | for (ResCloudlet rcl : getCloudletExecList()) { 79 | // finished anyway, rounding issue... 80 | if (rcl.getRemainingCloudletLength() == 0) { 81 | toRemove.add(rcl); 82 | cloudletFinish(rcl); 83 | finished++; 84 | } 85 | } 86 | getCloudletExecList().removeAll(toRemove); 87 | 88 | //Task waiting queue is not empty, select tasks from the queue to execute 89 | if (!getCloudletWaitingQueue().isEmpty()) { 90 | for (int i = 0; i < finished; i++) { 91 | toRemove.clear(); 92 | for (ResCloudlet rcl : getCloudletWaitingQueue()) { 93 | if ((currentCpus - usedPes) >= rcl.getNumberOfPes()) { 94 | rcl.setCloudletStatus(Cloudlet.INEXEC); 95 | updateAverageWaitingTime(rcl.getCloudlet().getWaitingTime()); //update task waiting time 96 | 97 | virQueueSize.decrement(getVmId()); 98 | 99 | for (int k = 0; k < rcl.getNumberOfPes(); k++) { 100 | rcl.setMachineAndPeId(0, k); 101 | } 102 | getCloudletExecList().add(rcl); 103 | usedPes += rcl.getNumberOfPes(); 104 | getCloudletWaitingQueue().remove(rcl); 105 | 106 | break; 107 | } 108 | } 109 | } 110 | } 111 | 112 | //Estimate the time needed to complete the task in progress 113 | double nextEvent = Double.MAX_VALUE; 114 | for (ResCloudlet rcl : getCloudletExecList()) { 115 | double remainingLength = rcl.getRemainingCloudletLength(); 116 | double estimatedFinishTime = currentTime 117 | + (remainingLength / (capacity * rcl.getNumberOfPes())); 118 | if (estimatedFinishTime - currentTime < 0.1) { 119 | estimatedFinishTime = currentTime+ 0.1; 120 | } 121 | if (estimatedFinishTime < nextEvent) { 122 | nextEvent = estimatedFinishTime; 123 | } 124 | } 125 | 126 | setPreviousTime(currentTime); 127 | return nextEvent; 128 | } 129 | 130 | @Override 131 | public double cloudletSubmit(Cloudlet cloudlet, double fileTransferTime) { 132 | if ((currentCpus - usedPes) >= cloudlet.getNumberOfPes()) { 133 | ResCloudlet rcl = new ResCloudlet(cloudlet); 134 | updateAverageWaitingTime(cloudlet.getWaitingTime()); 135 | virQueueSize.decrement(getVmId()); 136 | 137 | rcl.setCloudletStatus(Cloudlet.INEXEC); 138 | 139 | for (int i = 0; i < cloudlet.getNumberOfPes(); i++) { 140 | rcl.setMachineAndPeId(0, i); 141 | } 142 | 143 | getCloudletExecList().add(rcl); 144 | usedPes += cloudlet.getNumberOfPes(); 145 | } else { 146 | ResCloudlet rcl = new ResCloudlet(cloudlet); 147 | rcl.setCloudletStatus(Cloudlet.QUEUED); 148 | if (addWaitingCloudlet(rcl)) 149 | return 0.0; 150 | else 151 | return -1.0; 152 | } 153 | 154 | double capacity = 0.0; 155 | int cpus = 0; 156 | for (Double mips : getCurrentMipsShare()) { 157 | capacity += mips; 158 | if (mips > 0) { 159 | cpus++; 160 | } 161 | } 162 | currentCpus = cpus; 163 | capacity /= cpus; 164 | 165 | // use the current capacity to estimate the extra amount of 166 | // time to file transferring. It must be added to the cloudlet length 167 | double extraSize = capacity * fileTransferTime; 168 | long length = cloudlet.getCloudletLength(); 169 | length += extraSize; 170 | cloudlet.setCloudletLength(length); 171 | return cloudlet.getCloudletLength() / capacity; 172 | } 173 | 174 | 175 | public boolean addWaitingCloudlet(ResCloudlet cloudlet) { 176 | 177 | if (getCloudletWaitingQueue().size() < getCloudletWaitingQueueLength()) { 178 | 179 | return getCloudletWaitingQueue().offer(cloudlet); 180 | 181 | } else { 182 | virQueueSize.decrement(getVmId()); 183 | 184 | Log.printLine("ERROR:VM #" + cloudlet.getCloudlet().getVmId() 185 | 186 | + " add Cloudlet #" + cloudlet.getCloudletId() 187 | 188 | + " FAILDED!! Queue Size :" 189 | 190 | + getCloudletWaitingQueue().size()); 191 | 192 | System.exit(0); 193 | 194 | return false; 195 | } 196 | } 197 | 198 | 199 | 200 | public ResCloudlet removeWaitingCloudlet() { 201 | 202 | return cloudletWaitingQueue.poll(); 203 | 204 | } 205 | 206 | 207 | 208 | private void updateAverageWaitingTime(double newWaitingTime) { 209 | 210 | if(newWaitingTime < 0) newWaitingTime = 0; 211 | setAverageWaitingTime((getAverageWaitingTime() 212 | * cloudletHasRun + newWaitingTime) 213 | / (cloudletHasRun + 1)); 214 | cloudletHasRun++; 215 | setPreviousAverageWaitingTime(getAverageWaitingTime()); 216 | 217 | } 218 | 219 | 220 | 221 | public double getAverageWaitingTime() { 222 | 223 | return aveWaitingTime; 224 | 225 | } 226 | 227 | 228 | 229 | public void setAverageWaitingTime(double averageWaitingTime) { 230 | 231 | this.aveWaitingTime = averageWaitingTime; 232 | 233 | } 234 | 235 | 236 | public void setPreviousAverageWaitingTime(double PreviousAverageWaitingTime){ 237 | this.PreviousAverageWaitingTime=PreviousAverageWaitingTime; 238 | } 239 | 240 | 241 | public double getPreviousAverageWaitingTime(){ 242 | return PreviousAverageWaitingTime; 243 | } 244 | 245 | public Queue getCloudletWaitingQueue() { 246 | 247 | return cloudletWaitingQueue; 248 | 249 | } 250 | 251 | 252 | 253 | public int getCloudletWaitingQueueLength() { 254 | 255 | return cloudletWaitingQueueLength; 256 | 257 | } 258 | 259 | 260 | 261 | public void setCloudletWaitingQueueLength(int cloudletWaitingQueueLength) { 262 | 263 | this.cloudletWaitingQueueLength = cloudletWaitingQueueLength; 264 | 265 | } 266 | 267 | 268 | 269 | public int getVm() { 270 | 271 | return vmId; 272 | 273 | } 274 | 275 | 276 | 277 | public void setVm(Vm vm) { 278 | 279 | this.vmId = vmId; 280 | 281 | } 282 | 283 | public int getVmId() { 284 | 285 | return vmId; 286 | 287 | } 288 | 289 | 290 | 291 | public void setVmId(int vmId) { 292 | 293 | this.vmId = vmId; 294 | 295 | } 296 | 297 | public void setT(double t){ 298 | this.t=t; 299 | } 300 | 301 | public double getT(){ 302 | return t; 303 | } 304 | } 305 | -------------------------------------------------------------------------------- /QDatacenter.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.List; 4 | 5 | import org.cloudbus.cloudsim.Cloudlet; 6 | import org.cloudbus.cloudsim.CloudletScheduler; 7 | import org.cloudbus.cloudsim.Datacenter; 8 | import org.cloudbus.cloudsim.DatacenterCharacteristics; 9 | import org.cloudbus.cloudsim.Host; 10 | import org.cloudbus.cloudsim.Log; 11 | import org.cloudbus.cloudsim.Storage; 12 | import org.cloudbus.cloudsim.Vm; 13 | import org.cloudbus.cloudsim.VmAllocationPolicy; 14 | import org.cloudbus.cloudsim.core.CloudSim; 15 | import org.cloudbus.cloudsim.core.CloudSimTags; 16 | import org.cloudbus.cloudsim.core.SimEvent; 17 | import org.cloudbus.cloudsim.power.PowerDatacenter; 18 | 19 | public class QDatacenter extends Datacenter{ 20 | 21 | public static final int CLOUDLET_SUBMIT_FAILED = 51; 22 | public QDatacenter(String name, DatacenterCharacteristics characteristics, 23 | VmAllocationPolicy vmAllocationPolicy, List storageList, 24 | double schedulingInterval) throws Exception { 25 | super(name, characteristics, vmAllocationPolicy, storageList, 26 | schedulingInterval); 27 | // TODO Auto-generated constructor stub 28 | } 29 | 30 | @Override 31 | protected void processCloudletSubmit(SimEvent ev, boolean ack) { 32 | updateCloudletProcessing(); 33 | try { 34 | Cloudlet cl = (Cloudlet) ev.getData(); 35 | 36 | // process this Cloudlet to this CloudResource 37 | cl.setResourceParameter(getId(), getCharacteristics().getCostPerSecond(), getCharacteristics() 38 | .getCostPerBw()); 39 | int userId = cl.getUserId(); 40 | int vmId = cl.getVmId(); 41 | // time to transfer the files 42 | double fileTransferTime = predictFileTransferTime(cl.getRequiredFiles()); 43 | Host host = getVmAllocationPolicy().getHost(vmId, userId); 44 | Vm vm = host.getVm(vmId, userId); 45 | 46 | //submit tasks to CloudletScheduler 47 | CloudletScheduler scheduler = vm.getCloudletScheduler(); 48 | double estimatedFinishTime = ((QCloudletSchedulerSpaceShared)scheduler).cloudletSubmit(cl, fileTransferTime); 49 | // submit success 50 | if (estimatedFinishTime > 0.0 && !Double.isInfinite(estimatedFinishTime)) { 51 | estimatedFinishTime += fileTransferTime; 52 | send(getId(), estimatedFinishTime, CloudSimTags.VM_DATACENTER_EVENT); 53 | }else{ 54 | if(estimatedFinishTime < 0.0){ 55 | Log.printLine("Cloudlet submit Failed! QDatacenter sends Cloudlet Vm# "+cl.vmId+" to Broker"); 56 | send(cl.getUserId(), estimatedFinishTime, CLOUDLET_SUBMIT_FAILED,cl); 57 | System.exit(0); 58 | } 59 | } 60 | if (ack) { 61 | int[] data = new int[3]; 62 | data[0] = getId(); 63 | data[1] = cl.getCloudletId(); 64 | data[2] = CloudSimTags.TRUE; 65 | 66 | // unique tag = operation tag 67 | int tag = CloudSimTags.CLOUDLET_SUBMIT_ACK; 68 | sendNow(cl.getUserId(), tag, data); 69 | } 70 | } catch (ClassCastException c) { 71 | Log.printLine(getName() + ".processCloudletSubmit(): " + "ClassCastException error."); 72 | c.printStackTrace(); 73 | 74 | } catch (Exception e) { 75 | Log.printLine(getName() + ".processCloudletSubmit(): " + "Exception error."); 76 | e.printStackTrace(); 77 | } 78 | checkCloudletCompletion(); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /QDatacenterBroker.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | 7 | import org.cloudbus.cloudsim.Cloudlet; 8 | import org.cloudbus.cloudsim.DatacenterBroker; 9 | import org.cloudbus.cloudsim.Log; 10 | import org.cloudbus.cloudsim.ResCloudlet; 11 | import org.cloudbus.cloudsim.UtilizationModel; 12 | import org.cloudbus.cloudsim.UtilizationModelFull; 13 | import org.cloudbus.cloudsim.Vm; 14 | import org.cloudbus.cloudsim.core.CloudSim; 15 | import org.cloudbus.cloudsim.core.CloudSimTags; 16 | import org.cloudbus.cloudsim.core.SimEvent; 17 | import org.cloudbus.cloudsim.lists.VmList; 18 | import org.cloudbus.cloudsim.power.PowerDatacenterBroker; 19 | 20 | public class QDatacenterBroker extends DatacenterBroker { 21 | 22 | 23 | private VmCloudletAssigner vmCloudletAssigner; // policy of task assignment 24 | private static final int CREATE_CLOUDLETS = 49; 25 | private static List delayList; // List of delay times submitted by each wave of cloud tasks 26 | private static List numLetList; 27 | private int currWave; 28 | private int numlets; 29 | private int numVm; 30 | private int vmMips[]; 31 | private double lambda; 32 | private double numletWaveInterval; 33 | private static int cloudletWaitingQueueLength; 34 | 35 | public QDatacenterBroker(String name, 36 | VmCloudletAssigner vmCloudletAssigner, int numlets, int numVm, 37 | double lambda, double numletWaveInterval,int[] vmMips, 38 | int cloudletWaitingQueueLength) throws Exception { 39 | super(name); 40 | setVmCloudletAssigner(vmCloudletAssigner); 41 | this.numlets = numlets; 42 | this.numVm = numVm; 43 | this.vmMips = vmMips; 44 | this.lambda = lambda; 45 | this.numletWaveInterval = numletWaveInterval; 46 | this.cloudletWaitingQueueLength = cloudletWaitingQueueLength; 47 | currWave = 0; 48 | } 49 | 50 | 51 | 52 | @Override 53 | 54 | public void processOtherEvent(SimEvent ev) { 55 | switch (ev.getTag()) { 56 | case CREATE_CLOUDLETS: // The generation and arrival of a new wave of cloud tasks 57 | int waveId = ((Integer) ev.getData()).intValue(); // Get the current wave number 58 | System.out.println(CloudSim.clock() + ":" + (waveId + 1)+ " cloudlet start arrive"); 59 | double time=CloudSim.clock(); 60 | 61 | submitCloudletList(createCloudlets(getId(), numLetList.get(waveId),waveId * 1000 + 1000)); // Generate a wave of cloud tasks 62 | if(waveId>0){ 63 | for (int s=0;s 0) { 72 | submitCloudlets(); 73 | } 74 | CloudSim.resumeSimulation(); 75 | // Log.printLine(getCloudletList().size()+"gggsss"); 76 | for(Cloudlet c:getCloudletList()){ 77 | c.setArrivetime(time); 78 | // Log.printLine(time); 79 | } 80 | break; 81 | case QDatacenter.CLOUDLET_SUBMIT_FAILED: 82 | cloudletSubmitFailed(ev); 83 | break; 84 | default: 85 | Log.printLine(getName() + ": unknown event type"); 86 | break; 87 | } 88 | } 89 | 90 | 91 | 92 | @Override 93 | public void startEntity() { 94 | Log.printLine(super.getName() + " is starting..."); 95 | try { 96 | createCloudletWave(numlets, lambda, numletWaveInterval); //Generate a delay list for the start of each wave cloud task 97 | } catch (Exception e) { 98 | System.out.println("Error generating cloud task queue!"); 99 | e.printStackTrace(); 100 | } 101 | setVmList(createVM(getId(), numVm, vmMips, 0)); 102 | 103 | for (int i = 0; i < delayList.size(); i++) { 104 | schedule(getId(), delayList.get(i), CREATE_CLOUDLETS, i); 105 | } 106 | schedule(getId(), 0, CloudSimTags.RESOURCE_CHARACTERISTICS_REQUEST); 107 | } 108 | 109 | 110 | 111 | @Override 112 | protected void submitCloudlets() { 113 | // Bind tasks to virtual machines 114 | Log.printLine("SSSSubmitCloudlet() size: " + getCloudletList().size()); 115 | 116 | if(getCloudletList().size()==0) return; 117 | List assignedCloudletList = getVmCloudletAssigner().cloudletAssign(getCloudletList(), getVmList()); //Assign tasks to virtual machines by task distributor 118 | 119 | for (Cloudlet cloudlet : assignedCloudletList) { 120 | Vm vm; 121 | if (cloudlet.getVmId() != -1) { 122 | vm = VmList.getById(getVmsCreatedList(), cloudlet.getVmId()); 123 | if (vm == null) { // vm was not created 124 | Log.printLine(CloudSim.clock() + ": " + getName() 125 | + ": Postponing execution of cloudlet " 126 | + cloudlet.getCloudletId() 127 | + ": bount VM not available"); 128 | continue; 129 | } 130 | Log.printLine(CloudSim.clock() + ": " + getName() 131 | + ": Sending cloudlet " + cloudlet.getCloudletId() 132 | + " to VM #" + vm.getId()); 133 | sendNow(getVmsToDatacentersMap().get(vm.getId()), 134 | CloudSimTags.CLOUDLET_SUBMIT, cloudlet); 135 | cloudletsSubmitted++; 136 | getCloudletSubmittedList().add(cloudlet); 137 | } 138 | } 139 | getCloudletList().clear(); //The task has been submitted to the task dispatcher, either has been allocated or entered the main task queue 140 | } 141 | 142 | 143 | 144 | @Override 145 | 146 | protected void processCloudletReturn(SimEvent ev) { 147 | Cloudlet cloudlet = (Cloudlet) ev.getData(); 148 | getCloudletReceivedList().add(cloudlet); 149 | 150 | cloudletsSubmitted--; 151 | List assignedCloudletList = getVmCloudletAssigner().cloudletAssign(null, getVmList()); 152 | // Schedule a task from the main queue 153 | if (assignedCloudletList != null) { 154 | for (Cloudlet cl : assignedCloudletList) { 155 | Vm vm; 156 | if (cl.getVmId() != -1) { 157 | vm = VmList.getById(getVmsCreatedList(), cl.getVmId()); 158 | Log.printLine(CloudSim.clock() + ": " + getName() 159 | + ": Sending cloudlet " + cl.getCloudletId() 160 | + " to VM #" + vm.getId()); 161 | sendNow(getVmsToDatacentersMap().get(vm.getId()),CloudSimTags.CLOUDLET_SUBMIT, cl); 162 | cloudletsSubmitted++; 163 | getCloudletSubmittedList().add(cl); 164 | }else{ 165 | //Log.printLine("assignedCloudletList Assign Error! Cloudlet#"+cl.getCloudletId()); 166 | } 167 | } 168 | } else { 169 | //Log.printLine("CloudletReturn Assign NULL!"); 170 | } 171 | 172 | 173 | if (getCloudletList().size() == 0 && cloudletsSubmitted == 0) { 174 | if (currWave < delayList.size()) { 175 | System.out.println((delayList.size() - currWave)+ "wave have not arrived......"); 176 | return; 177 | } 178 | 179 | Log.printLine(CloudSim.clock() + ": " + getName()+ ": All Cloudlets executed. Finishing..."); 180 | clearDatacenters(); 181 | finishExecution(); 182 | 183 | } else { 184 | if (getCloudletList().size() > 0 && cloudletsSubmitted == 0) { 185 | for (Vm vm : getVmsCreatedList()) { 186 | Log.printLine("QueueLeft:"+ ((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()) 187 | .getCloudletWaitingQueue().size()); 188 | } 189 | if (currWave < delayList.size()) { 190 | System.out.println("submit" + currWave + "wave tasks......"); 191 | return; 192 | 193 | } 194 | 195 | clearDatacenters(); 196 | createVmsInDatacenter(0); 197 | 198 | } 199 | 200 | } 201 | 202 | 203 | 204 | } 205 | 206 | 207 | 208 | protected void cloudletSubmitFailed(SimEvent ev) { 209 | Cloudlet cloudlet = (Cloudlet) ev.getData(); 210 | cloudletsSubmitted--; 211 | Log.printLine("\nQDatacenterBroker received CLOUDLET " 212 | + cloudlet.getCloudletId() + " Failed cloudletList Size:" 213 | + getCloudletList().size()); 214 | List cloudletList = new ArrayList(); 215 | cloudletList.add(cloudlet); 216 | submitCloudletList(cloudletList); 217 | Log.printLine("After submit cloudletList Size:"+ getCloudletList().size()); 218 | submitCloudlets(); 219 | } 220 | 221 | 222 | 223 | public VmCloudletAssigner getVmCloudletAssigner() { 224 | return vmCloudletAssigner; 225 | } 226 | 227 | 228 | 229 | public void setVmCloudletAssigner(VmCloudletAssigner vmCloudletAssigner) { 230 | this.vmCloudletAssigner = vmCloudletAssigner; 231 | } 232 | 233 | 234 | 235 | private static List createVM(int userId, int vms,int[] vmMips, int idShift) { 236 | LinkedList list = new LinkedList(); 237 | long size = 10000; // image size (MB) 238 | int ram = 512; // vm memory (MB) 239 | int mips = 1000;// 250; 240 | long bw = 1000; 241 | int pesNumber = 1; // number of cpus 242 | String vmm = "Xen"; // VMM name 243 | Vm[] vm = new Vm[vms]; 244 | 245 | for (int i = 0; i < vms; i++) { 246 | vm[i] = new Vm(idShift + i, userId, vmMips[i], pesNumber, ram, bw, size,vmm, new QCloudletSchedulerSpaceShared(idShift + i, 247 | cloudletWaitingQueueLength)); 248 | list.add(vm[i]); 249 | 250 | } 251 | return list; 252 | 253 | } 254 | 255 | 256 | public static List createCloudlets(int userId, int cloudlets,int idShift) { 257 | LinkedList list = new LinkedList(); 258 | long length = 4000; 259 | long fileSize = 300; 260 | long outputSize = 300; 261 | int pesNumber = 1; 262 | double deadline=100; 263 | UtilizationModel utilizationModel = new UtilizationModelFull(); 264 | Cloudlet[] cloudlet = new Cloudlet[cloudlets]; 265 | for (int i = 0; i < cloudlets; i++) { 266 | length=(long)(2500*Math.random()+500);//[500,3000] 267 | fileSize=(long)(500*Math.random()+1);//[1,512] 268 | outputSize=(long)(500*Math.random()+1);//[1,512] 269 | deadline=(int)(70*Math.random()+50);//[50,120] 270 | cloudlet[i] = new Cloudlet(idShift + i, length, pesNumber,fileSize, outputSize, utilizationModel, utilizationModel,utilizationModel); 271 | cloudlet[i].setUserId(userId); 272 | cloudlet[i].setDeadline(deadline); 273 | list.add(cloudlet[i]); 274 | } 275 | return list; 276 | 277 | } 278 | 279 | public static List createCloudlet(int userId, int cloudlets,int idShift) { 280 | LinkedList list = new LinkedList(); 281 | long length = 1000; 282 | long fileSize = 300; 283 | long outputSize = 300; 284 | int pesNumber = 1; 285 | UtilizationModel utilizationModel = new UtilizationModelFull(); 286 | Cloudlet[] cloudlet = new Cloudlet[cloudlets]; 287 | for (int i = 0; i < cloudlets; i++) { 288 | cloudlet[i] = new Cloudlet(idShift + i, length, pesNumber,fileSize, outputSize, utilizationModel, utilizationModel,utilizationModel); 289 | cloudlet[i].setUserId(userId); 290 | list.add(cloudlet[i]); 291 | } 292 | return list; 293 | 294 | } 295 | 296 | 297 | 298 | public static double f_Poisson(double lambda, int k) { 299 | double e = 2.7182818284; 300 | double result; 301 | result = Math.pow(e, -lambda) * Math.pow(lambda, k); 302 | for (int i = 1; i <= k; i++) { 303 | result = result / i; 304 | } 305 | return result; 306 | 307 | } 308 | 309 | 310 | 311 | public static void createCloudletWave(int numlets, double lambda, 312 | double numletWaveInterval) throws Exception { 313 | int numLetWave = 100000; 314 | ArrayList numLet = new ArrayList(); 315 | int tmpCloudlets = 0; 316 | double tmp = numlets; 317 | for (int i = 0; i <= numLetWave; i++) { 318 | int x = 0; 319 | double y = Math.random(), cdf = f_Poisson(lambda, x); 320 | while (cdf < y) { 321 | x++; 322 | cdf += f_Poisson(lambda, x); 323 | } 324 | numLet.add(i, x); 325 | // numLet.add(i, (int) (tmp * f_Poisson(lambda, i))); 326 | if (numLet.get(i) <= 0) { 327 | numLet.set(i, 1); 328 | } 329 | if ((tmpCloudlets + numLet.get(i)) > numlets) { 330 | numLet.set(i, numlets - tmpCloudlets); 331 | System.out.println("adjust to: numLet[" + i + "]: "+ numLet.get(i) + "\tlambda: " + lambda+ "\tf_Poisson: " + f_Poisson(lambda, i)); 332 | break; 333 | 334 | } 335 | 336 | tmpCloudlets += numLet.get(i); 337 | System.out.println("numLet[" + i + "]: " + numLet.get(i)+ "\tlambda: " + lambda + "\tf_Poisson: "+ f_Poisson(lambda, i)); 338 | 339 | } 340 | 341 | delayList = new LinkedList(); 342 | numLetList = new LinkedList(); 343 | for (int i = 0; i < numLet.size(); i++) { 344 | delayList.add(i, numletWaveInterval * i); 345 | numLetList.add(i, numLet.get(i)); 346 | } 347 | } 348 | 349 | public static List getNumLetList() { 350 | return numLetList; 351 | } 352 | 353 | 354 | public static void setNumLetList(List numLetList) { 355 | QDatacenterBroker.numLetList = numLetList; 356 | } 357 | 358 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Q-learning-based-Dynamic-task-scheduling-for-energy-efficient-cloud-computing 2 | We propose a Q-learning based task scheduling framework for energy-efficient cloud computing (QEEC). 3 | QEEC has two phases. In the first phase a centralized task dispatcher is used to implement the M/M/S queueing model, by which the arriving user requests are assigned to each server in a cloud. In the second phase a Q-learning based scheduler on each server first prioritizes all the requests by task laxity and task life time, then uses a continuously-updating policy to assign tasks to virtual machines, applying incentives to reward the assignments that can minimize task response time and maximize each server’s CPU utilization. 4 | 5 | We open sourced the code of our team’s paper ‘Q-learning based Dynamic task scheduling for energy efficient cloud computing', so as to facilitate more scholars to understand the implementation details of the QEEC framework in CloudSim cloud enviroment. 6 | 7 | ## Citation 8 | Our paper has been published in Future Generation Computer System and you can cite as follow: 9 | ``` 10 | @article{DingFZKYZ20, 11 | author = {Ding Ding and 12 | Xiaocong Fan and 13 | Yihuan Zhao and 14 | Kaixuan Kang and 15 | Qian Yin and 16 | Jing Zeng}, 17 | title = {Q-learning based dynamic task scheduling for energy-efficient cloud 18 | computing}, 19 | journal = {Future Generation Computer System}, 20 | volume = {108}, 21 | pages = {361--371}, 22 | year = {2020}, 23 | url = {https://doi.org/10.1016/j.future.2020.02.018}, 24 | doi = {10.1016/j.future.2020.02.018}, 25 | } 26 | ``` 27 | ## Environment 28 | eclipse + java or you can run as a jar file. 29 | 30 | ## Main code file introduction 31 | 1. Use the LuncherMM1.java and LuncherMMS.java files to verfy that the M/M/S model can offer shorter task response time than the M/M/1 model under the same conditions. 32 | 2. Run the LuncherQlearning.java for getting QEEC framework results and LuncherBaselinesTest.java for baseline algorithms(fair, random, and greedy) results obtain. 33 | 3. VmCloudletAssignerXXX.java contains different algorithms'(mentioned above) implementation. 34 | 4. We put these .java files under cloudsim-3.0s/examples/org/cloudbus/cloudsim/examples/ dir. 35 | -------------------------------------------------------------------------------- /Utilization.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Utilization { 7 | private static List Utilization=new ArrayList(); 8 | private static List Utilization1=new ArrayList(); 9 | private static List Utilization2=new ArrayList(); 10 | private static List Utilization3=new ArrayList(); 11 | 12 | public static List getUtilization() { 13 | return Utilization; 14 | } 15 | public static void setUtilization(List utilization) { 16 | Utilization = utilization; 17 | } 18 | public static List getUtilization1() { 19 | return Utilization1; 20 | } 21 | public static void setUtilization1(List utilization1) { 22 | Utilization1 = utilization1; 23 | } 24 | public static List getUtilization2() { 25 | return Utilization2; 26 | } 27 | public static void setUtilization2(List utilization2) { 28 | Utilization2 = utilization2; 29 | } 30 | public static List getUtilization3() { 31 | return Utilization3; 32 | } 33 | public static void setUtilization3(List utilization3) { 34 | Utilization3 = utilization3; 35 | } 36 | 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /VirtualQueueSize.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class VirtualQueueSize { 7 | private static VirtualQueueSize instance = new VirtualQueueSize(); 8 | private static List queueSize; 9 | private static int maxLength; 10 | 11 | 12 | public VirtualQueueSize() { 13 | super(); 14 | } 15 | 16 | public static VirtualQueueSize getInstance() { 17 | if (instance == null) 18 | instance = new VirtualQueueSize(); 19 | return instance; 20 | } 21 | 22 | public static void init(int numVm, int length) { 23 | 24 | queueSize = new ArrayList(); 25 | for (int i = 0; i < numVm; i++) { 26 | queueSize.add(0); 27 | } 28 | setMaxLength(length); 29 | } 30 | 31 | public static boolean increment(int id) { 32 | 33 | //Log.printLine("Increment Before Id#" + id +" "+ getQueueSize().get(id)); 34 | if (getQueueSize().get(id) >= getMaxLength()) 35 | return false; 36 | getQueueSize().set(id, getQueueSize().get(id) + 1); 37 | 38 | //Log.printLine("Increment After Id#" + id +" "+ getQueueSize().get(id)); 39 | return true; 40 | 41 | } 42 | 43 | 44 | 45 | public static boolean decrement(int id){ 46 | 47 | //Log.printLine("Decrement Before Id#" + id +" "+ getQueueSize().get(id)); 48 | if (getQueueSize().get(id) <= 0) 49 | return false; 50 | //System.exit(0); 51 | 52 | getQueueSize().set(id, getQueueSize().get(id) - 1); 53 | //Log.printLine("Decrement After Id#" + id +" "+ getQueueSize().get(id)); 54 | return true; 55 | 56 | } 57 | 58 | 59 | 60 | public static List getQueueSize() { 61 | return queueSize; 62 | } 63 | 64 | 65 | 66 | public static int getMaxLength() { 67 | return maxLength; 68 | 69 | } 70 | 71 | 72 | 73 | public static void setMaxLength(int maxLength) { 74 | VirtualQueueSize.maxLength = maxLength; 75 | 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /VmCloudletAssigner.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Queue; 9 | 10 | import org.cloudbus.cloudsim.Cloudlet; 11 | import org.cloudbus.cloudsim.Vm; 12 | 13 | public abstract class VmCloudletAssigner { 14 | 15 | 16 | 17 | protected VirtualQueueSize vQueueSize = VirtualQueueSize.getInstance(); // Global variable of virtual subqueue length 18 | protected static Queue globalCloudletWaitingQueue = new LinkedList(); //Main task queue 19 | 20 | 21 | 22 | public abstract List cloudletAssign(List cloudletList, List vmList);//Cloud task assign policy 23 | 24 | 25 | 26 | public static Queue getGlobalCloudletWaitingQueue() { //Get the global task waiting queue 27 | return globalCloudletWaitingQueue; 28 | 29 | } 30 | 31 | 32 | 33 | public static void setGlobalCloudletWaitingQueue( //Set the global task waiting queue 34 | Queue globalCloudletWaitingQueue) { 35 | VmCloudletAssigner.globalCloudletWaitingQueue = globalCloudletWaitingQueue; 36 | } 37 | 38 | 39 | 40 | protected List getToAssignCloudletList(List cloudletList) { //Generate a cloud task list that waiting to be assigned 41 | List toAssignCloudletList = new ArrayList(); //List of cloud tasks waiting to be configured 42 | if (cloudletList != null) { 43 | System.out.println("assign cloudletList tasks " + cloudletList.size()); 44 | if (getGlobalCloudletWaitingQueue().size() != 0) { //The global task waiting queue is not empty 45 | 46 | for (int i = 0; i < getGlobalCloudletWaitingQueue().size(); i++) //Assign the tasks in the global cloud task waiting queue to the cloud task list 47 | toAssignCloudletList.add(getGlobalCloudletWaitingQueue().poll()); 48 | } 49 | 50 | toAssignCloudletList.addAll(cloudletList); 51 | } else { 52 | 53 | if (getGlobalCloudletWaitingQueue().size() != 0) { 54 | toAssignCloudletList.add(getGlobalCloudletWaitingQueue().poll()); 55 | } else { 56 | return toAssignCloudletList; 57 | } 58 | 59 | } 60 | return toAssignCloudletList; 61 | } 62 | 63 | 64 | 65 | protected List> initVmWaitingQueueSizeList() { //Initialize the virtual machine waiting queue length list 66 | List virQueueSize = vQueueSize.getQueueSize(); 67 | List> vmWaitingQueueSizeList = new ArrayList>(); 68 | Map queueSize; 69 | for (int i = 0; i < virQueueSize.size(); i++) { 70 | queueSize = new HashMap(); 71 | queueSize.put("id", i); 72 | queueSize.put("size", virQueueSize.get(i)); 73 | vmWaitingQueueSizeList.add(queueSize); 74 | } 75 | return vmWaitingQueueSizeList; 76 | } 77 | 78 | 79 | 80 | protected List getAssignedCloudletList(int success, List toAssignCloudletList) { // Generate successfully assigned task list 81 | List assignedCloudletList = new ArrayList();//Successfully assigned task list 82 | for (int j = 0; j < success; j++)//The first success tasks were successfully assigned 83 | assignedCloudletList.add(toAssignCloudletList.get(j)); 84 | toAssignCloudletList.removeAll(assignedCloudletList);// Delete successfully assigned task 85 | return assignedCloudletList; 86 | } 87 | 88 | 89 | 90 | protected void finishAssign(List toAssignCloudletList){ //task assignment end 91 | for (int j = 0; j < toAssignCloudletList.size(); j++) { //Unsuccessfully assigned tasks are returned to the main task queue 92 | getGlobalCloudletWaitingQueue().offer(toAssignCloudletList.get(j)); 93 | } 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /VmCloudletAssignerFair.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | import java.util.List; 3 | import java.util.Map; 4 | 5 | import org.cloudbus.cloudsim.Cloudlet; 6 | import org.cloudbus.cloudsim.CloudletSchedulerSpaceShared; 7 | import org.cloudbus.cloudsim.Log; 8 | import org.cloudbus.cloudsim.ResCloudlet; 9 | import org.cloudbus.cloudsim.Vm; 10 | public class VmCloudletAssignerFair extends VmCloudletAssigner { 11 | 12 | 13 | @Override 14 | public List cloudletAssign(List cloudletList, 15 | List vmList) { 16 | if (vmList != null || vmList.size() != 0) { 17 | List toAssignCloudletList = getToAssignCloudletList(cloudletList); 18 | if (toAssignCloudletList.size() < 1) { 19 | return null; 20 | // System.exit(0); 21 | } 22 | 23 | int m = vmList.size(); 24 | int n = toAssignCloudletList.size(); 25 | int maxCloudletsWaitingLength = vQueueSize.getMaxLength(); 26 | List> vmWaitingQueueSizeList = initVmWaitingQueueSizeList(); 27 | 28 | long totalworkload=0; 29 | for(Cloudlet c:toAssignCloudletList){ 30 | totalworkload=totalworkload+c.getCloudletLength(); 31 | } 32 | 33 | int i; 34 | for (i = 0; i < n; i++) { 35 | int index = 0; 36 | int mSize = maxCloudletsWaitingLength + 1; 37 | for (int j = 0; j < m; j++) { 38 | if (mSize > vmWaitingQueueSizeList.get(j).get("size")) { 39 | mSize = vmWaitingQueueSizeList.get(j).get("size"); 40 | index = j; 41 | } 42 | } 43 | 44 | for (int j = 0; j < m; j++) { 45 | System.out.print(vmWaitingQueueSizeList.get(j).get("size") 46 | + " "); 47 | } 48 | System.out.println(); 49 | 50 | if (mSize >= maxCloudletsWaitingLength) { 51 | Log.printLine("mSize=50 list(0):" + mSize); 52 | break; 53 | } 54 | 55 | int id = vmWaitingQueueSizeList.get(index).get("id"); 56 | if (vQueueSize.increment(id)) { 57 | vmWaitingQueueSizeList.get(index).put("size", ++mSize); 58 | toAssignCloudletList.get(i).setVmId(id); 59 | 60 | } else { 61 | Log.printLine(index + "Index Assign Full Error!! Vm#" + id 62 | + " mSize:" + mSize + " vQueueSize:" 63 | + vQueueSize.getQueueSize().get(id)); 64 | System.exit(0); 65 | } 66 | } 67 | 68 | double s=0;double s1=0;double s2=0;double s3=0; double s4=0; double s5=0; double u1=0;double u=0; 69 | double u2=0;double u3=0;double u4=0;double u5=0;double Utilization=0; double Utilization1=0; 70 | double Utilization2=0; double Utilization3=0; double Utilization4=0; double Utilization5=0; long len=0;long len1=0; 71 | long len2=0;long len3=0;double w=0;long len4=0;long len5=0; 72 | 73 | for(Vm vm:vmList){ 74 | switch(vm.getHost().getId()){ 75 | 76 | case 0: 77 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 78 | len=len+rcl.getCloudletLength();} 79 | vm.getHost().setWorkload(len); 80 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 81 | s++; 82 | } 83 | u++;break; 84 | 85 | case 1: 86 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 87 | len1=len1+rcl.getCloudletLength();} 88 | vm.getHost().setWorkload(len1); 89 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 90 | s1++; 91 | } 92 | u1++;break; 93 | 94 | case 2: 95 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 96 | len2=len2+rcl.getCloudletLength();} 97 | vm.getHost().setWorkload(len2); 98 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 99 | s2++; 100 | } 101 | u2++;break; 102 | 103 | case 3: 104 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 105 | len3=len3+rcl.getCloudletLength();} 106 | vm.getHost().setWorkload(len3); 107 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 108 | s3++; 109 | } 110 | u3++;break; 111 | 112 | case 4: 113 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 114 | len4=len4+rcl.getCloudletLength();} 115 | vm.getHost().setWorkload(len4); 116 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 117 | s4++; 118 | } 119 | u4++;break; 120 | 121 | case 5: 122 | 123 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 124 | len5=len5+rcl.getCloudletLength();} 125 | vm.getHost().setWorkload(len5); 126 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 127 | s5++; 128 | } 129 | u5++;break; 130 | } 131 | } 132 | Utilization=s/u;Utilization1=s1/u1;Utilization2=s2/u2;Utilization3=s3/u3;Utilization4=s4/u4;Utilization5=s5/u5; 133 | 134 | w=len*1.0/totalworkload; 135 | Log.printLine("length"+totalworkload+"\t"+len+"\t"+w); 136 | for (Vm vm:vmList) { 137 | if(vm.getHost().getId()==0){ 138 | 139 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 140 | Log.printLine(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT()+"\t"+Utilization); 141 | } 142 | if(vm.getHost().getId()==1){ 143 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization1,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 144 | } 145 | if(vm.getHost().getId()==2){ 146 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization2,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 147 | } 148 | if(vm.getHost().getId()==3){ 149 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization3,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 150 | } 151 | if(vm.getHost().getId()==4){ 152 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization4,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 153 | } 154 | if(vm.getHost().getId()==5){ 155 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization5,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 156 | } 157 | } 158 | 159 | 160 | List assignedCloudletList = getAssignedCloudletList(i, 161 | toAssignCloudletList); 162 | finishAssign(toAssignCloudletList); 163 | Log.printLine("Assign Finished! Left:" 164 | + getGlobalCloudletWaitingQueue().size() + " Success:" 165 | + assignedCloudletList.size()); 166 | return assignedCloudletList; 167 | } else { 168 | 169 | Log.printLine("VmCloudletAssignerFair No VM Error!!"); 170 | 171 | return null; 172 | 173 | } 174 | } 175 | 176 | 177 | 178 | } 179 | -------------------------------------------------------------------------------- /VmCloudletAssignerGreedy.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | 4 | import java.util.Collections; 5 | import java.util.Comparator; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import org.cloudbus.cloudsim.Cloudlet; 10 | import org.cloudbus.cloudsim.CloudletSchedulerSpaceShared; 11 | import org.cloudbus.cloudsim.Log; 12 | import org.cloudbus.cloudsim.ResCloudlet; 13 | import org.cloudbus.cloudsim.Vm; 14 | 15 | public class VmCloudletAssignerGreedy extends VmCloudletAssigner { 16 | 17 | 18 | @Override 19 | public List cloudletAssign(List cloudletList, 20 | List vmList) { 21 | 22 | if (vmList != null || vmList.size() != 0) { 23 | 24 | List toAssignCloudletList = getToAssignCloudletList(cloudletList); 25 | List> vmWaitingQueueSizeList = initVmWaitingQueueSizeList(); 26 | 27 | if (toAssignCloudletList.size() < 1) { 28 | return null; 29 | // System.exit(0); 30 | } 31 | 32 | long totalworkload=0; 33 | for(Cloudlet c:toAssignCloudletList){ 34 | totalworkload=totalworkload+c.getCloudletLength(); 35 | } 36 | int maxCloudletsWaitingLength = vQueueSize.getMaxLength(); 37 | 38 | 39 | Collections.sort(vmList, new VmComparator()); 40 | 41 | Collections.sort(toAssignCloudletList, new CloudletComparator()); 42 | 43 | 44 | int vmNum = vmList.size(); 45 | int clNum = toAssignCloudletList.size(); 46 | double[][] time = new double[clNum][vmNum]; 47 | for(int i=0;i=0;j--) 69 | { 70 | if(minLoad > vmLoad[j] + time[i][j]) 71 | { 72 | minLoad = vmLoad[j]+time[i][j]; 73 | index=j; 74 | } 75 | else if(minLoad==vmLoad[j]+time[i][j] && vmTask[j]= maxCloudletsWaitingLength) { 90 | Log.printLine("mSize=50 list(0):" + mSize); 91 | break; 92 | } 93 | 94 | if (vQueueSize.increment(index)) { 95 | vmWaitingQueueSizeList.get(index).put("size", ++mSize); 96 | toAssignCloudletList.get(i).setVmId(index); 97 | }else { 98 | Log.printLine(index + "Index Assign Full Error!! Vm#" + index 99 | + " mSize:" + mSize + " vQueueSize:" 100 | + vQueueSize.getQueueSize().get(index)); 101 | System.exit(0); 102 | } 103 | } 104 | 105 | 106 | double s=0;double s1=0;double s2=0;double s3=0; double s4=0; double s5=0; double u1=0;double u=0; 107 | double u2=0;double u3=0;double u4=0;double u5=0;double Utilization=0; double Utilization1=0; 108 | double Utilization2=0; double Utilization3=0; double Utilization4=0; double Utilization5=0; long len=0;long len1=0; 109 | long len2=0;long len3=0;double w=0;long len4=0;long len5=0; 110 | 111 | for(Vm vm:vmList){ 112 | switch(vm.getHost().getId()){ 113 | 114 | case 0: 115 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 116 | len=len+rcl.getCloudletLength();} 117 | vm.getHost().setWorkload(len); 118 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 119 | s++; 120 | } 121 | u++;break; 122 | 123 | case 1: 124 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 125 | len1=len1+rcl.getCloudletLength();} 126 | vm.getHost().setWorkload(len1); 127 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 128 | s1++; 129 | } 130 | u1++;break; 131 | 132 | case 2: 133 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 134 | len2=len2+rcl.getCloudletLength();} 135 | vm.getHost().setWorkload(len2); 136 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 137 | s2++; 138 | } 139 | u2++;break; 140 | 141 | case 3: 142 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 143 | len3=len3+rcl.getCloudletLength();} 144 | vm.getHost().setWorkload(len3); 145 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 146 | s3++; 147 | } 148 | u3++;break; 149 | 150 | case 4: 151 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 152 | len4=len4+rcl.getCloudletLength();} 153 | vm.getHost().setWorkload(len4); 154 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 155 | s4++; 156 | } 157 | u4++;break; 158 | 159 | case 5: 160 | 161 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 162 | len5=len5+rcl.getCloudletLength();} 163 | vm.getHost().setWorkload(len5); 164 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 165 | s5++; 166 | } 167 | u5++;break; 168 | } 169 | } 170 | 171 | Utilization=s/u;Utilization1=s1/u1;Utilization2=s2/u2;Utilization3=s3/u3;Utilization4=s4/u4;Utilization5=s5/u5; 172 | 173 | for (Vm vm:vmList) { 174 | if(vm.getHost().getId()==0){ 175 | 176 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 177 | Log.printLine(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT()+"\t"+Utilization); 178 | } 179 | if(vm.getHost().getId()==1){ 180 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization1,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 181 | } 182 | if(vm.getHost().getId()==2){ 183 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization2,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 184 | } 185 | if(vm.getHost().getId()==3){ 186 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization3,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 187 | } 188 | if(vm.getHost().getId()==4){ 189 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization4,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 190 | } 191 | if(vm.getHost().getId()==5){ 192 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization5,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 193 | } 194 | } 195 | 196 | List assignedCloudletList = getAssignedCloudletList(i-1, 197 | toAssignCloudletList); 198 | 199 | finishAssign(toAssignCloudletList); 200 | 201 | Log.printLine("Assign Finished! Left:" 202 | + getGlobalCloudletWaitingQueue().size() + " Success:" 203 | + assignedCloudletList.size()); 204 | 205 | return assignedCloudletList; 206 | } 207 | else { 208 | Log.printLine("VmCloudletAssignerFair No VM Error!!"); 209 | return null; 210 | } 211 | } 212 | 213 | private class CloudletComparator implements Comparator 214 | { 215 | @Override 216 | public int compare(Cloudlet cl1, Cloudlet cl2) 217 | { 218 | return (int)(cl2.getCloudletLength() - cl1.getCloudletLength()); 219 | } 220 | 221 | } 222 | 223 | private class VmComparator implements Comparator 224 | { 225 | public int compare(Vm vm1, Vm vm2) 226 | { 227 | return (int)(vm1.getMips() - vm2.getMips()); 228 | } 229 | } 230 | 231 | 232 | 233 | } 234 | -------------------------------------------------------------------------------- /VmCloudletAssignerRandom.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.Random; 6 | 7 | import org.cloudbus.cloudsim.Cloudlet; 8 | import org.cloudbus.cloudsim.CloudletSchedulerSpaceShared; 9 | import org.cloudbus.cloudsim.Log; 10 | import org.cloudbus.cloudsim.ResCloudlet; 11 | import org.cloudbus.cloudsim.Vm; 12 | 13 | public class VmCloudletAssignerRandom extends VmCloudletAssigner { 14 | 15 | @Override 16 | public List cloudletAssign(List cloudletList, 17 | List vmList) { 18 | if (vmList != null || vmList.size() != 0) { 19 | List toAssignCloudletList = getToAssignCloudletList(cloudletList); 20 | if (toAssignCloudletList.size() < 1) { 21 | return null; 22 | // System.exit(0); 23 | } 24 | 25 | int m = vmList.size(); 26 | int n = toAssignCloudletList.size(); 27 | int maxCloudletsWaitingLength = vQueueSize.getMaxLength(); 28 | List> vmWaitingQueueSizeList = initVmWaitingQueueSizeList(); 29 | 30 | long totalworkload=0; 31 | for(Cloudlet c:toAssignCloudletList){ 32 | totalworkload=totalworkload+c.getCloudletLength(); 33 | } 34 | 35 | int i; 36 | for (i = 0; i < n; i++) { 37 | int index = randomInt(0, m); 38 | Log.printLine(index+"hahaha"); 39 | int mSize = vmWaitingQueueSizeList.get(index).get("size"); 40 | if (mSize >= maxCloudletsWaitingLength) { 41 | for (int j = 0, tmp = maxCloudletsWaitingLength + 1; j < m; j++) { 42 | if (tmp > vmWaitingQueueSizeList.get(j).get("size")) { 43 | tmp = vmWaitingQueueSizeList.get(j).get("size"); 44 | index = j; 45 | } 46 | } 47 | 48 | mSize = vmWaitingQueueSizeList.get(index).get("size"); 49 | if (mSize >= maxCloudletsWaitingLength) { 50 | //Log.printLine("mSize=50 list(0):" + mSize); 51 | break; 52 | } 53 | 54 | } 55 | 56 | int id = vmWaitingQueueSizeList.get(index).get("id"); 57 | if (vQueueSize.increment(id)) { 58 | vmWaitingQueueSizeList.get(index).put("size", ++mSize); 59 | toAssignCloudletList.get(i).setVmId(id); 60 | 61 | } else { 62 | Log.printLine(index + "Index Assign Full Error!! Vm#" + id 63 | + " mSize:" + mSize + " vQueueSize:" 64 | + vQueueSize.getQueueSize().get(id)); 65 | System.exit(0); 66 | } 67 | 68 | } 69 | 70 | double s=0;double s1=0;double s2=0;double s3=0; double s4=0; double s5=0; double u1=0;double u=0; 71 | double u2=0;double u3=0;double u4=0;double u5=0;double Utilization=0; double Utilization1=0; 72 | double Utilization2=0; double Utilization3=0; double Utilization4=0; double Utilization5=0; long len=0;long len1=0; 73 | long len2=0;long len3=0;double w=0;long len4=0;long len5=0; 74 | 75 | for(Vm vm:vmList){ 76 | switch(vm.getHost().getId()){ 77 | 78 | case 0: 79 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 80 | len=len+rcl.getCloudletLength();} 81 | vm.getHost().setWorkload(len); 82 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 83 | s++; 84 | } 85 | u++;break; 86 | 87 | case 1: 88 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 89 | len1=len1+rcl.getCloudletLength();} 90 | vm.getHost().setWorkload(len1); 91 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 92 | s1++; 93 | } 94 | u1++;break; 95 | 96 | case 2: 97 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 98 | len2=len2+rcl.getCloudletLength();} 99 | vm.getHost().setWorkload(len2); 100 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 101 | s2++; 102 | } 103 | u2++;break; 104 | 105 | case 3: 106 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 107 | len3=len3+rcl.getCloudletLength();} 108 | vm.getHost().setWorkload(len3); 109 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 110 | s3++; 111 | } 112 | u3++;break; 113 | 114 | case 4: 115 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 116 | len4=len4+rcl.getCloudletLength();} 117 | vm.getHost().setWorkload(len4); 118 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 119 | s4++; 120 | } 121 | u4++;break; 122 | 123 | case 5: 124 | 125 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 126 | len5=len5+rcl.getCloudletLength();} 127 | vm.getHost().setWorkload(len5); 128 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 129 | s5++; 130 | } 131 | u5++;break; 132 | } 133 | } 134 | 135 | Utilization=s/u;Utilization1=s1/u1;Utilization2=s2/u2;Utilization3=s3/u3;Utilization4=s4/u4;Utilization5=s5/u5; 136 | 137 | for (Vm vm:vmList) { 138 | if(vm.getHost().getId()==0){ 139 | 140 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 141 | Log.printLine(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT()+"\t"+Utilization); 142 | } 143 | if(vm.getHost().getId()==1){ 144 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization1,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 145 | } 146 | if(vm.getHost().getId()==2){ 147 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization2,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 148 | } 149 | if(vm.getHost().getId()==3){ 150 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization3,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 151 | } 152 | if(vm.getHost().getId()==4){ 153 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization4,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 154 | } 155 | if(vm.getHost().getId()==5){ 156 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization5,((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getAverageWaitingTime()); 157 | } 158 | } 159 | 160 | List assignedCloudletList = getAssignedCloudletList(i, toAssignCloudletList); 161 | 162 | finishAssign(toAssignCloudletList); 163 | 164 | Log.printLine("Assign Finished! Left:" 165 | + getGlobalCloudletWaitingQueue().size() + " Success:" 166 | + assignedCloudletList.size()); 167 | 168 | return assignedCloudletList; 169 | 170 | } else { 171 | Log.printLine("VmCloudletAssignerRandom No VM Error!!"); 172 | return null; 173 | } 174 | } 175 | 176 | 177 | 178 | private int randomInt(int min, int max) { 179 | Random random = new Random(); 180 | return random.nextInt(max) % (max - min + 1) + min; 181 | } 182 | 183 | 184 | 185 | } 186 | -------------------------------------------------------------------------------- /VmCloudletAssignerReinforcementLearning.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.io.BufferedWriter; 4 | import java.util.ArrayList; 5 | import java.util.Collections; 6 | import java.util.Comparator; 7 | import java.util.HashMap; 8 | import java.util.LinkedList; 9 | import java.util.List; 10 | import java.util.Map; 11 | import java.util.Random; 12 | 13 | import org.cloudbus.cloudsim.Cloudlet; 14 | import org.cloudbus.cloudsim.CloudletSchedulerSpaceShared; 15 | import org.cloudbus.cloudsim.HostStateHistoryEntry; 16 | import org.cloudbus.cloudsim.Log; 17 | import org.cloudbus.cloudsim.ResCloudlet; 18 | import org.cloudbus.cloudsim.Vm; 19 | import org.cloudbus.cloudsim.VmScheduler; 20 | 21 | //import sun.org.mozilla.javascript.internal.ast.Assignment; 22 | 23 | public class VmCloudletAssignerReinforcementLearning extends VmCloudletAssigner { //reinforcement learning policy implement 24 | 25 | private VirtualQueueSize vQueueSize = VirtualQueueSize.getInstance(); 26 | private GenExcel genExcel = null; 27 | private static double gamma; 28 | private static double alpha; 29 | private static double epsilon; 30 | private static Map> QList = new HashMap>(); //Q table 31 | int s=0; 32 | private static double Utilization=0; 33 | private static double Utilization1=0; 34 | private static double Utilization2=0; 35 | private static double Utilization3=0; 36 | private static double Utilization4=0; 37 | private static double Utilization5=0; 38 | private static double Utilization6=0; 39 | private static double Utilization7=0; 40 | private static double Utilization8=0; 41 | private static double Utilization9=0; 42 | private static double Utilization10=0; 43 | private static double Utilization11=0; 44 | private static double Utilization12=0; 45 | private static double Utilization13=0; 46 | private static double Utilization14=0; 47 | private static double Utilization15=0; 48 | private static double Utilization16=0; 49 | private static double Utilization17=0; 50 | private static double Utilization18=0; 51 | private static double Utilization19=0; 52 | 53 | long totalworkload=0; 54 | private final List stateHistory = new LinkedList(); 55 | 56 | 57 | 58 | public VmCloudletAssignerReinforcementLearning( double gamma, double alpha, double epsilon, GenExcel genExcel) { 59 | this.gamma = gamma; 60 | this.alpha = alpha; 61 | this.epsilon = epsilon; 62 | this.genExcel=genExcel; 63 | this.genExcel.init(); 64 | QList=this.genExcel.init(); 65 | 66 | } 67 | 68 | @Override 69 | public List cloudletAssign(List cloudletList, List vmList) { 70 | double r=-1; 71 | 72 | if (vmList != null || vmList.size() != 0) { 73 | List toAssignCloudletList = getToAssignCloudletList(cloudletList); //Initial startup authorization authorization 74 | if (toAssignCloudletList.size() < 1) { //There are no tasks waiting to be assigned, return an empty list 75 | return null; 76 | // System.exit(0); 77 | } 78 | 79 | List>[] vmWaitingQueueSizeList =new ArrayList[10000]; 80 | List>[] tmpSizeList =new ArrayList[10000]; 81 | 82 | // //Task preprocessing 83 | 84 | // for(Cloudlet c:toAssignCloudletList){ 85 | // 86 | // if(c.getCloudletLength()<2000){ 87 | // c.setSe(3); 88 | // }else if(c.getCloudletLength()>2000&&c.getCloudletLength()<4000){ 89 | // c.setSe(2); 90 | // }else{ 91 | // c.setSe(1); 92 | // } 93 | // 94 | // if(c.getWaitingTime()<5){ 95 | // c.setWe(1); 96 | // }else if(c.getWaitingTime()>5&&c.getWaitingTime()<15){ 97 | // c.setWe(2); 98 | // }else{ 99 | // c.setWe(3); 100 | // } 101 | // 102 | // if(c.getDeadline()<10){ 103 | // c.setDe(3); 104 | // }else if(c.getDeadline()>10&&c.getDeadline()<18){ 105 | // c.setDe(2); 106 | // }else{ 107 | // c.setDe(1); 108 | // } 109 | // 110 | // } 111 | // 112 | // //The proportion of the three indicators is 5:3:2 113 | // for(Cloudlet c:toAssignCloudletList){ 114 | // int rank=(int) (0.5*c.getSe()+0.3*c.getWe()+0.2*c.getDe()); 115 | // c.setRank(rank); 116 | // } 117 | 118 | 119 | //sort tasks 120 | Collections.sort(toAssignCloudletList,new CloudletComparator()); 121 | int i=0; 122 | int k=0; 123 | int m = vmList.size(); 124 | int n = toAssignCloudletList.size(); //Number of task lists to be allocated 125 | int maxCloudletsWaitingLength = vQueueSize.getMaxLength(); //Maximum length of subtask queue 126 | int numFreeVm = m; 127 | for (int s = 0; s< 10000; s++) { 128 | vmWaitingQueueSizeList[s] = new ArrayList>(); 129 | vmWaitingQueueSizeList[s] = initVmWaitingQueueSizeList();//Initialize the virtual subqueue captain list 130 | // vqueuesize[s]=VirtualQueueSize.getInstance(); 131 | } 132 | // List> tmpSizeList = updateTmpSizeList(-1, numFreeVm, vmWaitingQueueSizeList);//Temporary queue 133 | for (int j = 0; j < 10000; j++) { 134 | tmpSizeList[j] = new ArrayList>(); 135 | tmpSizeList[j] = updateTmpSizeList(-1, numFreeVm, vmWaitingQueueSizeList[j]); 136 | } 137 | 138 | for(Cloudlet c:toAssignCloudletList){ 139 | totalworkload=totalworkload+c.getCloudletLength(); 140 | } 141 | 142 | for (i = 0; i < n; i++) { //Assign tasks to suitable virtual machines 143 | 144 | for (int j = 0; j < m; j++) { //Output the length of the waiting queue of all virtual machines 145 | System.out.print(vmWaitingQueueSizeList[k].get(j).get("size") + " "); 146 | } 147 | System.out.println(); 148 | 149 | int index = createAction(numFreeVm, vmWaitingQueueSizeList[k]);//Select action: the id of the selected virtual machine, corresponding to the column number in the Q value table 150 | int mSize = tmpSizeList[k].get(index).get("size"); 151 | if (mSize >= maxCloudletsWaitingLength) {// If the selected queue is full, remove this queue and select again 152 | 153 | if (numFreeVm >1) {//If the number of idle queues can be reduced to 1 or more, the temporary queue is updated, that is, the full queue is thrown away 154 | tmpSizeList[k] = updateTmpSizeList(index, numFreeVm--, tmpSizeList[k]); 155 | // System.out.println(numFreeVm); 156 | i--; 157 | continue; 158 | } 159 | else { //The waiting queues of all virtual machines are full 160 | Log.printLine("mSize=50 list(0):" + mSize); 161 | break; 162 | } 163 | 164 | } 165 | int id = tmpSizeList[k].get(index).get("id"); //The id of the selected virtual machine 166 | 167 | if(k==9){ //Partial update steps 168 | //Since VirtualQueueSize is a singleton mode, only one instance is created 169 | if(tmpSizeList[k].get(index).get("size") assignedCloudletList = getAssignedCloudletList(i, toAssignCloudletList); //Get the list of successfully assigned tasks 209 | 210 | finishAssign(toAssignCloudletList); 211 | 212 | Log.printLine("Assign Finished! Left:" 213 | + getGlobalCloudletWaitingQueue().size() + " Success:" 214 | + assignedCloudletList.size()); 215 | 216 | return assignedCloudletList; 217 | 218 | } else { 219 | Log.printLine("VmCloudletAssignerLearning No VM Error!!"); 220 | return null; 221 | } 222 | 223 | } 224 | 225 | private int createAction(int numVm, List> vmWaitingQueueSizeList) { //Generate the action(vm id) of selecting a virtual machine 226 | int current_action; 227 | int x = randomInt(0, numVm); 228 | String state_idx = createState_idx(numVm, vmWaitingQueueSizeList); //The row number of the Q value table generated according to the current state of the waiting queue of each virtual machine 229 | System.out.println(state_idx+" state"); 230 | if (!QList.containsKey(state_idx)) { //If this row does not exist in the Q value table, initialize this row 231 | initRowOfQList(state_idx, numVm); 232 | }else{ 233 | System.out.println(state_idx+"already exit"); 234 | } 235 | 236 | //generate the action 237 | if (((double) x / 100) < (1 - epsilon)) { 238 | int umax = 0; 239 | double tmp = -20000.0; 240 | for (int i = 0; i < numVm; i++) { 241 | if (tmp < QList.get(state_idx).get(i)) { 242 | tmp = QList.get(state_idx).get(i); 243 | umax = i; 244 | } 245 | } 246 | if (tmp == -20000.0) { 247 | System.out.println("no exploit......!"); 248 | System.exit(0); 249 | } 250 | current_action = umax; 251 | } 252 | else{ //explore 253 | current_action = randomInt(0, numVm - 1); 254 | } 255 | return current_action; 256 | } 257 | 258 | private double updateQList(int action_idx, int numVm, List vmList, List> vmWaitingQueueSizeList) { 259 | double sample = ((QCloudletSchedulerSpaceShared) vmList 260 | .get(action_idx).getCloudletScheduler()).getAverageWaitingTime(); 261 | if (sample == 0.0) { //If this value is 0, initialize to a large enough number 262 | sample = 1000000.0; 263 | } 264 | 265 | double reward=0; 266 | double h=0;double vms=0; 267 | double s=0;double s1=0;double s2=0;double s3=0;double s4=0;double s5=0; 268 | double s6=0;double s7=0;double s8=0;double s9=0;double s10=0;double s11=0; 269 | double s12=0;double s13=0;double s14=0;double s15=0;double s16=0;double s17=0; 270 | double s18=0;double s19=0; 271 | double u1=0;double u=0;double u2=0;double u3=0;double u4=0;double u5=0; 272 | double u6=0;double u7=0;double u8=0;double u9=0;double u10=0;double u11=0; 273 | double u12=0;double u13=0;double u14=0;double u15=0;double u16=0;double u17=0; 274 | double u18=0;double u19=0; 275 | double ts=0;double a=0;int t=0; 276 | long len=0;long len1=0;long len2=0;long len3=0;long len4=0;long len5=0; 277 | long len6=0;long len7=0;long len8=0;long len9=0;long len10=0;long len11=0; 278 | long len12=0;long len13=0;long len14=0;long len15=0;long len16=0;long len17=0; 279 | long len18=0;long len19=0; 280 | for(Vm vm:vmList){ 281 | switch(vm.getHost().getId()){ 282 | 283 | case 0: 284 | t=0; 285 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 286 | double tt= (((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT()); 287 | t++; 288 | len=len+rcl.getCloudletLength(); 289 | Log.printLine("cloudlet"+rcl.getCloudlet().getCloudletId()+"\t"+tt); 290 | } 291 | vm.getHost().setWorkload(len); 292 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 293 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 294 | s++; 295 | } 296 | u++;break; 297 | 298 | case 1: 299 | t=0; 300 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 301 | t++; 302 | len1=len+rcl.getCloudletLength(); 303 | } 304 | vm.getHost().setWorkload(len); 305 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 306 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 307 | s1++; 308 | } 309 | u1++;break; 310 | 311 | case 2: 312 | t=0; 313 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 314 | t++; 315 | len2=len+rcl.getCloudletLength(); 316 | } 317 | vm.getHost().setWorkload(len); 318 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 319 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 320 | s2++; 321 | } 322 | u2++;break; 323 | 324 | case 3: 325 | t=0; 326 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 327 | t++; 328 | len3=len3+rcl.getCloudletLength(); 329 | } 330 | vm.getHost().setWorkload(len); 331 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 332 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 333 | s3++; 334 | } 335 | u3++;break; 336 | 337 | case 4: 338 | t=0; 339 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 340 | t++; 341 | len4=len4+rcl.getCloudletLength(); 342 | } 343 | vm.getHost().setWorkload(len); 344 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 345 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 346 | s4++; 347 | } 348 | u4++;break; 349 | 350 | case 5: 351 | t=0; 352 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 353 | t++; 354 | len5=len5+rcl.getCloudletLength(); 355 | } 356 | vm.getHost().setWorkload(len); 357 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 358 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 359 | s5++; 360 | } 361 | u5++;break; 362 | 363 | case 6: 364 | t=0; 365 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 366 | t++; 367 | len5=len5+rcl.getCloudletLength(); 368 | } 369 | vm.getHost().setWorkload(len); 370 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 371 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 372 | s6++; 373 | } 374 | u6++;break; 375 | 376 | case 7: 377 | t=0; 378 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 379 | t++; 380 | len5=len5+rcl.getCloudletLength(); 381 | } 382 | vm.getHost().setWorkload(len); 383 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 384 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 385 | s7++; 386 | } 387 | u7++;break; 388 | 389 | case 8: 390 | t=0; 391 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 392 | t++; 393 | len5=len5+rcl.getCloudletLength(); 394 | } 395 | vm.getHost().setWorkload(len); 396 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 397 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 398 | s8++; 399 | } 400 | u8++;break; 401 | 402 | case 9: 403 | t=0; 404 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 405 | t++; 406 | len5=len5+rcl.getCloudletLength(); 407 | } 408 | vm.getHost().setWorkload(len); 409 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 410 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 411 | s9++; 412 | } 413 | u9++;break; 414 | 415 | case 10: 416 | t=0; 417 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 418 | t++; 419 | len5=len5+rcl.getCloudletLength(); 420 | } 421 | vm.getHost().setWorkload(len); 422 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 423 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 424 | s10++; 425 | } 426 | u10++;break; 427 | 428 | case 11: 429 | t=0; 430 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 431 | t++; 432 | len5=len5+rcl.getCloudletLength(); 433 | } 434 | vm.getHost().setWorkload(len); 435 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 436 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 437 | s11++; 438 | } 439 | u11++;break; 440 | 441 | case 12: 442 | t=0; 443 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 444 | t++; 445 | len5=len5+rcl.getCloudletLength(); 446 | } 447 | vm.getHost().setWorkload(len); 448 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 449 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 450 | s12++; 451 | } 452 | u12++;break; 453 | 454 | case 13: 455 | t=0; 456 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 457 | t++; 458 | len5=len5+rcl.getCloudletLength(); 459 | } 460 | vm.getHost().setWorkload(len); 461 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 462 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 463 | s13++; 464 | } 465 | u13++;break; 466 | 467 | case 14: 468 | t=0; 469 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 470 | t++; 471 | len5=len5+rcl.getCloudletLength(); 472 | } 473 | vm.getHost().setWorkload(len); 474 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 475 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 476 | s14++; 477 | } 478 | u14++;break; 479 | 480 | case 15: 481 | t=0; 482 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 483 | t++; 484 | len5=len5+rcl.getCloudletLength(); 485 | } 486 | vm.getHost().setWorkload(len); 487 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 488 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 489 | s15++; 490 | } 491 | u15++;break; 492 | 493 | case 16: 494 | t=0; 495 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 496 | t++; 497 | len5=len5+rcl.getCloudletLength(); 498 | } 499 | vm.getHost().setWorkload(len); 500 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 501 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 502 | s16++; 503 | } 504 | u16++;break; 505 | 506 | case 17: 507 | t=0; 508 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 509 | t++; 510 | len5=len5+rcl.getCloudletLength(); 511 | } 512 | vm.getHost().setWorkload(len); 513 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 514 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 515 | s17++; 516 | } 517 | u17++;break; 518 | 519 | case 18: 520 | t=0; 521 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 522 | t++; 523 | len5=len5+rcl.getCloudletLength(); 524 | } 525 | vm.getHost().setWorkload(len); 526 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 527 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 528 | s18++; 529 | } 530 | u18++;break; 531 | 532 | case 19: 533 | t=0; 534 | for(ResCloudlet rcl: ((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList()){ 535 | t++; 536 | len5=len5+rcl.getCloudletLength(); 537 | } 538 | vm.getHost().setWorkload(len); 539 | vm.setCloudnum(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()); 540 | if(((CloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getCloudletExecList().size()!=0){ 541 | s19++; 542 | } 543 | u19++;break; 544 | } 545 | } 546 | Utilization=s/u;Utilization1=s1/u1;Utilization2=s2/u2;Utilization3=s3/u3;Utilization4=s4/u4;Utilization5=s5/u5; 547 | Utilization6=s6/u6;Utilization7=s7/u7;Utilization8=s8/u8;Utilization9=s9/u9;Utilization10=s10/u10;Utilization11=s11/u11; 548 | Utilization12=s12/u12;Utilization13=s13/u13;Utilization14=s14/u14;Utilization15=s15/u15;Utilization16=s16/u16;Utilization17=s17/u17; 549 | Utilization18=s18/u18;Utilization19=s19/u19; 550 | 551 | for (Vm vm:vmList) { 552 | if(vm.getHost().getId()==0){ 553 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization,sample); 554 | Log.printLine(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT()+"\t"+Utilization); 555 | } 556 | if(vm.getHost().getId()==1){ 557 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization1,sample); 558 | } 559 | if(vm.getHost().getId()==2){ 560 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization2,sample); 561 | } 562 | if(vm.getHost().getId()==3){ 563 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization3,sample); 564 | } 565 | if(vm.getHost().getId()==4){ 566 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization4,sample); 567 | } 568 | if(vm.getHost().getId()==5){ 569 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization5,sample); 570 | } 571 | if(vm.getHost().getId()==6){ 572 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization6,sample); 573 | } 574 | if(vm.getHost().getId()==7){ 575 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization7,sample); 576 | } 577 | if(vm.getHost().getId()==8){ 578 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization8,sample); 579 | } 580 | if(vm.getHost().getId()==9){ 581 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization9,sample); 582 | } 583 | if(vm.getHost().getId()==10){ 584 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization10,sample); 585 | } 586 | if(vm.getHost().getId()==11){ 587 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization11,sample); 588 | } 589 | if(vm.getHost().getId()==12){ 590 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization12,sample); 591 | } 592 | if(vm.getHost().getId()==13){ 593 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization13,sample); 594 | } 595 | if(vm.getHost().getId()==14){ 596 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization14,sample); 597 | } 598 | if(vm.getHost().getId()==15){ 599 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization15,sample); 600 | } 601 | if(vm.getHost().getId()==16){ 602 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization16,sample); 603 | } 604 | if(vm.getHost().getId()==17){ 605 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization17,sample); 606 | } 607 | if(vm.getHost().getId()==18){ 608 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization18,sample); 609 | } 610 | if(vm.getHost().getId()==19){ 611 | vm.getHost().addStateHistoryEntry(((QCloudletSchedulerSpaceShared) vm.getCloudletScheduler()).getT(), Utilization19,sample); 612 | } 613 | 614 | } 615 | 616 | if(vmList.get(action_idx).getHost().getStateHistory().size()-2>=0){ 617 | if(vmWaitingQueueSizeList.get(action_idx).get("size")==0&&vmList.get(action_idx).getHost().getStateHistory().get(vmList.get(action_idx).getHost().getStateHistory().size()-2).getUtilization()<=vmList.get(action_idx).getHost().getStateHistory().get(vmList.get(action_idx).getHost().getStateHistory().size()-1).getUtilization()&&vmList.get(action_idx).getHost().getStateHistory().get(vmList.get(action_idx).getHost().getStateHistory().size()-2).getWorkLoad()<=vmList.get(action_idx).getHost().getStateHistory().get(vmList.get(action_idx).getHost().getStateHistory().size()-1).getWorkLoad()){ 618 | reward=1; 619 | }else{ 620 | if(((QCloudletSchedulerSpaceShared) vmList.get(action_idx).getCloudletScheduler()).getAverageWaitingTime()<=((QCloudletSchedulerSpaceShared) vmList.get(action_idx).getCloudletScheduler()).getPreviousAverageWaitingTime()&&s/u>=0.3&&s1/u1>=0.3&&s2/u2>=0.3&&s3/u3>=0.3&&s4/u4>=0.3&&s5/u5>=0.3&&vmList.get(action_idx).getCloudnum()==0){ 621 | reward=0; 622 | }else{ 623 | if(vmWaitingQueueSizeList.get(action_idx).get("size")>=1){ 624 | reward=-1; 625 | }else{ 626 | 627 | } 628 | 629 | } 630 | 631 | } 632 | }else{ 633 | 634 | if(((QCloudletSchedulerSpaceShared) vmList.get(action_idx).getCloudletScheduler()).getAverageWaitingTime()<=((QCloudletSchedulerSpaceShared) vmList.get(action_idx).getCloudletScheduler()).getPreviousAverageWaitingTime()){ 635 | reward=1; 636 | }else{ 637 | if(vmWaitingQueueSizeList.get(action_idx).get("size")>=1){ 638 | reward=-1; 639 | }else{ 640 | if(s/u>=0.3&&s1/u1>=0.3&&s2/u2>=0.3&&s3/u3>=0.3&&s4/u4>=0.3&&s5/u5>=0.3&& 641 | s6/u6>=0.3&&s7/u7>=0.3&&s8/u8>=0.3&&s9/u9>=0.3&&s10/u10>=0.3&&s11/u11>=0.3&& 642 | s12/u12>=0.3&&s13/u13>=0.3&&s14/u14>=0.3&&s15/u15>=0.3&&s16/u16>=0.3&&s17/u17>=0.3&& 643 | s18/u18>=0.3&&s19/u19>=0.3&& 644 | vmList.get(action_idx).getCloudnum()==0) 645 | reward=1; 646 | } 647 | } 648 | 649 | } 650 | 651 | // reward = 1/sample; The reward value generated by the reciprocal of the average waiting time of tasks in the selected virtual machine and the resource utilization of the host 652 | String state_idx = createLastState_idx(action_idx, numVm, vmWaitingQueueSizeList); //The status line number when the current task is not assigned to the virtual machine queue 653 | String next_state_idx = createState_idx(numVm, vmWaitingQueueSizeList); //The status line number after the current task is assigned to the virtual machine queue 654 | 655 | System.out.println(((QCloudletSchedulerSpaceShared) vmList.get(action_idx).getCloudletScheduler()).getAverageWaitingTime()+" "+reward); 656 | System.out.println("\n output state_idx\n" + state_idx); 657 | System.out.println("\n output next_state_idx\n" + next_state_idx); 658 | 659 | if (!QList.containsKey(next_state_idx)) { //If the updated row does not exist in the Q value table, initialize it 660 | initRowOfQList(next_state_idx, numVm); 661 | } 662 | double QMaxNextState = -1.0; 663 | for (int i = 0; i < numVm; i++) { //Get the maximum value of the updated status line 664 | if (QMaxNextState < QList.get(next_state_idx).get(i)) { 665 | QMaxNextState = QList.get(next_state_idx).get(i); 666 | } 667 | } 668 | double QValue = QList.get(state_idx).get(action_idx) //Q function 669 | + alpha * (reward + gamma * QMaxNextState - QList.get(state_idx).get(action_idx)); 670 | 671 | QList.get(state_idx).put(action_idx, QValue); 672 | 673 | this.genExcel.fillData(QList, state_idx, action_idx, QValue); 674 | return reward; 675 | } 676 | 677 | private int randomInt(int min, int max) { 678 | if (min == max) { 679 | return min; 680 | } 681 | Random random = new Random(); 682 | return random.nextInt(max) % (max - min + 1) + min; 683 | } 684 | 685 | private String createLastState_idx(int action_idx, int numVm, 686 | List> vmWaitingQueueSizeList) { 687 | String state_idx = ""; 688 | for (int i = 0; i < numVm; i++) { 689 | if (i == action_idx) { 690 | state_idx += "-" + (vmWaitingQueueSizeList.get(i).get("size").intValue() - 1); 691 | } 692 | else { 693 | state_idx += "-" + vmWaitingQueueSizeList.get(i).get("size").intValue(); 694 | } 695 | } 696 | return state_idx; 697 | } 698 | 699 | private String createState_idx(int numVm, List> vmWaitingQueueSizeList) { 700 | String state_idx = ""; 701 | for (int i = 0; i < numVm; i++) { 702 | state_idx += "-" + vmWaitingQueueSizeList.get(i).get("size").intValue(); 703 | } 704 | return state_idx; 705 | } 706 | 707 | private void initRowOfQList(String state_idx, int numColumn) { 708 | QList.put(state_idx, new HashMap()); 709 | for (int i = 0; i < numColumn; i++) { 710 | QList.get(state_idx).put(i, 0.0); 711 | } 712 | } 713 | 714 | private List> updateTmpSizeList(int index, int numFreeVm, //Update the status of the temporary virtual machine waiting queue length list 715 | List> originSizeList) { 716 | List> tmp = new ArrayList>(); 717 | for (int j = 0; j < numFreeVm; j++) { 718 | if (index == -1 || originSizeList.get(j).get("id") != originSizeList.get(index).get("id")) { //Remove the selected virtual machine (virtual machine waiting queue full) from the temporary list 719 | tmp.add(originSizeList.get(j)); 720 | } 721 | } 722 | return tmp; 723 | } 724 | 725 | private class CloudletComparator implements Comparator{ 726 | 727 | @Override 728 | public int compare(Cloudlet o1, Cloudlet o2) { 729 | // TODO Auto-generated method stub 730 | return -((int)o1.getRank()-o2.getRank()); 731 | } 732 | 733 | } 734 | } 735 | -------------------------------------------------------------------------------- /VmCloudletAssignermm1.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.Collections; 4 | import java.util.Comparator; 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Random; 8 | 9 | import org.cloudbus.cloudsim.Cloudlet; 10 | import org.cloudbus.cloudsim.Log; 11 | import org.cloudbus.cloudsim.Vm; 12 | 13 | 14 | public class VmCloudletAssignermm1 extends VmCloudletAssigner{ 15 | 16 | int index=(int)(5*Math.random()); 17 | @Override 18 | public List cloudletAssign(List cloudletList, 19 | List vmList) { 20 | if (vmList != null || vmList.size() != 0) { 21 | List toAssignCloudletList = getToAssignCloudletList(cloudletList); 22 | if (toAssignCloudletList.size() < 1) { 23 | return null; 24 | // System.exit(0); 25 | } 26 | 27 | //task preprocess 28 | for(Cloudlet c:toAssignCloudletList){ 29 | 30 | if(c.getCloudletLength()<2000){ 31 | c.setSe(3); 32 | }else if(c.getCloudletLength()>200 && c.getCloudletLength()<4000){ 33 | c.setSe(2); 34 | }else{ 35 | c.setSe(1); 36 | } 37 | 38 | if(c.getWaitingTime()<5){ 39 | c.setWe(1); 40 | }else if(c.getWaitingTime()>10&&c.getWaitingTime()<15){ 41 | c.setWe(2); 42 | }else{ 43 | c.setWe(3); 44 | } 45 | 46 | if(c.getDeadline()<6){ 47 | c.setDe(3); 48 | }else if(c.getDeadline()>6&&c.getDeadline()<20){ 49 | c.setDe(2); 50 | }else{ 51 | c.setDe(1); 52 | } 53 | 54 | } 55 | 56 | 57 | for(Cloudlet c:toAssignCloudletList){ 58 | int rank=(int) (0.5*c.getSe()+0.3*c.getWe()+0.2*c.getDe()); 59 | c.setRank(rank); 60 | } 61 | 62 | 63 | Collections.sort(toAssignCloudletList,new CloudletComparator()); 64 | int m = vmList.size(); 65 | int n = toAssignCloudletList.size(); 66 | int maxCloudletsWaitingLength = vQueueSize.getMaxLength(); 67 | List> vmWaitingQueueSizeList = initVmWaitingQueueSizeList(); 68 | 69 | 70 | int i; 71 | for (i = 0; i < n; i++) { 72 | index=(int)(5*Math.random()); 73 | int mSize = vmWaitingQueueSizeList.get(index).get("size"); 74 | if (mSize >= maxCloudletsWaitingLength) { 75 | 76 | for (int j = 0, tmp = maxCloudletsWaitingLength + 1; j < m; j++) { 77 | if (tmp > vmWaitingQueueSizeList.get(j).get("size")) { 78 | tmp = vmWaitingQueueSizeList.get(j).get("size"); 79 | index = j; 80 | } 81 | } 82 | 83 | mSize = vmWaitingQueueSizeList.get(index).get("size"); 84 | if (mSize >= maxCloudletsWaitingLength) { 85 | //Log.printLine("mSize=50 list(0):" + mSize); 86 | break; 87 | } 88 | 89 | } 90 | 91 | int id = vmWaitingQueueSizeList.get(index).get("id"); 92 | if (vQueueSize.increment(id)) { 93 | vmWaitingQueueSizeList.get(index).put("size", ++mSize); 94 | toAssignCloudletList.get(i).setVmId(id); 95 | } else { 96 | Log.printLine(index + "Index Assign Full Error!! Vm#" + id 97 | + " mSize:" + mSize + " vQueueSize:" 98 | + vQueueSize.getQueueSize().get(id)); 99 | System.exit(0); 100 | } 101 | index = (index++)%m; 102 | Log.printLine(index+"hahaxuniji"); 103 | } 104 | 105 | List assignedCloudletList = getAssignedCloudletList(i, toAssignCloudletList); 106 | 107 | finishAssign(toAssignCloudletList); 108 | 109 | Log.printLine("Assign Finished! Left:" 110 | + getGlobalCloudletWaitingQueue().size() + " Success:" 111 | + assignedCloudletList.size()); 112 | 113 | return assignedCloudletList; 114 | 115 | } else { 116 | Log.printLine("VmCloudletAssignerRandom No VM Error!!"); 117 | return null; 118 | } 119 | } 120 | 121 | 122 | private class CloudletComparator implements Comparator{ 123 | 124 | @Override 125 | public int compare(Cloudlet o1, Cloudlet o2) { 126 | // TODO Auto-generated method stub 127 | return -((int)o1.getRank()-o2.getRank()); 128 | } 129 | 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /VmCloudletAssignermms.java: -------------------------------------------------------------------------------- 1 | package org.cloudbus.cloudsim.examples; 2 | 3 | import java.util.Collections; 4 | import java.util.Comparator; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.cloudbus.cloudsim.Cloudlet; 9 | import org.cloudbus.cloudsim.Log; 10 | import org.cloudbus.cloudsim.Vm; 11 | 12 | public class VmCloudletAssignermms extends VmCloudletAssigner{ 13 | 14 | @Override 15 | public List cloudletAssign(List cloudletList, 16 | List vmList) { 17 | if (vmList != null || vmList.size() != 0) { 18 | List toAssignCloudletList = getToAssignCloudletList(cloudletList); 19 | if (toAssignCloudletList.size() < 1) { 20 | return null; 21 | // System.exit(0); 22 | } 23 | 24 | //task preprocess 25 | for(Cloudlet c:toAssignCloudletList){ 26 | 27 | if(c.getCloudletLength()<1500){ 28 | c.setSe(3); 29 | }else if(c.getCloudletLength()>1500&&c.getCloudletLength()<2500){ 30 | c.setSe(2); 31 | }else{ 32 | c.setSe(1); 33 | } 34 | 35 | if(c.getWaitingTime()<45){ 36 | c.setWe(1); 37 | }else if(c.getWaitingTime()>45&&c.getWaitingTime()<60){ 38 | c.setWe(2); 39 | }else{ 40 | c.setWe(3); 41 | } 42 | 43 | if(c.getDeadline()<50){ 44 | c.setDe(3); 45 | }else if(c.getDeadline()>50&&c.getDeadline()<60){ 46 | c.setDe(2); 47 | }else{ 48 | c.setDe(1); 49 | } 50 | 51 | } 52 | 53 | 54 | for(Cloudlet c:toAssignCloudletList){ 55 | int rank=(int) (0.5*c.getSe()+0.3*c.getWe()+0.2*c.getDe()); 56 | c.setRank(rank); 57 | } 58 | 59 | 60 | Collections.sort(toAssignCloudletList,new CloudletComparator()); 61 | int m = vmList.size(); 62 | int n = toAssignCloudletList.size(); 63 | int maxCloudletsWaitingLength = vQueueSize.getMaxLength(); 64 | List> vmWaitingQueueSizeList = initVmWaitingQueueSizeList(); 65 | 66 | int i; 67 | for (i = 0; i < n; i++) { 68 | int index = (int)(5*Math.random()); 69 | int mSize = maxCloudletsWaitingLength + 1; 70 | //toAssignCloudletList.get(i).setCloudletLength(20000); 71 | for (int j = 0; j < m; j++) { 72 | if (mSize > vmWaitingQueueSizeList.get(j).get("size")) { 73 | mSize = vmWaitingQueueSizeList.get(j).get("size"); 74 | index = j; 75 | } 76 | } 77 | 78 | for (int j = 0; j < m; j++) { 79 | System.out.print(vmWaitingQueueSizeList.get(j).get("size") 80 | + " "); 81 | } 82 | System.out.println(); 83 | 84 | if (mSize >= maxCloudletsWaitingLength) { 85 | Log.printLine("mSize=50 list(0):" + mSize); 86 | break; 87 | } 88 | 89 | int id = vmWaitingQueueSizeList.get(index).get("id"); 90 | if (vQueueSize.increment(id)) { 91 | vmWaitingQueueSizeList.get(index).put("size", ++mSize); 92 | toAssignCloudletList.get(i).setVmId(id); 93 | 94 | } else { 95 | Log.printLine(index + "Index Assign Full Error!! Vm#" + id 96 | + " mSize:" + mSize + " vQueueSize:" 97 | + vQueueSize.getQueueSize().get(id)); 98 | System.exit(0); 99 | } 100 | } 101 | 102 | List assignedCloudletList = getAssignedCloudletList(i, 103 | toAssignCloudletList); 104 | finishAssign(toAssignCloudletList); 105 | Log.printLine("Assign Finished! Left:" 106 | + getGlobalCloudletWaitingQueue().size() + " Success:" 107 | + assignedCloudletList.size()); 108 | return assignedCloudletList; 109 | } else { 110 | 111 | Log.printLine("VmCloudletAssignerFair No VM Error!!"); 112 | 113 | return null; 114 | 115 | } 116 | } 117 | 118 | private class CloudletComparator implements Comparator{ 119 | 120 | @Override 121 | public int compare(Cloudlet o1, Cloudlet o2) { 122 | // TODO Auto-generated method stub 123 | return -((int)o1.getRank()-o2.getRank()); 124 | } 125 | 126 | } 127 | 128 | } 129 | --------------------------------------------------------------------------------