Commit c90ef93d authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

first test for group models

parent 6900e8e5
Pipeline #55045 passed with stage
in 1 minute and 7 seconds
......@@ -52,6 +52,9 @@ public class CentroidGroup implements Group {
@Override
public boolean equals(Group o) {
boolean result = false;
if (o == null){
return result;
}
if (this == o) {
result = true;
......
......@@ -2,7 +2,7 @@ package org.vadere.simulator.models.groups;
import org.vadere.state.scenario.Pedestrian;
public class CentroidGroupFactory implements GroupFactory {
public class CentroidGroupFactory extends GroupFactory {
private CentroidGroupModel groupCollection;
private GroupSizeDeterminator groupSizeDeterminator;
......@@ -22,6 +22,7 @@ public class CentroidGroupFactory implements GroupFactory {
private void assignToGroup(Pedestrian ped) {
currentGroup.addMember(ped);
ped.addGroupId(currentGroup.getID());
groupCollection.registerMember(ped, currentGroup);
}
......@@ -42,11 +43,17 @@ public class CentroidGroupFactory implements GroupFactory {
.nextGroupSize());
}
@Override
@Override //listener methode (aufruf
public void elementAdded(Pedestrian pedestrian) {
if (requiresNewGroup()) {
createNewGroup();
}
assignToGroup(pedestrian);
}
@Override
public void elementRemoved(Pedestrian ped) {
CentroidGroup group = groupCollection.removeMember(ped);
System.out.printf("Remove ped %s from group %s %n", ped.getId(), group.getID());
}
}
......@@ -5,6 +5,7 @@ import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.vadere.annotation.factories.models.ModelClass;
import org.vadere.simulator.models.Model;
......@@ -27,11 +28,12 @@ public class CentroidGroupModel implements GroupModel {
private IPotentialFieldTarget potentialFieldTarget;
private AttributesCGM attributesCGM;
private int nextFreeGroupId = 0;
private AtomicInteger nextFreeGroupId;
public CentroidGroupModel() {
this.groupFactories = new TreeMap<>();
this.pedestrianGroupData = new HashMap<>();
this.nextFreeGroupId = new AtomicInteger(0);
}
@Override
......@@ -48,11 +50,7 @@ public class CentroidGroupModel implements GroupModel {
}
protected int getFreeGroupId() {
int result = nextFreeGroupId;
nextFreeGroupId++;
return result;
return nextFreeGroupId.getAndIncrement();
}
@Override
......@@ -78,6 +76,16 @@ public class CentroidGroupModel implements GroupModel {
pedestrianGroupData.put(ped, (CentroidGroup) group);
}
@Override
public CentroidGroup removeMember(ScenarioElement ped) {
return pedestrianGroupData.remove(ped);
}
public Map<ScenarioElement, CentroidGroup> getPedestrianGroupData() {
return pedestrianGroupData;
}
@Override
public CentroidGroup getNewGroup(final int size) {
CentroidGroup result = new CentroidGroup(getFreeGroupId(), size,
......@@ -87,11 +95,15 @@ public class CentroidGroupModel implements GroupModel {
@Override
public void preLoop(final double simTimeInSec) {
topography.addElementAddedListener(Pedestrian.class, getGroupFactory(-1));
topography.getSources().stream().forEach( source -> {
topography.addElementAddedListener(Pedestrian.class, getGroupFactory(source.getId()));
topography.addElementRemovedListener(Pedestrian.class, getGroupFactory(source.getId()));
});
}
@Override
public void postLoop(final double simTimeInSec) {}
public void postLoop(final double simTimeInSec) {
}
@Override
public void update(final double simTimeInSec) {}
......
......@@ -100,13 +100,13 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
@Override
public double getAgentPotential(VPoint pos, Agent pedestrian,
Agent otherPedestrian) {
System.out.printf("Ped1: %s, Ped1: %s %n", pedestrian.getId(), otherPedestrian.getId());
CentroidGroup group = groupCollection.getGroup(pedestrian);
CentroidGroup groupOther = groupCollection.getGroup(otherPedestrian);
double potential = potentialFieldPedestrian.getAgentPotential(pos,
pedestrian, otherPedestrian);
if (group.equals(groupOther)) {
if (group!= null && group.equals(groupOther)) {
potential *= attributesCGM.getGroupMemberRepulsionFactor();
}
......
......@@ -3,6 +3,8 @@ package org.vadere.simulator.models.groups;
import org.vadere.simulator.models.SpeedAdjuster;
import org.vadere.state.scenario.Pedestrian;
//verhalten.... (update step)
public class CentroidGroupSpeedAdjuster implements SpeedAdjuster {
private final CentroidGroupModel groupCollection;
......
package org.vadere.simulator.models.groups;
import org.vadere.state.scenario.DynamicElementAddListener;
import org.vadere.state.scenario.DynamicElementRemoveListener;
import org.vadere.state.scenario.Pedestrian;
public interface GroupFactory extends DynamicElementAddListener<Pedestrian> {
public int getOpenPersons();
//wenn ped erzeugt und entfernet werden
public abstract class GroupFactory
implements DynamicElementAddListener<Pedestrian>, DynamicElementRemoveListener<Pedestrian>{
public abstract int getOpenPersons();
@Override
public abstract void elementAdded(Pedestrian element);
@Override
public abstract void elementRemoved(Pedestrian element);
}
package org.vadere.simulator.models.groups;
import org.vadere.simulator.models.Model;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.ScenarioElement;
public interface GroupModel extends Model {
......@@ -8,6 +9,8 @@ public interface GroupModel extends Model {
public void registerMember(ScenarioElement ped, Group currentGroup);
public CentroidGroup removeMember(ScenarioElement ped);
public Group getNewGroup(int size);
public GroupFactory getGroupFactory(int SourceId);
......
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.annotation.factories.dataprocessors.DataProcessorClass;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.groups.CentroidGroupModel;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianIdKey;
import java.util.Optional;
@DataProcessorClass
public class PedestrianGroupIDProcessor extends DataProcessor<TimestepPedestrianIdKey, Integer>{
public PedestrianGroupIDProcessor(){
super("groupId");
}
@Override
protected void doUpdate(SimulationState state) {
Integer timeStep = state.getStep();
Optional<MainModel> mainModel = state.getMainModel();
if (mainModel.isPresent()){
Optional<CentroidGroupModel> model = mainModel.get()
.getSubmodels().stream()
.filter(m -> m instanceof CentroidGroupModel)
.map(m -> (CentroidGroupModel) m).findAny();
if (model.isPresent()){
model.get().getPedestrianGroupData().entrySet().forEach(entry ->{
this.putValue(new TimestepPedestrianIdKey(timeStep, entry.getKey().getId()), entry.getValue().getID());
});
}
}
}
@Override
public void init(final ProcessorManager manager) {
super.init(manager);
}
public String[] toStrings(TimestepPedestrianIdKey key){
Integer i = this.getValue(key);
return new String[]{Integer.toString(i)};
}
}
......@@ -60,6 +60,10 @@ public class Pedestrian extends Agent {
}
}
public void addGroupId(int groupId){
groupIds.add(groupId);
}
public <T extends ModelPedestrian> T getModelPedestrian(Class<? extends T> modelType) {
return (T) modelPedestrianMap.get(modelType);
}
......
Supports Markdown
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