From 240c81dcf6386f84434f83c0eaadf6af93d25c86 Mon Sep 17 00:00:00 2001 From: alexmorten Date: Wed, 6 Mar 2019 15:08:44 +0100 Subject: [PATCH 1/2] simple cell figting mechanic --- cell_computing_service.py | 6 ++++++ cis_config.py | 2 ++ cis_env.py | 39 +++++++++++++++++++++++++++++++++++++++ dna_decoding.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/cell_computing_service.py b/cell_computing_service.py index d37f107..372df10 100644 --- a/cell_computing_service.py +++ b/cell_computing_service.py @@ -22,6 +22,7 @@ def ComputeCellInteractions(self, incoming_batch, context): new_cells = [] id_to_cell = {} id_to_cell_moved = {} + id_combination_to_distance_checked = {} id_to_cell_energy_averaged = {} for c in incoming_batch.cells_to_compute: id_to_cell[c.id] = c @@ -34,6 +35,11 @@ def ComputeCellInteractions(self, incoming_batch, context): c, id_to_cell, id_to_cell_moved) # Interaction + # figting + + for c in incoming_batch.cells_to_compute: + cis_env.eat_other_cells(c, incoming_batch.cells_to_compute, id_to_cell, id_combination_to_distance_checked) + # Energy for c in incoming_batch.cells_to_compute: cis_env.feed_cell( diff --git a/cis_config.py b/cis_config.py index 7c8396a..85cf484 100644 --- a/cis_config.py +++ b/cis_config.py @@ -17,3 +17,5 @@ CONNECTION_LIKELYHOOD = 0.5 DNA_SUBSLICE_CHANCE = 0.4 WANTED_CELL_AMOUNT_PER_BUCKET = 200 +CELL_EATING_DISTANCE = 20 +CELL_EATING_STRENGTH = 10 diff --git a/cis_env.py b/cis_env.py index 616b4ca..05888cb 100644 --- a/cis_env.py +++ b/cis_env.py @@ -2,6 +2,7 @@ import random import numpy as np import dna_decoding +import math from cis_cell import random_vector_of_length @@ -82,3 +83,41 @@ def group_connected_cells(group, cell, cell_dict): group.append(other_cell) group_connected_cells(group, other_cell, cell_dict) return group + + +def eat_other_cells(cell, other_cells, cell_dict, already_checked_dict): + cell_group = group_connected_cells([cell], cell, cell_dict) + for other_cell in other_cells: + _value, already_checked = get_value(already_checked_dict, key_combination(other_cell.id, cell.id)) + if already_checked: + next + if in_distance(cell.pos, other_cell.pos, conf.CELL_EATING_DISTANCE): + eat_both_ways(cell, other_cell) + + already_checked_dict[key_combination(cell.id, other_cell.id)] = True + + +def eat_both_ways(cell, other_cell): + cell_eating_strength = dna_decoding.eating_strength(cell.dna, other_cell.dna) + other_cell_eating_strength = dna_decoding.eating_strength(other_cell.dna, cell.dna) + transfer_amount = int(cell_eating_strength - other_cell_eating_strength) + cell.energy_level += transfer_amount + other_cell.energy_level -= transfer_amount + + +def in_distance(pos, other_pos, distance): + d_x = abs(pos.x - other_pos.x) + if d_x > distance: + return False + d_y = abs(pos.y - other_pos.y) + if d_y > distance: + return False + d_z = abs(pos.z - other_pos.z) + if d_z > distance: + return False + + return math.sqrt(d_x * d_x + d_y * d_y + d_z * d_z) < distance + + +def key_combination(id, other_id): + return "{0}/{1}".format(id, other_id) diff --git a/dna_decoding.py b/dna_decoding.py index 770663f..7385c97 100644 --- a/dna_decoding.py +++ b/dna_decoding.py @@ -24,9 +24,32 @@ def feature_in_dna(dna, feature, shift_by=0, start_at=0): feature_hit_count += bin(overlap).count('1') feature_copy = shift_bits_by(feature_copy, shift_by) max_hits = len(dna) * bin(feature).count('1') + if max_hits == 0: + return 0 return feature_hit_count / max_hits +def eating_strength(dna, other_dna): + feature = eating_strength_feature(dna) + dna_diff = dna_difference(dna, other_dna) + return feature_in_dna(dna_diff, feature) + + +def eating_strength_feature(dna): + feature = 0 + for b in dna: + feature = feature ^ b + return feature + + +def dna_difference(dna, other_dna): + a = int.from_bytes(dna, byteorder='big') + b = int.from_bytes(dna, byteorder='big') + diff = a ^ b + + return bytes([diff]) + + def feature_value_with_connections(dna, feature, current_connection_count): shift_param = current_connection_count % 8 start_at_param = int(current_connection_count / 8) % len(dna) @@ -124,3 +147,9 @@ def random_bool_with_threshold(threshold): def mutate_bit_array(bit_array): random_index = random.randint(0, len(bit_array) - 1) bit_array[random_index] = not bit_array[random_index] + + +def element_or_zero(byte_arr, index): + if index >= len(byte_arr): + return 0 + return byte_arr[index] From 0052f89bfa3fe0d870a6ccb171352112895d1de0 Mon Sep 17 00:00:00 2001 From: alexmorten Date: Thu, 7 Mar 2019 19:32:20 +0100 Subject: [PATCH 2/2] only eat closest cell --- cell_computing_service.py | 2 +- cis_env.py | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cell_computing_service.py b/cell_computing_service.py index d8dc792..5602136 100644 --- a/cell_computing_service.py +++ b/cell_computing_service.py @@ -43,7 +43,7 @@ def ComputeCellInteractions(self, incoming_batch, context): # Fighting for c in incoming_batch.cells_to_compute: - cis_env.eat_other_cells(c, incoming_batch.cells_to_compute, id_to_cell, id_combination_to_distance_checked) + cis_env.eat_closest_other_cell(c, incoming_batch.cells_to_compute, id_to_cell, id_combination_to_distance_checked) # Energy # Get Energy diff --git a/cis_env.py b/cis_env.py index 601f8c1..7d57148 100644 --- a/cis_env.py +++ b/cis_env.py @@ -123,16 +123,23 @@ def group_connected_cells(group, cell, cell_dict): return group -def eat_other_cells(cell, other_cells, cell_dict, already_checked_dict): +def eat_closest_other_cell(cell, other_cells, cell_dict, already_checked_dict): + min_distance = None + closest_cell = None cell_group = group_connected_cells([cell], cell, cell_dict) for other_cell in other_cells: _value, already_checked = get_value(already_checked_dict, key_combination(other_cell.id, cell.id)) if already_checked: next if in_distance(cell.pos, other_cell.pos, conf.CELL_EATING_DISTANCE): - eat_both_ways(cell, other_cell) + dist_to_other = distance(cell.pos, other_cell.pos) + if min_distance is None or dist_to_other < min_distance: + min_distance = dist_to_other + closest_cell = other_cell + already_checked_dict[key_combination(cell.id, other_cell.id)] = True - already_checked_dict[key_combination(cell.id, other_cell.id)] = True + if closest_cell is not None: + eat_both_ways(cell, closest_cell) def eat_both_ways(cell, other_cell): @@ -156,6 +163,11 @@ def in_distance(pos, other_pos, distance): return math.sqrt(d_x * d_x + d_y * d_y + d_z * d_z) < distance +def distance(pos, other_pos): + d_x = pos.x - other_pos.x + d_y = pos.y - other_pos.y + d_z = pos.z - other_pos.z + return math.sqrt(d_x * d_x + d_y * d_y + d_z * d_z) def key_combination(id, other_id): return "{0}/{1}".format(id, other_id)