def EVAL(ast, env): if not type(ast) is list: return eval_ast(ast, env) elif len(ast) == 0: return ast elif type(ast) is list: first_symbol = ast[0] if first_symbol == 'def!': key = ast[1] value = EVAL(ast[2], env) env.set(key, value) return value elif first_symbol == 'let*': let_env = Env.Env(env) e1 = ast[1] e2 = ast[2] for i in range(0, len(e1), 2): let_env.set(e1[i], EVAL(e1[i + 1], let_env)) return EVAL(e2, let_env) else: fn = eval_ast(ast, env) return Mal.Number(eval_function(fn)) else: raise Exception("EVAL: Type Error")
def start(self): # init environment self.board_row = int(self.size_setting.row.get()) self.board_col = int(self.size_setting.col.get()) self.env = Env.Env(board_row=self.board_row, board_col=self.board_col) # init players i = 0 for player_setting in [self.player1, self.player2]: if player_setting.type.get() == 0: # human self.players[i] = 0 if player_setting.name.get(): self.names[i] = player_setting.name.get() else: self.names[i] = "Human " + str(i + 1) elif player_setting.type.get() == 1: # computer self.players[i] = 1 self.names[i] = "Computer " + str(i + 1) i += 1 self.env.players = self.players self.env.turn_to_player = {0: self.names[0], 1: self.names[1]} print(self.players, self.names) # init prediction self.env.prediction = self.predict_setting.predict_int.get() # init game_board if self.game_board: self.game_board.frame.destroy() self.game_board = GameBoard(master=self.window, board_row=self.board_row, board_col=self.board_col, env=self.env) self.game_board.frame.grid(row=1, column=0)
def __init__(self, parent_edge): ''' Parameter: edges : a list of edges that this state will point to ''' self.v = 0 # Change self.board = Env() self.edge_pointers = [] self.parent_edge = parent_edge
def handle_test(extra_data=None, **overwrite): """ create env, run test and record test results :param extra_data: extra data that runner or main passed to test case :param overwrite: args that runner or main want to overwrite :return: None """ # create env instance env_config = DefaultEnvConfig.get_default_config() for key in kwargs: if key in env_config: env_config[key] = kwargs[key] env_config.update(overwrite) env_inst = Env.Env(**env_config) # prepare for xunit test results xunit_file = os.path.join( env_inst.app_cls.get_log_folder(env_config["test_suite_name"]), XUNIT_FILE_NAME) XUNIT_RECEIVER.begin_case(test_func.__name__, time.time(), test_func_file_name) try: Utility.console_log("starting running test: " + test_func.__name__, color="green") # execute test function test_func(env_inst, extra_data) # if finish without exception, test result is True result = True except Exception as e: # handle all the exceptions here traceback.print_exc() result = False # log failure XUNIT_RECEIVER.failure(str(e), test_func_file_name) finally: # do close all DUTs env_inst.close() # end case and output result XUNIT_RECEIVER.end_case(test_func.__name__, time.time()) with open(xunit_file, "ab+") as f: f.write( xunitgen.toxml(XUNIT_RECEIVER.results(), XUNIT_DEFAULT_TEST_SUITE)) if result: Utility.console_log("Test Succeed: " + test_func.__name__, color="green") else: Utility.console_log(("Test Fail: " + test_func.__name__), color="red") TestResult.set_result(result, test_func.__name__) return result
def handle_test(extra_data=None, **overwrite): """ create env, run test and record test results :param extra_data: extra data that runner or main passed to test case :param overwrite: args that runner or main want to overwrite :return: None """ # create env instance env_config = DefaultEnvConfig.get_default_config() for key in kwargs: if key in env_config: env_config[key] = kwargs[key] env_config.update(overwrite) env_inst = Env.Env(**env_config) # prepare for xunit test results junit_file_path = env_inst.app_cls.get_log_folder( env_config["test_suite_name"]) junit_test_case = JunitReport.create_test_case(case_info["name"]) result = False try: Utility.console_log("starting running test: " + test_func.__name__, color="green") # execute test function test_func(env_inst, extra_data) # if finish without exception, test result is True result = True except Exception as e: # handle all the exceptions here traceback.print_exc() # log failure junit_test_case.add_failure_info( str(e) + ":\r\n" + traceback.format_exc()) finally: if not case_info["junit_report_by_case"]: JunitReport.test_case_finish(junit_test_case) # do close all DUTs, if result is False then print DUT debug info env_inst.close(dut_debug=(not result)) # end case and output result JunitReport.output_report(junit_file_path) if result: Utility.console_log("Test Succeed: " + test_func.__name__, color="green") else: Utility.console_log(("Test Fail: " + test_func.__name__), color="red") return result
def __init__(self): self.commands = { "cd": Commands.cd, "clr": Commands.clr, "dir": Commands.dir, "environ": Commands.environ, "echo": Commands.echo, "display": Commands.echo, "help": Commands.displayHelp, "pause": Commands.pause, "quit": Commands.quitShell, } self.env = Env.Env(os.path.abspath(os.path.dirname(sys.argv[0])))
def fit(self): # TODO: 用 N 個trajectory 的更新 agent # self.model.fit(s, a*r...) # self.model.fit(s, a, sample_weight = r...) S = np.concatenate(self.s_batch) A = np.concatenate(self.a_batch) R = np.concatenate(self.r_batch) # normalize R R -= np.mean(R) R /= np.std(R) + K.epsilon() R += K.epsilon() self.model.fit( x = S, y = A, sample_weight = R, epochs = 1, verbose = 0 ) self.batch_counter = 0 if __name__ == '__main__': episode = 100 n = 10 learning_rate = 0.01 env = stock.Env('./stockData/')
from Env import * print("Welcome to tic-tac-toe pvp") Board = Env() Board.reset() legal_input = [1, 2, 3, 4, 5, 6, 7, 8, 9] legal_input = set(legal_input) def parse_input(): print("\n" + str(Board.player_turn())) while True: try: action = int( input("Choose from available moves: " + str(sorted(Board.legal_moves())))) if (action not in legal_input): print("Illegal input") continue break except: print("Illegal input") legal_input.remove(action) return action while (not Board.done): action = parse_input() Board.step(action) Board.render()
# if done[0]: # print(done[1]) # break # curr_state = next_state from DQN import DQN from Env import * import numpy as np from params import * import time dqn = DQN() dqn.eval_net = torch.load('./eval_net.pkl') dqn.target_net = torch.load('./target_net.pkl') TARGET = np.array(getPos(randAng())) env = Env(TARGET) curr_state = env.reset() stp = 0 while True: action = dqn.choose_action(curr_state) next_state, reward, done = env.step(action) stp += 1 if done[0]: print(stp) break curr_state = next_state time.sleep(0.1) distance = np.linalg.norm(TARGET - curr_state.numpy()[0:3]) print(distance)
#!/usr/bin/env python # Arash Taher # Jun 2013 # A rudimentary implementation for scheme interpreter # Based on original implementation of SICP # Main part of our program : REPL # Running evaluator as a program import Env import Eval import Apply import Lexical import logging # Global enviroment global_env = Env.Env({}, None) # Primitives evaluation helper function def primitive_ops(func): def op(params): acc = params[0] for num in params[1:]: acc = func(acc, num) return acc return op # Definition of primitive operations global_env.define_var('+', primitive_ops(lambda x, y: x + y))
import sys import Reader import Printer import Mal import Env repl_env = Env.Env() repl_env.set('+', lambda a, b: a + b) repl_env.set('-', lambda a, b: a - b) repl_env.set('*', lambda a, b: a * b) repl_env.set('/', lambda a, b: int(a / b)) def READ(arg): return Reader.read_str(arg) def PRINT(arg): return Printer.pr_str(arg) def EVAL(ast, env): if not type(ast) is list: return eval_ast(ast, env) elif len(ast) == 0: return ast elif type(ast) is list: first_symbol = ast[0]
def store(self, transition): if len(self.memory) == self.capacity: self.memory.pop(0) self.memory.append(transition) self.pointer += 1 def sample(self, batch_size): assert self.pointer >= self.capacity, 'Memory has not been fulfilled' indices = np.random.choice(self.capacity, size=batch_size) return [self.memory[i] for i in indices] if __name__ == '__main__': env_name = 'Pendulum-v0' env = Env.Env(env_name) action_bounds = torch.from_numpy(env.action_bounds) explore_var = action_bounds.detach().clone() td3 = TD3(env.n_features, action_bounds) for i_epoch in range(10000): for i_episode in range(Config.MAX_EPISODE): s = env.start() old_a = None episode_rewards = 0 for i_iter in range(5000): env.show() if td3.memory.pointer < Config.MEMORY_CAPACITY: a = env.sample_action() else:
def make(): return Env.Env()
[1, 3, 0, 0, 0, 1, 0, 0, 3, 1, 2, 1], [1, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1], [1, 3, 0, 1, 3, 0, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 2, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ]) lr = 0.8 eps = 0.2 epsDiscount = 0.99 discount = 0.8 dynaPlanLen = 50 useTree = True keepPlanTree = True treeSearchLen = 50 e = Env.Env(grid) a = Agent.Agent(e, eps=eps, lr=lr, epsDiscount=epsDiscount, discount=discount, keepPlan=keepPlanTree, useTree=useTree, treeSearchLen=treeSearchLen) a.solve(plan_len=dynaPlanLen)
def handle_test(extra_data=None, **overwrite): """ create env, run test and record test results :param extra_data: extra data that runner or main passed to test case :param overwrite: args that runner or main want to overwrite :return: None """ # create env instance env_config = DefaultEnvConfig.get_default_config() for key in kwargs: if key in env_config: env_config[key] = kwargs[key] env_config.update(overwrite) env_inst = Env.Env(**env_config) # prepare for xunit test results junit_file_path = env_inst.app_cls.get_log_folder( env_config["test_suite_name"]) junit_test_case = JunitReport.create_test_case(case_info["ID"]) result = False try: Utility.console_log("starting running test: " + test_func.__name__, color="green") # execute test function test_func(env_inst, extra_data) # if finish without exception, test result is True result = True except Exception as e: # handle all the exceptions here traceback.print_exc() # log failure junit_test_case.add_failure_info( str(e) + ":\r\n" + traceback.format_exc()) finally: # do close all DUTs, if result is False then print DUT debug info close_errors = env_inst.close(dut_debug=(not result)) # We have a hook in DUT close, allow DUT to raise error to fail test case. # For example, we don't allow DUT exception (reset) during test execution. # We don't want to implement in exception detection in test function logic, # as we need to add it to every test case. # We can implement it in DUT receive thread, # and raise exception in DUT close to fail test case if reset detected. if close_errors: for error in close_errors: junit_test_case.add_failure_info(str(error)) result = False if not case_info["junit_report_by_case"]: JunitReport.test_case_finish(junit_test_case) # end case and output result JunitReport.output_report(junit_file_path) if result: Utility.console_log("Test Succeed: " + test_func.__name__, color="green") else: Utility.console_log(("Test Fail: " + test_func.__name__), color="red") return result
global _Generator if platform in Makefile.Generators.keys(): _Generator = Makefile.Generators[platform](Makefile.platform) else: raise Exception('Unknown target platform {0}'.format(platform)) Makefile.project = Makefile.createProject(cls, name, platform, importer, _Generator) _Generator.initialize(Makefile, Makefile.project) # substituteVars @staticmethod def substituteVars(input): global _Env for k, v in _Env.vars.items(): if isinstance(v, str): input = input.replace('$(' + k + ')', v) return input # generate @staticmethod def generate(): global _Generator _Generator.generate() _Env = Env.Env() _Generator = None
def clear(): global _Env _Env = Env.Env()
def Env_process_target(recieve_que, send_que, config): print('Env process start') env = Env.Env(config) env.run(recieve_que, send_que)
test_dict[pair[0]] = int(pair[1]) else: test_dict[pair[0]] = pair[1] else: raise ValueError('Error in argument item {} of {}'.format( test_item, test)) test_dict['app_bin'] = args.app_bin list_of_dicts.append(test_dict) TinyFW.set_default_config(env_config_file=args.env_config_file) env_config = TinyFW.get_default_config() env_config['app'] = UT env_config['dut'] = IDF.IDFDUT env_config['test_suite_name'] = 'unit_test_parsing' env = Env.Env(**env_config) detect_update_unit_test_info(env, extra_data=list_of_dicts, app_bin=args.app_bin) for i in range(1, args.repeat + 1): if args.repeat > 1: Utility.console_log("Repetition {}".format(i), color="green") for dic in list_of_dicts: t = dic.get('type', SIMPLE_TEST_ID) if t == SIMPLE_TEST_ID: run_unit_test_cases(extra_data=dic) elif t == MULTI_STAGE_ID: run_multiple_stage_cases(extra_data=dic) elif t == MULTI_DEVICE_ID: run_multiple_devices_cases(extra_data=dic)
import Env env = Env.Env("easy.txt") print(env.blockAt(0, 0)) print(env.stateEquals(env.dState)) print(env.stateEquals(env.state)) print(env.done()) print(env.getSize()) print() print(env.addBlock(0, 0, ("red", 0))) # under existing block print(env.addBlock(0, 0, ("red", 4))) # floating print(env.addBlock(0, 0, ("red", 3))) # nice :) print(env.addBlock(1, 1, ("red", 0))) # no blocks here yet, but nice :) print(env.addBlock(1, 1, ("red", 2))) # floating, but works print(env.addBlock(0, 20, ("red", 3))) # out of bounds print() print(env.takeBlock(1, 1)) # take that floating red block print(env.takeBlock(1, 1)) # take that first red block print(env.takeBlock(1, 1)) # no block to take # TODO: take one that is surrounded print("done")