From 8cc8dfc0b13a3ba311e8fde93aed65f606ff5b4a Mon Sep 17 00:00:00 2001 From: Nabil Ould Hamou Date: Mon, 14 Oct 2024 18:33:21 +0200 Subject: [PATCH] =?UTF-8?q?J'ai=20rajout=C3=A9=20des=20trucs=20et=20j'ai?= =?UTF-8?q?=20la=20flemme=20de=20faire=20un=20commit=20correct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 18 +++++++ src/main/java/logique/Maze.java | 29 +++++++++++- src/main/java/logique/Population.java | 67 +++++++++++++++++++++++++-- 3 files changed, 108 insertions(+), 6 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 8cd5935..3ed9c66 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,4 +1,7 @@ import logique.Maze; +import logique.Population; + +import java.util.List; import static com.raylib.Raylib.*; import static com.raylib.Jaylib.BLACK; @@ -11,6 +14,14 @@ public class Main { SetTargetFPS(100); + Maze m = new Maze(500, 10, 0.01f, 0.7f, 0.5f); + m.init(); + m.evaluate(); + Population p = m.getBest(); + p.setCoordinates(0, 4); + List moves = p.getMoves(); + + long lastTime = System.currentTimeMillis(); while (!WindowShouldClose()) { BeginDrawing(); ClearBackground(RAYWHITE); @@ -23,6 +34,13 @@ public class Main { } } + p.render(); + if (System.currentTimeMillis() - lastTime > 250 && !moves.isEmpty()) { + p.move(moves.getFirst()); + moves.removeFirst(); + lastTime = System.currentTimeMillis(); + } + DrawFPS(20, 20); EndDrawing(); } diff --git a/src/main/java/logique/Maze.java b/src/main/java/logique/Maze.java index 6f53f54..e218424 100644 --- a/src/main/java/logique/Maze.java +++ b/src/main/java/logique/Maze.java @@ -1,11 +1,17 @@ package logique; +import com.sun.tools.jconsole.JConsoleContext; +import utils.Pair; + import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; public class Maze { - public static int[][] MAZE = { + public static int[][] MAZE = new int[][]{ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 0, 1, 0, 0, 0}, {1, 0, 1, 0, 1, 0, 1, 0, 1, 1}, @@ -17,6 +23,7 @@ public class Maze { {1, 0, 1, 1, 0, 1, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; + public static Pair goal = new Pair<>(9, 1); private int populationSize; private int iterations; @@ -25,6 +32,8 @@ public class Maze { private float crossover; private float elitism; + private final Random random = new Random(); + private List populationList; public Maze(int populationSize, int iterations, float mutation, float crossover, float elitism) { @@ -38,11 +47,27 @@ public class Maze { } public void init() { - + for (int i = 0; i < populationSize; i++) { + Population p = new Population(); + for (int j = 0; j < 40; j++) { + p.move(random.nextInt(4)); + } + populationList.add(p); + } } public void evaluate() { + populationList = populationList.stream() + .sorted(Comparator.comparing(p -> p.score())) + .toList(); + } + public void createNextGeneration() { + + } + + public Population getBest() { + return populationList.getFirst(); } public int getPopulationSize() { diff --git a/src/main/java/logique/Population.java b/src/main/java/logique/Population.java index b3c8619..f7ae3a1 100644 --- a/src/main/java/logique/Population.java +++ b/src/main/java/logique/Population.java @@ -1,14 +1,20 @@ package logique; +import com.raylib.Jaylib; +import com.raylib.Raylib; import utils.Pair; +import java.util.ArrayList; +import java.util.Arrays; import java.util.BitSet; +import java.util.List; public class Population { + private int chromosomeLength = 0; private BitSet chromosome; - private int posX; - private int posY; + private int posX = 0; + private int posY = 4; public Population() { this.chromosome = new BitSet(); @@ -19,29 +25,82 @@ public class Population { } public void move(int type) { + int movement = 0; switch (type) { case 0: // UP - + movement = -1; + if (isCoordinateValid(posX, posY + movement)) { + posY += movement; + } break; case 1: // DOWN + movement = 1; + if (isCoordinateValid(posX, posY + movement)) { + posY += movement; + } break; case 2: // L + movement = -1; + if (isCoordinateValid(posX + movement, posY)) { + posX += movement; + } break; case 3: // R + movement = 1; + if (isCoordinateValid(posX + movement, posY)) { + posX += movement; + } break; } + + int[] bin = new int[2]; + for (int i = 0; type > 0; i++) { + bin[i] = type % 2; + type /= 2; + } + + for (int j : bin) { + chromosome.set(chromosomeLength++, j == 1); + } + } + + public float score() { + return (float) Math.pow((double) Maze.goal.getFirst() - posX, 2.d) + + (float) Math.pow((double) Maze.goal.getSecond() - posY, 2.d); + } + + public List getMoves() { + List moves = new ArrayList<>(); + for (int i = 0; i < chromosomeLength; i += 2) { + String s = ""; + for (int j = i; j < i+2; j++) { + s += chromosome.get(j) ? "1" : "0"; + } + moves.add(Integer.parseInt(s, 2)); + } + + return moves; } private boolean isCoordinateValid(int x, int y) { if (x > Maze.MAZE[0].length || x < 0) return false; if (y > Maze.MAZE.length || y < 0) return false; - return Maze.MAZE[x][y] != 1; + return Maze.MAZE[y][x] != 1; + } + + public void render() { + Raylib.DrawCircle(posX * 50 + 25, posY * 50 + 25, 25.f, Jaylib.RED); } public Pair getCoordinates() { return new Pair<>(posX, posY); } + public void setCoordinates(int x, int y) { + posX = x; + posY = y; + } + public BitSet getChromosome() { return chromosome; }