Untitled

 avatar
unknown
plain_text
9 months ago
2.3 kB
7
Indexable
import random
import math

def entropy_of_macro(state):
    length = len(state)
    ones = state.count('1')
    multiplicity = math.comb(length, ones)
    return math.log2(multiplicity)

def advance(state):
    k = random.randint(0, len(state) - 1)  # a random integer between 0 and len(state)-1 inclusive
    # Flip the kth character in the string state
    state_list = list(state)
    state_list[k] = '1' if state_list[k] == '0' else '0'
    return ''.join(state_list)

def record_data(state_len, timesteps):
    state = '0' * state_len  # string of length state_len
    history = []
    for _ in range(timesteps):
        history.append(entropy_of_macro(state))
        state = advance(state)
    return history

def get_distance(history):
    max_entropy = max(history)  # max value in history
    low_to_high_vals = []
    high_to_low_vals = []
    q = 0
    while q < len(history):
        if history[q] == 0:
            p = q  # place a marker p at this point
            # Find the nearest max entropy state to the right
            right = p + 1
            while right < len(history) and history[right] != max_entropy:
                right += 1
            if right >= len(history):
                q += 1
                continue  # this 0 point is no good
            # Find the nearest max entropy state to the left
            left = p - 1
            while left >= 0 and history[left] != max_entropy:
                left -= 1
            if left < 0:
                q += 1
                continue  # this 0 point is no good
            # Record distances
            low_to_high_vals.append(p - left)
            high_to_low_vals.append(right - p)
            q += 1  # Move q forward
        else:
            q += 1
    return low_to_high_vals, high_to_low_vals

def main():
    state_len = 8
    timesteps = 100000  0
    history = record_data(state_len, timesteps)
    low_to_high, high_to_low = get_distance(history)
    # Print average of low_to_high
    avg_low_to_high = sum(low_to_high) / len(low_to_high) if low_to_high else 0
    # Print average of high_to_low
    avg_high_to_low = sum(high_to_low) / len(high_to_low) if high_to_low else 0
    print(f"Average time from low to high entropy: {avg_low_to_high}")
    print(f"Average time from high to low entropy: {avg_high_to_low}")

main()
Editor is loading...
Leave a Comment