Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: prediction by map position #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions src/main/java/com/opennars/applications/crossing/Crossing.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
import com.opennars.applications.crossing.NarListener.Prediction;
import com.opennars.sgui.NarSimpleGUI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.opennars.io.events.Events;
import org.opennars.io.events.OutputHandler.DISAPPOINT;
import org.opennars.main.Nar;
Expand All @@ -36,9 +39,10 @@
public class Crossing extends PApplet {
Nar nar;
int entityID = 1;

List<Prediction> predictions = new ArrayList<Prediction>();

List<Prediction> disappointments = new ArrayList<Prediction>();
Map<Vec2Int, List<Prediction>> predictionsByMapPosition = new HashMap<>();

final int streetWidth = 40;
final int fps = 50;
@Override
Expand All @@ -48,7 +52,7 @@ public void setup() {
nar = new Nar();
nar.narParameters.VOLUME = 0;
nar.narParameters.DURATION*=10;
NarListener listener = new NarListener(cameras.get(0), nar, predictions, disappointments);
NarListener listener = new NarListener(cameras.get(0), nar, predictionsByMapPosition, disappointments);
nar.on(Events.TaskAdd.class, listener);
nar.on(DISAPPOINT.class, listener);
} catch (Exception ex) {
Expand Down Expand Up @@ -123,12 +127,31 @@ public void draw() {
}
t++;
nar.cycles(10);
removeOutdatedPredictions(predictions);
removeOutdatedPredictions(predictionsByMapPosition);
removeOutdatedPredictions(disappointments);
for (Prediction pred : predictions) {
Entity e = pred.ent;
e.draw(this, streets, trafficLights, entities, pred.truth, pred.time - nar.time());

for (final Vec2Int gridPositionOfPrediction : predictionsByMapPosition.keySet()) {
final List<Prediction> predictionsOfCell = predictionsByMapPosition.get(gridPositionOfPrediction);

if (predictionsOfCell.size() == 0) {
continue;
}

Prediction highestPredictionOfCell = predictionsOfCell.get(0);
for (Prediction pred : predictionsByMapPosition.get(gridPositionOfPrediction)) {
double mulThis = Util.truthToValue(pred.truth) * Util.timeToValue(pred.time - nar.time());
double mulHighest = Util.truthToValue(highestPredictionOfCell.truth) * Util.timeToValue(highestPredictionOfCell.time - nar.time());

if (mulThis > mulHighest) {
highestPredictionOfCell = pred;
}
}

Entity e = highestPredictionOfCell.ent;
e.draw(this, streets, trafficLights, entities, highestPredictionOfCell.truth, highestPredictionOfCell.time - nar.time());
}


if(showAnomalies) {
for (Prediction pred : disappointments) {
Entity e = pred.ent;
Expand All @@ -148,13 +171,20 @@ public void draw() {
}

public void removeOutdatedPredictions(List<Prediction> predictions) {
List<Prediction> toDelete = new ArrayList<Prediction>();
for(Prediction pred : predictions) {
if(pred.time <= nar.time()) {
List<Prediction> toDelete = new ArrayList<>();
for (Prediction pred : predictions) {
if (pred.time <= nar.time()) {
toDelete.add(pred);
}
}
predictions.removeAll(toDelete);

}

public void removeOutdatedPredictions(Map<Vec2Int, List<Prediction>> predictionsByMapPosition) {
for (final Vec2Int gridPositionOfPrediction : predictionsByMapPosition.keySet()) {
removeOutdatedPredictions(predictionsByMapPosition.get(gridPositionOfPrediction));
}
}

float mouseScroll = 0;
Expand Down
59 changes: 52 additions & 7 deletions src/main/java/com/opennars/applications/crossing/NarListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.opennars.entity.Sentence;
import org.opennars.entity.Stamp;
import org.opennars.entity.Task;
Expand Down Expand Up @@ -52,13 +54,13 @@ public Prediction(Entity ent, TruthValue truth, long time) {
this.truth = truth;
}
}
List<NarListener.Prediction> predictions;

Map<Vec2Int, List<Prediction>> predictionsByMapPosition;
List<NarListener.Prediction> disappointments;
Nar nar;
Camera camera;
public NarListener(Camera camera, Nar nar, List<NarListener.Prediction> predictions, List<NarListener.Prediction> disappointments) {
this.predictions = predictions;
public NarListener(Camera camera, Nar nar, Map<Vec2Int, List<Prediction>> predictionsByMapPosition, List<NarListener.Prediction> disappointments) {
this.predictionsByMapPosition = predictionsByMapPosition;
this.disappointments = disappointments;
this.nar = nar;
this.camera = camera;
Expand All @@ -82,12 +84,55 @@ public void event(Class event, Object[] args) {
if (event == Events.TaskAdd.class) {
Task t = (Task) args[0];
if (/*t.sentence.getOccurenceTime() > nar.time() && */t.sentence.isJudgment() && t.sentence.getTruth().getExpectation() >= nar.narParameters.DEFAULT_CONFIRMATION_EXPECTATION) {
Prediction result = predictionFromTask(t);
if(result != null) {
predictions.add(result);
predictionFromTaskAndAddToPredictions(t);
}
}
}

public void predictionFromTaskAndAddToPredictions(Task t) {
Prediction prediction = null;
int posX=0, posY=0;

//format: "<(*,car,50_82) --> at>. %0.45;0.26%";
if(t.sentence.term instanceof Inheritance) {
Inheritance positionInh = (Inheritance) t.sentence.term;
if(positionInh.getSubject() instanceof Product) {
Product prod = (Product) positionInh.getSubject();
if(prod.size() == 2) {
Term type = prod.term[0];
String position = prod.term[1].toString();
if(position.contains("_")) {
try {
posX = camera.minX + Util.discretization * Integer.valueOf(position.split("_")[0]);
posY = camera.minY + Util.discretization * Integer.valueOf(position.split("_")[1]);
//int id = 0; //Integer.valueOf(idStr.toString()); often a dep var
Entity pred;
if(type.toString().startsWith(car.toString())) {
String id = type.toString().substring(car.toString().length(), type.toString().length());
pred = new Car(Integer.valueOf(id), posX, posY, 0, 0);
prediction = new Prediction(pred, t.sentence.truth, t.sentence.getOccurenceTime());
}
else
if(type.toString().startsWith(pedestrian.toString())) {
String id = type.toString().substring(pedestrian.toString().length(), type.toString().length());
pred = new Pedestrian(Integer.valueOf(id), posX, posY, 0, 0);
prediction = new Prediction(pred, t.sentence.truth, t.sentence.getOccurenceTime());
}
} catch(Exception ex) {} //wrong format, it's not such a type of prediction but something else
}
}
}
}

if (prediction == null) {
return;
}

if (!predictionsByMapPosition.containsKey(new Vec2Int(posX, posY))) {
predictionsByMapPosition.put(new Vec2Int(posX, posY), new ArrayList<>());
}
List<Prediction> predictions = predictionsByMapPosition.get(new Vec2Int(posX, posY));
predictions.add(prediction);
}

public Prediction predictionFromTask(Task t) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/opennars/applications/crossing/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class Util {

public static Random rnd = new Random();
public static final int discretization =10;
public static final int discretization = 10;

public static double distance(double posX, double posY, double posX2, double posY2) {
double dx = posX - posX2;
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/opennars/applications/crossing/Vec2Int.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* The MIT License
*
* Copyright 2018 The OpenNARS authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.opennars.applications.crossing;

public class Vec2Int {
public int x;
public int y;

public Vec2Int(final int x, final int y) {
this.x = x;
this.y = y;
}

@Override
public int hashCode() {
return x * 5000 + y;
}

@Override
public boolean equals(Object other) {
return other instanceof Vec2Int && x == ((Vec2Int)other).x && y == ((Vec2Int)other).y;
}
}