Exemplo n.º 1
0
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" ) )
Stamps_Events = pickle.load( open( "../events/"+ city + "_" + gv.provider + "_sorted_dict_events_obj.pkl", "rb" ) )

# for k in DistancesFrom_Zone_Ordered.keys():
#       print('key', k)
Exemplo n.º 2
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")