patternjavaMinor
Model spatial representation
Viewed 0 times
spatialrepresentationmodel
Problem
Earlier I posted a question about model simulation, now I have code for model 'spatial' representation. My model consists of moving parts that can perform actions and each part occupies space within the model.
I would like to use a graph to to represent relationships between model parts. ISpace defines methods for working with points(vertices). Space is the base class and I used two HashMaps to represent vertices and points because there should be one-to-one relation between them. I have looked at JGraph documentation and they represent graphs with sets of vertices and edges(links between two vertices), so maybe I should move my Point class into my Vertex class? (probably just use JGraph or another package instead of my own code).
Space2D should represent a 2D toroidal space (like in Conway's game of life). Vertex could be any class.
```
package spacetest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
public class SpaceTest {
public static void main(String[] args) {
int vertexCount = 1000;
int spaceWidth = 99;
int spaceHeight = 99;
Space2D space = new Space2D(0, spaceWidth, 0, spaceHeight);
Random rand = new Random();
for (int i = 0; i {
boolean addElement(P point, V vertex);
boolean removeElement(P point, V vertex);
boolean elementExists(P point, V vertex);
boolean pointExists(V vertex);
P getPoint(V vertex);
boolean vertexExists(P point);
V getVertex(P point);
boolean moveVertex(V vertex, P destination);
Collection getPoints();
Collection getVertices();
ArrayList getNeighbors(V vertex);
ArrayList getAdjacentPoints(P point);
}
abstract class Space implements ISpace {
private final Map vertices = new HashMap<>();
private final Map points = new HashMap<>();
private final Map> adjacencyMap = new HashMap<>();
@Override
p
I would like to use a graph to to represent relationships between model parts. ISpace defines methods for working with points(vertices). Space is the base class and I used two HashMaps to represent vertices and points because there should be one-to-one relation between them. I have looked at JGraph documentation and they represent graphs with sets of vertices and edges(links between two vertices), so maybe I should move my Point class into my Vertex class? (probably just use JGraph or another package instead of my own code).
Space2D should represent a 2D toroidal space (like in Conway's game of life). Vertex could be any class.
```
package spacetest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
public class SpaceTest {
public static void main(String[] args) {
int vertexCount = 1000;
int spaceWidth = 99;
int spaceHeight = 99;
Space2D space = new Space2D(0, spaceWidth, 0, spaceHeight);
Random rand = new Random();
for (int i = 0; i {
boolean addElement(P point, V vertex);
boolean removeElement(P point, V vertex);
boolean elementExists(P point, V vertex);
boolean pointExists(V vertex);
P getPoint(V vertex);
boolean vertexExists(P point);
V getVertex(P point);
boolean moveVertex(V vertex, P destination);
Collection getPoints();
Collection getVertices();
ArrayList getNeighbors(V vertex);
ArrayList getAdjacentPoints(P point);
}
abstract class Space implements ISpace {
private final Map vertices = new HashMap<>();
private final Map points = new HashMap<>();
private final Map> adjacencyMap = new HashMap<>();
@Override
p
Solution
You have some broad-spectrum questions....
-
If JGraph supports the features you need, then absolutely, you should use it.
Writing code is fun, and challenging, and all those things.... but, at some point you have to maintain it. Maintaining code becomes tedious, especially when you could be doing other things like writing code.
Offloading asks that are going to become maintenance tasks is a smart thing.
-
In your own space model, you are not doing things in what I would consider a logical format.... for a start, your code is not thread-safe.
I would expect your program to have performance challenges, and the logical first step in any performance-challenged code is to use parallelism. As a consequence, I would expect Point, Vertex, and Space2D to be Immutable... (class is a
-
Vertex+edge vs. your model. Typically with models like this there are a few foundational perspectives that make sense. You could, for example, use polar coordinates instead of x/y because polar-coordinates are more easy to do spacial geometry with. Unfortunately they involve more complicated algorithms getting data in to and out of the model. If your work is primarily 'inside the model' rather than 'outside the model', then using polar space may make sense because the bulk of the work will be faster.
The same is true for things like Edges vs. Vertices. They each have advantages in different conditions. Depending on your conditions, the one model may make sense over the other.
What you have presented here is not nearly enough to make that decision.... but, for what it's worth, what you are doing is also not really pushing the performance limits enough on your systems to make the differences noticeable.
-
If JGraph supports the features you need, then absolutely, you should use it.
Writing code is fun, and challenging, and all those things.... but, at some point you have to maintain it. Maintaining code becomes tedious, especially when you could be doing other things like writing code.
Offloading asks that are going to become maintenance tasks is a smart thing.
-
In your own space model, you are not doing things in what I would consider a logical format.... for a start, your code is not thread-safe.
I would expect your program to have performance challenges, and the logical first step in any performance-challenged code is to use parallelism. As a consequence, I would expect Point, Vertex, and Space2D to be Immutable... (class is a
public final class ..., all fields are private final, and there are no 'setter' methods). Immutable classes are thread safe, and will help a lot with performance.-
Vertex+edge vs. your model. Typically with models like this there are a few foundational perspectives that make sense. You could, for example, use polar coordinates instead of x/y because polar-coordinates are more easy to do spacial geometry with. Unfortunately they involve more complicated algorithms getting data in to and out of the model. If your work is primarily 'inside the model' rather than 'outside the model', then using polar space may make sense because the bulk of the work will be faster.
The same is true for things like Edges vs. Vertices. They each have advantages in different conditions. Depending on your conditions, the one model may make sense over the other.
What you have presented here is not nearly enough to make that decision.... but, for what it's worth, what you are doing is also not really pushing the performance limits enough on your systems to make the differences noticeable.
Context
StackExchange Code Review Q#43124, answer score: 3
Revisions (0)
No revisions yet.