Пример #1
0
def solver2():
    car_info = loadData(sys.argv[1])
    grid_map = initialEmptyGridMap(car_info)
    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)
    needUpdateGrid = True
    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]
        if car_model.enough_info == False:
            print 'Online Source: '+repr(source)
            car_model.updateSolver2(incremental_car_history )
            #car_model.printModel()
            updateGridMap(grid_map, current_time_step, incremental_car_history, car_model, car_model.enough_info)
            #plotGridMap(grid_map, current_time_step, current_time_step + 500)
            came_from, sub_goal = oneStepAction(grid_map, source)
            #only need one step and no overlab 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
        else:
            print 'Offline Source: '+repr(source)
            #car_model.printModel()
            if needUpdateGrid:
                updateGridMap(grid_map, current_time_step, incremental_car_history, car_model, car_model.enough_info)
                needUpdateGrid = False
                #plotGridMap(grid_map)
            came_from, goal  = aStar(grid_map, source)
            generateAnswer(came_from, goal, output_file)
            if (goal[1] >= NUM_GRIDS_Y - EXTRA_GRIDS):
                cross += 1
            source = (goal[0]+1,0)
            current_time_step = source[0]
    #for time_step in range(0, max_time_steps):
    #    if time_step%500 == 0:
    #        print time_step
    #        car_model.printModel()
    #        plotGridMap(grid_map, time_step, time_step + 4)
    getScore(cross, death)
    print 'write to {0} file'.format(output_file)