Skip to content

Commit

Permalink
backend finished
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 19, 2020
1 parent 5c13c19 commit 9b80542
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 27 deletions.
5 changes: 4 additions & 1 deletion src/sample/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import simulation.Simulation;

public class Main extends Application {

Expand All @@ -18,6 +19,8 @@ public void start(Stage primaryStage) throws Exception{


public static void main(String[] args) {
launch(args);
Simulation simulation = new Simulation("Sysad",100,122,0.3,10,40,22, 100);
simulation.start();
//launch(args);
}
}
7 changes: 7 additions & 0 deletions src/simulation/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Animal{
private final int birthDay;
Expand All @@ -16,6 +17,7 @@ public class Animal{
this.energy = energy;
this.genome = new Genome();
this.location = location;
this.direction = Direction.values()[(new Random(System.nanoTime()).nextInt(8))];
};

Animal(Animal parent1, Animal parent2, int birthDay, Location location){
Expand Down Expand Up @@ -59,6 +61,10 @@ public double getEnergy() {
return energy;
}

public void addEnergy(double energy){
this.energy += energy;
}

public Location getLocation() { return location; }

public void setLocation(Location location) {
Expand All @@ -68,4 +74,5 @@ public void setLocation(Location location) {
public void setEnergy(double energy) {
this.energy = energy;
}

}
10 changes: 10 additions & 0 deletions src/simulation/AnimalBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public void insert(Animal animal){
locationAnimalMap.get(animal.getLocation()).add(animal);
}
}

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

public void remove(Animal animal) {
Location locationToRemove = null;
for (Map.Entry<Location, List<Animal>> entry : locationAnimalMap.entrySet()) {
Expand All @@ -39,6 +44,11 @@ public void remove(Animal animal) {
locationAnimalMap.remove(locationToRemove);
}
}

public boolean noAnimalsAt(Location location){
return locationAnimalMap.get(location) == null;
}

public List<Animal> get(Location location){
List<Animal> animals = locationAnimalMap.get(location);
if (animals == null) return new ArrayList<Animal>();
Expand Down
12 changes: 12 additions & 0 deletions src/simulation/AnimalEnergyComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package simulation;

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

}
2 changes: 1 addition & 1 deletion src/simulation/AnimalStateException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package simulation;

public class AnimalStateException extends SimulationErrorException {
public class AnimalStateException extends Exception {
public AnimalStateException(String errorMessage) {
super(errorMessage);
}
Expand Down
2 changes: 2 additions & 0 deletions src/simulation/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public Direction shiftedBy(int step){
return Direction.values()[(this.ordinal()+step)%8];
}



public int getX() {
return x;
}
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/InvalidRectangleException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package simulation;

public class InvalidRectangleException extends SimulationErrorException {
public class InvalidRectangleException extends Exception {
public InvalidRectangleException(String errorMessage) {
super(errorMessage);
}
Expand Down
8 changes: 8 additions & 0 deletions src/simulation/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ public static Location getRandom(int xLowerBound, int yLowerBound, int xUpperBou
public Location stepTo(Direction direction){
return new Location(x+ direction.getX(),y+direction.getY());
}

@Override
public String toString() {
return "Location{" +
"x=" + x +
", y=" + y +
'}';
}
}
39 changes: 26 additions & 13 deletions src/simulation/PlantBoard.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
package simulation;

import java.util.AbstractMap;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class PlantBoard {
AbstractMap<Location,Integer> plantedSpots = new TreeMap<Location,Integer>();
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)){
int plantsNum = plantedSpots.get(location);
plantedSpots.remove(location);
plantedSpots.put(location,plantsNum+1);
int plantsNum = getPlantedSpots().get(location);
getPlantedSpots().remove(location);
getPlantedSpots().put(location,plantsNum+1);
}
else{
plantedSpots.put(location,1);
getPlantedSpots().put(location,1);
}
}
public void unplant(int x, int y) throws UnplantingUnplantedLocationException{
if (! isPlanted(x,y)){
public void unplant(Location location) throws UnplantingUnplantedLocationException{
if (! isPlanted(location)){
throw new UnplantingUnplantedLocationException("Trying to unplant an unplanted location"
+ "x: " + x + "y: "+ y + "\n");
+ "x: " + location.getX() + "y: "+ location.getY() + "\n");
}
else {
Location location = new Location(x,y);
int plantsNum = plantedSpots.get(location);
plantedSpots.remove(location);
int plantsNum = getPlantedSpots().get(location);
getPlantedSpots().remove(location);
if (plantsNum != 1){
plantedSpots.put(location,plantsNum+-1);
getPlantedSpots().put(location,plantsNum+-1);
}
}
}

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));
}
public boolean isPlanted(Location location){
return plantedSpots.containsKey(location);
return getPlantedSpots().containsKey(location);
}

public List<Location> getPlantedLocations() {
return plantedSpots.keySet().stream().collect(Collectors.toList());
}

public AbstractMap<Location, Integer> getPlantedSpots() {
return plantedSpots;
}
}
113 changes: 105 additions & 8 deletions src/simulation/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package simulation;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
import java.util.Random;
import java.util.stream.Collectors;

public class Simulation {

private final double minimalReproduceEnergy;
private int currentDay = 0;
public final String name;
public final AnimalBoard animalBoard = new AnimalBoard();
Expand All @@ -16,14 +19,17 @@ public class Simulation {
private final double moveEnergy;
private final double plantEnergy;
private final int initialAnimalsNum;
private final double initialEnergy;

public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum) {
public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum, double initialEnergy) {
this.name = name;
this.width = width;
this.height = height;
this.initialEnergy = initialEnergy;
this.jungle = new Jungle(width,height,jungleRatio);
this.moveEnergy = moveEnergy;
this.plantEnergy = plantEnergy;
this.minimalReproduceEnergy = 2*moveEnergy;
this.initialAnimalsNum = initialAnimalsNum;
}

Expand All @@ -49,27 +55,118 @@ private void moveAnimals() throws AnimalStateException {
if (animal.getEnergy() < 0) {
animal.die(currentDay);
} else {
animal.setLocation(correctOverlap(animal.getLocation().stepTo(animal.getDirection())));
System.out.println("Moving animal from " + animal.getLocation() +" to "+toBoardLimits(animal.getLocation().stepTo(animal.getDirection())));
animal.setLocation(toBoardLimits(animal.getLocation().stepTo(animal.getDirection())));
}
animalBoard.update();
}
}

public Location correctOverlap(Location location){
public Location toBoardLimits(Location location){
return new Location(location.getX()%width, location.getY()%height);
}
public void eatPlants() throws UnplantingUnplantedLocationException{
for (Location location : plantBoard.getPlantedLocations()) {
plantBoard.unplant(location);
List<Animal> animals = animalBoard.get(location);
if (animals != null) {
double highestEnergy = animals.stream()
.max(new AnimalEnergyComparator())
.get()
.getEnergy();
List<Animal> highestEnergyAnimals = animals.stream()
.filter((animal -> animal.getEnergy() == highestEnergy))
.collect(Collectors.toList());
int n = highestEnergyAnimals.size();
for (Animal animal : highestEnergyAnimals) {
animal.addEnergy(plantEnergy / n);
}
}
}
}

public void start(){
public void reproduceAnimals(){
for (Location location : animalBoard.locations()){
List<Animal> possibleParents = new ArrayList<>();
for (Animal animal : animalBoard.get(location)){
if (animal.getEnergy() > minimalReproduceEnergy && animal.getBirthDay() < currentDay){
possibleParents.add(animal);
}
}
if (possibleParents.size() >= 2) {
possibleParents.sort(new AnimalEnergyComparator());
List<Animal> parents = possibleParents
.stream().limit(2).collect(Collectors.toList());
animalBoard.insert(
new Animal(
parents.get(0),
parents.get(1),
currentDay,
getChildLocation(location)));
for (Animal parent : parents) {
parent.setEnergy(parent.getEnergy() * 0.75);
}
}
}
}

Location getChildLocation(Location location){
List<Direction> candidateDirections = new ArrayList<>();
Random rng = new Random(System.nanoTime());
for (Direction direction : Direction.values()){
Location target = toBoardLimits(location.stepTo(direction));
if (!plantBoard.isPlanted(target) && animalBoard.noAnimalsAt(target)){
candidateDirections.add(direction);
}
}
if (candidateDirections.isEmpty()) {
candidateDirections = Arrays.stream(Direction.values()).collect(Collectors.toList());;
}
return toBoardLimits(location.stepTo(candidateDirections.get(rng.nextInt(candidateDirections.size()))));
}
public void addInitialAnimals(){
for (int i = 0; i<initialAnimalsNum;i++){
animalBoard.insert(new Animal(initialEnergy,0,Location.getRandom(width,height)));
}
}

public void start(){
addInitialAnimals();
try {
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
} catch (SimulationErrorException e){
System.out.println(e.toString());
}
}

public void simulateOneDay() throws SimulationErrorException{
currentDay++;
System.out.println("Starting day "+ currentDay);
try {
System.out.println("Moving animals");
moveAnimals();
System.out.println("Eating plants");
eatPlants();
System.out.println("Reproducing animals");
reproduceAnimals();
System.out.println("Adding plants");
addPlants();
}catch(AnimalStateException e){
throw new SimulationErrorException(e.toString());
} catch(AnimalStateException e){
throw new SimulationErrorException("AnimalStateException: "+ e.toString(),this);
} catch (UnplantingUnplantedLocationException e){
throw new SimulationErrorException("UnplantingUnplantedLocationException: " + e.toString(),this);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/simulation/SimulationErrorException.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package simulation;

public class SimulationErrorException extends Exception{
public SimulationErrorException(String errorMessage) {
super(errorMessage);
public SimulationErrorException(String errorMessage, Simulation simulation) {
super("Error in simulation " + simulation.getName() + errorMessage);
}
}
2 changes: 1 addition & 1 deletion src/simulation/UnplantingUnplantedLocationException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package simulation;

public class UnplantingUnplantedLocationException extends SimulationErrorException {
public class UnplantingUnplantedLocationException extends Exception {
public UnplantingUnplantedLocationException(String errorMessage) {
super(errorMessage);
}
Expand Down

0 comments on commit 9b80542

Please sign in to comment.