diff --git a/README.md b/README.md index af017febfe..5a45b923e0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,99 @@ -# Earn Crypto with AI Agents: Prometheus 24/7 Builder Task (Beta v0) +import math -The **Prometheus 24/7 Builder Task** spins up an **AI agent** capable of continuously writing code, **earning you KOII**. Automated code writing agents can constantly build useful new products, increasing the value of the network _and_ your node. Our ultimate goal is to have **AI agents writing Koii tasks**, growing the network with **more opportunities for node operators to earn rewards**. +def calc_prob(data, symbol): + return data.count(symbol) / sum(data.count(x) for x in set(data)) -This repository is where our agents submit their completed code. You can see the results [here](https://github.com/koii-network/prometheus-beta/pulls). If you'd like to see how the agent works, the code is available in the [Prometheus 24/7 Builder repository](https://github.com/koii-network/builder-247). +def shannon_fano_tree(data): + if len(data) == 0: + return None + if len(data) == 1: + return Node(symbol=data[0], frequency=1.0, left=None, right=None) + + symbols = [symbol for symbol in set(data)] + freqs = [calc_prob(data, symbol) for symbol in symbols] + total_freq = sum(freqs) + + left_data, right_data, left_freqs, right_freqs = split_data(data, freqs, total_freq) + + left_node = shannon_fano_tree(left_data) + right_node = shannon_fano_tree(right_data) + + return Node( + symbol=None, + frequency=total_freq, + left=left_node, + right=right_node, + ) + +def split_data(data, freqs, total_freq): + left_freqs = [] + right_freqs = [] + left_data = [] + right_data = [] + + for i, freq in enumerate(freqs): + if freq / total_freq < 0.5: + left_freqs.append(freq) + left_data.extend([data[i]] * (len(data) * freq / total_freq)) + else: + right_freqs.append(freq) + right_data.extend([data[i]] * (len(data) * freq / total_freq)) + + return (left_data, right_data, left_freqs, right_freqs) + +class Node: + def __init__(self, symbol, frequency, left=None, right=None): + self.symbol = symbol + self.frequency = frequency + self.left = left + self.right = right + +def build_shannon_fano_tree(data): + return shannon_fano_tree(data) + +def generate_codes(node, code, code_dict): + if node is None: + return + + if node.symbol is not None: + code_dict[node.symbol] = code + return + + generate_codes(node.left, code + "0", code_dict) + generate_codes(node.right, code + "1", code_dict) + +def shannon_fano_compression(data): + tree = build_shannon_fano_tree(data) + code_dict = {} + generate_codes(tree, "", code_dict) + encoded_data = ''.join([code_dict[symbol] for symbol in data]) + return encoded_data + +def shannon_fano_decompression(encoded_data, tree): + result = [] + node = tree + + for bit in encoded_data: + if bit == "0": + node = node.left + else: + node = node.right + + if node.symbol is not None: + result.append(node.symbol) + node = tree + + return ''.join(result) + +def main(): + data = "this is an example of shannon fano compression" + encoded_data = shannon_fano_compression(data) + tree = build_shannon_fano_tree(data) + decompressed_data = shannon_fano_decompression(encoded_data, tree) + + print("Original data:", data) + print("Encoded data:", encoded_data) + print("Decompressed data:", decompressed_data) + +if __name__ == "__main__": + main() \ No newline at end of file