Java源码示例:burlap.mdp.core.oo.state.OOState
示例1
public static void validate(State s) {
BCAgent a = (BCAgent)((OOState)s).object(HelperNameSpace.CLASS_AGENT);
if (a.x < 0) {
throw new IllegalStateException("Invalid agent x: " + a.x);
}
if (a.y < 0) {
throw new IllegalStateException("Invalid agent y: " + a.y);
}
if (a.z < 0) {
throw new IllegalStateException("Invalid agent z: " + a.z);
}
}
示例2
@Override
public boolean isTerminal(State s) {
OOState os = (OOState)s;
BCAgent a = (BCAgent)os.object(CLASS_AGENT);
HelperGeometry.Pose agentPose = HelperGeometry.Pose.fromXyz(a.x, a.y, a.z);
HelperGeometry.Pose goalPose = getGoalPose(s);
//are they at goal location or dead
double distance = goalPose.distance(agentPose);
//System.out.println("Distance: " + distance + " goal at: " + goalPose);
if (distance < 0.5) {
return true;
} else {
return false;
}
}
示例3
/**
* Find the gold block and return its pose.
* @param s the state
* @return the pose of the agent being one unit above the gold block.
*/
public static HelperGeometry.Pose getGoalPose(State s) {
OOState os = (OOState)s;
List<ObjectInstance> blocks = os.objectsOfClass(HelperNameSpace.CLASS_BLOCK);
for (ObjectInstance oblock : blocks) {
BCBlock block = (BCBlock)oblock;
if (block.type == 41) {
int goalX = block.x;
int goalY = block.y;
int goalZ = block.z;
return HelperGeometry.Pose.fromXyz(goalX, goalY + 1, goalZ);
}
}
return null;
}
示例4
@Override
public List<Action> allApplicableActions(State s) {
BCAgent a = (BCAgent)((GenericOOState)s).object(CLASS_AGENT);
List<ObjectInstance> blocks = ((OOState)s).objectsOfClass(HelperNameSpace.CLASS_BLOCK);
for (ObjectInstance block : blocks) {
if (HelperActions.blockIsOneOf(Block.getBlockById(((BCBlock)block).type), HelperActions.dangerBlocks)) {
int dangerX = ((BCBlock)block).x;
int dangerY = ((BCBlock)block).y;
int dangerZ = ((BCBlock)block).z;
if ((a.x == dangerX) && (a.y - 1 == dangerY) && (a.z == dangerZ) || (a.x == dangerX) && (a.y == dangerY) && (a.z == dangerZ)) {
return new ArrayList<Action>();
}
}
}
//otherwise we pass check
return Arrays.<Action>asList(new SimpleAction(typeName));
}
示例5
@Override
public void processCommand(ICommandSender p_71515_1_, String[] p_71515_2_) {
MinecraftDomainGenerator mdg = new MinecraftDomainGenerator();
SADomain domain = mdg.generateDomain();
State in = MinecraftStateGeneratorHelper.getCurrentState(BurlapCraft.currentDungeon);
List<State> reachable = StateReachability.getReachableStates(in, domain, new SimpleHashableStateFactory());
for(State s : reachable){
OOState os = (OOState)s;
BCAgent a = (BCAgent)os.object(CLASS_AGENT);
System.out.println(a.x + ", " + a.y + ", " + a.z + ", " + a.rdir + ", "+ a.vdir + ", " + a.selected);
}
System.out.println(reachable.size());
}
示例6
private void updatePropTextArea(State s){
if(!(domain instanceof OODomain) || !(s instanceof OOState)){
return ;
}
StringBuilder buf = new StringBuilder();
List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
for(PropositionalFunction pf : props){
List<GroundedProp> gps = pf.allGroundings((OOState)s);
for(GroundedProp gp : gps){
if(gp.isTrue((OOState)s)){
buf.append(gp.toString()).append("\n");
}
}
}
propViewer.setText(buf.toString());
}
示例7
@Override
public double[] features(State s) {
List<Double> featureValueList = new LinkedList<Double>();
for(PropositionalFunction pf : this.pfsToUse){
//List<GroundedProp> gps = s.getAllGroundedPropsFor(pf);
List<GroundedProp> gps = pf.allGroundings((OOState)s);
for(GroundedProp gp : gps){
if(gp.isTrue((OOState)s)){
featureValueList.add(1.);
}
else{
featureValueList.add(0.);
}
}
}
double [] fv = new double[featureValueList.size()];
int i = 0;
for(double f : featureValueList){
fv[i] = f;
i++;
}
return fv;
}
示例8
protected void updatePropTextArea(State s){
if(!(domain instanceof OODomain) || !(s instanceof OOState)){
return ;
}
StringBuilder buf = new StringBuilder();
List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
for(PropositionalFunction pf : props){
//List<GroundedProp> gps = s.getAllGroundedPropsFor(pf);
List<GroundedProp> gps = pf.allGroundings((OOState)s);
for(GroundedProp gp : gps){
if(gp.isTrue((OOState)s)){
buf.append(gp.toString()).append("\n");
}
}
}
propViewer.setText(buf.toString());
}
示例9
private void updatePropTextArea(State s){
if(!(domain instanceof OODomain) || !(s instanceof OOState)){
return ;
}
StringBuilder buf = new StringBuilder();
List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
for(PropositionalFunction pf : props){
//List<GroundedProp> gps = s.getAllGroundedPropsFor(pf);
List<GroundedProp> gps = pf.allGroundings((OOState)s);
for(GroundedProp gp : gps){
if(gp.isTrue((OOState)s)){
buf.append(gp.toString()).append("\n");
}
}
}
propViewer.setText(buf.toString());
}
示例10
@Override
public List<Action> allApplicableActions(State s) {
List <Action> res = new ArrayList<Action>();
if(!(s instanceof OOState)){
throw new RuntimeException("Cannot get object-parameterized grounded actions in state, because " + s.getClass().getName() + " does not implement OOState");
}
//otherwise need to do parameter binding
List <List <String>> bindings = OOStateUtilities.getPossibleBindingsGivenParamOrderGroups((OOState)s, this.getParameterClasses(), this.getParameterOrderGroups());
for(List <String> params : bindings){
String [] aprams = params.toArray(new String[params.size()]);
ObjectParameterizedAction ga = this.generateAction(aprams);
if(this.applicableInState(s, ga)) {
res.add(ga);
}
}
return res;
}
示例11
/**
* Returns all possible groundings of this {@link PropositionalFunction} for the given {@link State}
* @param s the {@link State} in which all groundings will be returned
* @return a {@link List} of all possible groundings of this {@link PropositionalFunction} for the given {@link State}
*/
public List<GroundedProp> allGroundings(OOState s){
List <GroundedProp> res = new ArrayList<GroundedProp>();
if(this.getParameterClasses().length == 0){
res.add(new GroundedProp(this, new String[]{}));
return res; //no parameters so just the single gp without params
}
if(!(s instanceof OOState)){
throw new RuntimeException("Cannot generate all GroundedProp objects for state " + s.getClass().getName() + " because it does not implement OOState");
}
List <List <String>> bindings = OOStateUtilities.getPossibleBindingsGivenParamOrderGroups((OOState)s, this.getParameterClasses(), this.getParameterOrderGroups());
for(List <String> params : bindings){
String [] aprams = params.toArray(new String[params.size()]);
GroundedProp gp = new GroundedProp(this, aprams);
res.add(gp);
}
return res;
}
示例12
/**
* Updates the propositional function evaluation text display for the given state.
* @param s the input state on which propositional functions are to be evaluated.
*/
protected void updatePropTextArea(State s){
if(!(domain instanceof OODomain) || !(s instanceof OOState)){
return ;
}
StringBuilder buf = new StringBuilder();
List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
for(PropositionalFunction pf : props){
List<GroundedProp> gps = pf.allGroundings((OOState)s);
for(GroundedProp gp : gps){
if(gp.isTrue((OOState)s)){
buf.append(gp.toString()).append("\n");
}
}
}
propViewer.setText(buf.toString());
}
示例13
protected void updatePropTextArea(State s){
if(!(domain instanceof OODomain) || !(s instanceof OOState)){
return;
}
StringBuilder buf = new StringBuilder();
List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
for(PropositionalFunction pf : props){
List<GroundedProp> gps = pf.allGroundings((OOState)s);
for(GroundedProp gp : gps){
if(gp.isTrue((OOState)s)){
buf.append(gp.toString()).append("\n");
}
}
}
propViewer.setText(buf.toString());
}
示例14
protected int computeOOHashCode(OOState s){
int [] hashCodes = new int[s.numObjects()];
List<ObjectInstance> objects = s.objects();
for(int i = 0; i < hashCodes.length; i++){
ObjectInstance o = objects.get(i);
int oHash = this.computeFlatHashCode(o);
int classNameHash = o.className().hashCode();
int nameHash = o.name().hashCode();
int totalHash = oHash + 31*classNameHash + 31*31*nameHash;
hashCodes[i] = totalHash;
}
//sort for invariance to order
Arrays.sort(hashCodes);
HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(17, 31);
hashCodeBuilder.append(hashCodes);
return hashCodeBuilder.toHashCode();
}
示例15
protected boolean ooStatesEqual(OOState s1, OOState s2){
if (s1 == s2) {
return true;
}
if(s1.numObjects() != s2.numObjects()){
return false;
}
List<ObjectInstance> theseObjects = s1.objects();
for(ObjectInstance ob : theseObjects){
ObjectInstance oByName = s2.object(ob.name());
if(oByName == null){
return false;
}
if(!flatStatesEqual(ob, oByName)){
return false;
}
}
return true;
}
示例16
protected int computeOOHashCode(OOState s){
int [] hashCodes = new int[s.numObjects()];
List<ObjectInstance> objects = s.objects();
for(int i = 0; i < hashCodes.length; i++){
ObjectInstance o = objects.get(i);
int oHash = this.computeFlatHashCode(o);
int classNameHash = o.className().hashCode();
int totalHash = oHash + 31*classNameHash;
hashCodes[i] = totalHash;
}
//sort for invariance to order
Arrays.sort(hashCodes);
HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(17, 31);
hashCodeBuilder.append(hashCodes);
return hashCodeBuilder.toHashCode();
}
示例17
@Override
public boolean isTrue(OOState st, String... params) {
FrostbiteAgent agent = (FrostbiteAgent)st.object(params[0]);
int ah = agent.height;
if (ah != 0)
return false;
// Agent is on a platform
if (getLandedPlatformSpeed((FrostbiteState)st) != 0)
return false;
int ay = agent.y + agentSize / 2;
return ay >= gameIceHeight;
}
示例18
@Override
public boolean isTrue(OOState st, String... params) {
LLAgent agent = (LLAgent)st.object(params[0]);
LLBlock.LLPad pad = (LLBlock.LLPad)st.object(params[1]);
double l = pad.left;
double r = pad.right;
double b = pad.bottom;
double t = pad.top;
double x = agent.x;
double y = agent.y;
//on pad means landed on surface, so y should be equal to top
if(x >= l && x < r && y >= b && y <= t){
return true;
}
return false;
}
示例19
@Override
public boolean isTrue(OOState st, String... params) {
ObjectInstance agent = st.object(params[0]);
ObjectInstance location = st.object(params[1]);
int ax = (Integer)agent.get("x");
int ay = (Integer)agent.get("y");
int lx = (Integer)location.get("x");
int ly = (Integer)location.get("y");
if(ax == lx && ay == ly){
return true;
}
return false;
}
示例20
@Override
public void paintObject(Graphics2D g2, OOState s, ObjectInstance ob,
float cWidth, float cHeight) {
int x = (Integer)ob.get(FrostbiteDomain.VAR_X);
int y = (Integer)ob.get(FrostbiteDomain.VAR_Y);
int size = (Integer)ob.get(FrostbiteDomain.VAR_SIZE);
boolean activated = (Boolean)ob.get(FrostbiteDomain.VAR_ACTIVATED);
if (activated)
g2.setColor(activatedPlatformColor);
else
g2.setColor(Color.white);
g2.fill(new Rectangle2D.Double(x, y, size, size));
if (x + size > gameWidth)
g2.fill(new Rectangle2D.Double(x - gameWidth, y, size, size));
else if (x < 0)
g2.fill(new Rectangle2D.Double(x + gameWidth, y, size, size));
}
示例21
@Override
public double[] features(State s) {
double [] fv = new double[this.numLocations];
int aL = this.getActiveLocationVal((OOState)s);
if(aL != -1){
fv[aL] = 1.;
}
return fv;
}
示例22
protected int getActiveLocationVal(OOState s){
List<GroundedProp> gps = this.inLocationPF.allGroundings(s);
for(GroundedProp gp : gps){
if(gp.isTrue(s)){
GridLocation l = (GridLocation)s.object(gp.params[1]);
return l.type;
}
}
return -1;
}
示例23
@Override
public boolean isTrue(OOState s, String... params) {
ObjectInstance agent = s.object(params[0]);
ObjectInstance location = s.object(params[1]);
int ax = (Integer)agent.get(VAR_X);
int ay = (Integer)agent.get(VAR_Y);
int lx = (Integer)location.get(VAR_X);
int ly = (Integer)location.get(VAR_Y);
return ax == lx && ay == ly;
}
示例24
@Override
public void paintObject(Graphics2D g2, OOState s, ObjectInstance ob,
float cWidth, float cHeight) {
//agent will be filled in gray
g2.setColor(Color.GRAY);
//set up floats for the width and height of our domain
float fWidth = ExampleOOGridWorld.this.map.length;
float fHeight = ExampleOOGridWorld.this.map[0].length;
//determine the width of a single cell on our canvas
//such that the whole map can be painted
float width = cWidth / fWidth;
float height = cHeight / fHeight;
int ax = (Integer)ob.get(VAR_X);
int ay = (Integer)ob.get(VAR_Y);
//left coordinate of cell on our canvas
float rx = ax*width;
//top coordinate of cell on our canvas
//coordinate system adjustment because the java canvas
//origin is in the top left instead of the bottom right
float ry = cHeight - height - ay*height;
//paint the rectangle
g2.fill(new Ellipse2D.Float(rx, ry, width, height));
}
示例25
@Override
public void paintObject(Graphics2D g2, OOState s, ObjectInstance ob,
float cWidth, float cHeight) {
//agent will be filled in blue
g2.setColor(Color.BLUE);
//set up floats for the width and height of our domain
float fWidth = ExampleOOGridWorld.this.map.length;
float fHeight = ExampleOOGridWorld.this.map[0].length;
//determine the width of a single cell on our canvas
//such that the whole map can be painted
float width = cWidth / fWidth;
float height = cHeight / fHeight;
int ax = (Integer)ob.get(VAR_X);
int ay = (Integer)ob.get(VAR_Y);
//left coordinate of cell on our canvas
float rx = ax*width;
//top coordinate of cell on our canvas
//coordinate system adjustment because the java canvas
//origin is in the top left instead of the bottom right
float ry = cHeight - height - ay*height;
//paint the rectangle
g2.fill(new Rectangle2D.Float(rx, ry, width, height));
}
示例26
@Override
public int executeAction(Action a) {
BCAgent agent = (BCAgent)((OOState)this.environment.currentObservation()).object(HelperNameSpace.CLASS_AGENT);
int rotDir = ((this.direction + agent.rdir) % 4);
System.out.println(rotDir);
switch (rotDir) {
case 0:
System.out.println("Face South");
HelperActions.faceSouth();
break;
case 1:
System.out.println("Face West");
HelperActions.faceWest();
break;
case 2:
System.out.println("Face North");
HelperActions.faceNorth();
break;
case 3:
System.out.println("Face East");
HelperActions.faceEast();
break;
default:
break;
}
return this.delayMS;
}
示例27
@Override
public boolean isTrue(OOState s, String... params) {
BCAgent a = (BCAgent)s.object(params[0]);
BCBlock b = (BCBlock)s.object(params[1]);
if ((a.x == b.x) && ((b.y + 1) == a.y) && a.z == b.z) {
return true;
}
return false;
}
示例28
@Override
public boolean isTrue(OOState st, String... params) {
FrostbiteAgent agent = (FrostbiteAgent)st.object(params[0]);
int ay = agent.x + agentSize / 2;
return ay < gameIceHeight;
}
示例29
@Override
public boolean satisfies(State s) {
//List<GroundedProp> gps = s.getAllGroundedPropsFor(pf);
List<GroundedProp> gps = this.pf.allGroundings((OOState)s);
for(GroundedProp gp : gps){
if(gp.isTrue((OOState)s)){
return true;
}
}
return false;
}
示例30
/**
* Returns true if there existing a {@link GroundedProp} for the provided {@link State} that is in true in the {@link State}.
* @param s the {@link State} to test.
* @return true if there existing a {@link GroundedProp} for the provided {@link State} that is in true in the {@link State}; false otherwise.
*/
public boolean someGroundingIsTrue(OOState s){
List<GroundedProp> gps = this.allGroundings(s);
for(GroundedProp gp : gps){
if(gp.isTrue(s)){
return true;
}
}
return false;
}