/** * Written by Josh Lifton 15MAY2000. * Copyright (c) Josh Lifton 2000. **/ import java.awt.*; public class BoidGene { /* FIELDS */ private double numberOfTrials; private double trialsWon; private double heading, speed, x, y, maximumSpeed, minimumSpeed, turnAngle, acceleration, sensorRange, collisionRange, deathRange, deathConstant, attackConstant, retreatConstant; private Color color; private Flock flock; /* CONSTRUCTORS */ public BoidGene(double direction, double magnitude, double xPos, double yPos, double maxSpeed, double minSpeed, Color c, double angle, double accel, double sensor, double death, double deathC, double collision, double attackC, double retreatC, Flock gaggle) { setMaximumSpeed(maxSpeed); setMinimumSpeed(minSpeed); setHeading(direction); setSpeed(magnitude); setX(xPos); setY(yPos); setColor(c); setTurnAngle(angle); setAcceleration(accel); setSensorRange(sensor); setDeathRange(death); setDeathConstant(deathC); setAttackConstant(attackC); setRetreatConstant(attackC); setCollisionRange(collision); setFlock(gaggle); setNumberOfTrials(0.0); setTrialsWon(0.0); } /* METHODS */ public double getFitness() { return getTrialsWon()/getNumberOfTrials(); } public BoidGene reproduceWith(BoidGene o) { double direction, magnitude, xPos, yPos, maxSpeed, minSpeed, turnRate, accel, sRange, cRange, attackC, retreatC; if (Math.random() > 0.5) { direction = o.getHeading(); } else { direction = getHeading(); } if (Math.random() > 0.5) { magnitude = o.getSpeed(); } else { magnitude = getSpeed(); } if (Math.random() > 0.5) { xPos = o.getX(); } else { xPos = getX(); } if (Math.random() > 0.5) { yPos = o.getY(); } else { yPos = getY(); } if (Math.random() > 0.5) { maxSpeed = o.getMaximumSpeed(); } else { maxSpeed = getMaximumSpeed(); } if (Math.random() > 0.5) { minSpeed = o.getMinimumSpeed(); } else { minSpeed = getMinimumSpeed(); } if (minSpeed > maxSpeed) { maxSpeed = minSpeed; } if (Math.random() > 0.5) { turnRate = o.getTurnAngle(); } else { turnRate = getTurnAngle(); } if (Math.random() > 0.5) { accel = o.getAcceleration(); } else { accel = getAcceleration(); } if (Math.random() > 0.5) { sRange = o.getSensorRange(); } else { sRange = getSensorRange(); } if (Math.random() > 0.5) { cRange = o.getCollisionRange(); } else { cRange = getCollisionRange(); } if (Math.random() > 0.5) { attackC = o.getAttackConstant(); } else { attackC = getAttackConstant(); } if (Math.random() > 0.5) { retreatC = o.getRetreatConstant(); } else { retreatC = getRetreatConstant(); } return new BoidGene( direction, magnitude, xPos, yPos, maxSpeed, minSpeed, Color.red, turnRate, accel, sRange, getDeathRange(), getDeathConstant(), cRange, attackC, retreatC, getFlock()); } /* GET AND SET METHODS */ public double getNumberOfTrials() {return numberOfTrials;} public void setNumberOfTrials(double n) {numberOfTrials = n;} public double getTrialsWon() {return trialsWon;} public void setTrialsWon(double t) {trialsWon = t;} public double getHeading() {return heading;} public void setHeading(double direction) {heading = direction;} public double getSpeed() {return speed;} public void setSpeed(double magnitude) {speed = magnitude;} public double getX() {return x;} public void setX(double xPos) {x=xPos;} public double getY() {return y;} public void setY(double yPos) {y=yPos;} public double getMaximumSpeed() {return maximumSpeed;} public void setMaximumSpeed(double s) {maximumSpeed = s;} public double getMinimumSpeed() {return minimumSpeed;} public void setMinimumSpeed(double s) {minimumSpeed = s;} public Color getColor() {return color;} public void setColor(Color c) {color = c;} public double getTurnAngle() {return turnAngle;} public void setTurnAngle(double a) {turnAngle = a;} public double getAcceleration() {return acceleration;} public void setAcceleration(double a) {acceleration = a;} public double getCollisionRange() {return collisionRange;} public void setCollisionRange(double collision) {collisionRange = collision;} public double getSensorRange() {return sensorRange;} public void setSensorRange(double sensor) {sensorRange = sensor;} public double getDeathRange() {return deathRange;} public void setDeathRange(double death) {deathRange = death;} public double getDeathConstant() {return deathConstant;} public void setDeathConstant(double c) {deathConstant = c;} public double getAttackConstant() {return attackConstant;} public void setAttackConstant(double c) {attackConstant = c;} public double getRetreatConstant() {return retreatConstant;} public void setRetreatConstant(double c) {retreatConstant = c;} public Flock getFlock() {return flock;} public void setFlock(Flock gaggle) {flock = gaggle;} }