def execute_turn(parameter, strategy, turns, duty_cycle): n_nodes = parameter.number_node trans_range = parameter.trans_range # meters speed_sink = parameter.speed_sink # meters/second size_area = parameter.size_area # area: meters² sink_route = list(parameter.sink_route) environment = Environment(n_nodes, trans_range, speed_sink, size_area, sink_route, strategy, duty_cycle) energy_begin = environment.energy_network_level() packets_transfer = 0 packets_lost = 0 nodes_founds = [] nodes_lost = [] clock = 0 for i in range(turns): while not environment.end_sink_turn(): pckt_trans, pckt_lost, n_founds, n_lost = environment.check_environment() packets_transfer += pckt_trans packets_lost += pckt_lost nodes_founds += n_founds nodes_lost += n_lost clock += 1 environment.define_sink_route(list(parameter.sink_route), parameter.speed_sink) nodes_founds = set(nodes_founds) nodes_lost = environment.nodes_lost() energy_end = environment.energy_network_level() environment.finalize() return [clock, packets_transfer, packets_lost, nodes_founds, n_nodes, nodes_lost, energy_begin, energy_end]
def play(self): environment = Environment(True, 4) while not environment.isTerminal(): state = environment.get_state() qval = self.net.predict(state.reshape(1, 4, 84, 84), batch_size=1) action = (np.argmax(qval)) reward = environment.act(action)
def __init__(self): self.p = Player(savedPlayerJSON) #self.updatePlayer(self.p,True,0,0) if self.p.isDead(): self.p = Player("./json/newPlayerTemplate.json") self.trend = getTrend() #print self.trend self.scen = Scenario() self.scen.loadFromFile(savedScenJSON) if self.scen.finished: option = getReplies(self.p.lastID) self.followUpTweet(option) if option == 1: item = self.scen.finish1[1] exp = self.scen.finish1[2] health = self.scen.finish1[3] elif option ==2: item = self.scen.finish2[1] exp = self.scen.finish2[2] health = self.scen.finish2[3] else: item = self.scen.finish3[1] exp = self.scen.finish3[2] health = self.scen.finish3[3] self.updatePlayer(self.p,item,exp,health) tempEnviro = Environment(self.trend) tempEnviro.generateScenario(self.trend).saveScenario() self.scen = loadScenFromFile(savedScenJSON)
class MainWindow(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.parent.title("GLIDE") self.welcomeScreen = WelcomeScreen(self.parent, self) # can create a StartPopupWindow x = (self.parent.winfo_screenwidth() - DIM_X)/2 y = (self.parent.winfo_screenheight() - DIM_Y)/2 self.parent.geometry('%dx%d+%d+%d' % (DIM_X, DIM_Y, x, y)) self.pack() self.envt = None self.parent.protocol("WM_DELETE_WINDOW", self.exit) self.canQuit = True def createNewEnvt(self, username): self.welcomeScreen.pack_forget() self.envt = Environment(self.parent, self, username) self.envt.pack() def exit(self): if self.canQuit: frame = Frame(bg="") frame.pack() popup = GlideChoose(self, "Are you sure you want to quit?", "Quit GLIDE", True) def kill(self): if self.envt != None: self.envt.setEndStatus(True) self.parent.destroy()
def visitScope(self, scope): environment = Environment() for identifier in scope.symbol_table: val = scope.symbol_table[identifier] if isinstance(val, Variable): box = val.st_visit(self) environment.insert(identifier, box) return environment
def main(): start_pos = Vec2(0.25,1.75) start_speed = Vec2(0.0,-0.1) start = RobotState(start_pos,start_speed,0.0) goal_pos = Vec2(2.75,1.75) goal_speed = Vec2(0.0,0.0) goal = RobotState(goal_pos,goal_speed,0.0) square_vertices = [Vec2(0,0),Vec2(0.5,0),Vec2(0.5,0.5),Vec2(0,0.5)] square = Polygon(square_vertices,True,True) obs1 = Affine.translation(Vec2(0.5,1.0)) * square obs2 = Affine.translation(Vec2(2.0,0.5)) * square env = Environment(Globals.PLAYGROUND_BORDER,[],[obs1,obs2]) #if poly_collides_poly(obs1,obs2): # print "(=" #else: # print ")=" #with PyCallGraph(output = GraphvizOutput()): path = aStar(start,goal,env) #print "start:\n {s}".format(s = start) #print "goal:\n {g}".format(g = goal) #for node in path: # print node while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() screen.fill(black) pygame.draw.lines(screen,white,True,convert_to_px_coords(env.border_as_tuple_list()),1) for obs in env.obstacles_as_tuples(): pygame.draw.lines(screen,red,True,convert_to_px_coords(obs),1) pygame.draw.lines(screen,green,False,convert_to_px_coords(Util.path_to_tuples(path)),1) for node in path: draw_state(node) pygame.display.update()
def hunt(predatorGenome, preyGenome, turnlimit, printing): env = Environment(predatorGenome, preyGenome, printing) capture = False turnCount = 0 while capture != True: if turnCount == turnlimit: break capture = env.turn() turnCount += 1 #time.sleep(10) if printing == True: pylab.show(block=True) return turnCount
def __init__( self): self.debug= False selfInterpreter= self self.readbuf= ConsSimple( nil, nil) self.macroBuf= nil self.env_globals= Environment() env_locals= self.env_globals.newChild() self.env_quotationMode_globals= Environment() env_quotationMode_locals= self.env_quotationMode_globals.newChild() codeListConfig= codeListConfigMake( env_locals, False, env_quotationMode_locals) # 27f019a4b85c45ec9dbab2a59306eccc self.codeList= codeListMake( codeListConfig, codeListParametersMake( nil)) # ehemals 76ff812ddb0f47f5be9ca34925a4a3b6 del codeListConfig codeListGetParameters( self.codeList).cdr( ConsSimple( Symbol( 'progn-root'), nil)) self.codeListCurrentEvalTokenList= codeListGetParameterValuesCdrContainer2( self.codeList).cdr() self.codeListParent2EvalToken= nil self.codeCurrentLeafList= ConsSimple( self.codeList, nil) # 2bb546ca3ffd48bc851d17494f5c286f codeListEnsure( self.codeList) assert( ar_codeListConfig==self.getCurrentCodeListConfig().car()) self.codeLeafBackTree= ConsSimple( self.codeCurrentLeafList, nil) # 2bb546ca3ffd48bc851d17494f5c286f self.codeLevel= 0 self.essentials_macro_function= Essentials() #for i in ( pl_q1, pl_q2, pl_ql, pl_pyprint, pl_pyprintconses, pl_pyprintconsesRest, pl_pyprintStack, pl_eval, pl_qswap2): # i.setEssentials( self.essentials_macro_function) self.interpreterSymbolTable= InterpreterSymbolTable( self) ist= self.interpreterSymbolTable for k in 'pyprint pyprintconses pyprintconsesRest pyprintStack pyprintStack2 pyprintStackShort'.split(): ist.htInterpreterSymbolTableNames[ k].setEssentials( self.essentials_macro_function) del k for k in ist.htInterpreterSymbolTable: v= ist.htInterpreterSymbolTable[ k] self.env_globals.set( k, v) del k, v del ist
def visit_scope(self, scope): """Create box for scope.""" env = Environment() for key in scope.table: val = scope.table[key] if not isinstance(val, Variable): # Do not store constants or types in env continue else: box = val.accept(self) env.insert(key, box) return env
def __init__(self, id, prediction_q, training_q, episode_log_q): super(ProcessAgent, self).__init__() self.id = id self.prediction_q = prediction_q self.training_q = training_q self.episode_log_q = episode_log_q self.env = Environment() self.num_actions = self.env.get_num_actions() self.discount_factor = Config.DISCOUNT # one frame at a time self.wait_q = Queue(maxsize=1) self.exit_flag = Value('i', 0)
def test_nb_timestep_overflow_disc0(self): # on this _grid, first line with id 5 is overheated, # it is disconnected # then powerline 16 have a relative flow of 1.5916318201096937 # in this scenario i don't have a second line disconnection. case_file = self.case_file env_params = copy.deepcopy(self.env_params) env_params.HARD_OVERFLOW_THRESHOLD = 1.5 env_params.NB_TIMESTEP_POWERFLOW_ALLOWED = 0 env = Environment(init_grid_path=os.path.join(self.path_matpower, case_file), backend=self.backend, chronics_handler=self.chronics_handler, parameters=env_params) self.backend.load_grid(self.path_matpower, case_file) thermal_limit = 10 * self.lines_flows_init thermal_limit[self.id_first_line_disco] = self.lines_flows_init[ self.id_first_line_disco] / 2 thermal_limit[self.id_2nd_line_disco] = 400 self.backend.set_thermal_limit(thermal_limit) disco, infos = self.backend.next_grid_state(env, is_dc=False) assert len( infos) == 2 # check that there is a cascading failure of length 2 assert disco[self.id_first_line_disco] assert disco[self.id_2nd_line_disco] assert np.sum(disco) == 2
def compile(self, source, env=None, library=False): if env == None: env = Environment() # parse input (definitions, errors) = Parser().parse(source) # check for errors if len(errors) > 0: return ("", errors) # type checking for definition in definitions: TypeChecker().check(errors, env, definition) # check for errors if len(errors) > 0: return ("", errors) # code generation code = "" generator = CodeGenerator() for node in definitions: symbol = node[0].value() code += generator.generateDefinition(env, symbol, template) # generate library if library: code += generator.generateLibrary(template) return (code, errors)
def __init__(self,dir): self.dir = dir self.learning_network = Network(49,128) self.learning_network.load_state_dict(torch.load(self.dir)) self.learning_network.eval() self.curr_network = Network(49, 128) self.curr_network.load_state_dict(torch.load(self.dir)) self.curr_network.eval() self.env = Environment() self.trajectory_size = 5000 self.trajectory = Trajectory(self.trajectory_size) self.optimizer = torch.optim.SGD(self.learning_network.parameters(), lr=1e-3,weight_decay=10e-4, momentum=0.9) self.tree = Tree(self.curr_network)
def simple(): env = Environment(food_is_right=1) a = Agent(env, T=1, food_is_right_prior=1, policy=[[2,2]], mode=MODE) for _ in range(EPISODES): a.episode(info=True)
def BFS(rootNode, goalState, path, queueSize): q = [] q.append(rootNode) #print(rootNode.env.n) #print(rootNode.env.print()) while (len(q) != 0): n = q.pop(0) h = hashState(n.env) #print(h) if h not in hashed: hashed.append(h) if (n.env.goalTest(goalState)): print("Goal state reached") p = n while (p.parent != None): path.append( (p.env.vacCleanerX, p.env.vacCleanerY, p.env.action)) p = p.parent return n for x in Actions: state = Environment.nextState(n.env, x) nextNode = Node(state, n) q.append(nextNode) # else: # print("Duplicate state. Ignored!") print("Queue Size: {}".format(len(q))) queueSize.append(len(q)) return None
def __init__(self, width=800, height=600): pygame.init() self.paused = True self.width = width self.height = height self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption('Darwin') # Make the environment and creatures self.environment = Environment(initialPopSize=POPULATION_SIZE, idealImagePath='./examples/eye_run/eye.png') # Set up the 'initial generation' surface self.initialGenerationSurface = pygame.Surface((POP_DIM*CREATURE_SPACING, POP_DIM*CREATURE_SPACING)) self.initialGenerationSurface.fill(IDEAL_COLOR) self.initialFont = pygame.font.Font(None, 24) self.initialText = self.initialFont.render("Generation 0", 1, (10, 10, 10)) self.initialCreatureSurfaceList = self.create_empty_surfaces() self.update_creatures(creatureSurfaceList=self.initialCreatureSurfaceList) # Set up the 'current population' surface self.populationSurface = pygame.Surface((POP_DIM*CREATURE_SPACING, POP_DIM*CREATURE_SPACING)) self.populationSurface.fill(IDEAL_COLOR) self.cycleFont = pygame.font.Font(None, 24) self.creatureSurfaceList = self.create_empty_surfaces() # The ideal creature self.idealCreatureSurface = pygame.Surface((CREATURE_WIDTH, CREATURE_HEIGHT)) pygame.surfarray.blit_array(self.idealCreatureSurface, self.environment.idealCreature.RGB) self.idealFont = pygame.font.Font(None, 24) self.idealText = self.idealFont.render("Ideal Creature", 1, (10, 10, 10))
def trainAgentOffline_random(agent, environmentParameters, trainingEpisodes): """train an agent and measure its performance""" # learn from episodes for run in range(trainingEpisodes): # select initial environment parameters initialParameters = random.choice(environmentParameters) experienceEpisode(agent, Environment(*initialParameters)) agent.wipeShortMemory() if run % 10 == 0: # train from replay memory allInput, allLabels = [], [] for shortMemory in agent.replayMemory: netInput, labels = agent.getSarsaLambda(shortMemory) allInput.append(netInput) allLabels.append(labels) allInput = torch.cat(allInput) allLabels = torch.cat(allLabels) agent.learn(allInput, allLabels) return agent
def main(): num_eps = 5000 num_runs = 10 random.seed(0) np.random.seed(0) agent = Agent() env = Environment() rlglue = RLGlue(env, agent) del agent, env for run in range(num_runs): rlglue.rl_init() performances = [] for ep in range(num_eps): rlglue.rl_start() #rlglue.rl_env_message('renderON') terminal = False while not terminal: reward, state, action, terminal = rlglue.rl_step() # Find the first policy that performs at 100% performance = testPolicy(rlglue.rl_agent_message('policy')) * 100 performances.append(performance) if performance >= 100: #print(rlglue.rl_agent_message('policy')) print('Episode: %d' % (ep + 1)) break plt.plot(performances) plt.savefig('test.png')
def run(self): # Randomly sleep up to 1 second. Helps agents boot smoothly. time.sleep(np.random.rand()) # Training isn't gonna be repeatable because of the parallelization seed = np.int32(Config.RANDOM_SEED_1000 * 1000 + self.id) np.random.seed(seed) self.env = Environment(self.id) while self.exit_flag.value == 0: total_reward = 0 total_length = 0 count = 0 if Config.EVALUATE_MODE: print("This code shouldn't be used to evaluate.") assert(0) else: for x_, r_, a_, reward_sum in self.run_episode(): if Config.DEBUG: print('[ DEBUG ] ProcessAgent::x_.shape is: {}'.format(x_.shape)) if len(x_.shape) > 1: total_reward += reward_sum total_length += len(r_) + 1 # +1 for last frame that we drop self.training_q.put((x_, r_, a_))# NOTE if Config::USE_AUDIO == False, audio_ is None else: print('[ DEBUG ] x_ has wrong shape of {}'.format(x_.shape)) import sys; sys.exit() self.episode_log_q.put((datetime.now(), total_reward, total_length))
def TD_lambda_forward(process: MDP, env: Environment, lambda_: float = 0.7, alpha: float = 0.01, n_iter: int = 1000): state_value = np.zeros(process.nb_states) for i in range(n_iter): states, actions, returns = env.generate_episode() ep_len = len(returns) for t in range(0, ep_len): current_state = states[t] cumulated_return = 0 G_t = 0 for n in range(t, ep_len): cumulated_return += process.disc_fact**(n - t) * returns[n] G_n = cumulated_return + process.disc_fact**( n + 1 - t) * state_value[states[n + 1].index] G_t += lambda_**(n - t) * G_n G_t *= (1 - lambda_) state_value[current_state.index] += alpha * ( G_t - state_value[current_state.index]) return state_value
def setup(): env = Environment() env.add_connections({0: range((NUM_AGENTS * NUM_COPIES + 3))}) env.add_dumb_loads(0, 100000) env.set_environment_ready() env.reset() # load_agent_dict = {0:QTableAgent(env.get_load_action_space(), # {LOAD_BATTERY_STATE:[0,100],LOAD_PRICE_STATE:env.get_price_bounds(0)}, # {LOAD_BATTERY_STATE:20, LOAD_PRICE_STATE:10}, # default_action=1, # discount_factor=DISCOUNT_FACTOR # )} # load_agent_dict[0].set_learning_rate(LEARNING_RATE) for i in load_agent_dict.keys(): load_agent_dict[i].set_explore_rate(0) return env
def call(self, args): global cur_env fun_env = Environment(glob_env) plen = len(self.params) alen = len(args) if alen == plen: tmp = cur_env cur_env = fun_env fi = 0 while fi < plen: param = self.params[fi] cur_env.define(param, args[fi]) strval = str(args[fi]) kobes.append( Kobe(param.text, strval, param.row_b, param.row_e, param.col_b, param.col_e, strval, None, param.text + " = " + strval)) fi += 1 try: for stmt in self.body: stmt.eval() except Return as r: cur_env = tmp return r.getValue() cur_env = tmp else: raise ExpectedArgCount(plen)
def compare_optimal_and_real_reward(step, loops, T): opt_reward = np.fromfile('reward_optimal_policy_' + str(step), sep=',') y = np.zeros((step + 1, step + 1)) step = (1 / step) param_grid = { 'p': np.arange(1 + step, step=step), 'r': np.arange(1 + step, step=step) } grid = ParameterGrid(param_grid) for params in grid: env = Environment(food_is_right=params['p'], hint_reliability=params['r']) a = Agent(env, T=T, food_is_right_prior=params['p'], reliability_prior=params['r']) true_reward = 0 for _ in range(loops): true_reward += a.episode()[0] a.reset(a.A, a.r) true_reward /= loops y[int(params['r'] * (1 / step)), int(params['p'] * (1 / step))] = true_reward return opt_reward - y
def test_nb_timestep_overflow_disc2(self): # on this _grid, first line with id 18 is overheated, # it is disconnected # then powerline 16 have a relative flow of 1.5916318201096937 # in this scenario I have a second disconnection, because the powerline is allowed to be on overflow for 2 # timestep and is still on overflow here. case_file = self.case_file env_params = copy.deepcopy(self.env_params) env_params.HARD_OVERFLOW_THRESHOLD = 1.5 env_params.NB_TIMESTEP_POWERFLOW_ALLOWED = 2 env = Environment(init_grid_path=os.path.join(self.path_matpower, case_file), backend=self.backend, chronics_handler=self.chronics_handler, parameters=env_params) self.backend.load_grid(self.path_matpower, case_file) env.timestep_overflow[self.id_2nd_line_disco] = 2 thermal_limit = 10*self.lines_flows_init thermal_limit[self.id_first_line_disco] = self.lines_flows_init[self.id_first_line_disco]/2 thermal_limit[self.id_2nd_line_disco] = 400 self.backend.set_thermal_limit(thermal_limit) disco, infos = self.backend.next_grid_state(env, is_dc=False) assert len(infos) == 2 # check that there is a cascading failure of length 2 assert disco[self.id_first_line_disco] assert disco[self.id_2nd_line_disco] assert np.sum(disco) == 2 for i, grid_tmp in enumerate(infos): assert (not grid_tmp.get_line_status()[self.id_first_line_disco]) if i == 1: assert (not grid_tmp.get_line_status()[self.id_2nd_line_disco])
def setUp(self): tfi = TextFileInterface(relative_directory="TestDB/") self.environment = Environment(tfi, DEBUG=True) self.environment.database.clear_database() self.environment.database.create_account("root", "root", "administrator") self.environment.database.create_course("361", "SoftwareEngineering")
def test_nb_timestep_overflow_nodisc_2(self): # on this _grid, first line with id 18 is overheated, # it is disconnected # then powerline 16 have a relative flow of 1.5916318201096937 # in this scenario i don't have a second line disconnection because # the overflow is a soft overflow and the powerline is presumably overflow since only 1 # timestep case_file = self.case_file env_params = copy.deepcopy(self.env_params) env_params.HARD_OVERFLOW_THRESHOLD = 1.5 env = Environment(init_grid_path=os.path.join(self.path_matpower, case_file), backend=self.backend, chronics_handler=self.chronics_handler, parameters=env_params) self.backend.load_grid(self.path_matpower, case_file) env.timestep_overflow[self.id_2nd_line_disco] = 1 thermal_limit = 10 * self.lines_flows_init thermal_limit[self.id_first_line_disco] = self.lines_flows_init[ self.id_first_line_disco] / 2 thermal_limit[self.id_2nd_line_disco] = 400 self.backend.set_thermal_limit(thermal_limit) disco, infos = self.backend.next_grid_state(env, is_dc=False) assert len(infos) == 1 # check that don't simulate a cascading failure assert disco[self.id_first_line_disco] assert np.sum(disco) == 1
def main(): num_eps = 200000 agent = Agent() env = Environment() rlglue = RLGlue(env, agent) del agent, env solves = 0 rlglue.rl_init() rewards = [] for ep in range(num_eps): rlglue.rl_start() #rlglue.rl_env_message('renderON') terminal = False reward = 0 while not terminal: reward, state, action, terminal = rlglue.rl_step() if ep > 1000: rlglue.rl_env_message('renderON') print(state) time.sleep(0.1) rewards.append(reward) if ep >= 99: if np.average(rewards[ep-99:ep+1]) > 0.78: print('solved at episode %d' % ep+1) break else: pass
def __init__(self, duration, dt, height, width, env_file): self._duration = duration self._dt = dt # Create the Canvas onto which everything will be rendered self._root = Tk() self._canvas = Canvas( self._root, width=width, height=height) self._canvas.grid(column=0, row=0, sticky=(N, W, E, S)) # Set the title, and make the Window un-sizable self._parent = self._root self._parent.title("Awesome Robot Simulation") self._parent.resizable(0, 0) # Determine the window position (centered nicely) self.center_window(height, width) # Read in the environment data self._env = Environment() self._env.set_dimensions(height, width) if env_file: self._env.read_env(env_file)
def __init__(self, n0): self.n0 = n0 self.states = [[State(i + 1, j + 1) for j in range(21)] for i in range(10)] self.state_win = State(0, 0, 1, "win") self.state_lose = State(0, 0, -1, "lose") self.state_draw = State(0, 0, 0, "draw") self.agent = { "win": self.state_win, "lose": self.state_lose, "draw": self.state_draw, "playing": self.states } self.environment = Environment(self.agent) self.state_track = [] self.action_track = []
def draw_level_in_pygame(): # Initialize Level global myEnvironment myEnvironment = Environment() global myLevel myLevel = initLevel(level_set, current_level)
def testPolicy(policy): agent = testAgent(policy) env = Environment() rlglue = RLGlue(env, agent) del env, agent rlglue.rl_init() # set up 2d array for average rewards # rewards[step] = sum of rewards across all runs for that step rewards = [0 for i in range(1000)] for run in range(1): rlglue.rl_init() #rlglue.rl_env_message('renderON') rlglue.rl_start() terminal = False for step in range(1000): if not terminal: r, s, a, terminal = rlglue.rl_step() rewards[step] += r # average rewards rewards = [i / 1 for i in rewards] return rewards
def setUp(self): # powergrid self.backend = PandaPowerBackend() self.path_matpower = PATH_DATA_TEST_PP self.case_file = "test_case14.json" # chronics self.path_chron = os.path.join(PATH_CHRONICS, "chronics") self.chronics_handler = ChronicsHandler(chronicsClass=GridStateFromFile, path=self.path_chron) self.tolvect = 1e-2 self.tol_one = 1e-5 self.id_chron_to_back_load = np.array([0, 1, 10, 2, 3, 4, 5, 6, 7, 8, 9]) # force the verbose backend self.backend.detailed_infos_for_cascading_failures = True self.names_chronics_to_backend = {"loads": {"2_C-10.61": 'load_1_0', "3_C151.15": 'load_2_1', "14_C63.6": 'load_13_2', "4_C-9.47": 'load_3_3', "5_C201.84": 'load_4_4', "6_C-6.27": 'load_5_5', "9_C130.49": 'load_8_6', "10_C228.66": 'load_9_7', "11_C-138.89": 'load_10_8', "12_C-27.88": 'load_11_9', "13_C-13.33": 'load_12_10'}, "lines": {'1_2_1': '0_1_0', '1_5_2': '0_4_1', '9_10_16': '8_9_2', '9_14_17': '8_13_3', '10_11_18': '9_10_4', '12_13_19': '11_12_5', '13_14_20': '12_13_6', '2_3_3': '1_2_7', '2_4_4': '1_3_8', '2_5_5': '1_4_9', '3_4_6': '2_3_10', '4_5_7': '3_4_11', '6_11_11': '5_10_12', '6_12_12': '5_11_13', '6_13_13': '5_12_14', '4_7_8': '3_6_15', '4_9_9': '3_8_16', '5_6_10': '4_5_17', '7_8_14': '6_7_18', '7_9_15': '6_8_19'}, "prods": {"1_G137.1": 'gen_0_4', "3_G36.31": "gen_2_1", "6_G63.29": "gen_5_2", "2_G-56.47": "gen_1_0", "8_G40.43": "gen_7_3"}, } # _parameters for the environment self.env_params = Parameters() self.env = Environment(init_grid_path=os.path.join(self.path_matpower, self.case_file), backend=self.backend, chronics_handler=self.chronics_handler, parameters=self.env_params, names_chronics_to_backend=self.names_chronics_to_backend, actionClass=Action) self.array_double_dispatch = np.array([0., 12.41833569, 10.89081339, 0., -23.30914908])
def dcmotor (learning_method, number_of_rollouts, simulation_steps, learning_eposides, critic_structure, actor_structure, train_dir,\ nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100): A = np.matrix([[0.98965, 1.4747e-08], [7.4506e-09, 0]]) B = np.matrix([[128], [0]]) #intial state space s_min = np.array([[-1.0], [-1.0]]) s_max = np.array([[1.0], [1.0]]) #sample an initial condition for system x0 = np.matrix([ [random.uniform(s_min[0, 0], s_max[0, 0])], [random.uniform(s_min[1, 0], s_max[1, 0])], ]) print("Sampled initial state is:\n {}".format(x0)) Q = np.matrix("1 0 ; 0 1") R = np.matrix(".0005") x_min = np.array([[-1.5], [-1.5]]) x_max = np.array([[1.5], [1.5]]) u_min = np.array([[-1.]]) u_max = np.array([[1.]]) env = Environment(A, B, u_min, u_max, s_min, s_max, x_min, x_max, Q, R) args = { 'actor_lr': 0.000001, 'critic_lr': 0.00001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 100, 'max_episodes': learning_eposides, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir + "model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 5000 } actor = DDPG(env, args) #################### Shield ################# model_path = os.path.split(args['model_path'])[0] + '/' linear_func_model_name = 'K.model' model_path = model_path + linear_func_model_name + '.npy' shield = Shield(env, actor, model_path, force_learning=retrain_shield) shield.train_shield(learning_method, number_of_rollouts, simulation_steps, explore_mag=0.0004, step_size=0.0005) if shield_test: shield.test_shield(test_episodes, 5000, mode="single")
def simulate_predictions(self, phenome, num_trials): from nllsCuda import pyPredict, pyQxy, pyMultipleFitness from Environment import Environment env = Environment((self.emitter_x, self.emitter_y), 10.0, self.noise_stddev) samples = [] for trial in xrange(num_trials): t = [] for position in phenome.get_position(): x, y = position t.append((x, y, env.MeasuredPower(x, y))) samples.append(t) results = pyPredict(samples, [512, 512], (self.grid_x, self.grid_y)) return results
def main(): # Seed rng's for consistent testing random.seed(0) np.random.seed(0) # Generate agent, environment and RLGlue env = Environment() agent = Agent(env.get_actions()) rlglue = RLGlue(env, agent) del agent, env # Get generated policy policy = pickle.load(open('policy.pickle', 'rb')) # Test policy result = testPolicy(policy) print('result:', result)
def __init__(self, name, iterations): self._env = Environment.Environment() self._env.init() self._iter = self._env.stateFiles()[-1].iteration() self._output = False self._variables = {} self._name = name self._iterations = iterations
def get_data_file(self, filename): '''Return the point where this data file will end up!''' if not filename in self._data_files: return filename data_directory = Environment.generate_directory('DataFiles') return os.path.join(data_directory, os.path.split(filename)[-1])
def __init__(self, sess): print("Initializing the agent...") self.sess = sess self.env = Environment() self.state_size = self.env.get_state_size()[0] self.action_size = self.env.get_action_size() self.low_bound, self.high_bound = self.env.get_bounds() self.buffer = ExperienceBuffer() print("Creation of the actor-critic network") self.network = Network(self.state_size, self.action_size, self.low_bound, self.high_bound) self.sess.run(tf.global_variables_initializer()) DISPLAYER.reset()
def visit_Block(self, stmt): ''' Each block AST will get it's own Environment for its set of statements. We pass in the current self.environment as the enclosing environment for our new environment dedicated for this block. ''' assert isinstance(stmt, Stmt.Block), "must be of type Block Statement" self.execute_block(stmt.statements, Environment(self.environment))
def __init__(self, sess, n_agent, gui, displayer, buffer): print("Initializing agent %i..." % n_agent) self.n_agent = n_agent self.sess = sess self.gui = gui self.displayer = displayer self.buffer = buffer self.env = Environment() self.build_actor() self.build_update() self.create_summaries() print("Agent initialized !\n")
def train_sarsaLApprox_agent(n_iters, lam, record_history=False): # Create feature vector agent_features = [ range(1, 7), range(4, 10), range(7, 13), range(10, 16), range(13, 19), range(16, 22) ] dealer_features = [range(1, 5), range(4, 8), range(7, 11)] # Must pass agent features first since agent hand is first in state agent_feature_vector = FeatureVector(agent_features, dealer_features) # initialise sarsa agent sarsa_approx_agent = sarsaLApprox(agent_feature_vector, lam, gamma=1, n0=10) # Train agent for i in range(n_iters): # initialise the environment card_table = Environment() sarsa_approx_agent.init_etrace() sarsa_approx_agent.init_etrace_log() # game ends when terminal state is reached while card_table.is_state_terminal == False: s = card_table.state # agent takes action, gets reward a = sarsa_approx_agent.choose_action(s) s_, r = card_table.step(a) sarsa_approx_agent.update_value_function(s, a, r, s_) if record_history: sarsa_approx_agent.log_weights() # Return the trained agent return sarsa_approx_agent
def step(self, action): # This call updates self.step as well state, reward, done = Environment.step(self, action) # Check whether agent is done done = (state == self.absorbing_state) or done return state, reward, done
def write_cif(self): '''Write CIF to file.''' # update audit information for citations self.collate_audit_information() from Environment import Environment data_directory = Environment.generate_directory('DataFiles') with open(os.path.join(data_directory, self._outfile), 'w') as fh: self._cif.show(out=fh)
def main(): c1 = Circle(Vec2D(1.5,1),0.3) c2 = Circle(Vec2D(0.4,1.3),0.3) c3 = Circle(Vec2D(0.2,0.22),0.1) c4 = Circle(Vec2D(0.4,0.6),0.2) circles = [c1,c2,c3,c4] env = Environment([Obstacle(c1,Vec2D(-0.0001,0)),Obstacle(c2,Vec2D(0,-0.0001)),Obstacle(c3,Vec2D(0.0001,0)),Obstacle(c4,Vec2D(0,0.0001))]) #env = Environment([Obstacle(c1,Vec2D(0.0000,0.0001)),Obstacle(c4,Vec2D(0,-0.0001))]) #env = Environment([Obstacle(c1,Vec2D(0,-0.0001))]) start = Vec2D(0.01,0.01) end = Vec2D(2.99,1.99) path = env.path(start,end) clock = pygame.time.Clock() paused = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: paused = not paused if not paused: screen.fill(black) env.update() draw_env(env,start,end) draw_path(env.path(start,end)) #print clock.tick() pygame.display.update()
def __init__(self, from_fair_to_biased_probability, from_biased_to_fair_probability, fair_dice, biased_dice, number_of_throws): self.environment = Environment(from_fair_to_biased_probability, from_biased_to_fair_probability, number_of_throws) logger.info("Created {}".format(self.environment)) self.generator = Casino(from_fair_to_biased_probability, from_biased_to_fair_probability, fair_dice, biased_dice) logger.info("Created {}".format(self.generator))
def __init__(self): self.net = None self.env = Environment(False, 4) self.mem = Memory(32, 1000000) self.epsilon = 0.5 self.gamma = 0.7 self.number_of_actions = 4 try: self.load_network() except IOError: print 'No network found' self.create_model()
def __init__( self, size = (11, 11), predators = 1, episodes = 50000 ): ''' Constructor for setting up the GUI ''' pygame.init() # Clock init self.clock = pygame.time.Clock() # Environment print "Initializing the agents and environment, performing training." self.E = Environment( numberOfPredators=predators ) self.episodes = episodes # Setup the main screen self.size = size self.offset = 100 self.half_offset = 50 self.resolution = ( self.offset + size[0] * 50, \ self.offset + size[1] * 50 ) self.screen = pygame.display.set_mode( self.resolution ) pygame.display.set_caption( 'Autonomous Agents: Predators vs. Prey' ) # Setup the background self.background = pygame.Surface( self.screen.get_size() ) self.background = self.background.convert() self.background.fill( (255, 255, 255) ) self.__drawBoard() # Prey sprite self.Prey = pygame.image.load( "../../images/prey.png" ).convert() self.Prey_rect = self.Prey.get_rect() x, y = self.E.Prey.location self.Prey_rect.left = (self.half_offset) + (x * 51) + 1 self.Prey_rect.top = (self.half_offset) + (y * 51) + 1 self.Predators = list() self.Predators_rect = list() # Predator sprite for i in range(self.E.numberOfPredators): self.Predators.append( pygame.image.load( "../../images/predator.png" ).convert() ) x, y = self.E.Predators[i].location self.Predators_rect.append( self.Predators[i].get_rect() ) self.Predators_rect[i].left = (self.half_offset) + (x * 51) + 1 self.Predators_rect[i].top = (self.half_offset) + (y * 51) + 1 # Setup music pygame.mixer.music.load( "../../music/BennyHillShow.mp3" ) # Setup screen self.__update()
def runProto(self, proto): defFile = proto modelFile, iter = self._env.getModelFile(self._iter) print 'testing for iteration %d ...' % self._iter if self._output: dir = 'output_%s_%d' % (self._name, self._iter) tb.system('mkdir -p %s' % dir) self._variables['TEST_OUTPUT'] = 1 self._variables['TEST_OUTPUT_DIR'] = '"\\"%s\\""' % dir self._env.makeScratchDir() defPrototxt = self._env.prototxt(defFile, 'scratch', self._variables) print defFile, defPrototxt tb.system('%s test -weights %s -model %s -gpu 0 -iterations %d 2>&1' % (Environment.caffeBin(), modelFile, defPrototxt, self._iterations))
def __init__(self, id, prediction_q, training_q, episode_log_q): super(ProcessAgent, self).__init__() self.id = id self.prediction_q = prediction_q self.training_q = training_q self.episode_log_q = episode_log_q self.env = Environment() self.num_actions = self.env.get_num_actions() self.actions = np.arange(self.num_actions) self.discount_factor = Config.DISCOUNT # one frame at a time self.wait_q = Queue(maxsize=1) self.exit_flag = Value('i', 0)
def __init__(self): self.commands = { ":type": self.printType, ":list": self.printList, ":show": self.printDefinition, ":code": self.printGeneratedCode, ":string": self.setString, ":help": lambda x: None, "def": self.addDefinition, } self.printString = False self.env = Environment() self.checker = TypeChecker() self.ghci = GHCI() # imports self.ghci.execute("import Data.Char")
def __init__(self, n0): self.n0 = n0 self.states = [[State(i+1, j+1, 0, 'draw') for j in range(10)] for i in range(21)] self.state_win = State(0, 0, 1, 'win') self.state_lose = State(0, 0, -1, 'lose') self.state_tie = State(0, 0, 0, 'tie') self.agents = {'draw': self.states, 'win': self.state_win, 'lose': self.state_lose, 'tie': self.state_tie} self.environment = Environment(self.agents) self.current_state = self.states[random.randint(0,9)][random.randint(0,9)] self.states_action_num = {} self.states_action_return = {} #self.weight = np.random.uniform(-1, 1, size=36) self.weight = np.zeros(36) for i in range(21): for j in range(10): self.states_action_num[self.states[i][j]] = {'hit': 0, 'stick': 0} self.states_action_return[self.states[i][j]] = {'hit': 0, 'stick': 0} self.states_track = []
class Q: def __init__(self): self.net = None self.env = Environment(False, 4) self.mem = Memory(32, 1000000) self.epsilon = 0.5 self.gamma = 0.7 self.number_of_actions = 4 try: self.load_network() except IOError: print 'No network found' self.create_model() def create_model(self): print 'Creating model...' model = Sequential() model.add( Convolution2D(32, 8, 8, subsample=(4, 4), activation='relu', input_shape=(4, 84, 84))) model.add(Convolution2D(64, 4, 4, activation='relu', subsample=(2, 2))) model.add(Convolution2D(64, 3, 3, activation='relu', subsample=(1, 1))) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dense(self.number_of_actions, activation='linear')) model.compile(loss='mse', optimizer='rmsprop') self.net = model print 'Done!' def save_network(self): json_string = self.net.to_json() open('deep_q_network.json', 'w').write(json_string) self.net.save_weights('network_weights.h5', overwrite=True) def load_network(self): print 'Loading network...' model = model_from_json(open('deep_q_network.json').read()) model.load_weights('network_weights.h5') model.compile(loss='mse', optimizer='rmsprop') print 'Network loaded!' self.net = model def train(self, epochs): for i in xrange(epochs): state = self.env.get_state() while not self.env.isTerminal(): qval = self.net.predict(state.reshape(1, 4, 84, 84), batch_size=1) if random.random() < self.epsilon: # choose random action action = np.random.randint(0, self.number_of_actions) else: # choose best action from Q(s,a) values action = np.argmax(qval) # Take action, observe new state S' reward = self.env.act(action) new_state = self.env.get_state() # Experience replay storage is_terminal = self.env.isTerminal() self.mem.store(state, action, reward, new_state, is_terminal) print 'Game : {}'.format(i) if self.mem.isFull(): minibatch = self.mem.sample() self.train_on_minibatch(minibatch) state = new_state if self.epsilon > 0.1: # decrement epsilon over time self.epsilon -= (1 / 100000) self.env.restart() if i % 10 == 0: self.save_network() def train_on_minibatch(self, minibatch): x_train, y_train = [], [] for sample in minibatch: # Get max_Q(S',a) old_state, action, reward, new_state, terminal = sample old_qval = self.net.predict(old_state.reshape(1, 4, 84, 84), batch_size=1) newQ = self.net.predict(new_state.reshape(1, 4, 84, 84), batch_size=1) maxQ = np.max(newQ) y = np.zeros((1, self.number_of_actions)) y[:] = old_qval[:] if not terminal: # non-terminal state update = (reward + (self.gamma * maxQ)) else: # terminal state update = reward y[0][action] = update x_train.append(old_state.reshape(4, 84, 84)) y_train.append(y.reshape(self.number_of_actions, )) x_train = np.array(x_train) y_train = np.array(y_train) self.net.fit(x_train, y_train, batch_size=self.mem.batch_size, nb_epoch=1) def play(self): environment = Environment(True, 4) while not environment.isTerminal(): state = environment.get_state() qval = self.net.predict(state.reshape(1, 4, 84, 84), batch_size=1) action = (np.argmax(qval)) reward = environment.act(action)
class Simulation(object): # Constructor # @param duration Duration (in seconds) of the Simulation # @param dt Time step # @param height Height of the window # @param width Width of the window # @param env_file Path to the file containing Environment data def __init__(self, duration, dt, height, width, env_file): self._duration = duration self._dt = dt # Create the Canvas onto which everything will be rendered self._root = Tk() self._canvas = Canvas( self._root, width=width, height=height) self._canvas.grid(column=0, row=0, sticky=(N, W, E, S)) # Set the title, and make the Window un-sizable self._parent = self._root self._parent.title("Awesome Robot Simulation") self._parent.resizable(0, 0) # Determine the window position (centered nicely) self.center_window(height, width) # Read in the environment data self._env = Environment() self._env.set_dimensions(height, width) if env_file: self._env.read_env(env_file) # Center the window on the screen # @param wh Height of the window to center # @param ww Width of the window to center def center_window(self, wh, ww): sw = self._parent.winfo_screenwidth() sh = self._parent.winfo_screenheight() x, y = (sw - ww) / 2, (sh - wh) / 2 self._parent.geometry('%dx%d+%d+%d' % (ww, wh, x ,y)) # Start the event loop def start_event_loop(self): self.timer_triggered() self._parent.mainloop() # When the timer goes off, update and redraw everything and reset the timer def timer_triggered(self): self.update() self.redraw() self._canvas.after(self._dt, self.timer_triggered) # Update the simulation for the latest time step def update(self): sleep(0.05) # sleep so the animation doesn't finish too quickly self._duration -= self._dt; if not self._duration > 0: raw_input("Press <Enter> to stop the simulation...") # Print the results print for robot in self._env.robots(): M, N = robot.slam().M(), robot.slam().N() for i in range(N): computed_landmark_pos = np.matrix([0.0, 0.0]).T for k in range(M): computed_landmark_pos += robot.slam().particles()[k].features()[i][0] computed_landmark_pos /= M computed_landmark_x = computed_landmark_pos.item(0) computed_landmark_y = computed_landmark_pos.item(1) (actual_landmark_x, actual_landmark_y) = self._env.landmarks()[i].pos() print "Landmark %d => actual: (%f, %f), computed: (%f, %f)" % \ (i, actual_landmark_x, actual_landmark_y, computed_landmark_x, computed_landmark_y) print # TODO --- maybe just take the particle with the highest weight (maybe top 2 or 3)? [same for landmarks] actual_robot_x, actual_robot_y, actual_robot_theta = robot.pose() computed_robot_x, computed_robot_y, computed_robot_theta = (0.0, 0.0, 0.0) for k in range(M): (p_x, p_y, p_theta) = robot.slam().particles()[k].pose() computed_robot_x += p_x computed_robot_y += p_y computed_robot_theta += p_theta print "Robot pose => actual: (%f, %f, %f), computed: (%f, %f, %f)" % \ (actual_robot_x, actual_robot_y, actual_robot_theta, computed_robot_x / M, computed_robot_y / M, computed_robot_theta / M) exit() # Update all of the robots updated_robots = [] for robot in self._env.robots(): updated_robots.append(robot.update(self._dt)) self._env._robots = updated_robots # Redraw the canvas def redraw(self): self._canvas.delete(ALL) for wall in self._env.walls(): wall.draw(self._canvas) for landmark in self._env.landmarks(): landmark.draw(self._canvas) for robot in self._env.robots(): robot.draw(self._canvas) # Start the simulation def start(self): self.start_event_loop()
print "using caffe module from: %s" % caffe.__file__ print "using caffe._caffe module from: %s" % caffe._caffe.__file__ ldd = tb.run("ldd %s" % caffe._caffe.__file__) caffeLib = None for line in ldd.split("\n"): match = re.match("\\s*libcaffe.so => (.*\.so)", line) if match: caffeLib = match.group(1) break if caffeLib is None: raise Exception("cannot find libcaffe.so dependency") print "using caffe from %s" % caffeLib env = Environment(args.path, backend, args.unattended, args.silent) if args.command != "copy" and args.command != "compare": env.init() def checkJob(): currentId = "%s/current_id" % env.jobDir() if not os.path.exists(currentId): raise Exception("cannot find %s, no job seems to be running." % currentId) return open(currentId).read().strip() def checkNoJob(): currentId = "%s/current_id" % env.jobDir() if os.path.exists(currentId): raise Exception("%s exists, there seems to be a job running" % currentId)
def __init__(self, main, ad, influence, recommendation, input, graph, env, people): self.mainConfig = main self.influenceConfig = influence self.adConfig = ad self.recommendationConfig = recommendation self.inputConfig = input self.graphConfig = graph self.envConfig = env self.graph = people self.environment = Environment(self.graph, self.envConfig) pygame.init() surface = pygame.display.set_mode((1920, 1080)) pygame.display.set_caption("User Interactions") clock = pygame.time.Clock() self.graphDrawrer = GraphDrawrer(self.graph, self.mainConfig, self.influenceConfig, self.adConfig, self.recommendationConfig, self.graphConfig, self.envConfig) counter = 0 record = {} if self.mainConfig.influence and self.influenceConfig.type == "1": influence = Influence1(self.graph) influence.initialiseGraph() influence.startingNodes(int(self.influenceConfig.seed)) if self.mainConfig.influence and self.influenceConfig.type == "2": influence = Influence2(self.graph) influence.initialiseGraph() influence.startingNodes(int(self.influenceConfig.seed)) if self.mainConfig.recommendation: recommendation = Recommendation(self.graph) recommendation.intialise(self.recommendationConfig.seed) for person in self.graph.people: person.item1 = recommendation.getRecommendation(person, "item1") for person in self.graph.people: person.item2 = recommendation.getRecommendation(person, "item2") for person in self.graph.people: person.item3 = recommendation.getRecommendation(person, "item3") if self.mainConfig.social: social = Social(self.graph, self.graphDrawrer) if int(self.inputConfig.cull) < len(self.graph.connections): self.graph.trim(int(self.inputConfig.cull)) for n in range(0,10): for connection in self.graph.connections: if not (connection.between[0] in self.graph.people): self.graph.connections.remove(connection) for connection in self.graph.connections: if not (connection.between[1] in self.graph.people): self.graph.connections.remove(connection) # LineIntersection(self.graph) for n in range(0, 20): Layout(self.graph).force_directed() if self.mainConfig.second: graph = pygame.transform.scale(self.graphDrawrer.make_frame(True), (960 , 1080)) env = pygame.transform.scale(self.environment.make_frame(), (960, 1080)) surface.blit(graph, (0, 0)) surface.blit(env, (960, 0)) else: surface.blit(self.graphDrawrer.make_frame(True), (0,0)) pygame.display.update() clock.tick(60) # LineIntersection(self.graph) if self.mainConfig.advert: viral = Viral(self.graph, float(self.adConfig.seed), float(self.adConfig.email), float(self.adConfig.ad), float(self.adConfig.mu), float(self.adConfig.sigma)) for person in self.graph.people: person.totalPart = 0 viral.makeCampaign(self.graph) viral.seedEmail(self.graph, 5, 0) viral.seedAdvert(self.graph, 0.1, 0) record[counter] = viral.record(self.graph, 0) while self.graphDrawrer.go: counter += 1 if self.mainConfig.social: if (counter%60 == 0): for person in self.graph.people: social.checkWall(person) if random.random() < 0.1: social.postPhoto(person) if random.random() < 0.1: social.postStatus(person) if self.mainConfig.second: graph = pygame.transform.scale(self.graphDrawrer.make_frame(), (960 , 1080)) env = pygame.transform.scale(self.environment.make_frame(), (960, 1080)) surface.blit(graph, (0, 0)) surface.blit(env, (960, 0)) else: surface.blit(self.graphDrawrer.make_frame(), (0,0)) pygame.display.update() clock.tick(60) if counter % 5 == 0: if self.mainConfig.advert: viral.checkEmail(self.graph, 0) record[counter] = viral.record(self.graph, 0) if self.mainConfig.influence: influence.process(counter) Plotter(record)
# Create automatas automata_list = [Automata(states) for x in range(num_automata)] result = {} for x in range(times): for x in range(iterations): # Count yes for each of the automatas yes = 0 for automata in automata_list: yes = yes + 1 if automata.isYes() else yes # Create environment and calculate probability env = Environment(yes, len(automata_list)) env.probability() # Determine weither to punish or reward automatas for automata in automata_list: automata.reward() if env.doReward() else automata.punish() # Generate key for Dict result (IE: 3/2) result_key = "{0}/{1}".format(yes, len(automata_list) - yes) # Ensure that the key exist, init if not. result[result_key] = 1 if result_key not in result else result[result_key] + 1 out = "" for key, value in sorted(result.items(), key=operator.itemgetter(1), reverse=True): out = out + "[" + (key+ ": " + str(value)) + "]\t"
ui.setStartListener(start.start) ui.setStopListener(start.stop) ui.setSaveHandler(lambda : Save(agentList[0])) ui.setLoadHandler(lambda : Load(agentList)) ui.setPlayerHandler( dict( SARSA= (lambda : ChangeToSARSA(agentList, start)), Human= (lambda : ChangeToHuman(agentList, start)), Q_Learning= (lambda : True) )) episodeNum = 0 stepNum = 0 while 1: agent = agentList[0] env = Environment(gameScreenSize, discrete_size) state = env.start() if start.playerType == 'Human': action = 0 else: action = agent.start(state) episodeNum += 1 start.isReset = False while 1: #remove me agent = agentList[0] #update game status here form['stepLabel'].value = stepNum
def createNewEnvt(self, username): self.welcomeScreen.pack_forget() self.envt = Environment(self.parent, self, username) self.envt.pack()
class Interface(): ''' Graphical Interface for displaying the environment ''' # Constructor def __init__( self, size = (11, 11), predators = 1, episodes = 50000 ): ''' Constructor for setting up the GUI ''' pygame.init() # Clock init self.clock = pygame.time.Clock() # Environment print "Initializing the agents and environment, performing training." self.E = Environment( numberOfPredators=predators ) self.episodes = episodes # Setup the main screen self.size = size self.offset = 100 self.half_offset = 50 self.resolution = ( self.offset + size[0] * 50, \ self.offset + size[1] * 50 ) self.screen = pygame.display.set_mode( self.resolution ) pygame.display.set_caption( 'Autonomous Agents: Predators vs. Prey' ) # Setup the background self.background = pygame.Surface( self.screen.get_size() ) self.background = self.background.convert() self.background.fill( (255, 255, 255) ) self.__drawBoard() # Prey sprite self.Prey = pygame.image.load( "../../images/prey.png" ).convert() self.Prey_rect = self.Prey.get_rect() x, y = self.E.Prey.location self.Prey_rect.left = (self.half_offset) + (x * 51) + 1 self.Prey_rect.top = (self.half_offset) + (y * 51) + 1 self.Predators = list() self.Predators_rect = list() # Predator sprite for i in range(self.E.numberOfPredators): self.Predators.append( pygame.image.load( "../../images/predator.png" ).convert() ) x, y = self.E.Predators[i].location self.Predators_rect.append( self.Predators[i].get_rect() ) self.Predators_rect[i].left = (self.half_offset) + (x * 51) + 1 self.Predators_rect[i].top = (self.half_offset) + (y * 51) + 1 # Setup music pygame.mixer.music.load( "../../music/BennyHillShow.mp3" ) # Setup screen self.__update() def __del__( self ): pygame.quit() def __drawBoard( self ): ''' Draws the board ''' for i in range( self.size[0] + 1 ): pygame.draw.line( self.background, (0, 0, 0), ( self.half_offset, self.half_offset + i * 51), ( self.half_offset + self.size[1] * 51, self.half_offset + i * 51) ) for i in range( self.size[1] + 1 ): pygame.draw.line( self.background, (0, 0, 0), ( self.half_offset + i * 51, self.half_offset), ( self.offset /2 + i * 51, self.half_offset + self.size[0] * 51) ) def __update( self ): ''' Updates the location of the predator and the prey on the screen ''' # Create new frame frame = self.background.copy() frame.blit( self.Prey, self.Prey_rect ) for i in range( self.E.numberOfPredators ): frame.blit( self.Predators[i], self.Predators_rect[i] ) # Display frame self.screen.blit( frame, (0, 0) ) pygame.display.flip() def setPredator( self, i, location ): ''' Sets the predator location on the screen ''' self.Predators_rect[i].left = (self.half_offset) + ( (location[0] % self.size[0] ) * 51) + 1 self.Predators_rect[i].top = (self.half_offset) + ( (location[1] % self.size[1] ) * 51) + 1 def setPrey( self, location ): ''' Sets the prey location on the screen ''' self.Prey_rect.left = (self.half_offset) + ( (location[0] % self.size[0]) * 51) + 1 self.Prey_rect.top = (self.half_offset) + ( (location[1] % self.size[1]) * 51) + 1 def run( self ): ''' Updates the screen and checks for quit events ''' done = False running = True start = True self.E.qLearning( self.episodes, verbose=True ) self.E.resetAgents() pygame.mixer.music.play(-1) print "Start simulation" while not(done): #pygame.image.save( self.screen, "frame{0}.jpg".format( frame ) ) #frame += 1 # Run a step if running: self.E.simulateEnvironment() for event in pygame.event.get(): if event.type == pygame.QUIT: done = True break elif event.type == pygame.KEYDOWN and event.key == pygame.K_r: self.E.resetAgents() running = True for i in range( self.E.numberOfPredators ): self.setPredator( i, self.E.Predators[i].location ) self.setPrey( self.E.Prey.location ) self.__update() self.clock.tick(5) for i in range( self.E.numberOfPredators ): if self.E.Predators[i].location == self.E.Prey.location: running = False
class Interpreter: def __init__(self): self.commands = { ":type": self.printType, ":list": self.printList, ":show": self.printDefinition, ":code": self.printGeneratedCode, ":string": self.setString, ":help": lambda x: None, "def": self.addDefinition, } self.printString = False self.env = Environment() self.checker = TypeChecker() self.ghci = GHCI() # imports self.ghci.execute("import Data.Char") def interpret(self): try: self.printInfo() self.printLineInput() line = self.readLine() while line: try: self.execute(line) except TypeError as e: print(str(e)) self.printLineInput() line = self.readLine() print() finally: self.ghci.close() def printInfo(self): print("Commandline interpreter for zlang") print("Copyright 2015 by Roger Knecht") def printLineInput(self): print("zlang> ", end="", flush=True) def readLine(self): input = "" for line in stdin: if line[:-1].endswith("\\"): input += line[:-2] self.printLineInput() else: input += line return input def execute(self, line): tokens = line.strip().split(" ") tokens = list(filter(lambda x: len(x) > 0, tokens)) if len(tokens) == 0: return cmd = tokens[0] if cmd in self.commands: self.commands[cmd](line) else: self.printCall(line) def printType(self, line): tokens = line.strip().split(" ") if len(tokens) < 2: print("No symbol specified") for symbol in tokens[1:]: if symbol in self.env.elements: (type, node, local) = self.env.get(symbol) print(symbol + " :: " + str(type)) else: print(symbol + " :: Symbol does not exists") def printList(self, line): for name, (type, node, local) in self.env.elements.items(): print(name + " :: " + str(type)) sleep(0.05) def printDefinition(self, line): tokens = line.strip().split(" ") if len(tokens) < 2: print("No symbol specified") for symbol in tokens[1:]: if symbol in self.env.elements: (type, node, local) = self.env.get(symbol) print(str(node)) else: print(symbol + " :: Symbol does not exists") def printGeneratedCode(self, line): tokens = line.strip().split(" ") if len(tokens) < 2: print("No symbol specified") for symbol in tokens[1:]: if not symbol in self.env.elements: print("No symbol " + symbol) continue # code generation generator = CodeGenerator() code = generator.generateDefinition(self.env, symbol, template) if code == None: print("Native function") else: print(code.strip()) def addDefinition(self, line): (code, errors) = Compiler().compile(line, self.env) # check for errors if len(errors) > 0: for error in errors: print(error) return code = ":{\nlet\n" + code.strip() + "\n:}\n" self.ghci.execute(code) def printCall(self, line): (code, errors) = Compiler().compile("def v = " + line, self.env) # check for errors if len(errors) > 0: for error in errors: print(error) return if "v" in self.env.elements: (type, node, local) = self.env.get("v") if self.printString and str(type) == "[Int]": code = ":{\nlet\n" + code.strip() + "\n:}\nmap (\\x -> chr x) z_v\n" else: code = ":{\nlet\n" + code.strip() + "\n:}\nz_v\n" if self.ghci.evaluate(code): print(" :: " + str(type)) else: print("Abort. Evaluation took to long") def loadNative(self, path): native = open(path, "r") code = native.read() native.close() out = "" for definition in code.strip().split("\n\n"): out += ":{\nlet\n" + definition.strip() + "\n:}\n" self.ghci.execute(out) def loadModule(self, path): module = open(path, "r") (code, errors) = Compiler().compile(module.read(), self.env) module.close() # check for errors if len(errors) > 0: for error in errors: print(error) return out = "" for definition in code.strip().split("\n\n"): out += ":{\nlet\n" + definition.strip() + "\n:}\n" self.ghci.execute(out) def setString(self, line): tokens = line.strip().split(" ") if len(tokens) > 1: if tokens[1] == "on": self.printString = True elif tokens[1] == "off": self.printString = False