Skip to content

Instantly share code, notes, and snippets.

@JD-P
Created January 3, 2025 03:27
Show Gist options
  • Save JD-P/ea931e97288cb69d25fea4c81afbc8db to your computer and use it in GitHub Desktop.
Save JD-P/ea931e97288cb69d25fea4c81afbc8db to your computer and use it in GitHub Desktop.
class EntropyCollapsingProcess:
def __init__(self, initial_condition):
self.condition = initial_condition
self.history = []
def search(self, condition) -> tuple[float, Distribution]:
"""
Given current condition, returns:
- entropy: effective bits of uncertainty in the distribution
- action_dist: probability distribution over possible actions
"""
raise NotImplementedError
def sample_action(self, action_dist) -> Action:
"""Sample from the action distribution"""
return action_dist.sample()
def strengthen_condition(self, condition, action) -> Condition:
"""Update condition to include sampled action"""
return condition.union(action)
def step(self):
# Search surfaces a probability distribution with some entropy
entropy, action_dist = self.search(self.condition)
# Sample from the distribution
action = self.sample_action(action_dist)
# Update condition with sampled action
self.condition = self.strengthen_condition(self.condition, action)
# Track history
self.history.append({
'entropy': entropy,
'action': action
})
return entropy, action
def run_until_confident(self, entropy_threshold=0.1, max_steps=1000):
for _ in range(max_steps):
entropy, action = self.step()
if entropy < entropy_threshold: # Distribution is very peaked
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment