def simulate_Stationary(policy_file, config_file, row_number, column_number): policy = {} with open(policy_file) as f: csv_reader = csv.reader(f, delimiter=',') for row in csv_reader: policy[row[0]] = row[1] sim = Simulation(config_file) policy_modified = {} for key in policy.keys(): row = row_number - 1 - int(np.floor(int(key) / column_number)) column = int(key) % column_number if policy[key] == 'S': policy_modified[(column, row)] = 'south' elif policy[key] == 'E': policy_modified[(column, row)] = 'east' elif policy[key] == 'N': policy_modified[(column, row)] = 'north' elif policy[key] == 'W': policy_modified[(column, row)] = 'west' elif policy[key] == 'L': policy_modified[(column, row)] = 'stay' state = sim.get_state() #grab state done = False counter = 0 while not done: action = policy_modified[state['agents'][0]] print(action) done, state = sim.move(action) #main call if counter == 0: sleep(5) sleep(0.5) counter = counter + 1
def main(): sim = Simulation("config.txt") done = False while not done: done, state = sim.move(["south"]) #main call
def simulate_Markovian(policy_file, config_file, time_horizon, row_number, column_number): policy = {} print(policy_file) with open(policy_file, 'r') as f: csv_reader = csv.reader(f, delimiter=',') for k in csv_reader: policy[k[0]] = k[1] sim = Simulation(config_file) num_of_states = row_number * column_number policy_modified = [{} for i in range(int(time_horizon))] for key in policy.keys(): time_step = int(int(key) / int(num_of_states)) row = int(row_number - 1 - np.floor((int(key) % int(num_of_states)) / column_number)) column = (int(key) % int(num_of_states)) % column_number if policy[key] == 'S': policy_modified[time_step][(column, row)] = 'south' elif policy[key] == 'E': policy_modified[time_step][(column, row)] = 'east' elif policy[key] == 'N': policy_modified[time_step][(column, row)] = 'north' elif policy[key] == 'W': policy_modified[time_step][(column, row)] = 'west' elif policy[key] == 'L': policy_modified[time_step][(column, row)] = 'stay' print(policy_modified) state = sim.get_state() #grab state done = False counter = 0 while not done: action = policy_modified[counter][state['agents'][0]] print(action) done, state = sim.move(action) #main call if counter == 0: sleep(5) sleep(0.5) counter = counter + 1
def main(): global pubs, time_steps, sim, num_error, trials, trial, pubs_clf rospy.init_node('subs', anonymous=True) steps = 200 trials = 20 subs = rospy.Subscriber('live_cmd', Action, callback) pubs = rospy.Publisher('log', Sim, queue_size=10) pubs_clf = rospy.Publisher('reset_clf', Bool, queue_size=10) log = Sim() state = sim.get_state() log.state = state['agents'][0][0] sleep(1) dec = Bool() dec.data = True pubs.publish(log) pubs_clf.publish(dec) done = False while not rospy.is_shutdown(): if time_steps == steps: subs.unregister() trial += 1 time_steps = 0 num_error = 0 sim = Simulation("/home/kasun/new_ws/src/openbci/src/config.txt" ) # absolute path is better sleep(.2) subs = rospy.Subscriber('cmd', Action, callback) log = Sim() state = sim.get_state() log.state = state['agents'][0][0] sleep(1) dec = Bool() dec.data = True pubs.publish(log) pubs_clf.publish(dec) else: pass
def main(): sim = Simulation("config.txt") #sim.load_matrix_file("matrix.txt") --only include if a matrix file is used sim.load_slip_file("slip.txt") #turns slipping to 'on' state = sim.get_state() #grab state done = False while not done: done, state = sim.move(["keyboard"]) #main call print(sim.get_history(2)["agents"]) #get the history from 2 time steps back
# if step < len(action_stack): return action_stack[step] # else: # return 0 #with open('simulation.json') as f: # data = json.load(f) with open('GOAPvsADLnE.json') as f: data = json.load(f) nodes = data['nodes'] sim = Simulation('simulation.config') sim.draw() cur_state = sim.get_state() current_node = nodes['0'] a_state_pos = (cur_state['agents'][0]) o1_state_pos = (cur_state['moving_obstacles'][0]) o2_state = (cur_state['moving_obstacles'][1]) step = 0 while True: trans = current_node['trans'] index = str(random.choice(trans)) current_node = nodes[index] next_o1_state_pos = get_o1_state(current_node) next_o2_state_pos = get_o2_state(current_node) next_o3_state_pos = get_o3_state(current_node)
from environment import Simulation import pygame def make_move(): key_input = raw_input("What's your move? ") done, state = sim.move([key_input]) #main call def main(): done = False while not done: make_move() if __name__ == '__main__': sim = Simulation("config.txt") # absolute path is better main()
subs.unregister() trial += 1 time_steps = 0 num_error = 0 sim = Simulation("/home/kasun/new_ws/src/openbci/src/config.txt" ) # absolute path is better sleep(.2) subs = rospy.Subscriber('cmd', Action, callback) log = Sim() state = sim.get_state() log.state = state['agents'][0][0] sleep(1) dec = Bool() dec.data = True pubs.publish(log) pubs_clf.publish(dec) else: pass if __name__ == '__main__': time_steps = 0 num_error = 0 trial = 1 # sleep(20) print 'first' sim = Simulation("/home/kasun/new_ws/src/openbci/src/config.txt" ) # absolute path is better # sleep(10) main()
def __init__(self, configFile, matrixFile, staFile, traFile): Simulation.__init__(self, configFile, matrixFile) self.read_sta_file(staFile) self.read_tra_file(traFile)
return "south" elif next_pos[1] - cur_pos[1] == -1 and next_pos[0] - cur_pos[0] == 0: return "north" elif next_pos[1] - cur_pos[1] == 0 and next_pos[0] - cur_pos[0] == 0: return "stay" else: raise ValueError('Invalid input: cur_pos ' + str(cur_pos) + ', next_pos ' + str(next_pos)) with open('ctrl.json') as f: data = json.load(f) nodes = data['nodes'] sim = Simulation('ex.config') sim.draw() cur_state = sim.get_state() a_state = xy_to_state(cur_state['agents'][0]) o_state = xy_to_state(cur_state['moving_obstacles'][0]) current_node = nodes['130'] while True: trans = current_node['trans'] index = str(random.choice(trans)) current_node = nodes[index] next_o_state = get_o_state(current_node) next_a_state = get_a_state(current_node) a_state_pos = state_to_xy(a_state) o_state_pos = state_to_xy(o_state) next_a_state_pos = state_to_xy(next_a_state)
#!/usr/bin/env python from environment import Simulation from std_msgs.msg import String import pygame import rospy # This will be your subscriber node. # You will need to create a callback function. # You will need to move sim.move(command) inside the callback function. # command can only be one of the following "south", "north", "west", "east". # Remember to initiate a ROS node and subscribe to the topic /cmd. def callback(msg): done, state = sim.move([msg.data]) def main(): rospy.init_node('subs', anonymous=True) pub = rospy.Subscriber('cmd', String, callback) done = False while not done: pass if __name__ == '__main__': sim = Simulation("/home/sahabi/catkin_ws/src/wiald/src/config.txt") # absolute path is better main()
# return (time_step, spatial_step, collision, interrupt) if (a_state_pos == (2, 1)): f.close() return (time_step, spatial_step, collision, interrupt) collisions = 0 interrupts = 0 maximum_time_steps = 0 maximum_spatial_steps = 0 total_time_steps = 0 total_spatial_steps = 0 with open('GOAPvsADLnE.json') as f: data = json.load(f) nodes = data['nodes'] sim = Simulation('simulation.config') for i in range(1, 11): # collisions = collisions + simulate() result = simulate(sim, data, nodes) print i print result collisions = collisions + result[2] interrupts = interrupts + result[3] total_time_steps = total_time_steps + result[0] total_spatial_steps = total_spatial_steps + result[1] if (maximum_spatial_steps < result[1]): maximum_spatial_steps = result[1] if (maximum_time_steps < result[0]): maximum_time_steps = result[0]