Пример #1
0
def solver3():
    car_info = loadData(sys.argv[1])
    grid_map = initialEmptyGridMap(car_info)
    barrier = getBarrier(car_info)
    #plotGridMap(barrier, 1000, 1000 + HORIZON)
    test_grid = np.copy(grid_map)
    car_model = CarModel()
    source = (0,0)
    max_time_steps = sorted(car_info.keys())[-1]
    current_time_step = 0
    output_file = sys.argv[3]
    death = 0
    cross = 0
    clearFile(output_file)
    last_valid_came_from = {}
    f = open(output_file, 'a')
    (t, y) = source
    s = '{0}\n{1} {2}\n#\n'.format(0*GRID_T,CHICKEN_X,(0-NUM_GRIDS_BELOW_ZERO)*GRID_Y)
    f.write(s)
    f.close()
    while current_time_step < max_time_steps:
        incremental_car_history = {}
        incremental_car_history[current_time_step] = car_info[current_time_step]
        print 'Online Source: '+repr(source)
        car_model.updateSolver3(incremental_car_history )
        speeds, intervals = car_model.getGaussianParams()
        #print speeds
        #print intervals
        #prin "\n#######################################################################\n"
        #car_model.printModel()
        updateGridMapSolver3(grid_map, current_time_step, incremental_car_history, car_model,speeds, intervals, barrier)
        #if current_time_step % 1000 == 0:
        #    print speeds
        #    print intervals
        #print car_model.initial_pos_xs
        #print car_model.initial_times
        #    plotGridMap(grid_map, current_time_step, current_time_step + 500)
        came_from, sub_goal = oneStepAction(grid_map, source)
        #only need one step and no overlap then just transfer in source
        if sub_goal == (-1,-1):
            goal = last_valid_came_from[sorted(last_valid_came_from.keys())[-1]]
            while goal != None:
                current = last_valid_came_from[goal]
                if current[0] == current_time_step+1:
                    source = current
                    break
                goal = current
        else:
            last_valid_came_from = came_from
            source = sub_goal
        f = open(output_file, 'a')
        (t, y) = source
        s = '{0}\n{1} {2}\n#\n'.format(t*GRID_T,CHICKEN_X,(y-NUM_GRIDS_BELOW_ZERO)*GRID_Y)
        f.write(s)
        f.close()
        #check Excution
        addBlocks(test_grid, {source[0]:car_info[source[0]]})
        if (test_grid[source[1]][current_time_step+1] != 0):
            print "death!", source, test_grid[source[1]][source[0]]
            death += 1
            source = (current_time_step+1, 0)

        if (source[1] >= NUM_GRIDS_Y - EXTRA_GRIDS):
            cross += 1
            #reach goal
            source = (current_time_step+1, 0)
        current_time_step += 1
    f = open(output_file, 'a')
    (t,y) = source
    s = '{0}\n{1} {2}\n#\n'.format(t*GRID_T,CHICKEN_X,(y-NUM_GRIDS_BELOW_ZERO)*GRID_Y)
    f.write(s)
    f.close()

    getScore(cross, death)
    print 'write to {0} file'.format(output_file)