Skip to content

Instantly share code, notes, and snippets.

@Sohojoe
Last active October 27, 2023 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sohojoe/4068fe81ec3355a5f4190e10d8138cfc to your computer and use it in GitHub Desktop.
Save Sohojoe/4068fe81ec3355a5f4190e10d8138cfc to your computer and use it in GitHub Desktop.
screwing around with active inference
import numpy as np
# Define the complexity terms for each action
complexity_a1 = 0.1 # Lower complexity for fixing it yourself
complexity_a2 = 0.5 # Higher complexity for asking a colleague
# Initialize beliefs and probabilities with updated values
Q_a1_dynamic = 0.5 # Initial belief that you can fix the bug yourself
Q_a2_dynamic = 0.5 # Initial belief that your colleague can fix the bug
# Define the empirical priors
prior_a1 = 0.7 # Stronger initial preference for fixing it yourself
prior_a2 = 0.3 # Lower initial preference for asking a colleague
# Define a function to calculate the free energy for each action
def calculate_free_energy(Q, complexity, prior):
# Free Energy = -ln(Q) + Complexity - ln(Prior)
# Adding a small constant to avoid log(0)
epsilon = 1e-10
free_energy = -np.log(Q + epsilon) + complexity - np.log(prior + epsilon)
return free_energy
# Define the number of steps and rate of belief decay for a1
num_steps = 30
decay_rate_a1 = 0.2
# Initialize lists to store free energy values over time
free_energy_a1_over_time = []
free_energy_a2_over_time = []
action_history_complexity = []
Q_a1_dynamic_over_time = []
Q_a2_dynamic_over_time = []
time_needed_to_debug_with_college = 60
bug_fixed = False
# Calculate free energy over time
for step in range(num_steps):
# Calculate the free energy for each action at this step
free_energy_a1 = calculate_free_energy(Q_a1_dynamic, complexity_a1, prior_a1)
free_energy_a2 = calculate_free_energy(Q_a2_dynamic, complexity_a2, prior_a2)
# Store values
free_energy_a1_over_time.append(free_energy_a1)
free_energy_a2_over_time.append(free_energy_a2)
Q_a1_dynamic_over_time.append(Q_a1_dynamic)
Q_a2_dynamic_over_time.append(Q_a2_dynamic)
# Choose action with the lowest Free Energy
if free_energy_a1 < free_energy_a2:
action = "Try to fix myself"
# Update belief: your belief that you can fix it yourself decreases over time
Q_a1_dynamic *= (1 - decay_rate_a1)
else:
action = "Ask a colleague"
# Your belief that asking a colleague is effective
Q_a2_dynamic = Q_a2_dynamic - 0.05 if Q_a2_dynamic - 0.05 > 0 else 0.00001
# Update belief based on whether the bug was fixed or not
time_needed_to_debug_with_college -= 20
if time_needed_to_debug_with_college <= 0:
bug_fixed = True
action_history_complexity.append(action)
# Stop the simulation if the bug is fixed
if bug_fixed:
break
# Display the free energy values over time
# free_energy_a1_over_time, free_energy_a2_over_time
for i in range(len(action_history_complexity)):
print(f"{i} {action_history_complexity[i]} {free_energy_a1_over_time[i]:.2f} {free_energy_a2_over_time[i]:.2f} {Q_a1_dynamic_over_time[i]:.2f} {Q_a2_dynamic_over_time[i]:.2f}")
# Let's create a simplified Python script to act as a general-purpose decision-making tool based on active inference.
# Note: This is a very basic example and doesn't cover all the nuances of active inference.
import numpy as np
def calculate_free_energy(Q, complexity, prior):
epsilon = 1e-10
return -np.log(Q + epsilon) + complexity - np.log(prior + epsilon)
def decision_making_tool():
# Step 1: Identify the Problem
problem = input("What problem do you want to solve? ")
# Step 2: List Possible Actions
actions = input("List possible actions separated by commas: ").split(",")
# Initialize data structures
initial_beliefs = {}
empirical_priors = {}
complexities = {}
# Step 3: Identify Outcomes and Initial Beliefs
for action in actions:
belief = float(input(f"Initial belief that '{action.strip()}' will solve the problem (0 to 1): "))
initial_beliefs[action] = belief
# Step 4: Set Empirical Priors
for action in actions:
prior = float(input(f"Initial preference for '{action.strip()}' (0 to 1): "))
empirical_priors[action] = prior
# Step 5: Identify Complexity (Optional)
for action in actions:
complexity = float(input(f"Complexity of taking action '{action.strip()}' (higher means more complex): "))
complexities[action] = complexity
# Step 7: Run the Model to Recommend an Action
print("\nCalculating the best action to take...")
free_energies = {}
for action in actions:
free_energy = calculate_free_energy(initial_beliefs[action], complexities[action], empirical_priors[action])
free_energies[action] = free_energy
# Step 8: Provide Recommendations
recommended_action = min(free_energies, key=free_energies.get)
print(f"The recommended action to solve '{problem}' is: {recommended_action.strip()}")
# Run the decision-making tool
decision_making_tool()
import numpy as np
def calculate_free_energy(Q, complexity, prior):
epsilon = 1e-10
return -np.log(Q + epsilon) + complexity - np.log(prior + epsilon)
def update_belief(current_belief):
# Ask the user for feedback
outcome_happened = input("Did the desired outcome happen? (yes/no): ")
if outcome_happened.lower() == 'yes':
# Increase the belief (up to a maximum of 1)
new_belief = min(current_belief + 0.1, 1.0)
else:
# Decrease the belief (down to a minimum of 0)
new_belief = max(current_belief - 0.1, 0.0)
# Allow the user to manually adjust the confidence if desired
adjust_confidence = input("Would you like to manually adjust the confidence in this action? (yes/no): ")
if adjust_confidence.lower() == 'yes':
new_belief = float(input("Enter the new confidence level (0 to 1): "))
return new_belief
def decision_making_tool_multiple_steps():
# Same initial steps as before
problem = input("What problem do you want to solve? ")
actions = input("List possible actions separated by commas: ").split(",")
initial_beliefs = {}
empirical_priors = {}
complexities = {}
for action in actions:
belief = float(input(f"Initial belief that '{action.strip()}' will solve the problem (0 to 1): "))
initial_beliefs[action] = belief
prior = float(input(f"Initial preference for '{action.strip()}' (0 to 1): "))
empirical_priors[action] = prior
complexity = float(input(f"Complexity of taking action '{action.strip()}' (higher means more complex): "))
complexities[action] = complexity
# Loop for multiple timesteps
while True:
print("\nCalculating the best action to take...")
free_energies = {}
for action in actions:
free_energy = calculate_free_energy(initial_beliefs[action], complexities[action], empirical_priors[action])
free_energies[action] = free_energy
recommended_action = min(free_energies, key=free_energies.get)
print(f"The recommended action to solve '{problem}' is: {recommended_action.strip()}")
# Update beliefs based on user feedback
initial_beliefs[recommended_action] = update_belief(initial_beliefs[recommended_action])
# Ask if the user wants to continue
continue_decision = input("Would you like to continue? (yes/no): ")
if continue_decision.lower() == 'no':
break
# Run the extended decision-making tool
# Note: To run this, you'd need to copy the code into your local Python environment
# decision_making_tool_multiple_steps()
if __name__ == "__main__":
decision_making_tool_multiple_steps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment