def simulate( arrayOfVehicleArrivals ): # reset global variables such as time, done/failed lots common.updateGlobals( arrayOfVehicleArrivals ) global currentTime global llfSimpleIndex # initialize a CSV document for storing all data csvGen.generateCSV( "llfSimple" ) # iterate through each vehicle in each minute for minute, numVehiclesPerMin in enumerate( arrayOfVehicleArrivals ): for vehicle in numVehiclesPerMin: port = chargePorts.openChargePort() # check if it actually needs to be charged if vehicle.currentCharge > vehicle.chargeNeeded: csvGen.exportVehicleToCSV( vehicle, "Charge Not Needed" ) common.cantChargeLot.append( vehicle ) continue # there is an open chargePort, add vehicle to it if port is not None: # add to chargePort chargePorts.chargePorts[ port ] = vehicle # make listener chargePorts.chargePortListeners[ port ].insert( 0 , chargeEvent.ChargeEvent( vehicle , common.currentTime ) ) # no open chargePort, append to llfQueue else: llfSimpleQueue.append( vehicle ) # update the llfIndex if this vehicle is better if llfSimpleIndex == -1 or vehicle.laxity < llfSimpleQueue[ llfSimpleIndex ].laxity: llfSimpleIndex = len( llfSimpleQueue ) - 1 updateVehiclesLLFSimple() common.currentTime += 1 # vehicles done arriving, now continue with the simulation while chargePorts.chargePortsEmpty() == False or len( llfSimpleQueue ) != 0: updateVehiclesLLFSimple() common.currentTime += 1 # print "LLF Simple: total number of cars: ", common.numberOfVehiclesInSimulation , \ # " elapsed time: " , common.currentTime , \ # " done charging lot: " , len( common.doneChargingLot ) , \ # " failed charging lot: " , len( common.failedLot ) , \ # " cant charge lot: " , len( common.cantChargeLot ) , \ # " llfQueue size: " , len( llfSimpleQueue ) , \ # " chargePort " , chargePorts.toString() # write a CSV of all the data in chargePortListeners csvGen.exportChargePortsToCSV( "llfSimple" ) output = [common.calcProfit(), len(common.doneChargingLot), len(common.failedLot), len(common.declinedLot), common.numberOfVehiclesInSimulation, common.currentTime] return output
def updateVehiclesFCFSAC(): # check each chargePort for index, vehicle in enumerate( chargePorts.chargePorts ): # add 1 minute of charge if vehicle is not None: vehicle.currentCharge += ( vehicle.chargeRate ) / 60.0 removed = False; # check if done charging if vehicle.currentCharge >= vehicle.chargeNeeded: # this vehicle is on the out, so wrap up its listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle from grid and document it csvGen.exportVehicleToCSV( vehicle, "SUCCESS" ) common.doneChargingLot.append( vehicle ) del schedules[ index ][ 0 ] # remove the vehicle from the schedule # the next vehicle if len( schedules[ index ] ) != 0: nextVehicle = schedules[ index ][ 0 ] chargePorts.chargePorts[ index ] = nextVehicle # and then make a new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) else: chargePorts.chargePorts[ index ] = None removed = True; # check if deadline reached, should never happen with admission control if common.currentTime >= vehicle.depTime and not removed: # print "current time: ", common.currentTime, " depTime: ", vehicle.depTime, " timeLeft: ", vehicle.timeLeftToCharge() # this vehicle is on the out, so wrap up its listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle from grid and document it csvGen.exportVehicleToCSV( vehicle, "FAILURE" ) common.failedLot.append( vehicle ) del schedules[ index ][ 0 ] # remove the vehicle for the schedule # the next vehicle if len( schedules[ index ] ) != 0: nextVehicle = schedules[ index ][ 0 ] chargePorts.chargePorts[ index ] = nextVehicle # and then make a new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) else: chargePorts.chargePorts[ index ] = None
def simulate( arrayOfVehicleArrivals ): # reset global variables such as time, done/failed lots common.updateGlobals( arrayOfVehicleArrivals ) global currentTime global earliestDLIndex # initialize a CSV document for storing all data csvGen.generateCSV( "edf" ) # iterate through each vehicle in each minute for minute, numVehiclesPerMin in enumerate( arrayOfVehicleArrivals ): for vehicle in numVehiclesPerMin: port = chargePorts.openChargePort() # check if it actually needs to be charged if vehicle.currentCharge > vehicle.chargeNeeded: csvGen.exportVehicleToCSV( vehicle, "Charge Not Needed" ) common.cantChargeLot.append( vehicle ) continue # a port is open so start charging the vehicle if port is not None: # add to chargePort chargePorts.chargePorts[ port ] = vehicle # initialize a listener object for its charging activity chargePorts.chargePortListeners[ port ].insert( 0 , chargeEvent.ChargeEvent( vehicle, common.currentTime ) ) # no ports available so put in queue else: edfQueue.append( vehicle ) if earliestDLIndex == -1 or vehicle.depTime < edfQueue[ earliestDLIndex ].depTime: earliestDLIndex = len( edfQueue ) - 1 updateVehiclesEDF() common.currentTime += 1 # vehicles done arriving, now continue with the simulation while chargePorts.chargePortsEmpty() == False or not len( edfQueue ) == 0: updateVehiclesEDF() common.currentTime += 1 # print "EDF: total number of cars: ", common.numberOfVehiclesInSimulation , \ # " elapsed time: " , common.currentTime , \ # " done charging lot: " , len( common.doneChargingLot ) , \ # " failed charging lot: " , len( common.failedLot ) , \ # " cant charge lot: " , len( common.cantChargeLot ) , \ # " edfQueue size: " , len( edfQueue ) , \ # " chargePort " , chargePorts.toString() # write a CSV with all the chargePort logs csvGen.exportChargePortsToCSV( "edf" ) output = [common.calcProfit(), len(common.doneChargingLot), len(common.failedLot), len(common.declinedLot), common.numberOfVehiclesInSimulation, common.currentTime] return output
def updateVehiclesFCFS(): # check each chargePort for index, vehicle in enumerate( chargePorts.chargePorts ): # add 1 minute of charge if vehicle is not None: vehicle.currentCharge += ( vehicle.chargeRate ) / 60.0 removed = False; # check if done charging if vehicle.currentCharge >= vehicle.chargeNeeded: # this vehicle is on the out, so wrap up its listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle from grid and document it csvGen.exportVehicleToCSV( vehicle, "SUCCESS" ) common.doneChargingLot.append( vehicle ) # the next vehicle if not queue.empty(): nextVehicle = queue.get() chargePorts.chargePorts[ index ] = nextVehicle # and then make a new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) else: chargePorts.chargePorts[ index ] = None removed = True; # check if deadline reached if common.currentTime >= vehicle.depTime and not removed: # this vehicle is on the out, so wrap up its listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle from grid and document it csvGen.exportVehicleToCSV( vehicle, "FAILURE" ) common.failedLot.append( vehicle ) # the next vehicle if not queue.empty(): nextVehicle = queue.get() chargePorts.chargePorts[ index ] = nextVehicle # and then make a new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) else: chargePorts.chargePorts[ index ] = None
def simulate( arrayOfVehicleArrivals ): # reset global variables such as time, done/failed lots common.updateGlobals( arrayOfVehicleArrivals ) global currentTime # initialize a CSV document for storing all data csvGen.generateCSV( "fcfs" ) # iterate through each vehicle in each minute for minute, numVehiclesPerMin in enumerate( arrayOfVehicleArrivals ): for vehicle in numVehiclesPerMin: port = chargePorts.openChargePort() if vehicle.currentCharge > vehicle.chargeNeeded: csvGen.exportVehicleToCSV( vehicle, "Charge Not Needed" ) common.cantChargeLot.append( vehicle ) continue # a port is open so start charging the vehicle if port is not None: # add to chargePort chargePorts.chargePorts[ port ] = vehicle # initialize a listener object for its charging activity chargePorts.chargePortListeners[ port ].insert( 0 , chargeEvent.ChargeEvent( vehicle, common.currentTime ) ) # no ports are available so put the vehicle in the queue else: queue.put( vehicle ) updateVehiclesFCFS() common.currentTime += 1 # print "status: " , openChargePort() , " " , queue.empty() # run the clock until all vehicles have ran through the simulation while chargePorts.chargePortsEmpty() == False or not queue.empty(): updateVehiclesFCFS() common.currentTime += 1 # print "FCFS: total number of cars: ", common.numberOfVehiclesInSimulation , \ # " elapsed time: " , common.currentTime , \ # " done charging lot: " , len( common.doneChargingLot ) , \ # " failed charging lot: " , len( common.failedLot ) , \ # " cant charge lot: " , len( common.cantChargeLot ) , \ # " fcfsQueue size: " , queue.qsize() , \ # " chargePort " , chargePorts.toString() # write a CSV for all the chargePort logs csvGen.exportChargePortsToCSV( "fcfs" ) output = [common.calcProfit(), len(common.doneChargingLot), len(common.failedLot), len(common.declinedLot), common.numberOfVehiclesInSimulation, common.currentTime] return output
def updateVehiclesEDF(): global earliestDLIndex global latestChargePortDLIndex # cheack each chargePort for index, vehicle in enumerate( chargePorts.chargePorts ): # add one minute of charge if vehicle is not None: vehicle.currentCharge += ( vehicle.chargeRate ) / 60.0 removed = False #check if done charging if vehicle.currentCharge >= vehicle.chargeNeeded: # finish up the listener for this vehicle chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle from grid and document it csvGen.exportVehicleToCSV( vehicle, "SUCCESS" ) common.doneChargingLot.append( vehicle ) if len( edfQueue ) > 0: # get next vehicle and throw in chargePort nextVehicle = edfQueue[ earliestDLIndex ] chargePorts.chargePorts[ index ] = nextVehicle # make it a listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) # update queue del edfQueue[ earliestDLIndex ] earliestDLIndex = earliestDL() else: chargePorts.chargePorts[ index ] = None removed = True # check if deadline reached if common.currentTime >= vehicle.depTime and not removed: # this vehicle is on the out, so wrap up its listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle and document it csvGen.exportVehicleToCSV( vehicle, "FAILURE" ) common.failedLot.append( vehicle ) if len( edfQueue ) > 0: # get nextVehicle nextVehicle = edfQueue[ earliestDLIndex ] chargePorts.chargePorts[ index ] = nextVehicle # make new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) # update queue del edfQueue[ earliestDLIndex ] earliestDLIndex = earliestDL() else: chargePorts.chargePorts[ index ] = None # now we want to make sure that all the cars in the chargePorts are the best choices # we want to know the index of the worst car charging and compare that to the index of the best in the queue # also need to be able to cycle the queue so that it will put ALL the cars in the queue that are better into the chargePorts # edge cases to worry about: queue is empty, earliestDLIndex = -1 # start out by grabbing the latest chargePort latestChargePortDLIndex = latestChargePortDL() # prioritize edge cases, loop until swap the top DL are all in the queue while len( edfQueue ) > 0 and latestChargePortDLIndex != -1 and edfQueue[ earliestDLIndex ].depTime < chargePorts.chargePorts[ latestChargePortDLIndex ].depTime: swappingOut = chargePorts.chargePorts[ latestChargePortDLIndex ] swappingIn = edfQueue[ earliestDLIndex ] # close the listener for swappingOut chargePorts.chargePortListeners[ latestChargePortDLIndex ][ 0 ].terminateCharge( swappingOut , common.currentTime ) # swap occurs in the chargePorts chargePorts.chargePorts[ latestChargePortDLIndex ] = swappingIn # create a new listener for the vehicle that just got swapped in chargePorts.chargePortListeners[ latestChargePortDLIndex ].insert( 0 , chargeEvent.ChargeEvent( swappingIn , common.currentTime ) ) # swap finishes in the queue edfQueue[ earliestDLIndex ] = swappingOut # now update values for comparison earliestDLIndex = earliestDL() latestChargePortDLIndex = latestChargePortDL()
def updateVehiclesEDFPro(): #global schedules # cheack each chargePort for index, vehicle in enumerate( chargePorts.chargePorts ): # add one minute of charge if vehicle is not None: vehicle.currentCharge += ( vehicle.chargeRate ) / 60.0 removed = False #check if done charging if vehicle.currentCharge >= vehicle.chargeNeeded: # finish up the listener for this vehicle chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle from grid and document it csvGen.exportVehicleToCSV( vehicle, "SUCCESS" ) common.doneChargingLot.append( vehicle ) del schedules[ index ][ 0 ] # remove the vehicle from the schedule # the next vehicle if len( schedules[ index ] ) > 0: # get next vehicle and throw in chargePort nextVehicle = schedules[ index ][ 0 ] chargePorts.chargePorts[ index ] = nextVehicle # make it a listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) else: chargePorts.chargePorts[ index ] = None removed = True # check if deadline reached # shouldn't run into this with adminControl, but practice safe algorithms if common.currentTime >= vehicle.depTime and not removed: # this vehicle is on the out, so wrap up its listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle and document it csvGen.exportVehicleToCSV( vehicle, "FAILURE" ) common.failedLot.append( vehicle ) del schedules[ index ][ 0 ] # remove the vehicle for the schedule if len( schedules[ index ] ) > 0: # get nextVehicle nextVehicle = schedules[ index ][ 0 ] chargePorts.chargePorts[ index ] = nextVehicle # make new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) else: chargePorts.chargePorts[ index ] = None # a quick QA check if len( schedules[ index ] ) > 1: prev = schedules[ index ][ 0 ] for i in range( 1 , len( schedules[ index ] ) ): # the prev car should always have a depTime <= to current if prev.depTime > schedules[ index ][ i ].depTime: return "insert not working, schedules out of order" # update prev prev = schedules[ index ][ i ] # there are cases when a new car arrived with the earliestDL, but all swaps are done at discrete intervals # in this case, it is currently at index 0, but not being charged. This part will change that # look through each schedule for index, schedule in enumerate( schedules ): # compare schedule[ 0 ].depTime with that in the chargePort. swap out if different if len( schedule ) > 0: if schedule[ 0 ].depTime < chargePorts.chargePorts[ index ].depTime: # close the listener for swappingOut chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( chargePorts.chargePorts[ index ] , common.currentTime ) # swap occurs in the chargePorts chargePorts.chargePorts[ index ] = schedules[ index ][ 0 ] # create a new listener for the vehicle that just got swapped in chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( schedules[ index ][ 0 ] , common.currentTime ) )
def simulate( arrayOfVehicleArrivals ): #global schedules # reset global variables such as time, done/failed lots common.updateGlobals( arrayOfVehicleArrivals ) global currentTime # global earliestDLIndex # initialize a CSV document for storing all data csvGen.generateCSV( "edfPro" ) # iterate through each vehicle in each minute for minute, numVehiclesPerMin in enumerate( arrayOfVehicleArrivals ): for vehicle in numVehiclesPerMin: port = chargePorts.openChargePort() # check if it actually needs to be charged if vehicle.currentCharge > vehicle.chargeNeeded: csvGen.exportVehicleToCSV( vehicle, "Charge Not Needed" ) common.cantChargeLot.append( vehicle ) continue # a port is open so start charging the vehicle if port is not None: # add to chargePort and schedule chargePorts.chargePorts[ port ] = vehicle schedules[ port ].insert( 0 , vehicle ) if len( schedules[ port ] ) > 1: print "empty chargePort, but shit was scheduled to be there" break # initialize a listener object for its charging activity chargePorts.chargePortListeners[ port ].insert( 0 , chargeEvent.ChargeEvent( vehicle, common.currentTime ) ) # no ports available so try to put it in a queue, but have to find one that will work else: bestSchedule = -1 scheduleFlex = float( "inf" ); # iterate through every schedule for index, schedule in enumerate( schedules ): # insert a vehicle in the schedule and get its admission feasibility insertLocation = insertIntoSchedule( vehicle , index ) insertLocation = int( insertLocation ) admissionTest = genAdmissionFeasiblity( index ) # will it work? tempFlex = admissionFeasibility( index , admissionTest ) # check if it can work for this sched if tempFlex == False: # if it can't work, delete from schedule and move on del schedules[ index ][ insertLocation ] continue # now check if it's the best one if tempFlex < scheduleFlex: scheduleFlex = tempFlex bestSchedule = index # regardless, delete from schedule for now del schedules[ index ][ insertLocation ] # now will we do an official insert? or decline if bestSchedule >= 0 and scheduleFlex > 0: insertIntoSchedule( vehicle , bestSchedule ) # decline else: csvGen.exportVehicleToCSV( vehicle, "DECLINED" ) common.declinedLot.append( vehicle ) updateVehiclesEDFPro() common.currentTime += 1 # vehicles done arriving, now continue with the simulation while chargePorts.chargePortsEmpty() == False or not schedulesEmpty(): updateVehiclesEDFPro() common.currentTime += 1 # print "EDFPro: total number of cars: ", common.numberOfVehiclesInSimulation , \ # " elapsed time: " , common.currentTime , \ # " done charging lot: " , len( common.doneChargingLot ) , \ # " failed charging lot: " , len( common.failedLot ) , \ # " declined lot: ", len( common.declinedLot ), \ # " cant charge lot: " , len( common.cantChargeLot ) , \ # " schedules: " , schedulesEmpty() , \ # " chargePort " , chargePorts.toString() # write a CSV with all the chargePort logs csvGen.exportChargePortsToCSV( "edfPro" ) output = [common.calcProfit(), len(common.doneChargingLot), len(common.failedLot), len(common.declinedLot), common.numberOfVehiclesInSimulation, common.currentTime] return output
def updateVehiclesLLFSimple(): global currentTime global llfSimpleIndex # increment the charge for the cars that were charging for index, vehicle in enumerate( chargePorts.chargePorts ): # add one minute of charge if vehicle is not None: vehicle.currentCharge += ( vehicle.chargeRate ) / 60.0 # now move cars around so the laxity property is maintained for index, vehicle in enumerate( chargePorts.chargePorts ): if vehicle is not None: removed = False #check if done charging if vehicle.currentCharge >= vehicle.chargeNeeded: # finish up listener chargePorts.chargePortListeners[ index][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle from grid, document it csvGen.exportVehicleToCSV( vehicle, "SUCCESS" ) common.doneChargingLot.append( vehicle ) if len( llfSimpleQueue ) > 0: # get next vehicle, throw in chargePort nextVehicle = llfSimpleQueue[ llfSimpleIndex ] chargePorts.chargePorts[ index ] = nextVehicle # make it a listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle, common.currentTime ) ) # update queue del llfSimpleQueue[ llfSimpleIndex ] llfSimpleIndex = lowestLaxity() else: chargePorts.chargePorts[ index ] = None removed = True # check if deadline reached if common.currentTime >= vehicle.depTime and not removed: # wrap up listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle, document it csvGen.exportVehicleToCSV( vehicle, "FAILURE" ) common.failedLot.append( vehicle ) if len( llfSimpleQueue ) > 0: # get next vehicle nextVehicle = llfSimpleQueue[ llfSimpleIndex ] chargePorts.chargePorts[ index ] = nextVehicle # make new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) # update queue del llfSimpleQueue[ llfSimpleIndex ] llfSimpleIndex = lowestLaxity() # function defined in LLF section, iterates through llfQueue for lowest laxity else: chargePorts.chargePorts[ index ] = None highestLaxityChargePortIndex = highestLaxityChargePort() # check if all cars in chargePorts still have lowest laxity while len( llfSimpleQueue ) > 0 and highestLaxityChargePortIndex != -1 and llfSimpleQueue[ llfSimpleIndex ].laxity < chargePorts.chargePorts[ highestLaxityChargePortIndex ].laxity: swappingOut = chargePorts.chargePorts[ highestLaxityChargePortIndex ] swappingIn = llfSimpleQueue[ llfSimpleIndex ] # close the listener for swappingOut chargePorts.chargePortListeners[ highestLaxityChargePortIndex ][ 0 ].terminateCharge( swappingOut , common.currentTime ) # swap occurs in chargePorts chargePorts.chargePorts[ highestLaxityChargePortIndex ] = swappingIn # create a new listener chargePorts.chargePortListeners[ highestLaxityChargePortIndex ].insert( 0 , chargeEvent.ChargeEvent( swappingIn , common.currentTime ) ) # swap occurs in the queue llfSimpleQueue[ llfSimpleIndex ] = swappingOut # now update values for comparison llfSimpleIndex = lowestLaxity() highestLaxityChargePortIndex = highestLaxityChargePort()
def updateVehiclesLLF(): global currentTime global llfIndex # increment the charge for the cars that were charging for index, vehicle in enumerate( chargePorts.chargePorts ): # add one minute of charge if vehicle is not None: vehicle.currentCharge += ( vehicle.chargeRate ) / 60.0 # print "Charge: " , vehicle.currentCharge , " " , vehicle.chargeNeeded # print "Timing: " , currentTime , " ", vehicle.depTime # update the laxity for all the peeps updateLaxityForAll() # now move cars around so the laxity property is maintained for index, vehicle in enumerate( chargePorts.chargePorts ): if vehicle is not None: removed = False #check if done charging if vehicle.currentCharge >= vehicle.chargeNeeded: # finish up the listener for this vehicle chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle, common.currentTime ) # remove finished vehicle and document it csvGen.exportVehicleToCSV( vehicle, "SUCCESS" ) common.doneChargingLot.append( vehicle ) if len( llfQueue ) > 0: # get next vehicle and throw in chargePort nextVehicle = llfQueue[ llfIndex ] chargePorts.chargePorts[ index ] = nextVehicle # make it a listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle, common.currentTime ) ) # update queue del llfQueue[ llfIndex ] llfIndex = lowestLaxity() else: chargePorts.chargePorts[ index ] = None removed = True # check if deadline reached if common.currentTime >= vehicle.depTime and not removed: # wrap up the listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle, common.currentTime ) # remove finished vehicle and document it csvGen.exportVehicleToCSV( vehicle, "FAILURE" ) common.failedLot.append( vehicle ) if len( llfQueue ) > 0: # get next vehicle nextVehicle = llfQueue[ llfIndex ] chargePorts.chargePorts[ index ] = nextVehicle # make it a listener chargePorts.chargePortListeners[ index ].insert( 0, chargeEvent.ChargeEvent( nextVehicle, common.currentTime ) ) # update queue del llfQueue[ llfIndex ] llfIndex = lowestLaxity() else: chargePorts.chargePorts[ index ] = None # check to make sure all the cars in the chargePorts are the best possible choices # get index of the worst that is charging highestLaxityChargePortIndex = highestLaxityChargePort() # check if all cars in chargePorts still have lowest laxity while len( llfQueue ) > 0 and highestLaxityChargePortIndex != -1 and llfQueue[ llfIndex ].laxity < chargePorts.chargePorts[ highestLaxityChargePortIndex ].laxity: swappingOut = chargePorts.chargePorts[ highestLaxityChargePortIndex ] swappingIn = llfQueue[ llfIndex ] # close the listener for swappingOut chargePorts.chargePortListeners[ highestLaxityChargePortIndex ][ 0 ].terminateCharge( swappingOut, common.currentTime ) # swap occurs in the chargePorts chargePorts.chargePorts[ highestLaxityChargePortIndex ] = swappingIn # create a new listener for swappingIn chargePorts.chargePortListeners[ highestLaxityChargePortIndex ].insert( 0 , chargeEvent.ChargeEvent( swappingIn , common.currentTime ) ) # swap finishes up in the queue llfQueue[ llfIndex ] = swappingOut # now update values for comparison llfIndex = lowestLaxity() highestLaxityChargePortIndex = highestLaxityChargePort()
def simulate( arrayOfVehicleArrivals ): # reset global variables such as time, done/failed lots common.updateGlobals( arrayOfVehicleArrivals ) global currentTime # initialize a CSV document for storing all data csvGen.generateCSV( "fcfsAC" ) # iterate through each vehicle in each minute for minute, numVehiclesPerMin in enumerate( arrayOfVehicleArrivals ): for vehicle in numVehiclesPerMin: port = chargePorts.openChargePort() # in case something wrong with distribution, pull vehicle out of system if vehicle.currentCharge > vehicle.chargeNeeded: csvGen.exportVehicleToCSV( vehicle, "Charge Not Needed" ) common.cantChargeLot.append( vehicle ) continue # a port is open so start charging the vehicle if port is not None: # add to chargePort chargePorts.chargePorts[ port ] = vehicle schedules[ port ].append(vehicle) # initialize a listener object for its charging activity chargePorts.chargePortListeners[ port ].insert( 0 , chargeEvent.ChargeEvent( vehicle, common.currentTime ) ) # no ports are available so put the vehicle in the queue else: # queue.put( vehicle ) bestPortInfo = findEarliestEndingSchedule() bestPortIndex = bestPortInfo[ 0 ] #index bestPortEndTime = bestPortInfo[ 1 ] #end time # vehicle declined because not enough time to charge if vehicle.depTime - bestPortEndTime < vehicle.timeToCharge: csvGen.exportVehicleToCSV( vehicle, "DECLINED" ) common.declinedLot.append( vehicle ) # vehicle appended to best schedule else: schedules[ bestPortIndex ].append( vehicle ) updateVehiclesFCFSAC() common.currentTime += 1 # print "status: " , openChargePort() , " " , queue.empty() # run the clock until all vehicles have ran through the simulation while chargePorts.chargePortsEmpty() == False or not schedulesEmpty(): updateVehiclesFCFSAC() common.currentTime += 1 # print "FCFSAC: total number of cars: ", common.numberOfVehiclesInSimulation , \ # " elapsed time: " , common.currentTime , \ # " done charging lot: " , len( common.doneChargingLot ) , \ # " failed charging lot: " , len( common.failedLot ) , \ # " cant charge lot: " , len( common.cantChargeLot ) , \ # " declined lot: " , len( common.declinedLot ) , \ # " fcfsACQueue schedules: " , schedules , \ # " chargePort " , chargePorts.toString() # write a CSV for all the chargePort logs csvGen.exportChargePortsToCSV( "fcfsAC" ) output = [common.calcProfit(), len(common.doneChargingLot), len(common.failedLot), len(common.declinedLot), common.numberOfVehiclesInSimulation, common.currentTime] return output
def simulate(arrayOfVehicleArrivals): # reset global variables such as time, done/failed lots common.updateGlobals(arrayOfVehicleArrivals) global currentTime global llfIndex # initialize a CSV document for storing all data csvGen.generateCSV("llfSmartACB") # iterate through each vehicle in each minute for minute, numVehiclesPerMin in enumerate(arrayOfVehicleArrivals): for vehicle in numVehiclesPerMin: port = chargePorts.openChargePort() # check if it actually needs to be charged if vehicle.currentCharge > vehicle.chargeNeeded: csvGen.exportVehicleToCSV(vehicle, "Charge Not Needed") common.cantChargeLot.append(vehicle) continue # there is an open chargePort, add vehicle to it if port is not None: # add to chargePort chargePorts.chargePorts[port] = vehicle # initialize a listener object for its charging activity chargePorts.chargePortListeners[port].insert( 0, chargeEvent.ChargeEvent(vehicle, common.currentTime)) # no open chargePort, append to llfQueue else: if vehicleCanFit(vehicle): llfQueue.append(vehicle) # update the llfIndex if this vehicle is better if llfIndex == -1 or vehicle.laxity < llfQueue[ llfIndex].laxity: llfIndex = len(llfQueue) - 1 else: csvGen.exportVehicleToCSV(vehicle, "DECLINED") common.declinedLot.append(vehicle) updateVehiclesLLF() common.currentTime += 1 # vehicles done arriving, now continue with the simulation while chargePorts.chargePortsEmpty() == False or not len(llfQueue) == 0: updateVehiclesLLF() common.currentTime += 1 # print "LLF Complex: total number of cars: ", common.numberOfVehiclesInSimulation , \ # " elapsed time: " , common.currentTime , \ # " done charging lot: " , len( common.doneChargingLot ) , \ # " failed charging lot: " , len( common.failedLot ) , \ # " declined lot: ", len( common.declinedLot ), \ # " cant charge lot: " , len( common.cantChargeLot ) , \ # " llfQueue size: " , len( llfQueue ) , \ # " chargePort " , chargePorts.toString() # write the CSV with all the chargePort logs csvGen.exportChargePortsToCSV("llfSmartACB") output = [ common.calcProfit(), len(common.doneChargingLot), len(common.failedLot), len(common.declinedLot), common.numberOfVehiclesInSimulation, common.currentTime ] return output
def updateVehicles(): # print "------------ next update -----------" # check each chargePort for index, vehicle in enumerate( chargePorts.chargePorts ): if vehicle is not None: removed = False # add 1 minute of charge vehicle.currentCharge += ( vehicle.chargeRate ) / 60.0 minutesCharged[vehicle.id] += 1 # when would we kick out a vehicle? # definitely when it's hit its charge or when one is scheduled to start next if ( vehicle.currentCharge >= vehicle.chargeNeeded ) or ( len( schedules[ index ] ) > 1 and schedules[ index ][ 1 ].startTime == common.currentTime ): # print "vehicle ", vehicle.id, " is done or the next one is starting" # finish up the listener for this vehicle chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle from grid and document it csvGen.exportVehicleToCSV( vehicle, "SUCCESS" ) common.doneChargingLot.append( vehicle ) # print "success: ",vehicle.id removed = True # remove the vehicle that was charging from the front of the schedule del schedules[ index ][ 0 ] # now add the next vehicle from the schedule, if possible if len( schedules[ index ] ) > 0: # next vehicle nextVehicle = schedules[ index ][ 0 ] while nextVehicle is not None and nextVehicle.depTime < common.currentTime: csvGen.exportVehicleToCSV( nextVehicle, "FAILURE" ) common.failedLot.append( nextVehicle ) # print "failed here: ", nextVehicle.id del schedules[ index ][ 0 ] nextVehicle = None if len(schedules[ index ]) > 0: nextVehicle = schedules[ index ][ 0 ] if nextVehicle is not None: chargePorts.chargePorts[ index ] = nextVehicle # make new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) else: chargePorts.chargePorts[ index ] = None # no vehicles to add in schedule else: chargePorts.chargePorts[index] = None # probably not going to be used, but we can still check for depTime if common.currentTime >= vehicle.depTime and not removed: # this vehicle is on the out, so wrap up its listener chargePorts.chargePortListeners[ index ][ 0 ].terminateCharge( vehicle , common.currentTime ) # remove finished vehicle and document it csvGen.exportVehicleToCSV( vehicle, "FAILURE" ) common.failedLot.append( vehicle ) # print "failed here2: ", vehicle.id del schedules[ index ][ 0 ] # now add the next vehicle from the schedule, if possible if len( schedules[ index ] ) > 0: # next vehicle nextVehicle = schedules[ index ][ 0 ] while nextVehicle is not None and nextVehicle.depTime < common.currentTime: # print "vehicle ",nextVehicle.id," was next but its depTime passed" csvGen.exportVehicleToCSV( nextVehicle, "FAILURE" ) common.failedLot.append(nextVehicle) # print "failed here3: ", nextVehicle.id del schedules[ index ][ 0 ] nextVehicle = None if len(schedules[ index ]) > 0: nextVehicle = schedules[ index ][ 0 ] if nextVehicle is not None: chargePorts.chargePorts[ index ] = nextVehicle # make new listener chargePorts.chargePortListeners[ index ].insert( 0 , chargeEvent.ChargeEvent( nextVehicle , common.currentTime ) ) else: chargePorts.chargePorts[ index ] = None else: chargePorts.chargePorts[ index ] = None
def simulate( arrayOfVehicleArrivals ): # reset global variables such as time, done/failed lots common.updateGlobals( arrayOfVehicleArrivals ) global currentTime global numOverlapInserts global minutesCharged global allVehicles minutesCharged = [0] * common.numberOfVehiclesInSimulation allVehicles = [] # initialize a CSV document for storing all data csvGen.generateCSV( "dsac" ) # iterate through each vehicle in each minute for minute, numVehiclesPerMin in enumerate( arrayOfVehicleArrivals ): for vehicle in numVehiclesPerMin: port = chargePorts.openChargePort() allVehicles.append(vehicle) if vehicle.currentCharge > vehicle.chargeNeeded: csvGen.exportVehicleToCSV( vehicle, "Charge Not Needed" ) common.cantChargeLot.append( vehicle ) print "CHARGE NOT NEEDED: ",vehicle.id continue # a port is open so start charging the vehicle if port is not None: # add to chargePort and respective schedule chargePorts.chargePorts[ port ] = vehicle schedules[ port ].append( vehicle ) # initialize a listener object for its charging activity chargePorts.chargePortListeners[ port ].insert( 0 , chargeEvent.ChargeEvent( vehicle, common.currentTime ) ) continue # no ports are available so put the vehicle in the queue else: # any appendables? appendable = findAppendableChargePort( vehicle ) # add to schedule if appendable != -1: # there is at least one car in the schedule if len(schedules[ appendable ]) > 0: tempVehicle = schedules[ appendable ][ len( schedules[ appendable ] ) - 1 ] #last item newStartTime = tempVehicle.startTime + tempVehicle.timeToCharge # schedule is appendable because it is empty else: carInChargePort = chargePorts.chargePorts[ appendable ] newStartTime = carInChargePort.startTime + carInChargePort.timeToCharge vehicle.updateStartTime( newStartTime ) schedules[ appendable ].append( vehicle ) # an ugly conflict else: vehicle.updateStartTime( vehicle.depTime - vehicle.timeToCharge ) leastProfitConflictResults = leastProfitConflict( vehicle ) leastProfitConflictPort = leastProfitConflictResults[0] leastProfitConflictSchedule = leastProfitConflictResults[1] # vehicle appended with conflict if leastProfitConflictPort > 0: schedules[ leastProfitConflictPort ] = leastProfitConflictSchedule numOverlapInserts += 1 if leastProfitConflictSchedule[0] == vehicle: chargePorts.chargePorts[leastProfitConflictPort] = vehicle else: # CSV to decline car csvGen.exportVehicleToCSV( vehicle, "DECLINED" ) common.declinedLot.append( vehicle ) updateVehicles() common.currentTime += 1 # vehicles done arriving, now continue with the simulation while not chargePorts.chargePortsEmpty() or not schedulesEmpty():\ updateVehicles() common.currentTime += 1 # print "DSAC: total number of cars: ", common.numberOfVehiclesInSimulation , \ # " elapsed time: " , common.currentTime , \ # " done charging lot: " , len( common.doneChargingLot ) , \ # " failed charging lot: " , len( common.failedLot ) , \ # " declind lot: ", len( common.declinedLot ) , \ # " cant charge lot: " , len( common.cantChargeLot ) , \ # " schedules: " , schedulesToString() , \ # " chargePorts " , chargePorts.toString() # write a CSV for all the chargePort logs csvGen.exportChargePortsToCSV( "dsac" ) failed = uniqueVehicles(common.failedLot) done = uniqueVehicles(common.doneChargingLot) - failed # if a car gets split and is in done and failed, that is considered failed declined = uniqueVehicles(common.declinedLot) output = [totalProfit(), len(done), len(failed), len(declined), common.numberOfVehiclesInSimulation, common.currentTime] return output
def updateVehiclesLLFSimple(): global currentTime global llfSimpleIndex # increment the charge for the cars that were charging for index, vehicle in enumerate(chargePorts.chargePorts): # add one minute of charge if vehicle is not None: vehicle.currentCharge += (vehicle.chargeRate) / 60.0 # now move cars around so the laxity property is maintained for index, vehicle in enumerate(chargePorts.chargePorts): if vehicle is not None: removed = False #check if done charging if vehicle.currentCharge >= vehicle.chargeNeeded: # finish up listener chargePorts.chargePortListeners[index][0].terminateCharge( vehicle, common.currentTime) # remove finished vehicle from grid, document it csvGen.exportVehicleToCSV(vehicle, "SUCCESS") common.doneChargingLot.append(vehicle) if len(llfSimpleQueue) > 0: # get next vehicle, throw in chargePort nextVehicle = llfSimpleQueue[llfSimpleIndex] chargePorts.chargePorts[index] = nextVehicle # make it a listener chargePorts.chargePortListeners[index].insert( 0, chargeEvent.ChargeEvent(nextVehicle, common.currentTime)) # update queue del llfSimpleQueue[llfSimpleIndex] llfSimpleIndex = lowestLaxity() else: chargePorts.chargePorts[index] = None removed = True # check if deadline reached if common.currentTime >= vehicle.depTime and not removed: # wrap up listener chargePorts.chargePortListeners[index][0].terminateCharge( vehicle, common.currentTime) # remove finished vehicle, document it csvGen.exportVehicleToCSV(vehicle, "FAILURE") common.failedLot.append(vehicle) if len(llfSimpleQueue) > 0: # get next vehicle nextVehicle = llfSimpleQueue[llfSimpleIndex] chargePorts.chargePorts[index] = nextVehicle # make new listener chargePorts.chargePortListeners[index].insert( 0, chargeEvent.ChargeEvent(nextVehicle, common.currentTime)) # update queue del llfSimpleQueue[llfSimpleIndex] llfSimpleIndex = lowestLaxity( ) # function defined in LLF section, iterates through llfQueue for lowest laxity else: chargePorts.chargePorts[index] = None highestLaxityChargePortIndex = highestLaxityChargePort() # check if all cars in chargePorts still have lowest laxity while len( llfSimpleQueue ) > 0 and highestLaxityChargePortIndex != -1 and llfSimpleQueue[ llfSimpleIndex].laxity < chargePorts.chargePorts[ highestLaxityChargePortIndex].laxity: swappingOut = chargePorts.chargePorts[ highestLaxityChargePortIndex] swappingIn = llfSimpleQueue[llfSimpleIndex] # close the listener for swappingOut chargePorts.chargePortListeners[highestLaxityChargePortIndex][ 0].terminateCharge(swappingOut, common.currentTime) # swap occurs in chargePorts chargePorts.chargePorts[ highestLaxityChargePortIndex] = swappingIn # create a new listener chargePorts.chargePortListeners[ highestLaxityChargePortIndex].insert( 0, chargeEvent.ChargeEvent(swappingIn, common.currentTime)) # swap occurs in the queue llfSimpleQueue[llfSimpleIndex] = swappingOut # now update values for comparison llfSimpleIndex = lowestLaxity() highestLaxityChargePortIndex = highestLaxityChargePort()