def post_report(ticket_id): """ Posting a report to the database of reports. """ try: ticket = tickets.find_one({'id': ticket_id}) if ticket is None: ticket = scrape(ticket_id) if 'reports' not in ticket: ticket['reports'] = [] report = json.loads(request.form.get('report')) assert (isinstance(report, dict)), "report is not a dict" for fld in ['status', 'spkgs', 'base', 'machine', 'time']: assert (fld in report), "{} missing in report".format(fld) machine_name = report['machine'][-1] if machine_name in BLACKLIST: msg = 'machine {} is blacklisted'.format(machine_name) raise RuntimeError(msg) prune_pending(ticket, report['machine']) ticket['reports'].append(report) db.logs.put(request.files.get('log'), _id=log_name(ticket_id, report)) if 'retry' in ticket: ticket['retry'] = False ticket['last_activity'] = now_str() db.save_ticket(ticket) return "ok (report successfully posted)" except: traceback.print_exc() return "error in posting the report"
def post_report(ticket_id): try: ticket = db.lookup_ticket(ticket_id) if ticket is None: ticket = trac.scrape(ticket_id) if 'reports' not in ticket: ticket['reports'] = [] report = json.loads(request.form.get('report')) assert isinstance(report, dict) for fld in ['status', 'patches', 'spkgs', 'base', 'machine', 'time']: assert fld in report patchbot.prune_pending(ticket, report['machine']) ticket['reports'].append(report) db.logs.put(request.files.get('log'), _id=log_name(ticket_id, report)) if 'retry' in ticket: ticket['retry'] = False ticket['last_activity'] = now_str() db.save_ticket(ticket) return "ok" except: traceback.print_exc() return "error"
def scrape(ticket_id, force=False, db=None): """ Return available information about given ticket from the patchbot database, and update this information if necessary. The information is either taken from the patchbot database, or obtained from the trac database. If the trac-ticket-info has changed since the last update of the patchbot-ticket-info, then the patchbot-ticket-info is refreshed. If ``force`` is ``True``, it will update the patchbot-ticket-info even if the trac-ticket-info has not changed. OUTPUT: a dictionary EXAMPLES:: sage: scrape(18033) """ ticket_id = int(ticket_id) if ticket_id == 0: if db is not None: db_info = db.lookup_ticket(ticket_id) if db_info is not None: return db_info return { 'id': ticket_id, 'title': 'base', 'page_hash': '0', 'status': 'base', 'priority': 'base', 'component': 'base', 'depends_on': [], 'spkgs': [], 'authors': [], 'participants': []} # hash is defined from the rss of trac page rss = get_url("{}/ticket/{}?format=rss".format(TRAC_URL, ticket_id)) page_hash = digest(rss.encode('utf8')) # First try to use the patchbot database if db is not None: # TODO: perhaps the db caching should be extracted outside of # this function... db_info = db.lookup_ticket(ticket_id) if (not force and db_info is not None and db_info['page_hash'] == page_hash): return db_info # nothing in the database, now fetch the info from trac server trac_server = TracServer(Config()) trac_info = trac_server.load(ticket_id) # this part is about finding the authors and it needs work ! authors = set() git_commit_of_branch = git_commit(trac_info.branch) if trac_info.branch: branch = trac_info.branch if branch.startswith('u/'): authors.add((branch.split('/')[1]).strip()) authors = list(authors) authors_fullnames = set() for auth in trac_info.author.split(','): author = auth.strip() if author: authors_fullnames.add(author) authors_fullnames = list(authors_fullnames) data = { 'id': ticket_id, 'title': trac_info.title, 'page_hash': page_hash, 'status': trac_info.status, 'resolution': trac_info.resolution, 'milestone': trac_info.milestone, 'priority': trac_info.priority, 'component': trac_info.component, 'depends_on': extract_depends_on(trac_info.dependencies), 'spkgs': extract_spkgs(trac_info.description), 'authors': authors, 'authors_fullnames': authors_fullnames, 'participants': extract_participants(rss), 'git_branch': trac_info.branch, 'git_repo': TRAC_REPO if trac_info.branch.strip() else None, 'git_commit': git_commit_of_branch, 'last_activity': now_str(), } if db is not None: db.save_ticket(data) db_info = db.lookup_ticket(ticket_id) return db_info else: return data
def scrape(ticket_id, force=False, db=None): """ Scrapes the trac page for ticket_id, updating the database if needed. """ ticket_id = int(ticket_id) if ticket_id == 0: if db is not None: db_info = db.lookup_ticket(ticket_id) if db_info is not None: return db_info return { 'id' : ticket_id, 'title' : 'base', 'page_hash' : '0', 'status' : 'base', 'priority' : 'base', 'component' : 'base', 'depends_on' : [], 'spkgs' : [], 'patches' : [], 'authors' : [], 'participants' : [], } rss = get_url("%s/ticket/%s?format=rss" % (TRAC_URL, ticket_id)) page_hash = digest(rss) # rss isn't as brittle if db is not None: # TODO: perhaps the db caching should be extracted outside of this function... db_info = db.lookup_ticket(ticket_id) if not force and db_info is not None and db_info['page_hash'] == page_hash: return db_info # TODO: Is there a better format that still has all the information? html = get_url("%s/ticket/%s" % (TRAC_URL, ticket_id)) authors = set() patches = [] for patch, who in extract_patches(rss): authors.add(who) patches.append(patch + "#" + digest(get_patch(ticket_id, patch))) authors = list(authors) data = { 'id' : ticket_id, 'title' : extract_title(rss), 'page_hash' : page_hash, 'status' : extract_status(html), 'milestone' : extract_milestone(html), 'merged' : extract_merged(html), 'priority' : extract_priority(html), 'component' : extract_component(html), 'depends_on' : extract_depends_on(html), 'spkgs' : extract_spkgs(html), 'patches' : patches, 'authors' : authors, 'participants' : extract_participants(rss), 'last_activity' : now_str(), } if db is not None: db.save_ticket(data) db_info = db.lookup_ticket(ticket_id) return db_info else: return data
def train(): # setup =========================== max_episode = 300 # 学習において繰り返す最大エピソード数 max_step = 200 # 1エピソードの最大ステップ数 n_warmup_steps = 10000 # warmupを行うステップ数 interval = 1 # モデルや結果を吐き出すステップ間隔 actions_list = [-1, 1] # 行動(action)の取りうる値のリスト gamma = 0.99 # 割引率 epsilon = 0.1 # ε-greedyのパラメータ memory_size = 10000 batch_size = 32 result_dir = os.path.join('./result/pendulum', now_str()) # インスタンス作成 ================== os.makedirs(result_dir, exist_ok=True) print(result_dir) env = gym.make('Pendulum-v0') dim_state = env.env.observation_space.shape[0] q_network = Qnetwork(dim_state, actions_list, gamma=gamma) policy = EpsilonGreedyPolicy(q_network, epsilon=epsilon) header = ["num_episode", "loss", "td_error", "reward_avg"] recorder = RecordHistory(os.path.join(result_dir, "history.csv"), header) recorder.generate_csv() # warmup======================= print('warming up {:,} steps...'.format(n_warmup_steps)) memory = [] total_step = 0 step = 0 state = env.reset() while True: step += 1 total_step += 1 action = random.choice(actions_list) epsilon, q_values = 1.0, None next_state, reward, done, info = env.step([action]) # reward clipping if reward < -1: c_reward = -1 else: c_reward = 1 memory.append((state, action, c_reward, next_state, done)) state = next_state if step > max_step: state = env.reset() step = 0 if total_step > n_warmup_steps: break memory = memory[-memory_size:] print('warming up {:,} steps... done.'.format(n_warmup_steps)) # training====================== print('training {:,} episodes...'.format(max_episode)) num_episode = 0 episode_loop = True while episode_loop: num_episode += 1 step = 0 step_loop = True episode_reward_list, loss_list, td_list = [], [], [] state = env.reset() while step_loop: step += 1 total_step += 1 action, epsilon, q_values = policy.get_action(state, actions_list) next_state, reward, done, info = env.step([action]) # reward clipping if reward < -1: c_reward = -1 else: c_reward = 1 memory.append((state, action, c_reward, next_state, done)) episode_reward_list.append(c_reward) exps = random.sample(memory, batch_size) loss, td_error = q_network.update_on_batch(exps) loss_list.append(loss) td_list.append(td_error) q_network.sync_target_network(soft=0.01) state = next_state memory = memory[-memory_size:] # end of episode if step >= max_step: step_loop = False reward_avg = np.mean(episode_reward_list) loss_avg = np.mean(loss_list) td_error_avg = np.mean(td_list) print("{}episode reward_avg:{} loss:{} td_error:{}".format( num_episode, reward_avg, loss_avg, td_error_avg)) if num_episode % interval == 0: model_path = os.path.join( result_dir, 'episode_{}.h5'.format(num_episode)) q_network.main_network.save(model_path) history = { "num_episode": num_episode, "loss": loss_avg, "td_error": td_error_avg, "reward_avg": reward_avg } recorder.add_histry(history) if num_episode >= max_episode: episode_loop = False env.close() print('training {:,} episodes... done.'.format(max_episode))
async def train(self, pre_episodes=0, pre_total_step=0): total_step = pre_total_step all_rewards = [] result_dir = os.path.join('./logs/', util.now_str()) os.makedirs(result_dir, exist_ok=True) header = ["num_episode", "total_reward", "episode_length"] recorder = util.RecordHistory(os.path.join(result_dir, "history.csv"), header) recorder.generate_csv() for ep in range(pre_episodes + 1, self.config.episodes + 1): await util.sendCommand(util.COMMAND_MAP[util.Commands.RESET.value]) s0 = await util.getState() # s0 = self.env.reset() # self.agent.reset() done = False step = 0 actor_loss, critics_loss, reward = 0, 0, 0 done_count = 0 # decay noise self.agent.decay_epsilon() while done_count < 100: action = self.agent.get_action(s0) # translate action to motor speed here lms = int(action[0] * 127) rms = int(action[1] * 127) s1, r1, done, _ = await util.getNextState(lms, rms) # s1, r1, done = self.env.step(action) if done: done_count += 1 self.agent.buffer.add(s0, action, r1, done, s1) s0 = s1 if self.agent.buffer.size() > self.config.batch_size: loss_a, loss_c = self.agent.learning() actor_loss += loss_a critics_loss += loss_c reward += r1 step += 1 total_step += 1 if step + 1 > self.config.max_steps: break all_rewards.append(reward) avg_reward = float(np.mean(all_rewards[-100:])) self.board_logger.scalar_summary('Reward per episode', ep, all_rewards[-1]) self.board_logger.scalar_summary( 'Best 100-episodes average reward', ep, avg_reward) print( 'total step: %5d, episodes %3d, episode_step: %5d, episode_reward: %5f' % (total_step, ep, step, reward)) history = { "num_episode": ep, "total_reward": reward, "episode_length": step, } recorder.add_histry(history) # check point if self.config.checkpoint and ep % self.config.checkpoint_interval == 0: self.agent.save_checkpoint(ep, total_step, self.outputdir) # save model at last self.agent.save_model(self.outputdir) asyncio.get_event_loop().stop()
def scrape(ticket_id, force=False, db=None): """ Scrapes the trac page for ticket_id, updating the database if needed. """ ticket_id = int(ticket_id) if ticket_id == 0: if db is not None: db_info = db.lookup_ticket(ticket_id) if db_info is not None: return db_info return { 'id' : ticket_id, 'title' : 'base', 'page_hash' : '0', 'status' : 'base', 'priority' : 'base', 'component' : 'base', 'depends_on' : [], 'spkgs' : [], 'patches' : [], 'authors' : [], 'participants' : [], } rss = get_url("%s/ticket/%s?format=rss" % (TRAC_URL, ticket_id)) tsv = parse_tsv(get_url("%s/ticket/%s?format=tab" % (TRAC_URL, ticket_id))) page_hash = digest(rss) # rss isn't as brittle if db is not None: # TODO: perhaps the db caching should be extracted outside of this function... db_info = db.lookup_ticket(ticket_id) if not force and db_info is not None and db_info['page_hash'] == page_hash: return db_info authors = set() patches = [] if tsv['branch'].strip(): # TODO: query history branch = tsv['branch'] if branch.startswith('u/'): authors.add(branch.split('/')[1]) else: for patch, who in extract_patches(rss): authors.add(who) patches.append(patch + "#" + digest(get_patch(ticket_id, patch))) authors = list(authors) data = { 'id' : ticket_id, 'title' : tsv['summary'], 'page_hash' : page_hash, 'status' : tsv['status'], 'resolution' : tsv['resolution'], 'milestone' : tsv['milestone'], 'merged' : tsv['merged'], 'priority' : tsv['priority'], 'component' : tsv['component'], 'depends_on' : extract_depends_on(tsv), 'spkgs' : extract_spkgs(tsv), 'patches' : patches, 'authors' : authors, 'participants' : extract_participants(rss), 'git_branch' : tsv['branch'], 'git_repo' : TRAC_REPO if tsv['branch'].strip() else None, 'git_commit' : git_commit(tsv['branch']), 'last_activity' : now_str(), } if db is not None: db.save_ticket(data) db_info = db.lookup_ticket(ticket_id) return db_info else: return data