Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # branch_predict.py
- import random
- import itertools
- test_data = [p for p in itertools.permutations(range(1, 10), 4)]
- test = []
- def branch_predict(choices=None, ans=None):
- global tree
- if not choices:
- tree = {'choices': []} # Reset the tree
- return None
- def predict(choices):
- current_node = tree
- for choice in choices:
- if choice not in current_node:
- current_node[choice] = {}
- current_node = current_node[choice]
- return current_node.get('prediction')
- if not ans:
- return 1 if predict(choices) else 0
- # Repair tree structure based on choices and ans
- node = tree
- for choice in choices:
- if choice not in node:
- node[choice] = {}
- node = node[choice]
- node['prediction'] = ans
- branch_predict()
- def test_elimination():
- global test
- def validate_list(numbers):
- contains_2_and_5 = (2 in numbers and 5 in numbers)
- contains_7_at_index_2_or_3 = 7 in (numbers[3:4])
- confidence = ''
- issue = ''
- if contains_2_and_5:
- ans = ('N', 0)
- issue = 'contains_2_and_5'
- elif contains_7_at_index_2_or_3:
- ans = ('N', 0)
- issue = 'contains_7_at_index_2_or_3'
- elif 10 < sum(chosen_numbers) < 20:
- ans = ('N', 0)
- issue = 'sum = ' + str(sum(chosen_numbers))
- else:
- ans = ('Y', 1)
- confidence = '...'
- predicted = branch_predict(numbers)
- stars = '*****'
- if predicted:
- confidence = '$$$'
- if predicted != ans[1]:
- stars = ' '
- branch_predict(numbers, ans)
- print(chosen_numbers, stars, [ans[1], predicted], confidence, ' ' + issue)
- for i in range(5000):
- if not test:
- test = test_data[:]
- random.shuffle(test)
- chosen_numbers = test.pop()
- guess = branch_predict(chosen_numbers)
- validate_list(chosen_numbers)
- test_elimination()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement