def print_most_selected_path(self, delimiter='->'): """ Returns a string that visualizes the path in the tree that has been taken most frequently. This can be useful for debugging. """ sb = StringBuilder() best_val = np.inf best_child = None best_action = None best_prob = 0.0 most_observations = 0 for state in self.children: child = self.children[state] prob = self.successor_state_distribution[state] if most_observations < child.observations: most_observations = child.observations best_child = self.children[state] best_val = best_child.value best_prob = prob if best_val == -np.inf: best_val = 0.0 sb.append("(p={prob},v={val}){d}".format(prob=best_prob, val=best_val, d=delimiter)) if best_child is not None: sb.append(best_child.print_most_selected_path(delimiter=delimiter)) return sb.to_string()
def print_most_selected_path(self, delimiter='->'): """ Returns a string that visualizes the path in the tree that has been taken most frequently. This can be useful for debugging. """ sb = StringBuilder() most_selections = 0.0 best_child = None best_action = None for action in self.children: if self.children[action].value is not None: if most_selections < self.children[action].observations: most_selections = self.children[action].observations best_child = self.children[action] best_val = best_child.value best_action = action if best_child is not None: sb.append("(a={action},v={val}){d}".format(action=best_action, val=best_val, d=delimiter)) sb.append(best_child.print_most_selected_path(delimiter=delimiter)) else: sb.append("Leaf decision node" + delimiter) return sb.to_string()
def join_words_builder(words: List[str]) -> str: """Joins words using a StringBuilder""" builder = StringBuilder() for word in words: builder.append(word) return builder.to_string()
def pretty_print(self, depth, num_tabs=0, new_lines=False): """ Returns a string that pretty prints the tree rooted from this node and values to a certain depth. This can be useful for debugging. Args: depth: the depth of the search tree to print out num_tabs: the amount of indentation use when printing this level new_lines: if we should add new lines to try make it more readable Returns: String, a pretty printed tree, using D for decision nodes and C for chance nodes. We use parenthesis '()' to denote children and use square parenthesis '[]' to denote the value of a node and how many times it has been selected. """ sb = StringBuilder() indent = '\t' * num_tabs sb.append("D[") if isinstance(self.value, float): sb.append("{val:.3f}".format(val=self.value)) else: sb.append("{val}".format(val=self.value)) sb.append(",") sb.append(str(self.observations)) sb.append("](") if depth == 0: sb.append("...)") return sb.to_string() first_iter = True for action in self.children: if not first_iter: sb.append(", ") else: first_iter = False if new_lines: sb.append("\n") sb.append(indent) sb.append(action) sb.append("->") child_node_string = self.children[action].pretty_print( depth=depth - 1, num_tabs=num_tabs + 1, new_lines=new_lines) sb.append(child_node_string) sb.append(")") return sb.to_string()
def pretty_print(self): """ Returns a pretty string describing the environment """ sb = StringBuilder() cell_width = int(np.floor(np.log10(self.max_treasure))) + 1 row_delimiter = '+' + (('-'*cell_width) + '+') * self.num_cols + '\n' for row in range(self.num_rows): sb.append(row_delimiter) for col in range(self.num_cols): sb.append('|') if row < self.depths[col]: sb.append(' '*cell_width) elif row > self.depths[col]: sb.append(' ' + 'x'*(cell_width-2) + ' ') else: sb.append(str(self.treasure[col]).zfill(cell_width)) sb.append('|\n') sb.append(row_delimiter) sb.append("Transition Noise: {tn}\n\n".format(tn=self.transition_noise)) return sb.to_string()