def RunSim(BestEffort, algorithmName, algorithm, AvaiableChargingStations, tankThreshold, walkingTreshold, ZoneCars, RechargingStation_Zones, Stamps_Events, DistancesFrom_Zone_Ordered, lastS, pThreshold, kwh, gamma, randomStrtingLevel, return_dict, processID, direction, city): gv.init() sf.assingVariables(city) time_init = time.time() numberOfStations = len(RechargingStation_Zones) policy, fileID, fname = foutname(BestEffort,algorithmName,AvaiableChargingStations,numberOfStations,tankThreshold, walkingTreshold, pThreshold, kwh, gamma) NRecharge = 0 NStart = 0 NEnd = 0 MeterRerouteStart = [] MeterRerouteEnd = [] NDeath = 0 ActualBooking = 0 BookingID_Car = {} ZoneID_Zone = {} sf.ReloadZonesCars(ZoneCars, ZoneID_Zone, AvaiableChargingStations) sf.FillDistancesFrom_Recharging_Zone_Ordered(DistancesFrom_Zone_Ordered,\ DistancesFrom_Recharging_Zone_Ordered,\ RechargingStation_Zones) cars = 0 for z in ZoneCars.keys(): if len(ZoneCars[z]) > 0: for i in range (len(ZoneCars[z])): ZoneCars[z][i].setRechKwh(kwh) ZoneCars[z][i].setGamma(gamma) cars+=1 print ('cars', cars) # cars = 0 # for z in ZoneCars.keys(): # if len(ZoneCars[z]) > 0: # for i in range (len(ZoneCars[z])): # print(ZoneCars[z][i].getGamma()) # cars +=1 # print('cars', cars) if randomStrtingLevel == True : for zone in ZoneCars: if len(ZoneCars[zone]) > 0 : for car in ZoneCars[zone]: car.BatteryCurrentCapacity = round(random.SystemRandom().random(),2) * car.BatteryMaxCapacity # for k in DistancesFrom_Recharging_Zone_Ordered.keys(): # print(k) # # for el in DistancesFrom_Recharging_Zone_Ordered[1]: # print('eucle', el[0]) # print('zones', el[1].getZones()) # print(el[1].getDistance()) # print() output_directory ="../output/Simulation_"+str(lastS)+"/" if not os.path.exists(output_directory): os.makedirs(output_directory) fout = open(output_directory+fname,"w") fout2 = open(output_directory+"debugproblem.txt","w") a = datetime.datetime.now() WriteOutHeader(fout, { "Provider": gv.provider, "Policy": policy, "Algorithm": algorithm, "ChargingStations":numberOfStations, "AvaiableChargingStations":AvaiableChargingStations, "TankThreshold":tankThreshold, "WalkingTreshold": walkingTreshold, "pThreshold": pThreshold, "kwh": kwh, "gamma": gamma}) fout.write("Type;ToRecharge;Recharged;ID;Lvl;Distance;Iter;Recharge;StartRecharge;Stamp;EventCoords;ZoneC;Discharge;TripDistance;FileID;extractedP;ZoneID;OccupiedCS\n") i=0 occupiedCS = 0 #with click.progressbar(Stamps_Events, length=len(Stamps_Events)) as bar: for Stamp in Stamps_Events: for Event in Stamps_Events[Stamp]: i+=1 if(Event.type == "s"): fout2.write("%d %d \n"%(Stamp,ActualBooking))#,TotalCar1,TotalCar2)) ActualBooking +=1 BookingStarting_Position = sf.coordinates_to_index(Event.coordinates) BookingID = Event.id_booking NearestCar, DistanceV, ZoneID, Iter = SearchNearestBestCar(DistancesFrom_Zone_Ordered,ZoneID_Zone,\ BookingStarting_Position, Stamp) if NearestCar.WasInRecharge == True : occupiedCS -= 1 Recharge, StartRecharge = NearestCar.Recharge(Stamp) NearestCar.setStartPosition(Event.coordinates) NearestCar.setGamma(gamma) NearestCar.setRechKwh(kwh) BookingID_Car[BookingID] = NearestCar Lvl = NearestCar.getBatteryLvl() ID = NearestCar.getID() ZoneC = zoneIDtoCoordinates(ZoneID) EventCoords = Event.coordinates #Loop Unrooling outputString = "s;" outputString += "nan;" outputString += "nan;" outputString += "%d;"% ID outputString += "%.2f;"% Lvl outputString += "%d;"% DistanceV outputString += "%d;"% Iter outputString += "%.2f;"%Recharge outputString += "%d;"%StartRecharge outputString += "%d;"% Stamp outputString += "[%.6f,%.6f];"%(EventCoords[0],EventCoords[1]) outputString += "[%.6f,%.6f];"%(ZoneC[0],ZoneC[1]) outputString += "nan;" outputString += "nan;" outputString += "%s;"%fileID outputString += "%nan;" outputString += "%d;"% ZoneID outputString += "%d\n"% occupiedCS fout.write(outputString) if(DistanceV> 0): MeterRerouteStart.append(DistanceV) NStart+=1 else: BookingEndPosition = sf.coordinates_to_index(Event.coordinates) if(BookingEndPosition<0): print(Event.coordinates) ActualBooking -=1 BookedCar = BookingID_Car[Event.id_booking] Discarge, TripDistance = BookedCar.Discharge(Event.coordinates) Lvl, ToRecharge, Recharged, DistanceV, ZoneID, Iter, extractedP = ParkCar(RechargingStation_Zones,DistancesFrom_Zone_Ordered,ZoneID_Zone,\ BookingEndPosition, BookedCar, tankThreshold, walkingTreshold, BestEffort,\ pThreshold) #extra consuption if there is rerouting # if DistanceV > 0: # BookedCar.setStartPosition(Event.coordinates) # DiscargeR, TripDistanceR = BookedCar.Discharge(sf.zoneIDtoCoordinates(ZoneID)) # Discarge += DiscargeR # TripDistance += TripDistanceR #Please notice that TripDistanceR > Distance because TripDistanceR keeps in account the corr. fact #Distance is dist(centre_arrival_Zone, centre_leaving_zone), so in this distance is biased by an error of 150m # print("Distnace", Distance) # print("Discharge", Discarge) # print("DischargeR", DiscargeR) # print("TripDistance", TripDistance) # print("TripDistanceR", TripDistanceR) # print("check", sf.haversine(Event.coordinates[0], # Event.coordinates[1], # sf.zoneIDtoCoordinates(ZoneID)[0], # sf.zoneIDtoCoordinates(ZoneID)[1] # )*gv.CorrectiveFactor # ) # print("-------------------------") BookedCar.setStartRecharge(Stamp) ID = BookedCar.getID() del BookingID_Car[Event.id_booking] ZoneC = zoneIDtoCoordinates(ZoneID) if Recharged == True : occupiedCS += 1 EventCoords = Event.coordinates #Loop Unrooling outputString = "e;" outputString += "%s;"%ToRecharge outputString += "%s;"%Recharged outputString += "%d;"% ID outputString += "%.2f;"% Lvl outputString += "%d;"% DistanceV outputString += "%d;"% Iter outputString += "nan;" outputString += "nan;" outputString += "%d;"% Stamp outputString += "[%.6f,%.6f];"%(EventCoords[0],EventCoords[1]) outputString += "[%.6f,%.6f];"%(ZoneC[0],ZoneC[1]) outputString += "%.2f;"%Discarge outputString += "%.2f;"%TripDistance outputString += "%s;"%fileID outputString += "%.2f;"%extractedP outputString += "%d;"% ZoneID outputString += "%d\n"% occupiedCS fout.write(outputString) #fout.write(dict_to_string(d)) if(DistanceV > 0): MeterRerouteEnd.append(DistanceV) if(Recharged == True): NRecharge +=1 if(BookedCar.getBatterCurrentCapacity()<0): NDeath +=1 NEnd+=1 # print (i, Event.type, occupiedCS) # if occupiedCS > AvaiableChargingStations * len(RechargingStation_Zones): # print ("Noooooo") # break b = datetime.datetime.now() c = (b - a).total_seconds() #print("End Simulation: "+str(int(c))) fout.close() fout2.close() if return_dict == None : os.system('scp %s bigdatadb:/data/03/Carsharing_data/output/Simulation_%d/%s'%(output_directory+fname,lastS,fname)) os.system('cat %s | ssh bigdatadb hdfs dfs -put -f - Simulator/output/Simulation_%s/%s' %(output_directory+fname,lastS,fname)) os.system('rm %s'%(output_directory+fname)) return if return_dict != None: PercRerouteEnd = len(MeterRerouteEnd)/NEnd*100 PercRerouteStart = len(MeterRerouteStart)/NStart*100 PercRecharge = NRecharge/NEnd*100 PercDeath = NDeath/NEnd*100 MedianMeterEnd = np.median(np.array(MeterRerouteEnd)) MeanMeterEnd = np.mean(np.array(MeterRerouteEnd)) MedianMeterStart = np.median(np.array(MeterRerouteStart)) MeanMeterStart = np.mean(np.array(MeterRerouteStart)) RetValues = {} RetValues["ProcessID"] = processID RetValues["Direction"] = direction RetValues["PercRerouteEnd"] = PercRerouteEnd RetValues["PercRerouteStart"] = PercRerouteStart RetValues["PercRecharge"] = PercRecharge RetValues["PercDeath"] = PercDeath RetValues["MedianMeterEnd"] = MedianMeterEnd RetValues["MeanMeterEnd"] = MeanMeterEnd RetValues["MedianMeterStart"] = MedianMeterStart RetValues["MeanMeterStart"] = MeanMeterStart RetValues["NEnd"] = NEnd RetValues["NStart"] = NStart RetValues['WeightedWalkedDistance'] = (MeanMeterEnd * PercRerouteEnd + ((PercRecharge - PercRerouteEnd) * 150))/100 return_dict[processID] = RetValues current_folder = os.getcwd().split("/") output_folder = "" for i in range(0,len(current_folder)-1): output_folder += current_folder[i]+"/" output_folder+="output/" print('PID %d, time: %.3f'%(processID, time.time()-time_init)) #do not use #os.system('ssh bigdatadb hdfs dfs -put /data/03/Carsharing_data/output/Simulation_%d/%s Simulator/output/Simulation_%d/%s &' %(lastS,fname,lastS,fname)) #os.system('ssh bigdatadb cat /data/03/Carsharing_data/output/Simulation_%d/%s | hdfs dfs -put -f - Simulator/output/Simulation_%s/%s &' %(lastS,fname,lastS,fname)) return RetValues
def RunSim( BestEffort, Algorithm, PolesPerStation, TankThreshold, WalkingTreshold, Zone_Cars, ChargingStationsZones, Stamps_Events, Zone_DistanceFromZones, lastS, pThreshold, Kwh, Gamma, randomStrtingLevel, ReturnDict, ProcessID, Direction, # a cosa serve? City): ZoneID_Zone = {} global Zone_DistanceFromChargingZones NumberOfStations = len(ChargingStationsZones) ''' Prepare all the information related to the simulation ''' GV.init() assingVariables(City) #Reload Zones grid with cars in the predefined starting position sf.ReloadZonesCars(Zone_Cars, ZoneID_Zone, PolesPerStation) #Load a support Matrix with distance from ANY zone to the CHARGING zones only #Used to speedup the research of an avaiable charging station Zone_DistanceFromChargingZones = sf.FillDistancesFrom_Recharging_Zone_Ordered( Zone_DistanceFromZones, ChargingStationsZones) #Prepare the dictionary with all the simulation parameter SimulationParameters = sf.SetSimulationParameter( BestEffort, Algorithm, PolesPerStation, NumberOfStations, TankThreshold, WalkingTreshold, pThreshold, Kwh, Gamma) #Prepare the folder containing the results FileID = SimulationParameters["FileID"] OutputDir = "../output/Simulation_" + str(lastS) + "/" if not os.path.exists(OutputDir): os.makedirs(OutputDir) fout = open(OutputDir + FileID + ".csv", "w") WriteOutHeader(fout, SimulationParameters) '''Q: A COSA SERVONO QUESTI 2 LOOP E PERCHÈ SEPARATI? Li usiamo?''' cars = 0 for z in Zone_Cars.keys(): if len(Zone_Cars[z]) > 0: for i in range(len(Zone_Cars[z])): Zone_Cars[z][i].setRechKwh(Kwh) Zone_Cars[z][i].setGamma(Gamma) cars += 1 #Initialized the car with a random battery level if randomStrtingLevel == True: for zone in Zone_Cars: if len(Zone_Cars[zone]) > 0: for car in Zone_Cars[zone]: car.BatteryCurrentCapacity = round( random.SystemRandom().random(), 2) * car.BatteryMaxCapacity '''''' OccupiedCS = 0 NRecharge = 0 NStart = 0 NEnd = 0 NDeath = 0 ActualBooking = 0 MeterRerouteStart = [] MeterRerouteEnd = [] BookingID_Car = {} for Stamp in Stamps_Events: for Event in Stamps_Events[Stamp]: #Start event specify a new booking starting if (Event.type == "s"): NStart += 1 ActualBooking += 1 #Look for the best car near the customer #Best car in term of battery either pluged or not NearestCar, Distance, ZoneID, Iter = SearchNearestBestCar( Zone_DistanceFromZones, ZoneID_Zone, Event, Stamp) BookingID_Car[Event.id_booking] = NearestCar if NearestCar.WasInCharge == True: OccupiedCS -= 1 Recharge, StartRecharge, StartRechageLVL, PoleID = NearestCar.setStartParameters( Stamp, Event, Gamma) OutString = toStringBooking(Stamp, Event, "nan", "nan", NearestCar, Distance, Iter, "nan", "nan", FileID, "nan", ZoneID, OccupiedCS, Recharge, StartRecharge, StartRechageLVL, PoleID) fout.write(OutString) if (Distance > 0): MeterRerouteStart.append(Distance) if (Event.type == "e"): ActualBooking -= 1 NEnd += 1 BookedCar = BookingID_Car[Event.id_booking] Discharge, TripDistance = BookedCar.setEndParameters( Stamp, Event) #Park the car according to the defined policy ToRecharge, Recharged, Distance, ZoneID, Iter, extractedP = ParkCar(ChargingStationsZones,Zone_DistanceFromZones,ZoneID_Zone,\ Event, BookedCar, TankThreshold, WalkingTreshold, BestEffort,pThreshold) if Recharged == True: OccupiedCS += 1 OutString = toStringBooking(Stamp,Event,ToRecharge,Recharged,BookedCar,Distance,Iter,\ Discharge,TripDistance,FileID,extractedP,ZoneID,OccupiedCS,-1, -1,-1, -1) fout.write(OutString) if (Distance > 0): MeterRerouteEnd.append(Distance) if (Recharged == True): NRecharge += 1 if (BookedCar.getBatteryCurrentCapacity() < 0): NDeath += 1 del BookingID_Car[Event.id_booking] fout.close() exit(0) #Compute statistics if ReturnDict was given sf.DumpStats(ReturnDict, ProcessID, Direction, OutputDir, FileID, lastS, NStart, NEnd, NRecharge, NDeath, MeterRerouteEnd, MeterRerouteStart) return