Skip to content

Commit

Permalink
adding LTL patterns and creating new worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
samira-shirzadeh committed Aug 20, 2023
1 parent a6b5e6a commit 35b3c84
Show file tree
Hide file tree
Showing 50 changed files with 59,887 additions and 135 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static Vec3 getActualSize(WorldEntity block) {
Vec3 logicalSize = block.extent ;
boolean isLargeBlock = block.getStringProperty("blockType").contains("Large")
|| block.getStringProperty("blockType").contains("Window1x1Flat")
|| block.getStringProperty("blockType").contains("BasicAssembler")
;
if(isLargeBlock) {
var size = Vec3.mul(logicalSize,CubeSize.Large.getValue()) ;
Expand Down Expand Up @@ -192,7 +193,8 @@ public static WorldEntity findClosestBlock(WorldModel wom, Predicate<WorldEntity
.filter(e -> selector.test(e))
.collect(Collectors.toList());
if(candidates.isEmpty()) return null ;

System.out.println("candidate in find close block " + candidates.size());
candidates.forEach(e -> System.out.println("candidator to move" + e));
if(candidates.size() == 1) return candidates.get(0) ;

// if there are more than one, sort the candidates to get the closest one:
Expand Down
104 changes: 102 additions & 2 deletions JvmClient/src/jvmMain/java/uuspaceagent/UUGoalLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import spaceEngineers.model.ToolbarLocation;


import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class UUGoalLib {

Expand Down Expand Up @@ -78,6 +80,20 @@ public static Function<UUSeAgentState,GoalStructure> closeTo(TestAgent agent,
) ;
}

public static Function<UUSeAgentState,GoalStructure> closeTo(TestAgent agent,
String blockType,
SEBlockFunctions.BlockSides side,
float delta) {
return closeTo(agent,
"type " + blockType,
(UUSeAgentState state) -> (WorldEntity e)
->
blockType.equals(e.getStringProperty("blockType")),
side,
delta
) ;
}

/**
* Use this to target a block using a generic selector function.
*/
Expand Down Expand Up @@ -120,6 +136,25 @@ public static Function<UUSeAgentState,GoalStructure> closeTo(TestAgent agent,
} ;
}

public static Function<UUSeAgentState,GoalStructure> closeToPosition(TestAgent agent,
String blockType,
SEBlockFunctions.BlockSides side,
Vec3 targetPosition,
float radius,
float delta) {
float sqradius = radius * radius ;
return closeTo(agent,
"type " + blockType,
(UUSeAgentState state) -> (WorldEntity e)
->
blockType.equals(e.getStringProperty("blockType"))
&& Vec3.sub(e.position, targetPosition ).lengthSq() <= sqradius,
side,
delta
) ;
}


public static GoalStructure grinderEquiped() {
return lift("Grinder equiped",
action("equip grinder").do1((UUSeAgentState state) -> {
Expand Down Expand Up @@ -189,7 +224,8 @@ public static GoalStructure targetBlockOK(TestAgent agent, Predicate<WorldEntity

public static GoalStructure grinded(TestAgent agent, float targetIntegrity) {

GoalStructure grind = DEPLOYonce(agent, (UUSeAgentState state) -> {
// changing DEPLOYONCE to DEPLOY. in order to be ablabe to call this goal for different blocks
GoalStructure grind = DEPLOY(agent, (UUSeAgentState state) -> {
WorldEntity target = state.targetBlock() ;
if(target == null) {
return FAIL("Grinding autofail: there is no target block.") ;
Expand Down Expand Up @@ -229,7 +265,7 @@ public static GoalStructure veryclose2DTo(String goalname, Vec3 p) {
}
return goal(goalname)
.toSolve((Float square_distance) -> {
//System.out.println(">> sq-dist " + square_distance) ;
System.out.println(">> sq-dist " + square_distance ) ;
return square_distance <= UUTacticLib.THRESHOLD_SQUARED_DISTANCE_TO_POINT ;
})
.withTactic(FIRSTof(UUTacticLib.straightline2DMoveTowardsACT(p).lift() , ABORT()))
Expand All @@ -246,4 +282,68 @@ public static GoalStructure face2DToward(String goalname, Vec3 p) {
.lift() ;
}


public static boolean findItemPredicate(UUSeAgentState st, String blockType){
List<WorldEntity> blocks = SEBlockFunctions.getAllBlocks(st.wom).stream().filter(e -> blockType.equals(e.getStringProperty("blockType"))).collect(Collectors.toList());
System.out.println(" === #wom=" + st.wom.elements.size());
System.out.println("number of blocks" + blocks.size() );
for(var block : blocks) {
System.out.println("Candidates: " + block.id + " type and properties" + block);
}
var numberofBlocks = SEBlockFunctions.getAllBlocks(st.wom);
System.out.println("number of Blocks: "+ numberofBlocks.size());

//get blocks in different way
// Observation rawGridsAndBlocksStates = st.env().getController().getObserver().observeBlocks() ;
// WorldModel gridsAndBlocksStates = SeEnvironmentKt.toWorldModel(rawGridsAndBlocksStates) ;
// var candidates = SEBlockFunctions.getAllBlocks(gridsAndBlocksStates);
// for(var block : SEBlockFunctions.getAllBlocks(gridsAndBlocksStates)) {
// if(block.getProperty("blockType").equals(blockType)) i++;
//
// }

//print distance to each block
// candidates.forEach(e ->
// System.out.println("distance: "+ Vec3.dist(e.position, st.wom.position) + " block type" + e.getStringProperty("blockType") + "position: " + e.position)
// );


if(blocks.size()>0) return false;
return true ;
}
public static GoalStructure interacted(TestAgent agent, float targetIntegrity) {

GoalStructure grind = DEPLOY(agent, (UUSeAgentState state) -> {
WorldEntity target = state.targetBlock() ;
if(target == null) {
return FAIL("interacting autofail: there is no target block.") ;
}
String targetId = target.id ;
float precentageTagetIntegrity = 100 * targetIntegrity ;
float integrityThreshold = ((float) target.getProperty("maxIntegrity")) * targetIntegrity ;

return goal("block " + targetId + "(" + target.getStringProperty("blockType") + ") is grinded to integrity <= " + precentageTagetIntegrity + "%")
.toSolve((WorldEntity e) -> e == null || ((float) e.getProperty("integrity") <= integrityThreshold))
.withTactic(action("Grinding")
.do1((UUSeAgentState st) -> {
UUTacticLib.interacted(state,50);
Observation rawGridsAndBlocksStates = st.env().getController().getObserver().observeBlocks() ;
WorldModel gridsAndBlocksStates = SeEnvironmentKt.toWorldModel(rawGridsAndBlocksStates) ;
return SEBlockFunctions.findWorldEntity(st.wom,targetId) ;
})
.lift())
.lift() ;
}) ;

GoalStructure stopGrinding = lift("Grinding stopped",
action("stop grinding")
.do1((UUSeAgentState st) -> {
st.env().endUsingTool();
return true ;
})
) ;

return SEQ(grinderEquiped(), grind, stopGrinding, barehandEquiped()) ;
}

}
18 changes: 16 additions & 2 deletions JvmClient/src/jvmMain/java/uuspaceagent/UUTacticLib.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uuspaceagent;

import eu.iv4xr.framework.mainConcepts.WorldEntity;
import eu.iv4xr.framework.spatial.Vec3;
import nl.uu.cs.aplib.mainConcepts.Action;
import nl.uu.cs.aplib.mainConcepts.Tactic;
Expand Down Expand Up @@ -346,12 +347,12 @@ public static Tactic navigateToTAC(Vec3 destination) {

// check first if we should turn on/off jetpack:
if(state.wom.position.y - state.navgrid.origin.y <= NavGrid.AGENT_HEIGHT
&& path.get(0).y == 0 && state.jetpackRunning()
&& !path.isEmpty() && path.get(0).y == 0 && state.jetpackRunning()
) {
state.env().getController().getCharacter().turnOffJetpack() ;
}
else {
if (path.get(0).y > 0 && !state.jetpackRunning()) {
if (!path.isEmpty() && path.get(0).y > 0 && !state.jetpackRunning()) {
state.env().getController().getCharacter().turnOnJetpack() ;
}
}
Expand Down Expand Up @@ -451,4 +452,17 @@ public static List<DPos3> smoothenPath(List<DPos3> path) {
}
return path ;
}

/**
* Interact with an object like door
* @param state
* @param duration
*/
public static void interacted(UUSeAgentState state, int duration) {
WorldEntity target = state.targetBlock() ;
String targetId = target.id ;
for(int k=0; k<duration; k++) {
state.env().getController().getAdmin().getCharacter().use(targetId,k,1);
}
}
}
43 changes: 43 additions & 0 deletions JvmClient/src/jvmTest/java/uuspaceagent/Coba_Identify_Id.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package uuspaceagent;

import eu.iv4xr.framework.mainConcepts.WorldEntity;

import static uuspaceagent.TestUtils.loadSE;
import eu.iv4xr.framework.mainConcepts.WorldEntity;
import eu.iv4xr.framework.mainConcepts.WorldModel;
import org.junit.jupiter.api.Test;
import spaceEngineers.model.*;
import static uuspaceagent.TestUtils.loadSE;


public class Coba_Identify_Id {
@Test
public void test() throws InterruptedException {
var state = loadSE("world-3 blocks").snd;

state.updateState(state.agentId);

System.out.println("** find the id of blocks");

state.updateState(state.agentId);

WorldEntity agentInfo = state.wom.elements.get(state.agentId);
System.out.println("** Agent's info: " + PrintInfos.showWorldEntity(agentInfo));

System.out.println("** Agent's info: " + PrintInfos.showWOMElements(state.wom));

/*
System.out.println("======");
CharacterObservation cobs = state.env().getController().getObserver().observe() ;
if(cobs.getTargetBlock() != null) {
System.out.println("=== target block: " + cobs.getTargetBlock().getId());
}
*/

WorldEntity target = SEBlockFunctions.findClosestBlock(state.wom, "BasicAssembler", 10);
String batteryId = target.id;
System.out.println("** target state: " + PrintInfos.showWorldEntity(target));

TestUtils.closeConnectionToSE(state);
}
}
79 changes: 79 additions & 0 deletions JvmClient/src/jvmTest/java/uuspaceagent/Test_GrindingNearest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package uuspaceagent;

import eu.iv4xr.framework.mainConcepts.TestAgent;
import eu.iv4xr.framework.mainConcepts.TestDataCollector;
import nl.uu.cs.aplib.mainConcepts.GoalStructure;
import nl.uu.cs.aplib.utils.Pair;
import org.junit.jupiter.api.Test;

import static nl.uu.cs.aplib.AplibEDSL.DEPLOYonce;
import static nl.uu.cs.aplib.AplibEDSL.SEQ;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static uuspaceagent.PrintInfos.showWOMAgent;
import static uuspaceagent.TestUtils.console;
import static uuspaceagent.TestUtils.loadSE;

public class Test_GrindingNearest {

public Pair<TestAgent, UUSeAgentState> deployAgent() throws InterruptedException {
var agentAndState = loadSE("world-4 blocks") ; // loadSE("myworld-3") ;
TestAgent agent = agentAndState.fst ;
UUSeAgentState state = agentAndState.snd ;
Thread.sleep(1000);
state.updateState(state.agentId);
// agent start location should be around:<10.22475,-5.0025,53.75382>,
// orientationForward: <-0.08024501,7.549446E-5,0.99677515> ... so looking towards z-axis
console(showWOMAgent(state.wom));
return new Pair<TestAgent, UUSeAgentState>(agent,state) ;
}

public void test_Goal(TestAgent agent, UUSeAgentState state, GoalStructure G) throws InterruptedException {
agent.setGoal(G) ;
int turn= 0 ;
while(G.getStatus().inProgress()) {
//console(">> [" + turn + "] " + showWOMAgent(state.wom));
agent.update();
//Thread.sleep(50);
turn++ ;
if (turn >= 1400) break ;
}
//closeIfCloseable(state.env().getController());
TestUtils.closeConnectionToSE(state);
}

@Test
public void test_navigate_and_grind() throws InterruptedException {
// This is a position that is unreachable, so this goal should abort
console("*** start test...") ;
var agentAndState = deployAgent();
TestAgent agent = agentAndState.fst ;
agent.setTestDataCollector(new TestDataCollector()) ;

// GoalStructure G = SEQ(DEPLOYonce(agent, UUGoalLib.closeTo(new Vec3(2.5f,-3.75f,-3f))),
GoalStructure G = SEQ(
DEPLOYonce(agent, UUGoalLib.closeTo(agent,
"BasicAssembler",
SEBlockFunctions.BlockSides.FRONT,
20f,
0.5f)),
UUGoalLib.targetBlockOK(agent, e ->
"BasicAssembler".equals(e.getStringProperty("blockType"))
&& (float) e.getProperty("integrity") == (float) e.getProperty("maxIntegrity"),
false
),
// UUGoalLib.photo("C:\\workshop\\projects\\iv4xr\\Screenshots\\LargeBlockBatteryBlock.png"),
UUGoalLib.grinded(agent,0.5f),
UUGoalLib.targetBlockOK(agent, e ->
(float) e.getProperty("integrity") <= 0.5f * (float) e.getProperty("maxIntegrity"),
false
)
//,
// UUGoalLib.photo("C:\\workshop\\projects\\iv4xr\\Screenshots\\LargeBlockBatteryBlock_at_50.png")
);
Thread.sleep(5000);
test_Goal(agent, agentAndState.snd, G) ;
G.printGoalStructureStatus();
assertTrue(G.getStatus().success());
assertTrue(agent.getTestDataCollector().getNumberOfPassVerdictsSeen() == 2) ;
}
}
Loading

0 comments on commit 35b3c84

Please sign in to comment.