Skip to content

Commit

Permalink
genome finished
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 15, 2020
1 parent 2712c08 commit ad06fc4
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 143 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
out/*
.idea/*
*.iml
*.iml
test/*
51 changes: 12 additions & 39 deletions src/simulation/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,31 @@ public class Animal{
private final int birthDay;
private int deathDay;
private final Genome genome;
private Simulation simulation;

private Location location;
private Direction direction;
private int energy;

Animal(int energy, int birthDay){
this.birthDay = birthDay;
this.energy = energy;
this.genome = new Genome();
};

private List<AnimalStateAfterDay> stateAfterEachDay = new ArrayList<>();

Animal(Animal parent1, Animal parent2, int energy){
this.simulation = parent1.simulation;
Animal(Animal parent1, Animal parent2, int birthDay){
this.genome = new Genome(parent1.getGenome(), parent2.getGenome());
this.energy = energy;
this.birthDay = simulation.getCurrentDay();
this.energy = parent1.energy / 4 + parent2.energy / 4;
this.birthDay = birthDay;
}

public void kill() throws AnimalStateException {
public void kill(int deathDay) throws AnimalStateException {
if (!this.isAlive())
throw new AnimalStateException("Trying to kill a dead animal: " + this.toString());
this.energy = 0;
this.deathDay = simulation.getCurrentDay();
this.deathDay = deathDay;
}
public Direction shift() {
return direction = direction.shiftedBy(genome.pickRandomGene());
public void shift() {
direction = direction.shiftedBy(genome.pickRandomGene());
}

public Location move() throws AnimalStateException {
if (!this.isAlive())
throw new AnimalStateException("Trying to move a dead animal: " + this.toString());
if (this.energy < simulation.getParams().getMoveEnergy())
this.kill();
this.energy -= simulation.getParams().getMoveEnergy();
int boardWidth = simulation.getParams().getWidth();
int boardHeight = simulation.getParams().getHeight();
int newX = (location.getX() + direction.getX() + boardWidth) % boardWidth;
int newY = (location.getY() + direction.getY() + boardHeight) % boardHeight;
return location = new Location(newX,newY);
}

private void saveStateAfterDay(){

}


public int getBirthDay() {
return birthDay;
}
Expand All @@ -63,10 +44,6 @@ public Genome getGenome() {
return genome;
}

public Location getLocation() {
return location;
}

public Direction getDirection() {
return direction;
}
Expand All @@ -78,8 +55,4 @@ public boolean isAlive() {
public int getEnergy() {
return energy;
}

public List<AnimalStateAfterDay> getStateAfterEachDay() {
return stateAfterEachDay;
}
}
17 changes: 0 additions & 17 deletions src/simulation/AnimalStateAfterDay.java

This file was deleted.

18 changes: 16 additions & 2 deletions src/simulation/Board.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package simulation;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Board {
private AbstractSet<Location> plantedSpots = new TreeSet<>();
private AbstractMap<Location, List<Animal>> animalLocations = new TreeMap<>();
private AbstractMap<Genome,Integer> dominantGenomes = new TreeMap<>();
private AbstractMap<Location, List<Animal>> locationAnimalMap = new TreeMap<>();
public List<Animal> getAnimalList(){
List<Animal> allAnimals = new ArrayList<Animal>();
for (List<Animal> locationAnimals : locationAnimalMap.values()){
allAnimals = Stream.concat(allAnimals.stream(),locationAnimals.stream()).collect(Collectors.toList());
}
return allAnimals;
}
public void insert(Animal animal, int x, int y){
locationAnimalMap.get(new Location(x,y)).add(animal);
}
public List<Animal> getAnimalsAt(int x, int y){
return locationAnimalMap.get(new Location(x,y));
}
}
43 changes: 0 additions & 43 deletions src/simulation/BoardParams.java

This file was deleted.

9 changes: 0 additions & 9 deletions src/simulation/Day.java

This file was deleted.

76 changes: 58 additions & 18 deletions src/simulation/Genome.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,84 @@
package simulation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.*;
import java.util.stream.Collectors;

public class Genome {
private final List<Integer> genes;
private int length;

Genome(int length){
Genome(){
Random rng = new Random();
List<Integer> genes;
do {
genes = new ArrayList<>();
for (int i = 0; i < length; i++) {
for (int i = 0; i < 32; i++) {
genes.add(rng.nextInt(8));
}
} while (! this.isValid());
this.genes = (List<Integer>) genes.stream().sorted();
this.length = length;
} while (! containsAllGenes(genes));
this.genes = genes.stream().sorted().collect(Collectors.toList());
};

Genome(Genome g1, Genome g2){
Random rng = new Random();
List<Integer> genes = new ArrayList<>();
Genome(Genome g0, Genome g1){
Random rng = new Random(System.nanoTime());
List<Integer> genes = new ArrayList<Integer>();
List<Genome> parentGenomes = new ArrayList<Genome>();
parentGenomes.add(g0);
parentGenomes.add(g1);
int breakpt1 = rng.nextInt(31);
int breakpt2 = breakpt1 + 1 + rng.nextInt(31-breakpt1);
int firstPartFrom = rng.nextInt(2);
int secondPartFrom = rng.nextInt(2);
int thirdPartFrom = firstPartFrom == secondPartFrom ? (firstPartFrom ^ 1) & 1 : rng.nextInt(2);

this.genes = genes;
genes.addAll(parentGenomes.get(firstPartFrom).genes.subList(0,breakpt1));
genes.addAll(parentGenomes.get(secondPartFrom).genes.subList(breakpt1,breakpt2));
genes.addAll(parentGenomes.get(thirdPartFrom).genes.subList(breakpt2,32));
this.genes = Genome.addMissingGenes(genes.stream().sorted().collect(Collectors.toList()));
};

private static List<Integer> addMissingGenes(List<Integer> genes){
while (getMissingGene(genes) != null){
AbstractSet<Integer> occurringGenes = new TreeSet<Integer>();
AbstractSet<Integer> repeatingGenes = new TreeSet<Integer>();
List<Integer> repeatingGeneIndices = new ArrayList<Integer>();
for (Integer gene : genes){
if(occurringGenes.contains(gene)){
repeatingGenes.add(gene);
}
occurringGenes.add(gene);
}
for (int i = 0; i < genes.size(); i++){
if (repeatingGenes.contains(genes.get(i))){
repeatingGeneIndices.add(i);
}
}
Random rng = new Random(System.nanoTime());
int i = rng.nextInt(repeatingGeneIndices.size());
genes.set(repeatingGeneIndices.get(i),getMissingGene(genes));
}
return genes;
}

public int pickRandomGene(){
int i = new Random().nextInt(genes.size());
return genes.get(i);
}

public boolean isValid(){
// checks if the genome contains all 8 genes
int bitMask = 0;
public static boolean containsAllGenes(List<Integer> genes){
return getMissingGene(genes) == null;
}

public static Integer getMissingGene(List<Integer> genes){
int bitMask = 0; // bitwise 00000000
for (int gene : genes) bitMask |= (1 << gene);
return bitMask == (2^8 - 1);
for (int i = 0; i < 8; i++){
if (((bitMask >> i) & 1) == 0){
return i;
}
}
return null;
}

public boolean isValid(){
return Genome.containsAllGenes(this.genes);
}
}
13 changes: 13 additions & 0 deletions src/simulation/GenomeWatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package simulation;

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

public class GenomeWatcher{
Board board;
private List<Genome> genomeFrequency = new ArrayList<Genome>();

public void update(){};
public Genome getDominantGenome(){return genomeFrequency.get(0);};
public List<Animal> getAnimalsWithDominantGenome(){return null;};
};
32 changes: 19 additions & 13 deletions src/simulation/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
package simulation;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class Simulation {

private List<Day> day = new ArrayList<Day>();
private final BoardParams params;
private int currentDay = 0;
private final String name;


public Simulation(String name, BoardParams params) {
public final String name;
public final Board board = new Board();
private final int width;
private final int height;
private final double jungleRatio;
private final double moveEnergy;
private final double plantEnergy;
private final int initialAnimalsNum;
private AbstractMap<Genome,Integer> dominantGenomes = new TreeMap<>();

public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum) {
this.name = name;
this.params = params;
this.width = width;
this.height = height;
this.jungleRatio = jungleRatio;
this.moveEnergy = moveEnergy;
this.plantEnergy = plantEnergy;
this.initialAnimalsNum = initialAnimalsNum;
}



public int getCurrentDay() {
return currentDay;
}

public String getName() {
return name;
}

public BoardParams getParams() {
return this.params;
}
}
2 changes: 1 addition & 1 deletion src/simulation/SimulationErrorException.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ public class SimulationErrorException extends Exception{
public SimulationErrorException(String errorMessage) {
super(errorMessage);
}
}
}

0 comments on commit ad06fc4

Please sign in to comment.