Skip to content

Commit

Permalink
Adapted to use CST MindViewer and a MemoryContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
rgudwin committed Jun 9, 2022
2 parents a78cf46 + 123e074 commit 3d127a0
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 311 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'application'
mainClassName = 'ExperimentMain'
version = '0.0.2'
version = '0.0.3'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
Expand All @@ -24,8 +24,8 @@ repositories {
}

dependencies {
implementation fileTree(dir: 'lib', include: ['*.jar'])
implementation 'com.github.CST-Group:cst-desktop:1.0.3'
implementation 'com.github.CST-Group:WS3DProxy:0.0.1'
implementation 'org.json:json:20180813'
testImplementation group: 'junit', name: 'junit', version: '4.10'
}
Expand Down
Binary file removed lib/WS3DProxy.jar
Binary file not shown.
49 changes: 33 additions & 16 deletions src/main/java/AgentMind.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.Collections;
import java.util.List;
import memory.CreatureInnerSense;
import support.MindView;
import ws3dproxy.model.Thing;

/**
Expand All @@ -43,90 +42,108 @@
public class AgentMind extends Mind {

private static int creatureBasicSpeed=3;
private static int reachDistance=50;
private static int reachDistance=40;
public ArrayList<Codelet> behavioralCodelets = new ArrayList<Codelet>();

public AgentMind(Environment env) {
super();

// Create CodeletGroups and MemoryGroups for organizing Codelets and Memories
createCodeletGroup("Sensory");
createCodeletGroup("Motor");
createCodeletGroup("Perception");
createCodeletGroup("Behavioral");
createMemoryGroup("Sensory");
createMemoryGroup("Motor");
createMemoryGroup("Working");

// Declare Memory Objects
Memory legsMO;
Memory legsMO; // This Memory is going to be a MemoryContainer
Memory handsMO;
Memory visionMO;
Memory innerSenseMO;
Memory closestAppleMO;
Memory knownApplesMO;

//Initialize Memory Objects
legsMO=createMemoryObject("LEGS", "");
legsMO=createMemoryContainer("LEGS");
registerMemory(legsMO,"Motor");
handsMO=createMemoryObject("HANDS", "");
registerMemory(handsMO,"Motor");
List<Thing> vision_list = Collections.synchronizedList(new ArrayList<Thing>());
visionMO=createMemoryObject("VISION",vision_list);
registerMemory(visionMO,"Sensory");
CreatureInnerSense cis = new CreatureInnerSense();
innerSenseMO=createMemoryObject("INNER", cis);
registerMemory(innerSenseMO,"Sensory");
Thing closestApple = null;
closestAppleMO=createMemoryObject("CLOSEST_APPLE", closestApple);
registerMemory(closestAppleMO,"Working");
List<Thing> knownApples = Collections.synchronizedList(new ArrayList<Thing>());
knownApplesMO=createMemoryObject("KNOWN_APPLES", knownApples);
registerMemory(knownApplesMO,"Working");

// Create and Populate MindViewer
MindView mv = new MindView("MindView");
mv.addMO(knownApplesMO);
mv.addMO(visionMO);
mv.addMO(closestAppleMO);
mv.addMO(innerSenseMO);
mv.addMO(handsMO);
mv.addMO(legsMO);
mv.StartTimer();
mv.setVisible(true);

// Create Sensor Codelets
// Create Sensor Codelets
Codelet vision=new Vision(env.c);
vision.addOutput(visionMO);
insertCodelet(vision); //Creates a vision sensor
registerCodelet(vision,"Sensory");

Codelet innerSense=new InnerSense(env.c);
innerSense.addOutput(innerSenseMO);
insertCodelet(innerSense); //A sensor for the inner state of the creature
registerCodelet(innerSense,"Sensory");

// Create Actuator Codelets
Codelet legs=new LegsActionCodelet(env.c);
legs.addInput(legsMO);
insertCodelet(legs);
registerCodelet(legs,"Motor");

Codelet hands=new HandsActionCodelet(env.c);
hands.addInput(handsMO);
insertCodelet(hands);
registerCodelet(hands,"Motor");

// Create Perception Codelets
Codelet ad = new AppleDetector();
ad.addInput(visionMO);
ad.addOutput(knownApplesMO);
insertCodelet(ad);
registerCodelet(ad,"Perception");

Codelet closestAppleDetector = new ClosestAppleDetector();
closestAppleDetector.addInput(knownApplesMO);
closestAppleDetector.addInput(innerSenseMO);
closestAppleDetector.addOutput(closestAppleMO);
insertCodelet(closestAppleDetector);
registerCodelet(closestAppleDetector,"Perception");

// Create Behavior Codelets
Codelet goToClosestApple = new GoToClosestApple(creatureBasicSpeed,reachDistance);
goToClosestApple.addInput(closestAppleMO);
goToClosestApple.addInput(innerSenseMO);
goToClosestApple.addOutput(legsMO);
insertCodelet(goToClosestApple);
registerCodelet(goToClosestApple,"Behavioral");

behavioralCodelets.add(goToClosestApple);

Codelet eatApple=new EatClosestApple(reachDistance);
eatApple.addInput(closestAppleMO);
eatApple.addInput(innerSenseMO);
eatApple.addOutput(handsMO);
eatApple.addOutput(knownApplesMO);
insertCodelet(eatApple);
registerCodelet(eatApple,"Behavioral");
behavioralCodelets.add(eatApple);

Codelet forage=new Forage();
forage.addInput(knownApplesMO);
forage.addOutput(legsMO);
insertCodelet(forage);
registerCodelet(forage,"Behavioral");
behavioralCodelets.add(forage);

// sets a time step for running the codelets to avoid heating too much your machine
for (Codelet c : this.getCodeRack().getAllCodelets())
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
* Klaus Raizer, Andre Paraense, Ricardo Ribeiro Gudwin
*****************************************************************************/

import support.ResourcesGenerator;
import ws3dproxy.CommandExecException;
import ws3dproxy.WS3DProxy;
import ws3dproxy.model.Creature;
import ws3dproxy.model.World;
import ws3dproxy.util.Constants;
import ws3dproxy.util.Logger;

/**
*
* @author rgudwin
*/
public class Environment {
public final class Environment {

public String host="localhost";
public int port = 4011;
Expand All @@ -41,18 +44,25 @@ public Environment() {
World.createFood(0, 350, 75);
World.createFood(0, 100, 220);
World.createFood(0, 250, 210);
c = proxy.createCreature(100,450,0);
c = proxy.createCreature(100,450,0,0);
c.start();
//c.setRobotID("r0");
//c.startCamera("r0");


grow(w,7);
} catch (CommandExecException e) {

}
System.out.println("Robot "+c.getName()+" is ready to go.");



}

public synchronized void grow(World w, int time) {
try {
if (time <= 0) {
time = Constants.TIMEFRAME;
}
w.getDimensionAndDeliverySpot();
ResourcesGenerator rg = new ResourcesGenerator(time, w.getEnvironmentWidth(), w.getEnvironmentHeight(), w.getDeliverySpot().getX(), w.getDeliverySpot().getY());
rg.start();
} catch (CommandExecException ex) {
Logger.logException(World.class.getName(), ex);
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/ExperimentMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Klaus Raizer, Andre Paraense, Ricardo Ribeiro Gudwin
*****************************************************************************/

import br.unicamp.cst.util.viewer.MindViewer;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -32,11 +33,13 @@ public class ExperimentMain {


public ExperimentMain() {
//WS3DProxy.logger.setLevel(Level.SEVERE);
Logger.getLogger("codelets").setLevel(Level.SEVERE);
// Create Environment
Environment env=new Environment(); //Creates only a creature and some apples
AgentMind a = new AgentMind(env); // Creates the Agent Mind and start it
AgentMind a = new AgentMind(env); // Creates the Agent Mind and start it
// The following lines create the MindViewer and configure it
MindViewer mv = new MindViewer(a,"MindViewer",a.behavioralCodelets);
mv.setVisible(true);

}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/codelets/behaviors/EatClosestApple.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class EatClosestApple extends Codelet {
public EatClosestApple(int reachDistance) {
setTimeStep(50);
this.reachDistance=reachDistance;
this.name = "EatClosestApple";
}

@Override
Expand All @@ -71,8 +72,8 @@ public void proc() {
double appleX=0;
double appleY=0;
try {
appleX=closestApple.getX1();
appleY=closestApple.getY1();
appleX=closestApple.getCenterPosition().getX();
appleY=closestApple.getCenterPosition().getY();
appleName = closestApple.getName();


Expand All @@ -93,13 +94,15 @@ public void proc() {
double distance = pSelf.distance(pApple);
JSONObject message=new JSONObject();
try {
if(distance<reachDistance){ //eat it
if(distance<=reachDistance){ //eat it
message.put("OBJECT", appleName);
message.put("ACTION", "EATIT");
handsMO.setI(message.toString());
activation=1.0;
DestroyClosestApple();
}else{
handsMO.setI(""); //nothing
activation=0.0;
}

// System.out.println(message);
Expand All @@ -109,6 +112,7 @@ public void proc() {
}
}else{
handsMO.setI(""); //nothing
activation=0.0;
}
//System.out.println("Before: "+known.size()+ " "+known);

Expand Down
22 changes: 12 additions & 10 deletions src/main/java/codelets/behaviors/Forage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import br.unicamp.cst.core.entities.Codelet;
import br.unicamp.cst.core.entities.Memory;
import br.unicamp.cst.core.entities.MemoryContainer;
import br.unicamp.cst.core.entities.MemoryObject;
import java.util.List;
import org.json.JSONException;
Expand All @@ -38,13 +39,14 @@ public class Forage extends Codelet {

private Memory knownMO;
private List<Thing> known;
private Memory legsMO;
private MemoryContainer legsMO;


/**
* Default constructor
*/
public Forage(){
public Forage(){
this.name = "Forage";
}

@Override
Expand All @@ -54,23 +56,23 @@ public void proc() {
JSONObject message=new JSONObject();
try {
message.put("ACTION", "FORAGE");
legsMO.setI(message.toString());

activation=1.0;
legsMO.setI(message.toString(),activation,name);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
else activation=0.0;
JSONObject message=new JSONObject();
message.put("ACTION", "FORAGE");
legsMO.setI(message.toString(),activation,name);
}

@Override
public void accessMemoryObjects() {
knownMO = (MemoryObject)this.getInput("KNOWN_APPLES");
legsMO=(MemoryObject)this.getOutput("LEGS");

// TODO Auto-generated method stub

legsMO = (MemoryContainer)this.getOutput("LEGS");
}

@Override
Expand Down
Loading

0 comments on commit 3d127a0

Please sign in to comment.