From 0ad23a8d7d6bf479ae902b64ed88e4eba5e977c4 Mon Sep 17 00:00:00 2001 From: Auric Vente Date: Sat, 20 Jul 2024 06:55:15 -0600 Subject: [PATCH] Mods --- README.md | 10 ++-------- cromulant/ants.py | 28 ++++++++++++---------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 05766ca..8696c9a 100644 --- a/README.md +++ b/README.md @@ -32,21 +32,15 @@ You watch who gets the most triumphs or the most hits. The ant with the highest score is shown in the footer. -Clicking the footer scrolls to the bottom. - -There is a filter to filter updates. - ## Usage Just open it, hatch some ants, and place it somewhere in your monitor. ## Algorithm -A random ant from ants that haven't had an update in at least 10 minutes is picked. +A random ant is picked based on weights (oldest update date weighs more). -Or any ant is picked if none meet that requirement. - -Then a number between 1 and 10 is randomly picked. +Then a random number between 1 and 10 is picked. For each number an action happens to produce an update. diff --git a/cromulant/ants.py b/cromulant/ants.py index 77dec3b..508b42e 100644 --- a/cromulant/ants.py +++ b/cromulant/ants.py @@ -147,25 +147,21 @@ class Ants: if Ants.empty(): return None - if len(Ants.ants) == 1: - return Ants.ants[0] - - ignore = [] - current = Ants.get_current() - - if current: - ignore.append(current) - now = Utils.now() - mins = 10 * 60 - # Filter ants where updated is older than 10 minutes - ants = [a for a in Ants.ants if (now - a.updated) >= mins] + ages = [(now - ant.updated) for ant in Ants.ants] - if not len(ants): - ants = Ants.ants + # Normalize ages to create weights + total_age = sum(ages) - ants = [a for a in ants if a not in ignore] - return random.choice(ants) + if total_age == 0: + weights = [1] * len(Ants.ants) # If all ages are zero, use equal weights + else: + weights = [ + int(age / total_age * 1000) for age in ages + ] # Scale and cast to int + + # Perform weighted random selection + return random.choices(Ants.ants, weights=weights, k=1)[0] @staticmethod def get_current() -> Ant | None: