Commit 8428332a authored by Ant01n3's avatar Ant01n3

Activated the DemographicManager in BoidGraph.

It is actually created with a constant probability of 0.01 for birth and
death for each species.

A bug remains : a species disappear, is this tied to the display and
ui.class ??
parent 03bed016
......@@ -109,6 +109,11 @@ public class Boid extends AdjacencyListNode {
}
public void checkNeighborhood(Boid... boids) {
if(getGraph().getNode(getId()) == null) {
System.err.printf("I do not exist any more !!!%n");
return;
}
if (boids != null) {
Iterator<Boid> it = getNeighborNodeIterator();
LinkedList<Boid> toRemove = null;
......@@ -141,8 +146,11 @@ public class Boid extends AdjacencyListNode {
}
for (Boid b2 : boids) {
if (getEdgeBetween(b2) == null)
getGraph().addEdge(getEdgeId(this, b2), this, b2);
if (getEdgeBetween(b2) == null) {
if(getGraph().getNode(b2.getId())!= null)
getGraph().addEdge(getEdgeId(this, b2), this, b2);
else System.err.printf("%s does not exists !!%n", b2.getId());
}
}
} else {
while (getDegree() > 0)
......
......@@ -135,9 +135,11 @@ public class BoidGraph extends AdjacencyListGraph {
* Listeners for boid-graph specific events.
*/
protected ArrayList<BoidGraphListener> listeners = new ArrayList<BoidGraphListener>();
/**
* New context.
* New boids simulation represented as an interaction graph.
*
* <p>All parameters are set to defaults.</p>
*/
public BoidGraph() {
super("boids-context");
......@@ -159,6 +161,14 @@ public class BoidGraph extends AdjacencyListGraph {
setForcesFactory(new NTreeForcesFactory(this));
}
/**
* New boids simulation represented as an interaction graph.
*
* <p>This pre-load a configuration from a DGS file.</p>
*
* @param dgsConfig The initial configuration in DGS format.
* @throws IOException If the DGS cannot be read.
*/
public BoidGraph(String dgsConfig) throws IOException {
this();
loadDGSConfiguration(dgsConfig);
......@@ -207,6 +217,10 @@ public class BoidGraph extends AdjacencyListGraph {
config.removeSink(this);
}
/**
* Set the factory used to instantiate the force system to use.
* @param bff The force system factory.
*/
public void setForcesFactory(BoidForcesFactory bff) {
if (forcesFactory != null)
forcesFactory.end();
......@@ -290,15 +304,19 @@ public class BoidGraph extends AdjacencyListGraph {
this.maxSteps = maxSteps;
}
/**
* The random number generator used.
* @return The random number generator.
*/
public Random getRandom() {
return random;
}
public BoidSpecies getOrCreateSpecies(String name) {
protected BoidSpecies getOrCreateSpecies(String name) {
return getOrCreateSpecies(name, null);
}
public BoidSpecies getOrCreateSpecies(String name, String clazz) {
protected BoidSpecies getOrCreateSpecies(String name, String clazz) {
BoidSpecies species = boidSpecies.get(name);
if (species == null) {
......@@ -342,25 +360,54 @@ public class BoidGraph extends AdjacencyListGraph {
return species;
}
/**
* The species with the given name.
* @param name The species name.
* @return The corresponding species or null if not found.
*/
public BoidSpecies getSpecies(String name) {
return boidSpecies.get(name);
}
/**
* The number of boid species actually.
* @return The number of boid species.
*/
public int getSpeciesCount() {
return boidSpecies.size();
}
/**
* The species whose name is "default".
* @return The default species.
*/
public BoidSpecies getDefaultSpecies() {
return getSpecies("default");
}
/**
* Add a species with name "default" if does not yet exists.
* @return The created default species or the old one if it was already present.
*/
public BoidSpecies addDefaultSpecies() {
return getOrCreateSpecies("default");
}
/**
* Remove a species.
*
* This also removed all the boids of this species. You cannot remove the "default" species.
*
* @param name The species name.
*/
public void deleteSpecies(String name) {
if (!name.equals("default"))
boidSpecies.remove(name);
if (!name.equals("default")) {
BoidSpecies species = boidSpecies.get(name);
if(species != null) {
species.release();
boidSpecies.remove(name);
}
}
}
// Commands
......@@ -457,32 +504,32 @@ public class BoidGraph extends AdjacencyListGraph {
loop = true;
while (loop) {
stepBegins(step);
for (BoidSpecies sp : boidSpecies.values())
sp.terminateLoop();
sleep(sleepTime);
step++;
step();
if (maxSteps > 0 && step > maxSteps)
loop = false;
sleep(sleepTime);
}
}
public void step() {
stepBegins(step + 1);
for (BoidGraphListener listener : listeners) {
listener.step(step + 1);
}
step++;
stepBegins(step);
}
@Override
public void stepBegins(double step) {
super.stepBegins(step);
for (BoidSpecies sp : boidSpecies.values()) {
sp.terminateStep(step);
}
for (BoidGraphListener listener : listeners) {
listener.step(step);
}
forcesFactory.step();
super.stepBegins(step);
}
public boolean isLooping() {
......@@ -514,6 +561,17 @@ public class BoidGraph extends AdjacencyListGraph {
public void addBoidGraphListener(BoidGraphListener listener) {
listeners.add(listener);
}
/**
* Unregister a listener for boid specific events.
* @param listener The listener to remove.
*/
public void removeBoidGraphListener(BoidGraphListener listener) {
int index = listeners.indexOf(listener);
if(index >= 0) {
listeners.remove(index);
}
}
@Override
protected void addNodeCallback(AbstractNode node) {
......
......@@ -44,7 +44,7 @@ public interface BoidGraphListener {
* forces model.
* @param time The current iteration time.
*/
void step(int time);
void step(double time);
/**
* One boid was added.
......
......@@ -33,6 +33,7 @@ import java.util.HashMap;
import java.util.Iterator;
import org.graphstream.boids.BoidGraph;
import org.graphstream.graph.Node;
/**
* Parameters for each boids species.
......@@ -143,12 +144,17 @@ public class BoidSpecies implements Iterable<Boid> {
/**
* Allow to create unique identifiers for boids.
*/
private int currentIndex = 0;
protected int currentIndex = 0;
/**
* Allow to create unique identifiers for boids.
*/
private long timestamp = System.nanoTime();
protected long timestamp = System.nanoTime();
/**
* Handle the addition or removal of boids.
*/
protected DemographicManager pop;
/**
* New default species with a random color.
......@@ -177,6 +183,7 @@ public class BoidSpecies implements Iterable<Boid> {
inertia = 1.1f;
fearFactor = 1;
addSpeciesNameInUIClass = true;
pop = new DemographicManager.SpeciesDemographicManager(this, ctx, new Probability.ConstantProbability(0.01), new Probability.ConstantProbability(0.01));
this.color = new Color(ctx.random.nextFloat(), ctx.random.nextFloat(),
ctx.random.nextFloat());
......@@ -325,10 +332,11 @@ public class BoidSpecies implements Iterable<Boid> {
/**
* This method is called by {@link org.graphstream.boids.BoidGraph} at the
* end of each step. It can be used by sub-classes to add some code.
* @param time The current boid graph time.
*/
public void terminateLoop() {
// Do nothing.
// Can be used by extending classes.
public void terminateStep(double time) {
System.err.printf("## pop for %s%n", getName());
pop.step(time);
}
/**
......@@ -568,7 +576,41 @@ public class BoidSpecies implements Iterable<Boid> {
public void setAngleOfView(double aov) {
angleOfView = aov;
}
/**
* The probability that a boids clones itself.
* @param probability A probability instance.
*/
public void setReproductionProbability(Probability probability) {
pop.setReproduceCondition(probability);
}
/**
* The probability that a boids die.
* @param probability A probability instance.
*/
public void setDeathCondition(Probability probability) {
pop.setDeathProbability(probability);
}
/**
* Unregister this species from the boids graph.
*
* This removes all boids pertaining to this species, and release the link with the graph.
*/
public void release() {
pop.release();
Iterator<Node> i = ctx.getNodeIterator();
while(i.hasNext()) {
Boid b = (Boid) i.next();
if(b.getSpecies() == this) {
i.remove();
}
}
}
/*
* (non-Javadoc)
*
......
......@@ -154,7 +154,7 @@ public class DemographicManager implements BoidGraphListener {
b = futureParents.poll();
String id = b.getSpecies().createNewId();
System.err.printf(" -> adding boid %s%n", id);
ctx.addNode(id);
// ctx.addBoid(b.getSpecies(), b.getPosition().x, b.getPosition().y, b
// .getPosition().z);
......@@ -181,6 +181,8 @@ public class DemographicManager implements BoidGraphListener {
}
}
System.err.printf("Preparing to remove %d boids and add %d boids.%n", toRemove.size(), futureParents.size());
makeLove();
killAll();
......@@ -199,9 +201,16 @@ public class DemographicManager implements BoidGraphListener {
* Call this method after each step of boid computation, to add or remove boids according to
* reproduction rules.
*/
public void step(int time) {
public void step(double time) {
check();
}
/**
* Release this manager, and remove as a listener for the graph.
*/
public void release() {
ctx.removeBoidGraphListener(this);
}
/**
* A demographic manager that handles boids species.
......
......@@ -65,6 +65,7 @@ public class NTreeForces extends BoidForces {
*
* @see org.graphstream.boids.BoidForces#getPosition()
*/
@Override
public Point3 getPosition() {
return p.getPosition();
}
......@@ -74,6 +75,7 @@ public class NTreeForces extends BoidForces {
*
* @see org.graphstream.boids.BoidForces#setPosition(double, double, double)
*/
@Override
public void setPosition(double x, double y, double z) {
p.setPosition(x, y, z);
}
......@@ -83,6 +85,7 @@ public class NTreeForces extends BoidForces {
*
* @see org.graphstream.boids.BoidForces#getNextPosition()
*/
@Override
public Point3 getNextPosition() {
return p.getNextPosition();
}
......@@ -91,10 +94,10 @@ public class NTreeForces extends BoidForces {
* Recursively explore the n-tree to search for intersection cells, and the
* visible boids.
*
* @param source
* The boid the forces are computed on.
* @param cell
* The cell to explore recursively.
* @param contacts
* The set of visible boids to build by exploration.
*/
protected void exploreTree(Cell cell, Set<Boid> contacts) {
if (intersection(boid, cell)) {
......@@ -113,10 +116,10 @@ public class NTreeForces extends BoidForces {
* A leaf cell has been found that is in intersection with the boid area,
* computes the forces from this cell.
*
* @param source
* The boid the forces are computed on.
* @param cell
* The cell.
* @param contacts
* The set of visible boids to build by exploration.
*/
protected void forcesFromCell(Cell cell, Set<Boid> contacts) {
Iterator<? extends Particle> particles = cell.getParticles();
......@@ -190,6 +193,7 @@ public class NTreeForces extends BoidForces {
*
* @see org.graphstream.boids.BoidForces#getNeighborhood()
*/
@Override
public Collection<Boid> getNeighborhood() {
HashSet<Boid> neigh = new HashSet<Boid>();
Cell startCell = p.getCell().getTree().getRootCell();
......
......@@ -166,7 +166,7 @@ public class NTreeForcesFactory implements BoidForcesFactory, ElementSink {
*/
public void nodeRemoved(String sourceId, long timeId, String nodeId) {
Boid b = ctx.getNode(nodeId);
pbox.removeParticle(((NTreeForces) b.getForces()).p);
pbox.removeParticle(((NTreeForces) b.getForces()).p.getId());
}
/*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment