diff --git a/AMOEBA3/src/MAS/agents/ContextProjection.java b/AMOEBA3/src/MAS/agents/ContextProjection.java new file mode 100644 index 0000000000000000000000000000000000000000..6d6d3fbe0c33f25b9af867eebb6342885ef6e3aa --- /dev/null +++ b/AMOEBA3/src/MAS/agents/ContextProjection.java @@ -0,0 +1,57 @@ +package MAS.agents; + +import java.io.Serializable; + +import MAS.agents.context.Context; +import MAS.kernel.World; + +public class ContextProjection implements Serializable{ + + + private Percept percept; + private Context context; + + + private double start; + private double end; + + + + + public ContextProjection(Percept percept, Context context) { + this.percept = percept; + this.context = context; + this.start = context.getRanges().get(this.percept).getStart(); + this.end = context.getRanges().get(this.percept).getEnd(); + } + + public void setRanges(double start, double end) { + this.start = start; + this.end = end; + } + + public void setRangeStart(double start) { + this.start = start; + } + + public void setRangeEnd(double end) { + this.end = end; + } + + public void update() { + this.start = context.getRanges().get(this.percept).getStart(); + this.end = context.getRanges().get(this.percept).getEnd(); + } + + public String getRanges() { + return "{" + start + " , " + end + "}"; + } + + public boolean contains(Double value) { + return ((value > start) && (value < end)); + } + + public Context getContex() { + return this.context; + } +} diff --git a/AMOEBA3/src/MAS/agents/Percept.java b/AMOEBA3/src/MAS/agents/Percept.java index 1d5484882ec8ab64d8112971270f0e42a74f8141..ce1b5dbb3d848a7dca6b5fd65827d337cd876e6d 100644 --- a/AMOEBA3/src/MAS/agents/Percept.java +++ b/AMOEBA3/src/MAS/agents/Percept.java @@ -2,6 +2,7 @@ package MAS.agents; import java.io.Serializable; import java.util.ArrayList; +import java.util.HashMap; import MAS.kernel.Config; import MAS.kernel.World; @@ -24,6 +25,9 @@ public class Percept extends SystemAgent implements Serializable { protected ArrayList<Agent> targets = new ArrayList<Agent>(); protected ArrayList<Agent> activatedContext = new ArrayList<Agent>(); + public HashMap<Context, ContextProjection> contextProjections = new HashMap<Context, ContextProjection>(); + public ArrayList<Context> validContexteProjection = new ArrayList<Context>(); + private double min = Double.MAX_VALUE; private double max = Double.MIN_VALUE; @@ -99,6 +103,9 @@ public class Percept extends SystemAgent implements Serializable { this.min = p.min; this.max = p.max; this.isEnum = p.isEnum; + + contextProjections = new HashMap<Context, ContextProjection>(); + validContexteProjection = new ArrayList<Context>(); } @@ -111,9 +118,24 @@ public class Percept extends SystemAgent implements Serializable { oldValue = value; value = sensor.getValue(); ajustMinMax(); + computeContextProjectionValidity(); + } + public void computeContextProjectionValidity() { + validContexteProjection = new ArrayList<Context>(); + for(ContextProjection contextProjection : contextProjections.values()) { + if(contextProjection.contains(this.value)) { + validContexteProjection.add(contextProjection.getContex()); + System.out.println("Percept "+this.name+ " Context "+contextProjection.getContex().getName()); + } + } + + for(Context context : validContexteProjection) { + context.setPerceptValidity(this); + } + } /* (non-Javadoc) @@ -294,6 +316,20 @@ public class Percept extends SystemAgent implements Serializable { public void setEnum(boolean isEnum) { this.isEnum = isEnum; } + + + public void addContextProjection(Context context) { + ContextProjection newContextProjection = new ContextProjection(this, context); + contextProjections.put(context, newContextProjection); + } + + public void deleteContextProjection(Context context) { + contextProjections.remove(context); + } + + public void updateContextProjection(Context context) { + contextProjections.get(context).update(); + } } diff --git a/AMOEBA3/src/MAS/agents/context/Context.java b/AMOEBA3/src/MAS/agents/context/Context.java index d8ebffc349f1358a779034f728d0b407e411e69e..52123a4f29d73196dc4ea8e7592a0a75b8767743 100644 --- a/AMOEBA3/src/MAS/agents/context/Context.java +++ b/AMOEBA3/src/MAS/agents/context/Context.java @@ -53,6 +53,8 @@ public class Context extends AbstractContext implements Serializable{ private boolean valid = false; private boolean firstTimePeriod = true; + private HashMap<Percept, Boolean> perceptValidities = new HashMap<Percept, Boolean>(); + /** * The main constructor, used by AMOEBA to build new context agent. @@ -62,6 +64,7 @@ public class Context extends AbstractContext implements Serializable{ public Context(World world, Head head) { super(world); buildContext(head); + } @@ -90,6 +93,8 @@ public class Context extends AbstractContext implements Serializable{ ranges.get(v).setValue(v.getValue()); sendExpressMessage(null, MessageType.REGISTER, v); firstPoint.addDimension(v, v.getValue()); + + v.addContextProjection(this); } localModel = this.world.buildLocalModel(this); firstPoint.setProposition(this.controller.getOracleValue()); @@ -98,6 +103,11 @@ public class Context extends AbstractContext implements Serializable{ this.world.getScheduler().addAlteredContext(this); this.setName(String.valueOf(this.hashCode())); this.world.startAgent(this); + + perceptValidities = new HashMap<Percept, Boolean>(); + for(Percept percept : var) { + perceptValidities.put(percept, false); + } } /** @@ -228,15 +238,23 @@ public class Context extends AbstractContext implements Serializable{ public void play() { super.play(); + if(computeValidityByPercepts()) { + System.out.println("Valid context by Percepts "+this.name); + } + if (computeValidity()) { sendMessage(getActionProposal(), MessageType.PROPOSAL, controller); Config.print("Message envoyé", 4); + System.out.println("Valid context by Context "+this.name); } this.activations = 0; this.valid = false; - + // Reset percepts validities + for(Percept percept : perceptValidities.keySet()) { + perceptValidities.put(percept, false); + } } //--------------------------------NCS Resolutions----------------------------------------- @@ -803,11 +821,26 @@ private Percept getPerceptWithLesserImpactOnVolume(ArrayList<Percept> containing * @see agents.context.AbstractContext#die() */ public void die () { + for(Percept percept : perceptSenders) { + percept.deleteContextProjection(this); + } + localModel.die(); super.die(); } + public void setPerceptValidity(Percept percept) { + perceptValidities.put(percept, true); + } + public Boolean computeValidityByPercepts() { + Boolean test = true; + for(Percept percept : perceptValidities.keySet()) { + System.out.println(percept.getName()+"--->"+perceptValidities.get(percept)); + test = test && perceptValidities.get(percept); + } + return test; + } } diff --git a/AMOEBA3/src/MAS/agents/context/Range.java b/AMOEBA3/src/MAS/agents/context/Range.java index 00a2b09b493a2dc69ff5b3a5d488ec41bcac1f59..8ff9d14dafcb3f2e309be21d3952786e06792d09 100644 --- a/AMOEBA3/src/MAS/agents/context/Range.java +++ b/AMOEBA3/src/MAS/agents/context/Range.java @@ -113,13 +113,13 @@ public class Range implements Serializable, Comparable { if (isPerceptEnum()) { this.setStart_inclu(start_inclu); this.setEnd_inclu(end_inclu); - this.start = Math.round(p.getValue()); - this.end = Math.round(p.getValue()); + this.setStart( Math.round(p.getValue())); + this.setEnd( Math.round(p.getValue())); } else { this.setStart_inclu(start_inclu); this.setEnd_inclu(end_inclu); - this.start = start - Math.abs(extendedrangeatcreation * start); - this.end = end + Math.abs(extendedrangeatcreation * end); + this.setStart(start - Math.abs(extendedrangeatcreation * start)); + this.setEnd( end + Math.abs(extendedrangeatcreation * end)); } this.context = context; id = maxid; @@ -139,8 +139,8 @@ public class Range implements Serializable, Comparable { */ public Range(Range r) { super(); - this.start = r.start; - this.end = r.end; + this.setStart( r.start); + this.setEnd( r.end); this.start_inclu = r.start_inclu; this.end_inclu = r.end_inclu; this.value = r.value; @@ -169,10 +169,10 @@ public class Range implements Serializable, Comparable { private boolean extend(double target, Percept p) { int c = contains(target); if (c == -1) { - start = target; + this.setStart(target); return true; } else if (c == 1) { - end = target; + this.setEnd(target); return true; } else { return false; @@ -218,9 +218,9 @@ public class Range implements Serializable, Comparable { */ private void adaptMaxWithoutAVT(Context c, double oracleValue) { if (contains(oracleValue) == 0.0) { - end -= (end - start) * percent_down; + this.setEnd(end - ((end - start) * percent_down)); } else { - end += (end - start) * percent_up; + this.setEnd(end + ((end - start) * percent_up)); } } @@ -233,9 +233,9 @@ public class Range implements Serializable, Comparable { private void adaptMinWithoutAVT(Context c, double oracleValue) { if (contains(oracleValue) == 0.0) { - start += (end - start) * percent_up; + this.setStart(start + ((end - start) * percent_up)); } else { - start -= (end - start) * percent_down; + this.setStart(start - ((end - start) * percent_down)); } } @@ -268,7 +268,7 @@ public class Range implements Serializable, Comparable { } else { AVT_deltaMax *= AVT_acceleration; } - end -= AVT_deltaMax; + this.setEnd(end - AVT_deltaMax); AVT_lastFeedbackMax = -1; @@ -279,7 +279,7 @@ public class Range implements Serializable, Comparable { } else { AVT_deltaMax *= AVT_deceleration; } - end += AVT_deltaMax; + this.setEnd(end + AVT_deltaMax); AVT_lastFeedbackMax = 1; } @@ -302,7 +302,7 @@ public class Range implements Serializable, Comparable { } else { AVT_deltaMin *= AVT_acceleration; } - start += AVT_deltaMin; + this.setStart(start + AVT_deltaMin); AVT_lastFeedbackMin = -1; @@ -313,7 +313,7 @@ public class Range implements Serializable, Comparable { } else { AVT_deltaMin *= AVT_deceleration; } - start -= AVT_deltaMin; + this.setStart(start - AVT_deltaMin); AVT_lastFeedbackMin = 1; } @@ -436,12 +436,12 @@ public class Range implements Serializable, Comparable { } else { if (Math.abs(r.getStart() - this.getEnd()) > Math.abs(r.getEnd() - this.getStart())) { //Change min - start = r.getEnd(); + this.setStart(r.getEnd()); this.setStart_inclu(!r.isEnd_inclu()); } else { //Change max - end = r.getStart(); + this.setEnd( r.getStart()); this.setEnd_inclu(!r.isStart_inclu()); } } @@ -564,8 +564,8 @@ public class Range implements Serializable, Comparable { if (getLenght() < v.getMinMaxDistance() * Range.minLenghtRatio) { double distanceToAdd = (v.getMinMaxDistance() * Range.minLenghtRatio) - getLenght(); - start -= distanceToAdd / 2.0; - end += distanceToAdd / 2.0; + this.setStart(start - (distanceToAdd / 2.0)); + this.setEnd(end + (distanceToAdd / 2.0)); } } @@ -575,8 +575,8 @@ public class Range implements Serializable, Comparable { * @param percent : percentage of the length */ private void addMargin(double percent) { - start -= this.getLenght() * percent; - end += this.getLenght() * percent; + this.setStart(start - (this.getLenght() * percent)); + this.setEnd(end + (this.getLenght() * percent)); } @@ -644,5 +644,15 @@ public class Range implements Serializable, Comparable { public int compareTo(Object o) { return this.compareTo(o); } + + private void setStart(double newStartValue) { + this.start = newStartValue; + if(this.context != null) this.percept.updateContextProjection(this.context); + } + + private void setEnd(double newEndValue) { + this.end = newEndValue; + if(this.context != null) this.percept.updateContextProjection(this.context); + } } diff --git a/AMOEBA3/src/experiments/badContext/AMOEBA_UI.java b/AMOEBA3/src/experiments/badContext/AMOEBA_UI.java index 3804d3c54c0edebeaa895676b2c949e69799e30c..a9b08209eb639ff4bcf95e7f39cd866226b057d4 100644 --- a/AMOEBA3/src/experiments/badContext/AMOEBA_UI.java +++ b/AMOEBA3/src/experiments/badContext/AMOEBA_UI.java @@ -113,5 +113,4 @@ public class AMOEBA_UI { return message; } - } diff --git a/AMOEBA3/src/experiments/badContext/BadContextLauncherEasy.java b/AMOEBA3/src/experiments/badContext/BadContextLauncherEasy.java index 4132dc262de9682cbec411435937d77268664d07..dd9f4d060993ecf599610f65ef12a7d0b46074ea 100644 --- a/AMOEBA3/src/experiments/badContext/BadContextLauncherEasy.java +++ b/AMOEBA3/src/experiments/badContext/BadContextLauncherEasy.java @@ -2,9 +2,12 @@ package experiments.badContext; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.SimpleTimeZone; import MAS.agents.Agent; +import MAS.agents.ContextProjection; import MAS.agents.Percept; import MAS.agents.context.Context; import MAS.agents.localModel.TypeLocalModel; @@ -41,6 +44,7 @@ public class BadContextLauncherEasy implements Serializable { /* This is the initialization of the studied system. It's only for the sake of example, not a part of AMOEBA initialization*/ BadContextManager bcm = new BadContextManager(); bcm.setWorld(amoeba.getScheduler().getWorld()); + ArrayList<Percept> percepts = new ArrayList<Percept>(); for (int i = 0 ; i < 100 ; i++) { @@ -49,7 +53,16 @@ public class BadContextLauncherEasy implements Serializable { /*This is a learning step of AMOEBA*/ amoeba.learn(new HashMap<String, Double>(bcm.getOutput())); - + percepts = amoeba.getScheduler().getWorld().getAllPercept(); + for (Percept percept : percepts) { + System.out.println("Nbr of contexts "+percept.contextProjections.size()); + Collection<ContextProjection> contextProjections = percept.contextProjections.values(); + for(ContextProjection contextProjection : contextProjections) { + System.out.println(contextProjection.getRanges()); + } + + } + System.out.println(" - - - - "); try { Thread.sleep(100); @@ -71,7 +84,7 @@ public class BadContextLauncherEasy implements Serializable { ArrayList<Agent> A = AMOEBA_UI.getContexts(amoeba); ArrayList<Context> C = AMOEBA_UI.getContextsAsContexts(amoeba); - System.out.println(C); + //System.out.println(C); Percept P0 = P.get(0); Percept P1 = P.get(1);