J'ai rajouté des trucs et j'ai la flemme de faire un commit correct

This commit is contained in:
Nabil Ould Hamou 2024-10-14 18:33:21 +02:00
parent 4d879f83a6
commit 8cc8dfc0b1
3 changed files with 108 additions and 6 deletions

View file

@ -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<Integer> 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();
}

View file

@ -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<Integer, Integer> 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<Population> 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() {

View file

@ -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<Integer> getMoves() {
List<Integer> 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<Integer, Integer> getCoordinates() {
return new Pair<>(posX, posY);
}
public void setCoordinates(int x, int y) {
posX = x;
posY = y;
}
public BitSet getChromosome() {
return chromosome;
}