Commit 8c90ce54 authored by erreiip's avatar erreiip
Browse files

Add tick methods to Bots

parent 866c64dc
Loading
Loading
Loading
Loading
+413 B (670 B)
Loading image diff...
+3 −4
Changes for jeuAssemblage/src/bot/difficulty/AbstractDifficulty.java: 3 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ import bot.strategy.AIStrategy;
import bot.strategy.AbstractStrategyMemo;
import model.PlayBoard;

public abstract class AbstractDifficulty implements IBot, IStrategyBot {
public abstract class AbstractDifficulty implements IBot {

    public final int AI_STRATEGY = 0;
    public final int MEMO_STRATEGY = 1;
@@ -39,10 +39,9 @@ public abstract class AbstractDifficulty implements IBot, IStrategyBot {
        }
    }


    @Override
    public Move nextMove() {
        return this.strategyBot.nextMove();
    public void tick() {
        this.strategyBot.tick();
    }
   
    
+1 −1
Changes for jeuAssemblage/src/bot/interfaces/IBot.java: 1 added line, 1 removed line.
Original line number Diff line number Diff line
package bot.interfaces;

public interface IBot {
public interface IBot extends IStrategyBot {

    public abstract void setStrategy(int strategy);
}
+1 −3
Changes for jeuAssemblage/src/bot/interfaces/IStrategyBot.java: 1 added line, 3 removed lines.
Original line number Diff line number Diff line
package bot.interfaces;

import bot.Move;

public interface IStrategyBot {

    public abstract Move nextMove();
    public abstract void tick();
}
+62 −25
Changes for jeuAssemblage/src/bot/strategy/AIStrategy.java: 62 added lines, 25 removed lines.
Original line number Diff line number Diff line
@@ -22,10 +22,11 @@ public class AIStrategy implements IStrategyBot {
    
    private static final int POP_SIZE = 100;

    protected List<Move> moves;
    protected List<PlayBoard> pop;
    protected PlayBoard model;
    protected long seed;
    protected int testSize;
    private Random rand;
    
    public AIStrategy(PlayBoard model) {
        this(model, SeedUtils.generateRandomSeed(), AIStrategy.EASY);
@@ -42,22 +43,21 @@ public class AIStrategy implements IStrategyBot {
        this.model = model;
        this.seed = seed;
        this.testSize = testSize;
        this.moves = getMoves();
    }
    
    private List<Move> getMoves() {
        List<Move> ret = new LinkedList<Move>();
        this.rand = new Random(this.seed);

        Random rand = new Random(this.seed);

        List<PlayBoard> population = createPopulation(this.model, POP_SIZE, rand);
        population.add(model);
        this.pop = createPopulation(this.model, POP_SIZE, this.rand);
        this.pop.add(model);
    }
    
        for (int i = 0; i < this.testSize; i++) {
            population = mutation(population, rand, 1d);
    @Override
    public void tick() {
        pop = mutation(pop, 1d);
    }

        PlayBoard bestSolution = population.get(0);
    public List<Move> getMoves() {
        List<Move> ret = new ArrayList<Move>();

        PlayBoard bestSolution = pop.get(0);
        ArrayList<Piece> pieceAl = new ArrayList<Piece>();

        for (int i = 0; i < bestSolution.getBoardHeight(); i++) {
@@ -80,7 +80,11 @@ public class AIStrategy implements IStrategyBot {
        return ret;
    }
    
    private List<PlayBoard> mutation(List<PlayBoard> toMutate, Random rand, double mutationRate) {

    // ******************
    // Privates methods
    // ******************
    private List<PlayBoard> mutation(List<PlayBoard> toMutate, double mutationRate) {

        List<PlayBoard> postMutation = new ArrayList<PlayBoard>();

@@ -91,11 +95,11 @@ public class AIStrategy implements IStrategyBot {
            if (nextRand < mutationRate) {
                PlayBoard son;
                if (nextRand < mutationRate / 3) {
                    son = individualMutationSwapping(parent, rand);
                    son = individualMutationSwapping(parent);
                } else if (nextRand < 2 * (mutationRate / 3)) {
                    son = individualMutationRotation(parent, rand);
                    son = individualMutationRotation(parent);
                } else {
                    son = individualMutationMouvement(parent, rand);
                    son = individualMutationMouvement(parent);
                }

                if (!parent.equals(son)) postMutation.add(son);
@@ -109,7 +113,7 @@ public class AIStrategy implements IStrategyBot {
        return postMutation;
    }
    
    private PlayBoard individualMutationSwapping(PlayBoard parent1, Random rand) {
    private PlayBoard individualMutationSwapping(PlayBoard parent1) {

        PlayBoard son = PlayBoard.constructCopyPlayBoard(parent1);

@@ -127,7 +131,7 @@ public class AIStrategy implements IStrategyBot {
        return son;
    }
    
    private PlayBoard individualMutationRotation(PlayBoard parent1, Random rand) {
    private PlayBoard individualMutationRotation(PlayBoard parent1) {

        PlayBoard son = PlayBoard.constructCopyPlayBoard(parent1);

@@ -158,7 +162,7 @@ public class AIStrategy implements IStrategyBot {
        return son;
    }
    
    private PlayBoard individualMutationMouvement(PlayBoard parent1, Random rand) {
    private PlayBoard individualMutationMouvement(PlayBoard parent1) {
        final int MAX_MOUVEMENT = 4;

        PlayBoard son = PlayBoard.constructCopyPlayBoard(parent1);
@@ -187,11 +191,6 @@ public class AIStrategy implements IStrategyBot {
        return son;
    }

    @Override
    public Move nextMove() {
        return this.moves.remove(0);
    }

    private static List<PlayBoard> createPopulation(PlayBoard model, int size, Random rand) {

        List<PlayBoard> population = new ArrayList<PlayBoard>();
@@ -206,6 +205,44 @@ public class AIStrategy implements IStrategyBot {
        return population;
    }

    // ******************
    // ONLY HERE FOR TESTS
    // ******************
    private List<Move> testAI() {
        List<Move> ret = new LinkedList<Move>();

        Random rand = new Random(this.seed);

        List<PlayBoard> population = createPopulation(this.model, POP_SIZE, rand);
        population.add(model);

        for (int i = 0; i < this.testSize; i++) {
            population = mutation(population, 1d);
        }

        PlayBoard bestSolution = population.get(0);
        ArrayList<Piece> pieceAl = new ArrayList<Piece>();

        for (int i = 0; i < bestSolution.getBoardHeight(); i++) {
            for (int j = 0; j < bestSolution.getBoardWidth(); j++) {
                int pieceId = bestSolution.getPieceIdAt(j, i);
                if (pieceId != 0) {
                    Piece p = bestSolution.getPieceById(pieceId);
                    if (!pieceAl.contains(p)) {
                        Point upperLeftCorner = bestSolution.getUpperLeftPieceCornerById(pieceId);
                        ret.add(new Move(pieceId, upperLeftCorner.x, upperLeftCorner.y));
                        pieceAl.add(p);
                    }
                }
            }
        }

        System.out.println(bestSolution);
        System.out.println(bestSolution.getArea());

        return ret;
    }

    public static void main(String args) {
        new AIStrategy(PlayBoard.constructPlayBoard(SeedUtils.generateRandomSeed(), 8, 8, 4));
    }
Loading