Ejemplo n.º 1
0
def optimizedSimulation(initialState, fileLoc, fileName, gridSize):
    plotFigures = False
    carsPos = np.zeros(shape=(initialState.cars.length(), 2))
    eventsPos = []
    eventsStartTime = []
    eventsEndTime = []
    for d, k in enumerate(initialState.cars.getUnCommitedKeys()):
        carsPos[d, :] = deepcopy(initialState.cars.getObject(k).position)
    # get opened event locations from state -
    for k in initialState.events.getUnCommitedKeys():
        eventsPos.append(deepcopy(initialState.events.getObject(k).position))
        eventsStartTime.append(
            deepcopy(initialState.events.getObject(k).startTime))
        eventsEndTime.append(deepcopy(
            initialState.events.getObject(k).endTime))

    m, obj = runMaxFlowOpt(0, carsPos, np.array(eventsPos),
                           np.array(eventsStartTime), np.array(eventsEndTime),
                           initialState.closeReward,
                           initialState.cancelPenalty,
                           initialState.openedNotCommitedPenalty, 0)
    dataOut = plotResults(m, carsPos, np.array(eventsPos),
                          np.array(eventsStartTime), np.array(eventsEndTime),
                          plotFigures, fileLoc, fileName, gridSize)

    dataOut['cost'] = -obj.getValue()
    return dataOut
def optimizedSimulation(initialState, numFigure):
    carsPos = np.zeros(shape=(initialState.cars.length(), 2))
    eventsPos = []
    eventsStartTime = []
    eventsEndTime = []
    for d, k in enumerate(initialState.cars.getUnCommitedKeys()):
        carsPos[d, :] = deepcopy(initialState.cars.getObject(k).position)
    # get opened event locations from state -
    for k in initialState.events.getUnCommitedKeys():
        eventsPos.append(deepcopy(initialState.events.getObject(k).position))
        eventsStartTime.append(
            deepcopy(initialState.events.getObject(k).startTime))
        eventsEndTime.append(deepcopy(
            initialState.events.getObject(k).endTime))

    m, obj = runMaxFlowOpt(0, carsPos, np.array(eventsPos),
                           np.array(eventsStartTime), np.array(eventsEndTime),
                           initialState.closeReward,
                           initialState.cancelPenalty,
                           initialState.openedNotCommitedPenalty, 0)
    plotResults(m, carsPos, np.array(eventsPos), np.array(eventsStartTime),
                np.array(eventsEndTime), numFigure)
Ejemplo n.º 3
0
def anticipatorySimulation(initState,
                           nStochastic,
                           gs,
                           tPred,
                           eTimeWindow,
                           lam,
                           shouldPrint=False):
    """
    this function is the anticipatory simulation
    :param initState: initial state of the system (cars and events)
    :param nStochastic: number of stochastic runs
    :param gs: grid size (X*Y)
    :param tPred: number of time steps for prediction
    :param eTimeWindow: number of time steps an event is opened
    :param lam: rate of events for poission distribution
    :param shouldPrint: True/False if the code should print log
    :return:
    """
    isGoal = False
    current = initState
    while not isGoal:
        currentTime = current.time
        optionalExpectedCost = []
        optionalStatesList = []
        optionalTotalCost = []
        optionalActualCost = []
        stochasticEventsDict = createStochasticEvents(nStochastic, gs, 1,
                                                      1 + tPred, lam,
                                                      eTimeWindow)
        for i, optionalState in enumerate(descendantGenerator(current)):
            optionalStatesList.append(optionalState)
            # initialize variables for stochastic optimization
            carsPos = np.zeros(shape=(optionalState.cars.length(), 2))
            currentEventsPos = []
            currentEventStartTime = []
            currentEventsEndTime = []
            stochasticCost = np.zeros(shape=(nStochastic, 1))
            # get car locations from state -
            for d, k in enumerate(optionalState.cars.getUnCommitedKeys()):
                carsPos[d, :] = deepcopy(
                    optionalState.cars.getObject(k).position)
            # get opened event locations from state -
            for k in optionalState.events.getUnCommitedKeys():
                if optionalState.events.getObject(
                        k).status == Status.OPENED_NOT_COMMITED:
                    currentEventsPos.append(
                        deepcopy(optionalState.events.getObject(k).position))
                    # assume that event start time is current time for deterministic runs and the time left for event
                    # is the time left - current time.
                    # the deterministic run is from (currentTime+1) therefore need to subtract that value and not CurrentTime
                    currentEventStartTime.append(
                        deepcopy(optionalState.events.getObject(k).startTime) -
                        (currentTime + 1))
                    currentEventsEndTime.append(
                        deepcopy(optionalState.events.getObject(k).endTime) -
                        (currentTime + 1))
            # run deterministic optimization for stochastic events -
            for j in range(len(stochasticEventsDict)):
                if len(stochasticEventsDict[j]['eventsPos']) + len(
                        currentEventsPos) > 0:
                    # there are events to be tested in deterministic optimization:
                    eventsPos = deepcopy(currentEventsPos)
                    eventsStartTime = deepcopy(currentEventStartTime)
                    eventsEndTime = deepcopy(currentEventsEndTime)
                    temp = [
                        eventsPos.append(e)
                        for e in stochasticEventsDict[j]['eventsPos']
                    ]
                    temp = [
                        eventsStartTime.append(e[0])
                        for e in stochasticEventsDict[j]['eventsTimeWindow']
                    ]
                    temp = [
                        eventsEndTime.append(e[1])
                        for e in stochasticEventsDict[j]['eventsTimeWindow']
                    ]
                    eventsPos = np.array(eventsPos).reshape(len(eventsPos), 2)
                    eventsStartTime = np.array(eventsStartTime)
                    eventsEndTime = np.array(eventsEndTime)
                    stime = time.clock()
                    m, obj = runMaxFlowOpt(
                        0, carsPos, eventsPos, eventsStartTime, eventsEndTime,
                        optionalState.closeReward, optionalState.cancelPenalty,
                        optionalState.openedNotCommitedPenalty, 0)
                    etime = time.clock()
                    runTime = etime - stime
                    # if shouldPrint:
                    #     print("finished MIO for run:"+str(j+1)+"/"+str(len(stochasticEventsDict)))
                    #     print("run time of MIO is:"+str(runTime))
                    #     print("cost of MIO is:"+str(-obj.getValue()))
                    stochasticCost[j] = -obj.getValue()
                else:
                    stochasticCost[j] = 0
            # calculate expected cost of all stochastic runs for this spesific optional State
            if shouldPrint:
                print("stochastic cost of optional run is:")
                print(np.transpose(stochasticCost))
            expectedCost = np.mean(stochasticCost)
            if shouldPrint:
                print('finished optional state # ' + str(i))
                print('state cost is: ' + str(optionalState.gval) +
                      ', expected cost is: ' + str(expectedCost) +
                      ' , commited cost is:' + str(optionalState.optionalGval))
            optionalExpectedCost.append(expectedCost)
            optionalActualCost.append(optionalState.gval)
            # optional total cost includes the actual cost of movement + optional cost for commited events + expected cost of future events
            optionalTotalCost.append(expectedCost + optionalState.gval +
                                     optionalState.optionalGval)
        chosenTotalCost = np.min(np.array(optionalTotalCost))
        chosenIndex = np.argmin(np.array(optionalTotalCost))
        if shouldPrint:
            print('t:' + str(currentTime) + ', chosen action:' +
                  str(chosenIndex) + ' , chosen cost is: ' +
                  str(chosenTotalCost))
        chosenState = optionalStatesList[chosenIndex]
        current = chosenState
        # check if this state is a goal or not-
        if current.goalCheck():
            isGoal = True
            print('finished run - total cost is:' + str(current.gval))
        # dump logs
        dataInRun = postAnalysis(current.path())
        # Anticipatory output:
        with open(
                'SimAnticipatoryMioResults_' + str(currentTime + 1) + 'time_' +
                str(current.events.length()) + 'numEvents_' +
                str(current.cars.length()) + 'numCars_' + str(lam) + 'lam_' +
                str(gs) + 'gridSize.p', 'wb') as out:
            pickle.dump(
                {
                    'pathresults': current.path(),
                    'time': dataInRun['timeVector'],
                    'gs': gs,
                    'OpenedEvents': dataInRun['openedEvents'],
                    'closedEvents': dataInRun['closedEvents'],
                    'canceledEvents': dataInRun['canceledEvents'],
                    'allEvents': dataInRun['allEvents'],
                    'cost': current.gval
                }, out)

    return current.path()
Ejemplo n.º 4
0
def anticipatorySimulation(initState, nStochastic, gs, tPred, eTimeWindow, simStartTime, probabilityMatrix, shouldPrint=False):
    """
    this function is the anticipatory simulation
    :param initState: initial state of the system (cars and events)
    :param nStochastic: number of stochastic runs
    :param gs: grid size (X*Y)
    :param tPred: number of time steps for prediction
    :param eTimeWindow: number of time steps an event is opened
    :param lam: rate of events for poission distribution
    :param shouldPrint: True/False if the code should print log
    :return:
    """
    isGoal  = False
    current = initState
    optionalActions        = np.array([[0, 0], [0, 1], [0, -1], [1, 0], [-1, 0]]).astype(int)
    optionalActionsToIndex = {(0,  0): 0,
                              (0,  1): 1,
                              (0, -1): 2,
                              (1,  0): 3,
                              (-1, 0): 4}
    while not isGoal:
        currentTime = current.time
        stochasticEventsDict = createStochasticEvents(simStartTime, nStochastic, 1, 1 + tPred, probabilityMatrix, eTimeWindow, currentTime)
        nCars                = current.cars.length()
        carOrder = np.array(range(nCars))
        np.random.shuffle(carOrder)
        carsMovement = np.zeros(shape=(nCars, 2))
        for carIndex in carOrder:
            totalExpectedCost = []
            expectedCost = []
            tempMovement = deepcopy(carsMovement)
            for optionalAction in optionalActions:
                tempMovement[carIndex, :] = optionalAction
                tempState = spesificDescendantGenerator(current, tempMovement.astype(int))
                stochasticCost = np.zeros(shape=(nStochastic, 1))
                # initialize variables for stochastic optimization
                carsPos = np.zeros(shape=(tempState.cars.length(), 2))
                currentEventsPos = []
                currentEventStartTime = []
                currentEventsEndTime = []
                # get car locations from state -
                for d, k in enumerate(tempState.cars.getUnCommitedKeys()):
                    carsPos[d, :] = deepcopy(tempState.cars.getObject(k).position)
                # get opened event locations from state -
                for k in tempState.events.getUnCommitedKeys():
                    if tempState.events.getObject(k).status == Status.OPENED_NOT_COMMITED:
                        currentEventsPos.append(deepcopy(tempState.events.getObject(k).position))
                        # assume that event start time is current time for deterministic runs and the time left for event
                        # is the time left - current time.
                        # the deterministic run is from (currentTime+1) therefore need to subtract that value and not CurrentTime
                        currentEventStartTime.append(
                            deepcopy(tempState.events.getObject(k).startTime) - (currentTime + 1))
                        currentEventsEndTime.append(
                            deepcopy(tempState.events.getObject(k).endTime) - (currentTime + 1))
                # run deterministic optimization for stochastic events -
                for j in range(len(stochasticEventsDict)):
                    if len(stochasticEventsDict[j]['eventsPos']) + len(currentEventsPos) > 0:
                        # there are events to be tested in deterministic optimization:
                        eventsPos = deepcopy(currentEventsPos)
                        eventsStartTime = deepcopy(currentEventStartTime)
                        eventsEndTime = deepcopy(currentEventsEndTime)
                        temp = [eventsPos.append(e) for e in stochasticEventsDict[j]['eventsPos']]
                        temp = [eventsStartTime.append(e[0]) for e in stochasticEventsDict[j]['eventsTimeWindow']]
                        temp = [eventsEndTime.append(e[1]) for e in stochasticEventsDict[j]['eventsTimeWindow']]
                        eventsPos = np.array(eventsPos).reshape(len(eventsPos), 2)
                        eventsStartTime = np.array(eventsStartTime)
                        eventsEndTime = np.array(eventsEndTime)
                        stime = time.process_time()
                        m, obj = runMaxFlowOpt(0, carsPos, eventsPos, eventsStartTime, eventsEndTime,
                                               tempState.closeReward, tempState.cancelPenalty,
                                               tempState.openedNotCommitedPenalty, 0)
                        etime = time.process_time()
                        runTime = etime - stime
                        try:
                            stochasticCost[j] = -obj.getValue()
                        except:
                            print('failed cuz of gurobi!')
                    else:
                        stochasticCost[j] = 0
                # calculate expected cost of all stochastic runs for this spesific optional State
                if shouldPrint:
                    print("stochastic cost of optional run is:")
                    print(np.transpose(stochasticCost))
                expectedCost.append(np.mean(stochasticCost))
                # optional total cost includes the actual cost of movement + optional cost for commited events + expected cost of future events
                totalExpectedCost.append(expectedCost[-1] + tempState.gval + tempState.optionalGval)
                if shouldPrint:
                    print('state cost is: ' + str(tempState.gval) + ', expected cost is: ' + str(expectedCost[-1]) +
                          ' , commited cost is:' + str(tempState.optionalGval))
            chosenActionIndex = np.argmin(np.array(totalExpectedCost))
            chosenAction = optionalActions[chosenActionIndex]
            carsMovement[carIndex, :] = chosenAction

        newState = spesificDescendantGenerator(current, carsMovement.astype(int))
        current = newState
        print('t:' + str(currentTime) + ' , chosen cost is: ' + str(current.gval))
        # check if this state is a goal or not-
        if current.goalCheck():
            isGoal = True
            print('finished run - total cost is:' + str(current.gval))
        # dump logs
        dataInRun = postAnalysis(current.path())
        # Anticipatory output:
        # with open('SimAnticipatoryMioResults_' + str(currentTime + 1) + 'time_' + str(
        #         current.events.length()) + 'numEvents_' + str(current.cars.length()) + 'numCars_uberData.p',
        #           'wb') as out:
        #     pickle.dump({'pathresults': current.path(),
        #                  'time': dataInRun['timeVector'],
        #                  'gs': gs,
        #                  'OpenedEvents': dataInRun['openedEvents'],
        #                  'closedEvents': dataInRun['closedEvents'],
        #                  'canceledEvents': dataInRun['canceledEvents'],
        #                  'allEvents': dataInRun['allEvents'],
        #                  'stochasticResults': optionalTotalCost,
        #                  'stochasticEventsDict': stochasticEventsDict,
        #                  'cost': current.gval}, out)
    return current.path()