def PostContext(self): tUser = self.GetUser() tDonor = DonorRecord() tDonatorRsName = self.request.get('name') #logging.debug("Quantity Entered: " + str(self.request.get('quantity'))) tDonatorAmount = int(self.request.get('quantity')) tDonatorNote = self.request.get('memo') tDonatorForumName = self.request.get('fname') tFormEmail = self.request.get('email') tAgentGold = self.request.get('agentgold') tDonor.donorAgent = tUser.email() tDonor.donorForumName = tDonatorForumName tDonor.donorMemo = tDonatorNote tDonor.donorRsName = tDonatorRsName tDonor.donorGoldAmount = int(tDonatorAmount) tDonor.put() tAgent = Agent() try: if (len(tFormEmail) > 0): tAgent = Agent().GetAgentByEmail(tFormEmail) else: tAgent = Agent().GetAgentByEmail(str(tUser.email())) except: tAgent = Agent().GetAgentByEmail(str(tUser.email())) if (tAgent.agentGoldSupply == None): tAgent.agentGoldSupply = 0 if (tDonatorAmount < 0): #logging.debug(str(tAgentGold)) tDonatorAmount = tDonatorAmount / 1000000.0 if (tAgentGold == 'on'): tCommission = tDonatorAmount * 0.65 * -1.0 else: tAgent.agentGoldSupply += int(tDonatorAmount * 1000000.0) tCommission = tDonatorAmount * 0.05 * -1.0 #logging.debug("Agent Commission: " + str(tDonatorAmount * -1.0)) tAgent.agentCurrentCommission += tCommission tAgent.agentTotalCommission += tCommission else: tAgent.agentGoldSupply += tDonatorAmount tAgent.put() return {}
def post(self): tAgent = Agent() tUser = users.get_current_user() if (tUser): tAgent = Agent().GetAgentByEmail(tUser.email()) tAgent.put() #logging.debug("Action by Agent: " + tUser.email()) if (tAgent.agentOnline == False): self.response.out.write("offline") else: self.response.out.write("online")
def test_train(self): env = Env(balance=250000, FX_DATA_FILE='../data/raw/FX_Demo/sample_USD_JPY_S5.pickle') agent = Agent(input_data_shape=(10, )) mount_agent = Agent(actions=10, input_data_shape=(10, )) print(len(env.fx_time_data_buy)) trainer = Trainer_priority(env, agent, mount_agent, data_end_index=len(env.fx_time_data_buy) - 2) trainer.train()
def PostContext(self): tContext = {} if (self.IsUserAdmin()): try: tAgentKey = self.request.get('custid') tAgent = Agent() tAgent = Agent().get(tAgentKey) tAgent.agentCurrentCommission = 0.0 tAgent.put() self.response.out.write("Success") except: self.response.out.write("Error") tContext['nowrite'] = True return tContext
def post(self): tUrl = "https://api-3t.paypal.com/nvp" tPaypalPayload = {} tPaypal = PaypalRefund() tAgent = Agent() tOrder = Order() tUser = users.get_current_user() tTransId = str(self.request.get('orderid')) tAgentEmail = str(tUser.email()) tAgent = Agent().GetAgentByEmail(tAgentEmail) tRefundAgent = tAgentEmail tOrderQuery = Order.all() tOrderQuery.filter("orderTransactionId", tTransId) #logging.debug("Transaction id: " + tTransId) tOrder = tOrderQuery.get() if (tOrder.orderDeliver != 'True'): tPaypalPayload['METHOD'] = "RefundTransaction" tPaypalPayload['TRANSACTIONID'] = tTransId tPayloadEncoded = tPaypal.GeneratePayload(tPaypalPayload) request_cookies = mechanize.CookieJar() request_opener = mechanize.build_opener( mechanize.HTTPCookieProcessor(request_cookies)) request_opener.addheaders = [('Content-Type', 'application/x-www-form-urlencoded')] mechanize.install_opener(request_opener) tResponse = mechanize.urlopen(url=tUrl, timeout=25.0, data=tPayloadEncoded) #logging.debug("Mechanize Package") #logging.debug("Url: " + tUrl) #logging.debug("Data: " + str(tPaypalPayload)) tResult = tResponse.read() #logging.debug(tResult) tOrder.orderIsRefunded = "True" tOrder.orderRefundAgent = tRefundAgent tOrder.orderLocked = "True" tOrder.orderRefundId = "" tOrder.put() self.response.out.write("Order Locked and Refunded")
def main(): env = gym.make(FLAGS.env_name) agent = Agent(num_actions=env.action_space.n, config=FLAGS) if FLAGS.train: # Train mode for _ in range(FLAGS.num_episodes): terminal = False observation = env.reset() for _ in range(random.randint(1, FLAGS.no_op_steps)): last_observation = observation observation, _, _, _ = env.step(0) # Do nothing state = agent.get_initial_state(observation, last_observation) while not terminal: last_observation = observation action = agent.get_action(state) observation, reward, terminal, _ = env.step(action) # env.render() processed_observation = preprocess(observation, last_observation) state = agent.run(state, action, reward, terminal, processed_observation) else: # Test mode # env.monitor.start(ENV_NAME + '-test') for _ in range(FLAGS.num_episodes_at_test): terminal = False observation = env.reset() for _ in range(random.randint(1, FLAGS.no_op_steps)): last_observation = observation observation, _, _, _ = env.step(0) # Do nothing state = agent.get_initial_state(observation, last_observation) while not terminal: last_observation = observation action = agent.get_action_at_test(state) observation, _, terminal, _ = env.step(action) env.render() processed_observation = preprocess(observation, last_observation) state = np.append(state[1:, :, :], processed_observation, axis=0)
def PostContext(self): tContext = {} tAgent = Agent() tUser = self.GetUser() tAgent = Agent.GetAgentByEmail(tUser.email()) tSaveAgent = False tNewNick = self.request.get('nick') tSoundOpt = self.request.get('sound') if (tNewNick != None and len(tNewNick) > 0): tAgent.agentNickName = tNewNick tSaveAgent = True if (tSoundOpt != None and len(tSoundOpt) > 0): tSoundOpt = str(tSoundOpt) if (tSoundOpt == "true"): tAgent.agentSoundPreference = 'True' tSaveAgent = True elif (tSoundOpt == "false"): tAgent.agentSoundPreference = 'False' tSaveAgent = True if (tSaveAgent): tAgent.put() self.LOCATION = "/" self.REDIRECT = True return tContext
def GetContext(self): tAgent = Agent() tAgentList = [] tAgentOrders = [] tAgentDonations = [] tAgentRequest = self.request.get('agent') context = {} tAgentList = Agent.all() context['agents'] = tAgentList if (tAgentRequest != ""): tAgent = Agent.get(tAgentRequest) tAgentOrdersQuery = Order.all() tAgentOrdersQuery.filter('orderAgent', str(tAgentRequest)) tAgentOrdersQuery.order('-orderCompleted') tAgentOrders = tAgentOrdersQuery.fetch(100) tAgentDonorsQuery = DonorRecord.all() tAgentDonorsQuery.filter('donorAgent', tAgent.agentId) tAgentDonorsQuery.order('-donorDate') tAgentDonations = tAgentDonorsQuery.fetch(100) #logging.debug("Agent Order Count: " + str(len(tAgentOrders))) #logging.debug("Agent Donation Count: " + str(len(tAgentDonations))) context['agent'] = tAgent context['orders'] = tAgentOrders context['donations'] = tAgentDonations context['extended'] = 'True' return context
def run(params, task_id, job_dir, name=None): keys = params.keys() agent_keys = [key for key in keys if 'agent' in key] meta = params.pop('__meta__') verbose = meta['verbose'] trial_num = meta['trial_num'] protocol = meta['protocol'] experiment_name = meta['experiment_name'] prepare_environment(meta) # Load Agents Based on Model agents = [] for agent_key in agent_keys: agent_params = params.pop(agent_key) agents += [Agent(agent_dict=agent_params, name=agent_key)] params['agents'] = agents # Load Protocol and Train (Results callback will collect results) module_name = 'protocols.%s.train' % (protocol) train = getattr(import_module(module_name), 'train') info, results = train(**params, verbose=False) # AFTER DONE TRAINING SAVE RESULTS FILE results.insert(0, {'experiment_name': experiment_name, 'protocol': protocol, 'trial_num': trial_num, **info}) if name is not None: result_file = '%s.npy' % name else: result_file = '%i.npy' % task_id np.save(result_file, results) save_file(job_dir, result_file)
def agent_worker(config, policy, learner_w_queue, global_episode, n_agent, log_dir, training_on, replay_queue, update_step): agent = Agent(config, policy, global_episode=global_episode, n_agent=n_agent, log_dir=log_dir) agent.run(training_on, replay_queue, learner_w_queue, update_step)
def main(): # Training settings parser = argparse.ArgumentParser(description='PyTorch Poly Echo') parser = add_experiment_args(parser) args = parser.parse_args() np.set_printoptions(formatter={'float': '{: 0.3f}'.format}) params_file = os.path.join(WORK_DIR, '%s.json' % args.task_id) with open(params_file) as file: params = json.load(file) keys = params.keys() agent_keys = [key for key in keys if 'agent' in key] num_agents = len(agent_keys) meta = params.pop('__meta__') verbose = meta['verbose'] trial_num = meta['trial_num'] protocol = meta['protocol'] experiment_name = meta['experiment_name'] prepare_environment(meta) agents = [] for agent_key in agent_keys: agent_params = params.pop(agent_key) agents += [Agent(agent_dict=agent_params, name=agent_key)] if num_agents == 2: A = agents[0] B = agents[1] else: A = agents[0] B = agents[0] # Load Protocol module_name = 'protocols.%s.trainer' % (protocol) trainer = getattr(import_module(module_name), 'trainer') total_batches_sent = 0 results, symbols_sent = [], [] test_SNR_dbs = get_test_SNR_dbs()[ params['bits_per_symbol']]['ber_roundtrip'] num_iterations = params['num_iterations'] total_iterations = num_iterations + (1 if num_agents == 2 else 0) for i in range(total_iterations): B, A, batches_sent = trainer(agents=[A, B], bits_per_symbol=params['bits_per_symbol'], batch_size=params['batch_size'], train_SNR_db=params['train_SNR_db'], signal_power=params['signal_power'], backwards_only=(i == num_iterations)) total_batches_sent += batches_sent if i % params['results_every'] == 0: result = test(agent1=agents[0], agent2=agents[1] if num_agents == 2 else None, bits_per_symbol=params['bits_per_symbol'], test_SNR_dbs=test_SNR_dbs, signal_power=params['signal_power'], test_batch_size=params['test_batch_size']) test_bers = result['test_bers'] print('Test: Roundtrip BER: ', test_bers) symbols_sent += [total_batches_sent * params['batch_size']] results += [result]
def get(self): tAgent = Agent() tAgentsOnline = [] tCurrentTime = datetime.datetime.now() tIncrement = datetime.timedelta(minutes = -30) tTime = tIncrement + tCurrentTime tAgentQuery = Agent().all() tAgentQuery.filter("agentOnline", True) tAgentQuery.filter("agentLastActive <", tTime) tAgentsOnline = tAgentQuery.fetch(10) if (len(tAgentsOnline) > 0): for tAgent in tAgentsOnline: tAgent.agentOnline = False tAgent.put()
def get(self): tAgent = Agent() tUser = users.get_current_user() if (tUser): tAgent = Agent().GetAgentByEmail(tUser.email()) if (tAgent.agentOnline == True): if (tAgent.agentNotify == True): tAgent.agentNotify = False tAgent.put() self.response.out.write("1") elif (tAgent.agentNotify == False): self.response.out.write("0") else: self.response.out.write("0") tAgent.agentNotify = False tAgent.put() else: self.response.out.write("2")
def agent_worker(config, policy, learner_w_queue, global_episode, i, agent_type, experiment_dir, training_on, replay_queue, update_step): agent = Agent(config, policy=policy, global_episode=global_episode, n_agent=i, agent_type=agent_type, log_dir=experiment_dir) agent.run(training_on, replay_queue, learner_w_queue, update_step)
def test_evaluate(self): self.env = Env( balance=250000, FX_DATA_FILE='../data/raw/FX_Demo/sample_USD_JPY_S5.pickle') self.agent = Agent() self.agent.model.compile(optimizer=Adam(), loss="mse") state = (self.env.balance, self.env.stock_balance) y = self.agent.evaluate(state=state) assert y.shape == (3, ) assert any(y) is True
def post(self): tOrderKey = self.request.get('orderid') #logging.debug("tOrderKey: " + tOrderKey) tPaOrder = PaOrder() tPaOrder = PaOrder.get(tOrderKey) tUser = users.get_current_user() tAgent = Agent().GetAgentByEmail(str(tUser.email())) if (tPaOrder.paOrderDeliver == False and tPaOrder.paOrderLock == False and tAgent.agentIsEnabled == True): tGoldAmount = tPaOrder.paAmountInt tGoldAmountLong = tGoldAmount tGoldAmount = tGoldAmount / 1000000 if (tAgent.agentGoldSupply == None): tAgent.agentGoldSupply = 0 tCommission = tGoldAmount * 0.05 + 0.50 tAgent.agentGoldSupply = int( tAgent.agentGoldSupply) - int(tGoldAmountLong) tAgent.agentCurrentCommission = tAgent.agentCurrentCommission + tCommission tAgent.agentTotalCommission = tAgent.agentTotalCommission + tCommission tAgentOrders = tAgent.agentOrders #Add order to agent pa orders tAgentOrders.append(tOrderKey) tAgent.agentOrders = tAgentOrders tAgent.agentCurrentOrderTotal = tAgent.agentCurrentOrderTotal + 1 tAgentKey = tAgent.put() tPaOrder.paDeliveryAgent = str(tAgent.agentId) tPaOrder.paDeliveryAgentNick = tAgent.agentNickName tPaOrder.paOrderDeliver = True tPaOrder.paOrderLock = True tKey = tPaOrder.put() #logging.debug("Delivery by Agent: " + str(tAgentKey)) #logging.debug("Delivery of Order: " + str(tKey)) self.response.headers[ 'Cache-Control'] = 'Cache-Control: no-cache, must-revalidate' self.response.headers['Content-Type'] = 'Content-Type: plain/text' self.response.out.write("Order Delivered") else: #logging.debug('Attempted to Deliver ' + tOrderKey + " by Agent " + tAgent.agentId) self.response.headers[ 'Cache-Control'] = 'Cache-Control: no-cache, must-revalidate' self.response.headers['Content-Type'] = 'Content-Type: plain/text' self.response.out.write("Order Not Deliverable")
def get(self): tAgentQuery = Agent().all() tAgentQuery.filter('agentOnline', True) tAgents = tAgentQuery.fetch(10) if (len(tAgents) > 0): self.response.out.write(str(len(tAgents))) else: self.response.out.write(str(0)) exit
def test_act(self): self.env = Env( balance=250000, FX_DATA_FILE='../data/raw/FX_Demo/sample_USD_JPY_S5.pickle') self.agent = Agent() self.agent.model.compile(optimizer=Adam(), loss="mse") state = (self.env.balance, self.env.stock_balance) action = self.agent.act(state, epsilon=0.1) action_state = False if action == 0 or action == 1 or action == 2: action_state = True assert action_state is True
def GetContext(self): tContext = {} if (self.IsUserAdmin()): tAgentsQuery = Agent().all() tAgentsQuery.order("agentNickName") tAgentsQuery.filter("agentIsEnabled", True) tAgents = tAgentsQuery.fetch(100) tAgent = Agent() for tAgent in tAgents: tAgent.__setattr__( 'agentGoldSupplyEocString', NumberToGp.ConvertIntToBet(tAgent.agentGoldSupplyEoc)) tAgent.__setattr__( 'agentGoldSupply07String', NumberToGp.ConvertIntToBet(tAgent.agentGoldSupply07)) tContext['agents'] = tAgents return tContext else: self.redirect("/")
def main(): parser = argparse.ArgumentParser( description='Execute train reinforcement learning.') parser.add_argument( '--dataset_name', type=str, default="../data/raw/FX_Demo/sample10000_USD_JPY_S5.pickle", help='an integer for the accumulator') args = parser.parse_args() print(args.dataset_name) env = Env(balance=250000, FX_DATA_FILE=args.dataset_name) agent = Agent(input_data_shape=(10, )) mount_agent = Agent(actions=10, input_data_shape=(10, )) trainer = Trainer(env, agent, mount_agent, Adam(lr=1e-6), data_end_index=len(env.fx_time_data_buy) - 2) trainer.train()
def GetAssignedAgent(self, pOrder=None): tAgent = Agent() tPaypal = PaypalOrder() tAgents = [] Switch = {} tOrder = Order() tOrder = pOrder #Need to implement these methods #Switch[(1,2)] = tPaypal.UseFullAndBackupAgents #Switch[(0,2)] = tPaypal.UseBackupAgent #Switch[(2,2)] = tPaypal.UseFullAgent Switch[(0, 0)] = tPaypal.AssignNoAgent Switch[(0, 1)] = tPaypal.UseBackupAgent Switch[(1, 0)] = tPaypal.UseFullAgent Switch[(1, 1)] = tPaypal.UseFullAndBackupAgents Switch[(2, 0)] = tPaypal.UseFullAgent Switch[(2, 1)] = tPaypal.UseFullAndBackupAgents Switch[(3, 0)] = tPaypal.UseFullAgent Switch[(3, 1)] = tPaypal.UseFullAgent #Based on the raw online numbers of each group tCurrentState = (tPaypal.GetNumberofOnlineFullAgents(), tPaypal.GetNumberofOnlineBackupAgents()) #logging.debug("Current State" + str(tCurrentState)) #The end agent will be handled in each function tAgent = Switch[tCurrentState]() if (tOrder != None): try: #logging.debug("Agent Current Total: " + str(tAgent.agentCurrentOrderTotal)) #logging.debug("Order Quantity: " + str(tOrder.orderQuantity)) tAgent.agentCurrentOrderTotal = tAgent.agentCurrentOrderTotal + int( tOrder.orderQuantity) #logging.debug("New Agent Current Total: " + str(tAgent.agentCurrentOrderTotal)) tAgent.agentNotify = True tAgent.put() #logging.debug("GetAssignedAgent returning agent: " + str(tAgent.agentId)) return tAgent.agentId except: #logging.debug("Hit an error") return "No Agent Online" else: try: return str(tAgent.agentId) except: return "No Agent Online"
def UseFullAgent(self): tAgent = Agent() tAgents = [] tPaypal = PaypalOrder() tOnlineAgents = tPaypal.GetNumberofOnlineFullAgents() tAvailableAgents = tPaypal.GetNumberofAvailableFullAgents() if (tOnlineAgents > 0 and tAvailableAgents == 0): tPaypal.ResetOnlineAgents() tAgents = tPaypal.GetAvailableFullAgents() try: tAgent = tAgents[0] return tAgent except: return "No Agent Online"
def post(self): tUser = users.get_current_user() tAgent = Agent().GetAgentByEmail(tUser.email()) tStatus = tAgent.agentOnline if (tStatus == False): tAgent.agentOnline = True tAgent.agentCurrentOrderTotal = 100 tReturn = "You're Online!" else: tAgent.agentOnline = False tReturn = "You're Offline!" tAgent.put() self.response.out.write(tReturn) exit
def UseFullAndBackupAgents(self): tAgent = Agent() tAgents = [] tPaypal = PaypalOrder() tOnlineBackups = tPaypal.GetNumberofOnlineBackupAgents() tAvailableBackups = tPaypal.GetNumberofAvailableBackupAgents() tOnlineAgents = tPaypal.GetNumberofOnlineFullAgents() tAvailableAgents = tPaypal.GetNumberofAvailableFullAgents() tTotalOnline = tOnlineAgents + tOnlineBackups tTotalAvailable = tAvailableAgents + tAvailableBackups if (tTotalOnline > 0 and tTotalAvailable == 0): tPaypal.ResetOnlineAgents() tAgents = tPaypal.GetAvailableAgents() try: tAgent = tAgents[0] return tAgent except: return "No Agent Online"
def UseBackupAgent(self): #logging.debug("UseBackupAgent Called") tAgent = Agent() tAgents = [] tPaypal = PaypalOrder() tOnlineBackups = tPaypal.GetNumberofOnlineBackupAgents() tAvailableBackups = tPaypal.GetNumberofAvailableBackupAgents() #logging.debug("Online Backups: " + str(tOnlineBackups)) #logging.debug("Available Backups: " + str(tAvailableBackups)) if (tOnlineBackups > 0 and tAvailableBackups == 0): #logging.debug("Resetting Online Agents") tPaypal.ResetOnlineAgents() tAgents = tPaypal.GetAvailableBackupAgents() try: tAgent = tAgents[0] #logging.debug("UseBackupAgent Returning " + str(tAgent.agentId)) return tAgent except: #logging.debug("Error in UseBackupAgent") return "No Agent Online"
def add_agent(self): self.db.new_agent(Agent(rnd_fullname(), rnd_salary()))
def post(self): tUser = self.GetUser() locale.setlocale(locale.LC_ALL, "") tContext = {} tContext['login'] = users.create_login_url(self.request.uri) tContext['logout'] = users.create_logout_url(self.request.uri) tContext['error'] = '' tContext['TIME'] = str(datetime.datetime.now()) if (tUser == None and self.REQUIRE_AUTH_POST == True): if (self.GetLocation() != "../views/index.html"): self.redirect("/") else: tTemplate = os.path.join(os.path.dirname(__file__), "../views/index.html") self.response.out.write(render(tTemplate, tContext)) return else: self.USER = tUser tContext['user'] = tUser tPostContext = self.PostContext() tContext.update(tPostContext) tLocation = self.GetLocation() tRedirect = self.GetRedirect() #logging.debug("User:"******"Context: " + str(tContext)) #logging.debug("Location: " + str(tLocation)) #logging.debug("Redirect: " + str(tRedirect)) #if(tContext.has_key('agent')): #tAgent = tContext['agent'] #else: try: tAgent = Agent().GetAgentByEmail(str(tUser.email())) except: tAgent = Agent() if (tAgent.agentSoundDelay == None or tAgent.agentSoundDelay == ""): tAgent.agentSoundDelay = 10000 if (tAgent.agentSoundSelection == None or tAgent.agentSoundSelection == ""): tAgent.agentSoundSelection = "beep" if (tAgent.agentSoundRepeat == None or tAgent.agentSoundRepeat == ""): tAgent.agentSoundRepeat = 1 #logging.debug("Sound delay: " + str(tAgent.agentSoundDelay)) #logging.debug("Sound selection: " + str(tAgent.agentSoundSelection)) #logging.debug("Sound repeat: " + str(tAgent.agentSoundRepeat)) tContext['agent'] = tAgent if (tContext.has_key('nowrite')): if (tContext['nowrite'] == True): return else: if (tRedirect == False): tTemplate = os.path.join(os.path.dirname(__file__), tLocation) self.response.out.write(render(tTemplate, tContext)) else: self.redirect(tLocation)
def get(self): tUser = self.GetUser() locale.setlocale(locale.LC_ALL, "") tContext = {} tContext['login'] = users.create_login_url(self.request.uri) tContext['logout'] = users.create_logout_url(self.request.uri) tContext['error'] = '' tContext['TIME'] = str(datetime.datetime.now()) if (tUser == None): if (self.GetLocation() != "../views/index.html"): self.redirect("/") else: tContext['error'] = 'Login is required to access the portal' tTemplate = os.path.join(os.path.dirname(__file__), "../views/index.html") self.response.out.write(render(tTemplate, tContext)) return else: self.USER = tUser tContext['user'] = tUser tContext.update(self.GetContext()) tLocation = self.GetLocation() tRedirect = self.GetRedirect() #logging.debug("User: "******"Context: " + str(tContext)) #logging.debug("Location: " + str(tLocation)) #logging.debug("Redirect: " + str(tRedirect)) #if(tContext.has_key('agent')): # tAgent = tContext['agent'] #else: try: tAgent = Agent().GetAgentByEmail(str(tUser.email())) except: tAgent = Agent() if (tAgent.agentSoundDelay == None or tAgent.agentSoundDelay == ""): tAgent.agentSoundDelay = 10000 if (tAgent.agentSoundSelection == None or tAgent.agentSoundSelection == ""): tAgent.agentSoundSelection = "beep" if (tAgent.agentSoundRepeat == None or tAgent.agentSoundRepeat == ""): tAgent.agentSoundRepeat = 1 #logging.debug("Sound delay: " + str(tAgent.agentSoundDelay)) #logging.debug("Sound selection: " + str(tAgent.agentSoundSelection)) #logging.debug("Sound repeat: " + str(tAgent.agentSoundRepeat)) tContext['agent'] = tAgent logging.debug('Context: ' + str(tContext)) if tAgent.agentIsAdmin: tContext['isAdmin'] = 'True' if (tAgent.agentIsEnabled == False): tContext['error'] = 'Your agent access is not active' tTemplate = os.path.join(os.path.dirname(__file__), "../views/index.html") self.response.out.write(render(tTemplate, tContext)) return if (tRedirect == False): tTemplate = os.path.join(os.path.dirname(__file__), tLocation) self.response.out.write(render(tTemplate, tContext)) else: self.redirect(tLocation)
def Agent(self): return Agent(self)
def run(jobs_file, job_id=None, plot=False, echo_symlink_to=None, job_date=None): # pr.enable() with open(jobs_file) as jfile: jobs = json.load(jfile) if isinstance(jobs, dict): # ToDo: make this more explicit, but basically, you can give me a json of SINGLE param dict, aka rerun a # single job that was spit out (ex: echo/experiments/gradient_passing/QPSK_neural_and_neural/results/0.json) jobs = [jobs] elif job_id is not None: # 0 = False you dummy plot = plot jobs = [jobs[job_id]] else: # NO PLOTTING IF YOU ARE RUNNING A BUNCH OF JOBS...NO! plot = False for params in jobs: params_copy = deepcopy(params) keys = params.keys() agent_keys = [key for key in keys if 'agent' in key] meta = params.pop('__meta__') verbose = meta['verbose'] job_id = meta['job_id'] trial_num = meta['trial_num'] protocol = meta['protocol'] experiment_name = meta['experiment_name'] experiment_dir = os.path.abspath(os.path.join(ECHO_DIR, 'experiments', protocol, experiment_name)) results_dir = os.path.abspath(os.path.join(experiment_dir, 'results')) # DEAL WITH SYMLINKING FOR RUNNING ON BRC if echo_symlink_to is not None: assert os.path.isdir(echo_symlink_to), "Invalid symlink path" if os.path.isdir(results_dir) and not os.path.islink(results_dir): old_results_dir = os.path.abspath(os.path.join(experiment_dir, 'old_results')) os.makedirs(old_results_dir, exist_ok=True) n = len(os.listdir(old_results_dir)) os.rename(results_dir, os.path.abspath(os.path.join(old_results_dir, '%i' % n))) _experiment_dir = os.path.abspath(os.path.join(echo_symlink_to, 'experiments', protocol, experiment_name)) job_date = "results" + (job_date if job_date is not None else "") _results_dir = os.path.abspath(os.path.join(_experiment_dir, job_date)) os.makedirs(_results_dir, exist_ok=True) if os.path.islink(results_dir) and os.readlink(results_dir) != _results_dir: try: os.remove(results_dir) except OSError: pass if not os.path.islink(results_dir): try: os.symlink(_results_dir, results_dir) except OSError as e: if e.errno == errno.EEXIST: assert os.readlink(results_dir) == _results_dir else: raise e else: os.makedirs(results_dir, exist_ok=True) results_file = '%s/%i.npy' % (results_dir, job_id) if os.path.isfile(results_file) and plot: print("result already found") else: params_file = '%s/%i.json' % (results_dir, job_id) with open(params_file, 'w') as pf: pf.write(json.dumps(params_copy, indent=4)) if verbose: print("...running run_experiment.py with:", protocol, experiment_name) prepare_environment(meta) # Load Agents Based on Model agents = [] for agent_key in agent_keys: agent_params = params.pop(agent_key) agents += [Agent(agent_dict=agent_params, name=agent_key, verbose=verbose)] params['agents'] = agents # Load Protocol and Train (Results callback will collect results) module_name = 'protocols.%s.train' % (protocol) train = getattr(import_module(module_name), 'train') info, results = train(**params, verbose=verbose, plot_callback=lambda **kwargs: None) # AFTER DONE TRAINING SAVE RESULTS FILE results.insert(0, {'protocol': protocol, 'trial_num': trial_num, 'experiment_name': experiment_name, **info}) np.save(results_file, results) if verbose: print("...params for this job have been saved into:", params_file) print("...results for this job have been saved into:", results_file) # pr.disable() # pr.dump_stats('%s%i.pstat'% (experiment_name,job_id) ) if plot: from importlib import util if util.find_spec('matplotlib') is not None: from plot_experiment import animated_plot animated_plot(results=results) else: print("Cannot plot; matplotlib not found") return ()