Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
gs-boids
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
graphstream
gs-boids
Commits
d5f746b6
Commit
d5f746b6
authored
Mar 13, 2012
by
gsavin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Nettoyage de printemps.
parent
e88cc204
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
391 additions
and
302 deletions
+391
-302
src/org/graphstream/boids/Boid.java
src/org/graphstream/boids/Boid.java
+24
-41
src/org/graphstream/boids/BoidSpecies.java
src/org/graphstream/boids/BoidSpecies.java
+60
-81
src/org/graphstream/boids/Context.java
src/org/graphstream/boids/Context.java
+271
-179
src/org/graphstream/boids/Forces.java
src/org/graphstream/boids/Forces.java
+0
-1
src/org/graphstream/boids/configExample.dgs
src/org/graphstream/boids/configExample.dgs
+36
-0
No files found.
src/org/graphstream/boids/Boid.java
View file @
d5f746b6
...
...
@@ -42,15 +42,16 @@ import org.miv.pherd.geom.Vector3;
public
class
Boid
extends
AdjacencyListNode
{
protected
final
BoidSpecies
species
;
protected
BoidParticle
particle
;
protected
BoidSpecies
species
;
protected
Forces
forces
;
public
Boid
(
Graph
graph
,
String
id
)
{
super
((
AbstractGraph
)
graph
,
id
);
particle
=
new
BoidParticle
((
Context
)
graph
);
species
=
((
Context
)
graph
).
getDefaultSpecies
();
forces
=
getDefaultForces
();
public
Boid
(
Graph
graph
,
BoidSpecies
species
,
String
id
)
{
super
((
AbstractGraph
)
graph
,
id
);
this
.
particle
=
new
BoidParticle
((
Context
)
graph
);
this
.
species
=
species
;
this
.
forces
=
getDefaultForces
();
}
public
void
setPosition
(
double
x
,
double
y
,
double
z
)
{
...
...
@@ -65,10 +66,6 @@ public class Boid extends AdjacencyListNode {
return
species
;
}
public
void
setSpecies
(
BoidSpecies
species
)
{
this
.
species
=
species
;
}
public
BoidParticle
getParticle
()
{
return
particle
;
}
...
...
@@ -77,31 +74,17 @@ public class Boid extends AdjacencyListNode {
return
new
Forces
.
BasicForces
();
}
@Override
protected
void
attributeChanged
(
String
sourceId
,
long
timeId
,
String
attribute
,
AttributeChangeEvent
event
,
Object
oldValue
,
Object
newValue
)
{
if
(
attribute
.
equals
(
"species"
))
{
Context
ctx
=
(
Context
)
getGraph
();
BoidSpecies
species
=
ctx
.
getOrCreateSpecies
(
newValue
.
toString
());
this
.
species
=
species
;
}
super
.
attributeChanged
(
sourceId
,
timeId
,
attribute
,
event
,
oldValue
,
newValue
);
}
protected
void
checkNeighborhood
(
BoidParticle
...
particles
)
{
//
System.err.printf("Boid %s :%n", id);
//
System.err.printf("Boid %s :%n", id);
if
(
particles
!=
null
)
{
Iterator
<
Boid
>
it
=
getNeighborNodeIterator
();
LinkedList
<
Boid
>
toRemove
=
null
;
//
System.err.printf("Sees [ ");
//
for(BoidParticle p : particles) {
//
System.err.printf("%s ", p.getBoid().id);
//
}
//
System.err.printf("]%nHas Neighbors [ ");
//
System.err.printf("Sees [ ");
//
for(BoidParticle p : particles) {
//
System.err.printf("%s ", p.getBoid().id);
//
}
//
System.err.printf("]%nHas Neighbors [ ");
while
(
it
.
hasNext
())
{
boolean
found
=
false
;
Boid
b
=
it
.
next
();
...
...
@@ -112,17 +95,17 @@ public class Boid extends AdjacencyListNode {
break
;
}
}
//
System.err.printf("%s(%b)", b.id, found);
//
System.err.printf("%s(%b)", b.id, found);
if
(!
found
&&
!
forces
.
isVisible
(
b
.
particle
,
this
.
getPosition
()))
{
if
(
toRemove
==
null
)
toRemove
=
new
LinkedList
<
Boid
>();
//
System.err.printf("(del)");
//
System.err.printf("(del)");
toRemove
.
add
(
b
);
}
}
//
System.err.printf("%n");
//
System.err.printf("%n");
if
(
toRemove
!=
null
)
{
for
(
Boid
b
:
toRemove
)
...
...
@@ -131,16 +114,16 @@ public class Boid extends AdjacencyListNode {
toRemove
.
clear
();
toRemove
=
null
;
}
//
System.err.printf("adds link to [ ");
//
System.err.printf("adds link to [ ");
for
(
BoidParticle
p
:
particles
)
{
if
(
getEdgeBetween
(
p
.
getBoid
().
getId
())
==
null
)
{
getGraph
().
addEdge
(
getEdgeId
(
this
,
p
.
getBoid
()),
getId
(),
p
.
getBoid
().
getId
());
//
System.err.printf("%s ", p.getBoid().id);
//
System.err.printf("%s ", p.getBoid().id);
}
}
}
//
System.err.printf("]%n");
//
System.err.printf("]%n");
}
public
static
final
String
getEdgeId
(
Boid
b1
,
Boid
b2
)
{
...
...
@@ -167,8 +150,8 @@ public class Boid extends AdjacencyListNode {
-
ctx
.
area
,
ctx
.
random
.
nextDouble
()
*
(
ctx
.
area
*
2
)
-
ctx
.
area
,
0
);
this
.
dir
=
new
Vector3
(
ctx
.
random
.
nextDouble
(),
ctx
.
random
.
nextDouble
(),
0
);
this
.
dir
=
new
Vector3
(
ctx
.
random
.
nextDouble
(),
ctx
.
random
.
nextDouble
(),
0
);
this
.
ctx
=
ctx
;
}
...
...
@@ -178,7 +161,7 @@ public class Boid extends AdjacencyListNode {
mySpeciesContacts
=
0
;
forces
.
compute
(
Boid
.
this
,
cell
.
getTree
().
getRootCell
());
forces
.
direction
.
scalarMult
(
species
.
directionFactor
);
forces
.
attraction
.
scalarMult
(
species
.
attractionFactor
);
forces
.
repulsion
.
scalarMult
(
species
.
repulsionFactor
);
...
...
@@ -199,7 +182,7 @@ public class Boid extends AdjacencyListNode {
}
else
{
dir
.
scalarMult
(
species
.
speedFactor
);
}
if
(
ctx
.
storeForcesAttributes
)
forces
.
store
(
this
);
...
...
src/org/graphstream/boids/BoidSpecies.java
View file @
d5f746b6
...
...
@@ -2,6 +2,7 @@ package org.graphstream.boids;
import
java.awt.Color
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
org.graphstream.boids.Context
;
...
...
@@ -10,56 +11,38 @@ import org.graphstream.boids.Context;
*
* @author Antoine Dutot
*/
public
class
BoidSpecies
extends
HashMap
<
String
,
Boid
>
{
public
class
BoidSpecies
implements
Iterable
<
Boid
>
{
/**
*
*/
private
static
final
long
serialVersionUID
=
6005548670964581065L
;
// Attributes
public
static
enum
Parameter
{
COUNT
,
ANGLE_OF_VIEW
,
VIEW_ZONE
,
SPEED_FACTOR
,
MAX_SPEED
,
MIN_SPEED
,
WIDTH
,
TRAIL
,
DIRECTION_FACTOR
,
ATTRACTION_FACTOR
,
REPULSION_FACTOR
,
INERTIA
,
FEAR_FACTOR
}
/**
* Shared settings.
*/
protected
Context
ctx
;
public
static
enum
Parameter
{
COUNT
,
VIEW_ZONE
,
SPEED_FACTOR
,
MAX_SPEED
,
MIN_SPEED
,
WIDTH
,
TRAIL
,
DIRECTION_FACTOR
,
ATTRACTION_FACTOR
,
REPULSION_FACTOR
,
INERTIA
,
FEAR_FACTOR
}
/**
* Initial number of boids of this species.
*/
public
int
count
=
200
;
/**
* The species name.
*/
p
ublic
String
name
=
randomName
()
;
p
rotected
final
String
name
;
/**
* The distance at which a boid is seen.
*/
p
ublic
double
viewZone
=
0.15f
;
p
rotected
double
viewZone
=
0.15f
;
/**
* The boid angle of view, [-1..1]. This is the cosine of the angle between
the boid
*
direction and the direction toward another boid. -1 Means 360 degrees, all is visible.
*
0 means 180 degrees only boids in front are visible, 0.5 means 90 degrees,
* 0.25 means 45 degrees.
* The boid angle of view, [-1..1]. This is the cosine of the angle between
*
the boid direction and the direction toward another boid. -1 Means 360
*
degrees, all is visible. 0 means 180 degrees only boids in front are
*
visible, 0.5 means 90 degrees,
0.25 means 45 degrees.
*/
p
ublic
double
angleOfView
=
-
1
;
p
rotected
double
angleOfView
=
-
1
;
/**
* The boid speed at each step. This is the factor by which the speedFactor
...
...
@@ -67,17 +50,17 @@ public class BoidSpecies extends HashMap<String, Boid> {
* accelerate the boid displacement, it also impacts the boid behaviour
* (oscillation around the destination point, etc.)
*/
p
ublic
double
speedFactor
=
0.3f
;
p
rotected
double
speedFactor
=
0.3f
;
/**
* Maximum speed bound.
*/
p
ublic
double
maxSpeed
=
1
f
;
p
rotected
double
maxSpeed
=
1
f
;
/**
* Minimum speed bound.
*/
p
ublic
double
minSpeed
=
0.04f
;
p
rotected
double
minSpeed
=
0.04f
;
/**
* The importance of the other boids direction "overall" vector. The
...
...
@@ -85,7 +68,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
* divided by the number of boids seen. This factor is the importance of
* this direction in the boid direction.
*/
p
ublic
double
directionFactor
=
0.1f
;
p
rotected
double
directionFactor
=
0.1f
;
/**
* How much other visible boids attract a boid. The barycenter of the
...
...
@@ -93,7 +76,7 @@ public class BoidSpecies extends HashMap<String, Boid> {
* boid an this barycenter. This factor is the importance of this vector in
* the boid direction.
*/
p
ublic
double
attractionFactor
=
0.5f
;
p
rotected
double
attractionFactor
=
0.5f
;
/**
* All the visible boids repulse the boid. The repulsion vector is the sum
...
...
@@ -101,69 +84,73 @@ public class BoidSpecies extends HashMap<String, Boid> {
* boid, scaled by the number of visible boids. This factor is the
* importance of this vector in the boid direction.
*/
p
ublic
double
repulsionFactor
=
0.001f
;
p
rotected
double
repulsionFactor
=
0.001f
;
/**
* The inertia is the importance of the boid previous direction in the boid
* direction.
*/
p
ublic
double
inertia
=
1.1f
;
p
rotected
double
inertia
=
1.1f
;
/**
* Factor for repulsion on boids of other species. The fear that this
* species produces on other species.
*/
p
ublic
double
fearFactor
=
1
;
p
rotected
double
fearFactor
=
1
;
/**
* The species main colour.
*/
p
ublic
Color
color
=
new
Color
(
1
,
0
,
0
);
p
rotected
Color
color
=
new
Color
(
1
,
0
,
0
);
/**
* The size of the trail in the GUI if any.
*/
p
ublic
int
trail
=
0
;
p
rotected
int
trail
=
0
;
/**
* The width of the particle in the GUI if any.
*/
p
ublic
int
width
=
4
;
//protected DemographicManager demographicManager
;
p
rotected
int
width
=
4
;
protected
HashMap
<
String
,
Boid
>
boids
;
// Constructors
private
int
currentIndex
=
0
;
private
long
timestamp
=
System
.
nanoTime
();
/**
* New default species with a random colour.
*/
public
BoidSpecies
(
Context
ctx
,
String
name
)
{
this
.
boids
=
new
HashMap
<
String
,
Boid
>();
this
.
ctx
=
ctx
;
this
.
name
=
name
;
this
.
color
=
new
Color
(
ctx
.
random
.
nextFloat
(),
ctx
.
random
.
nextFloat
(),
ctx
.
random
.
nextFloat
());
}
// Access
// Commands
public
String
getName
()
{
return
name
;
}
private
int
currentIndex
=
0
;
private
long
timestamp
=
System
.
nanoTime
();
public
Iterator
<
Boid
>
iterator
()
{
return
boids
.
values
().
iterator
();
}
public
void
set
(
String
p
,
String
val
)
throws
IllegalArgumentException
{
Parameter
param
=
Parameter
.
valueOf
(
p
.
toUpperCase
());
set
(
param
,
val
);
}
public
void
set
(
Parameter
p
,
String
val
)
{
switch
(
p
)
{
System
.
out
.
printf
(
"set %s of %s to %s\n"
,
p
.
name
(),
name
,
val
);
switch
(
p
)
{
case
COUNT:
count
=
Integer
.
parseInt
(
val
);
setCount
(
Integer
.
parseInt
(
val
)
);
break
;
case
VIEW_ZONE:
viewZone
=
Double
.
parseDouble
(
val
);
System
.
out
.
printf
(
"%s view zone : %f\n"
,
name
,
viewZone
);
break
;
case
SPEED_FACTOR:
speedFactor
=
Double
.
parseDouble
(
val
);
...
...
@@ -195,52 +182,44 @@ public class BoidSpecies extends HashMap<String, Boid> {
case
FEAR_FACTOR:
fearFactor
=
Double
.
parseDouble
(
val
);
break
;
case
ANGLE_OF_VIEW:
angleOfView
=
Double
.
parseDouble
(
val
);
break
;
}
}
public
String
createNewId
()
{
return
String
.
format
(
"%s
_
%x_%x"
,
name
,
timestamp
,
currentIndex
++);
return
String
.
format
(
"%s
.
%x_%x"
,
name
,
timestamp
,
currentIndex
++);
}
public
void
register
(
Boid
b
)
{
put
(
b
.
getId
(),
b
);
}
public
void
unregister
(
Boid
b
)
{
remove
(
b
.
getId
());
public
Boid
createBoid
()
{
return
createBoid
(
createNewId
());
}
public
void
terminateLoop
()
{
// Do nothing.
// Can be used by extending classes.
}
protected
static
String
randomName
()
{
return
String
.
format
(
"%c%c%c%c%c"
,
(
char
)
(
64
+
Math
.
random
()
*
26
),
(
char
)
(
64
+
Math
.
random
()
*
26
),
(
char
)
(
64
+
Math
.
random
()
*
26
),
(
char
)
(
64
+
Math
.
random
()
*
26
),
(
char
)
(
64
+
Math
.
random
()
*
26
));
public
Boid
createBoid
(
String
id
)
{
return
new
Boid
(
ctx
,
this
,
id
);
}
public
int
getPopulation
(
)
{
return
size
(
);
void
register
(
Boid
b
)
{
boids
.
put
(
b
.
getId
(),
b
);
}
public
int
getCount
(
)
{
return
count
;
void
unregister
(
Boid
b
)
{
boids
.
remove
(
b
.
getId
())
;
}
public
void
setCount
(
int
count
)
{
this
.
count
=
count
;
public
void
terminateLoop
()
{
// Do nothing.
// Can be used by extending classes.
}
public
String
getName
()
{
return
name
;
public
int
getPopulation
()
{
return
boids
.
size
()
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
public
void
setCount
(
int
count
)
{
while
(
boids
.
size
()
<
count
)
ctx
.
addNode
(
createNewId
());
}
public
double
getViewZone
()
{
...
...
src/org/graphstream/boids/Context.java
View file @
d5f746b6
...
...
@@ -30,20 +30,23 @@
*/
package
org.graphstream.boids
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.lang.reflect.InvocationTargetException
;
import
java.util.HashMap
;
import
org.graphstream.graph.Graph
;
import
org.graphstream.graph.NodeFactory
;
import
org.graphstream.graph.implementations.AbstractNode
;
import
org.graphstream.graph.implementations.AdjacencyListGraph
;
import
org.graphstream.ui.spriteManager.Sprite
;
import
org.graphstream.ui.spriteManager.SpriteManager
;
import
org.graphstream.stream.file.FileSourceDGS
;
import
org.graphstream.ui.swingViewer.Viewer
;
import
org.graphstream.ui.swingViewer.util.Camera
;
import
org.miv.pherd.ParticleBox
;
import
org.miv.pherd.ntree.Anchor
;
import
org.miv.pherd.ntree.CellSpace
;
import
org.miv.pherd.ntree.OctreeCellSpace
;
import
org.util.Environment
;
import
java.util.Random
;
...
...
@@ -55,86 +58,48 @@ import java.util.Random;
* @author Antoine Dutot
*/
public
class
Context
extends
AdjacencyListGraph
{
// Attribute
public
static
enum
Parameter
{
MAX_STEPS
,
AREA
,
SLEEP_TIME
,
STORE_FORCES_ATTRIBUTES
,
REMOVE_CAUGHT_BOIDS
,
NORMALIZE_MODE
,
RANDOM_SEED
}
/**
* Number of steps to run the simulation, 0 means infinity.
*/
p
ublic
int
maxSteps
=
0
;
p
rotected
int
maxSteps
;
/**
* The radius of the explored area. The real area range is [-area..area] in
* all three dimensions.
*/
public
float
area
=
1
;
/**
* Number of species.
*/
public
int
speciesCount
=
3
;
/**
* Maximum number of particles per cell.
*/
public
int
maxParticlesPerCell
=
10
;
protected
float
area
;
/**
* Number of milliseconds to sleep between each particle computation step.
*/
p
ublic
int
sleepTime
=
20
;
p
rotected
int
sleepTime
;
/**
* Store the forces as attributes so that each listener can retrieve the
* force vectors.
*/
p
ublic
boolean
storeForcesAttributes
=
false
;
p
rotected
boolean
storeForcesAttributes
;
/**
* Remove the boids caught by a predator ?.
*/
p
ublic
boolean
removeCaughtBoids
=
false
;
p
rotected
boolean
removeCaughtBoids
;
/**
* Normalise boids attraction/repulsion vectors (make the boids move
* constantly, since very small vectors can be extended).
*/
public
boolean
normalizeMode
=
true
;
/**
* Actual configuration directory.
*/
public
String
configDir
=
System
.
getProperty
(
"user.dir"
);
/**
* The file separator.
*/
public
String
fileSep
=
System
.
getProperty
(
"file.separator"
);
/**
* Show a GUI to monitor boids ?.
*/
public
boolean
showGui
=
false
;
/**
* public void setSleepTime( int sleepTime ) { this.sleepTime = sleepTime; }
*
* Add a mouse "boid" if the GUI is used.
*/
public
boolean
showMouse
=
false
;
protected
boolean
normalizeMode
;
/**
* The fixed random seed.
*/
public
long
randomSeed
=
1
;
public
String
[]
speciesFiles
=
null
;
// Attribute
/**
* The environment.
*/
protected
Environment
env
;
protected
long
randomSeed
;
protected
CellSpace
space
;
...
...
@@ -143,68 +108,89 @@ public class Context extends AdjacencyListGraph {
*/
protected
ParticleBox
pbox
;
// Attributes
/**
* Species for boids.
*/
protected
HashMap
<
String
,
BoidSpecies
>
boidSpecies
;
/**
* Species for the predator.
*/
// protected PredatorSpecies predatorSpecies;
/**
* The mouse representation. The mouse is a particle like boids.
*/
// protected Mouse mouse;
/**
* The main loop condition.
*/
protected
boolean
loop
=
false
;
protected
boolean
loop
;
/**
* Current step.
*/
protected
int
step
=
0
;
protected
int
step
;
/**
* Random number generator.
*/
public
Random
random
=
new
Random
();
protected
boolean
enableFakeStop
=
false
;
// Construction
protected
Random
random
;
/**
* New uninitialised context.
*
* Use setup().
* New context.
*/
public
Context
()
{
super
(
"boids-context"
);
setNodeFactory
(
new
BoidFactory
());
//nodeFactory = new BoidFactory();
int
maxParticlesPerCell
=
10
;
random
=
new
Random
();
randomSeed
=
random
.
nextLong
();
random
=
new
Random
(
randomSeed
);
loop
=
false
;
normalizeMode
=
true
;
removeCaughtBoids
=
false
;
storeForcesAttributes
=
false
;
sleepTime
=
20
;
area
=
1
;
maxSteps
=
0
;
boidSpecies
=
new
HashMap
<
String
,
BoidSpecies
>();
//boidSpecies.put("default", new BoidSpecies(this, "default"));
getOrCreateSpecies
(
"default"
);
space
=
new
OctreeCellSpace
(
new
Anchor
(-
area
,
-
area
,
-
area
),
new
Anchor
(
area
,
area
,
area
));
space
=
new
OctreeCellSpace
(
new
Anchor
(-
area
,
-
area
,
-
area
),
new
Anchor
(
area
,
area
,
area
));
pbox
=
new
ParticleBox
(
maxParticlesPerCell
,
space
,
new
BoidCellData
());
}
// Access
public
Context
(
String
dgsConfig
)
throws
IOException
{
this
();
loadDGSConfiguration
(
dgsConfig
);
}
/**
* Load configuration from a dgs file. See 'configExample.dgs' for an
* example of dgs configuration.
*
* @param dgs
* path to the DGS file containing the configuration.
* @throws IOException
* if something wrong happens with io.
*/
public
void
loadDGSConfiguration
(
String
dgs
)
throws
IOException
{
FileInputStream
in
=
new
FileInputStream
(
dgs
);
loadDGSConfiguration
(
in
);
in
.
close
();
}
public
void
setSpeciesFiles
(
String
files
)
{
speciesFiles
=
files
.
split
(
","
);
/**
* Load configuration from a dgs file. See 'configExample.dgs' for an
* example of dgs configuration.
*