Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some bugs. Still thinking about Animal Tree. #6

Merged
merged 4 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions src/main/java/agh/ics/oop/model/Genotype.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package agh.ics.oop.model;
package agh.ics.oop.model.animal;

import agh.ics.oop.model.movement.MapDirection;
import agh.ics.oop.model.movement.Vector2d;
import agh.ics.oop.model.util.AnimalTree;
import agh.ics.oop.model.util.RandomInteger;
import agh.ics.oop.model.worldMaps.Globe;
import agh.ics.oop.model.worldMaps.AnimalConfig;

public class Animal {
private final Globe globe;
private Vector2d position;
private MapDirection direction;
private Genotype genotype;
Expand All @@ -17,40 +15,41 @@ public class Animal {
private int dayOfDeath;
private int currentGeneIndex;
private AnimalTree animalTree;
private int minEnergyToReproduce;


public Animal(Globe globe) {
this.globe=globe;
this.position=new Vector2d(RandomInteger.getRandomInt(globe.getWidth()) + 1, RandomInteger.getRandomInt(globe.getHeight()) + 1);
this.direction=MapDirection.values()[RandomInteger.getRandomInt(8)];
this.energy=globe.getAnimalsStartingEnergy();
this.genotype=new Genotype(globe.getGenotypeLength());
this.currentGeneIndex=RandomInteger.getRandomInt(globe.getGenotypeLength());
public Animal(Vector2d position, AnimalConfig animalConfig) {
this.position=position;
this.direction=MapDirection.values()[RandomInteger.getRandomInt(7)];
this.energy=animalConfig.startingEnergy();
this.genotype=new Genotype(animalConfig.genomeLength(), animalConfig.minNumberOfMutations(), animalConfig.maxNumberOfMutations());
this.currentGeneIndex=RandomInteger.getRandomInt(animalConfig.genomeLength() - 1);
this.animalTree=new AnimalTree(this);
this.minEnergyToReproduce=animalConfig.minEnergyToReproduce();
}

public Animal(Animal mother, Animal father) {
this.globe=mother.getGlobe();
public Animal(Animal mother, Animal father, AnimalConfig animalConfig) {
this.position=mother.getPosition();
this.direction=MapDirection.values()[RandomInteger.getRandomInt(8)];
this.energy=globe.getEnergyUsedToReproduce()*2;
this.direction=MapDirection.values()[RandomInteger.getRandomInt(7)];
this.energy= animalConfig.energyUsedToReproduce()*2;
this.genotype=new Genotype(mother,father);
this.currentGeneIndex=RandomInteger.getRandomInt(globe.getGenotypeLength());
mother.useEnergy(globe.getEnergyUsedToReproduce());
this.currentGeneIndex=RandomInteger.getRandomInt(animalConfig.genomeLength() - 1);
this.minEnergyToReproduce=animalConfig.minEnergyToReproduce();
mother.useEnergy(animalConfig.energyUsedToReproduce());
mother.animalTree.addChild(this.animalTree);
father.useEnergy(globe.getEnergyUsedToReproduce());
father.useEnergy(animalConfig.energyUsedToReproduce());
father.animalTree.addChild(this.animalTree);
}

private void useEnergy(int energyUsedToReproduce) {
this.energy-=energyUsedToReproduce;
}
public void nextGene(){
this.currentGeneIndex=(this.currentGeneIndex+1)%globe.getGenotypeLength();
this.currentGeneIndex=(this.currentGeneIndex+1)%genotype.getGenes().size();
}

public boolean canReproduce(){
return this.energy>=globe.getMinEnergyToReproduce();
return this.energy>=this.minEnergyToReproduce;
}

public int getNumberOfChildren(){
Expand All @@ -69,12 +68,7 @@ public int getEnergy() {
}

public Genotype getGenotype() {
return genotype;
return new Genotype(genotype);
}

public Globe getGlobe() {
return globe;
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package agh.ics.oop.model.util;

import agh.ics.oop.model.Animal;
package agh.ics.oop.model.animal;

import java.util.ArrayList;
import java.util.List;
Expand Down
100 changes: 100 additions & 0 deletions src/main/java/agh/ics/oop/model/animal/Genotype.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package agh.ics.oop.model.animal;

import agh.ics.oop.model.util.RandomInteger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class Genotype {
private final List<Integer> genes;
private final int minNumberOfMutations;
private final int maxNumberOfMutations;

public Genotype(int genotypeLength, int minNumberOfMutations, int maxNumberOfMutations) {
this.minNumberOfMutations=minNumberOfMutations;
this.maxNumberOfMutations=maxNumberOfMutations;

genes = new ArrayList<>();
for (int i = 0; i < genotypeLength; i++) {
this.genes.add(RandomInteger.getRandomInt(7));
}
}
public Genotype(Genotype other) { //copying method
this.genes = new ArrayList<>(other.genes);
this.minNumberOfMutations = other.minNumberOfMutations;
this.maxNumberOfMutations = other.maxNumberOfMutations;
}

public Genotype(Animal mother, Animal father) {
validateParents(mother, father);

this.minNumberOfMutations=mother.getGenotype().minNumberOfMutations;
this.maxNumberOfMutations=mother.getGenotype().maxNumberOfMutations;

int divisionPoint = (int) (((double) mother.getEnergy() / (father.getEnergy() + mother.getEnergy())) * mother.getGenotype().genes.size());
boolean chooseLeftSide = RandomInteger.getRandomBoolean();

int genotypeLength = mother.getGenotype().genes.size();
genes = new ArrayList<>(genotypeLength);
for (int i = 0; i < genotypeLength; i++) {
if ((chooseLeftSide && i < divisionPoint) || (!chooseLeftSide && i >= divisionPoint)) {
genes.add(mother.getGenotype().genes.get(i));
} else {
genes.add(father.getGenotype().genes.get(i));
}
}

mutate();
}

private void validateParents(Animal mother, Animal father) {
if (mother == null || father == null) {
throw new IllegalArgumentException("Both mother and father must not be null");
}
}

private void mutate() {
int numberOfMutations = RandomInteger.getRandomInt(minNumberOfMutations, maxNumberOfMutations);

for (int i = 0; i < numberOfMutations; i++) {
switchGenes(RandomInteger.getRandomInt(genes.size()-1), RandomInteger.getRandomInt(genes.size()-1)); //opcja symulacji z podmianką
randomGene(RandomInteger.getRandomInt(genes.size()-1)); //opcja symulacji pełna losowość
}
}

public void switchGenes(int firstGeneIndex, int secondGeneIndex) {
Collections.swap(genes, firstGeneIndex, secondGeneIndex);
}
public void randomGene(int geneIndex) {
genes.set(geneIndex, RandomInteger.getRandomInt(7));
}

public String toString() {
StringBuilder genotypeToString = new StringBuilder();
for (Integer gene : genes) {
genotypeToString.append(gene.toString());
}
return genotypeToString.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Genotype genotype = (Genotype) o;
return Objects.equals(genes, genotype.genes);
}
@Override
public int hashCode() {
return Objects.hash(genes);
}
public int getCurrentGene(int currentGeneIndex) {
return genes.get(currentGeneIndex);
}

public List<Integer> getGenes() {
return new ArrayList<>(genes);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agh.ics.oop.model.utils;
package agh.ics.oop.model.util;

public class CommonMethods { // idk if they are necessary and if so should they be merged to one method: valueBiggerThan(value, min) ?
public static void checkIfNotNegative(int value) throws IllegalArgumentException{
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/agh/ics/oop/model/util/RandomInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
public class RandomInteger {
private static final Random random = new Random();

public static int getRandomInt(int bound) {
return random.nextInt(bound);
public static int getRandomInt(int max) {
return random.nextInt(max+1);
}

public static int getRandomInt(int min, int max) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/agh/ics/oop/model/worldMaps/AnimalConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package agh.ics.oop.model.worldMaps;

import static agh.ics.oop.model.utils.CommonMethods.checkIfNotNegative;
import static agh.ics.oop.model.utils.CommonMethods.checkIfPositive;
import static agh.ics.oop.model.util.CommonMethods.checkIfNotNegative;
import static agh.ics.oop.model.util.CommonMethods.checkIfPositive;

public record AnimalConfig(int startingCount, int startingEnergy, int minEnergyToReproduce, int energyUsedToReproduce,
int minNumberOfMutations, int maxNumberOfMutations, int genomeLength) {
Expand Down
Loading