Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple cell figting mechanic #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cell_computing_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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
Expand All @@ -39,6 +40,12 @@ def ComputeCellInteractions(self, incoming_batch, context):

# Interaction


# Fighting
for c in incoming_batch.cells_to_compute:
cis_env.eat_closest_other_cell(c, incoming_batch.cells_to_compute, id_to_cell, id_combination_to_distance_checked)

# Energy
# Get Energy
food_fac = conf.WANTED_CELL_AMOUNT_PER_BUCKET / len(incoming_batch.cells_to_compute)
for c in incoming_batch.cells_to_compute:
Expand Down
2 changes: 2 additions & 0 deletions cis_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
CONNECTION_LIKELYHOOD = 0.5
DNA_SUBSLICE_CHANCE = 0.4
WANTED_CELL_AMOUNT_PER_BUCKET = 200
CELL_EATING_DISTANCE = 20
CELL_EATING_STRENGTH = 10
WAVE_PROPAGATION = 299792458
51 changes: 51 additions & 0 deletions cis_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import numpy as np
import dna_decoding
import math
from cis_cell import random_vector_of_length


Expand Down Expand Up @@ -120,3 +121,53 @@ def group_connected_cells(group, cell, cell_dict):
group.append(other_cell)
group_connected_cells(group, other_cell, cell_dict)
return group


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):
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

if closest_cell is not None:
eat_both_ways(cell, closest_cell)


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 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)
29 changes: 29 additions & 0 deletions dna_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]