Commit 31bb06fa authored by Christina's avatar Christina
Browse files

Merge remote-tracking branch 'origin/add_knowledge_base' into target_changer_prob_list

parents fbbdbcd4 112cf7da
Pipeline #239429 failed with stages
in 5 minutes and 53 seconds
...@@ -655,4 +655,8 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp ...@@ -655,4 +655,8 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
System.out.println(res.toString()); System.out.println(res.toString());
} }
@Override
public void personapi_setInformation(String[] args) throws IOException {
}
} }
...@@ -13,8 +13,10 @@ import org.vadere.manager.traci.commandHandler.variables.PersonVar; ...@@ -13,8 +13,10 @@ import org.vadere.manager.traci.commandHandler.variables.PersonVar;
import org.vadere.manager.traci.commands.TraCICommand; import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.commands.TraCIGetCommand; import org.vadere.manager.traci.commands.TraCIGetCommand;
import org.vadere.manager.traci.commands.TraCISetCommand; import org.vadere.manager.traci.commands.TraCISetCommand;
import org.vadere.manager.traci.compound.CompoundObject;
import org.vadere.manager.traci.response.TraCIGetResponse; import org.vadere.manager.traci.response.TraCIGetResponse;
import org.vadere.simulator.control.simulation.SimulationState; import org.vadere.simulator.control.simulation.SimulationState;
import org.vadere.state.psychology.perception.types.InformationStimulus;
import org.vadere.state.scenario.Pedestrian; import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.util.StateJsonConverter; import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.geometry.Vector3D; import org.vadere.util.geometry.Vector3D;
...@@ -392,6 +394,26 @@ public class PersonCommandHandler extends CommandHandler<PersonVar> { ...@@ -392,6 +394,26 @@ public class PersonCommandHandler extends CommandHandler<PersonVar> {
return cmd; return cmd;
} }
@PersonHandler(cmd = TraCICmd.SET_PERSON_STATE, var = PersonVar.STIMULUS, name = "setInformation")
public TraCICommand process_setStimulus(TraCISetCommand cmd, RemoteManager remoteManager) {
CompoundObject data = (CompoundObject) cmd.getVariableValue();
double start_t = (double)data.getData(0, TraCIDataType.DOUBLE);
double obsolete_at = (double)data.getData(1, TraCIDataType.DOUBLE);
String information = (String)data.getData(2, TraCIDataType.STRING);
// LinkedList<Integer> data = tmp.stream().map(Integer::parseInt).collect(Collectors.toCollection(LinkedList::new));
remoteManager.accessState((manager, state) -> {
Pedestrian ped = state.getTopography().getPedestrianDynamicElements()
.getElement(Integer.parseInt(cmd.getElementId()));
if (checkIfPedestrianExists(ped, cmd)) {
InformationStimulus s = new InformationStimulus(start_t, obsolete_at, information);
ped.getKnowledgeBase().add_information(s);
cmd.setOK();
}
});
return cmd;
}
@PersonHandler(cmd = TraCICmd.SET_PERSON_STATE, var = PersonVar.TARGET_LIST, name = "setTargetList", dataTypeStr = "ArrayList<String>") @PersonHandler(cmd = TraCICmd.SET_PERSON_STATE, var = PersonVar.TARGET_LIST, name = "setTargetList", dataTypeStr = "ArrayList<String>")
public TraCICommand process_setTargetList(TraCISetCommand cmd, RemoteManager remoteManager) { public TraCICommand process_setTargetList(TraCISetCommand cmd, RemoteManager remoteManager) {
List<String> tmp = (List<String>) cmd.getVariableValue(); List<String> tmp = (List<String>) cmd.getVariableValue();
......
...@@ -36,6 +36,7 @@ public enum PersonVar { ...@@ -36,6 +36,7 @@ public enum PersonVar {
ADD(0x80, TraCIDataType.STRING), ADD(0x80, TraCIDataType.STRING),
REMOVE_STAGE(0xc5, TraCIDataType.INTEGER), // set REMOVE_STAGE(0xc5, TraCIDataType.INTEGER), // set
TARGET_LIST(0xfe, TraCIDataType.STRING_LIST), // get, set TARGET_LIST(0xfe, TraCIDataType.STRING_LIST), // get, set
STIMULUS(0xfd, TraCIDataType.COMPOUND_OBJECT)
; ;
......
...@@ -18,8 +18,8 @@ import java.util.Collection; ...@@ -18,8 +18,8 @@ import java.util.Collection;
@DataProcessorClass(label = "MeshDensityCountingProcessor") @DataProcessorClass(label = "MeshDensityCountingProcessor")
public class MeshDensityCountingProcessor extends DataProcessor<TimestepFaceIdKey, Integer> { public class MeshDensityCountingProcessor extends DataProcessor<TimestepFaceIdKey, Integer> {
private final static String propertyNameNumberOfPedestrians = "numberOfPedestrians"; protected final static String propertyNameNumberOfPedestrians = "numberOfPedestrians";
private MeshProcessor meshProcessor; protected MeshProcessor meshProcessor;
public MeshDensityCountingProcessor() { public MeshDensityCountingProcessor() {
super("meshDensityCounting"); super("meshDensityCounting");
...@@ -32,30 +32,19 @@ public class MeshDensityCountingProcessor extends DataProcessor<TimestepFaceIdKe ...@@ -32,30 +32,19 @@ public class MeshDensityCountingProcessor extends DataProcessor<TimestepFaceIdKe
this.meshProcessor = (MeshProcessor) manager.getProcessor(getAttributes().getMeshProcessorId()); this.meshProcessor = (MeshProcessor) manager.getProcessor(getAttributes().getMeshProcessorId());
} }
private IMesh<PVertex, PHalfEdge, PFace> getMesh() { protected IMesh<PVertex, PHalfEdge, PFace> getMesh() {
return meshProcessor.getTriangulation().getMesh(); return meshProcessor.getTriangulation().getMesh();
} }
private MeasurementArea getMeasurementArea() { protected MeasurementArea getMeasurementArea() {
return meshProcessor.getMeasurementArea(); return meshProcessor.getMeasurementArea();
} }
private IIncrementalTriangulation<PVertex, PHalfEdge, PFace> getTriangulation() { protected IIncrementalTriangulation<PVertex, PHalfEdge, PFace> getTriangulation() {
return meshProcessor.getTriangulation(); return meshProcessor.getTriangulation();
} }
@Override protected void doUpdateOnPed(Pedestrian ped){
protected void doUpdate(SimulationState state) {
Collection<Pedestrian> peds = state.getTopography().getElements(Pedestrian.class);
// reset count
for(PFace f : getMesh().getFaces()) {
getMesh().setIntegerData(f, propertyNameNumberOfPedestrians, 0);
}
// compute count
for(Pedestrian ped : peds) {
if(getMeasurementArea().asPolygon().contains(ped.getPosition())) { if(getMeasurementArea().asPolygon().contains(ped.getPosition())) {
PFace f = getTriangulation().locate(ped.getPosition(), ped).get(); PFace f = getTriangulation().locate(ped.getPosition(), ped).get();
int n = getMesh().getIntegerData(f, propertyNameNumberOfPedestrians) + 1; int n = getMesh().getIntegerData(f, propertyNameNumberOfPedestrians) + 1;
...@@ -64,6 +53,14 @@ public class MeshDensityCountingProcessor extends DataProcessor<TimestepFaceIdKe ...@@ -64,6 +53,14 @@ public class MeshDensityCountingProcessor extends DataProcessor<TimestepFaceIdKe
} }
} }
protected void reset_count(){
// reset count
for(PFace f : getMesh().getFaces()) {
getMesh().setIntegerData(f, propertyNameNumberOfPedestrians, 0);
}
}
protected void write_count(SimulationState state){
// write count // write count
int faceId = 1; int faceId = 1;
for(PFace f : getMesh().getFaces()) { for(PFace f : getMesh().getFaces()) {
...@@ -73,6 +70,20 @@ public class MeshDensityCountingProcessor extends DataProcessor<TimestepFaceIdKe ...@@ -73,6 +70,20 @@ public class MeshDensityCountingProcessor extends DataProcessor<TimestepFaceIdKe
} }
} }
@Override
protected void doUpdate(SimulationState state) {
Collection<Pedestrian> peds = state.getTopography().getElements(Pedestrian.class);
reset_count();
// compute count
for(Pedestrian ped : peds) {
doUpdateOnPed(ped);
}
write_count(state);
}
@Override @Override
public AttributesMeshDensityCountingProcessor getAttributes() { public AttributesMeshDensityCountingProcessor getAttributes() {
if(super.getAttributes() == null) { if(super.getAttributes() == null) {
......
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.annotation.factories.dataprocessors.DataProcessorClass;
import org.vadere.simulator.control.simulation.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.state.attributes.processor.AttributesMeshPedStimulusCountingProcessor;
import org.vadere.state.scenario.Pedestrian;
import java.util.Collection;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@DataProcessorClass(label = "MeshPedStimulusCountingProcessor")
public class MeshPedStimulusCountingProcessor extends MeshDensityCountingProcessor {
private Predicate<Pedestrian> filter_by_stimuli;
private Pattern filter_pattern = null;
public MeshPedStimulusCountingProcessor(){
setAttributes(new AttributesMeshPedStimulusCountingProcessor());
}
@Override
public void init(ProcessorManager manager) {
super.init(manager);
// setup filter
if (getAttributes().isRegexFilter()){
filter_pattern = Pattern.compile(getAttributes().getInformationFilter());
filter_by_stimuli = ped -> ped.getKnowledgeBase().knows_about(filter_pattern);
} else {
filter_by_stimuli = ped -> ped.getKnowledgeBase().knows_about(getAttributes().getInformationFilter());
}
}
@Override
protected void doUpdate(SimulationState state) {
Collection<Pedestrian> peds = state.getTopography().getElements(Pedestrian.class);
reset_count();
// compute count
for(Pedestrian ped : peds) {
// update knowledgeBase
ped.getKnowledgeBase().update_obsolete(state.getSimTimeInSec());
// filter by knowledge of pedestrian
if (filter_by_stimuli.test(ped)) {
doUpdateOnPed(ped);
}
}
write_count(state);
}
@Override
public AttributesMeshPedStimulusCountingProcessor getAttributes() {
if(super.getAttributes() == null) {
setAttributes(new AttributesMeshPedStimulusCountingProcessor());
}
return (AttributesMeshPedStimulusCountingProcessor)super.getAttributes();
}
}
package org.vadere.state.attributes.processor;
import java.util.ArrayList;
public class AttributesMeshPedStimulusCountingProcessor extends AttributesMeshDensityCountingProcessor {
private String informationFilter = "";
private boolean isRegexFilter = false;
public String getInformationFilter() {
return informationFilter;
}
public void setInformationFilter(String informationFilter) {
checkSealed();
this.informationFilter = informationFilter;
}
public boolean isRegexFilter() {
return isRegexFilter;
}
public void setRegexFilter(boolean regexFilter) {
checkSealed();
isRegexFilter = regexFilter;
}
}
package org.vadere.state.psychology;
import org.vadere.state.psychology.perception.types.InformationStimulus;
import org.vadere.state.psychology.perception.types.Stimulus;
import java.util.ArrayList;
import java.util.regex.Pattern;
public class KnowledgeBase {
private ArrayList<InformationStimulus> knowledge;
public KnowledgeBase() {
this.knowledge = new ArrayList<>();
}
/**
* remove Information no longer usable at given time.
*/
public void update_obsolete(double current_time){
knowledge.removeIf(i-> i.getObsolete_at() > 0 && current_time > i.getObsolete_at());
}
/**
* True if KnowledgeBase contains this information.
*/
public boolean knows_about(String information){
return knowledge.stream().anyMatch(i->i.getInformation().equals(information));
}
public boolean knows_about(Pattern information){
return knowledge.stream().anyMatch(i->information.matcher(i.getInformation()).matches());
}
public boolean knows_about(String information, Class<? extends Stimulus> clz){
return knowledge.stream().anyMatch(i->i.getInformation().equals(information));
}
public void add_information(InformationStimulus info){
knowledge.add(info);
}
public void remove_information(String info){
knowledge.removeIf(i->i.getInformation().equals(info));
}
}
...@@ -12,17 +12,19 @@ public class PsychologyStatus { ...@@ -12,17 +12,19 @@ public class PsychologyStatus {
private ThreatMemory threatMemory; private ThreatMemory threatMemory;
private SelfCategory selfCategory; private SelfCategory selfCategory;
private GroupMembership groupMembership; private GroupMembership groupMembership;
private KnowledgeBase knowledgeBase;
// Constructors // Constructors
public PsychologyStatus() { public PsychologyStatus() {
this(null, new ThreatMemory(), SelfCategory.TARGET_ORIENTED, GroupMembership.OUT_GROUP); this(null, new ThreatMemory(), SelfCategory.TARGET_ORIENTED, GroupMembership.OUT_GROUP, new KnowledgeBase());
} }
public PsychologyStatus(Stimulus mostImportantStimulus, ThreatMemory threatMemory, SelfCategory selfCategory, GroupMembership groupMembership) { public PsychologyStatus(Stimulus mostImportantStimulus, ThreatMemory threatMemory, SelfCategory selfCategory, GroupMembership groupMembership, KnowledgeBase knowledgeBase) {
this.mostImportantStimulus = mostImportantStimulus; this.mostImportantStimulus = mostImportantStimulus;
this.threatMemory = threatMemory; this.threatMemory = threatMemory;
this.selfCategory = selfCategory; this.selfCategory = selfCategory;
this.groupMembership = groupMembership; this.groupMembership = groupMembership;
this.knowledgeBase = knowledgeBase;
} }
public PsychologyStatus(PsychologyStatus other) { public PsychologyStatus(PsychologyStatus other) {
...@@ -30,6 +32,7 @@ public class PsychologyStatus { ...@@ -30,6 +32,7 @@ public class PsychologyStatus {
this.threatMemory = other.getThreatMemory() != null ? other.getThreatMemory().clone() : null; this.threatMemory = other.getThreatMemory() != null ? other.getThreatMemory().clone() : null;
this.selfCategory = other.getSelfCategory(); this.selfCategory = other.getSelfCategory();
this.groupMembership = other.getGroupMembership(); this.groupMembership = other.getGroupMembership();
this.knowledgeBase = other.getKnowledgeBase();
} }
// Getter // Getter
...@@ -37,6 +40,7 @@ public class PsychologyStatus { ...@@ -37,6 +40,7 @@ public class PsychologyStatus {
public ThreatMemory getThreatMemory() { return threatMemory; } public ThreatMemory getThreatMemory() { return threatMemory; }
public SelfCategory getSelfCategory() { return selfCategory; } public SelfCategory getSelfCategory() { return selfCategory; }
public GroupMembership getGroupMembership() { return groupMembership; } public GroupMembership getGroupMembership() { return groupMembership; }
public KnowledgeBase getKnowledgeBase() { return knowledgeBase; }
// Setter // Setter
public void setMostImportantStimulus(Stimulus mostImportantStimulus) { public void setMostImportantStimulus(Stimulus mostImportantStimulus) {
......
package org.vadere.state.psychology.perception.types;
/**
* Class encodes some kind of information a pedestrian knows about.
* The information is active from the time gien in {@link #time} and will be
* forgotten at {@link #obsolete_at} (or never if {@link #obsolete_at} == -1
*/
public class InformationStimulus extends Stimulus {
private String information;
private double obsolete_at;
public InformationStimulus(String information) {
super(0.0);
this.information = information;
this.obsolete_at = -1; // never
}
public InformationStimulus(double time, double obsolete_at, String information) {
super(time);
this.information = information;
this.obsolete_at = obsolete_at;
}
public InformationStimulus(InformationStimulus other) {
super(other.time);
this.information = other.information;
this.obsolete_at = other.obsolete_at;
}
public String getInformation() {
return information;
}
public double getObsolete_at() {
return obsolete_at;
}
@Override
public Stimulus clone() {
return new InformationStimulus(this);
}
}
...@@ -36,7 +36,7 @@ import java.util.List; ...@@ -36,7 +36,7 @@ import java.util.List;
@Type(value = Wait.class, name = "Wait"), @Type(value = Wait.class, name = "Wait"),
@Type(value = WaitInArea.class, name = "WaitInArea"), @Type(value = WaitInArea.class, name = "WaitInArea"),
@Type(value = ChangeTarget.class, name = "ChangeTarget"), @Type(value = ChangeTarget.class, name = "ChangeTarget"),
@Type(value = ChangeTargetScripted.class, name = "ChangeTargetScripted") @Type(value = InformationStimulus.class, name = "InformationAvailable")
}) })
// "time" is set when the stimulus is injected into the simulation run and must not be de-/serialized. // "time" is set when the stimulus is injected into the simulation run and must not be de-/serialized.
@JsonIgnoreProperties({ "time" }) @JsonIgnoreProperties({ "time" })
......
package org.vadere.state.scenario; package org.vadere.state.scenario;
import org.vadere.state.attributes.scenario.AttributesAgent; import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.psychology.KnowledgeBase;
import org.vadere.state.psychology.PsychologyStatus; import org.vadere.state.psychology.PsychologyStatus;
import org.vadere.state.psychology.cognition.GroupMembership; import org.vadere.state.psychology.cognition.GroupMembership;
import org.vadere.state.psychology.cognition.SelfCategory; import org.vadere.state.psychology.cognition.SelfCategory;
...@@ -66,7 +67,7 @@ public class Pedestrian extends Agent { ...@@ -66,7 +67,7 @@ public class Pedestrian extends Agent {
idAsTarget = -1; idAsTarget = -1;
isChild = false; isChild = false;
isLikelyInjured = false; isLikelyInjured = false;
psychologyStatus = new PsychologyStatus(null, new ThreatMemory(), SelfCategory.TARGET_ORIENTED, GroupMembership.OUT_GROUP); psychologyStatus = new PsychologyStatus(null, new ThreatMemory(), SelfCategory.TARGET_ORIENTED, GroupMembership.OUT_GROUP, new KnowledgeBase());
groupIds = new LinkedList<>(); groupIds = new LinkedList<>();
groupSizes = new LinkedList<>(); groupSizes = new LinkedList<>();
modelPedestrianMap = new HashMap<>(); modelPedestrianMap = new HashMap<>();
...@@ -110,6 +111,7 @@ public class Pedestrian extends Agent { ...@@ -110,6 +111,7 @@ public class Pedestrian extends Agent {
public ThreatMemory getThreatMemory() { return psychologyStatus.getThreatMemory(); } public ThreatMemory getThreatMemory() { return psychologyStatus.getThreatMemory(); }
public SelfCategory getSelfCategory() { return psychologyStatus.getSelfCategory(); } public SelfCategory getSelfCategory() { return psychologyStatus.getSelfCategory(); }
public GroupMembership getGroupMembership() { return psychologyStatus.getGroupMembership(); } public GroupMembership getGroupMembership() { return psychologyStatus.getGroupMembership(); }
public KnowledgeBase getKnowledgeBase() {return psychologyStatus.getKnowledgeBase(); }
public LinkedList<Integer> getGroupIds() { return groupIds; } public LinkedList<Integer> getGroupIds() { return groupIds; }
public LinkedList<Integer> getGroupSizes() { public LinkedList<Integer> getGroupSizes() {
return groupSizes; return groupSizes;
......
...@@ -242,6 +242,12 @@ ...@@ -242,6 +242,12 @@
<artifactId>py4j</artifactId> <artifactId>py4j</artifactId>
<version>0.10.8</version> <version>0.10.8</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.github.davidmoten/rtree -->
<dependency>
<groupId>com.github.davidmoten</groupId>
<artifactId>rtree</artifactId>
<version>0.8.7</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
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