-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_usage.py
57 lines (45 loc) · 1.62 KB
/
basic_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Example demonstrating basic usage of FiberTree.
"""
from fbtree import create_tree, Move
def main():
# Create a tree in memory
tree = create_tree()
# Create and record some paths
# Path 1: A winning sequence
tree.start_path()
tree.add_move(Move("A"))
tree.add_move(Move("B"))
tree.add_move(Move("C"))
tree.record_outcome('win')
# Path 2: Another winning sequence
tree.start_path()
tree.add_move(Move("A"))
tree.add_move(Move("B"))
tree.add_move(Move("D"))
tree.record_outcome('win')
# Path 3: A losing sequence
tree.start_path()
tree.add_move(Move("A"))
tree.add_move(Move("E"))
tree.record_outcome('loss')
# Get the current full path
current_path = tree.get_complete_path()
print(f"Current path: {[str(move) for move in current_path]}")
# Get statistics for the current path
stats = tree.get_statistics()
print(f"Current path statistics: {stats}")
# Find the best continuation from a specific position
starting_path = [Move("A")]
best_moves = tree.get_best_continuation(starting_path, top_n=2)
print(f"Best continuations from 'A':")
for move_info in best_moves:
print(f" {move_info['move']} - Win rate: {move_info['win_rate']:.2f}, Visits: {move_info['visits']}")
# Get move frequency at first position
first_move_freq = tree.get_move_frequency(depth=1)
print(f"First move frequencies: {first_move_freq}")
# Save the tree to a file
tree.save("my_first_tree.json")
print("Tree saved to my_first_tree.json")
if __name__ == "__main__":
main()