示例#1
0
def printMatrix_CS(CS_placement):
    '''
    function which prints the zones with a charging station
    '''
    print("R:", gv.NRows, "C:", gv.NColumns)
    matrix = {}
    for row in range(gv.NRows):
        matrix[row] = {}
        for col in range(gv.NColumns, 0, -1):
            matrix[row][col] = " "

    for id in zonesMetrics["id"]:
        couple = sf.zoneIDtoMatrixCoordinates(id)
        matrix[couple[2]][couple[1]] = '\''
    k = 0
    for cs in CS_placement:
        couple = sf.zoneIDtoMatrixCoordinates(cs)
        matrix[couple[2]][couple[1]] = str(k)
        k += 1

    print("\t")
    for i in range(gv.NColumns + 1, 0, -1):
        if i == gv.NColumns + 1:
            print("\t", end='')
        else:
            print(str(i) + "\t", end='')
    print()

    for row in range(gv.NRows - 1, -1, -1):
        print(str(row) + "\t", end='')
        for col in range(gv.NColumns, 0, -1):
            print(matrix[row][col] + "\t", end='')
        print()
    return
示例#2
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
示例#3
0
def exploreNeighbours():
    '''
    extract a random index to chose which is the station to move
    :return: logical coordinates of NESW zones, NESW direction, logical index of the zone
    '''
    xynew = {}
    direction = {}
    myindex = np.random.randint(len(RechargingStation_Zones), size=1)[0]
    ID = RechargingStation_Zones[myindex]
    print('extracted ID', ID)
    retv = sf.zoneIDtoMatrixCoordinates(ID)
    xy = [retv[1], retv[2]]

    max = 2  ## increment to have more choicee
    i = 0
    for dst in range(1, max):
        xynew[i] = [xy[0] - dst, xy[1]]
        direction[i] = [0, xy[0] - dst, xy[1]]

        xynew[i + 1] = [xy[0], xy[1] - dst]
        direction[i + 1] = [1, xy[0], xy[1] - dst]

        xynew[i + 2] = [xy[0] + dst, xy[1]]
        direction[i + 2] = [2, xy[0] + dst, xy[1]]

        xynew[i + 3] = [xy[0], xy[1] + dst]
        direction[i + 3] = [3, xy[0], xy[1] + dst]
        i = i + 4
    # print("expNeigh - dir", direction)
    # print()
    return xynew, direction, myindex
示例#4
0
def printMatrix(xynew):
    '''
    function which prints the zones with a charging station
    '''
    print("R:", gv.NRows, "C:", gv.NColumns)
    matrix = {}
    for row in range(gv.NRows):
        matrix[row] = {}
        for col in range(gv.NColumns, 0, -1):
            matrix[row][col] = " "

    for id in zonesMetrics["id"]:
        couple = sf.zoneIDtoMatrixCoordinates(id)
        matrix[couple[2]][couple[1]] = '\''
    for k in xynew.keys():
        couple = xynew[k]
        matrix[couple[1]][couple[0]] = str(k)

    print("\t")
    for i in range(gv.NColumns + 1, 0, -1):
        if i == gv.NColumns + 1:
            print("\t", end='')
        else:
            print(str(i) + "\t", end='')
    print()

    for row in range(gv.NRows - 1, -1, -1):
        print(str(row) + "\t", end='')
        for col in range(gv.NColumns, 0, -1):
            print(matrix[row][col] + "\t", end='')
        print()
    return
示例#5
0
    def wrapperIDtoCoords(zoneID, coord):
        out = sf.zoneIDtoCoordinates(zoneID)

        if coord == "lon":
            return out[0]
        else:
            return out[1]
示例#6
0
def queryBookings(typology):
    books_cursor = queryToCollection(typology)
    if (books_cursor == "err from cursor" or books_cursor.count() == 0):
        return "err"
    else:
        #            print books_cursor.count()
        #            bookings_df = pd.DataFrame(columns = pd.Series(books_cursor.next()).index)
        bookings_df = pd.DataFrame(list(books_cursor))

        bookings_df['duration_dr'] = bookings_df.driving.apply(
            lambda x: float(x['duration'] / 60))
        bookings_df['distance_dr'] = bookings_df.driving.apply(
            lambda x: x['distance'])
        bookings_df = bookings_df.drop('driving', 1)

        bookings_df['type'] = bookings_df.origin_destination.apply(
            lambda x: x['type'])
        bookings_df['coordinates'] = bookings_df.origin_destination.apply(
            lambda x: x['coordinates'])
        bookings_df = bookings_df.drop('origin_destination', 1)

        bookings_df['start'] = bookings_df.coordinates.apply(lambda x: x[0])
        bookings_df['end'] = bookings_df.coordinates.apply(lambda x: x[1])
        bookings_df = bookings_df.drop('coordinates', 1)

        bookings_df['start_lon'] = bookings_df.start.apply(
            lambda x: float(x[0]))
        bookings_df['start_lat'] = bookings_df.start.apply(
            lambda x: float(x[1]))
        bookings_df = bookings_df.drop('start', 1)

        bookings_df['end_lon'] = bookings_df.end.apply(lambda x: float(x[0]))
        bookings_df['end_lat'] = bookings_df.end.apply(lambda x: float(x[1]))
        bookings_df = bookings_df.drop('end', 1)

        bookings_df['distance'] = bookings_df.apply(
            lambda x: sf.haversine(float(x['start_lon']), float(x[
                'start_lat']), float(x['end_lon']), float(x['end_lat'])),
            axis=1)

        bookings_df[
            'duration'] = bookings_df.final_date - bookings_df.init_date
        bookings_df['duration'] = bookings_df['duration'].apply(
            lambda x: x.days * 24 * 60 + x.seconds / 60)

        bookings_df['duration_pt'] = bookings_df.public_transport.apply(
            lambda x: x['duration'])
        bookings_df['distance_pt'] = bookings_df.public_transport.apply(
            lambda x: x['distance'])
        bookings_df['arrival_date_pt'] = bookings_df.public_transport.apply(
            lambda x: x['arrival_date'])
        bookings_df['arrival_time_pt'] = bookings_df.public_transport.apply(
            lambda x: x['arrival_time'])
        bookings_df = bookings_df.drop('public_transport', 1)

        bookings_df = bookings_df[bookings_df["start_lon"] <= 7.8]

        return bookings_df
示例#7
0
def ParkCar(ChargingStationsZones,Zone_DistanceFromZones,ZoneID_Zone, Event, BookedCar, \
            TankThreshold, WalkingTreshold, BestEffort, pThreshold):

    ToRecharge = False
    Recharged = False
    Distance = -1
    Iter = 0
    p = random.SystemRandom().random()

    BookingEndPosition = sf.coordinates_to_index(Event.coordinates)
    Lvl = BookedCar.getBatteryLvl()

    #BestEffort is True if the policy is FreeFloating or Hybrid  -> parkings only in CS when hitted
    #In case plug the car only if the user is willing to plug [based on p extracted and pThreshold]
    #pThreshold == 0 -> park only Needed
    #pThreshold == 100 -> hybrid
    if (BestEffort and BookingEndPosition in ChargingStationsZones
            and pThreshold >= p):
        ZoneI = ZoneID_Zone[BookingEndPosition]
        Recharged = ZoneI.getParkingAtRechargingStations(BookedCar)
        if (Recharged):
            return ToRecharge, Recharged, Distance, ZoneI.ID, 1, p

    #If battery lvl below the TankThreshold look for a car in a charging station
    if (Lvl < TankThreshold):
        Iter = 0
        ToRecharge = True
        for DistanceI in Zone_DistanceFromChargingZones[BookingEndPosition]:
            Iter += 1
            Distance = DistanceI[1].getDistance()
            if (Distance > WalkingTreshold): break
            #Since the final destination of the user is unkown, and to avoid selecting allways the same zone
            #Get zones at the same distance in a random order
            RandomZones = DistanceI[1].getRandomZones()

            for ZoneI_ID in RandomZones:
                ZoneI = ZoneID_Zone[ZoneI_ID]
                if (ZoneI.ID in ChargingStationsZones):
                    Recharged = ZoneI.getParkingAtRechargingStations(BookedCar)
                    if (Recharged):
                        return ToRecharge, Recharged, Distance, ZoneI.ID, Iter, p

    #Park at any parking in the end booking zone
    #End here if the car did not need to be charged
    #No plug is available
    for DistanceI in Zone_DistanceFromZones[BookingEndPosition]:
        RandomZones = DistanceI[1].getZones()
        for ZoneI_ID in RandomZones:
            ZoneI = ZoneID_Zone[ZoneI_ID]
            ZoneI.getAnyParking(BookedCar)
            return ToRecharge, Recharged, 0, ZoneI.ID, 1, p
示例#8
0
def EvalDistance(i, j):
    Xi = i % gv.NColumns
    Yi = int(i / gv.NColumns)

    Xj = j % gv.NColumns
    Yj = int(j / gv.NColumns)

    CentalLoni = (Xi + 0.5) * gv.ShiftLon + gv.minLon
    CentalLati = (Yi + 0.5) * gv.ShiftLat + gv.minLat
    CentalLonj = (Xj + 0.5) * gv.ShiftLon + gv.minLon
    CentalLatj = (Yj + 0.5) * gv.ShiftLat + gv.minLat

    dh = sf.haversine(CentalLoni, CentalLati, CentalLonj, CentalLatj)

    de = sqrt(pow((Xi - Xj), 2) + pow((Yi - Yj), 2))

    return de, dh
示例#9
0
def SearchNearestBestCar(Zone_DistanceFromZones, ZoneID_Zone, Event, Stamp):

    SelectedCar = ""
    Distance = -1
    Iter = 0

    BookingStartingPosition = sf.coordinates_to_index(Event.coordinates)
    for DistanceI in Zone_DistanceFromZones[BookingStartingPosition]:
        Iter += 1
        RandomZones = DistanceI[1].getRandomZones()
        for ZoneI_ID in RandomZones:
            ZoneI = ZoneID_Zone[ZoneI_ID]
            SelectedCar = SearchAvailableCar(ZoneI, Stamp)
            if (SelectedCar != ""):
                Distance = DistanceI[1].getDistance()
                return SelectedCar, Distance, ZoneI.ID, Iter

    print("Error NO cars avaiable")
    exit(0)
    return -1, -1
示例#10
0
def toStringBooking(Stamp,Event,ToRecharge,Recharged,NearestCar,Distance,Iter,\
                     Discharge,TripDistance,FileID,extractedP,ZoneID,OccupiedCS,
                     Recharge, StartRecharge,StartRechageLVL, PoleID):

    Lvl = NearestCar.getBatteryLvl()
    ID = NearestCar.getID()
    ZoneC = sf.zoneIDtoCoordinates(ZoneID)
    EventCoords = Event.coordinates

    outputString = Event.type + ";"
    outputString += str(ToRecharge) + ";"
    outputString += str(Recharged) + ";"
    outputString += "%d;" % ID
    outputString += "%.2f;" % Lvl
    outputString += "%d;" % Distance
    outputString += "%d;" % Iter
    outputString += "%.2f;" % Recharge
    outputString += "%d;" % StartRecharge
    outputString += "%.2f;" % StartRechageLVL
    outputString += "%d;" % Stamp
    outputString += "[%.6f,%.6f];" % (EventCoords[0], EventCoords[1])
    outputString += "[%.6f,%.6f];" % (ZoneC[0], ZoneC[1])
    if (Discharge == "nan"):
        outputString += "nan;"
        outputString += "nan;"
    else:
        outputString += "%.2f;" % Discharge
        outputString += "%.2f;" % TripDistance
    outputString += "%s;" % FileID
    if (extractedP == "nan"):
        outputString += "nan;"
    else:
        outputString += "%.2f;" % extractedP
    outputString += "%d;" % ZoneID
    outputString += "%d;" % OccupiedCS
    outputString += "%d\n" % PoleID

    return outputString
示例#11
0
def queryToCollection(typology):

    city = "Torino"
    provider = "car2go"
    initdate = "2017-9-5T00:00:00"
    finaldate = "2017-11-2T00:00:00"

    collections = {}
    d1 = {}
    d1["enjoy"] = "enjoy_PermanentBookings"
    d1["car2go"] = "PermanentBookings"
    collections["bookings"] = d1

    d2 = {}
    d2["enjoy"] = "enjoy_PermanentParkings"
    d2["car2go"] = "PermanentParkings"
    collections["parkings"] = d2

    collection = collections[typology][provider]

    initdate = int(
        time.mktime(
            datetime.datetime.strptime(initdate,
                                       "%Y-%m-%dT%H:%M:%S").timetuple()))
    finaldate = int(
        time.mktime(
            datetime.datetime.strptime(finaldate,
                                       "%Y-%m-%dT%H:%M:%S").timetuple()))
    mongo_collection = sf.setup_mongodb(collection)
    data = mongo_collection.find({
        "city": city,
        "init_time": {
            "$gt": initdate,
            "$lt": finaldate
        }
    })
    return data
def main():

    #TO AVOID: "OSError: [Errno 24] Too many open files"
    #bashCommand = "ulimit -n 2048"
    #process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    #process.communicate()
    ###

    BestEffort_list = [True, False]
    algorithm_list = ["max-parking"]
    # numberOfStations_list = []
    maxZones = numeberOfZones(gv.city)
    # for i in range(2                       , round(maxZones*0.05) + 1, 1):  numberOfStations_list.append(i)
    # for i in range(round(maxZones*0.05) + 2, round(maxZones*0.1)  + 1, 2): numberOfStations_list.append(i)
    # for i in range(round(maxZones*0.1)  + 2, round(maxZones*0.3)  + 1, 5): numberOfStations_list.append(i)
    myConfig = organizeCS(int(maxZones * 0.07) * 4)

    # AvaiableChargingStations_list = [4] #PALINE PER ZONA
    pThresholds = [50]
    tankThresholds_list = [25]
    walkingTreshold = 1000000
    upperTankThreshold = [100]
    randomInitLvl = False
    kwh_list = [2]

    # print("Total Simulations: %d"%(len(AvaiableChargingStations_list) * len(numberOfStations_list)*
    #                                len(algorithm_list)* len(kwh_list) *
    #                                (2*len(tankThresholds_list) + len(pThresholds) )
    #                                )
    #       )

    print("#START Loading#")
    aglobal = datetime.datetime.now()
    nsimulations = 0
    a = datetime.datetime.now()
    Stamps_Events = pickle.load(
        open(
            "../events/" + city + "_" + gv.provider +
            "_sorted_dict_events_obj.pkl", "rb"))
    print("Stamp Events len: ", len(Stamps_Events))
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Loading Events: " + str(int(c)) + "\n")

    a = datetime.datetime.now()
    DistancesFrom_Zone_Ordered = pickle.load(
        open("../input/" + city + "_" + gv.provider + "_ZoneDistances.p",
             "rb"))
    print("DistancesFrom_Zone_Ordered len: ", len(DistancesFrom_Zone_Ordered))
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Loading Zones: " + str(int(c)) + "\n")

    a = datetime.datetime.now()
    ZoneCars = pickle.load(
        open("../input/" + city + "_" + gv.provider + "_ZoneCars.p", "rb"))
    print("ZoneCars: ", len(ZoneCars))
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Loading Cars: " + str(int(c)) + "\n")

    batcmd = 'ssh bigdatadb hadoop fs -ls /user/cocca/Simulator/output/'  #Solo per controllare il ticket

    lastS = -1
    try:
        output = subprocess.check_output(batcmd,
                                         stderr=subprocess.STDOUT,
                                         shell=True)
        if (len(str(output)) < 5): lastS = 0
        else: lastS = int(str(output).split(" ")[1]) + 1
    except subprocess.CalledProcessError as e:
        output = e.output
        if ("Kerberos" in str(output)):
            print("ERROR: Kerberos Token not present. \n \
            Please log in the bigdata server to request kerberos Token")
            exit(-1)
    print("#END Loading#\n")

    print("Ouput in output/Simulation_%d" % lastS)
    print("Ouput in output_analysis/Simulation_%d\n" % lastS)

    os.system(
        'ssh bigdatadb mkdir /data/03/Carsharing_data/output/Simulation_%d' %
        lastS)
    os.system(
        'ssh bigdatadb mkdir /data/03/Carsharing_data/output_analysis/Simulation_%d'
        % lastS)

    if not os.path.exists("../output/Simulation_" + str(lastS)):
        os.makedirs("../output/Simulation_" + str(lastS))
        os.makedirs("../output_analysis/Simulation_" + str(lastS))
        # os.makedirs("../output_analysis/Simulation_"+str(lastx1S)+"/fig")

    SimulationConfigFile = "../output_analysis/Simulation_%d/configuration.txt" % lastS
    fout = open(SimulationConfigFile, "w")

    fout.write("Simulation Configuration: \n")

    str_out = "BestEffort: "
    for val in BestEffort_list:
        str_out += str(val) + " "
    str_out += "\n"

    # str_out += "AvaiableChargingStations: "
    # for val in AvaiableChargingStations_list:
    #     str_out+= str(val)+" "
    # str_out+= "\n"

    str_out += "Algorithm: "
    for val in algorithm_list:
        str_out += str(val) + " "
    str_out += "\n"

    # str_out += "NumberOfStations: "
    # for val in numberOfStations_list:
    #     str_out+= str(val)+" "
    # str_out+= "\n"

    str_out += "TankThresholds: "
    for val in tankThresholds_list:
        str_out += str(val) + " "
    str_out += "\n"

    str_out += "UpperTankThresholds: "
    for val in upperTankThreshold:
        str_out += str(val) + " "
    str_out += "\n"

    str_out += "pThresholds: "
    for val in pThresholds:
        str_out += str(val * 100) + " "
    str_out += "\n"

    fout.write(str_out)
    fout.close()

    os.system('cp %s ../output/Simulation_%d/configuration.txt' %
              (SimulationConfigFile, lastS))
    os.system(
        'cat %s | ssh bigdatadb hdfs dfs -put -f - Simulator/output/Simulation_%s/configuration.txt'
        % (SimulationConfigFile, lastS))
    #
    jobs = []

    for BestEffort in BestEffort_list:
        for algorithm in algorithm_list:
            print("Running simulations:")
            for tankThreshold in tankThresholds_list:
                for utt in upperTankThreshold:
                    for pt in pThresholds:
                        for kwh in kwh_list:

                            for z in ZoneCars.keys():
                                if len(ZoneCars[z]) > 0:
                                    for i in range(len(ZoneCars[z])):
                                        ZoneCars[z][i].setRechKwh(kwh)

                            for zones_index in myConfig.keys():
                                listOfCond = myConfig[zones_index]

                                if len(listOfCond) == 0: continue

                                for configElement in listOfCond:
                                    numberOfStations = zones_index
                                    AvaiableChargingStations = configElement[
                                        'acs']
                                    if configElement['acs_min'] > 0:
                                        AvaiableChargingStations_last = configElement[
                                            'acs_min']
                                    else:
                                        AvaiableChargingStations_last = -1

                                    if sf.validSimulation(
                                            BestEffort, tankThreshold, utt,
                                            pt) == False:
                                        continue

                                    RechargingStation_Zones = loadRecharing(
                                        algorithm, numberOfStations, city)
                                    # print (algorithm, RechargingStation_Zones)

                                    p = Process(
                                        target=RunSim,
                                        args=(BestEffort,
                                              algorithm.replace(
                                                  "_", "-"), algorithm,
                                              AvaiableChargingStations,
                                              tankThreshold, walkingTreshold,
                                              ZoneCars,
                                              RechargingStation_Zones,
                                              Stamps_Events,
                                              DistancesFrom_Zone_Ordered,
                                              lastS, utt, pt, kwh,
                                              randomInitLvl, None, -1, None,
                                              city,
                                              AvaiableChargingStations_last))

                                    nsimulations += 1

                                    jobs.append(p)
                                    p.start()

                                    if (len(jobs) > 120):
                                        time.sleep(
                                            .1
                                        )  #only to print after other prints
                                        print("\nWaiting for %d simulations" %
                                              len(jobs))
                                        with click.progressbar(
                                                jobs, length=len(jobs)) as bar:
                                            for proc in bar:
                                                proc.join()
                                        jobs.clear()

            print("")

    time.sleep(.1)  #only to print after other prints
    print("\nWaiting for %d simulations" % len(jobs))
    with click.progressbar(jobs, length=len(jobs)) as bar:
        for proc in bar:
            proc.join()

    b = datetime.datetime.now()
    c = (b - aglobal).total_seconds()

    if (check_and_run_missing(lastS, Stamps_Events, DistancesFrom_Zone_Ordered,
                              ZoneCars, city) < 0):
        exit(-1)

    print("Run %d Simulations took %d seconds" % (nsimulations, c))

    print("\nStart Spark Analysis")
    a = datetime.datetime.now()

    os.system(
        'scp ../Analysis/Spark_Analyzer.py bigdatadb:/tmp/CarSharing_Spark_Analyzer.py'
    )
    os.system(
        'ssh bigdatadb export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/'
    )
    os.system(
        'ssh bigdatadb spark2-submit --master yarn --deploy-mode client /tmp/CarSharing_Spark_Analyzer.py %d'
        % lastS)
    os.system(
        'scp bigdatadb:/data/03/Carsharing_data/output_analysis/Simulation_%d/out_analysis.txt ../output_analysis/Simulation_%d/'
        % (lastS, lastS))

    os.system(
        'scp bigdatadb:/tmp/Carsharing_Output/out_analysis.txt ../output_analysis/Simulation_%d/'
        % lastS)
    os.system(
        'cat ../output_analysis/Simulation_%d/out_analysis.txt | ssh bigdatadb hdfs dfs -put -f - Simulator/output/Simulation_%s/out_analysis.txt'
        % (lastS, lastS))

    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    #
    # print("Analyze data with Spark took %d seconds" %(c))
    # print("\nPlot graphs")
    # a = datetime.datetime.now()
    # os.system('python3 ../Analysis/plot_heatmap.py %d'%lastS)
    # b = datetime.datetime.now()
    # c = (b - a).total_seconds()
    # print("Plot graphs took %d seconds" %(c))
    #
    print("Ouput in output/Simulation_%d" % lastS)
    print("Ouput in output_analysis/Simulation_%d" % lastS)

    print(nsimulations)

    return
示例#13
0
def formatBookings():
    collection = "enjoy_PermanentBookings"
    if gv.provider == "car2go":
        collection = "PermanentBookings"
    enjoy_bookings = sf.setup_mongodb(collection)

    print("***********************")
    print("city", gv.city)
    print(
        "initDate ",
        datetime.datetime.fromtimestamp(int(
            gv.initDate)).strftime('%Y-%m-%d %H:%M:%S'))
    print(
        "fianlDate",
        datetime.datetime.fromtimestamp(int(
            gv.finalDate)).strftime('%Y-%m-%d %H:%M:%S'))
    print("***********************")

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

    bookings_df = pd.DataFrame(list(bookings))

    if gv.city == "Vancouver":
        bookings_df["init_time"] = bookings_df["init_time"].sub(25200)
        bookings_df["final_time"] = bookings_df["final_time"].sub(25200)

    bookings_df.to_pickle('../input/bookings_' + gv.city)

    bookings_df[
        "duration"] = bookings_df["final_time"] - bookings_df["init_time"]
    bookings_df["duration"] = bookings_df["duration"].astype(int)
    bookings_df = bookings_df.drop('driving', 1)

    bookings_df['type'] = bookings_df.origin_destination.apply(
        lambda x: x['type'])
    bookings_df['coordinates'] = bookings_df.origin_destination.apply(
        lambda x: x['coordinates'])
    bookings_df = bookings_df.drop('origin_destination', 1)

    bookings_df['end'] = bookings_df.coordinates.apply(lambda x: x[0])
    bookings_df['start'] = bookings_df.coordinates.apply(lambda x: x[1])
    bookings_df = bookings_df.drop('coordinates', 1)

    bookings_df['start_lon'] = bookings_df.start.apply(lambda x: float(x[0]))
    bookings_df['start_lat'] = bookings_df.start.apply(lambda x: float(x[1]))
    bookings_df = bookings_df.drop('start', 1)

    bookings_df['end_lon'] = bookings_df.end.apply(lambda x: float(x[0]))
    bookings_df['end_lat'] = bookings_df.end.apply(lambda x: float(x[1]))
    bookings_df = bookings_df.drop('end', 1)

    bookings_df['distance'] = bookings_df.apply(
        lambda x: sf.haversine(float(x['start_lon']), float(x['start_lat']),
                               float(x['end_lon']), float(x['end_lat'])),
        axis=1)

    bookings_df = bookings_df[bookings_df["distance"] >= 700]
    bookings_df = bookings_df[bookings_df["duration"] >= 120]
    bookings_df = bookings_df[bookings_df["duration"] <= 3600]

    if gv.city == "Torino":
        bookings_df = bookings_df[bookings_df["start_lon"] <= 7.8]

    return bookings_df
示例#14
0
def main(par_numberOfStations):
    '''
    :param par_numberOfStations: #CS placed in the city
    :return: the optimal CS placement
    '''
    iniTimeSim = datetime.datetime.now()

    walkingTreshold = 1000000
    city = "Torino"
    zones = sf.numberOfZones(city)
    algorithm = "max-parking"
    numberOfStations = 18
    tankThreshold = 25
    AvaiableChargingStations = 4
    BestEffort = True
    pThreshold = 1
    randomInitLvl = False

    global tested_solution
    tested_solution = {}

    batcmd = 'ssh bigdatadb hadoop fs -ls /user/cocca/Simulator/output/'  #Solo per controllare il ticket
    lastS = -1
    try:
        output = subprocess.check_output(batcmd,
                                         stderr=subprocess.STDOUT,
                                         shell=True)
        if (len(str(output)) < 5): lastS = 0
        else: lastS = int(str(output).split(" ")[1]) + 1
    except subprocess.CalledProcessError as e:
        output = e.output
        if ("Kerberos" in str(output)):
            print("ERROR: Kerberos Token not present. \n \
            Please log in the bigdata server to request kerberos Token")
            exit(-1)
    '''
    Trace, city config and CS placement upload
    '''
    a = datetime.datetime.now()
    Stamps_Events = pickle.load(
        open(
            "../events/" + city + "_" + gv.provider +
            "_sorted_dict_events_obj.pkl", "rb"))
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Load Events: " + str(int(c)))

    a = datetime.datetime.now()
    global DistancesFrom_Zone_Ordered
    DistancesFrom_Zone_Ordered = pickle.load(
        open("../input/" + city + "_" + gv.provider + "_ZoneDistances.p",
             "rb"))
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Load Zones: " + str(int(c)))
    ZoneCars = pickle.load(
        open("../input/" + city + "_" + gv.provider + "_ZoneCars.p", "rb"))

    a = datetime.datetime.now()
    global RechargingStation_Zones
    RechargingStation_Zones = sf.loadRecharing(algorithm, numberOfStations,
                                               city)
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Load Recharging: " + str(int(c)))
    '''Entra qui solo se non carico abbastanza stazioni, cioè mai'''
    while len(RechargingStation_Zones) < numberOfStations:
        rn = np.random.randint(gv.NColumns * gv.NRows, size=1)[0]
        if (rn not in RechargingStation_Zones):
            RechargingStation_Zones.append(rn)

    print('Start', sorted(RechargingStation_Zones))

    results = ""
    step = 0
    fit_impr_perc = 100
    manager = multiprocessing.Manager()
    NNI_counter = 0
    fitness_best = 1e12

    global followDirection
    followDirection = False
    '''
    optmization
    '''
    while step <= 10000 and fit_impr_perc >= 0.1:
        print('while', sorted(RechargingStation_Zones))
        return_dict = manager.dict()

        # if step % 100 == 0:
        print("Iteration #", step)

        if followDirection == True:
            '''
            If the algorithm has a direction to follow, follow that direction
            '''
            # print ("In FD")
            xynew, direction = exploreDirection(directionToFollow)
            if len(xynew) == 0:
                # print (step, "in Len")
                ## On the board, No exploring in depth
                xynew, direction, myindex = exploreNeighbours()
        else:
            '''
            If there is not any direction, explore some random direction point with the NSEW neighbors
            '''
            xynew, direction, myindex = exploreNeighbours()
        # print("MyIndex:", myindex)

        RechargingStation_Zones_new = {}
        '''
        xynew contains the ID + logical coordniates of the station which is going to be changed in the CSplacement
        explore xynew to create a new CSP
        check the if the solution was not already tested
        '''
        for k in range(0, len(xynew)):
            IDn = MatrixCoordinatesToID(xynew[k][0], xynew[k][1])
            if IDn in list(
                    zonesMetrics.id) and IDn not in RechargingStation_Zones:
                tmp = RechargingStation_Zones.copy()
                tmp[myindex] = IDn
                if solution_already_exists(tmp, tested_solution) == True:
                    # print('solution tested', "-".join(str(e) for e in sorted(tmp)) )
                    RechargingStation_Zones_new[k] = []

                else:
                    RechargingStation_Zones_new[k] = tmp
                    my_sol = sorted(RechargingStation_Zones_new[k])
                    key = "-".join([str(e) for e in my_sol])
                    tested_solution[key] = True
                    # print('not used', key)

        Sol2Test = {}
        sol_index = 0
        for k in RechargingStation_Zones_new.keys():
            if len(RechargingStation_Zones_new[k]) > 0:
                Sol2Test[sol_index] = RechargingStation_Zones_new[k]
                sol_index += 1
        print('before', len(RechargingStation_Zones_new))
        RechargingStation_Zones_new = Sol2Test.copy()
        print('after', len(RechargingStation_Zones_new))

        if len(RechargingStation_Zones_new) > 50:
            print('ouch')
            fdebug = open('debug.txt', 'w')
            fdebug.write('len xwnew: ' + str(len(xynew)) + "\n")
            fdebug.write('myindex: ' + str(myindex) + "\n")
            fdebug.write('ID: ' + str(RechargingStation_Zones[myindex]) + "\n")
            fdebug.write('Rech: ' +
                         ' '.join(str(e)
                                  for e in RechargingStation_Zones) + "\n")
            fdebug.write('Direction' + str(directionToFollow) + "\n")
            fdebug.write('-----\n')
            for k in RechargingStation_Zones_new.keys():
                fdebug.write(' '.join(
                    str(e) for e in RechargingStation_Zones_new[k]) + '\n')
            fdebug.close()
            input()

        if len(RechargingStation_Zones_new) == 0:
            print('NNI')
            print('FD', followDirection)
            followDirection = False
            NNI_counter += 1
            if NNI_counter == 36: break
            continue
        NNI_counter = 0

        jobs = []
        start_sim_time = time.time()
        # RechargingStation_Zones_new2 = {}
        # RechargingStation_Zones_new2[0] = RechargingStation_Zones_new[0]
        for i in RechargingStation_Zones_new:
            print("RSZ_new", i, RechargingStation_Zones_new[i])
            p = Process(
                target=RunSim,
                args=(BestEffort, algorithm.replace("_", "-"), algorithm,
                      AvaiableChargingStations, tankThreshold, walkingTreshold,
                      ZoneCars, RechargingStation_Zones_new[i], Stamps_Events,
                      DistancesFrom_Zone_Ordered, lastS, pThreshold, 2,
                      randomInitLvl, return_dict, i, direction[i], city))

            # p = Process(target=RunSim_Toy,args = (
            #                       RechargingStation_Zones_new[i],
            #                       i,
            #                       direction[i],
            #                       return_dict,
            #                       ))

            jobs.append(p)
            p.start()

        for proc in jobs:
            proc.join()

        end_sim_time = time.time() - start_sim_time
        print('Time for %d sim: %d s' %
              (len(RechargingStation_Zones_new), end_sim_time))
        print()

        followDirection = False
        '''
        # Results analysis
        '''

        for val in return_dict.values():

            new_results = val
            '''
            Optimality condition
            if results (previous solution) is empity or the optimality condtion is true
            A direction to follow has been found,
            saving solutions
            '''
            #
            fitness_new = cost_function(new_results)
            if results == "" or fitness_new < fitness_best:
                if results == "":
                    fit_impr_perc = 100
                    fitness_old = 1e8
                else:
                    fit_impr_perc = (fitness_old -
                                     fitness_new) * 100 / fitness_old

                followDirection = True

                directionToFollow = new_results["Direction"]
                RechargingStation_Zones = RechargingStation_Zones_new[int(
                    new_results["ProcessID"])].copy()

                print_new_solution(results, new_results, fit_impr_perc,
                                   fitness_old, fitness_new,
                                   RechargingStation_Zones, step)
                fout = open(
                    "../output/best_solutions_" + city + "_" +
                    str(numberOfStations) + "_" + str(refer_time) + ".txt",
                    "a")
                write_new_solution(fout, results, new_results, fit_impr_perc,
                                   fitness_old, fitness_new,
                                   RechargingStation_Zones, step, end_sim_time)

                results = new_results.copy()
                fitness_best = fitness_new

        step += 1
        print()
    print(step + NNI_counter, "Iteration done in",
          (datetime.datetime.now() - iniTimeSim) / 60, "minutes")
示例#15
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")
示例#16
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
示例#17
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
示例#18
0
def main():

    #TO AVOID: "OSError: [Errno 24] Too many open files"
    #bashCommand = "ulimit -n 2048"
    #process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    #process.communicate()
    ###
    '''
    RUNNING CONFIGURATION
    '''

    BestEffort_list = [True]
    algorithm_list = ["max-parking"]
    numberOfStations_list = []
    maxZones = numeberOfZones(gv.city)
    numberOfStations_list = []
    for i in range(2, round(maxZones * 0.05) + 1, 1):
        numberOfStations_list.append(i)
    for i in range(round(maxZones * 0.05) + 2, round(maxZones * 0.1) + 1, 2):
        numberOfStations_list.append(i)
    for i in range(round(maxZones * 0.1) + 2, round(maxZones * 0.3) + 1, 5):
        numberOfStations_list.append(i)
    for i in range(2, round(maxZones * 0.3), 2):
        numberOfStations_list.append(i)
    numberOfStations_list = [
        5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75
    ]
    AvaiableChargingStations_list = [4]  #PALINE PER ZONA
    pThresholds = [0.5]
    tankThresholds_list = [25]
    walkingTreshold = 1000000
    randomInitLvl = False
    kwh_list = [2]
    gamma_list = [0, 0.25, 0.5, 0.75, 1]
    # gamma_list = [1, 5, 10]

    print("#START Loading#")
    aglobal = datetime.datetime.now()
    nsimulations = 0
    a = datetime.datetime.now()
    Stamps_Events = pickle.load(
        open(
            "../events/" + city + "_" + gv.provider +
            "_sorted_dict_events_obj.pkl", "rb"))
    print("Stamp Events len: ", len(Stamps_Events))
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Loading Events: " + str(int(c)) + "\n")

    a = datetime.datetime.now()
    DistancesFrom_Zone_Ordered = pickle.load(
        open("../input/" + city + "_" + gv.provider + "_ZoneDistances.p",
             "rb"))
    print("DistancesFrom_Zone_Ordered len: ", len(DistancesFrom_Zone_Ordered))
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Loading Zones: " + str(int(c)) + "\n")

    a = datetime.datetime.now()
    ZoneCars = pickle.load(
        open("../input/" + city + "_" + gv.provider + "_ZoneCars.p", "rb"))
    print("ZoneCars: ", len(ZoneCars))
    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    print("End Loading Cars: " + str(int(c)) + "\n")

    batcmd = 'ssh bigdatadb hadoop fs -ls /user/cocca/Simulator/output/'  #Solo per controllare il ticket
    lastS = -1
    try:
        output = subprocess.check_output(batcmd,
                                         stderr=subprocess.STDOUT,
                                         shell=True)
        if (len(str(output)) < 5): lastS = 0
        else: lastS = int(str(output).split(" ")[1]) + 1
    except subprocess.CalledProcessError as e:
        output = e.output
        if ("Kerberos" in str(output)):
            print("ERROR: Kerberos Token not present. \n \
           Please log in the bigdata server to request kerberos Token")
            exit(-1)
    lastS = 57
    print("#END Loading#\n")

    print("Ouput in output/Simulation_%d" % lastS)
    print("Ouput in output_analysis/Simulation_%d\n" % lastS)

    os.system(
        'ssh bigdatadb mkdir /data/03/Carsharing_data/output/Simulation_%d' %
        lastS)
    os.system(
        'ssh bigdatadb mkdir /data/03/Carsharing_data/output_analysis/Simulation_%d'
        % lastS)

    if not os.path.exists("../output/Simulation_" + str(lastS)):
        os.makedirs("../output/Simulation_" + str(lastS))
        os.makedirs("../output_analysis/Simulation_" + str(lastS))
        os.makedirs("../output_analysis/Simulation_" + str(lastS) + "/fig")

    SimulationConfigFile = "../output_analysis/Simulation_%d/configuration.txt" % lastS
    fout = open(SimulationConfigFile, "w")

    fout.write("Simulation Configuration: \n")

    str_out = "BestEffort: "
    for val in BestEffort_list:
        str_out += str(val) + " "
    str_out += "\n"

    str_out += "AvaiableChargingStations: "
    for val in AvaiableChargingStations_list:
        str_out += str(val) + " "
    str_out += "\n"

    str_out += "Algorithm: "
    for val in algorithm_list:
        str_out += str(val) + " "
    str_out += "\n"

    str_out += "NumberOfStations: "
    for val in numberOfStations_list:
        str_out += str(val) + " "
    str_out += "\n"

    str_out += "TankThresholds: "
    for val in tankThresholds_list:
        str_out += str(val) + " "
    str_out += "\n"

    str_out += "pThresholds: "
    for val in pThresholds:
        str_out += str(val * 100) + " "
    str_out += "\n"

    str_out += "gamma: "
    for val in gamma_list:
        str_out += str(val * 100) + " "
    str_out += "\n"

    fout.write(str_out)
    fout.close()

    os.system('cp %s ../output/Simulation_%d/configuration.txt' %
              (SimulationConfigFile, lastS))
    os.system(
        'cat %s | ssh bigdatadb hdfs dfs -put -f - Simulator/output/Simulation_%s/configuration.txt'
        % (SimulationConfigFile, lastS))

    jobs = []

    configurations = sf.createListConfig(BestEffort_list,
                                         AvaiableChargingStations_list,
                                         algorithm_list, numberOfStations_list,
                                         tankThresholds_list, pThresholds,
                                         kwh_list, gamma_list)

    print("Total Simulations: %d" % (len(configurations)))

    for config in configurations:

        # for z in ZoneCars.keys():
        #   if len(ZoneCars[z]) > 0:
        #       for i in range (len(ZoneCars[z])):
        #           ZoneCars[z][i].setRechKwh(config['kwh'])
        #           config['gamma'] = 666
        #           ZoneCars[z][i].setGamma(config['gamma

        RechargingStation_Zones = loadRecharing(config["Algorithm"],
                                                config["numberOfStations"],
                                                city)
        p = Process(target=RunSim,
                    args=(config["BestEffort"], config["Algorithm"].replace(
                        "_", "-"), config["Algorithm"],
                          config["AvaiableChargingStations"],
                          config["tankThreshold"], walkingTreshold, ZoneCars,
                          RechargingStation_Zones, Stamps_Events,
                          DistancesFrom_Zone_Ordered, lastS, config["pt"],
                          config['kwh'], config['gamma'], randomInitLvl, None,
                          -1, None, city))

        nsimulations += 1

        jobs.append(p)
        p.start()

        return

        if (len(jobs) > 120):
            time.sleep(.1)  #only to print after other prints
            print("\nWaiting for %d simulations" % len(jobs))
            with click.progressbar(jobs, length=len(jobs)) as bar:
                for proc in bar:
                    proc.join()
            jobs.clear()

    time.sleep(.1)  #only to print after other prints
    print("\nWaiting for %d simulations" % len(jobs))
    with click.progressbar(jobs, length=len(jobs)) as bar:
        for proc in bar:
            proc.join()

    b = datetime.datetime.now()
    c = (b - aglobal).total_seconds()

    if (check_and_run_missing(lastS, Stamps_Events, DistancesFrom_Zone_Ordered,
                              ZoneCars, city) < 0):
        exit(-1)

    print("Run %d Simulations took %d seconds" % (nsimulations, c))

    print("\nStart Spark Analysis")
    a = datetime.datetime.now()

    os.system(
        'scp ../Analysis/Spark_Analyzer.py bigdatadb:/tmp/CarSharing_Spark_Analyzer.py'
    )
    os.system(
        'ssh bigdatadb export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/'
    )
    os.system(
        'ssh bigdatadb spark2-submit --master yarn --deploy-mode client /tmp/CarSharing_Spark_Analyzer.py %d'
        % lastS)
    os.system(
        'scp bigdatadb:/data/03/Carsharing_data/output_analysis/Simulation_%d/out_analysis.txt ../output_analysis/Simulation_%d/'
        % (lastS, lastS))

    os.system(
        'scp bigdatadb:/tmp/Carsharing_Output/out_analysis.txt ../output_analysis/Simulation_%d/'
        % lastS)
    os.system(
        'cat ../output_analysis/Simulation_%d/out_analysis.txt | ssh bigdatadb hdfs dfs -put -f - Simulator/output/Simulation_%s/out_analysis.txt'
        % (lastS, lastS))

    b = datetime.datetime.now()
    c = (b - a).total_seconds()
    #
    # print("Analyze data with Spark took %d seconds" %(c))
    # print("\nPlot graphs")
    # a = datetime.datetime.now()
    # os.system('python3 ../Analysis/plot_heatmap.py %d'%lastS)
    # b = datetime.datetime.now()
    # c = (b - a).total_seconds()
    # print("Plot graphs took %d seconds" %(c))
    #
    print("Ouput in output/Simulation_%d" % lastS)
    print("Ouput in output_analysis/Simulation_%d" % lastS)

    return
示例#19
0
def formatBookings(d):

    bookings_df = ""

    if (os.path.isfile('../input/bookings_' + d["city"]) == False):
        collection = "enjoy_PermanentBookings"
        if (d["provider"] == "car2go"):
            collection = "PermanentBookings"
        enjoy_bookings = dc.setup_mongodb(collection)

        print("***********************")
        print("city", d["city"])
        print(
            "initdate ",
            datetime.fromtimestamp(int(
                d["initdate"])).strftime('%Y-%m-%d %H:%M:%S'))
        print(
            "fianldate",
            datetime.fromtimestamp(int(
                d["finaldate"])).strftime('%Y-%m-%d %H:%M:%S'))
        print("***********************")

        bookings = enjoy_bookings.find({
            "city": d["city"],
            "init_time": {
                "$gt": d["initdate"],
                "$lt": d["finaldate"]
            }
        })

        bookings_df = pd.DataFrame(list(bookings))

        if ("Europe" not in d["timezone"]):
            #convert from their time zone in our time zone
            bookings_df['init_time'] = list(
                map(AbroadStamptoLocal, bookings_df["timezone"],
                    bookings_df['init_time']))
            bookings_df['final_time'] = list(
                map(AbroadStamptoLocal, bookings_df["timezone"],
                    bookings_df['final_time']))

        bookings_df.to_pickle('../input/bookings_' + d["city"])

    else:
        print("read")
        bookings_df = pd.read_pickle('../input/bookings_' + d["city"])

    bookings_df[
        "duration"] = bookings_df["final_time"] - bookings_df["init_time"]
    bookings_df["duration"] = bookings_df["duration"].astype(int)
    bookings_df = bookings_df.drop('driving', 1)

    bookings_df['type'] = bookings_df.origin_destination.apply(
        lambda x: x['type'])
    bookings_df['coordinates'] = bookings_df.origin_destination.apply(
        lambda x: x['coordinates'])
    bookings_df = bookings_df.drop('origin_destination', 1)

    bookings_df['end'] = bookings_df.coordinates.apply(lambda x: x[0])
    bookings_df['start'] = bookings_df.coordinates.apply(lambda x: x[1])
    bookings_df = bookings_df.drop('coordinates', 1)

    bookings_df['start_lon'] = bookings_df.start.apply(lambda x: float(x[0]))
    bookings_df['start_lat'] = bookings_df.start.apply(lambda x: float(x[1]))
    bookings_df = bookings_df.drop('start', 1)

    bookings_df['end_lon'] = bookings_df.end.apply(lambda x: float(x[0]))
    bookings_df['end_lat'] = bookings_df.end.apply(lambda x: float(x[1]))
    bookings_df = bookings_df.drop('end', 1)

    bookings_df['distance'] = bookings_df.apply(
        lambda x: sf.haversine(float(x['start_lon']), float(x['start_lat']),
                               float(x['end_lon']), float(x['end_lat'])),
        axis=1)

    bookings_df = bookings_df[bookings_df["distance"] >= 700]
    bookings_df = bookings_df[bookings_df["duration"] >= 120]
    bookings_df = bookings_df[bookings_df["duration"] <= 3600]

    if d["city"] == "Torino":
        bookings_df = bookings_df[bookings_df["start_lon"] <= 7.8]

    return bookings_df
示例#20
0
# #!/usr/bin/env python3
# # -*- coding: utf-8 -*-
import pandas as pd
import json
import ast
import numpy as np

import sys
import os
p = os.path.abspath('..')
sys.path.append(p + "/")
sys.path.append(p + "/Simulator/")
import Simulator.Globals.SupportFunctions as sf
import Simulator.Globals.GlobalVar as gv
gv.init()
sf.assingVariables()

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)
示例#21
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
示例#22
0
import datetime as datetime
import pickle
import multiprocessing
from multiprocessing import Process
import subprocess

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

import pprint
pp = pprint.PrettyPrinter()

import cProfile
city = 'Torino'
gv.init()
sf.assingVariables(city)

walkingTreshold = 1000000
city = "Torino"
zones = sf.numberOfZones(city)
algorithm = "max-parking"
numberOfStations = 20
tankThreshold = 25
AvaiableChargingStations = 4
BestEffort = False
pThreshold = 0.5
randomInitLvl = False
return_dict = {}

ZoneCars = pickle.load( open( "../input/"+ city + "_" + gv.provider +"_ZoneCars.p", "rb" ) )
DistancesFrom_Zone_Ordered = pickle.load( open( "../input/"+ city + "_" + gv.provider + "_ZoneDistances.p", "rb" ) )
示例#23
0
sys.path.append(p + "/Simulator/")

from Simulator.Simulator_LV import *
import datetime as datetime
import pickle
import multiprocessing
from multiprocessing import Process
import subprocess

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

import cProfile
city = 'Torino'
gv.init()
sf.assingVariables(city)

print('SERVER')

walkingTreshold = 1000000
city = "Torino"
zones = sf.numberOfZones(city)
algorithm = "max-parking"
numberOfStations = 18
tankThreshold = 25
AvaiableChargingStations = 4
BestEffort = True
pThreshold = 1
randomInitLvl = False
return_dict = {}
示例#24
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
示例#25
0
def CheckMissing(lastS):
    output_directory = "../output/Simulation_" + str(lastS) + "/"  
    fin = open(output_directory + "configuration.txt")
    
    BestEffort_list = []
    algorithm_list = []
    AvaiableChargingStations_list = []
    numberOfStations_list = []
    tankThresholds_list = []
    walkingTreshold = 1000000
    upperTankThreshold_list = []
    pThresholds_list = []

    for line in fin:
        ls = line.split(":")
        if(ls[0] == "BestEffort"): fill_bool_field(BestEffort_list, ls[1])
        if(ls[0] == "Algorithm"): fill_str_field(algorithm_list, ls[1])
        if(ls[0] == "NumberOfStations"): fill_int_field(numberOfStations_list, ls[1])
        if(ls[0] == "AvaiableChargingStations"): fill_int_field(AvaiableChargingStations_list, ls[1])
        if(ls[0] == "TankThresholds"): fill_int_field(tankThresholds_list, ls[1])
        if(ls[0] == "upperTankThreshold"): fill_int_field(upperTankThreshold_list, ls[1])
        if(ls[0] == "pThreshold"): fill_int_field(pThresholds_list, ls[1])
    
    '''
    Simulation Configuration: 
    BestEffort: False True 
    AvaiableChargingStations: 2 3 4 5 6 7 8 
    Algorithm: max-parking max-time avg-time 
    NumberOfStations: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 
    TankThresholds: -1 5 10 15 20 25 50     
    '''
    
    files = []
    
    batcmd = "ssh bigdatadb ls /data/03/Carsharing_data/output/Simulation_" + str(lastS) + "/"
    try:
        output = subprocess.check_output(batcmd, stderr=subprocess.STDOUT, shell=True)
        
        files = str(output).strip()[2:-3].split("\\n")

    except subprocess.CalledProcessError as e:
        output = e.output
        exit(-1)
        

    '''BestEffort_list = [False, True]
    algorithm_list = ["max-parking", "max-time","avg-time"]
    numberOfStations_list = [i for i in range(2,42,2)]#42
    AvaiableChargingStations_list = [2,3,4,5,6,7,8]
    tankThresholds_list = [-1,5,10,15,20,25,50]
    walkingTreshold = 1000000#int(sys.argv[4]) # in [m]'''
    
    Total = 0
    
    #if("car2go_FreeFloating_avg-time_10_2_-1_1000000.txt" not in files): print("asds")
    
    Missing = []
    for BestEffort in BestEffort_list:
        for AvaiableChargingStations in AvaiableChargingStations_list:
            for algorithm in algorithm_list:
                for numberOfStations in numberOfStations_list:
                    for tankThreshold in tankThresholds_list:
                        for utt in upperTankThreshold_list:
                            for pt in pThresholds_list:
                                if sf.validSimulation(BestEffort, tankThreshold, utt, pt) == False:
                                    continue

                                policy, fileID, fname = foutname(BestEffort, algorithm, AvaiableChargingStations,
                                                                 numberOfStations, tankThreshold, walkingTreshold, utt, pt)

                                Total += 1
                                if(fname not in files):
                                    Missing.append((BestEffort, AvaiableChargingStations, algorithm, numberOfStations, tankThreshold, fname, utt, pt))
                                else:
                                    files.remove(fname)
                        
                            
    print("Total Simulations: %d" % Total)
    print("MISSING: %d" % len(Missing))

    return Missing
示例#26
0
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"]

    d = {}
def main():

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

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

    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):
        #if(ZoneNumCars[i]>0): print(i,ZoneNumCars[i])
        ZoneDistances[i] = {}

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

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

    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)
        # AppendCaselle(i,ZoneDistances)

    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"))
    #pickle.dump( ZoneID_Zone, open( "../input/"+provider+"_ZoneID_Zone.p", "wb" ) )

    print("CPZD, End")
    return