def descendants(self): """ All child nodes and all their child nodes. >>> from anytree import Node >>> udo = Node("Udo") >>> marc = Node("Marc", parent=udo) >>> lian = Node("Lian", parent=marc) >>> loui = Node("Loui", parent=marc) >>> soe = Node("Soe", parent=lian) >>> udo.descendants (Node('/Udo/Marc'), Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lian/Soe'), Node('/Udo/Marc/Loui')) >>> marc.descendants (Node('/Udo/Marc/Lian'), Node('/Udo/Marc/Lian/Soe'), Node('/Udo/Marc/Loui')) >>> lian.descendants (Node('/Udo/Marc/Lian/Soe'),) """ return tuple(PreOrderIter(self))[1:]
def prepare_archetype(self, a, archetypes): a.current = a.id == getattr(self, 'archetype', {}).get('id', None) if a.get('all_num_decks') is not None and a.get( 'season_num_decks') is not None: a.all_show_record = a.get('all_wins') or a.get( 'all_draws') or a.get('all_losses') a.season_show_record = a.get('season_wins') or a.get( 'season_draws') or a.get('season_losses') a.show_matchups = a.all_show_record a.url = '/archetypes/{id}/'.format(id=a.id) a.best_decks = Container({'decks': []}) n = 3 while len(a.best_decks.decks) == 0 and n >= 0: for d in a.get('decks', []): if d.get('stars_safe', '').count('★') >= n: a.best_decks.decks.append(d) n -= 1 a.show_best_decks = len(a.decks) != len(a.best_decks.decks) counter = Counter() a.cards = [] a.most_common_cards = [] for d in a.get('decks', []): a.cards += d.maindeck + d.sideboard for c in d.maindeck: if not c['card'].type.startswith('Basic Land'): counter[c['name']] += c['n'] most_common_cards = counter.most_common(NUM_MOST_COMMON_CARDS_TO_LIST) cs = oracle.cards_by_name() for v in most_common_cards: self.prepare_card(cs[v[0]]) a.most_common_cards.append(cs[v[0]]) a.has_most_common_cards = len(a.most_common_cards) > 0 a.archetype_tree = PreOrderIter(a) for r in a.archetype_tree: # Prune branches we don't want to show if r.id not in [a.id for a in archetypes]: r.parent = None r['url'] = '/archetypes/{id}/'.format(id=r['id']) # It perplexes me that this is necessary. It's something to do with the way NodeMixin magic works. Mustache doesn't like it. r['depth'] = r.depth
def prepare_archetype(self, a: archetype.Archetype, archetypes: List[archetype.Archetype]) -> None: a.current = a.id == getattr(self, 'archetype', {}).get('id', None) a.show_record = a.get('num_decks') is not None and (a.get('wins') or a.get('draws') or a.get('losses')) counter = Counter() # type: ignore a.cards = [] a.most_common_cards = [] # Make a pass, collecting card counts for all decks and for tournament decks for d in a.get('decks', []): a.cards += d.maindeck + d.sideboard for c in d.maindeck: if not c.card.type_line.startswith('Basic Land'): counter[c['name']] += c['n'] most_common_cards = counter.most_common(prepare.NUM_MOST_COMMON_CARDS_TO_LIST) cs = oracle.cards_by_name() for v in most_common_cards: prepare.prepare_card(cs[v[0]], getattr(self, 'tournament_only', False)) a.most_common_cards.append(cs[v[0]]) a.has_most_common_cards = len(a.most_common_cards) > 0 for b in [b for b in PreOrderIter(a) if b.id in [a.id for a in archetypes]]: b['url'] = url_for('.archetype', archetype_id=b['id']) # It perplexes me that this is necessary. It's something to do with the way NodeMixin magic works. Mustache doesn't like it. b['depth'] = b.depth
def MCTS(self, bel, d): h = self.T.name for a in range(0, self.model.acts): tmp = Node(self.T.name + str(a), parent=self.T, value=self.Q0, count=self.N0) numLoops = 100 for i in range(0, numLoops): s = np.random.choice([i for i in range(0, self.model.N)], p=bel) self.simulate(s, h, d) #RenderTreeGraph(self.T).to_picture('tree1.png'); #print(RenderTree(self.T)); QH = [0] * self.model.acts for a in range(0, self.model.acts): QH[a] = [ node.value for node in PreOrderIter( self.T, filter_=lambda n: n.name == h + str(a)) ][0] act = np.argmax([QH[a] for a in range(0, self.model.acts)]) return [act, QH[act]]
def prepare_archetype(self, a, archetypes): num_most_common_cards_to_list = 10 a.current = a.id == getattr(self, 'archetype', {}).get('id', None) if a.get('all') and a.get('season'): a.all.show_record = a.all.get('wins') or a.all.get( 'draws') or a.all.get('losses') a.season.show_record = a.season.get('wins') or a.season.get( 'draws') or a.season.get('losses') a.url = url_for('archetype', archetype_id=a.id) a.best_decks = Container({'decks': []}) n = 3 while len(a.best_decks.decks) == 0 and n >= 0: for d in a.get('decks', []): if d.get('stars_safe', '').count('★') >= n: a.best_decks.decks.append(d) n -= 1 counter = Counter() a.cards = [] a.most_common_cards = [] for d in a.get('decks', []): a.cards += d.maindeck + d.sideboard for c in d.maindeck: if not c['card'].type.startswith('Basic Land'): counter[c['name']] += c['n'] most_common_cards = counter.most_common(num_most_common_cards_to_list) cs = oracle.cards_by_name() for v in most_common_cards: self.prepare_card(cs[v[0]]) a.most_common_cards.append(cs[v[0]]) a.archetype_tree = PreOrderIter(a) for r in a.archetype_tree: # Prune branches we don't want to show if r.id not in [a.id for a in archetypes]: r.parent = None r['url'] = url_for('archetype', archetype_id=r['id']) # It perplexes me that this is necessary. It's something to do with the way NodeMixin magic works. Mustache doesn't like it. r['depth'] = r.depth
def get_node(self, node_name): for node in PreOrderIter(self.get_root()): if node.name == node_name: return node raise ValueError("Node {} not found".format(node_name))
def testMCTSSim(): trails = 10 trailLength = 100 allReward = np.zeros(shape=(trails, trailLength)).tolist() for count in range(0, trails): if (trails == 1): fig, ax = plt.subplots() totalReward = 0 a = OnlineSolver() b = np.random.rand(a.model.N).tolist() x = np.random.randint(0, a.model.N) b[x] = 3 b = a.normalize(b) for step in range(0, trailLength): if (trails == 1): ax.cla() ax.plot(b, linewidth=4) ax.scatter(x, .4, s=150, c='r') ax.set_ylim([0, .5]) ax.set_title('POMCP Belief') plt.pause(0.1) [act, u] = a.MCTS(b, 3) totalReward += a.model.R[act][x] x = np.random.choice([i for i in range(0, a.model.N)], p=a.model.px[act][x]) ztrial = [0] * len(a.model.pz) for i in range(0, len(a.model.pz)): ztrial[i] = a.model.pz[i][x] z = ztrial.index(max(ztrial)) b = a.beliefUpdate(b, act, z) a.T = [ node for node in PreOrderIter( a.T, filter_=lambda n: n.name == a.T.name + str(act) + str(z)) ][0] #RenderTreeGraph(a.T).to_picture('tree2.png'); a.T.parent = None #print(a.T); #RenderTreeGraph(a.T).to_picture('tree1.png'); allReward[count][step] = totalReward print(allReward[count][-1]) averageAllReward = [0] * trailLength for i in range(0, trails): for j in range(0, trailLength): averageAllReward[j] += allReward[i][j] / trails allSigma = [0] * trailLength for i in range(0, trailLength): suma = 0 for j in range(0, trails): suma += (allReward[j][i] - averageAllReward[i])**2 allSigma[i] = np.sqrt(suma / trails) UpperBound = [0] * trailLength LowerBound = [0] * trailLength for i in range(0, trailLength): UpperBound[i] = averageAllReward[i] + allSigma[i] LowerBound[i] = averageAllReward[i] - allSigma[i] x = [i for i in range(0, trailLength)] plt.figure() plt.plot(x, averageAllReward, 'g') plt.plot(x, UpperBound, 'g--') plt.plot(x, LowerBound, 'g--') plt.fill_between(x, LowerBound, UpperBound, color='g', alpha=0.25) plt.xlabel('Time Step') plt.ylabel('Accumlated Reward') plt.title('Average Accumulated Rewards over Time for: ' + str(trails) + ' simulations') plt.show()
def testMCTSSim2D(): trails = 10 trailLength = 100 allReward = np.zeros(shape=(trails, trailLength)).tolist() random = False for count in range(0, trails): ''' if(trails == 1): fig,ax = plt.subplots(); ''' totalReward = 0 a = OnlineSolver() x1 = np.random.randint(-5, 5) x2 = np.random.randint(-5, 5) x = [x1, x2] b = GM() b.addG(Gaussian(x, [[1, 0], [0, 1]], 1)) for step in range(0, trailLength): ''' if(trails == 1): ax.cla(); ax.plot(b,linewidth=4); ax.scatter(x,.4,s=150,c='r'); ax.set_ylim([0,.5]); ax.set_title('POMCP Belief'); plt.pause(0.1); ''' if (random): act = np.random.randint(0, 5) else: [act, u] = a.MCTS(b, 2) totalReward += a.model.r[act].pointEval(x) #x = np.random.choice([i for i in range(0,a.model.N)],p=a.model.px[act][x]); tmpGM = GM() tmpGM.addG( Gaussian((np.array(x) + np.array(a.model.delA[act])).tolist(), a.model.delAVar, 1)) x = tmpGM.sample(1)[0] ztrial = [0] * len(a.model.pz) for i in range(0, len(a.model.pz)): ztrial[i] = a.model.pz[i].pointEval(x) z = ztrial.index(max(ztrial)) b = a.beliefUpdate(b, act, z, a.model) if (not random): #RenderTreeGraph(a.T).to_picture('tree2.png'); a.T = [ node for node in PreOrderIter(a.T, filter_=lambda n: n.name == a .T.name + str(act) + str(z)) ][0] a.T.parent = None #print(a.T); #RenderTreeGraph(a.T).to_picture('tree1.png'); allReward[count][step] = totalReward print(allReward[count][-1]) averageAllReward = [0] * trailLength for i in range(0, trails): for j in range(0, trailLength): averageAllReward[j] += allReward[i][j] / trails allSigma = [0] * trailLength for i in range(0, trailLength): suma = 0 for j in range(0, trails): suma += (allReward[j][i] - averageAllReward[i])**2 allSigma[i] = np.sqrt(suma / trails) UpperBound = [0] * trailLength LowerBound = [0] * trailLength for i in range(0, trailLength): UpperBound[i] = averageAllReward[i] + allSigma[i] LowerBound[i] = averageAllReward[i] - allSigma[i] x = [i for i in range(0, trailLength)] plt.figure() plt.plot(x, averageAllReward, 'g') plt.plot(x, UpperBound, 'g--') plt.plot(x, LowerBound, 'g--') plt.fill_between(x, LowerBound, UpperBound, color='g', alpha=0.25) plt.xlabel('Time Step') plt.ylabel('Accumlated Reward') plt.title('Average Accumulated Rewards over Time for: ' + str(trails) + ' simulations') plt.show()
def simulate(self, s, h, d): if (d == 0): return 0 if (len([ node for node in PreOrderIter(self.T, filter_=lambda n: n.name == h) ]) == 0): newRoot = [ node for node in PreOrderIter( self.T, filter_=lambda n: n.name == h[0:len(h) - 2]) ][0] newName = h[0:len(h) - 2] for a in range(0, self.model.acts): if (len([ node for node in PreOrderIter( self.T, filter_=lambda n: n.name == newName + str(a)) ]) == 0): tmp = Node(h + str(a), parent=newRoot, value=self.Q0, count=self.N0) for o in range(0, self.model.acts): if (len([ node for node in PreOrderIter( self.T, filter_=lambda n: n.name == newName + str( a) + str(o)) ]) == 0): tmp2 = Node(h + str(a) + str(o), parent=tmp, value=self.Q0, count=self.N0) #tmp = Node(h,parent=newRoot,value = self.Q0,count=self.N0); return self.getRolloutReward(s, d) else: newRoot = [ node for node in PreOrderIter(self.T, filter_=lambda n: n.name == h) ][0] newName = h for a in range(0, self.model.acts): if (len([ node for node in PreOrderIter( self.T, filter_=lambda n: n.name == newName + str(a)) ]) == 0): tmp = Node(h + str(a), parent=newRoot, value=self.Q0, count=self.N0) for o in range(0, self.model.acts): if (len([ node for node in PreOrderIter( self.T, filter_=lambda n: n.name == newName + str( a) + str(o)) ]) == 0): tmp2 = Node(h + str(a) + str(o), parent=tmp, value=self.Q0, count=self.N0) QH = [0] * self.model.acts NH = [0] * self.model.acts NodeH = [0] * self.model.acts #print([node.name for node in PreOrderIter(self.T)]) #RenderTreeGraph(self.T).to_picture('tree1.png'); for a in range(0, self.model.acts): #print(h,a); QH[a] = [ node.value for node in PreOrderIter( self.T, filter_=lambda n: n.name == h + str(a)) ][0] NH[a] = [ node.count for node in PreOrderIter( self.T, filter_=lambda n: n.name == h + str(a)) ][0] NodeH[a] = [ node for node in PreOrderIter( self.T, filter_=lambda n: n.name == h + str(a)) ][0] aprime = np.argmax([ QH[a] + self.exploreParam * np.sqrt(np.log(sum(NH) / NH[a])) for a in range(0, self.model.acts) ]) [sprime, o, r] = self.generate(s, aprime) q = r + self.model.discount * self.simulate( sprime, h + str(aprime) + str(o), d - 1) NodeH[aprime].count += 1 NodeH[aprime].value += (q - QH[a]) / NH[a] return q