Skip to content

Commit

Permalink
added highlighting dominating genome
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 23, 2020
1 parent 23b657f commit 90b3603
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 36 deletions.
4 changes: 2 additions & 2 deletions parameters.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"width": 15,
"height": 15,
"width": 20,
"height": 20,
"jungleRatio": 0.1,
"moveEnergy": 1.0,
"initialEnergy": 100.0,
Expand Down
Binary file added resources/images/highlight-animal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 14 additions & 1 deletion src/sample/SimulationCanvas.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package sample;

import javafx.scene.image.Image;
import javafx.scene.canvas.Canvas;
import simulation.Location;
import simulation.Simulation;


import java.util.List;

public class SimulationCanvas extends Canvas {
private final Simulation simulation;
private final TerrainTileFactory terrainTileFactory;
private final AnimalTileFactory animalTileFactory;
private final int tileSize = 10;
private final Image highlightAnimalImage = new Image("images/highlight-animal.png");

SimulationCanvas(Simulation simulation){
super(simulation.getWidth()*10,simulation.getHeight()*10);
Expand Down Expand Up @@ -51,10 +57,17 @@ public void update(){

}
private void drawTile(int x, int y, Tile tile){
int tileSize = 10;
int drawX = x* tileSize;
int drawY = y* tileSize;
this.getGraphicsContext2D().drawImage(tile.getImage(),drawX,drawY);
}

public void highlightLocations(List<Location> locations){
for (Location location :locations){
int x = location.getX()*tileSize;
int y = location.getY()*tileSize;
this.getGraphicsContext2D().drawImage(highlightAnimalImage,x,y);
}
}

}
51 changes: 48 additions & 3 deletions src/sample/SimulationThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,46 @@

import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import simulation.Animal;
import simulation.Simulation;
import simulation.SimulationErrorException;
import simulation.StatsWatcher;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

public class SimulationThread extends Thread{
private final Stage stage = new Stage();
private final Simulation simulation;
private final Scene simulationScene;
private final Scene scene;
private final Group root = new Group();
private final StatsWatcher statsWatcher;
private final SimulationCanvas canvas;
private final Label statsLabel = new Label();
private final Button pauseButton;
private final Button highlightButton;
private final AtomicBoolean paused = new AtomicBoolean(false);
private final AtomicBoolean dominatingAnimalsHighlighted = new AtomicBoolean(false);

public void run() {
Platform.runLater(this::displaySimulation);
}
public SimulationThread(Simulation simulation){
this.simulation = simulation;
this.simulationScene = new Scene(root, simulation.getWidth()*10+400, Math.max(simulation.getHeight()*10,400), Color.BLACK);
this.scene = new Scene(root, simulation.getWidth()*10+400, Math.max(simulation.getHeight()*10,185), Color.BLACK);
this.statsWatcher = new StatsWatcher(simulation);
this.canvas = new SimulationCanvas(simulation);
this.highlightButton = createHighlightButton();
this.pauseButton = createPauseButton();
}
public void displaySimulation() {
canvas.setLayoutX(400);
Expand All @@ -40,7 +50,10 @@ public void displaySimulation() {

root.getChildren().add(statsLabel);
root.getChildren().add(canvas);
stage.setScene(simulationScene);
root.getChildren().add(pauseButton);
root.getChildren().add(highlightButton);

stage.setScene(scene);
stage.setTitle("Evolution Simulator");
stage.setResizable(false);

Expand Down Expand Up @@ -75,4 +88,36 @@ public void displaySimulation() {
Thread th = new Thread(task);
th.start();
}
private Button createHighlightButton(){
Button button = new Button("Highlight dominating Genome");
button.setLayoutX(90);
button.setLayoutY(150);
button.setVisible(false);
button.setOnMouseClicked(mouseEvent -> {
if(dominatingAnimalsHighlighted.get()){
canvas.drawBackground();
canvas.update();
dominatingAnimalsHighlighted.set(false);
}
else {
canvas.highlightLocations(
statsWatcher.getAnimalsWithDominatingGenome()
.stream().map(Animal::getLocation)
.collect(Collectors.toList()));
this.dominatingAnimalsHighlighted.set(true);
}
});
return button;
}
private Button createPauseButton(){
Button button = new Button("RUN/PAUSE");
button.setLayoutX(5);
button.setLayoutY(150);
button.setOnMouseClicked(mouseEvent -> { paused.set(!paused.get());
this.highlightButton.setVisible(!this.highlightButton.isVisible());
});
return button;
}


}
4 changes: 0 additions & 4 deletions src/simulation/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class Animal{
this.genome = new Genome();
this.location = location;
this.direction = Direction.values()[(new Random(System.nanoTime()).nextInt(8))];
//System.out.println("Spawning "+this.toString()+" at "+location.toString());
};

Animal(Animal parent1, Animal parent2, int birthDay, Location location){
Expand All @@ -30,19 +29,16 @@ public class Animal{
this.direction = Direction.values()[(new Random(System.nanoTime()).nextInt(8))];
parent1.children.add(this);
parent2.children.add(this);
//System.out.println("Born "+this.toString()+", child of "+parent1.toString()+" and "+parent2.toString()+", at "+location);
}

public void die(int deathDay) throws AnimalStateException {
//System.out.println(this.toString()+" dies at "+this.location);
if (!this.isAlive())
throw new AnimalStateException("Trying to kill a dead animal: " + this.toString());
this.energy = 0;
this.deathDay = deathDay;
}
public void shift() {
direction = direction.shiftedBy(genome.pickRandomGene());
//System.out.println(this.toString() + " shifts to " + this.direction);
}

public int getBirthDay() {
Expand Down
8 changes: 4 additions & 4 deletions src/simulation/AnimalBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class AnimalBoard {
private AbstractMap<Location, List<Animal>> locationAnimalMap = new TreeMap<>();
private List<Animal> deadAnimals = new ArrayList<>();
private final List<Animal> deadAnimals = new ArrayList<>();

public List<Animal> getAllAlive(){
List<Animal> allAnimals = new ArrayList<>();
Expand All @@ -21,13 +21,13 @@ public void insert(Animal animal){
locationAnimals.add(animal);
}
else {
locationAnimalMap.put(animal.getLocation(), new ArrayList<Animal>());
locationAnimalMap.put(animal.getLocation(), new ArrayList<>());
locationAnimalMap.get(animal.getLocation()).add(animal);
}
}

public List<Location> locations(){
return locationAnimalMap.keySet().stream().collect(Collectors.toList());
return new ArrayList<>(locationAnimalMap.keySet());
}


Expand Down Expand Up @@ -67,7 +67,7 @@ public boolean noAnimalsAt(Location location){

public List<Animal> get(Location location){
List<Animal> animals = locationAnimalMap.get(location);
if (animals == null) return new ArrayList<Animal>();
if (animals == null) return new ArrayList<>();
else return animals;
}

Expand Down
2 changes: 0 additions & 2 deletions src/simulation/AnimalEnergyComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import java.util.Comparator;

public class AnimalEnergyComparator implements Comparator<Animal> {

@Override
public int compare(Animal a1, Animal a2) {
return Double.compare(a1.getEnergy(), a2.getEnergy());
}

}
4 changes: 0 additions & 4 deletions src/simulation/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ public enum Direction {
public Direction shiftedBy(int step){
return Direction.values()[(this.ordinal()+step)%8];
}



public int getX() {
return x;
}

public int getY() {
return y;
}
Expand Down
7 changes: 1 addition & 6 deletions src/simulation/PlantBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

public class PlantBoard {
private AbstractMap<Location,Integer> plantedSpots = new TreeMap<Location,Integer>();
public void plant(int x, int y){
plant(new Location(x,y));
}

public void plant(Location location){
if (!isPlanted(location))
getPlantedSpots().put(location,1);
Expand All @@ -25,9 +23,6 @@ public void unplant(Location location) throws UnplantingUnplantedLocationExcepti
}
}

public void unplant(int x, int y) throws UnplantingUnplantedLocationException{
unplant(new Location(x,y));
}
public boolean isPlanted(int x, int y){
return isPlanted(new Location(x,y));
}
Expand Down
12 changes: 3 additions & 9 deletions src/simulation/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class Simulation {
private final double plantEnergy;
private final int initialAnimalsNum;
private final double initialEnergy;
private List<Location> eatenPlants = new ArrayList<Location>();
private List<Location> previousAnimalLocations = new ArrayList<Location>();
private List<Location> eatenPlants = new ArrayList<>();
private List<Location> previousAnimalLocations = new ArrayList<>();

public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum, double initialEnergy) throws InvalidStartingParametersException{
this.name = name;
Expand Down Expand Up @@ -94,7 +94,6 @@ public void eatPlants() throws UnplantingUnplantedLocationException{
}
plantBoard.unplant(location);
eatenPlants.add(location);
//System.out.println("Plant eaten at " + location.toString() + " by " + animals.get(0).toString());
}
}
}
Expand Down Expand Up @@ -134,7 +133,7 @@ Location getChildLocation(Location location){
}
}
if (candidateDirections.isEmpty()) {
candidateDirections = Arrays.stream(Direction.values()).collect(Collectors.toList());;
candidateDirections = Arrays.stream(Direction.values()).collect(Collectors.toList());
}
return toBoardLimits(location.stepTo(candidateDirections.get(rng.nextInt(candidateDirections.size()))));
}
Expand All @@ -150,15 +149,10 @@ public void start(){

public void simulateOneDay() throws SimulationErrorException{
currentDay++;
System.out.println("\nSTARTING DAY "+ currentDay + "\n=======================================");
try {
//System.out.println("\nMoving animals\n=======================================");
moveAnimals();
//System.out.println("\nEating plants\n=======================================");
eatPlants();
//System.out.println("\nReproducing animals\n=======================================");
reproduceAnimals();
//System.out.println("\nAdding plants\n=======================================");
addPlants();
} catch(AnimalStateException e){
throw new SimulationErrorException("AnimalStateException: "+ e.toString(),this);
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/StatsWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String getSummary(){
"\nPlants: " + getPlantsNum() +
"\nAverage energy: " + toTwoDecimalPlaces(getAverageEnergy()) +
"\nAverage children number: " + toTwoDecimalPlaces(getAverageChildrenNumber()) +
"\nAverage life expectancy for dead animals: " + toTwoDecimalPlaces(getAverageLifeExpectancy()) +
"\nAverage life expectancy: " + toTwoDecimalPlaces(getAverageLifeExpectancy()) +
"\nDominating genome: " + (getDominatingGenome().isPresent() ? getDominatingGenome().get(): "none")+
"\nAnimals with dominating genome: " + getAnimalsWithDominatingGenome().size();
}
Expand Down

0 comments on commit 90b3603

Please sign in to comment.