Skip to content

Commit

Permalink
Reformatted for Black Pepper code style
Browse files Browse the repository at this point in the history
  • Loading branch information
markhobson committed Nov 15, 2018
1 parent 9f8480d commit 9d843f2
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 257 deletions.
23 changes: 12 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>uk.co.blackpepper.neuroevolution</groupId>
<artifactId>neuroevolution</artifactId>
Expand All @@ -22,11 +23,11 @@
<target>1.8</target>
</configuration>
</plugin>

</plugins>

</pluginManagement>
</plugins>

</pluginManagement>

</build>

<dependencyManagement>
Expand All @@ -38,21 +39,21 @@
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
</dependency>

</dependencies>

</dependencies>

</dependencyManagement>

<dependencies>
Expand All @@ -74,7 +75,7 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</dependencies>

</project>
49 changes: 17 additions & 32 deletions src/main/java/uk/co/blackpepper/neuroevolution/ConnectionGene.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.Objects;

public class ConnectionGene implements Gene
{
public class ConnectionGene implements Gene {
private final NodeGene input;

private final NodeGene output;
Expand All @@ -14,10 +14,8 @@ public class ConnectionGene implements Gene

private final int innovation;

ConnectionGene(NodeGene input, NodeGene output, double weight, boolean enabled, int innovation)
{
if (input.equals(output))
{
ConnectionGene(NodeGene input, NodeGene output, double weight, boolean enabled, int innovation) {
if (input.equals(output)) {
throw new IllegalArgumentException("Cannot connect node gene to itself");
}

Expand All @@ -28,63 +26,51 @@ public class ConnectionGene implements Gene
this.innovation = innovation;
}

private ConnectionGene(ConnectionGene that)
{
private ConnectionGene(ConnectionGene that) {
this(that.input, that.output, that.weight, that.enabled, that.innovation);
}

public NodeGene getInput()
{
public NodeGene getInput() {
return input;
}

public NodeGene getOutput()
{
public NodeGene getOutput() {
return output;
}

public double getWeight()
{
public double getWeight() {
return weight;
}

public boolean isEnabled()
{
public boolean isEnabled() {
return enabled;
}

public ConnectionGene disable()
{
if (!enabled)
{
public ConnectionGene disable() {
if (!enabled) {
throw new IllegalStateException("Connection gene already disabled");
}

return new ConnectionGene(input, output, weight, false, innovation);
}

public int getInnovation()
{
public int getInnovation() {
return innovation;
}

@Override
public ConnectionGene copy()
{
public ConnectionGene copy() {
return new ConnectionGene(this);
}

@Override
public int hashCode()
{
public int hashCode() {
return Objects.hash(input, output, weight, enabled, innovation);
}

@Override
public boolean equals(Object object)
{
if (!(object instanceof ConnectionGene))
{
public boolean equals(Object object) {
if (!(object instanceof ConnectionGene)) {
return false;
}

Expand All @@ -98,8 +84,7 @@ public boolean equals(Object object)
}

@Override
public String toString()
{
public String toString() {
return String.format("[in=%s out=%s w=%f %d i=%d]", input, output, weight, enabled ? 1 : 0, innovation);
}
}
4 changes: 2 additions & 2 deletions src/main/java/uk/co/blackpepper/neuroevolution/Gene.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package uk.co.blackpepper.neuroevolution;

public interface Gene
{
public interface Gene {
Gene copy();
}
13 changes: 5 additions & 8 deletions src/main/java/uk/co/blackpepper/neuroevolution/GeneFactory.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package uk.co.blackpepper.neuroevolution;

public class GeneFactory
{
public class GeneFactory {
private int innovation;

public GeneFactory()
{
public GeneFactory() {
innovation = 1;
}

public ConnectionGene newConnectionGene(NodeGene input, NodeGene output, double weight)
{
public ConnectionGene newConnectionGene(NodeGene input, NodeGene output, double weight) {
return new ConnectionGene(input, output, weight, true, nextInnovation());
}

private int nextInnovation()
{
private int nextInnovation() {
return innovation++;
}
}
79 changes: 27 additions & 52 deletions src/main/java/uk/co/blackpepper/neuroevolution/Genome.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,44 @@
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

public class Genome
{
public class Genome {
private final List<Gene> genes;

public Genome(Gene... genes)
{
public Genome(Gene... genes) {
this(Stream.of(genes));
}

public Genome(Stream<Gene> genes)
{
public Genome(Stream<Gene> genes) {
List<Gene> genesList = genes.collect(toList());
checkConnectionGenesNodes(genesList);
checkConnectionGenesUnique(genesList);

this.genes = genesList;
}

private Genome(Genome that)
{
private Genome(Genome that) {
this(copyGenes(that.getGenes().collect(toList())));
}

public Genome addGene(Gene gene)
{
public Genome addGene(Gene gene) {
return addGenes(Stream.of(gene));
}

public Genome addGenes(Stream<? extends Gene> genes)
{
public Genome addGenes(Stream<? extends Gene> genes) {
return new Genome(Stream.concat(getGenes(), genes));
}

public Genome addInputNodes(int count)
{
public Genome addInputNodes(int count) {
return addGenes(Stream.generate(NodeGene::newInput).limit(count));
}

public Genome addOutputNodes(int count)
{
public Genome addOutputNodes(int count) {
return addGenes(Stream.generate(NodeGene::newOutput).limit(count));
}

public Genome disableGene(ConnectionGene connection)
{
if (!genes.contains(connection))
{
public Genome disableGene(ConnectionGene connection) {
if (!genes.contains(connection)) {
throw new IllegalArgumentException("Unknown gene");
}

Expand All @@ -71,45 +62,37 @@ public Genome disableGene(ConnectionGene connection)
);
}

public Stream<Gene> getGenes()
{
public Stream<Gene> getGenes() {
return genes.stream();
}

public Stream<NodeGene> getNodeGenes()
{
public Stream<NodeGene> getNodeGenes() {
return getNodeGenes(genes);
}

public Stream<ConnectionGene> getConnectionGenes()
{
public Stream<ConnectionGene> getConnectionGenes() {
return getConnectionGenes(genes);
}

public boolean connects(NodeGene input, NodeGene output)
{
public boolean connects(NodeGene input, NodeGene output) {
return getConnectionGenes()
.map(gene -> new HashSet<>(asList(gene.getInput(), gene.getOutput())))
.anyMatch(gene -> gene.equals(new HashSet<>(asList(input, output))));
}

public Genome mutate(GeneFactory geneFactory, Random random)
{
public Genome mutate(GeneFactory geneFactory, Random random) {
return new GenomeMutator(geneFactory, random).mutate(this);
}

public Genome copy()
{
public Genome copy() {
return new Genome(this);
}

public void print(PrintStream out)
{
public void print(PrintStream out) {
out.println(" " + genes);
}

private static Stream<Gene> copyGenes(Collection<Gene> genes)
{
private static Stream<Gene> copyGenes(Collection<Gene> genes) {
Map<NodeGene, NodeGene> newNodesByOriginal = getNodeGenes(genes)
.collect(toMap(gene -> gene, NodeGene::copy, throwingMerger(), LinkedHashMap::new));

Expand All @@ -119,53 +102,46 @@ private static Stream<Gene> copyGenes(Collection<Gene> genes)
return Stream.concat(newNodesByOriginal.values().stream(), newConnections);
}

private static ConnectionGene copyConnectionGene(ConnectionGene connection, Map<NodeGene, NodeGene> nodeMap)
{
private static ConnectionGene copyConnectionGene(ConnectionGene connection, Map<NodeGene, NodeGene> nodeMap) {
NodeGene newInput = nodeMap.get(connection.getInput());
NodeGene newOutput = nodeMap.get(connection.getOutput());

return new ConnectionGene(newInput, newOutput, connection.getWeight(), connection.isEnabled(),
connection.getInnovation());
}

private static void checkConnectionGenesNodes(Collection<Gene> genes)
{
private static void checkConnectionGenesNodes(Collection<Gene> genes) {
List<NodeGene> nodes = getNodeGenes(genes)
.collect(toList());

boolean valid = getConnectionGenes(genes)
.flatMap(gene -> Stream.of(gene.getInput(), gene.getOutput()))
.allMatch(nodes::contains);

if (!valid)
{
if (!valid) {
throw new IllegalArgumentException("Connection gene references unknown node gene");
}
}

private static void checkConnectionGenesUnique(Collection<Gene> genes)
{
private static void checkConnectionGenesUnique(Collection<Gene> genes) {
List<Set<NodeGene>> connectionNodes = getConnectionGenes(genes)
.map(gene -> new HashSet<>(asList(gene.getInput(), gene.getOutput())))
.collect(toList());

boolean unique = connectionNodes.size() == connectionNodes.stream().distinct().count();

if (!unique)
{
if (!unique) {
throw new IllegalArgumentException("Duplicate connection genes");
}
}

private static Stream<NodeGene> getNodeGenes(Collection<Gene> genes)
{
private static Stream<NodeGene> getNodeGenes(Collection<Gene> genes) {
return genes.stream()
.filter(gene -> gene instanceof NodeGene)
.map(NodeGene.class::cast);
}

private static Stream<ConnectionGene> getConnectionGenes(Collection<Gene> genes)
{
private static Stream<ConnectionGene> getConnectionGenes(Collection<Gene> genes) {
return genes.stream()
.filter(gene -> gene instanceof ConnectionGene)
.map(ConnectionGene.class::cast);
Expand All @@ -174,8 +150,7 @@ private static Stream<ConnectionGene> getConnectionGenes(Collection<Gene> genes)
/**
* @see Collectors#throwingMerger()
*/
private static <T> BinaryOperator<T> throwingMerger()
{
private static <T> BinaryOperator<T> throwingMerger() {
return (u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
};
Expand Down
Loading

0 comments on commit 9d843f2

Please sign in to comment.