Skip to content
Snippets Groups Projects
Commit 89a7a468 authored by BrunoDatoMeneses's avatar BrunoDatoMeneses
Browse files

ADD: context neighbours detection with on click viewer

parent 20a1a65e
Branches
No related tags found
1 merge request!1Merge Master
...@@ -131,7 +131,7 @@ public class AMOEBA_UI { ...@@ -131,7 +131,7 @@ public class AMOEBA_UI {
context.getNearestNeighbours(); context.getNearestNeighbours();
} }
//displayContextInfo(usedAmoeba); displayContextInfo(usedAmoeba);
} }
...@@ -147,8 +147,12 @@ public class AMOEBA_UI { ...@@ -147,8 +147,12 @@ public class AMOEBA_UI {
} }
System.out.println(percept.getName()+" overlaps "+percept.perceptOverlaps.size()); System.out.println(percept.getName()+" overlaps "+percept.perceptOverlaps.size());
percept.displaySortedRanges(); for(String key : percept.perceptOverlaps.keySet()) {
percept.displaySortedRangesTreeSet(); System.out.println(percept.perceptOverlaps.get(key));
}
//percept.displaySortedRanges();
//percept.displaySortedRangesTreeSet();
} }
} }
...@@ -160,6 +164,22 @@ public class AMOEBA_UI { ...@@ -160,6 +164,22 @@ public class AMOEBA_UI {
System.out.println("********************************** CONTEXT **********************************"); System.out.println("********************************** CONTEXT **********************************");
System.out.println(context.getName()+" neighbours : "+context.neigbours.size()); System.out.println(context.getName()+" neighbours : "+context.neigbours.size());
System.out.println("*************************** OVERLAPS ***************************");
for(Context ctxt : context.contextOverlapsByPercept.keySet()) {
System.out.print(ctxt.getName() + " --> ");
for(Percept percept : getAllPercepts(usedAmoeba)) {
System.out.print(percept.getName() + "(" + context.contextOverlapsByPercept.get(ctxt).get(percept) + ") ");
}
System.out.println("");
}
System.out.println("*************************** NEAREST NEIGHBOURS ***************************"); System.out.println("*************************** NEAREST NEIGHBOURS ***************************");
for(Percept percept : getAllPercepts(usedAmoeba)) { for(Percept percept : getAllPercepts(usedAmoeba)) {
System.out.print(percept.getName()); System.out.print(percept.getName());
......
...@@ -47,7 +47,7 @@ public class BadContextLauncherEasy implements Serializable { ...@@ -47,7 +47,7 @@ public class BadContextLauncherEasy implements Serializable {
bcm.setWorld(amoeba.getScheduler().getWorld()); bcm.setWorld(amoeba.getScheduler().getWorld());
ArrayList<Percept> percepts = new ArrayList<Percept>(); ArrayList<Percept> percepts = new ArrayList<Percept>();
for (int i = 0 ; i < 10 ; i++) { for (int i = 0 ; i < 20 ; i++) {
/* This is the studied system part. Feel free to use any data source.*/ /* This is the studied system part. Feel free to use any data source.*/
bcm.playOneStep(0); bcm.playOneStep(0);
...@@ -57,7 +57,7 @@ public class BadContextLauncherEasy implements Serializable { ...@@ -57,7 +57,7 @@ public class BadContextLauncherEasy implements Serializable {
percepts = amoeba.getScheduler().getWorld().getAllPercept(); percepts = amoeba.getScheduler().getWorld().getAllPercept();
try try
{ {
Thread.sleep(1000); Thread.sleep(500);
} }
catch(InterruptedException ex) catch(InterruptedException ex)
{ {
......
...@@ -51,8 +51,9 @@ public class BadContextManager implements StudiedSystem, Serializable{ ...@@ -51,8 +51,9 @@ public class BadContextManager implements StudiedSystem, Serializable{
@Override @Override
public HashMap<String, Double> getOutput() { public HashMap<String, Double> getOutput() {
HashMap<String, Double> out = new HashMap<String, Double>(); HashMap<String, Double> out = new HashMap<String, Double>();
double size = 55;
result = (y > -55 && y < 55 && x < 55 && x > -55) ? 0.0 : 1.0; result = (y > -size && y < size && x < size && x > -size) ? 0.0 : 1.0;
// result = (x > 2*y) ? 0.0 : 1.0; // result = (x > 2*y) ? 0.0 : 1.0;
out.put("x",x); out.put("x",x);
......
...@@ -60,6 +60,11 @@ public class Context extends AbstractContext implements Serializable{ ...@@ -60,6 +60,11 @@ public class Context extends AbstractContext implements Serializable{
public HashMap<Context,String> neigbours = new HashMap<Context,String>(); public HashMap<Context,String> neigbours = new HashMap<Context,String>();
public HashMap<Percept , HashMap<String, Context>> nearestNeighbours; public HashMap<Percept , HashMap<String, Context>> nearestNeighbours;
public HashMap<Percept , HashMap<String, ArrayList<Context>>> sortedPossibleNeighbours = new HashMap<Percept , HashMap<String, ArrayList<Context>>>();
public ArrayList<Context> possibleNeighbours = new ArrayList<Context>();
public ArrayList<Context> neighbours = new ArrayList<Context>();
/** /**
...@@ -120,8 +125,16 @@ public class Context extends AbstractContext implements Serializable{ ...@@ -120,8 +125,16 @@ public class Context extends AbstractContext implements Serializable{
nearestNeighbours = new HashMap<Percept , HashMap<String, Context>>(); nearestNeighbours = new HashMap<Percept , HashMap<String, Context>>();
for(Percept p : ranges.keySet()) { for(Percept p : ranges.keySet()) {
nearestNeighbours.put(p, new HashMap<String, Context>()); nearestNeighbours.put(p, new HashMap<String, Context>());
sortedPossibleNeighbours.put(p, new HashMap<String, ArrayList<Context>>());
nearestNeighbours.get(p).put("start", null); nearestNeighbours.get(p).put("start", null);
nearestNeighbours.get(p).put("end", null); nearestNeighbours.get(p).put("end", null);
sortedPossibleNeighbours.get(p).put("start", new ArrayList<Context>() );
sortedPossibleNeighbours.get(p).put("end", new ArrayList<Context>() );
} }
neigbours = new HashMap<Context,String>(); neigbours = new HashMap<Context,String>();
...@@ -586,6 +599,52 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing ...@@ -586,6 +599,52 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing
s += "Context : " + getName() + "\n"; s += "Context : " + getName() + "\n";
for (Percept v : ranges.keySet()) { for (Percept v : ranges.keySet()) {
s += v.getName() + " : " + ranges.get(v).toString() + "\n"; s += v.getName() + " : " + ranges.get(v).toString() + "\n";
s += "Neighbours : \n";
if(nearestNeighbours.get(v).get("start") != null) {
s+= "START :" + nearestNeighbours.get(v).get("start").getName() + "\n";
}
else {
s+= "START : \n";
}
s += "Sorted start possible neighbours :\n";
if(sortedPossibleNeighbours.get(v).get("start").size()>0) {
for(Context ctxt : sortedPossibleNeighbours.get(v).get("start")) {
if(ctxt.equals(this)) {
s += "# " + ctxt.getName() + " --> " + ctxt.getRanges().get(v).getStart() + "\n";
}
else {
s += ctxt.getName() + " ---> " + ctxt.getRanges().get(v).getStart() + "\n";
}
}
}
s += "Sorted end possible neighbours :\n";
if(sortedPossibleNeighbours.get(v).get("end").size()>0) {
for(Context ctxt : sortedPossibleNeighbours.get(v).get("start")) {
if(ctxt.equals(this)) {
s += "# " +ctxt.getName()+ " --> " + ctxt.getRanges().get(v).getEnd() + "\n";
}
else {
s += ctxt.getName() + " ---> " + ctxt.getRanges().get(v).getEnd() + "\n";
}
}
}
if(nearestNeighbours.get(v).get("end") != null) {
s+= "END :" + nearestNeighbours.get(v).get("end").getName() + "\n";
}
else {
s+= "END : \n";
}
} }
s += "Number of activations : " + activations + "\n"; s += "Number of activations : " + activations + "\n";
if (actionProposition != null) { if (actionProposition != null) {
...@@ -600,6 +659,11 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing ...@@ -600,6 +659,11 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing
} else { } else {
s += "Local model : " + localModel.getFormula(this) + "\n"; s += "Local model : " + localModel.getFormula(this) + "\n";
} }
s += "Possible neighbours : \n";
for(Context ctxt : possibleNeighbours) {
s += ctxt.getName() + "\n";
}
return s; return s;
} }
...@@ -926,6 +990,10 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing ...@@ -926,6 +990,10 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing
} }
if(contextOverlapedInOtherPerceptsTest) { if(contextOverlapedInOtherPerceptsTest) {
contextOverlapedInOtherPercepts.add(ctxt); contextOverlapedInOtherPercepts.add(ctxt);
if(!possibleNeighbours.contains(ctxt)) {
possibleNeighbours.add(ctxt);
}
} }
} }
...@@ -939,17 +1007,23 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing ...@@ -939,17 +1007,23 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing
public void getNearestNeighbours(){ public void getNearestNeighbours(){
HashMap<Percept, HashMap<String , ArrayList<Context>>> sortedPossibleNeigbours = new HashMap<Percept, HashMap<String , ArrayList<Context>>>(); HashMap<Percept, HashMap<String , ArrayList<Context>>> localSortedPossibleNeigbours = new HashMap<Percept, HashMap<String , ArrayList<Context>>>();
for(Percept p : ranges.keySet()) { for(Percept p : ranges.keySet()) {
sortedPossibleNeigbours.put(p,getSortedPossibleNeigbours(p)); localSortedPossibleNeigbours.put(p,getSortedPossibleNeigbours(p));
sortedPossibleNeigbours.get(p).get("start").add(this); localSortedPossibleNeigbours.get(p).get("start").add(this);
sortedPossibleNeigbours.get(p).get("end").add(this); localSortedPossibleNeigbours.get(p).get("end").add(this);
Collections.sort(localSortedPossibleNeigbours.get(p).get("start"), p.customRangeComparators.get("start"));
Collections.sort(localSortedPossibleNeigbours.get(p).get("end"), p.customRangeComparators.get("end"));
sortedPossibleNeighbours.get(p).put("start", localSortedPossibleNeigbours.get(p).get("start"));
sortedPossibleNeighbours.get(p).put("end", localSortedPossibleNeigbours.get(p).get("end"));
Collections.sort(sortedPossibleNeigbours.get(p).get("start"), p.customRangeComparators.get("start"));
Collections.sort(sortedPossibleNeigbours.get(p).get("end"), p.customRangeComparators.get("end"));
} }
...@@ -957,45 +1031,70 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing ...@@ -957,45 +1031,70 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing
for(Percept p : ranges.keySet()) { for(Percept p : ranges.keySet()) {
Context startNeighbour = getNearestContextBySortedPerceptAndRange(localSortedPossibleNeigbours.get(p), p, "start");
Context endNeighbour = getNearestContextBySortedPerceptAndRange(localSortedPossibleNeigbours.get(p), p, "end");
nearestNeighbours.get(p).put("end", startNeighbour);
nearestNeighbours.get(p).put("start", endNeighbour);
nearestNeighbours.get(p).put("end", getNearestContextBySortedPerceptAndRange(sortedPossibleNeigbours.get(p), p, "start")); neighbours.add(startNeighbour);
nearestNeighbours.get(p).put("start", getNearestContextBySortedPerceptAndRange(sortedPossibleNeigbours.get(p), p, "end")); neighbours.add(endNeighbour);
} }
} }
public Context getNearestContextBySortedPerceptAndRange(HashMap<String , ArrayList<Context>> sortedContextbyRange, Percept percept, String range) { public Context getNearestContextBySortedPerceptAndRange(HashMap<String , ArrayList<Context>> sortedPossibleNeigbours, Percept percept, String range) {
int indexOfCurrentContext;
int i;
if(range.equals("start")) {
i=0; int indexOfCurrentContext = sortedPossibleNeigbours.get(range).indexOf(this);
while(sortedContextbyRange.get("start").get(i).getRanges().get(percept).getStart() < this.getRanges().get(percept).getStart() && (i<sortedContextbyRange.get("start").size())) {
i+=1; if(sortedPossibleNeigbours.get(range).size()>1) {
}
if(i==sortedContextbyRange.get("start").size()) { if((indexOfCurrentContext > 0) && ( indexOfCurrentContext < sortedPossibleNeigbours.get(range).size()-1)) {
return null; if(range.equals("start")) {
return sortedPossibleNeigbours.get(range).get(indexOfCurrentContext+1);
}
else if(range.equals("end")) {
return sortedPossibleNeigbours.get(range).get(indexOfCurrentContext-1);
}
else {
return null;
}
} }
else {
return sortedContextbyRange.get("start").get(i); else if(indexOfCurrentContext == 0 ) {
} if(range.equals("start")) {
} return sortedPossibleNeigbours.get(range).get(indexOfCurrentContext+1);
else if(range.equals("end")) { }
i=sortedContextbyRange.get("end").size()-1; else {
while(sortedContextbyRange.get("end").get(i).getRanges().get(percept).getEnd() > this.getRanges().get(percept).getEnd() && (i>0)) { return null;
i-=1; }
} }
if(i==0) {
return null; else if( indexOfCurrentContext == sortedPossibleNeigbours.get(range).size()-1) {
if(range.equals("end")) {
return sortedPossibleNeigbours.get(range).get(indexOfCurrentContext-1);
}
else {
return null;
}
} }
else { else {
return sortedContextbyRange.get("end").get(i); return null;
} }
} }
else { else {
return null; return null;
} }
} }
......
...@@ -521,8 +521,8 @@ public class Percept extends SystemAgent implements Serializable { ...@@ -521,8 +521,8 @@ public class Percept extends SystemAgent implements Serializable {
Collections.sort(sortedContextbyStartRanges, rangeStartComparator); Collections.sort(sortedContextbyStartRanges, rangeStartComparator);
displaySortedRanges(); //displaySortedRanges();
displaySortedRangesTreeSet(); //displaySortedRangesTreeSet();
//System.out.println("----------------------AUTO PRINT"); //System.out.println("----------------------AUTO PRINT");
//System.out.println(sortedEndRanges.size()+ " " + sortedStartRanges); //System.out.println(sortedEndRanges.size()+ " " + sortedStartRanges);
...@@ -626,7 +626,7 @@ public class Percept extends SystemAgent implements Serializable { ...@@ -626,7 +626,7 @@ public class Percept extends SystemAgent implements Serializable {
double contextEnd1 = getEndRangeProjection(context1); double contextEnd1 = getEndRangeProjection(context1);
double contextEnd2 = getEndRangeProjection(context2); double contextEnd2 = getEndRangeProjection(context2);
return ( (contextStart1< contextStart2 && contextStart2 <contextEnd1) || ((contextStart1< contextEnd2 && contextEnd2 <contextEnd1)) ) ; return ( (contextStart1< contextStart2 && contextStart2 <contextEnd1) || ((contextStart1< contextEnd2 && contextEnd2 <contextEnd1)) ) || ( (contextStart2< contextStart1 && contextStart1 <contextEnd2) || ((contextStart2< contextEnd1 && contextEnd1 <contextEnd2)) ) ;
} }
......
...@@ -27,4 +27,10 @@ public class PerceptOverlap { ...@@ -27,4 +27,10 @@ public class PerceptOverlap {
contexts.add(context2); contexts.add(context2);
return contexts; return contexts;
} }
public String toString() {
String s = "";
s += "{ " + context1.getName() + " , " + context2.getName() + " }";
return s;
}
} }
...@@ -617,5 +617,14 @@ public class Scheduler implements Serializable{ ...@@ -617,5 +617,14 @@ public class Scheduler implements Serializable{
this.contexts = contexts; this.contexts = contexts;
} }
public Context getContextByName(String name) {
for(Agent agt: contexts) {
if(agt.getName().equals(name)) {
return (Context)agt;
}
}
return null;
}
} }
...@@ -1202,6 +1202,7 @@ private void startPanelController() { ...@@ -1202,6 +1202,7 @@ private void startPanelController() {
node.addAttribute("ui.class","ContextColorDynamic"); node.addAttribute("ui.class","ContextColorDynamic");
node.setAttribute("ui.color", (n.getActionProposal() - min) / (max - min) ); node.setAttribute("ui.color", (n.getActionProposal() - min) / (max - min) );
//node.setAttribute("ui.color", 0.5 );
} }
} }
...@@ -1224,6 +1225,55 @@ private void startPanelController() { ...@@ -1224,6 +1225,55 @@ private void startPanelController() {
// } // }
} }
public void highlightContextNeighbours(Context context) {
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (String name : world.getAgents().keySet()) {
SystemAgent a = world.getAgents().get(name);
if (a instanceof Context) {
double val = ((Context) a).getActionProposal();
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
}
}
for (String name : world.getAgents().keySet()) {
SystemAgent a = world.getAgents().get(name);
if (a instanceof Context) {
Context n = (Context)a;
Node node = graph.getNode(name);
node.addAttribute("ui.class","ContextColorDynamic");
node.setAttribute("ui.color", 0.1 );
if(context.possibleNeighbours.contains(n)) {
node.setAttribute("ui.color", 0.5 );
}
if(context.neighbours.contains(n)){
node.setAttribute("ui.color", 1 );
}
if(context.equals(n)) {
node.setAttribute("ui.color", 0.0 );
}
}
}
}
/** /**
...@@ -1274,12 +1324,16 @@ private void startPanelController() { ...@@ -1274,12 +1324,16 @@ private void startPanelController() {
world.getAmoeba().getLogFile().messageToDebug("Push button", 5, new LogMessageType[] {LogMessageType.INFORMATION}); world.getAmoeba().getLogFile().messageToDebug("Push button", 5, new LogMessageType[] {LogMessageType.INFORMATION});
toolBarInfo.setVisible(true); toolBarInfo.setVisible(true);
currentId = id; currentId = id;
Context pushedContext = world.getScheduler().getContextByName(id);
System.out.println("node pushed : " + id); System.out.println("node pushed : " + id);
//String info = world.getAgents().get(id).toString(); //String info = world.getAgents().get(id).toString();
//System.out.println(info); //System.out.println(info);
//JOptionPane.showMessageDialog(this, info, "Context : " + id, JOptionPane.PLAIN_MESSAGE); //JOptionPane.showMessageDialog(this, info, "Context : " + id, JOptionPane.PLAIN_MESSAGE);
if (currentId != null) { if (currentId != null) {
highlightContextNeighbours(world.getScheduler().getContextByName(currentId));
setTextAreaInfo(currentId); setTextAreaInfo(currentId);
if (sliderValue == currentTick || (!rememberState)) { if (sliderValue == currentTick || (!rememberState)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment