May 25, 2013, 09:51:53 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Prowlie Welcome back! The site migration is complete and normal service resumed.
Advanced search
Pages: [1]   Go Down
Print
Author Topic: Evolution Simulator  (Read 1758 times)
0 Members and 1 Guest are viewing this topic.
adzy-2k6
Senior Member
****
Offline Offline

Posts: 725


« on: February 27, 2010, 05:58:41 AM »

Recently in a scientific programming course at college we were assigned the task of creating a predator-prey evolution simulator. As you may notice, I have gotten into better habits since the gas simulation I posted a few months back. The language is V-python.

And btw, the code isn't finished yet. The predators are yet to come, and the prey AI is fairly simplistic. i am not likely going to have the prey evolve as the predators seem complex enough as it is.

Code:
from visual import*
import random
#Predator-Prey
#Adam O'Hara

#Creating the world
worldLength = 1000
worldWidth = 1000

ground=box(pos=(0,-5,0), size=(worldWidth*2,1,worldLength*2), color=color.red)
## Prey
#Prey settings
preyRadius = 5
preySpeed = 2
chanceOfChange = .01

preyList = []

#Function to create prey
def CreatePrey():
    prey = sphere(pos=(0,0,0), radius=preyRadius, color=color.green)
    prey.speed = preySpeed
    prey.swerveChance = chanceOfChange
    prey.velX = 0
    prey.velZ = 2
    preyList.append(prey)

def PreyAI(prey):
    prey.swerveProbability = random.random()
    if prey.swerveProbability < chanceOfChange:
        Swerve(prey)

##Predator
#Predator Settings
predatorResponseTime = 50
predatorDetectionRange = 100
predatorSpeed = 4
predatorEnergy= 2000



#General function for all movement
def Move(char):
    char.pos.x += char.velX
    char.pos.z += char.velZ
    if char.pos.x > worldWidth:
        char.pos.x = -worldWidth + (char.pos.x - worldWidth)
    if char.pos.x < -worldWidth:
        char.pos.x = worldWidth - (char.pos.x - worldWidth)
    if char.pos.z > worldLength:
        char.pos.z = -worldLength + (char.pos.z - worldLength)
    if char.pos.z < -worldLength:
        char.pos.z = worldLength - (char.pos.z - worldLength)

#Function for direction changes
def Swerve(char):
    char.angle = 6.284*random.random()
    char.velZ = char.speed*sin(char.angle)
    char.velX = char.speed*cos(char.angle)

#Create a number of prey
n = 20
for i in range(n):
    CreatePrey()

while(1==1):
    rate(100)
    for i in range (len(preyList)):
        Move(preyList[i])
        PreyAI(preyList[i])
« Last Edit: February 27, 2010, 10:19:39 PM by adzy-2k6 » Logged

chluaid
Bitey's Daddy
Administrator
Heroic
*****
Offline Offline

Gender: Male
Posts: 5379


ldf l srff r drlt sdtr


WWW
« Reply #1 on: February 27, 2010, 10:25:16 AM »

I'm fascinated with predator-prey simulations. As a non-coder, I can't wait to see this one in action. Will your predators change with each consume? I'd love to see a simulation where predators improve with each kill, eventually preying on the lesser predators. I guess they'd need a lifespan to avoid ending up with one all consuming god. I could watch that stuff for hours. I wish I could code Sad
Logged

<a href="http://bitey.com/images/dashkin/728_animated1.swf" target="_blank">http://bitey.com/images/dashkin/728_animated1.swf</a>
adzy-2k6
Senior Member
****
Offline Offline

Posts: 725


« Reply #2 on: February 27, 2010, 10:19:04 PM »

Im aiming for something a little more simple. The current plan is to only have the predators evolve to kill one type of prey. Currently, the two valriables that are going to change are the speed and detection radius. However, to stop the simulation simply setting them both to max im making it so that the faster it moves the more energy it drains, and the larger the detection radius the longer it takes to respond to anything the prey does. Predators currently have a counter that will simply count downwards and if they don't eat it will hit 0 and they die.

You never know though, once i have this basic simulation it will be very easy to expand it.
Logged

supermoose
Full Member
***
Offline Offline

Gender: Male
Posts: 240


Yes. I beat IWBTG. Only took ~60 hours of play.


« Reply #3 on: March 03, 2010, 05:06:17 AM »

this is cool.

just a suggestion, you may want to have your while loop run off of a variable which you can change to break it.  That way, you could put in what is essentially a "pause" button which can toggle that variable via keystroke.  Your simulations would freeze, and you could use that time to collect a bit better data for whatever this is supposed to simulate.
Logged

Brackenwood
   

 Logged
Pages: [1]   Go Up
Print
Jump to:  

Theme by Pieter, based on Black Rain by Crip Powered by SMF 1.1.16 | SMF © 2011, Simple Machines XHTML | CSS

Page created in 0.058 seconds with 26 queries.