Robot Rumble ALPHA
discord
try it!
boards
tutorial
docs
login
/
signup
import random import math import hashlib import struct BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" def base62_encode(data: bytes) -> str: num = int.from_bytes(data, "big") if num == 0: return BASE62_ALPHABET[0] result = [] while num > 0: num, rem = divmod(num, 62) result.append(BASE62_ALPHABET[rem]) return ''.join(reversed(result)) def base62_decode(encoded: str) -> bytes: num = 0 for char in encoded: num = num * 62 + BASE62_ALPHABET.index(char) byte_length = (num.bit_length() + 7) // 8 return num.to_bytes(byte_length, "big") def compress_floats(float_list): quantized = [int((f + 1) * 32767.5) for f in float_list] byte_data = struct.pack('>' + 'H' * len(quantized), *quantized) encoded_data = base62_encode(byte_data) return encoded_data def decompress_floats(encoded_data): byte_data = base62_decode(encoded_data) quantized = struct.unpack('>' + 'H' * (len(byte_data) // 2), byte_data) return [(q / 32767.5) - 1 for q in quantized] def fingerprint(float_list): byte_data = struct.pack('>' + 'f' * len(float_list), *float_list) hash_obj = hashlib.sha256(byte_data) fingerprint = hash_obj.hexdigest() return fingerprint class Layer: def __init__(self, input_size, output_size, activation): self.weights = [[random.uniform(-1, 1) for _ in range(input_size)] for _ in range(output_size)] self.biases = [random.uniform(-1, 1) for _ in range(output_size)] self.activation = activation def forward(self, inputs): return [ self.activation(sum(w * inp for w, inp in zip(weights, inputs)) + bias) for weights, bias in zip(self.weights, self.biases) ] def get_weights(self): return [w for row in self.weights for w in row] + self.biases def set_weights(self, weights, biases): if len(weights) != len(self.weights) or len(biases) != len(self.biases): raise ValueError("Weights or biases dimensions do not match layer dimensions") self.weights = weights self.biases = biases class NeuralNetwork: def __init__(self, input_size, hidden_size, output_size): self.hidden_layer = Layer(input_size, hidden_size, activation=lambda x: math.tanh(x)) self.output_layer = Layer(hidden_size, output_size, activation=lambda x: math.tanh(x)) def forward(self, inputs): return self.output_layer.forward(self.hidden_layer.forward(inputs)) def get_parameters(self): return self.hidden_layer.get_weights() + self.output_layer.get_weights() def set_parameters(self, parameters): hidden_weight_size = len(self.hidden_layer.weights) * len(self.hidden_layer.weights[0]) hidden_bias_size = len(self.hidden_layer.biases) hidden_weights = [parameters[i:i+len(self.hidden_layer.weights[0])] for i in range(0, hidden_weight_size, len(self.hidden_layer.weights[0]))] hidden_biases = parameters[hidden_weight_size:hidden_weight_size + hidden_bias_size] self.hidden_layer.set_weights(hidden_weights, hidden_biases) output_weight_size = len(self.output_layer.weights) * len(self.output_layer.weights[0]) output_bias_size = len(self.output_layer.biases) output_weights = [parameters[i:i+len(self.output_layer.weights[0])] for i in range(hidden_weight_size + hidden_bias_size, hidden_weight_size + hidden_bias_size + output_weight_size, len(self.output_layer.weights[0]))] output_biases = parameters[-output_bias_size:] self.output_layer.set_weights(output_weights, output_biases) shared_state = [0.0, 0.0, 0.0, 0.0] network = NeuralNetwork(input_size=12, hidden_size=24, output_size=6) network.set_parameters(decompress_floats( "VhjoEADDOIdCKHgCBmjA4xq7Xg7HD4o0nQVYv3GKsQX0sckUdkKhnXbsOpZOhhNsqesE9jk0XCGeyq1uG85VKXj6oEYMOUbr4Hbw6ifBrWUYfsEXdDBkxxTQBhm78qWnHmyGxy8qBcNJVeElxNfPK00MIy2KNEXIqKHCBxdbO6VofRhpiBBpNTibnAojvnh2v8FkYLBtoTjlk1vjsMk7Wpjgs42Ml4Hyppa3fPg52gYe16TQHF3150MZQn7AVbGXQTSOSo2prsjrDVQPDqjO9H9frrS5yx9DOpA3YUGb9V6KuOdVomWZLk6LCGSnSanj6bnJuUN9SDvcqaHLyRg51tDNqnlfFWbuRLr4M5wP2pmxatY9OWck63MpPfn6V8GimFEZSOMjgls0Ci4DOTdckuFyTJlb6vRl4NDqtmI0uA0mxUosMGXBz60lZveEewRuLL78CRQZTYbCWpSX0cCJvG4uNKTzBV6uBMbVaxtgIJZCvz3CtN3PByxDjlbchLGXdDzkVRFvekdO1En7N6HcmDcAim7On0xtLVeW3GWG83PSOJoCGfcEh22qc6y2Not3lobS5Z8FQDh69bxP18MgyAPFDeRpTgHxInPmFqXRCS9WLKHQq9K7OXTjtoLc3jv0HlQ1MLFXCaKzoVrGRf2XPqRF81NFx9cyqsXKK5Jcpa7g7cOWUMGFHeZgYTMg0k2O36UM62mXm7dwPePN6JVzux3cJgeiHImQfGqrC2OCFe0GICn8pUSdlk5RosBgjL7bFgMlhs8UNXon5PigIbsxbE5Mo5Z3yQaimeacXQX1WyRjNcJ2sQBMO3BLEwRp6VhgmsupFG9eFwhSp1CaCH9QA4MuEVufl5PQVBBzUJHFt43EsPn4NpfTK0C778MQ3XxLPG2OOrAGD4uuIgfb2K9IAvsZopOSEfAZTlFAi4hMK4SZ6QWHyQcbhvvzH4Wn0ZNIuqDm7ShtXch9EDH9rGR21mQl7puMl8epFX8xOEXAJDO4cPTUGhJy542e38yZJwl6iC14evoZ3omPrdgsxHOudYieRe0sfALamX26vL2sH4OH2t5yxc4U1iCa01LDcVVnYry0ARNZdKURP11oXdODwEBhLvlKVRxkTGpMKb0pe3X3tJKtSjKXI1vV06GRD4BhbFksmz2eiWPgD1qhL0GfMLAMS1jKGRd7OBNv8TCFsHKmvycur9GQQdlgRQ4QNvrJRZKJIxc8bglQrz6AVIvOyrh8B" )) def health_by_coords(state, coord): obj = state.obj_by_coords(coord) if obj and obj.team is not None: return obj.health / 5 if obj.team == state.our_team else -obj.health / 5 return 0 def robot(state, unit): global shared_state inputs = [float(unit.coords.x) / 19, float(unit.coords.y) / 19] + [ health_by_coords(state, coord) for coord in unit.coords.coords_around() ] + shared_state output = network.forward(inputs) action_value, direction_value = output[:2] action_value, direction_value = (1 + action_value) / 2, (1 + direction_value) / 2 shared_updates = output[2:] action_type = ActionType.Move if action_value > 0.5 else ActionType.Attack direction = ( Direction.North if direction_value < 0.25 else Direction.South if direction_value < 0.5 else Direction.East if direction_value < 0.75 else Direction.West ) memory_decay = 0.3 shared_state[:] = [ math.tanh(memory_decay * s + (1 - memory_decay) * u) for s, u in zip(shared_state, shared_updates) ] return Action.move(direction) if action_type == ActionType.Move else Action.attack(direction)
Made with <3 by Anton and Noa
github org