コード例 #1
0
DECLARE_METHODS_FUNC = methods_nbeacons.declare_methods
DECLARE_OPERATORS_FUNC = operators_nbeacons.declare_operators
GOAL_GRAPH_CMP_FUNC = None

DIMENSION = 16  # width and height of grid
BEACON_FAIL_RATE = 20  # percent chance each beacon will fail each tick
WIND_ENABLED = True
WIND_DIR = 'east'  # direction to push the agent if it moves in this direction
WIND_STRENGTH = 0  # number of extra tiles for the agent to move
NUM_QUICKSAND = 10

# Load domain
world = domainread.load_domain(DOMAIN_FILE)

# Create Starting state
state1 = nbeacons_util.NBeaconGrid()
state1.generate(width=DIMENSION,
                height=DIMENSION,
                num_beacons=10,
                num_quicksand_spots=NUM_QUICKSAND)
state1_str = state1.get_STRIPS_str()

# Load state
stateread.apply_state_str(world, state1_str)

# Creates a PhaseManager object, which wraps a MIDCA object
myMidca = base.PhaseManager(world, display=DISPLAY_FUNC, verbose=2)

# Add phases by name
for phase in [
        "Simulate", "Perceive", "Interpret", "Eval", "Intend", "Plan", "Act"
コード例 #2
0
def runexperiment():
    # reset in case we run multiple experiments
    DATADIR = "experiments/nbeacons-experiment-1-data/"
    NOW_STR = datetime.datetime.strftime(datetime.datetime.now(),
                                         '%Y-%m-%d--%H-%M-%S')
    DATA_FILENAME = DATADIR + "NBeaconsExperiment1" + NOW_STR + ".csv"
    DATA_FILE_HEADER_STR = "runID,numCycles,agentType,windDir,windStrength,goalsActionsAchieved\n"
    SCENARIO_FILENAME = DATADIR + "NBeaconsScenario" + NOW_STR + ".txt"

    runs = []

    # generate goals randomly, such that no goal is repeated twice
    num_goals = 1000
    goal_list = []
    i = 0
    possible_goals = range(10)
    last_chosen_goal = -1
    while i < num_goals:
        if last_chosen_goal == -1:
            curr_goal = random.choice(possible_goals)
            goal_list.append(curr_goal)
            last_chosen_goal = curr_goal
        else:
            tmp_possible_goals = set(possible_goals) - set([last_chosen_goal])
            curr_goal = random.sample(tmp_possible_goals, 1)[0]
            goal_list.append(curr_goal)
            last_chosen_goal = curr_goal
        i += 1

    goal_list = map(lambda x: goals.Goal('B' + str(x), predicate="activated"),
                    goal_list)
    #print("goal list is ")
    #for g in goal_list:
    #    print("  "+str(g))
    state1 = nbeacons_util.NBeaconGrid()
    #state1.generate_good_test()
    state1.generate(width=DIMENSION,
                    height=DIMENSION,
                    num_beacons=NUM_BEACONS,
                    num_quicksand_spots=NUM_QUICKSAND)
    state1_str = state1.get_STRIPS_str()

    # args are [runID, agentType, windDir, windStrength, startingState, goalList]
    individual_runs = [
        # no wind, same starting state, same starting goals
        [0, 'v', 'east', 0, state1_str, goal_list],
        [1, 'g', 'east', 0, state1_str, goal_list],
        [2, 'm', 'east', 0, state1_str, goal_list],
    ]

    runs = individual_runs

    # Uses multiprocessing to give each run its own python process
    print("-- Starting experiment using " + str(NUM_PROCESSES) +
          " processes...")
    t0 = time.time()
    # **** NOTE: it is very important chunksize is 1 and maxtasksperchild is 1
    # **** (each MIDCA must use its own python process)
    pool = Pool(processes=NUM_PROCESSES, maxtasksperchild=1)
    results = pool.map(singlerun, runs, chunksize=1)
    t1 = time.time()
    timestr = '%.2f' % (t1 - t0)
    print("-- Experiment finished! Took " + timestr + "s, generated " +
          str(len(results)) + " data points")
    print("-- Writing data to file...")
    f = open(DATA_FILENAME, 'w')
    f.write(DATA_FILE_HEADER_STR)
    for r in results:
        f.write(r)
    print("-- Data written to file " + str(DATA_FILENAME))
    print("-- Experiment complete!")

    # if you have pyttsx installed, a voice will tell you your experiments are finished
    try:
        import pyttsx
        engine = pyttsx.init()
        engine.setProperty('rate', 70)
        engine.say('Your experiments are have finished running')
        engine.runAndWait()
    except:
        pass  # do nothing