├── src ├── dots │ ├── Goal.java │ ├── Obstacle.java │ ├── Brain.java │ ├── LearningDots.java │ ├── Dot.java │ └── Population.java ├── main │ ├── App.java │ └── Main.java └── car │ ├── LearningCar.java │ ├── Track.java │ ├── Wall.java │ └── Car.java └── README.md /src/dots/Goal.java: -------------------------------------------------------------------------------- 1 | package dots; 2 | 3 | public class Goal { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaGeneticAlgorithm 2 | A simple genetic algorithm visualization (heavily inspired by CodeBullet) 3 | 4 | Uses glib4j (my other repository) 5 | -------------------------------------------------------------------------------- /src/main/App.java: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import net.x666c.glib.GFrame; 4 | import net.x666c.glib.graphics.Renderer; 5 | 6 | public abstract class App { 7 | 8 | protected int updates = 60; 9 | 10 | public abstract void setup(GFrame frame); 11 | public abstract void update(); 12 | public abstract void draw(Renderer r); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/Main.java: -------------------------------------------------------------------------------- 1 | package main; 2 | import car.LearningCar; 3 | import dots.LearningDots; 4 | import net.x666c.glib.GFrame; 5 | 6 | public class Main { 7 | 8 | static App current = new LearningCar(); 9 | 10 | public static void main(String[] args) { 11 | GFrame f = new GFrame(78, current.updates, current::draw, current::update); 12 | f.setSize(800, 800); 13 | current.setup(f); 14 | f.start(); 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /src/car/LearningCar.java: -------------------------------------------------------------------------------- 1 | package car; 2 | 3 | import static java.lang.Math.*; 4 | import java.awt.Color; 5 | import main.App; 6 | import net.x666c.glib.GFrame; 7 | import net.x666c.glib.graphics.Renderer; 8 | 9 | public class LearningCar extends App { 10 | 11 | Track track; 12 | 13 | // ----------------------------------------------------------------------------- 14 | 15 | public void setup(GFrame frame) { 16 | track = new Track(); 17 | 18 | frame.background(Color.GRAY); 19 | } 20 | 21 | // ----------------------------------------------------------------------------- 22 | 23 | public void update() { 24 | track.update(); 25 | } 26 | 27 | // ----------------------------------------------------------------------------- 28 | 29 | public void draw(Renderer r) { 30 | track.draw(r); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/dots/Obstacle.java: -------------------------------------------------------------------------------- 1 | package dots; 2 | 3 | import java.awt.Color; 4 | 5 | import net.x666c.glib.graphics.Renderer; 6 | import net.x666c.glib.math.phys.Collider; 7 | 8 | public class Obstacle { 9 | 10 | Collider collider; 11 | 12 | Color color1 = Color.BLACK, color2 = Color.YELLOW; 13 | 14 | // ------------------------------------------------------------ 15 | 16 | Obstacle(float x, float y, float width, float height) { 17 | collider = new Collider(x, y, width, height); 18 | } 19 | 20 | // ------------------------------------------------------------ 21 | 22 | void draw(Renderer r) { 23 | r.fill(); 24 | float step = (float) collider.width / 16f; 25 | boolean switchh = true; 26 | for (int i = 0; i < 16; i++) { 27 | if(switchh) { 28 | r.color(color1); 29 | switchh = false; 30 | } else { 31 | r.color(color2); 32 | switchh = true; 33 | } 34 | r.rectangle(collider.x() + step * i, collider.y(), collider.width() / 16f, collider.height()); 35 | } 36 | r.color(0); 37 | r.draw(); 38 | r.rectangle(collider.x(), collider.y(), collider.width(), collider.height()); 39 | } 40 | 41 | // ------------------------------------------------------------ 42 | 43 | } -------------------------------------------------------------------------------- /src/car/Track.java: -------------------------------------------------------------------------------- 1 | package car; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | import net.x666c.glib.graphics.Renderer; 8 | import net.x666c.glib.input.Input; 9 | 10 | public class Track { 11 | 12 | Car car; 13 | 14 | static List walls = new ArrayList(); 15 | 16 | // ----------------------------------------------------------------------------- 17 | 18 | Track() { 19 | car = new Car(400, 400); 20 | 21 | Collections.addAll(walls, new Wall(0, 0, 0, 800), new Wall(0, 0, 800, 800)); 22 | } 23 | 24 | // ----------------------------------------------------------------------------- 25 | 26 | void update() { 27 | for (Wall wall : walls) { 28 | wall.update(); 29 | } 30 | 31 | car.update(); 32 | 33 | if(Input.keyboard.key('w')) { 34 | car.move(); 35 | } 36 | if (Input.keyboard.key('a')) { 37 | car.left(); 38 | } 39 | if (Input.keyboard.key('s')) { 40 | car.brake(); 41 | } 42 | if (Input.keyboard.key('d')) { 43 | car.right(); 44 | } 45 | } 46 | 47 | // ----------------------------------------------------------------------------- 48 | 49 | void draw(Renderer r) { 50 | for (Wall wall : walls) { 51 | wall.draw(r); 52 | } 53 | car.draw(r); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/dots/Brain.java: -------------------------------------------------------------------------------- 1 | package dots; 2 | import java.util.Random; 3 | 4 | import net.x666c.glib.math.MathUtil; 5 | import net.x666c.glib.math.vector.Vector; 6 | public class Brain { 7 | 8 | Vector[] moves; 9 | int step; 10 | 11 | boolean dead = false; 12 | boolean reached = false; 13 | 14 | // ------------------------------------------------------------ 15 | 16 | Brain(int len) { 17 | moves = new Vector[len]; 18 | for (int i = 0; i < this.moves.length; i++) { 19 | float a = MathUtil.random((float) (Math.PI * 2)); 20 | moves[i] = Vector.fromAngle(a); 21 | } 22 | } 23 | 24 | // ------------------------------------------------------------ 25 | 26 | Vector nextMove() { 27 | Vector r = moves[step++]; 28 | if(step >= moves.length) 29 | dead = true; 30 | return r; 31 | } 32 | 33 | // ------------------------------------------------------------ 34 | 35 | Brain copy() { 36 | Brain brain = new Brain(moves.length); 37 | for (int i = 0; i < brain.moves.length; i++) { 38 | brain.moves[i] = this.moves[i].clone(); 39 | } 40 | return brain; 41 | } 42 | 43 | // ------------------------------------------------------------ 44 | 45 | void mutate(float probability) { 46 | for (int i = 0; i < moves.length; i++) { 47 | if(MathUtil.random() <= probability) { 48 | moves[i] = Vector.random(); 49 | } 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/car/Wall.java: -------------------------------------------------------------------------------- 1 | package car; 2 | 3 | import java.awt.geom.Line2D; 4 | import net.x666c.glib.graphics.Renderer; 5 | import net.x666c.glib.math.phys.Collider; 6 | import net.x666c.glib.math.vector.Line; 7 | import net.x666c.glib.math.vector.Point; 8 | import net.x666c.glib.math.vector.Vector; 9 | 10 | public class Wall { 11 | 12 | Line line; 13 | 14 | // ----------------------------------------------------------------------------- 15 | 16 | Wall(float x1, float y1, float x2, float y2) { 17 | line = new Line(x1, y1, x2, y2); 18 | } 19 | 20 | // ----------------------------------------------------------------------------- 21 | 22 | void draw(Renderer r) { 23 | r.fill(); 24 | r.color(~0); 25 | r.line(line); 26 | } 27 | 28 | // ----------------------------------------------------------------------------- 29 | 30 | void update() { 31 | 32 | } 33 | 34 | 35 | Point raycast(Line l) { 36 | final float x1 = line.x1; 37 | final float y1 = line.y1; 38 | 39 | final float x2 = line.x2; 40 | final float y2 = line.y2; 41 | 42 | final float x3 = l.x1; 43 | final float y3 = l.y1; 44 | 45 | final float x4 = l.x2; 46 | final float y4 = l.y2; 47 | 48 | final float den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); 49 | 50 | if(den == 0) 51 | return null; 52 | 53 | final float t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den; 54 | final float u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den; 55 | 56 | if(t > 0 && t < 1 && u > 0) { 57 | final float p1 = x1 + t * (x2 - x1); 58 | final float p2 = y1 + t * (y2 - y1); 59 | 60 | return new Point(p1, p2); 61 | } else { 62 | return null; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/dots/LearningDots.java: -------------------------------------------------------------------------------- 1 | package dots; 2 | 3 | import java.awt.Color; 4 | 5 | import javax.sql.PooledConnection; 6 | 7 | import main.App; 8 | import net.x666c.glib.GFrame; 9 | import net.x666c.glib.graphics.Renderer; 10 | 11 | public class LearningDots extends App { 12 | 13 | { 14 | updates = 100; 15 | seed = "abracadabra"; 16 | onlyBest = true; 17 | } 18 | 19 | static Obstacle[] obstacles = { 20 | new Obstacle(350, 600, 10, 200), 21 | new Obstacle(450, 600, 10, 200), 22 | 23 | new Obstacle(360, 600, 30, 10), 24 | new Obstacle(420, 600, 30, 10), 25 | 26 | new Obstacle(100, 500, 600, 10), 27 | 28 | new Obstacle(0, 300, 350, 10), 29 | new Obstacle(450, 300, 350, 10), 30 | }; 31 | 32 | // ------------------------------------------------------------ 33 | 34 | Population population; 35 | String seed = "cooldude"; 36 | boolean onlyBest; 37 | 38 | public void setup(GFrame frame) { 39 | frame.onKeyPress((char)27, () -> System.exit(0)); 40 | frame.onKeyPress('q', () -> frame.setUps(5000)); 41 | frame.onKeyPress('w', () -> frame.setUps(60)); 42 | frame.onKeyPress('b', () -> population.onlyBest(!population.showOnlyBest)); 43 | population = new Population(2000, seed, onlyBest); 44 | 45 | frame.background(Color.DARK_GRAY); 46 | } 47 | 48 | // ------------------------------------------------------------ 49 | 50 | public void update() { 51 | if (population.everybodyDied()) { 52 | // Do magic stuff 53 | System.out.println("Everyone died"); 54 | long t1 = System.nanoTime(); 55 | population.calculateFitness(); 56 | long t2 = System.nanoTime(); 57 | population.naturalSelection(); 58 | long t3 = System.nanoTime(); 59 | population.mutateBabies(); 60 | long t4 = System.nanoTime(); 61 | 62 | System.out.println("Evolving..."); 63 | System.out.printf("Fitness calc took: %.6f\n", (t2 - t1) / 1e6); 64 | System.out.printf("Natural selection took: %.6f\n", (t3 - t2) / 1e6); 65 | System.out.printf("Mutation took: %.6f\n", (t4 - t3) / 1e6); 66 | 67 | } else { 68 | population.update(); 69 | } 70 | 71 | } 72 | 73 | // ------------------------------------------------------------ 74 | 75 | public void draw(Renderer r) { 76 | population.draw(r); 77 | 78 | r.fill(); 79 | r.color(Color.BLUE); 80 | 81 | for (Obstacle obstacle : obstacles) { 82 | obstacle.draw(r); 83 | } 84 | } 85 | 86 | // ------------------------------------------------------------ 87 | 88 | static boolean collidesWithObstacle(Dot dot) { 89 | for (int i = 0; i < obstacles.length; i++) { 90 | if(obstacles[i].collider.intersects(dot.pos.x, dot.pos.y, dot.dotSize * 2, dot.dotSize * 2)) { 91 | return true; 92 | } 93 | } 94 | return false; 95 | } 96 | } -------------------------------------------------------------------------------- /src/dots/Dot.java: -------------------------------------------------------------------------------- 1 | package dots; 2 | 3 | import static net.x666c.glib.math.MathUtil.map; 4 | import static net.x666c.glib.math.MathUtil.random; 5 | import java.awt.Color; 6 | import java.awt.geom.Ellipse2D; 7 | import java.awt.geom.Point2D; 8 | 9 | import net.x666c.glib.graphics.Renderer; 10 | import net.x666c.glib.math.phys.Collider; 11 | import net.x666c.glib.math.vector.Vector; 12 | 13 | public class Dot { 14 | 15 | int dotSize = 3; 16 | 17 | Vector pos; 18 | Vector vel, acc; 19 | 20 | Brain brain; 21 | 22 | Color innerColor, outColor; 23 | 24 | double fitness; 25 | 26 | boolean bestDot = false; 27 | 28 | // ------------------------------------------------------------ 29 | 30 | Dot() { 31 | //pos = Vector.create(map(random(), 0, 1, 50, 800 - 50), map(random(), 0, 1, 650, 800 - 50)); 32 | pos = Vector.create(800 / 2, 780); 33 | vel = Vector.zero(); 34 | acc = Vector.zero(); 35 | 36 | brain = new Brain(1000); 37 | 38 | innerColor = Color.BLACK; 39 | outColor = Color.BLACK; 40 | } 41 | 42 | // ------------------------------------------------------------ 43 | 44 | void draw(Renderer r) { 45 | if (!bestDot) { 46 | r.fill(); 47 | r.color(innerColor); 48 | r.circle(pos.x, pos.y, dotSize); 49 | r.draw(); 50 | r.color(0); 51 | r.circle(pos.x, pos.y, dotSize); 52 | } else { 53 | r.fill(); 54 | r.color(Color.ORANGE); 55 | r.circle(pos.x, pos.y, dotSize+2); 56 | r.draw(); 57 | r.color(0); 58 | r.stroke(2); 59 | r.circle(pos.x, pos.y, dotSize+2); 60 | r.stroke(1); 61 | } 62 | } 63 | 64 | // ------------------------------------------------------------ 65 | 66 | void update() { 67 | if(!inBounds()) 68 | brain.dead = true; 69 | 70 | if(!brain.dead && !brain.reached) { 71 | acc = brain.nextMove(); 72 | vel.add(acc); 73 | vel.limit(5); 74 | pos.add(vel); 75 | } 76 | 77 | if(Population.goal.intersects(pos.x, pos.y, dotSize*2, dotSize*2)) { 78 | brain.reached = true; 79 | } 80 | 81 | colorize(); 82 | } 83 | 84 | // ------------------------------------------------------------ 85 | 86 | void calculateFitness() { 87 | if(brain.reached) { 88 | fitness = 1d/16d + 10000d/(double)(brain.step * brain.step); 89 | } else { 90 | double distance = Point2D.distance(0, pos.y, 0, Population.goal.y); 91 | fitness = 1d / (distance * distance); 92 | } 93 | } 94 | 95 | // ------------------------------------------------------------ 96 | 97 | Dot makeBaby() { 98 | Dot baby = new Dot(); 99 | baby.brain = this.brain.copy(); 100 | return baby; 101 | } 102 | 103 | // ------------------------------------------------------------ 104 | 105 | void colorize() { 106 | int r = (int)map(brain.moves.length - brain.step, brain.moves.length, 0, 0, 255); 107 | int g = (int)map(brain.moves.length - brain.step, brain.moves.length, 0, 255, 0); 108 | innerColor = new Color(r, g, 0); 109 | 110 | if(brain.dead) { 111 | outColor = Color.RED; 112 | } else if(brain.reached) { 113 | outColor = Color.BLACK; 114 | innerColor = new Color(Color.HSBtoRGB(0.16f, 1, (1f / 255f) * innerColor.getGreen())); 115 | } 116 | } 117 | 118 | // ------------------------------------------------------------ 119 | 120 | boolean inBounds() { 121 | boolean bounds1 = pos.x < 800-dotSize*2 && pos.x > 0+dotSize && pos.y < 800-dotSize*2 && pos.y > 0+dotSize; 122 | boolean collidesWithObstacle = LearningDots.collidesWithObstacle(this); 123 | 124 | return bounds1 && !collidesWithObstacle; 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/car/Car.java: -------------------------------------------------------------------------------- 1 | package car; 2 | 3 | import java.awt.Color; 4 | import java.awt.geom.AffineTransform; 5 | import java.awt.geom.Line2D; 6 | import java.awt.geom.Path2D; 7 | import java.awt.geom.Rectangle2D; 8 | import net.x666c.glib.graphics.Renderer; 9 | import net.x666c.glib.math.phys.Collider; 10 | import net.x666c.glib.math.vector.Line; 11 | import net.x666c.glib.math.vector.Point; 12 | import net.x666c.glib.math.vector.Vector; 13 | import static java.lang.Math.*; 14 | 15 | public class Car { 16 | 17 | Path2D.Float collider; 18 | Collider pos; 19 | Vector vel, acc; 20 | 21 | float rotation = 0; 22 | 23 | float speed = 3; 24 | float steering = 5; 25 | float brakeFactor = 0.956f; 26 | 27 | int width = 10; 28 | int height = 20; 29 | 30 | // ----------------------------------------------------------------------------- 31 | 32 | Car(float x, float y) { 33 | //bounds = new Line[] {new Line(400, 400, 400 + width, 400),new Line(410, 400, 400 + width, 400 + height),new Line(400 + width, 400 + height, 400, 400 + height),new Line(400, 400 + height, 400, 400)}; 34 | 35 | pos = new Collider(x, y, width, height); 36 | collider = new Path2D.Float(pos); 37 | 38 | vel = Vector.create(1e-30f, 1e-30f); 39 | acc = Vector.zero(); 40 | 41 | } 42 | 43 | // ----------------------------------------------------------------------------- 44 | 45 | void draw(Renderer r) { 46 | float midX = pos.x()+width/2; 47 | float midY = pos.y()+height/2; 48 | 49 | /*r.color(Color.GREEN); 50 | r.line(midX, midY, midX + (vel.x * 50), midY + (vel.y * 50)); 51 | r.color(Color.MAGENTA); 52 | r.line(midX, midY, midX + (acc.x * 50), midY + (acc.y * 50));*/ 53 | 54 | if(collusion(r)) 55 | r.color(Color.RED); 56 | else 57 | r.color(Color.GREEN); 58 | //r.g2d().rotate(Math.toRadians(rotation), midX, midY); 59 | 60 | r.rectangle(pos.x(), pos.y(), pos.width(), pos.height()); 61 | } 62 | 63 | // ----------------------------------------------------------------------------- 64 | 65 | void update() { 66 | //if(collusion()) {} 67 | 68 | pos.x += vel.x; 69 | pos.y += vel.y; 70 | 71 | acc = Vector.zero(); 72 | 73 | brake(); 74 | 75 | steering = (float) vel.magnitude() * 2f; 76 | } 77 | 78 | // ----------------------------------------------------------------------------- 79 | 80 | boolean collusion(Renderer r) { 81 | final Line l1 = new Line(pos.x(), pos.y(), pos.x() + pos.width(), pos.y()); 82 | final Line l2 = new Line(pos.x() + pos.width(), pos.y(), pos.x() + pos.width(), pos.y() + pos.height()); 83 | final Line l3 = new Line(pos.x() + pos.width(), pos.y() + pos.height(), pos.x(), pos.y() + pos.height()); 84 | final Line l4 = new Line(pos.x(), pos.y() + pos.height(), pos.x(), pos.y()); 85 | for (Wall wall : Track.walls) { 86 | final Point p1 = wall.raycast(l1); 87 | final Point p2 = wall.raycast(l2); 88 | final Point p3 = wall.raycast(l3); 89 | final Point p4 = wall.raycast(l4); 90 | try { 91 | r.color(Color.RED); 92 | r.point(p1); 93 | r.color(Color.GREEN); 94 | r.point(p2); 95 | r.color(Color.BLUE); 96 | r.point(p3); 97 | r.color(Color.MAGENTA); 98 | r.point(p4); 99 | } catch (Exception e) { 100 | } 101 | if(false) 102 | return true; 103 | } 104 | return false; 105 | } 106 | 107 | // ----------------------------------------------------------------------------- 108 | 109 | boolean moves = false; 110 | 111 | void move() { 112 | if(vel.magnitude() < speed) { 113 | Vector a = Vector.create(0, -0.1f); 114 | rotateVector(a, rotation); 115 | vel.add(a); 116 | } 117 | } 118 | 119 | // ----------------------------------------------------------------------------- 120 | 121 | void brake() { 122 | if(vel.magnitude() > 0.0001) 123 | vel.scale(brakeFactor); 124 | } 125 | 126 | // ----------------------------------------------------------------------------- 127 | 128 | void right() { 129 | rotateVector(vel, steering); 130 | rotation += steering;;//(rotation >= 360 ? -360 : steering); 131 | } 132 | 133 | // ----------------------------------------------------------------------------- 134 | 135 | void left() { 136 | rotateVector(vel, -steering); 137 | rotation -= steering; 138 | } 139 | 140 | // ----------------------------------------------------------------------------- 141 | 142 | void rotateVector(Vector v, float ang) { 143 | ang = (float) toRadians(ang); 144 | double x = (cos(ang) * v.x) - (sin(ang) * v.y); 145 | double y = (sin(ang) * v.x) + (cos(ang) * v.y); 146 | v.set((float)x, (float)y); 147 | } 148 | 149 | } -------------------------------------------------------------------------------- /src/dots/Population.java: -------------------------------------------------------------------------------- 1 | package dots; 2 | 3 | import java.awt.Color; 4 | import java.awt.geom.Ellipse2D; 5 | import java.util.Arrays; 6 | import java.util.Collections; 7 | import java.util.List; 8 | import java.util.concurrent.ThreadLocalRandom; 9 | 10 | import net.x666c.glib.graphics.Renderer; 11 | import net.x666c.glib.math.MathUtil; 12 | 13 | public class Population { 14 | 15 | static Ellipse2D.Float goal = new Ellipse2D.Float(800 / 2f, 10, 8, 8); 16 | 17 | String seed; 18 | int seedHash; 19 | 20 | int size; 21 | Dot[] members; 22 | 23 | double totalFitness; 24 | int minSteps = 1000; 25 | 26 | double averageFitness = Float.NaN, bestFitness = Float.NaN, worstFitenss = Float.NaN; 27 | int generation = 0; 28 | float mutationRate = 0.01f; 29 | 30 | boolean showOnlyBest; 31 | 32 | // ------------------------------------------------------------ 33 | 34 | Population(int size, String seed, boolean onlyBest) { 35 | this.showOnlyBest = onlyBest; 36 | this.seed = seed; 37 | this.seedHash = seed.hashCode(); 38 | MathUtil.randomSeed(seedHash); 39 | 40 | members = new Dot[size]; 41 | for (int i = 0; i < members.length; i++) { 42 | members[i] = new Dot(); 43 | } 44 | } 45 | 46 | // ------------------------------------------------------------ 47 | 48 | boolean everybodyDied() { 49 | for (int i = 0; i < members.length; i++) { 50 | if (!members[i].brain.dead && !members[i].brain.reached) 51 | return false; 52 | } 53 | return true; 54 | } 55 | 56 | // ------------------------------------------------------------ 57 | 58 | void calculateFitness() { 59 | for (Dot dot : members) { 60 | dot.calculateFitness(); 61 | } 62 | } 63 | 64 | // ------------------------------------------------------------ 65 | 66 | void calculateFitenssSum() { 67 | totalFitness = 0; 68 | 69 | bestFitness = 0; 70 | worstFitenss = 0; 71 | for (Dot dot : members) { 72 | totalFitness += dot.fitness; 73 | if(dot.fitness < worstFitenss) 74 | worstFitenss = dot.fitness; 75 | if(dot.fitness > bestFitness) 76 | bestFitness = dot.fitness; 77 | } 78 | averageFitness = totalFitness / members.length; 79 | } 80 | 81 | // ------------------------------------------------------------ 82 | 83 | void naturalSelection() { 84 | generation++; 85 | 86 | Dot[] newDots = new Dot[members.length]; 87 | calculateFitenssSum(); 88 | 89 | Dot bestDot = members[0]; 90 | for (int i = 0; i < newDots.length; i++) { 91 | if(members[i].fitness > bestDot.fitness) 92 | bestDot = members[i]; 93 | } 94 | 95 | if(bestDot.brain.reached) 96 | minSteps = bestDot.brain.step; 97 | 98 | newDots[0] = bestDot.makeBaby(); 99 | newDots[0].bestDot = true; 100 | 101 | for (int i = 1; i < newDots.length; i++) { 102 | Dot dad = selectDad(); 103 | newDots[i] = dad.makeBaby(); 104 | } 105 | members = newDots; 106 | } 107 | 108 | // ------------------------------------------------------------ 109 | 110 | void mutateBabies() { 111 | for (int i = 1; i < members.length; i++) { 112 | members[i].brain.mutate(mutationRate); 113 | } 114 | } 115 | 116 | // ------------------------------------------------------------ 117 | 118 | Dot selectDad() { 119 | float fitnessAdd = 0; 120 | float random = (float) ThreadLocalRandom.current().nextDouble(totalFitness); 121 | 122 | for (int i = 0; i < members.length; i++) { 123 | fitnessAdd += members[i].fitness; 124 | if(fitnessAdd > random) { 125 | return members[i]; 126 | } 127 | } 128 | 129 | 130 | // never 131 | System.out.println("oopsie"); 132 | List list = Arrays.asList(members); 133 | Collections.sort(list, (c1, c2) -> (int)Math.signum(c1.fitness - c2.fitness)); 134 | 135 | return list.get(MathUtil.random(list.size() / 256)); 136 | } 137 | 138 | // ------------------------------------------------------------ 139 | 140 | void draw(Renderer r) { 141 | r.fill(); 142 | r.color(Color.MAGENTA); 143 | r.oval(goal.x, goal.y, goal.width, goal.height); 144 | r.draw(); 145 | r.color(0); 146 | r.oval(goal.x, goal.y, goal.width, goal.height); 147 | 148 | if(!showOnlyBest) 149 | for (int i = 1; i < members.length; i++) { 150 | members[i].draw(r); 151 | } 152 | members[0].draw(r); 153 | 154 | r.text(String.format("Average fitness: %.16f", averageFitness), 10, 16); 155 | r.text(String.format("Best fitness: %.16f", bestFitness), 10, 16 * 2); 156 | r.text(String.format("Worst fitness: %.16f", worstFitenss), 10, 16 * 3); 157 | r.text("Generation: " + Integer.toString(generation), 10, 16 * 4); 158 | r.text("Seed: " + seed + " / " + seedHash, 10, 16 * 5); 159 | r.text("Mutation rate: " + Float.toString(mutationRate), 10, 16 * 6 + 2); 160 | } 161 | 162 | // ------------------------------------------------------------ 163 | 164 | void update() { 165 | for (int i = 0; i < members.length; i++) { 166 | members[i].update(); 167 | if(members[i].brain.step > minSteps) 168 | members[i].brain.dead = true; 169 | } 170 | } 171 | 172 | // ------------------------------------------------------------ 173 | 174 | void onlyBest(boolean is) { 175 | showOnlyBest = is; 176 | } 177 | 178 | } 179 | --------------------------------------------------------------------------------