Skip to content

Commit

Permalink
Completed stream-based implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cgokmen committed Jul 19, 2018
1 parent 41362d5 commit 9c3267e
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;

import java.util.Comparator;
import java.util.Map;

public class ForagingAlgorithm extends CompressionAlgorithm {
Expand Down Expand Up @@ -188,12 +189,11 @@ public boolean isMoveValid(AmoebotParticle p, ParticleGrid.Direction d) {
public Map<String, String> getInformation(ParticleGrid g) {
Map<String, String> info = super.getInformation(g);

int longestWaiting = 0;
for (Particle p : g.getAllParticles()) {
if (p instanceof ForagingAmoebotParticle) {
longestWaiting = Math.max(longestWaiting, ((ForagingAmoebotParticle) p).getLongestLastFedActivationsAgo());
}
}
int longestWaiting = g.getAllParticles()
.filter(p -> p instanceof ForagingAmoebotParticle)
.map(p -> ((ForagingAmoebotParticle) p).getLongestLastFedActivationsAgo())
.max(Comparator.comparing(Integer::valueOf))
.get();

info.put("Longest un-fed wait so far", longestWaiting + "");
return info;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/cemgokmen/particles/graphics/GridGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ public static void saveGridAsRasterImage(ParticleGrid grid, File file, String fo

public static void drawGridInfoOntoGraphics(ParticleGrid grid, Graphics2D graphics, double size) {
Map<String, String> info = new LinkedHashMap<>(grid.getGridInformation());
for (ParticleAlgorithm algorithm: grid.getRunningAlgorithms()) {
grid.getRunningAlgorithms().forEach(algorithm -> {
try {
info.putAll(PropertyUtils.getPropertyValues(algorithm, ParticleAlgorithm.class));
} catch (Exception e) {
e.printStackTrace();
}
}
});

graphics.setPaint(Color.BLACK);
graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, (int) (size / 50.0)));
Expand Down Expand Up @@ -250,7 +250,7 @@ private static void drawEdges(Graphics2D graphics, ParticleGrid grid) {

Set<Particle> drawnParticles = new HashSet<>();

for (Particle p : grid.getAllParticles()) {
grid.getAllParticles().forEach(p -> {
Vector position = grid.getUnitPixelCoordinates(grid.getParticlePosition(p)).multiply(EDGE_LENGTH);

for (Particle nbr : grid.getParticleNeighbors(p, false)) {
Expand All @@ -263,14 +263,14 @@ private static void drawEdges(Graphics2D graphics, ParticleGrid grid) {
}

drawnParticles.add(p);
}
});
}

private static void drawParticles(Graphics2D graphics, ParticleGrid grid) {
for (Particle p : grid.getAllParticles()) {
grid.getAllParticles().forEach(p -> {
Vector position = grid.getUnitPixelCoordinates(grid.getParticlePosition(p)).multiply(EDGE_LENGTH);
p.drawParticle(graphics, position, EDGE_LENGTH);
}
});
}

private static void drawCenterOfMass(Graphics2D graphics, ParticleGrid grid) {
Expand Down
55 changes: 31 additions & 24 deletions src/main/java/com/cemgokmen/particles/models/ParticleGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

import com.cemgokmen.particles.algorithms.ParticleAlgorithm;
import com.cemgokmen.particles.storage.ParticleStorage;
import com.cemgokmen.particles.util.RandomSelector;
import com.cemgokmen.particles.util.Utils;
import com.google.common.collect.ImmutableList;
import org.la4j.Vector;

import javax.annotation.Nonnull;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class ParticleGrid {
private int activationsRun = 0;
Expand Down Expand Up @@ -128,15 +131,21 @@ public boolean arePositionsAdjacent(Vector p1, Vector p2) {
return Arrays.asList(this.getAdjacentPositions(p1)).contains(p2);
}

public List<Particle> getAllParticles() {
return this.getStorage().getAllParticles();
public int getParticleCount() {
return this.getStorage().getParticleCount();
}

public int getParticleCount(@Nonnull Predicate<Particle> filter) {
return (int) this.getAllParticles(filter).count();
}

public List<Particle> getAllParticles(@Nonnull Predicate<Particle> filter) {
List<Particle> particles = this.getAllParticles();
particles.removeIf(filter);
public Stream<Particle> getAllParticles() {
return this.getStorage().getAllParticles();
}

return particles;
public Stream<Particle> getAllParticles(@Nonnull Predicate<Particle> filter) {
Stream<Particle> particles = this.getAllParticles();
return particles.filter(filter);
}

public List<Particle> getParticleNeighbors(Particle p, boolean includeNulls) {
Expand Down Expand Up @@ -195,46 +204,44 @@ public Particle getPositionNeighborInDirection(Vector p, Direction d, Predicate<
}

public void assignAllParticlesAlgorithm(ParticleAlgorithm algorithm) {
for (Particle p : this.getAllParticles()) {
this.getAllParticles().forEach(p -> {
p.setAlgorithm(algorithm);
}
});
}

public void runActivations(int numActivations) {
// We make the assumption that no particles will be added.
List<Particle> particleList = this.getAllParticles().collect(Collectors.toCollection(ArrayList::new));
RandomSelector<Particle> selector = RandomSelector.uniform(particleList);

for (int i = 0; i < numActivations; i++) {
List<Particle> particleList = this.getAllParticles();
Particle p = null;

while (p == null) {
p = selector.next(Utils.random);
if (!this.isParticleOnGrid(p)) p = null;
}

Particle p = particleList.get(Utils.randomInt(particleList.size()));
//System.out.println("Now activating " + p);
p.activate();
//System.out.println("Activated " + p);
this.activationsRun++;
}
}

public abstract List<Class<? extends ParticleAlgorithm>> getCompatibleAlgorithms();

public List<ParticleAlgorithm> getRunningAlgorithms() {
public Stream<ParticleAlgorithm> getRunningAlgorithms() {
Set<ParticleAlgorithm> runningAlgorithms = new HashSet<>();

for (Particle p : this.getAllParticles()) {
ParticleAlgorithm algorithm = p.getAlgorithm();
if (algorithm != null) {
runningAlgorithms.add(algorithm);
}
}
return new ArrayList<>(runningAlgorithms);
return this.getAllParticles().map(Particle::getAlgorithm).filter(Objects::nonNull).distinct();
}

public abstract Vector getUnitPixelCoordinates(Vector in);

public Vector getCenterOfMass() {
List<Particle> particles = this.getAllParticles();
return particles.stream()
return this.getAllParticles()
.map(this::getParticlePosition)
.reduce(Vector.zero(2), Vector::add)
.divide(particles.size());
.divide(this.getParticleCount());
}

public int getActivationsRun() {
Expand All @@ -244,7 +251,7 @@ public int getActivationsRun() {
public Map<String, String> getGridInformation() {
LinkedHashMap<String, String> map = new LinkedHashMap<>();

map.put("Particle count", this.getAllParticles().size() + "");
map.put("Particle count", this.getParticleCount() + "");
map.put("Activations run", this.activationsRun + "");
map.put("Center of mass", this.getCenterOfMass().toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ public Vector getLeveledParticlePosition(Particle p) {

@Override
public Vector getCenterOfMass() {
List<Particle> particles = this.getAllParticles();
return particles.stream()
return this.getAllParticles()
.map(this::getLeveledParticlePosition)
.reduce(Vector.zero(2), Vector::add)
.divide(particles.size());
.divide(this.getParticleCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ public static void main(String[] args) throws Exception {

// Run 10 million iterations of lambda = 10 compression first
System.out.println("Running compression");
while (true) {
/*while (true) {
grid.assignAllParticlesAlgorithm(new CompressionAlgorithm(10.0));
grid.runActivations(1000000);
GridGraphics.saveGridImage(grid, basePath.resolve(grid.getActivationsRun() + ".png").toFile());
}
/*System.out.println("Compression run successfully.");
System.out.println("Compression run successfully.");*/

// Run 10 million iterations of alpha = 10 separation
System.out.println("Running separation");
grid.assignAllParticlesAlgorithm(new SeparationAlgorithm(1.0, 10.0, true, false));
grid.runActivations(1000000);
GridGraphics.saveGridImage(grid, basePath.resolve(grid.getActivationsRun() + ".png").toFile());
System.out.println("Separation run successfully.");*/
while (true) {
grid.assignAllParticlesAlgorithm(new SeparationAlgorithm(1.0, 10.0, true, false));
grid.runActivations(1000000);
GridGraphics.saveGridImage(grid, basePath.resolve(grid.getActivationsRun() + ".png").toFile());
}
//System.out.println("Separation run successfully.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.la4j.Vector;

import java.util.List;
import java.util.stream.Stream;

public class BiMapParticleStorage implements ParticleStorage {
private final BiMap<Vector, Particle> map;
Expand All @@ -34,8 +35,8 @@ public BiMapParticleStorage(int expectedSize) {
}

@Override
public List<Particle> getAllParticles() {
return Lists.newArrayList(this.map.values());
public Stream<Particle> getAllParticles() {
return this.map.values().stream();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import com.cemgokmen.particles.models.Particle;
import org.la4j.Vector;

import java.util.List;
import java.util.stream.Stream;

public interface ParticleStorage {
List<Particle> getAllParticles();
Stream<Particle> getAllParticles();

int getParticleCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

public class TableParticleStorage implements ParticleStorage {
/**
Expand Down Expand Up @@ -77,8 +78,8 @@ public TableParticleStorage(List<Vector> extremities) {
}

@Override
public List<Particle> getAllParticles() {
return Lists.newArrayList(this.particlePositions.keySet());
public Stream<Particle> getAllParticles() {
return this.particlePositions.keySet().stream();
}

@Override
Expand Down

0 comments on commit 9c3267e

Please sign in to comment.