Python Forum
Noob - What attributes to use for agents in evo sim (GA type)
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noob - What attributes to use for agents in evo sim (GA type)
#3
(Sep-12-2017, 11:36 PM)Lux Wrote: Not sure how helpful this will be, but my advice would just be to start with a simpler version of this and try to modify it from there, which is generally what I do when working on a Python project.

As for the specific case of an evolution simulator, you might start with a few basic stats- maybe one that impacted the chance of death and one that impacted reproduction. You might repeat this in 'rounds', where every round, your organisms have a chance of dying and a chance of mating. Something like that.
If you can get that to work, you can build off of it to add complexity to your simulator.

Hope that helps!

Thanks. I was beginning to think I was too vague (maybe I was).

I've made some progress from that barebones code above to something...'working' if it can be called that.
I can spawn in a few 'cells' and represent them on screen. I have the code in for plants (food) as well as far as their stats, just not drawn on screen yet.

So after thinking about and reading other tutorials (Genetic algorithms) I've realized that I need some "substance" to my animal's genes, i.e. variables that get 'set' randomly that would dictate some sort of ability to survive (fitness?) and also are accessible for me to 'grasp' in the code and turn into a "gene" list that I can then splice and dice / mutate and pass to child(ren).

In any case, in the near term, I have a basic structure up as you mentioned. Right now my point of STUCK is how do I "kill" an animal and remove it from the "agent[i]" references/lists/objects? and not screw up the index? Running this code should show you this (as soon as something runs out of food).

This is what the error looks like:
Traceback (most recent call last):
  File "C:\Python\GA_sim.py", line 111, in <module>
    update_cells()
  File "C:\Python\GA_sim.py", line 86, in update_cells
    agent.remove(i)
ValueError: list.remove(x): x not in list
Hopefully the formatting isn't screwed.

import math
import numpy as np
from random import random, randint
import time
import pygame as pg

pg.init()
screen = pg.display.set_mode ((500, 500))

clock = pg.time.Clock()

black = (0,0,0)
white= (255,255,255)

class Agent:
    def __init__(self, sense_range, size):
        self.x = randint(0,500)
        self.y = randint(0,500)
        self.ang = 0
        self.dx = 0
        self.dy = 0
        self.speed = 1
        self.food = 100 + randint(-25,50)
        self.target = []
        self.age = 1
        self.sense_range = sense_range  # generic sensor range to allow it to see
        self.size = size                # how large the cell is
        self.color = (randint(0,255), randint(0,255),randint(0,255)) 

class Plant:
    def __init__(self):
        self.x = randint(0,500)
        self.y = randint(0,500)
        self.age = 1
        #varying colors 1-4
        #varying food value based on color
        self.color = 1 + randint(0,3)  
        self.food = self.color * 10 + randint(1,10)
        
def fitness (self, age):
    return (np.tanh (age/4))

def populate_cells (num): # Create initial cell animals
    for x in range(num):
        smell = randint(25,50)
        size = randint(1,5)
        agent.append(Agent(smell, size))

def populate_plants (num): # Create initial plants (food)
    for x in range(num):
        plant.append(Plant())

# ==== Initialize global vars / Create entities ====
agent = []
plant = []
gen = 1
populate_cells(20)
populate_plants(10)

#Some debug stuff to verify cell population
#------------
#for i in range(len(agent[:])):
#   print (agent[i].x, agent[i].y)
#------------

def update_cells():
    for i in range(len(agent[:])):
        cell = agent[i]
        cell.age += 1
        cell.food -= 0.5
        if cell.food < 0:
            cell.food = 0
        cell.size = (cell.food/10) # Change size based on its food level
        if cell.size < 1:
            cell.size= 1
        cell.dx = randint(-2,2)
        cell.dy = randint(-2,2)
    
        cell.x += cell.dx
        cell.y += cell.dy
        cell.x = int(cell.x)
        cell.y = int(cell.y)
        pg.draw.circle (screen, cell.color, (cell.x,cell.y), int(cell.size))

        if cell.food <= 0:
            agent.remove(i)
        
def update_plants():
    for i in range(len(plant[:])):
        shrub = plant[i]
        shrub.age += 1
    
rep_limit = 500
rep = 0
done = False

# ==== Main loop ====
while done == False:

    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()

    screen.fill(white)

    # Debug info showing food for agent[1]  
    font = pg.font.SysFont(None, 20)
    text= font.render ("Food: "+str(agent[1].food), True, black)
    screen.blit (text,(agent[1].x,agent[1].y+10))

    update_cells()
    update_plants()

    rep += 1
    if rep >= rep_limit:
        done = True
        print (done)

    
    pg.display.update()
    clock.tick(60)

pg.quit()
Reply


Messages In This Thread
RE: Noob - What attributes to use for agents in evo sim (GA type) - by PySam - Sep-12-2017, 11:54 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020