def main(): path = '/Users/chanaross/dev/Thesis/UberData/' data_name = '3D_allDataLatLonCorrected_250gridpickle_5min.p' data = pd.read_pickle(path + data_name) for t in range(300): #range(data.shape[2]): plotSpesificTime(data[:, :, t], t, path + 'picsFor3D/dataOut') listNames = ['dataOut' + '_' + str(t) + '.png' for t in range(300)] create_gif(path + 'picsFor3D/', listNames, 1, 'dataOut') return
def createTimeGif_allData(net_data, real_data, timeIndexs, fileName, pathName, dataType): lengthX = net_data.shape[1] lengthY = net_data.shape[2] maxTick = np.max(real_data).astype(int) for i, t in enumerate(timeIndexs): temp_net = net_data[i, :, :].reshape([1, lengthX, lengthY]) temp_real = real_data[i, :, :].reshape([1, lengthX, lengthY]) plotSpesificTime_allData(temp_net, temp_real, t, pathName, fileName, dataType, maxTick) listNames = [dataType + '_' + fileName + '_' + str(t) + '.png' for t in timeIndexs] create_gif(pathName, listNames, 1, dataType + '_' + fileName) for fileName in listNames: os.remove(pathName + fileName) return
def create_gif_plot(mat, nweek_plot, num_days, num_ticks_per_day, pathName, fileName): max_ticks = np.max(mat).astype(int) ticksDict = list(range(max_ticks + 1)) fig, axes = plt.subplots(1, 1) t = 0 for nday in range(num_days): for nticks in range(num_ticks_per_day): mat_plot = mat[:, :, nticks, nday, nweek_plot] try: sns.heatmap(mat_plot, cbar=True, center=1, square=True, vmin=0, vmax=np.max(ticksDict), cmap='CMRmap_r', cbar_kws=dict(ticks=ticksDict)) except: print("hi") plt.suptitle('week- {0}, day- {1},time- {2}:00'.format( nweek_plot, nday, t), fontsize=16) plt.title('num events') plt.xlabel('X axis') plt.ylabel('Y axis') plt.savefig(pathName + fileName + '_' + str(t) + '.png') plt.close() t += 1 timeIndexs = list(range(t)) listNames = [fileName + '_' + str(t) + '.png' for t in timeIndexs] create_gif(pathName, listNames, 1, fileName) for fileName in listNames: os.remove(pathName + fileName)
def plotResults(m, carsPos, eventsPos, eventsOpenTime, eventsCloseTime, plotFigures, fileLoc, fileName, gs): plot_gif = False if plot_gif: if not os.path.isdir(fileLoc + fileName): os.mkdir(fileLoc + fileName) nCars = carsPos.shape[0] nEvents = eventsPos.shape[0] paramKey = [v.varName.split('[')[0] for v in m.getVars()] param = {k: [] for k in paramKey} for v in m.getVars(): param[v.varName.split('[')[0]].append(v.x) param['cEventsToEvents'] = np.array(param['cEventsToEvents']).reshape( nEvents, nEvents) param['cCarsToEvents'] = np.array(param['cCarsToEvents']).reshape( nCars, nEvents) param['isPickedUp'] = np.array(param['isPickedUp']).reshape(nEvents, 1) param['pickUpTime'] = np.array(param['pickUpTime']).reshape(nEvents, 1) eIds = np.array(range(nEvents)) pathCars = [[carsPos[i, :]] for i in range(nCars)] targetPos = [] eventsPerCar = [[] for i in range(nCars)] for i in range(nCars): ePickedUp = eIds[param['cCarsToEvents'][i, :] == 1] if ePickedUp.size > 0: pathCars[i].append(eventsPos[ePickedUp, :][0]) targetPos.append(eventsPos[ePickedUp, :][0]) eventsPerCar[i].append(ePickedUp[0]) carNotFinished = True while carNotFinished: ePickedUp = eIds[param['cEventsToEvents'][ePickedUp[0], :] == 1] if ePickedUp.size > 0: pathCars[i].append(eventsPos[ePickedUp, :][0]) eventsPerCar[i].append(ePickedUp[0]) else: carNotFinished = False else: pathCars[i].append(carsPos[i, :]) targetPos.append(carsPos[i, :]) pathCars[i] = np.array(pathCars[i]) carFullPath = [[carsPos[i, :]] for i in range(nCars)] currentPos = copy.deepcopy(carsPos) maxTime = np.max(eventsCloseTime).astype(int) for c in range(nCars): k = 0 for t in range(int(maxTime)): if cdist(currentPos[c, :].reshape(1, 2), targetPos[c].reshape(1, 2), metric='cityblock') > 0: # move car towards target currentPos[c, :] = moveCar(currentPos[c, :], targetPos[c]) else: if len(eventsPerCar[c]) > 0: # car has reached current target, check if target can be picked up if t < param['pickUpTime'][eventsPerCar[c][k]]: # target has not opened yet, needs to wait for target to open targetPos[c] = copy.deepcopy(currentPos[c, :]) else: if len(eventsPerCar[c]) - 1 > k: # target is opened, advance car to next target k += 1 targetPos[c] = copy.deepcopy( eventsPos[eventsPerCar[c][k], :]) else: # car reached target and there are no more targets in cars list targetPos[c] = copy.deepcopy(currentPos[c, :]) currentPos[c, :] = moveCar(currentPos[c, :], targetPos[c]) carFullPath[c].append(copy.deepcopy(currentPos[c, :])) imageList = [] if plotFigures: fig, ax = plt.subplots() ax.set_title('total results') ax.scatter([], [], c='y', marker='*', label='Opened') ax.scatter([], [], c='k', marker='*', label='Created') ax.scatter([], [], c='r', marker='*', label='Canceled') ax.scatter([], [], c='g', marker='*', label='Closed') numClosedVec = np.zeros(maxTime) numCanceledVec = np.zeros(maxTime) numOpenedVec = np.zeros(maxTime) numTotalEventsVec = np.zeros(maxTime) tempEventsVec = np.zeros(maxTime) for t in range(int(maxTime)): numEventsOpened = 0 numEventsClosed = 0 numEventsCanceled = 0 for e in range(nEvents): if param['pickUpTime'][e] <= t and param['isPickedUp'][e]: numEventsClosed += 1 if eventsCloseTime[e] < t and not param['isPickedUp'][e]: numEventsCanceled += 1 if eventsOpenTime[e] <= t and eventsCloseTime[e] >= t: if not param['isPickedUp'][e]: numEventsOpened += 1 elif param['isPickedUp'][e] and param['pickUpTime'][e] > t: numEventsOpened += 1 numTotalEvents = numEventsClosed + numEventsCanceled + numEventsOpened numTotalEventsVec[t] = numTotalEvents numClosedVec[t] = numEventsClosed numOpenedVec[t] = numEventsOpened numCanceledVec[t] = numEventsCanceled if plot_gif: currentCarsPos = np.array([c[t] for c in carFullPath]) plotForGif(currentCarsPos, eventsPos, param['pickUpTime'], eventsOpenTime, eventsCloseTime, param['isPickedUp'], fileName, t, gs) timeVec = np.array(range(int(maxTime))) dataOut = { 'closedEvents': numClosedVec, 'canceledEvents': numCanceledVec, 'openedEvents': numOpenedVec, 'allEvents': numTotalEventsVec, 'time': timeVec } if plotFigures: plt.plot(timeVec, numClosedVec, c='g', marker='*') plt.plot(timeVec, numCanceledVec, c='r', marker='*') plt.plot(timeVec, numOpenedVec, c='y', marker='*') plt.plot(timeVec, numTotalEventsVec, c='k', marker='*') ax.grid(True) plt.legend() plt.show() if plot_gif: listNames = [ fileName + '_' + str(t) + '.png' for t in range(int(maxTime)) ] create_gif(fileLoc + fileName + '/', listNames, 1, fileName) return dataOut, param, carFullPath
def main(): imageList = [] FlagCreateGif = 0 fileLoc = '/Users/chanaross/dev/Thesis/Simulation/Anticipitory/with_machine_learning/Results/' for pickleName in pickleNames: lg = pickle.load(open(fileLoc + pickleName + '.p', 'rb')) simTime = 20 if 'Hungarian' in pickleName: events = lg['events'] gridSize = lg['gs'] simTime = np.min( [np.max([c['time'] for c in lg['cars'][0]]), simTime]) time = list(range(simTime)) if FlagCreateGif: for t in time: imageList.append(plotCurrentTimeGreedy(t, gridSize, lg)) kwargs_write = {'fps': 1.0, 'quantizer': 'nq'} imageio.mimsave('./gif' + pickleName + '.gif', imageList, fps=1) plt.close() plotBasicStatisticsOfEvents(gridSize, lg, pickleName, simTime) plotCarsHeatmap(gridSize, lg, simTime, pickleName) numEventsClosed = len([ e for e in events.values() if e['closed'] and e['timeStart'] + e['waitTime'] <= simTime ]) print('number of closed events:' + str(numEventsClosed)) cost = lg['cost'] print('total cost is : ' + str(cost)) elif 'SimAnticipatory' in pickleName or 'Greedy' in pickleName: if FlagCreateGif: if not os.path.isdir(fileLoc + pickleName): os.mkdir(fileLoc + pickleName) # this is the anticipatory results for inner MIO opt. time = lg['time'] gridSize = lg['gs'] simTime = np.max(time) openedEvents = np.array(lg['OpenedEvents']) closedEvents = np.array(lg['closedEvents']) canceledEvnets = np.array(lg['canceledEvents']) allEvents = np.array(lg['allEvents']) cost = lg['cost'] eventsPos = [ c.position for c in lg['pathresults'][-1].events.notCommited.values() ] carsPos = [ c.path for c in lg['pathresults'][-1].cars.notCommited.values() ] if FlagCreateGif: for t in time: plotCurrentTimeAnticipatory( lg['pathresults'][t], len(eventsPos), len(carsPos), gridSize, fileLoc + '/' + pickleName + '/' + pickleName) listNames = [pickleName + '_' + str(t) + '.png' for t in time] create_gif(fileLoc + pickleName + '/', listNames, 1, pickleName) plotBasicStatisticsOfEvents(gridSize, lg, pickleName, simTime) # plotCarsHeatmap(gridSize, lg, simTime, pickleName) print('number of closed events:' + str(closedEvents[-1])) cost = lg['cost'] print('total cost is : ' + str(cost)) elif 'Optimization' in pickleName: time = lg['time'] gridSize = lg['gs'] simTime = np.max(time) closedEvents = np.array(lg['closedEvents']) cost = lg['cost'] print('number of closed events:' + str(closedEvents[-1])) print('total cost is :' + str(cost)) plotBasicStatisticsOfEvents(gridSize, lg, pickleName, simTime) plt.show()
def main(): # network_path = '/Users/chanaross/dev/Thesis/MachineLearning/forGPU/GPU_results/limitedZero_500grid/' # network_name = 'gridSize11_epoch86_batch35_torch.pkl' network_path = '/Users/chanaross/dev/Thesis/MachineLearning/forGPU/GPU_results/FullGridInput_multiClass/' # network_name = 'gridSize11_epoch4_batch140_torch.pkl' network_names = [f for f in os.listdir(network_path) if f.endswith('.pkl')] # network_names = ['seq_10_bs_50_hs_20_lr_0.05_ot_1_torch_61Epochs.pkl'] data_path = '/Users/chanaross/dev/Thesis/UberData/' data_name = '3D_allDataLatLonCorrected_20MultiClass_500gridpickle_30min.p' # network_name = 'gridSize20_epoch608_batch9_torch.pkl' # data_path = '/Users/chanaross/dev/Thesis/UberData/' # data_name = '3D_allDataLatLonCorrected_500gridpickle_30min.p' dataInputReal = np.load(data_path + data_name) xmin = 0 xmax = dataInputReal.shape[0] ymin = 0 ymax = dataInputReal.shape[1] zmin = 48 dataInputReal = dataInputReal[xmin:xmax, ymin:ymax, zmin:] # shrink matrix size for fast training in order to test model # dataInputReal[dataInputReal > 1] = 1 # reshape input data for network format - lengthT = dataInputReal.shape[2] lengthX = dataInputReal.shape[0] lengthY = dataInputReal.shape[1] dataInputReal = np.swapaxes(dataInputReal, 0, 1) dataInputReal = np.swapaxes(dataInputReal, 0, 2) # dataInputReal = dataInputReal.reshape(lengthT, lengthX, lengthY) for network_name in network_names: my_net = torch.load(network_path + network_name, map_location=lambda storage, loc: storage) my_net.eval() plotEpochGraphs(my_net) accuracy = [] rmse = [] numEventsCreated = [] numEventsPredicted = [] correct_non_zeros = [] correct_zeros = [] timeOut = [] figPath = '/Users/chanaross/dev/Thesis/MachineLearning/forGPU/GPU_results/FullGridInput_multiClass/figures/' numRuns = 20 fileName = str(numRuns) + network_name.replace('.pkl', '') for i in range(numRuns): # print("run num:"+str(i)) start_time = i+200 # start_time = np.random.randint(50, dataInputReal.shape[0] - 50) timeOut.append(start_time) end_time = start_time + 0 realMatOut = createRealEventsUberML_network(dataInputReal, start_time, end_time) previousEventMatrix = getPreviousEventMat(dataInputReal, start_time, my_net.sequence_size) eventsPos, eventsTimeWindow, netEventOut = createEventDistributionUber(previousEventMatrix, my_net, 3, start_time, end_time) sizeMat = netEventOut.size rmse.append(sqrt(metrics.mean_squared_error(realMatOut.reshape(-1), netEventOut.reshape(-1)))) accuracy.append(np.sum(realMatOut == netEventOut) / (sizeMat)) sizeMat_zeros = netEventOut[realMatOut == 0].size sizeMat_non_zeros = netEventOut[realMatOut != 0].size if (sizeMat_non_zeros>0): correct_non_zeros.append(np.sum(netEventOut[realMatOut != 0] == realMatOut[realMatOut != 0]) / sizeMat_non_zeros) if sizeMat_zeros>0: correct_zeros.append(np.sum(netEventOut[realMatOut == 0] == realMatOut[realMatOut == 0]) / sizeMat_zeros) plotSpesificTime(realMatOut, netEventOut, start_time, figPath + fileName) numEventsCreated.append(np.sum(realMatOut)) numEventsPredicted.append(np.sum(netEventOut)) # realMatOut[realMatOut > 1] = 1 # distMatOut[distMatOut > 1] = 1 # accuracy1.append(np.sum(np.sum(realMatOut == distMatOut)/(realMatOut.shape[0]*realMatOut.shape[1]))) # if (realMatOut[realMatOut!=0].size >0): # non_zero_accuracy1.append(np.sum(np.sum(realMatOut[realMatOut != 0] == distMatOut[realMatOut != 0]))/(realMatOut[realMatOut != 0].size)) # # if (distMatOut[distMatOut!=0].size >0): # non_zero_accuracy1_dist.append(np.sum(np.sum(realMatOut[distMatOut != 0] == distMatOut[distMatOut != 0]))/(realMatOut[distMatOut != 0].size)) listNames = [fileName + '_' + str(t) + '.png' for t in timeOut] create_gif(figPath, listNames, 1, fileName) # plt.scatter(range(len(accuracy)), 100 * np.array(accuracy)) # plt.xlabel('run number [#]') # plt.ylabel('accuracy [%]') # plt.figure() # plt.scatter(range(len(rmse)), np.array(rmse)) # plt.xlabel('run number [#]') # plt.ylabel('RMSE') # plt.figure() plt.scatter(range(len(numEventsCreated)), np.array(numEventsCreated), label="num real events") plt.scatter(range(len(numEventsPredicted)), np.array(numEventsPredicted), label="num predicted") # plt.xlabel('run number [#]') # plt.ylabel('num events created') # plt.legend() # plt.figure() # plt.scatter(range(len(numEventsCreated)), np.abs(np.array(numEventsCreated) - np.array(numEventsPredicted)),label="difference between prediction and real") # plt.xlabel('run number [#]') # plt.ylabel('abs. (real - pred)') # plt.legend() # plt.figure() # plt.scatter(range(len(correct_zeros)), 100 * np.array(correct_zeros)) # plt.xlabel('run number [#]') # plt.ylabel('correct_zeros') # plt.figure() # plt.scatter(range(len(correct_non_zeros)), 100 * np.array(correct_non_zeros)) # plt.xlabel('run number [#]') # plt.ylabel('correct non zeros') print("average RMSE for " + str(numRuns) + " runs is:" + str(np.mean(np.array(rmse)))) print("average accuracy for " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(accuracy)))) print("average corrected zeros " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(correct_zeros)))) print("average corrected non zeros for " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(correct_non_zeros)))) plt.show() return
def main(): # data loader - path = '/Users/chanaross/dev/Thesis/ProbabilityFunction/theoreticalProbability/' figPath = '/Users/chanaross/dev/Thesis/ProbabilityFunction/theoreticalProbability/figures/' fileNameReal = 'Test_3Dmat_theoreticalData_8_mu_3_sigma.p' fileNameDist = 'CDF_4Dmat_theoreticalData_8_mu_3_sigma.p' # data real values are between 0 and k (k is the maximum amount of concurrent events at each x,y,t) # data dist have values that are the probability of having k events at x,y,t dataInputReal = np.load( path + fileNameReal) # matrix size is : [xsize , ysize, timeseq] dataInputProb = np.load( path + fileNameDist ) # matrix size is : [xsize , ysize, timeseq, probability for k events] # dataInputRealFull = np.zeros(shape=(dataInputReal.shape[0], dataInputReal.shape[1], dataInputReal.shape[2] * dataInputReal.shape[3])) # numTimes = dataInputReal.shape[2] # for i in range(dataInputReal.shape[3]): # dataInputRealFull[:, :, i*numTimes: (i+1)*numTimes] = dataInputReal[:, :, :, i] # dataInputRealFull.dump('/Users/chanaross/dev/Thesis/UberData/3D_allDataLatLonCorrected_20MultiClass_500gridpickle_30min.p') xmin = 0 xmax = dataInputReal.shape[0] ymin = 0 ymax = dataInputReal.shape[1] dataInputReal = dataInputReal[ xmin:xmax, ymin: ymax, :] # shrink matrix size for fast training in order to test model dataInputProb = dataInputProb[xmin:xmax, ymin:ymax, :, :] accuracy = [] accuracy1 = [] rmse = [] numEventsPredicted = [] numEventsCreated = [] correct_zeros = [] correct_non_zeros = [] timeOut = [] timeIndexs = np.arange(1000, 1200, 1).astype(int) numRuns = timeIndexs.size print(timeIndexs) fileNameOut = 'Theoretical_benchmark_results_' + str(numRuns) for t in timeIndexs: # start_time = np.random.randint(10, dataInputReal.shape[2]-10) start_time = t timeOut.append(start_time) realMatOut, distMatOut = getBenchmarkAccuracy(start_time, 5, dataInputReal, dataInputProb) sizeMat = realMatOut.shape[0] * realMatOut.shape[1] rmse.append( sqrt( metrics.mean_squared_error(realMatOut.reshape(-1), distMatOut.reshape(-1)))) accuracy.append(np.sum(realMatOut == distMatOut) / (sizeMat)) sizeMat_zeros = distMatOut[realMatOut == 0].size sizeMat_non_zeros = distMatOut[realMatOut != 0].size if (sizeMat_non_zeros > 0): correct_non_zeros.append( np.sum( distMatOut[realMatOut != 0] == realMatOut[realMatOut != 0]) / sizeMat_non_zeros) if sizeMat_zeros > 0: correct_zeros.append( np.sum( distMatOut[realMatOut == 0] == realMatOut[realMatOut == 0]) / sizeMat_zeros) plotSpesificTime(realMatOut, distMatOut, start_time, figPath + fileNameOut) numEventsCreated.append(np.sum(realMatOut)) numEventsPredicted.append(np.sum(distMatOut)) y_true = realMatOut.reshape(-1) y_pred = distMatOut.reshape(-1) labels = np.unique([y_true, y_pred]) # plot_confusion_matrix(y_true, y_pred, classes=labels, t=start_time,fileLoc=figPath, fileName=fileNameOut, normalize=True) # realMatOut[realMatOut > 1] = 1 # distMatOut[distMatOut > 1] = 1 # accuracy1.append(np.sum(np.sum(realMatOut == distMatOut)/(realMatOut.shape[0]*realMatOut.shape[1]))) # if (realMatOut[realMatOut!=0].size >0): # non_zero_accuracy1.append(np.sum(np.sum(realMatOut[realMatOut != 0] == distMatOut[realMatOut != 0]))/(realMatOut[realMatOut != 0].size)) # # if (distMatOut[distMatOut!=0].size >0): # non_zero_accuracy1_dist.append(np.sum(np.sum(realMatOut[distMatOut != 0] == distMatOut[distMatOut != 0]))/(realMatOut[distMatOut != 0].size)) listNames = [fileNameOut + '_' + str(t) + '.png' for t in timeOut] create_gif(figPath, listNames, 1, fileNameOut) # listNames = ['ConfMat_'+fileNameOut + '_' + str(t) + '.png' for t in timeOut] # create_gif(figPath, listNames, 1, 'ContMat_' + fileNameOut) plt.scatter(range(len(accuracy)), 100 * np.array(accuracy)) plt.xlabel('run number [#]') plt.ylabel('accuracy [%]') plt.figure() plt.scatter(range(len(rmse)), np.array(rmse)) plt.xlabel('run number [#]') plt.ylabel('RMSE') plt.figure() plt.scatter(range(len(numEventsCreated)), np.array(numEventsCreated), label="num real events") plt.scatter(range(len(numEventsPredicted)), np.array(numEventsPredicted), label="num predicted") plt.xlabel('run number [#]') plt.ylabel('num events created') plt.legend() plt.figure() plt.scatter( range(len(numEventsCreated)), np.abs(np.array(numEventsCreated) - np.array(numEventsPredicted)), label="difference between prediction and real") plt.xlabel('run number [#]') plt.ylabel('abs. (real - pred)') plt.legend() plt.figure() plt.scatter(range(len(correct_zeros)), 100 * np.array(correct_zeros)) plt.xlabel('run number [#]') plt.ylabel('correct_zeros') plt.figure() plt.scatter(range(len(correct_non_zeros)), 100 * np.array(correct_non_zeros)) plt.xlabel('run number [#]') plt.ylabel('correct non zeros') print("average RMSE for " + str(numRuns) + " runs is:" + str(np.mean(np.array(rmse)))) print("average accuracy for " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(accuracy)))) print("average corrected zeros " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(correct_zeros)))) print("average corrected non zeros for " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(correct_non_zeros)))) plt.show() return
def main(): # loading probability matrix from uber data. matrix is: x,y,h where x,y are the grid size and h is the time (0-24 hours) probFileName = '/Users/chanaross/dev/Thesis/ProbabilityFunction/CreateEvents/4D_UpdatedGrid_5min_250grid_LimitedProbability_CDFMat_wday_1.p' probabilityMatrix = np.load(probFileName) xLim = [0, 20] yLim = [40, 70] probabilityMatrix = probabilityMatrix[xLim[0]:xLim[1], yLim[0]:yLim[1], :, :] eventsFileName = '/Users/chanaross/dev/Thesis/UberData/4D_UpdatedGrid_5min_250Grid_LimitedEventsMat_wday1.p' eventsMatrix = np.load(eventsFileName) eventsMatrix = eventsMatrix[xLim[0]:xLim[1], yLim[0]:yLim[1], :, 1] np.random.seed(100) shouldRunAnticipatory = 1 shouldRunGreedy = 1 shouldRunOptimization = 1 loadFromPickle = 0 shouldPrint = False # params epsilon = 0.001 # distance between locations to be considered same location simStartTime = 0 lengthSim = 24 * 3 # one hour, each time step is 5 min. of real time numStochasticRuns = 50 lengthPrediction = 5 deltaTimeForCommit = 10 closeReward = 80 cancelPenalty = 140 openedCommitedPenalty = 1 openedNotCommitedPenalty = 5 gridSize = [probabilityMatrix.shape[0], probabilityMatrix.shape[1]] deltaOpenTime = 3 numCars = 4 carPosX = np.random.randint(0, gridSize[0], numCars) carPosY = np.random.randint(0, gridSize[1], numCars) carPos = np.column_stack((carPosX, carPosY)).reshape(numCars, 2) eventPos, eventTimes = createRealEventsDistributionUber( simStartTime, 0, lengthSim, eventsMatrix, deltaOpenTime, 0) eventStartTime = eventTimes[:, 0] eventEndTime = eventTimes[:, 1] # plt.scatter(eventPos[:,0],eventPos[:,1], c= 'r') # plt.scatter(carPos[:,0],carPos[:,1],c='k') # plt.show() uncommitedCarDict = {} commitedCarDict = {} uncommitedEventDict = {} commitedEventDict = {} numEvents = eventStartTime.shape[0] for i in range(numCars): tempCar = Car(carPos[i, :], i) uncommitedCarDict[i] = tempCar for i in range(numEvents): tempEvent = Event(eventPos[i, :], i, eventStartTime[i], eventEndTime[i]) uncommitedEventDict[i] = tempEvent carMonitor = commitMonitor(commitedCarDict, uncommitedCarDict) eventMonitor = commitMonitor(commitedEventDict, uncommitedEventDict) initState = State(root=True, carMonitor=carMonitor, eventMonitor=eventMonitor, cost=0, parent=None, time=0, openedNotCommitedPenalty=openedNotCommitedPenalty, openedCommitedPenalty=openedCommitedPenalty, cancelPenalty=cancelPenalty, closeReward=closeReward, timeDelta=deltaTimeForCommit, eps=epsilon) fileName = str(lengthPrediction) + 'lpred_' + str(simStartTime) + 'startTime_' + str(gridSize[0]) + 'gridX_' +\ str(gridSize[1]) + 'gridY_' + str(eventTimes.shape[0]) + 'numEvents_' + \ str(numStochasticRuns) + 'nStochastic_' + str(numCars) + 'numCars_uberData' fileLoc = '/Users/chanaross/dev/Thesis/Simulation/Anticipitory/Results/' if shouldRunAnticipatory: # run anticipatory: stime = time.process_time() pAnticipatory = anticipatorySimulation(initState, numStochasticRuns, gridSize, lengthPrediction, deltaOpenTime, simStartTime, probabilityMatrix, shouldPrint=shouldPrint) etime = time.process_time() runTimeA = etime - stime print('Anticipatory cost is:' + str(pAnticipatory[-1].gval)) print('run time is:' + str(runTimeA)) dataAnticipatory = postAnalysis(pAnticipatory) anticipatoryFileName = 'SimAnticipatoryMioFinalResults_' + fileName greedyFileName = 'SimGreedyFinalResults_' + fileName # Anticipatory output: with open( 'SimAnticipatory_bruteForce_MioFinalResults_' + fileName + '.p', 'wb') as out: pickle.dump( { 'runTime': runTimeA, 'pathresults': pAnticipatory, 'time': dataAnticipatory['timeVector'], 'gs': gridSize, 'OpenedEvents': dataAnticipatory['openedEvents'], 'closedEvents': dataAnticipatory['closedEvents'], 'canceledEvents': dataAnticipatory['canceledEvents'], 'allEvents': dataAnticipatory['allEvents'], 'cost': pAnticipatory[-1].gval }, out) time2 = [] if not os.path.isdir(fileLoc + anticipatoryFileName): os.mkdir(fileLoc + anticipatoryFileName) for s in pAnticipatory: plotForGif( s, numEvents, numCars, gridSize, fileLoc + '/' + anticipatoryFileName + '/' + anticipatoryFileName) time2.append(s.time) listNames = [ anticipatoryFileName + '_' + str(t) + '.png' for t in time2 ] create_gif(fileLoc + anticipatoryFileName + '/', listNames, 1, anticipatoryFileName) plt.close() if shouldRunGreedy: if loadFromPickle: pickleName = 'SimAnticipatoryMioFinalResults_8lpred_0startTime_10gridX_10gridY_23numEvents_30nStochastic_2numCars_uberData' pathName = '/home/chana/Documents/Thesis/FromGitFiles/Simulation/Anticipitory/PickleFiles/' dataPickle = pickle.load(open(pathName + pickleName + '.p', 'rb')) initState = dataPickle['pathresults'][0] gridSize = dataPickle['gs'] fileName = '15numEvents_2numCars_0.75lam_7gridSize' numEvents = initState.events.length() numCars = initState.cars.length() # run greedy: stime = time.process_time() pGreedy = greedySimulation(initState, True) etime = time.process_time() runTimeG = etime - stime print('Greedy cost is:' + str(pGreedy[-1].gval)) print('run time is: ' + str(runTimeG)) dataGreedy = postAnalysis(pGreedy) # Greedy output: with open('SimGreedyFinalResults_' + fileName + '.p', 'wb') as out: pickle.dump( { 'runTime': runTimeG, 'pathresults': pGreedy, 'time': dataGreedy['timeVector'], 'gs': gridSize, 'OpenedEvents': dataGreedy['openedEvents'], 'closedEvents': dataGreedy['closedEvents'], 'canceledEvents': dataGreedy['canceledEvents'], 'allEvents': dataGreedy['allEvents'], 'cost': pGreedy[-1].gval }, out) time2 = [] if not os.path.isdir(fileLoc + greedyFileName): os.mkdir(fileLoc + greedyFileName) for s in pGreedy: plotForGif(s, numEvents, numCars, gridSize, fileLoc + '/' + greedyFileName + '/' + greedyFileName) time2.append(s.time) listNames = [greedyFileName + '_' + str(t) + '.png' for t in time2] create_gif(fileLoc + greedyFileName + '/', listNames, 1, greedyFileName) plt.close() if shouldRunOptimization: if loadFromPickle: pickleName = 'SimAnticipatoryMioFinalResults_4lpred_0startTime_10gridX_10gridY_22numEvents_50nStochastic_2numCars_uberData' pathName = '/Users/chanaross/dev/Thesis/Simulation/Anticipitory/PickleFiles/' dataPickle = pickle.load(open(pathName + pickleName + '.p', 'rb')) initState = dataPickle['pathresults'][0] gridSize = dataPickle['gs'] fileName = '4lpred_0startTime_10gridX_10gridY_22numEvents_50nStochastic_2numCars_uberData' numEvents = initState.events.length() numCars = initState.cars.length() dataOptimization = optimizedSimulation(initState, fileLoc, fileName, gridSize) # optimization output: with open('SimOptimizationFinalResults_' + fileName + '.p', 'wb') as out: pickle.dump( { 'time': dataOptimization['time'], 'gs': gridSize, 'OpenedEvents': dataOptimization['openedEvents'], 'closedEvents': dataOptimization['closedEvents'], 'canceledEvents': dataOptimization['canceledEvents'], 'cost': dataOptimization['cost'], 'allEvents': dataOptimization['allEvents'] }, out) return
def main(): network_path = '/Users/chanaross/dev/Thesis/MachineLearning/forGPU/GPU_results/singleGridId_multiClassSmooth/' network_names = [ 'smooth_40_seq_5_bs_40_hs_128_lr_0.05_ot_1_wd_0.002_torch.pkl' ] # network_names = [f for f in os.listdir(network_path) if (f.endswith('.pkl'))] data_path = '/Users/chanaross/dev/Thesis/UberData/' data_name = '3D_allDataLatLonCorrected_20MultiClass_500gridpickle_30min.p' # dataInputReal = dataInputReal.reshape(lengthT, lengthX, lengthY) results = { 'networkName': [], 'mean RMSE': [], 'mean accuracy': [], 'mean zeros accuracy': [], 'mean nonZeros accuracy': [] } timeIndexs = np.arange(50, 400, 1).astype(int) numRuns = timeIndexs.size for network_name in network_names: print("network:" + network_name.replace('.pkl', '')) my_net = torch.load(network_path + network_name, map_location=lambda storage, loc: storage) my_net.eval() dataInputReal = np.load(data_path + data_name) xmin = 5 #0 xmax = 6 #dataInputReal.shape[0] ymin = 10 ymax = 11 #dataInputReal.shape[1] zmin = 48 dataInputReal = dataInputReal[ xmin:xmax, ymin:ymax, zmin:] # shrink matrix size for fast training in order to test model # dataInputReal = dataInputReal[5:6, 10:11, :] try: smoothParam = my_net.smoothingParam except: print("no smoothing param, using default 15") smoothParam = 15 dataInputSmooth = moving_average( dataInputReal, smoothParam ) # smoothing data so that results are more clear to network # dataInputReal[dataInputReal > 1] = 1 # reshape input data for network format - lengthT = dataInputReal.shape[2] lengthX = dataInputReal.shape[0] lengthY = dataInputReal.shape[1] dataset_uber = DataSet_oneLSTM_allGrid(dataInputReal, my_net.sequence_size) # creating data loader dataloader_uber = data.DataLoader(dataset=dataset_uber, batch_size=200, shuffle=False) dataInputReal = np.swapaxes(dataInputReal, 0, 1) dataInputReal = np.swapaxes(dataInputReal, 0, 2) dataInputSmooth = np.swapaxes(dataInputSmooth, 0, 1) dataInputSmooth = np.swapaxes(dataInputSmooth, 0, 2) accuracy = [] rmse = [] numEventsCreated = [] numEventsPredicted = [] correct_non_zeros = [] correct_zeros = [] timeOut = [] accuracySm = [] correct_non_zerosSm = [] correct_zerosSm = [] numEventsCreatedSm = [] numEventsPredictedSm = [] rmseSm = [] figPath = network_path + 'figures/' fileName = str(numRuns) + network_name.replace('.pkl', '') plotEpochGraphs(my_net, figPath, fileName) y_pred = [] y_true = [] y_trueSm = [] y_predSm = [] realOut = np.zeros([timeIndexs.size, lengthX, lengthY]) predOut = np.zeros([timeIndexs.size, lengthX, lengthY]) for x in range(lengthX): for y in range(lengthY): for t, i in enumerate(timeIndexs): # print("run num:"+str(i)) # start_time = i+2000 start_time = i start_timeSm = i - np.floor(smoothParam / 2).astype(int) timeOut.append(start_time) end_time = start_time + 0 end_timeSm = start_timeSm + 0 realMatOut = createRealEventsUberML_network( dataInputReal[:, x, y].reshape([lengthT, 1, 1]), start_time, end_time) realMatOutSm = createRealEventsUberML_network( dataInputSmooth[:, x, y].reshape( [dataInputSmooth.shape[0], 1, 1]), start_timeSm, end_timeSm) previousEventMatrix = getPreviousEventMat( dataInputReal[:, x, y].reshape([lengthT, 1, 1]), start_time, my_net.sequence_size) previousEventMatrixSm = getPreviousEventMat( dataInputSmooth[:, x, y].reshape( [dataInputSmooth.shape[0], 1, 1]), start_timeSm, my_net.sequence_size) eventsPos, eventsTimeWindow, netEventOut = createEventDistributionUber( previousEventMatrix, my_net, 3, start_time, end_time) _, _, netEventOutSm = createEventDistributionUber( previousEventMatrixSm, my_net, 3, start_timeSm, end_timeSm) sizeMat = netEventOut.size rmse.append( sqrt( metrics.mean_squared_error( realMatOut.reshape(-1), netEventOut.reshape(-1)))) rmseSm.append( sqrt( metrics.mean_squared_error( realMatOutSm.reshape(-1), netEventOutSm.reshape(-1)))) accuracy.append( np.sum(realMatOut == netEventOut) / (sizeMat)) accuracySm.append( np.sum(realMatOutSm == netEventOutSm) / sizeMat) sizeMat_zeros = netEventOut[realMatOut == 0].size sizeMat_non_zeros = netEventOut[realMatOut != 0].size sizeMat_non_zerosSm = netEventOut[realMatOutSm != 0].size sizeMat_zerosSm = netEventOut[realMatOutSm == 0].size if (sizeMat_non_zeros > 0): correct_non_zeros.append( np.sum(netEventOut[realMatOut != 0] == realMatOut[ realMatOut != 0]) / sizeMat_non_zeros) if sizeMat_zeros > 0: correct_zeros.append( np.sum(netEventOut[realMatOut == 0] == realMatOut[ realMatOut == 0]) / sizeMat_zeros) if (sizeMat_non_zerosSm > 0): correct_non_zerosSm.append( np.sum(netEventOutSm[realMatOutSm != 0] == realMatOutSm[realMatOutSm != 0]) / sizeMat_non_zerosSm) if sizeMat_zerosSm > 0: correct_zerosSm.append( np.sum(netEventOutSm[realMatOutSm == 0] == realMatOutSm[realMatOutSm == 0]) / sizeMat_zerosSm) numEventsCreated.append(np.sum(realMatOut)) numEventsPredicted.append(np.sum(netEventOut)) numEventsCreatedSm.append(np.sum(realMatOutSm)) numEventsPredictedSm.append(np.sum(netEventOutSm)) realOut[t, x, y] = realMatOut.reshape(-1) predOut[t, x, y] = netEventOut.reshape(-1) y_true.append(realMatOut.reshape(-1)) y_pred.append(netEventOut.reshape(-1)) y_trueSm.append(realMatOutSm.reshape(-1)) y_predSm.append(netEventOutSm.reshape(-1)) print("finished calculating network results") y_trueAr = np.array(y_true) y_predAr = np.array(y_pred) y_trueSmAr = np.array(y_trueSm) y_predSmAr = np.array(y_predSm) for t in range(timeIndexs.size): plotSpesificTime(realOut[t, :, :].reshape([1, lengthX, lengthY]), predOut[t, :, :].reshape([1, lengthX, lengthY]), timeIndexs[t], figPath + fileName) listNames = [fileName + '_' + str(t) + '.png' for t in timeIndexs] create_gif(figPath, listNames, 1, fileName) k = 0 for x in range(lengthX): for y in range(lengthY): plt.figure() plt.plot(timeIndexs, y_trueAr[k * timeIndexs.size:(k + 1) * timeIndexs.size], label='true output', linewidth=2, color='b') plt.plot(timeIndexs, y_trueSmAr[k * timeIndexs.size:(k + 1) * timeIndexs.size], label='true smooth output', linewidth=2, color='r') plt.plot(timeIndexs + 25, y_predAr[k * timeIndexs.size:(k + 1) * timeIndexs.size], label='predicted output', linewidth=2, color='k') plt.plot(timeIndexs + 25, y_predSmAr[k * timeIndexs.size:(k + 1) * timeIndexs.size], label='predicted smooth output', linewidth=2, color='m') plt.legend() plt.xlabel('Time') plt.ylabel('num events') plt.show() # plt.savefig(figPath + 'Results_' + 'x_' + str(x) + '_y_' + str(y) +'_'+fileName + '.png') # plt.close() k += 1 # plt.scatter(range(len(accuracy)), 100 * np.array(accuracy)) # plt.xlabel('run number [#]') # plt.ylabel('accuracy [%]') # plt.figure() # plt.scatter(range(len(rmse)), np.array(rmse)) # plt.xlabel('run number [#]') # plt.ylabel('RMSE') # plt.figure() # plt.scatter(range(len(numEventsCreated)), np.array(numEventsCreated), label="num real events") # plt.scatter(range(len(numEventsPredicted)), np.array(numEventsPredicted), label="num predicted") # plt.xlabel('run number [#]') # plt.ylabel('num events created') # plt.legend() # plt.figure() # plt.scatter(range(len(numEventsCreated)), np.abs(np.array(numEventsCreated) - np.array(numEventsPredicted)),label="difference between prediction and real") # plt.xlabel('run number [#]') # plt.ylabel('abs. (real - pred)') # plt.legend() # plt.figure() # plt.scatter(range(len(correct_zeros)), 100 * np.array(correct_zeros)) # plt.xlabel('run number [#]') # plt.ylabel('correct_zeros') # plt.figure() # plt.scatter(range(len(correct_non_zeros)), 100 * np.array(correct_non_zeros)) # plt.xlabel('run number [#]') # plt.ylabel('correct non zeros') print("average RMSE for " + str(numRuns) + " runs is:" + str(np.mean(np.array(rmse)))) print("average accuracy for " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(accuracy)))) print("average corrected zeros " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(correct_zeros)))) print("average corrected non zeros for " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(correct_non_zeros)))) print("smooth average RMSE for " + str(numRuns) + " runs is:" + str(np.mean(np.array(rmseSm)))) print("smooth average accuracy for " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(accuracySm)))) print("smooth average corrected zeros " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(correct_zerosSm)))) print("smooth average corrected non zeros for " + str(numRuns) + " runs is:" + str(100 * np.mean(np.array(correct_non_zerosSm)))) results['networkName'].append(network_name.replace('.pkl', '')) results['mean RMSE'].append(np.mean(np.array(rmse))) results['mean accuracy'].append(100 * np.mean(np.array(accuracy))) results['mean nonZeros accuracy'].append( 100 * np.mean(np.array(correct_non_zeros))) results['mean zeros accuracy'].append(100 * np.mean(np.array(correct_zeros))) df_results = pd.DataFrame.from_dict(results) df_results.to_csv(network_path + 'results.csv') plt.show() return