-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2712c08
commit ad06fc4
Showing
10 changed files
with
121 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
out/* | ||
.idea/* | ||
*.iml | ||
*.iml | ||
test/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters