コード例 #1
0
def check_and_run_missing(lastS, Stamps_Events, DistancesFrom_Zone_Ordered, ZoneCars, city):

    gv.init()
    sf.assingVariables(city)
    
    Missing = [""]
    
    Run = 0
    while len(Missing) > 0:
        Missing = CheckMissing(lastS)        
        RunMissing(Missing, Stamps_Events, DistancesFrom_Zone_Ordered, ZoneCars, lastS, city)
        Run += 1
        if(Run > 10):
            print("###ERROR### Impossible to conclude simulations after 10 attempts. Still Missing: %d simulations" % len(Missing))
            return -1

    Run = 0
    Missing = [""]
    while len(Missing) > 0:
        print("HDFS")
        Missing = CheckMissingHDFS(lastS)  
        putHDFS(lastS, Missing) 
        Run += 1
        if(Run > 10):
            print("###ERROR### Impossible to copy on HDFS all files after 10 attempts. Still Missing: %d files" % len(Missing))
            return -1
    return 0
コード例 #2
0
def main():

    if (os.path.isfile("creating.txt") == False):
        print("missing creating file")
        exit(0)
    city_config = open("creating.txt", "r")
    city = city_config.readline().strip()
    city_config.close()

    d = dc.readConfigFile(city)
    gv.init()
    dc.assingVariables(city)

    bookings_df = formatBookings(d)
    print("CVZ, all data queried")
    parkings_df = createParkingsFromBookings(bookings_df)
    print("CVZ, all parkings are created")
    printCSV(parkings_df, d)
    print("CVZ, input files generated")
コード例 #3
0
def main():

    gv.init()
    assingVariables()
    global city
    city = gv.city

    #zones = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,17,19,21,23,25,27, 29, 31, 33, 35, 37, 39, 41]
    outputPath = os.path.abspath("../output/")
    files = os.listdir(outputPath)
    zones = []
    for file in files:
        file = file.split(".")
        if len(file) > 1 and file[1] == "txt":
            file = file[0].split("_")
            if (len(file) == 4):
                zones.append(int(file[3]))

    zones = sorted(zones)
    '''
    TORINO
    '''

    to_not_load = loadOptmizationResults(zones)
    df = to_not_load
    df["zones"] = df["zones"].astype(int)
    index_df = df.groupby("zones")["newAvgValue"].min()
    index_df = index_df.reset_index()
    index_df = index_df.set_index(['zones', 'newAvgValue'])
    df = df.set_index(['zones', 'newAvgValue'])
    df = df[df.index.isin(index_df.index)]
    df["Nsteps"] = df.Nsteps.replace(0, np.nan)
    df = df.dropna(how='any', axis=0)
    df = df.iloc[0:len(df):3]
    df["SolutionIDforcingCaselle"] = df.apply(
        lambda x: addCaselle(x["solutionIDList"]), axis=1)
    #
    df[["solutionIDList"
        ]].to_csv("../../Torino_sim3.0/input/bestSolTorino.csv")
    df[["SolutionIDforcingCaselle"
        ]].to_csv("../../Torino_sim3.0/input/bestSolTorinoWithCaselle.csv")
コード例 #4
0
## p = /home/michele/Scrivania/MySym ##

import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import warnings
warnings.filterwarnings("ignore")
import json
import pprint
import codecs

import Simulator.Globals.SupportFunctions as sf
import Simulator.Globals.GlobalVar as gv

city = sys.argv[1]
gv.init()
sf.assingVariables(city)

pp = pprint.PrettyPrinter(indent=4)

def downloadCityBoarders():
    r = requests.get("http://www.car2go.com/api/v2.1/locations?oauth_consumer_key="+gv.car2goKey+"&format=json", verify=False)

    #url = "http://www.car2go.com/api/v2.1/locations?oauth_consumer_key="+gv.car2goKey+"&format=json"       
    #thepage = urllib.request.urlopen(url)
    #charset_encoding = thepage.info().get_content_charset()
    #str_json = thepage.read().decode(charset_encoding)

    citiedBorders = json.loads(r.content.decode("utf-8"))
    citiedBorders = citiedBorders["location"]
コード例 #5
0
ファイル: Simulator_DG.py プロジェクト: michelelt/sim3.0
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
コード例 #6
0
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
コード例 #7
0
def main():

    if (os.path.isfile("creating.txt") == False):
        print("missing creating file")
        exit(0)

    city_config = open("creating.txt", "r")
    city = city_config.readline().strip()
    city_config.close()
    gv.init()
    dc.assingVariables(city)

    #ZoneID_Zone = {}
    ZoneDistances = {}
    ZoneNumCars = [0 for i in range(0, gv.NColumns * gv.NRows + 1)]

    DictPlates = pickle.load(
        open(
            "../input/" + gv.city + "_" + gv.provider +
            "_plates_appeareance_obj.pkl", "rb"))
    for plate in DictPlates:
        CellIndex = sf.coordinates_to_index(DictPlates[plate].coordinates)
        ZoneNumCars[CellIndex] += 1

    k = 0
    ZoneCars = {}
    for i in range(0, gv.NColumns * gv.NRows + 1):
        ZoneDistances[i] = {}

        CarVector = []
        for j in range(0, ZoneNumCars[i]):

            CarVector.append(Car(gv.provider, k))
            k += 1
        ZoneCars[i] = CarVector

    pickle.dump(
        ZoneCars,
        open("../input/" + gv.city + "_" + gv.provider + "_ZoneCars.p", "wb"))

    print("CPZD, ", "Col:", gv.NColumns, "Row,", gv.NRows)

    for i in range(0, gv.NColumns * gv.NRows + 1):
        for j in range(i, gv.NColumns * gv.NRows):
            de, dh = EvalDistance(i, j)
            RealDistance = dh
            if de not in ZoneDistances[i]:
                ZoneDistances[i][de] = Distance(RealDistance)
            ZoneDistances[i][de].appendZone(j)
            if (i != j):
                if (de not in ZoneDistances[j]):
                    ZoneDistances[j][de] = Distance(RealDistance)
                ZoneDistances[j][de].appendZone(i)

    for i in range(0, len(ZoneDistances)):

        ZoneDistances[i] = sorted(ZoneDistances[i].items(),
                                  key=operator.itemgetter(0))

    pickle.dump(
        ZoneDistances,
        open("../input/" + gv.city + "_" + gv.provider + "_ZoneDistances.p",
             "wb"))

    print("CPZD, End")
    return
コード例 #8
0
def main():

    if (os.path.isfile("creating.txt") == False):
        print("missing creating file")
        exit(0)

    city_config = open("creating.txt", "r")
    city = city_config.readline().strip()
    city_config.close()

    gv.init()
    dc.assingVariables(city)

    collection = "enjoy_PermanentParkings"
    if gv.provider == "car2go":
        collection = "PermanentParkings"

    collection_parkings = dc.setup_mongodb(collection)
    parkings = collection_parkings.find({
        "city": gv.city,
        "init_time": {
            "$gt": gv.initDate,
            "$lt": gv.finalDate
        }
    })
    parkigns2 = parkings.clone()

    if gv.fleetSize.isnumeric():
        realFleetSize = int(gv.fleetSize)
    else:
        df = pd.DataFrame(list(parkigns2))
        df["DailyDate"] = df["init_date"].apply(
            lambda x: x.strftime("%Y/%m/%d"))
        carsPerDay = df.groupby('DailyDate')["plate"].nunique()
        #        carsPerDay = pd.Series(11)
        if gv.fleetSize == "mean":
            realFleetSize = int(round(carsPerDay.mean()))

        elif gv.fleetSize == "max":
            realFleetSize = int(carsPerDay.max())

        elif gv.fleetSize == "min":
            realFleetSize = int(carsPerDay.min())

        else:
            print("CCID, ERROR wrong fleetSize Value: " + str(gv.fleetSize))
            return -1

    parkigns2.close()
    print("CCID, realFleetSize:", str(realFleetSize), "gv.fleetSize:",
          str(gv.fleetSize))

    currentFleetSize = 0
    for val in parkings:
        coords = val['loc']['coordinates']
        lon1 = coords[0]
        lat1 = coords[1]
        #d = haversine(baselon, baselat, lon1, lat1)
        # if( checkPerimeter(lat1, lon1) or
        #    (provider == "car2go" and checkCasellePerimeter(lat1, lon1)) and
        #     currentFleetSize <= FleetSize):
        if currentFleetSize < realFleetSize:
            if sf.checkPerimeter(lat1, lon1):
                if val['plate'] not in dict_plates:
                    dict_plates[val['plate']] = PlatesData(
                        val['init_time'], val["loc"]['coordinates'])
                    currentFleetSize += 1

                else:
                    if dict_plates[val['plate']].timestamp >= val[
                            'init_time']:  #se non erano in ordine nel dataset iniziale
                        dict_plates[val['plate']] = PlatesData(
                            val['init_time'], val["loc"]['coordinates'])
            else:
                print("problem")
        else:
            print("CCID, len dict_plates:" + str(len(dict_plates)) +
                  "FleetSize:" + str(realFleetSize))
            print("CCID, cfs", currentFleetSize)
            break

    print("CCID", "Seen cars:", len(dict_plates))
    print("cfs", currentFleetSize)

    with open(
            "../input/" + gv.city + "_" + gv.provider +
            "_plates_appeareance_obj.pkl", 'wb') as handle:
        pickle.dump(dict_plates, handle)

    print("CCID, col:", gv.NColumns, " row:", gv.NRows)
    print("CCID, End\n")
    return
コード例 #9
0
def main():

    if (os.path.isfile("creating.txt") == False):
        print("missing creating file")
        exit(0)

    city_config = open("creating.txt", "r")
    city = city_config.readline().strip()
    city_config.close()

    gv.init()
    dc.assingVariables(city)

    collection = "enjoy_PermanentBookings"
    if (gv.provider == "car2go"):
        collection = "PermanentBookings"
    enjoy_bookings = dc.setup_mongodb(collection)

    bookings = enjoy_bookings.find({
        "city": gv.city,
        "init_time": {
            "$gt": gv.initDate,
            "$lt": gv.finalDate
        }
    })

    # geolocator = Nominatim()
    # location = geolocator.geocode("Torino")
    #baselon = location.longitude
    #baselat = location.latitude

    i = 0  #id del booking, numero progressivo

    NumEvents = 0
    NumEventsFiltered = 0
    Discarted = 0
    for booking in bookings:
        initt = booking['init_time']
        finalt = booking['final_time']
        duration = finalt - initt
        coords = booking['origin_destination']['coordinates']
        lon1 = coords[0][0]
        lat1 = coords[0][1]
        lon2 = coords[1][0]
        lat2 = coords[1][1]
        #d = haversine(baselon, baselat, lon2, lat2)
        #d1 = haversine(baselon, baselat, lon1, lat1)
        d2 = sf.haversine(lon1, lat1, lon2, lat2)

        if (duration > 120 and duration < 3600 and d2 > 500):
            # if( sf.checkPerimeter(lat1, lon1) and sfcheckPerimeter(lat2, lon2) or
            #    (provider == "car2go" and  ((checkPerimeter(lat1, lon1) and checkCasellePerimeter(lat2, lon2)) or  (checkCasellePerimeter(lat1, lon1) and checkPerimeter(lat2, lon2))))):
            if sf.checkPerimeter(lat1, lon1) and sf.checkPerimeter(lat2, lon2):
                NumEvents += 1
                id_events[i] = [
                    booking['init_time'], booking['final_time'],
                    EventBook(i, "s",
                              booking["origin_destination"]['coordinates'][0]),
                    EventBook(i, "e",
                              booking["origin_destination"]['coordinates'][1])
                ]
                if booking['init_time'] not in dict_bookings:
                    dict_bookings[booking['init_time']] = []
                dict_bookings[booking['init_time']].append([i, "s"])
                if booking['final_time'] not in dict_bookings:
                    dict_bookings[booking['final_time']] = []
                dict_bookings[booking['final_time']].append([i, "e"])
                i = i + 1

                if (i < 1000):
                    if booking['init_time'] not in dict_bookings_short:
                        dict_bookings_short[booking['init_time']] = []
                    dict_bookings_short[booking['init_time']].append(
                        EventBook(
                            i, "s",
                            booking["origin_destination"]['coordinates'][0]))
                    if booking['final_time'] not in dict_bookings_short:
                        dict_bookings_short[booking['final_time']] = []
                    dict_bookings_short[booking['final_time']].append(
                        EventBook(
                            i, "e",
                            booking["origin_destination"]['coordinates'][1]))
        else:
            Discarted += 1

    with open(
            "../events/" + gv.city + "_" + gv.provider + "_dict_bookings.pkl",
            'wb') as handle:
        pickle.dump(dict_bookings, handle)

    with open("../events/" + gv.city + "_" + gv.provider + "_id_events.pkl",
              'wb') as handle:
        pickle.dump(id_events, handle)

    print("End Pickles")

    print("Start")
    to_delete = []
    EventDeleted = 0
    for stamp in dict_bookings:
        startbooking = 0
        for event in dict_bookings[stamp]:
            if (event[1] == "s"): startbooking += 1

        if (startbooking > 30):
            EventDeleted += startbooking
            to_delete.append(stamp)

    for stamp in to_delete:
        events_to_delete = []
        for event in dict_bookings[stamp]:
            if (event[1] == "s"): events_to_delete.append(event[0])

        for event in events_to_delete:
            InitTime = id_events[event][0]
            FinalTime = id_events[event][1]
            InitInd = dict_bookings[InitTime].index([event, "s"])
            FinalInd = dict_bookings[FinalTime].index([event, "e"])

            del dict_bookings[InitTime][InitInd]
            del dict_bookings[FinalTime][FinalInd]

        if (len(dict_bookings[stamp]) == 0):
            del dict_bookings[stamp]

    for stamp in dict_bookings:
        for i in range(0, len(dict_bookings[stamp])):
            NumEventsFiltered += 1
            EventT = dict_bookings[stamp][i]
            if (EventT[1] == "s"):
                dict_bookings[stamp][i] = id_events[EventT[0]][2]
            else:
                dict_bookings[stamp][i] = id_events[EventT[0]][3]

    print("CPE, Num Events Filtered + Event deleted:",
          NumEventsFiltered + EventDeleted)
    print("CPE, Num Events Filtered:", NumEventsFiltered)
    print("CPE, Event Deleted:", EventDeleted)
    print("CPE, Dicarded:", Discarted)

    ordered_dict_booking = collections.OrderedDict(
        sorted(dict_bookings.items()))
    ordered_dict_booking_short = collections.OrderedDict(
        sorted(dict_bookings_short.items()))

    with open(
            "../events/" + gv.city + "_" + gv.provider +
            "_sorted_dict_events_obj.pkl", 'wb') as handle:
        pickle.dump(ordered_dict_booking, handle)

    with open(
            "../events/" + gv.city + "_" + gv.provider +
            "_sorted_dict_events_obj_short.pkl", 'wb') as handle:
        pickle.dump(ordered_dict_booking_short, handle)

    print("CPE, end\n")