package org.fog.placement; import java.util.*; import org.cloudbus.cloudsim.core.CloudSim; import org.fog.application.AppEdge; import org.fog.application.AppModule; import org.fog.application.Application; import org.fog.entities.Actuator; import org.fog.entities.FogDevice; import org.fog.entities.Sensor; import org.fog.entities.Tuple; //import sun.security.util.Length; public class ModulePlacementOnlyCloud extends ModulePlacement{ private List sensors; private List actuators; private int cloudId; public ModulePlacementOnlyCloud(List fogDevices, List sensors, List actuators, Application application) { this.setFogDevices(fogDevices); this.setApplication(application); this.setSensors(sensors); this.setActuators(actuators); this.setModuleToDeviceMap(new HashMap>()); this.setDeviceToModuleMap(new HashMap>()); this.setModuleInstanceCountMap(new HashMap>()); this.cloudId = CloudSim.getEntityId("cloud"); mapModules(); computeModuleInstanceCounts(); } private void computeModuleInstanceCounts(){ FogDevice cloud = getDeviceById(CloudSim.getEntityId("cloud")); getModuleInstanceCountMap().put(cloud.getId(), new HashMap()); for(Sensor sensor : getSensors()){ String sensorType = sensor.getSensorName(); if(!getModuleInstanceCountMap().get(cloud.getId()).containsKey(sensorType)) getModuleInstanceCountMap().get(cloud.getId()).put(sensorType, 0); getModuleInstanceCountMap().get(cloud.getId()).put(sensorType, getModuleInstanceCountMap().get(cloud.getId()).get(sensorType)+1); } for(Actuator actuator : getActuators()) { String actuatorType = actuator.getActuatorType(); if(!getModuleInstanceCountMap().get(cloud.getId()).containsKey(actuatorType)) getModuleInstanceCountMap().get(cloud.getId()).put(actuatorType, 0); getModuleInstanceCountMap().get(cloud.getId()).put(actuatorType, getModuleInstanceCountMap().get(cloud.getId()).get(actuatorType)+1); } while(!isModuleInstanceCalculationComplete()) { for(AppModule module : getApplication().getModules()) { int maxInstances = 0; for(AppEdge edge : getApplication().getEdges()){ if(!getModuleInstanceCountMap().get(cloudId).containsKey(edge.getSource())) continue; if(edge.getDestination().equals(module.getName()) && edge.getDirection()==Tuple.UP){ maxInstances = Math.max(maxInstances, getModuleInstanceCountMap().get(cloudId).get(edge.getSource())); } } getModuleInstanceCountMap().get(cloudId).put(module.getName(), maxInstances); } } System.out.println(getModuleInstanceCountMap()); } private boolean isModuleInstanceCalculationComplete() { for(AppModule module : getApplication().getModules()) { if(!getModuleInstanceCountMap().get(cloudId).containsKey(module.getName())) return false; } return true; } Random r= new Random(); @Override protected void mapModules() { List fogDevs=getFogDevices(); int fogSize=fogDevs.size(); Double[] util=new Double[fogSize]; Double[] hostEnergy=new Double[fogSize]; for(int l=0;l modules = getApplication().getModules(); int mSize=modules.size(); int psoorig[]=new int[fogSize]; int l=0; System.out.println("s/../././././.......... "+ mSize); for(int p=0; p modules) { int[][] oldpop= new int[20][modules.size()]; int[][] newpop= new int[20][modules.size()]; int[][] velocity= new int[20][modules.size()]; double[] pBest= new double[20]; double gBest=Double.MAX_VALUE; double gWorst=Double.MIN_VALUE; int c1=2,c2=3; double iw=0.7; for(int k=0;k<20;k++) { for(int j=0;jpBest[b]) { gBest=pBest[b]; } if(gWorst= orig[g]) { s++; } else break; } if(s>0) { //System.out.println("update is- "+k); k=orig[s-1]; System.out.println("update is- "+getDeviceById(k).getName()); } else { k=orig[0]; System.out.println("update is- "+ k); } //return k; return orig[r.nextInt(orig.length-1)]; } private int getBest(double[] arr) { /*int res = arr[0]; for (int i = 1; i < arr.length; i++) res = Math.min(res, arr[i]); return res;*/ double min = arr[0]; int minIndex=0; //Loop through the array for (int i = 0; i < arr.length; i++) { //Compare elements of array with min if(arr[i] max) { max = arr[i]; maxIndex=i; } } return maxIndex; } public double levy(double alpha) { Random random = new Random(); double ur = random.nextDouble(); if (random.nextInt(2) != 1) { return 1 / Math.pow(ur, 1 / alpha); } else { return -1 / Math.pow(ur, 1 / alpha); } } private double getEnergy(int[] place, List modules) { double result = 0.0; double[] util=new double[getFogDevices().size()]; double[] power=new double[getFogDevices().size()]; int i=0; Map map = new LinkedHashMap<>(); int p=0; for(AppModule module : modules) { FogDevice fd=getDeviceById(place[p]); map.put(place[p], 0.0); p++; } p=0; for(AppModule module : modules) { int k=place[p]; FogDevice fd=getDeviceById(k); map.put(k, (map.get(k) + module.getMips())); p++; //System.out.println("all keys are :" + map.keySet().toString()); } //System.out.println("all module are :" + p); for (int n : map.keySet()) { //System.out.println(n+"==>"+map.get(n)); double util1=map.get(n); FogDevice d=getDeviceById(n); util1=util1/d.getHost().getTotalMips(); if(util1>1) util1=1; double power1=d.getHost().getPowerModel().getPower(util1); //power[i]=appModules.stream().mapToDouble(x -> x.getCurrentRequestedRam()).sum(); result=result+power1; //fogDevice.setEnergyConsumption(power[i]); } return result; } public List getActuators() { return actuators; } public void setActuators(List actuators) { this.actuators = actuators; } public List getSensors() { return sensors; } public void setSensors(List sensors) { this.sensors = sensors; } }