Advertisement
here2share

# branch_predict.py

Dec 27th, 2023 (edited)
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. # branch_predict.py
  2.  
  3. import random
  4. import itertools
  5.  
  6. test_data = [p for p in itertools.permutations(range(1, 10), 4)]
  7. test = []
  8.  
  9. def branch_predict(choices=None, ans=None):
  10.     global tree
  11.  
  12.     if not choices:
  13.         tree = {'choices': []}  # Reset the tree
  14.         return None
  15.  
  16.     def predict(choices):
  17.         current_node = tree
  18.         for choice in choices:
  19.             if choice not in current_node:
  20.                 current_node[choice] = {}
  21.             current_node = current_node[choice]
  22.         return current_node.get('prediction')
  23.  
  24.     if not ans:
  25.         return 1 if predict(choices) else 0
  26.  
  27.     # Repair tree structure based on choices and ans
  28.     node = tree
  29.     for choice in choices:
  30.         if choice not in node:
  31.             node[choice] = {}
  32.         node = node[choice]
  33.     node['prediction'] = ans
  34. branch_predict()   
  35.  
  36. def test_elimination():
  37.     global test
  38.     def validate_list(numbers):
  39.         contains_2_and_5 = (2 in numbers and 5 in numbers)
  40.         contains_7_at_index_2_or_3 = 7 in (numbers[3:4])
  41.  
  42.         confidence = ''
  43.         issue = ''
  44.         if contains_2_and_5:
  45.             ans = ('N', 0)
  46.             issue = 'contains_2_and_5'
  47.         elif contains_7_at_index_2_or_3:
  48.             ans = ('N', 0)
  49.             issue = 'contains_7_at_index_2_or_3'
  50.         elif 10 < sum(chosen_numbers) < 20:
  51.             ans = ('N', 0)
  52.             issue = 'sum = ' + str(sum(chosen_numbers))
  53.         else:
  54.             ans = ('Y', 1)
  55.             confidence = '...'
  56.         predicted = branch_predict(numbers)
  57.         stars = '*****'
  58.         if predicted:
  59.             confidence = '$$$'
  60.         if predicted != ans[1]:
  61.             stars = '     '
  62.             branch_predict(numbers, ans)
  63.         print(chosen_numbers, stars, [ans[1], predicted], confidence, '   ' + issue)
  64.  
  65.     for i in range(5000):
  66.         if not test:
  67.             test = test_data[:]
  68.             random.shuffle(test)
  69.         chosen_numbers = test.pop()
  70.         guess = branch_predict(chosen_numbers)
  71.         validate_list(chosen_numbers)
  72.  
  73. test_elimination()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement