def __init__(self, world, seed, base, add): add = Value.get_float(add, world.act_shape) base = Agent.build(base, AddValueWorld(world, add), seed) def process_action(a): return a + add.get() super().__init__(base, process_action=process_action)
def __init__(self, world, seed, log_lr, n_steps, init): init = Value.get_float(init, world.act_shape) # Wrap the original world with a meta world of learning rates world = GradAscentLRWorld(world, n_steps, init) log_lr = Agent.build(log_lr, world, seed) # Run a single trajectory on GradAscentLRWorld value = world.final_value(log_lr) value.setflags(write=False) super().__init__(get_action=lambda _: value)
def fireEvent(self, event, channel=None, target=None): """Fire/Push a new Event into the system (queue) This will push the given Event, Channel and Target onto the Event Queue for later processing. if target is None, then target will be set as the Channel of the current Component, self.channel (defaulting back to None). If this Component's Manager is itself, enqueue on this Component's Event Queue, otherwise enqueue on this Component's Manager. :param event: The Event Object :type event: Event :param channel: The Channel this Event is bound for :type channel: str @keyword target: The target Component's channel this Event is bound for :type target: str or Component """ if channel is None and target is None: if type(event.channel) is TupleType: target, channel = event.channel else: channel = event.channel or event.name.lower() target = event.target or None else: channel = channel or event.channel or event.name.lower() if isinstance(target, Manager): target = getattr(target, "channel", "*") else: target = target or event.target or getattr(self, "channel", "*") event.channel = (target, channel) event.value = Value(event, self) if event.start is not None: self.fire(Start(event), *event.start) self.root._fire(event, (target, channel)) return event.value
def step(self, ctx, pc): self = hint(self, access_directly=True) ops = hint(self.ops, promote=True) lex_env = hint(self.lex_env, promote=True) op = getop(ops, pc) if op == PushConstantOp: idx = getop(ops, pc + 1) self.push(lex_env.constants[idx]) return pc + 2 elif op == DispatchOp: idx = getop(ops, pc + 1) argc = getop(ops, pc + 2) sym_value = lex_env.constants[idx] assert isinstance(sym_value, StringValue) sym = sym_value.value if sym == '+' and argc == 2: rhs = self.pop() lhs = self.pop() assert isinstance(lhs, IntValue) assert isinstance(rhs, IntValue) self.push(IntValue(lhs.value + rhs.value)) elif sym == '-' and argc == 2: rhs = self.pop() lhs = self.pop() assert isinstance(lhs, IntValue) assert isinstance(rhs, IntValue) self.push(IntValue(lhs.value - rhs.value)) elif sym == '<' and argc == 2: rhs = self.pop() lhs = self.pop() assert isinstance(lhs, IntValue) assert isinstance(rhs, IntValue) self.push(BoolValue(lhs.value < rhs.value)) elif sym == 'print' and argc == 1: print(self.pop().__repr__()) self.push(Value()) else: proc_lex_env = lex_env.get_proc_env(sym) if proc_lex_env: overload = proc_lex_env.procs[sym] self.push(ctx.run( overload.lex_env, overload.ops, 10, self.pop(), )) return pc + 3 elif op == ReturnOp: return -1 elif op == PopOp: self.drop() return pc + 1 elif op == JumpFalseOp: cnd = self.pop() if isinstance(cnd, BoolValue) and not cnd.value: idx = getop(ops, pc + 1) return idx else: return pc + 2 elif op == GetOp: idx = getop(ops, pc + 1) self.push(self.get(idx)) return pc + 2 return pc
def load_initial_order(self): for i, line in enumerate(open("initial_order.txt")): bits_str = line[:20] v = Value.from_bits_string(bits_str) self.set_memory(i, v)
def step(self): assert MIN_MEMORY_ADDR <= self.sequence_control < MAX_MEMORY_ADDR instr = self.get_memory(self.sequence_control) # debug if SHOW_RUNNNING_INSTRUCTION: print self.sequence_control, instr.as_order() op, addr, sl = instr.as_order() wide = (sl == "L") if op == "T": # TnS: m[n]=A; ABC=0 # TnL: w[n]=AB; ABC=0 a = self.get_accumulator(wide) self.set_memory(addr, a, wide) self.clear_accumulator() elif op == "H": # HnS: R += m[n] # HnL: RS += w[n] m = self.get_memory(addr, wide) r = self.get_multiplier(wide) r = m self.set_multiplier(r, wide) elif op == "E": # if A >= 0 goto n a = self.get_accumulator() if not a.is_negative(): # A >= 0 self.sequence_control = addr - 1 elif op == "G": # if A < 0 goto n a = self.get_accumulator() if a.is_negative(): # A < 0 self.sequence_control = addr - 1 elif op == "I": # Place the next paper tape character # in the *least* significant 5 bits of m[n]. c = self.cards[self.next_char] self.next_char += 1 v = io.ascii_to_edsac(c) self.set_memory(addr, Value.new_from_number(v)) if DEBUG_IO: print "read", c, v elif op == "A": # AnS: A += m[n] # AnL: AB += w[n] m = self.get_memory(addr, wide) r = self.get_accumulator(wide) r = r + m self.set_accumulator(r, wide) elif op == "S": m = self.get_memory(addr, wide) r = self.get_accumulator(wide) r = r - m self.set_accumulator(r, wide) elif op == "V": m = self.get_memory(addr, wide) r = self.get_multiplier(wide) v = m.multiply(r) if wide: a = self.accumulator else: a = self.get_accumulator(wide=True) a.set(a + v) elif op == "N": m = self.get_memory(addr, wide) r = self.get_multiplier(wide) v = m.as_integer() * r.as_integer() v = m.multiply(r) if wide: a = self.accumulator else: a = self.get_accumulator(wide=True) a.set(a - v) elif op == "R": # Shift right num_shift = _calc_num_shift(instr) v = self.accumulator.as_integer() v = v >> num_shift self.accumulator.set_from_number(v) elif op == "L": # Shift left num_shift = _calc_num_shift(instr) v = self.accumulator.as_unsigned() v = v << num_shift self.accumulator.set_from_number(v) elif op == "U": # UnS: m[n]=A # UnL: w[n]=AB self.set_memory(addr, self.get_accumulator(wide), wide) elif op == "C": raise NotImplementedError elif op == "Y": raise NotImplementedError elif op == "O": # output if DEBUG_IO: code = self.get_memory(addr).as_charcode() print "output %s %s %s" % ( io.edsac_to_letter(code), io.edsac_to_figure(code), code) else: sys.stdout.write( self.output( self.get_memory(addr).as_charcode())) elif op == "X": pass # no operation elif op == "F": raise NotImplementedError("Verify the last character" "output. What?") elif op == "Z": # finish return True else: raise AssertionError("Malformed Instruction:", instr.as_order()) self.sequence_control += 1 return False # not finished
from lexer import Lexer from parser_ import Parser from evaluator import Evaluator from context import Context from environment import * from values import Value global_env = Environment() global_env.set("null", Value(0)) global_func_env = FunctionEnvironment() context = Context("<program>", global_env, global_func_env) while True: text = input(">>> ") lexer = Lexer("<stdin>", text) tokens, errors = lexer.generate_tokens() if errors == None: # print(tokens) parser = Parser(tokens) ast = parser.parse() if ast.error == None: # print(ast.node) evaluator = Evaluator(ast.node, context) value = evaluator.evaluate() if value.error == None: print(value.value) else: print(value.error.as_string()) else: print(ast.error.as_string())
def step(self): assert MIN_MEMORY_ADDR <= self.sequence_control < MAX_MEMORY_ADDR instr = self.get_memory(self.sequence_control) # debug if SHOW_RUNNNING_INSTRUCTION: print self.sequence_control, instr.as_order() op, addr, sl = instr.as_order() wide = (sl == "L") if op == "T": # TnS: m[n]=A; ABC=0 # TnL: w[n]=AB; ABC=0 a = self.get_accumulator(wide) self.set_memory(addr, a, wide) self.clear_accumulator() elif op == "H": # HnS: R += m[n] # HnL: RS += w[n] m = self.get_memory(addr, wide) r = self.get_multiplier(wide) r = m self.set_multiplier(r, wide) elif op == "E": # if A >= 0 goto n a = self.get_accumulator() if not a.is_negative(): # A >= 0 self.sequence_control = addr - 1 elif op == "G": # if A < 0 goto n a = self.get_accumulator() if a.is_negative(): # A < 0 self.sequence_control = addr - 1 elif op == "I": # Place the next paper tape character # in the *least* significant 5 bits of m[n]. c = self.cards[self.next_char] self.next_char += 1 v = io.ascii_to_edsac(c) self.set_memory(addr, Value.new_from_number(v)) if DEBUG_IO: print "read", c, v elif op == "A": # AnS: A += m[n] # AnL: AB += w[n] m = self.get_memory(addr, wide) r = self.get_accumulator(wide) r = r + m self.set_accumulator(r, wide) elif op == "S": m = self.get_memory(addr, wide) r = self.get_accumulator(wide) r = r - m self.set_accumulator(r, wide) elif op == "V": m = self.get_memory(addr, wide) r = self.get_multiplier(wide) v = m.multiply(r) if wide: a = self.accumulator else: a = self.get_accumulator(wide=True) a.set(a + v) elif op == "N": m = self.get_memory(addr, wide) r = self.get_multiplier(wide) v = m.as_integer() * r.as_integer() v = m.multiply(r) if wide: a = self.accumulator else: a = self.get_accumulator(wide=True) a.set(a - v) elif op == "R": # Shift right num_shift = _calc_num_shift(instr) v = self.accumulator.as_integer() v = v >> num_shift self.accumulator.set_from_number(v) elif op == "L": # Shift left num_shift = _calc_num_shift(instr) v = self.accumulator.as_unsigned() v = v << num_shift self.accumulator.set_from_number(v) elif op == "U": # UnS: m[n]=A # UnL: w[n]=AB self.set_memory(addr, self.get_accumulator(wide), wide) elif op == "C": raise NotImplementedError elif op == "Y": raise NotImplementedError elif op == "O": # output if DEBUG_IO: code = self.get_memory(addr).as_charcode() print "output %s %s %s" % (io.edsac_to_letter(code), io.edsac_to_figure(code), code) else: sys.stdout.write( self.output(self.get_memory(addr).as_charcode())) elif op == "X": pass # no operation elif op == "F": raise NotImplementedError("Verify the last character" "output. What?") elif op == "Z": # finish return True else: raise AssertionError("Malformed Instruction:", instr.as_order()) self.sequence_control += 1 return False # not finished
def gen_list(size=None): if not size: size = randint(5, 365) return [Value(r, randint(1, 10)) for r in range(size)]