Notice to GitKraken users: A vulnerability has been found in the SSH key generation of GitKraken versions 7.6.0 to 8.0.0 (https://www.gitkraken.com/blog/weak-ssh-key-fix). If you use GitKraken and have generated a SSH key using one of these versions, please remove it both from your local workstation and from your LRZ GitLab profile.

21.10.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 23a3923c authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

allow lists as 'nextTarget' in a TargetChanger.

parent 46f5c4c2
......@@ -9,6 +9,6 @@
},
"reachDistance" : 0.0,
"nextTargetIsPedestrian" : false,
"nextTarget" : -1,
"nextTarget" : [-1],
"probabilityToChangeTarget" : 0.0
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package org.vadere.simulator.control.scenarioelements;
import org.apache.commons.math3.distribution.BinomialDistribution;
import org.apache.commons.math3.random.JDKRandomGenerator;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.scenario.*;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
......@@ -126,7 +127,9 @@ public class TargetChangerController {
}
private void useDynamicTargetForAgentOrUseStaticAsFallback(Agent agent) {
int nextTarget = targetChanger.getAttributes().getNextTarget();
int nextTarget = (targetChanger.getAttributes().getNextTarget().size() > 0)
? targetChanger.getAttributes().getNextTarget().get(0)
: Attributes.ID_NOT_SET;
Collection<Pedestrian> allPedestrians = topography.getElements(Pedestrian.class);
List<Pedestrian> pedsWithCorrectTargetId = allPedestrians.stream()
......@@ -160,7 +163,9 @@ public class TargetChangerController {
}
private void useStaticTargetForAgent(Agent agent) {
agent.setSingleTarget(targetChanger.getAttributes().getNextTarget(), false);
agent.setTargets(targetChanger.getAttributes().getNextTarget());
agent.setNextTargetListIndex(0);
agent.setIsCurrentTargetAnAgent(false);
}
private void notifyListenersTargetChangerAreaReached(final Agent agent) {
......
......@@ -35,6 +35,7 @@ public enum Version {
V1_8(1, 8),
V1_9(1, 9),
V1_10(1, 10),
V1_11(1, 11),
;
......
......@@ -11,16 +11,21 @@ import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.incident.helper.JsonFilterIterator;
import org.vadere.state.attributes.scenario.AttributesMeasurementArea;
import org.vadere.state.scenario.MeasurementArea;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.geometry.shapes.VShape;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
public interface JsonNodeExplorer {
default ObjectMapper getMapper(){
return StateJsonConverter.getMapper();
}
default void addToObjectNode(JsonNode node, String key, String value) {
((ObjectNode) node).put(key, value);
......@@ -111,6 +116,15 @@ public interface JsonNodeExplorer {
return nodeNotEmptyAnd(node, n -> n.getNodeType() == JsonNodeType.ARRAY);
}
default boolean nodeIsString(JsonNode node){
return nodeNotEmptyAnd(node, n -> n.getNodeType() == JsonNodeType.STRING);
}
default boolean nodeIsNumber(JsonNode node){
return nodeNotEmptyAnd(node, n -> n.getNodeType() == JsonNodeType.NUMBER);
}
default boolean nodeNotEmptyAnd(JsonNode node, Predicate<JsonNode> predicate) {
return !node.isMissingNode() && predicate.test(node);
}
......@@ -145,6 +159,21 @@ public interface JsonNodeExplorer {
});
}
default Iterator<JsonNode> iteratorTargetChangers(JsonNode node) throws MigrationException{
JsonNode tChanger = pathMustExist(node, "scenario/topography/targetChangers");
return new JsonFilterIterator(tChanger, n->true);
}
default Iterator<JsonNode> iteratorMeasurementAreas(JsonNode node) throws MigrationException{
JsonNode tChanger = pathMustExist(node, "scenario/topography/measurementAreas");
return new JsonFilterIterator(tChanger, n->true);
}
default Iterator<JsonNode> iteratorSources(JsonNode node) throws MigrationException{
JsonNode tChanger = pathMustExist(node, "scenario/topography/measurementAreas");
return new JsonFilterIterator(tChanger, n->true);
}
default Iterator<JsonNode> iteratorMeasurementArea(JsonNode node, int id) throws MigrationException {
JsonNode processors = pathMustExist(node, "scenario/topography/measurementAreas");
return new JsonFilterIterator(processors, n -> {
......@@ -194,6 +223,12 @@ public interface JsonNodeExplorer {
}
}
default void addArrayField(JsonNode node, String fieldName,Object data){
ObjectNode objNode = (ObjectNode)node;
ArrayNode dataNode = getMapper().valueToTree(data);
objNode.putArray(fieldName).addAll(dataNode);
}
/**
*
* @param scenarioFile scenarioFile to migrate
......
package org.vadere.simulator.projects.migration.jsontranformation.json;
import com.fasterxml.jackson.databind.JsonNode;
import org.vadere.annotation.factories.migrationassistant.MigrationTransformation;
import org.vadere.simulator.entrypoints.Version;
import org.vadere.simulator.projects.migration.MigrationException;
import org.vadere.simulator.projects.migration.jsontranformation.SimpleJsonTransformation;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Remove node "threatMemory" under "scenario.topography.dynamicElements.psychologyStatus"
*/
@MigrationTransformation(targetVersionLabel = "1.11")
public class TargetVersionV1_11 extends SimpleJsonTransformation {
public TargetVersionV1_11(){
super(Version.V1_11);
}
@Override
protected void initDefaultHooks() {
addPostHookFirst(this::targetChangerUseNextTargetList);
addPostHookLast(this::sort);
}
public JsonNode targetChangerUseNextTargetList(JsonNode node) throws MigrationException {
// targetChangers may be missing if an old scenario was never opened since the targetChangers where introduced
// This is ok, since a targetChangers node will be inserted if the scenario is read.
if ( !path(node, "scenario/topography/targetChangers").isMissingNode()){
Iterator<JsonNode> iter = iteratorTargetChangers(node);
while (iter.hasNext()){
JsonNode changer = iter.next();
JsonNode nextTarget = pathMustExist(changer, "nextTarget");
if (nodeIsNumber(nextTarget)){
int val = nextTarget.asInt();
List<Integer> valNew = new ArrayList<>();
valNew.add(val);
addArrayField(changer, "nextTarget", valNew);
}
}
}
return node;
}
}
......@@ -65,6 +65,14 @@ public class TargetChangerControllerTest {
}
}
private LinkedList<Integer> intList(Integer... ints){
LinkedList<Integer> ret = new LinkedList<>();
for (Integer anInt : ints) {
ret.add(anInt);
}
return ret;
}
private List<Pedestrian> createTwoPedestrianWithTargetT1(int startId) {
int seed = 0;
Random random = new Random(seed);
......@@ -150,7 +158,7 @@ public class TargetChangerControllerTest {
@Test
public void updateChangesTargetListOfAffectedPedestrianIfProbabilityIsOne() {
int nextTarget = 2;
LinkedList<Integer> nextTarget = intList(2);
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
......@@ -178,7 +186,7 @@ public class TargetChangerControllerTest {
pedestrians.get(1).setTargets(currentTargets);
pedestrians.get(1).setNextTargetListIndex(nextTargetIndex);
int nextTarget = 2;
LinkedList<Integer> nextTarget = intList(2);
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
......@@ -198,7 +206,7 @@ public class TargetChangerControllerTest {
@Test
public void updateDoesNotChangeTargetListOfAffectedPedestrianIfProbabilityIsZero() {
int nextTarget = 2;
LinkedList<Integer> nextTarget = intList(2);
double probability = 0.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
......@@ -218,9 +226,54 @@ public class TargetChangerControllerTest {
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), expectedTargetId);
}
@Test
public void targetChangerWithListOfTargetsAndStaticTargets() {
LinkedList<Integer> nextTarget = intList(2, 1);
double probability = 1.0;
pedestrians.forEach(p -> p.setTargets(intList(1)));
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
attributesTargetChanger.setNextTarget(nextTarget);
attributesTargetChanger.setProbabilityToChangeTarget(probability);
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), intList(1));
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), intList(1));
controllerUnderTest.update(simTimeInSec);
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), intList(1));
assertListEqual(pedestrians.get(1).getTargets(), intList(2, 1));
}
@Test
public void targetChangerWithListOfTargetsAndDynamicTargets() { //must choose first element
LinkedList<Integer> nextTarget = intList(1, 2);
double probability = 1.0;
pedestrians.forEach(p -> p.setTargets(intList(1)));
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
attributesTargetChanger.setNextTarget(nextTarget);
attributesTargetChanger.setProbabilityToChangeTarget(probability);
attributesTargetChanger.setNextTargetIsPedestrian(true);
TargetChanger targetChanger = new TargetChanger(attributesTargetChanger);
TargetChangerController controllerUnderTest = createTargetChangerController(targetChanger);
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), intList(1));
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), intList(1));
controllerUnderTest.update(simTimeInSec);
assertListContainsSingleTarget(pedestrians.get(0).getTargets(), intList(1));
assertListContainsSingleTarget(pedestrians.get(1).getTargets(), 1 + TargetPedestrian.UNIQUE_ID_OFFSET);
}
@Test
public void updateAddsTargetPedestrianToTopographyIfTargetIsDynamic() {
int nextTarget = 1;
LinkedList<Integer> nextTarget = intList(1);
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
......@@ -242,7 +295,7 @@ public class TargetChangerControllerTest {
@Test
public void updateChangesTargetListOfAffectedPedestrianIfTargetIsDynamic() {
int nextTarget = 1;
LinkedList<Integer> nextTarget = intList(1);
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
......@@ -266,7 +319,7 @@ public class TargetChangerControllerTest {
@Test
public void updateModifiesFollowersIfTargetIsDynamic() {
int nextTarget = 1;
LinkedList<Integer> nextTarget = intList(1);
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
......@@ -288,7 +341,7 @@ public class TargetChangerControllerTest {
@Test
public void updateModifiesPedestrianWithExistingFollwersIfTargetIsDynamic() {
int nextTarget = 1;
LinkedList<Integer> nextTarget = intList(1);
double probability = 1.0;
// Add two new agents were one follows the other.
......@@ -323,7 +376,7 @@ public class TargetChangerControllerTest {
@Test
public void updateUseStaticTargetAsFallbackIfNoPedestrianIsFoundIfTargetIsDynamic() {
int nextTarget = 3;
LinkedList<Integer> nextTarget = intList(3);
double probability = 1.0;
AttributesTargetChanger attributesTargetChanger = createAttributesWithFixedRectangle();
......@@ -347,9 +400,16 @@ public class TargetChangerControllerTest {
assertEquals(0, targetPedestrians.size());
}
private void assertListContainsSingleTarget(LinkedList<Integer> targetList, int targetId) {
assertTrue(targetList.size() == 1);
assertTrue(targetList.getFirst() == targetId);
private void assertListContainsSingleTarget(LinkedList<Integer> targetList, LinkedList<Integer> targetId) {
assertEquals(1, targetList.size());
assertEquals(targetList.getFirst(), targetId.getFirst());
}
private void assertListEqual(LinkedList<Integer> targetList, LinkedList<Integer> targetId) {
assertEquals(targetList, targetId);
}
private void assertListContainsSingleTarget(LinkedList<Integer> targetList, Integer targetId) {
assertEquals(1, targetList.size());
assertEquals(targetList.getFirst(), targetId);
}
}
\ No newline at end of file
......@@ -3,6 +3,9 @@ package org.vadere.state.attributes.scenario;
import org.vadere.state.attributes.AttributesEmbedShape;
import org.vadere.util.geometry.shapes.VShape;
import java.util.LinkedList;
import java.util.List;
/**
* Attributes of "TargetChanger" object, used by "TargetChangerController" during simulation.
*/
......@@ -28,7 +31,7 @@ public class AttributesTargetChanger extends AttributesEmbedShape {
* is heading to the given target as new target. Otherwise, use the given target
* id as "normal" target id.
*/
private int nextTarget = ID_NOT_SET;
private LinkedList<Integer> nextTarget = new LinkedList<>();
/**
* Change target of a given pedestrian only with a certain probability between
* 0 and 1.
......@@ -48,7 +51,7 @@ public class AttributesTargetChanger extends AttributesEmbedShape {
this.id = id;
}
public AttributesTargetChanger(final VShape shape, final int id, double reachDistance, int nextTarget, double probabilityToChangeTarget) {
public AttributesTargetChanger(final VShape shape, final int id, double reachDistance, LinkedList<Integer> nextTarget, double probabilityToChangeTarget) {
this.shape = shape;
this.id = id;
this.reachDistance = reachDistance;
......@@ -78,7 +81,7 @@ public class AttributesTargetChanger extends AttributesEmbedShape {
return nextTargetIsPedestrian;
}
public int getNextTarget() {
public LinkedList<Integer> getNextTarget() {
return nextTarget;
}
......@@ -106,7 +109,7 @@ public class AttributesTargetChanger extends AttributesEmbedShape {
this.nextTargetIsPedestrian = nextTargetIsPedestrian;
}
public void setNextTarget(int nextTarget) {
public void setNextTarget(LinkedList<Integer> nextTarget) {
this.nextTarget = nextTarget;
}
......
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