class Context(object): def __init__(self, instructions): self.registers = Registers() self.flags = Flags() self.instructions = instructions self.heap = Memory(size=40, mode=HEAP, start_address=0x0000) self.stack = Memory(size=40, mode=STACK, start_address=0xFFFF) self.registers.set(SP, 0xFFFF) def run(self): """ initialize the context and execute the first instruction """ self.registers.reset() def step(self): """ execute the next instruction whose address in the EIP value :return: """ self.registers.tick() self.flags.tick() self.heap.tick() self.stack.tick() next_address = self.registers.get(IP).value if next_address < len(self.instructions): next_instruction = self.instructions[next_address] next_instruction.execute(self) self.registers.set(IP, next_address + 1) return True else: return False
def try_alf_word(self): if self.get() == '"': # exactly five mix-chars in inverted or self.next() if self.get() == '"': s = "" else: s = self.get() if self.look() != '"': raise UnquotedStringError(self.line.argument) self.next() else: # less than six mix-chars not in inverted s = self.line.argument.rstrip('\n\r') self.ct = len(self.tokens) - 1 if s is None: s = "" s = s[:5] while len(s) < 5: s += " " # now s - string with len = 5 word = Memory.positive_zero() for i in xrange(1, 6): word[i] = charset.ord(s[i - 1]) if word[i] is None: raise InvalidCharError(s[i - 1]) return Memory.mix2dec(word)
def try_w_exp(self): """This function DO SELF.NEXT()""" word = Memory.positive_zero() value = self.try_exp() if value is None: return None if self.look() == "(": self.next() field = self.try_f_part() else: # it's property of w-exp that empty f-part means not default value # but 0:5 field = 5 self.next() if Memory.apply_to_word(value, word, field) is None: raise InvalidFieldSpecError(field) while True: if self.get() != ",": break self.next() value = self.try_exp() if value is None: raise ExpectedExpError(self.get_all_before_this()) if self.look() == "(": self.next() field = self.try_f_part() else: field = get_codes(self.line.operation)[1] self.next() if Memory.apply_to_word(value, word, field) is None: raise InvalidFieldSpecError(field) return Memory.mix2dec(word)
def __init__(self, parent, name, address, device_info=None, auto_update=False): self.auto_update = auto_update self.parent = parent self.last_values = {} Memory.__init__(self, name=name, width_bits=32, address=address, length_bytes=4) self.process_info(device_info) LOGGER.debug('New Register %s' % self)
def __init__(self, instructions): self.registers = Registers() self.flags = Flags() self.instructions = instructions self.heap = Memory(size=40, mode=HEAP, start_address=0x0000) self.stack = Memory(size=40, mode=STACK, start_address=0xFFFF) self.registers.set(SP, 0xFFFF)
def _determine_blockshape(self, outputSlot): """ Choose a blockshape using the slot metadata (if available) or an arbitrary guess otherwise. """ input_shape = outputSlot.meta.shape ideal_blockshape = outputSlot.meta.ideal_blockshape ram_usage_per_requested_pixel = outputSlot.meta.ram_usage_per_requested_pixel num_channels = 1 tagged_shape = outputSlot.meta.getTaggedShape() # Generally, we don't want to split requests across channels. if 'c' in tagged_shape.keys(): num_channels = tagged_shape['c'] channel_index = tagged_shape.keys().index('c') input_shape = input_shape[:channel_index] + input_shape[channel_index+1:] if ideal_blockshape: # Never enlarge 'ideal' in the channel dimension. num_channels = ideal_blockshape[channel_index] ideal_blockshape = ideal_blockshape[:channel_index] + ideal_blockshape[channel_index+1:] max_blockshape = input_shape available_ram = Memory.getAvailableRamComputation() if ram_usage_per_requested_pixel is None: # Make a conservative guess: 2*(bytes for dtype) * (num channels) + (fudge factor=4) ram_usage_per_requested_pixel = 2*outputSlot.meta.dtype().nbytes*num_channels + 4 warnings.warn( "Unknown per-pixel RAM requirement. Making a guess." ) # Safety factor (fudge factor): Double the estimated RAM usage per pixel safety_factor = 2.0 logger.info("Estimated RAM usage per pixel is {} * safety factor ({})" .format( Memory.format(ram_usage_per_requested_pixel), safety_factor ) ) ram_usage_per_requested_pixel *= safety_factor if ideal_blockshape is None: blockshape = determineBlockShape( input_shape, available_ram/(self._num_threads*ram_usage_per_requested_pixel) ) if 'c' in outputSlot.meta.getAxisKeys(): blockshape = blockshape[:channel_index] + (num_channels,) + blockshape[channel_index:] warnings.warn( "Chose an arbitrary request blockshape {}".format( blockshape ) ) else: logger.info("determining blockshape assuming available_ram is {}" ", split between {} threads" .format(Memory.format(available_ram), self._num_threads)) # By convention, ram_usage_per_requested_pixel refers to the ram used when requesting ALL channels of a 'pixel' # Therefore, we do not include the channel dimension in the blockshapes here. blockshape = determine_optimal_request_blockshape( max_blockshape, ideal_blockshape, ram_usage_per_requested_pixel, self._num_threads, available_ram ) if 'c' in outputSlot.meta.getAxisKeys(): blockshape = blockshape[:channel_index] + (num_channels,) + blockshape[channel_index:] logger.info( "Chose blockshape: {}".format( blockshape ) ) fmt = Memory.format(ram_usage_per_requested_pixel * numpy.prod(blockshape[:-1])) logger.info("Estimated RAM usage per block is {}".format(fmt)) return blockshape
def __init__(self, env, model, epsilon=.9, min_epsilon=.1, epsilon_decay=1e-3): self.env = env self.model = model self.epsilon = epsilon self.min_epsilon = min_epsilon self.epsilon_decay = epsilon_decay self.episode = 0 self.positiveMemory = Memory(model=self.model, episode_max_size=20) self.negativeMemory = Memory(model=self.model, episode_max_size=10)
def memory(): """ Create Memory instance for testing. :return: new memory instance """ from gb import GB from memory import Memory gb = GB() mem = Memory(gb) mem.load_cartridge(cartridge_data=bytes.fromhex("00")*0x8000) return mem
def _build_memory(screen): """ Initialize CPC464 memory map """ memory = Memory(screen) memory.add_chunk(0x0000, RomChunk(_get_data_from_file('6128L.rom'))) memory.add_chunk(0xC000, UpperRomChunk(0, _get_data_from_file('6128U-basic.rom'))) memory.apply_ram_map(0) memory.dump_map() return memory
def __init__(self, parent, name, address, length_bytes, device_info=None): """ :param parent: Parent object who owns this TenGbe instance :param name: Unique name of the instance :param address: :param length_bytes: :param device_info: Information about this device :return: """ Memory.__init__(self, name, 32, address, length_bytes) Gbe.__init__(self, parent, name, address, length_bytes, device_info)
def run(path, debug, max_cycles): with open(path, "rb") as rom_file: debug_title = debug == "TITLE" debug_header = debug == "HEADER" or debug == "ALL" debug_mem = debug == "MEMORY" or debug == "ALL" debug_instructions = debug == "INSTRUCTIONS" or debug == "ALL" debug_registers = debug == "REGISTERS" or debug == "ALL" rom = [i for i in rom_file.read()] header = Header(rom, debug_header) mem = Memory(rom, header) if debug_title: print("Title: " + header.name) if debug_instructions: print("PC: Operation") interrupts = Interrupts() cpu = CPU(mem, interrupts, debug_instructions, debug_registers) timer = Timer(interrupts) sound = Sound() link = Link() joypad = Joypad() lcdc = LCDC(mem, interrupts) mem.setupIO(lcdc, interrupts, timer, sound, link, joypad) total_cycles = 0 try: pygame.init() while cpu.run_state != "QUIT": for event in pygame.event.get(): if event.type == pygame.QUIT: cpu.run_state = "QUIT" if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP: joypad.keyEvent(event) interrupts.update() if cpu.run_state == "RUN": cpu.run() else: cpu.cycles += 1 timer.update(cpu.cycles) lcdc.update(cpu.cycles) total_cycles += cpu.popCycles() if max_cycles >= 0 and total_cycles > max_cycles: cpu.run_state = "QUIT" except AssertionError as e: if debug_mem: mem.display() traceback.print_tb(e.__traceback__) except KeyboardInterrupt as e: if debug_mem: mem.display() else: if debug_mem: mem.display()
def test(test_iter, folds, training_folds): results = [] mem = Memory() for i in range(test_iter): print "iteration %d ..." % (i + 1) ini_set = split_set2(folds, senseval.instances()[0:]) for j in range(folds): print"...fold %d ..." % (j + 1) sets = partition_set(training_folds, ini_set, j) print "-$$Train time$$-" mem.train(sets[0]) print "-$$results time$$-" results.append(mem.test(sets[1])) return results
def test_main(): mem = Memory() print "loading data_set" ini_set = split_set2(5, senseval.instances()[0:10000]) data_set = partition_set(4, ini_set, 0) #Serializer.save("/tmp/portioned_data", data_set) #data_set = Serializer.load("/tmp/portioned_data") print "training data" mem.train(data_set[0]) #print "saving data" #mem.save_values("/tmp/mem_internals") #mem.load_values("/tmp/mem_internals") print "------*********testing**********------" results = mem.test(data_set[1]) print "%3.1f %% accuracy" %(sum(results)/len(results) * 100)
def __init__(self, environment, inputs): self.environment = environment self.state_size = inputs self.nr_actions = environment.action_space.n self.memory = Memory(30000) self.discountFactor = 0.975 self.predictionModels = []
def __init__(self, config): self.config = config # 应用程序配置 self.connections = 0 # 连接客户端列表 if self.config['savetime'] != 0: # 不保存数据 self.thread = PeriodicCallback(self.save_db, int(self.config['savetime']) * 1000) self.thread.start() # 背景保存服务线程启动 self.workpool = ThreadPool(int(config['work_pool'])) # 工作线程池 self.status = Status(CacheServer.status_fields) # 服务器状态 self.slave_clients = {} # 同步客户端 self.is_sync = False # 是否在同步 if self.config['master'] is not None: # 从服务器启动 shutil.rmtree(config['db']) self.master_server = PyCachedClient(self.config['master'][0], self.config['master'][1]) self.master_server.sync(self.config['port']) self.slavepool = None self.slave = True # 是否为Slave模式 else: # 主服务器启动, 需要启动从命令工作线程 self.slavepool = ThreadPool(int(config['slave_pool'])) # slave Command send pools self.slave = False # 是否为Slave模式 self.memory = Memory(config['db']) # 缓存服务类 super(CacheServer, self).__init__()
def __init__(self, obs_dim, action_dim, hiddens_actor, hiddens_critic, layer_norm=False, memory_size=50000): self.obs_dim = obs_dim self.action_dim = action_dim self.noise_stddev = 1. self.noise_stddev_decrease = 5e-4 self.noise_stddev_lower = 5e-2 actor_activations = [dy.tanh for _ in range(len(hiddens_actor))] + [dy.tanh] critic_activations = [dy.tanh for _ in range(len(hiddens_critic))] + [None] self.actor = MLP(inpt_shape=(obs_dim,), hiddens=hiddens_actor + [action_dim], activation=actor_activations, layer_norm=layer_norm) self.critic = MLP(inpt_shape=(obs_dim + action_dim,), hiddens=hiddens_critic + [1], activation=critic_activations, layer_norm=layer_norm) self.actor_target = MLP(inpt_shape=(obs_dim,), hiddens=hiddens_actor + [action_dim], activation=actor_activations, layer_norm=layer_norm) self.critic_target = MLP(inpt_shape=(obs_dim + action_dim,), hiddens=hiddens_critic + [1], activation=critic_activations, layer_norm=layer_norm) self.actor_target.update(self.actor, soft=False) self.critic_target.update(self.critic, soft=False) self.trainer_actor = dy.AdamTrainer(self.actor.pc) self.trainer_critic = dy.AdamTrainer(self.critic.pc) self.trainer_actor.set_learning_rate(1e-4) self.trainer_critic.set_learning_rate(1e-3) self.memory = Memory(memory_size)
def __init__(self, environment, inputs): self.input_size = inputs self.output_size = environment.action_space.n self.memory = Memory(2000) self.discountFactor = 0.975 self.learnStart = 36 self.models = [None] * 5
def __init__(self, size_state, nr_actions, memorySize, discountFactor, learningRate, learnStart): self.input_size = size_state self.output_size = nr_actions self.memory = Memory(memorySize) self.discountFactor = discountFactor self.learnStart = learnStart self.learningRate = learningRate
def __init__(self, queuetype, memtype, t_cs_val = 13): self.t_cs = t_cs_val self.t_slice = 80 # in ms self.t_memmove = 10 # in ms if queuetype == "FCFS": self.process_queue = FCFSQueue() elif queuetype == "SRT": self.process_queue = SRTQueue() elif queuetype == "PWA": self.process_queue = PWAQueue() elif queuetype == "RR": self.process_queue = FCFSQueue() self.mem = Memory(memtype) self.queueType = queuetype self.CPUIdle = True self.n = 0 self.active_n = 0 self.maxID = 1 self.processInCPU = None #Note: processInCPU is the process in use of CPU, NOT in content switch self.burstTimeSum = 0 self.waitTimeSum = 0 self.waitTimeNum = 0 self.turnaroundTimeSum = 0 self.contentSwitchSum = 0 self.processInCPU_tobe = None self.defragtime = 0 self.p_list = []
def __init__(self, inputs, outputs, memorySize, discountFactor, learningRate, learnStart): self.input_size = inputs self.output_size = outputs self.memory = Memory(memorySize) self.discountFactor = discountFactor self.learnStart = learnStart self.learningRate = learningRate
def __init__(self): self.memory = Memory() self.OPERATORS = {":=" : self.opDefine, "<<" : self.opDefineKeyword, "++" : self.opIncrement} self.COMMANDS = {"remember" : self.comRemember, "recall" : self.comFindQuote, "evaluate" : self.comEvaluate, "count" : self.comCount, "findfactoid" : self.comFactoidSearch, "findquote" : self.comQuoteSearch, "delete" : self.comDeleteFactoid } self.PROCESSCOMMANDS = {"remember" : False, "recall" : False, "evaluate" : True, "count" : False, "findfactoid" : False, "findquote" : False, "delete" : False }
def __init__(self, env, params): self.env = env params.actions = env.actions() self.num_actions = env.actions() self.episodes = params.episodes self.steps = params.steps self.train_steps = params.train_steps self.update_freq = params.update_freq self.save_weights = params.save_weights self.history_length = params.history_length self.discount = params.discount self.eps = params.init_eps self.eps_delta = (params.init_eps - params.final_eps) / params.final_eps_frame self.replay_start_size = params.replay_start_size self.eps_endt = params.final_eps_frame self.random_starts = params.random_starts self.batch_size = params.batch_size self.ckpt_file = params.ckpt_dir+'/'+params.game self.global_step = tf.Variable(0, trainable=False) if params.lr_anneal: self.lr = tf.train.exponential_decay(params.lr, self.global_step, params.lr_anneal, 0.96, staircase=True) else: self.lr = params.lr self.buffer = Buffer(params) self.memory = Memory(params.size, self.batch_size) with tf.variable_scope("train") as self.train_scope: self.train_net = ConvNet(params, trainable=True) with tf.variable_scope("target") as self.target_scope: self.target_net = ConvNet(params, trainable=False) self.optimizer = tf.train.RMSPropOptimizer(self.lr, params.decay_rate, 0.0, self.eps) self.actions = tf.placeholder(tf.float32, [None, self.num_actions]) self.q_target = tf.placeholder(tf.float32, [None]) self.q_train = tf.reduce_max(tf.mul(self.train_net.y, self.actions), reduction_indices=1) self.diff = tf.sub(self.q_target, self.q_train) half = tf.constant(0.5) if params.clip_delta > 0: abs_diff = tf.abs(self.diff) clipped_diff = tf.clip_by_value(abs_diff, 0, 1) linear_part = abs_diff - clipped_diff quadratic_part = tf.square(clipped_diff) self.diff_square = tf.mul(half, tf.add(quadratic_part, linear_part)) else: self.diff_square = tf.mul(half, tf.square(self.diff)) if params.accumulator == 'sum': self.loss = tf.reduce_sum(self.diff_square) else: self.loss = tf.reduce_mean(self.diff_square) # backprop with RMS loss self.task = self.optimizer.minimize(self.loss, global_step=self.global_step)
def __init__(self): ''' Births a new Parrott, with a Naive Bayes brain and a Solr memory. ''' self.brain = naive_bayes.NaiveBayes() self.twitter = membrane.twitter.api() self.memory = Memory()
def __init__(self): self.control = Control() self.register = Registers(32) self.pcreg = PipelineReg() self.if_id = PipelineReg() self.id_exe = PipelineReg() self.exe_mem = PipelineReg() self.mem_wb = PipelineReg() self.alu = Alu() self.memory = Memory() self.initializePipeline()
def read(self, **kwargs): """ Memory.read returns a list for all bitfields, so just put those values into single values. """ memdata = Memory.read(self, **kwargs) results = memdata['data'] timestamp = memdata['timestamp'] for k, v in results.iteritems(): results[k] = v[0] self.last_values = results return {'data': results, 'timestamp': timestamp}
def run_model(config_file, machine_id): print "Running {}".format(machine_id) machine = Machine(config_file) machine.get_id(machine_id) memory = Memory(machine) networking = Networking(machine) ai_features = dict(memory.ai_features().items() + networking.ai_features().items()) procfs_features = dict(memory.procfs_features().items() + networking.procfs_features().items()) sysfs_features = dict(memory.sysfs_features().items() + networking.sysfs_features().items()) machine.write_features('ai_features', ai_features) machine.write_features('procfs', procfs_features) machine.write_features('sysfs', sysfs_features) machine.write_features('quick', { 'Security': 0, 'Stats': len(procfs_features) + len(sysfs_features), })
def reset(self): with open(self.fname) as f: self.mem = Memory(f) self.door_unlocked = False self.step_count = 1 self.break_at_finish = -1 self.registers = Registers() self.registers[PC] = self.mem[0xfffe] self.call_targets = [self.registers[PC]] self.callsites = [] self.current_block_start = self.registers[PC] self.insn_count = 0
def __init__(self): self.logger = Log() # Create components self.cpu = CPU(self) self.memory = Memory(self) self.interrupts = Interrupts(self) self.screen = Screen(self) self.gpu = GPU(self) self.debug_mode = False self.step_mode = False
class Stack(object): def __init__(self, size): self._stack = Memory(size) self._sp = 0 def push(self, word): self._sp -= 2 self._stack.write_word(self._sp, word) def pop(self): self._sp += 2 return self._stack.read_word(self._sp-2) def set_position(self, position): self._sp = position def position(self): return self._sp def __repr__(self): return str(self._stack)
def __init__(self, numIntRegisters = 32, numFloatRegisters = 32, timingModel = timingmodel.defaultTimingModel) : self.memory = Memory() self.registerFile = {} self.numIntRegisters = numIntRegisters self.numFloatRegisters = numFloatRegisters self.__createRegisterFile() self.memoryManager = MemoryManager(self.memory.heap[0], self.memory.heap[1] - self.memory.heap[0]) # print("Memory allocator " + str(self.memoryManager)) self.timingModel = timingModel() # print(self.timingModel) self.prog = None self.pc = self.memory.text[0] self.registerFile['sp'].write(self.memory.text[1] - 4) #initialize the stack pointer
def update_weights(self, memory: Memory): gamma = 0.95 elements = memory.sample(200) x = [] y = [] for element in elements: x.append(element.old_state) new_reward = element.reward + gamma * np.max(self._predict(element.new_state)) scores = self._predict(element.old_state) scores[element.action] = new_reward y.append(scores) x = np.asarray(x) y = np.asarray(y) self._model.fit(x=x, y=y, verbose=0)
def main(): env = gym.make(env_name) env.seed(500) torch.manual_seed(500) num_inputs = env.observation_space.shape[0] num_actions = env.action_space.n env.close() global_target_model = Model(num_inputs, num_actions) global_online_model = Model(num_inputs, num_actions) global_target_model.train() global_online_model.train() global_target_model.load_state_dict(global_online_model.state_dict()) global_target_model.share_memory() global_online_model.share_memory() global_memory = Memory(replay_memory_capacity) global_ep, global_ep_r, res_queue, global_memory_pipe = mp.Value('i', 0), mp.Value('d', 0.), mp.Queue(), mp.Queue() writer = SummaryWriter('logs') n = 2 epsilons = [(i * 0.05 + 0.1) for i in range(n)] actors = [Actor(global_target_model, global_memory_pipe, global_ep, global_ep_r, epsilons[i], i) for i in range(n)] [w.start() for w in actors] learner = Learner(global_online_model, global_target_model, global_memory, global_memory_pipe, res_queue) learner.start() res = [] while True: r = res_queue.get() if r is not None: res.append(r) [ep, loss] = r # writer.add_scalar('log/score', float(ep_r), ep) writer.add_scalar('log/loss', float(loss), ep) else: break [w.join() for w in actors]
def main(): # set up everything here # Initilaise pygame pygame.init() nes_icon = pygame.image.load('icon.png') pygame.display.set_icon(nes_icon) screen = pygame.display.set_mode((256, 240)) pygame.display.set_caption('NESS') done = False # Emulator components initialised here memory = Memory() cpu = CPU(memory) rom = ROM(memory) while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True cpu.execute()
def main(): env = Game() if initial.INITIAL_MEMORY_VERSION == None: memory = Memory(config.MEMORY_SIZE) # load neural network current_NN = Residual_CNN(config.REG_CONST,config.LEARNING_RATE,(2,) + env.grid_shape, env.action_size, config.HIDDEN_CNN_LAYERS) #???? best_NN = Residual_CNN(config.REG_CONST,config.LEARNING_RATE,(2,) + env.grid_shape, env.action_size, config.HIDDEN_CNN_LAYERS) best_player_version = 0 best_NN.model.set_weights(current_NN.model.get_weights()) #create players current_player = best_player = iter = 0 while 1: pass
def get_value(self, memory: Memory, paddress: int): rslt = { "%s[%.8X-%.8X]" % (self.name, self.offset, paddress): self.name, "content": None } print("get_value(%s) -->" % self.name) print(paddress) content = [] for field in self.fields: if field.pointer: address = memory.get_pointer_be(paddress + field.offset) else: address = paddress + field.offset print("processing %s.%s[0x%.8X]" % (self.name, field.name, address)) content.append(field.get_value(memory, address)) rslt["content"] = content return rslt
class TestCache(unittest.TestCase): def setUp(self): self.sys = MemSys("Test Platform", 64) self.memory = Memory(self.sys, 4, 4, 4, 2, 64, 1866, True, None) self.l2c = Cache(self.sys, 1037000, 0, 1, 16, 8, 534288, 64, 3, True, self.memory.get_component_id()) self.l1c0 = Cache(self.sys, 1037000, 0, 0, 16, 8, 16384, 64, 1, True, self.l2c.get_component_id()) self.cu0 = ComputeUnit(self.sys, 1037000, 0, True, self.l1c0.get_component_id()) self.sys.build_map() def test_cache_line_addr_calc(self): self.assertEqual(self.l1c0.get_cache_line(0x4000), 0x100) self.assertEqual(self.l1c0.get_cache_line(0x4010), 0x100) self.assertEqual(self.l1c0.get_cache_line(0x4110), 0x104) self.assertEqual(self.l1c0.get_cache_line(0x4112), 0x104) self.assertEqual(self.l1c0.get_cache_line(0x41100000), 0x1044000) self.assertEqual(self.l1c0.get_cache_line(0x4110000000000000), 0x104400000000000)
def handle_create(self, cpu, clock): base_task_id = cpu.memory.info[cpu.internal_state.PC + 1] print(f"\n--- CREATING TASK FOR BASE PROGR {base_task_id} ---") try: new_task = Task() new_task.initialize(len(self.tasks), self.base_tasks[base_task_id], Memory(50), Internal_State(), IO()) new_task.task_state = "READY" for i in range(len(new_task.progr)): new_task.memory.write(i, new_task.progr[i], cpu.error) self.tasks.append(new_task) cpu.internal_state.PC = cpu.internal_state.PC + 1 + 1 except: cpu.error.update(5) cpu.internal_state.mode = "PARADA" cpu.internal_state.mode_info = "RECEIVED PROGRAM " + \ str(base_task_id)
def __init__(self, step_size=96, types=1, spread=10, pip_cost=1000, leverage=500, min_lots=0.01, assets=100000, available_assets_rate=0.8): self.step_size = step_size spread /= pip_cost self.data() self.memory = Memory(5000000) self.types = types reward = Reward if types == 1 else Reward2 self.rewards = reward(spread, leverage, pip_cost, min_lots, assets, available_assets_rate) self.rewards.max_los_cut = -np.mean(self.atr) * pip_cost
def main(): human = HumanWASDPlayer() play(human, human) pvnet = PolicyValueNet(board_n, model_filename) mem = Memory() while True: mcts_player = MCTSPlayer(pvnet.get_pvnet_fn(), play_style = 3) bh, ph, vh = play(mcts_player, human, mcts_player) mem.save_data((bh, ph, vh)) mcts_player = MCTSPlayer(pvnet.get_pvnet_fn(), play_style = 3) bh, ph, vh = play(human, mcts_player, mcts_player) mem.save_data((bh, ph, vh))
def run(self): # Constants game = snakegame(gui=False, seed=1) MAX_MEMORY_SIZE = 100000 NEURONS = 6 #6 LAYERS = 2 #2 NUM_INPUTS = 8 #208#8 NUM_OUTPUTS = 3 LEARNING_RATE = 0.005 MAX_GAME_STEPS = 300000 LAMBDA = 0 # Setup trainer, memory and model architecture trainer = Trainer() memory = Memory(max_memory_size=MAX_MEMORY_SIZE) model = Network(neurons=NEURONS, layers=LAYERS, num_inputs=NUM_INPUTS, num_outputs=NUM_OUTPUTS, learning_rate=LEARNING_RATE, l=LAMBDA) # Create random agent to fill memory and train it rand_agent = RandomAgent(memory=memory, model=model) trainer.train(rand_agent, game, max_steps=MAX_MEMORY_SIZE, save_model=False) # Create smart agent and train it, use memory from random agent smart_agent = DDQNAgent(memory=rand_agent.memory, model=model, prioritized_replay=True, epsilon_min=10, stop_explore=30000) trainer.train(smart_agent, game, max_steps=MAX_GAME_STEPS, save_model=True)
def main(args): model_store_sprefix = "snapshot" # NormalizedEnv env = gym.make(args.env) env.seed(args.seed) torch.manual_seed(args.seed) env, generator, model, cont = get_functions(env, args) optimizer = optim.Adam(model.parameters(), lr=args.rllr) memory = Memory(args) agent = PPOAgent(args, model, optimizer, env, generator, memory, cont) if args.resume: agent.load_model(model_store_sprefix) agent.train(model_store_sprefix, args.save_interval)
def __init__(self, config_module=None): """ param:config_module: a scenario/robot specific module to prepare setup, that has the following members: get_all_conditions() -> return a list of conditions get_all_actions() -> return a list of actions """ self.memory = Memory() self.worldstate = WorldState() self.actions = set() if config_module is not None: for condition in config_module.get_all_conditions(self.memory): Condition.add(condition) for action in config_module.get_all_actions(self.memory): self.actions.add(action) self.planner = Planner(self.actions, self.worldstate, None) self._last_goal = None self._preempt_requested = False # preemption mechanism
def main(): memory = Memory() math = Math(memory) memory.set_math(math) while 42: command = input("> ") if command == "exit": break command = remove.whitespace(command) command = command.lower() validate.equation(command) if command.endswith('?'): math.calc(command[:-1]) else: memory.add_command(command) print(memory.array)
def run(env_id, seed, noise_type, **kwargs): # Create envs. env = gym.make(env_id) # Parse noise_type action_noise = None nb_actions = env.action_space.shape[-1] for current_noise_type in noise_type.split(','): current_noise_type = current_noise_type.strip() if current_noise_type == 'none': pass elif 'normal' in current_noise_type: _, stddev = current_noise_type.split('_') action_noise = NormalActionNoise(mu=np.zeros(nb_actions), sigma=float(stddev) * np.ones(nb_actions)) elif 'ou' in current_noise_type: _, stddev = current_noise_type.split('_') action_noise = OrnsteinUhlenbeckActionNoise( mu=np.zeros(nb_actions), sigma=float(stddev) * np.ones(nb_actions)) # Configure components. memory = Memory(limit=int(1e6), action_shape=env.action_space.shape, observation_shape=env.observation_space.shape) # Seed everything to make things reproducible. logger.info('seed={}, logdir={}'.format(seed, logger.get_dir())) if seed > 0: np.random.seed(seed) torch.manual_seed(seed) random.seed(seed) env.seed(seed) torch.cuda.manual_seed(seed) start_time = time.time() train(env=env, action_noise=action_noise, memory=memory, **kwargs) env.close() logger.info('total runtime: {}s'.format(time.time() - start_time))
def test_play_matches_neural_network(self): memory = Memory(config.MEMORY_SIZE) # At the beginning, we set a random model. It will be similar to an untrained CNN, and quicker. # We also set config.MCTS_SIMS, which is rather low, and will produce poor estimations from the MCTS. # The idea is encourage exploration and generate a lot of boards in memory, even if the probabilities # associated to their possible actions are wrong. # Memory is completed at the end of the game according to the final winner, in order to correct the values # of each move. All the moves of the winner receive value=1 and all the moves of the loser receive value=-1 # The neural network will learn to predict the probabilities and the values. # It will learn wrong probas and values at the beginning, but after some time, the CNN and the neural network # will improve from eachother and converge. player1 = Agent('cnn_agent_1', config.GRID_SHAPE[0] * config.GRID_SHAPE[1], config.GRID_SHAPE[1], config.MCTS_SIMS, config.CPUCT, GenRandomModel()) player2 = Agent('cnn_agent_2', config.GRID_SHAPE[0] * config.GRID_SHAPE[1], config.GRID_SHAPE[1], config.MCTS_SIMS, config.CPUCT, GenRandomModel()) scores, memory, points, sp_scores = play_matches.playMatches(player1, player2, config.EPISODES, lg.logger_main, turns_until_tau0 = config.TURNS_UNTIL_TAU0, memory = memory) # play_matches.playMatches() has copied stmemory to ltmemory, so we can clear stmemory safely memory.clear_stmemory() cnn1 = Residual_CNN(config.REG_CONST, config.LEARNING_RATE, (1,) + config.GRID_SHAPE, config.GRID_SHAPE[1], config.HIDDEN_CNN_LAYERS) cnn2 = Residual_CNN(config.REG_CONST, config.LEARNING_RATE, (1,) + config.GRID_SHAPE, config.GRID_SHAPE[1], config.HIDDEN_CNN_LAYERS) cnn2.model.set_weights(cnn1.model.get_weights()) cnn1.plot_model() player1.model = cnn1 ######## RETRAINING ######## player1.replay(memory.ltmemory) for _ in range(1): scores, memory, points, sp_scores = play_matches.playMatches(player1, player2, config.EPISODES, lg.logger_main, turns_until_tau0 = config.TURNS_UNTIL_TAU0, memory = memory) # play_matches.playMatches() has copied stmemory to ltmemory, so we can clear stmemory safely memory.clear_stmemory() player1.replay(memory.ltmemory) print('TOURNAMENT...') scores, _, points, sp_scores = play_matches.playMatches(player1, player2, config.EVAL_EPISODES, lg.logger_main, turns_until_tau0 = 0, memory = None) print('\nSCORES') print(scores) print('\nSTARTING PLAYER / NON-STARTING PLAYER SCORES') print(sp_scores)
def main(): parser = argparse.ArgumentParser(description='Emulates the 6502 chip.') parser.add_argument('--hex_file', required=True, help='Path to the hex file to be loaded in memory') parser.add_argument('--verbose', '-v', action='count', default=0) args = parser.parse_args() memory = Memory() cpu = CPU(memory) cpu.reset() memory.load_file(args.hex_file) while True: cpu.print_cpu_stats() command = raw_input("\nPress (R) to reset, (N) to execute next "\ "instruction, (M_[NUMBER]) to print a memory"\ " page or (Q) to quit: ") if command.upper() == 'R': cpu.reset() memory.load_file(args.hex_file) elif command.upper() == 'N': cpu.execute(1) elif command.upper() == 'Q': print('Good bye...') exit(0) elif command.upper().startswith('M_'): page_number = int(command.split('_')[1]) memory.dump_page(page_number) else: print('Command [%s] not recognized' % command)
def run(self): while self.global_ep.value < max_episode: self.local_model.pull_from_global_model(self.global_model) done = False score = 0 steps = 0 state = self.env.reset() state = torch.Tensor(state) state = state.unsqueeze(0) memory = Memory(n_step) while True: policy, value = self.local_model(state) action = self.get_action(policy, self.num_actions) next_state, reward, done, _ = self.env.step(action) next_state = torch.Tensor(next_state) next_state = next_state.unsqueeze(0) mask = 0 if done else 1 reward = reward if not done or score == 499 else -1 action_one_hot = torch.zeros(2) action_one_hot[action] = 1 memory.push(state, next_state, action_one_hot, reward, mask) score += reward state = next_state if len(memory) == n_step or done: batch = memory.sample() loss = self.local_model.push_to_global_model( batch, self.global_model, self.global_optimizer) self.local_model.pull_from_global_model(self.global_model) memory = Memory(n_step) if done: running_score = self.record(score, loss) break self.res_queue.put(None)
def open_app(app_name, cells, audio, arduino): current_app = None app_name = app_name.replace(" ", "") if app_name == 'riddles': current_app = Riddles("Riddles", cells, audio, arduino) elif app_name == 'learn': current_app = Learn("Learn", cells, audio, arduino) elif app_name == 'tutor': current_app = Tutor("Tutor", cells, audio, arduino) elif app_name == 'headlines': current_app = Headlines("Headlines", cells, audio, arduino) elif app_name == 'memory': current_app = Memory("Memory", cells, audio, arduino) if current_app is not None: audio.speak("Opening the application " + app_name) current_app.on_start() else: audio.speak( "I did not recognize the app. Could you try to open the app again?" )
def __init__(self): args = sys.argv self.inst_file = args[1] self.data_file = args[2] self.config_file = args[3] self.res_file = args[4] if len(args) != 5: self.print_help() sys.exit(1) self.instructions = [] self.scoreboards = [] self.clock_mgr = ClockMgr() self.memory_bus = MemoryBus() self.memory = Memory(self.data_file) self.reg_pc = 0 self.dcache = DCache(self.clock_mgr, self.memory_bus, self.memory) self.cpu = CPU(self.config_file) self.icache = ICache(self.cpu.icache_config[0], self.cpu.icache_config[1], self.clock_mgr, self.memory_bus)
def main(): environment_id, directory = argument_parsing() directory = os.path.join(directory, environment_id) environment = Environment(environment_id, directory) long_term_memory = LongTermMemory(directory) memory = long_term_memory.restoring_memory_object() if memory is None: memory = Memory(environment.observation_space, number_of_actions=environment.action_space.n) exploration_rate = long_term_memory.restoring_exploration_object() if exploration_rate is None: exploration_rate = 1 decision_maker = DecisionMaker( state_space=environment.observation_space, number_of_actions=environment.action_space.n, model_dir=directory, exploration_rate=exploration_rate) agent = Agent(environment, decision_maker, memory) agent.play() long_term_memory.saving_memory(memory) long_term_memory.saving_exploration_rate( decision_maker.get_exploration_rate())
def load_file(filename): global s_filename, again, memo, processes, times_to_compact_memory, n_processes, n_accesses s_filename = filename again = False processes = [] times_to_compact_memory = [] with open(filename) as f: lines = [line.strip() for line in f.readlines()] memo = Memory(lines[0].split()) n_accesses = 0 pid = 0 for line in lines[1:]: line = line.split() if (len(line) == 2): # COMPACTA times_to_compact_memory.append(int(line[0])) else: # Processo new_process = Process(pid, line, memo) processes.append(new_process) n_accesses += len(new_process.accesses) pid = (pid + 1) % 128 # Cada pid tem 8 bits times_to_compact_memory.sort() n_processes = len(processes) return True
class CPU(InstructionSet6502): register = CpuRegister() memory = Memory() def execute_instruction(self, op_code): pass def execute_pc(self, pc): pass def reset(self): pass def get_pc(self): pass def nmi(self, vector): # nmi中断 pass def irq(self, vector): # irq中断 pass
def open_app(self, app_name): current_app = None app_name = app_name.replace(" ", "") if app_name == 'riddles': current_app = Riddles("Riddles") elif app_name == 'learn': current_app = Learn("Learn") elif app_name == 'tutor': current_app = Tutor("Tutor") elif app_name == 'headlines': current_app = Headlines("Headlines") elif app_name == 'memory': current_app = Memory("Memory") if current_app is not None: glob.mainApp.audio.speak("Opening the application") glob.mainApp.audio.speak(current_app.name) current_app.on_start() else: # shouldn't occur glob.mainApp.audio.speak( "I did not recognize the app. Could you try to open the app again?" )
def setup(): ## Start Environment state_size = reduce(lambda x, y: x * y, env.observation_space.shape) # initialize memory mem_size = 2 * state_size + 1 + 1 + 1 # 1=action, 1=reward, 1 = done memory = Memory(mem_size, 10000) # default size = 10000 # get networks with tf.variable_scope('net') as scope: net = QNet(4) net.append(DenseLayer((4, 64))) net.append(ActivationLayer('tanh')) net.append(DenseLayer((64, 2))) net.setup() with tf.variable_scope('target') as scope: target_net = QNet(4) target_net.append(DenseLayer((4, 64))) target_net.append(ActivationLayer('tanh')) target_net.append(DenseLayer((64, 2))) target_net.setup() return net, target_net, memory
def run(env_id, exp_prefix, lr_p, lr_v, run_num, seed=1, gamma=.97): current_time = datetime.now().strftime('%b%d_%H-%M-%S') try: os.makedirs(F"./runs/{exp_prefix}") except OSError: pass logdir = F"./runs/{exp_prefix}/lr_p{lr_p}-lr_v{lr_v}-run_num{run_num}-{current_time}" writer = SummaryWriter(logdir) env = gym.make(env_id) memory = Memory(limit=int(1e6), action_shape=env.action_space.shape, observation_shape=env.observation_space.shape) critic = Critic(observation_shape=env.observation_space.shape) nactions = env.action_space.shape[-1] actor = Actor(nactions, observation_shape=env.observation_space.shape) torch.manual_seed(seed) train(env=env, memory=memory, actor=actor, critic=critic, writer=writer) writer.close()
def __init__(self, index, memory_size, batch_size, gamma, state_global, action_global, local=False): self.hidden = 200 self.memory = Memory(memory_size) self.state_dim = state_global[index] self.action_dim = action_global[index] self.Actor = Actor(self.state_dim, self.action_dim, self.hidden, self.hidden).to(device) # local决定是用局部信息还是全局信息,也决定是DDPG算法还是MADDPG算法 if not local: self.Critic = Critic(sum(state_global), sum(action_global), self.hidden, self.hidden).to(device) else: self.Critic = Critic(self.state_dim, self.action_dim, self.hidden, self.hidden).to(device) self.Actor_target = Actor(self.state_dim, self.action_dim, self.hidden, self.hidden).to(device) if not local: self.Critic_target = Critic(sum(state_global), sum(action_global), self.hidden, self.hidden).to(device) else: self.Critic_target = Critic(self.state_dim, self.action_dim, self.hidden, self.hidden).to(device) self.critic_train = torch.optim.Adam(self.Critic.parameters(), lr=LR_C) self.actor_train = torch.optim.Adam(self.Actor.parameters(), lr=LR_A) self.loss_td = nn.MSELoss() self.batch_size = batch_size self.gamma = gamma self.tau = 0.5 self.local = local
def __init__(self, global_scope="", current_scope=""): self.global_scope = global_scope self.current_scope = current_scope self.function_directory = FunctionDirectory() self.semantic_cube = SemanticCube() self.memory = Memory() self.temporal_variables = [] self.temporal_parameters_names = [] self.temporal_parameters_types = [] self.temporal_arguments_types = [] self.operand_stack = [] self.type_stack = [] self.operator_stack = [] self.quadruple_list = [] self.jump_list = [] self.return_list = [] self.temporal_variable_counter = 0 self.quadruple_number = 1 self.relational_operations = ['>', '<', '>=', '<=', '==', '!='] self.return_flag = False self.current_dimensioned_varible = {} self.dimensioned_varible_stack = [] self.dimensioned_varible_flag = False self.negation_stack = []
def __init__(self, environment, load_memory=True): self.action_space = Parameters.GAMES.get_action_space(Parameters.GAME) self.environment = environment if load_memory: self.memory = PrioritizedMemory() if Parameters.USE_PRIORITIZATION else Memory() self.step = 0 # select the type of DQN based on Parameters dqn_type = DDDQN if Parameters.USE_DDDQN else DQN # initialize the DQN and target DQN (with respective placeholders) self.dqn_input = tf.placeholder(tf.float32, [None, Parameters.IMAGE_HEIGHT, Parameters.IMAGE_WIDTH, Parameters.AGENT_HISTORY_LENGTH], name="DQN_input") self.dqn = dqn_type(self.dqn_input, self.action_space) self.target_dqn_input = tf.placeholder( tf.float32, [None, Parameters.IMAGE_HEIGHT, Parameters.IMAGE_WIDTH, Parameters.AGENT_HISTORY_LENGTH], name="target_DQN_input") self.target_dqn = dqn_type(self.target_dqn_input, self.action_space) # initialize the tensorflow session and variables self.tf_session = tf.Session() self.tf_saver = tf.train.Saver() self.load_session() self.initial_time = self.last_time = time.time() self.initial_step = self.step self.last_action = randint(0, self.action_space)
def main(): parser = argparse.ArgumentParser(description='NES Emulator.') parser.add_argument('rom_path', metavar='R', type=str, help='path to the nes rom') args = parser.parse_args() print(args.rom_path) with open(args.rom_path, 'rb') as file: rom_bytes = file.read() rom = ROM(rom_bytes) memory = Memory() ppu = PPU() cpu = CPU(memory, ppu) cpu.initialization() cpu.load_rom(rom)
def __init__(self): """Model of a standard contoller unit demonstrating the Texas 4-step (fetch,decode,execute,store) with methods for each.""" self.R1 = 0 # General purpose register to store final result. self.R2 = 0 # General purpose register to store file length. self.R3 = "" # Storage register to store mnemonic from memory. self.IR = 0 # Instruction register self.PC = 0 # Program counter/accumulator self.running = False # Machine state self.clock = time.time() # Start system clock self.ALU = ALU() # Arithmetic-logic units self.MEM = Memory() # System memory