Skip to content

Commit

Permalink
Merge pull request #167 from DaddyWesker/lumber_fix
Browse files Browse the repository at this point in the history
test_agent and some refactor in tests
  • Loading branch information
DaddyWesker authored Aug 28, 2023
2 parents beea721 + de5c4c5 commit 7ffdb64
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 189 deletions.
11 changes: 9 additions & 2 deletions tests/vereya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from base_test import BaseTest


def init_mission(mc, start_x, start_y, seed, forceReset="false", forceReuse="false"):
def init_mission(mc, start_x, start_z, seed, forceReset="false", forceReuse="false", start_y=78):
want_depth = False
video_producer = mb.VideoProducer(width=320 * 4,
height=240 * 4, want_depth=want_depth)
Expand All @@ -27,7 +27,7 @@ def init_mission(mc, start_x, start_y, seed, forceReset="false", forceReuse="fal
agentSections=[mb.AgentSection(name='Cristina',
agenthandlers=agent_handlers,
# depth
agentstart=mb.AgentStart([start_x, 78.0, start_y, 1]))])
agentstart=mb.AgentStart([start_x, start_y, start_z, 1]))])
flat_json = {"biome":"minecraft:plains",
"layers":[{"block":"minecraft:diamond_block","height":1}],
"structures":{"structures": {"village":{}}}}
Expand All @@ -54,3 +54,10 @@ def init_mission(mc, start_x, start_y, seed, forceReset="false", forceReuse="fal
else:
mc.setMissionXML(miss)
return mc, obs

def count_items(inv, name):
result = 0
for elem in inv:
if elem['type'] == name:
result += elem['quantity']
return result
1 change: 1 addition & 0 deletions tests/vereya/examples
2 changes: 1 addition & 1 deletion tests/vereya/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def main():
test_files = ['test_motion_vereya', 'test_craft',
'test_inventory', 'test_quit',
'test_observation', 'test_placement', 'test_image',
'test_consistency', 'test_motion_mob', 'test_mob']
'test_consistency', 'test_motion_mob', 'test_mob', 'test_agent']
res = run_tests(test_files)
if not res.wasSuccessful():
sys.exit(1)
Expand Down
88 changes: 88 additions & 0 deletions tests/vereya/test_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import unittest
import logging
import json
import time
from tagilmo import VereyaPython
import tagilmo.utils.mission_builder as mb
from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver
from base_test import BaseTest
from common import init_mission, count_items
from mcdemoaux.agenttools.agent import TAgent
from examples.minelogy import Minelogy
from examples.knowledge_lists import *
import examples.skills


item_to_obtain = "stone_pickaxe"

class Tester(TAgent):

def __init__(self, mc, visualizer=None, goal=None):
super().__init__(mc, visualizer)
self.set_goal(goal)

def set_goal(self, goal=None):
self.goal = examples.skills.Obtain(self, [item_to_obtain])

def run(self):
running = True
while running:
acts, running = self.goal.cycle()
for act in acts:
self.rob.sendCommand(act)
time.sleep(0.05)
self.blockMem.updateBlocks(self.rob)
acts = self.goal.stop()
for act in acts:
self.rob.sendCommand(act)

class TestAgent(BaseTest):
mc = None

@classmethod
def setUpClass(self, *args, **kwargs):
start = (4.0, 69.0, 68)
mc, obs = init_mission(None, start_x=start[0], start_y=start[1], start_z=start[2], forceReset='true', seed='2')
self.mc = mc
self.rob = obs
assert mc.safeStart()
time.sleep(4)

@classmethod
def tearDownClass(self, *args, **kwargs):
self.mc.stop()

def setUp(self):
super().setUp()
self.mc.sendCommand("chat /clear")
time.sleep(4)

def test_agent(self):
mc = self.mc
agent = Tester(mc)

# initialize_minelogy
item_list, recipes = agent.rob.getItemsAndRecipesLists()
self.assertNotEqual(item_list, None, "check item_list not None")
self.assertNotEqual(recipes, None, "check recipes not None")
blockdrops = agent.rob.getBlocksDropsList()
self.assertNotEqual(blockdrops, None, "check blockdrops not None")
agent.rob.updatePassableBlocks()
try:
mlogy = Minelogy(item_list, items_to_craft, recipes, items_to_mine, blockdrops, ore_depths)
except Exception as e:
print(f'Exception occured: {e}')
return
agent.set_mlogy(mlogy)
agent.run()
mc.observeProc()
inv = mc.getInventory()
self.assertEqual(count_items(inv, item_to_obtain), 1, msg=f"check if {item_to_obtain} was crafted")

def main():
VereyaPython.setupLogger()
unittest.main()


if __name__ == '__main__':
main()
15 changes: 3 additions & 12 deletions tests/vereya/test_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@
import tagilmo.utils.mission_builder as mb
from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver
from base_test import BaseTest
from common import init_mission
from common import init_mission, count_items


def test_basic_motion():
pass


def count_items(inv, name):
result = 0
for elem in inv:
if elem['type'] == name:
result += elem['quantity']
return result


class TestConsistency(BaseTest):
mc = None

@classmethod
def setUpClass(cls, *args, **kwargs):
start = (-151.0, -213.0)
mc, obs = init_mission(None, start_x=start[0], start_y=start[1], forceReset='true', seed='0145371047')
start = (-151.0, -213)
mc, obs = init_mission(None, start_x=start[0], start_z=start[1], forceReset='true', seed='0145371047')
cls.mc = mc
cls.rob = obs
assert mc.safeStart()
Expand Down
56 changes: 2 additions & 54 deletions tests/vereya/test_craft.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,18 @@
import tagilmo.utils.mission_builder as mb
from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver
from base_test import BaseTest


def init_mission(mc, start_x=None, start_y=None):
want_depth = False
video_producer = mb.VideoProducer(width=320 * 4,
height=240 * 4, want_depth=want_depth)

obs = mb.Observations()
obs.gridNear = [[-1, 1], [-2, 1], [-1, 1]]


agent_handlers = mb.AgentHandlers(observations=obs, video_producer=video_producer)

print('starting at ({0}, {1})'.format(start_x, start_y))

#miss = mb.MissionXML(namespace="ProjectMalmo.microsoft.com",
miss = mb.MissionXML(
agentSections=[mb.AgentSection(name='Cristina',
agenthandlers=agent_handlers,
# depth
agentstart=mb.AgentStart([start_x, 78.0, start_y, 1]))])
flat_json = {"biome":"minecraft:plains",
"layers":[{"block":"minecraft:diamond_block","height":1}],
"structures":{"structures": {"village":{}}}}

flat_param = "3;7,25*1,3*3,2;1;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake"
flat_json = json.dumps(flat_json).replace('"', "%ESC")
world = mb.defaultworld(
seed='4',
forceReset="false",
forceReuse="false")
miss.setWorld(world)
miss.serverSection.initial_conditions.allowedmobs = "Pig Sheep Cow Chicken Ozelot Rabbit Villager"
# uncomment to disable passage of time:
miss.serverSection.initial_conditions.time_pass = 'false'
miss.serverSection.initial_conditions.time_start = "1000"

if mc is None:
mc = MCConnector(miss)
obs = RobustObserver(mc)
else:
mc.setMissionXML(miss)
return mc, obs

from common import count_items, init_mission

def test_basic_motion():
pass


def count_items(inv, name):
result = 0
for elem in inv:
if elem['type'] == name:
result += elem['quantity']
return result


class TestCraft(BaseTest):
mc = None

@classmethod
def setUpClass(cls, *args, **kwargs):
start = (-125.0, 71.0)
mc, obs = init_mission(None, start_x=start[0], start_y=start[1])
mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='4', forceReset="true")
cls.mc = mc
assert mc.safeStart()
time.sleep(4)
Expand Down
58 changes: 2 additions & 56 deletions tests/vereya/test_human.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,15 @@
import time
import tagilmo.utils.mission_builder as mb
from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver


def init_mission(mc, start_x=None, start_y=None):
want_depth = False
# quit by reaching target or when zero health
mission_ending = """
<MissionQuitCommands quitDescription="give_up"/>
<RewardForMissionEnd>
<Reward description="give_up" reward="243"/>
</RewardForMissionEnd>
"""

video_producer = mb.VideoProducer(width=320 * 4,
height=240 * 4, want_depth=want_depth)

obs = mb.Observations(bHuman=False)
obs = mb.Observations(bHuman=True)
obs.gridNear = [[-1, 1], [-2, 1], [-1, 1]]


agent_handlers = mb.AgentHandlers(observations=obs,
all_str=mission_ending, video_producer=video_producer)

print('starting at ({0}, {1})'.format(start_x, start_y))

#miss = mb.MissionXML(namespace="ProjectMalmo.microsoft.com",
miss = mb.MissionXML(
agentSections=[mb.AgentSection(name='Cristina',
agenthandlers=agent_handlers,
# depth
agentstart=mb.AgentStart([start_x, 74.0, start_y, 1]))])
flat_json = {"biome":"minecraft:plains",
"layers":[{"block":"minecraft:diamond_block","height":1}],
"structures":{"structures": {"village":{}}}}

flat_param = "3;7,25*1,3*3,2;1;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake"
flat_json = json.dumps(flat_json).replace('"', "%ESC")
world = mb.defaultworld(
seed='5',
forceReset="false",
forceReuse="false")
miss.setWorld(world)
miss.serverSection.initial_conditions.allowedmobs = "Pig Sheep Cow Chicken Ozelot Rabbit Villager"
# uncomment to disable passage of time:
miss.serverSection.initial_conditions.time_pass = 'false'
miss.serverSection.initial_conditions.time_start = "1000"

if mc is None:
mc = MCConnector(miss)
obs = RobustObserver(mc)
else:
mc.setMissionXML(miss)
return mc, obs

from common import init_mission

class TestQuit(unittest.TestCase):
mc: MCConnector = None

@classmethod
def setUpClass(cls, *args, **kwargs):
start = 316.5, 5375.5
start = (-108.0, -187.0)
mc, obs = init_mission(None, start_x=start[0], start_y=start[1])
mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='5', forceReset="true")
cls.mc = mc
assert mc.safeStart()
time.sleep(3)
Expand Down
2 changes: 1 addition & 1 deletion tests/vereya/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestData(BaseTest):
@classmethod
def setUpClass(cls, *args, **kwargs):
start = (-126, 73.0)
mc, obs = init_mission(None, start_x=start[0], start_y=start[1], seed='4', forceReset='true')
mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='4', forceReset='true')
cls.mc = mc
cls.rob = obs
mc.safeStart()
Expand Down
47 changes: 2 additions & 45 deletions tests/vereya/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,7 @@
import tagilmo.utils.mission_builder as mb
from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver
from base_test import BaseTest


def init_mission(mc, start_x=None, start_y=None):
want_depth = False
video_producer = mb.VideoProducer(width=320 * 4,
height=240 * 4, want_depth=want_depth)

obs = mb.Observations()
obs.gridNear = [[-1, 1], [-2, 1], [-1, 1]]


agent_handlers = mb.AgentHandlers(observations=obs, video_producer=video_producer)

print('starting at ({0}, {1})'.format(start_x, start_y))

#miss = mb.MissionXML(namespace="ProjectMalmo.microsoft.com",
miss = mb.MissionXML(
agentSections=[mb.AgentSection(name='Cristina',
agenthandlers=agent_handlers,
# depth
agentstart=mb.AgentStart([start_x, 74.0, start_y, 1]))])
flat_json = {"biome":"minecraft:plains",
"layers":[{"block":"minecraft:diamond_block","height":1}],
"structures":{"structures": {"village":{}}}}

flat_param = "3;7,25*1,3*3,2;1;stronghold,biome_1,village,decoration,dungeon,lake,mineshaft,lava_lake"
flat_json = json.dumps(flat_json).replace('"', "%ESC")
world = mb.defaultworld(
seed='5',
forceReset="false",
forceReuse="false")
miss.setWorld(world)
miss.serverSection.initial_conditions.allowedmobs = "Pig Sheep Cow Chicken Ozelot Rabbit Villager"
# uncomment to disable passage of time:
miss.serverSection.initial_conditions.time_pass = 'false'
miss.serverSection.initial_conditions.time_start = "1000"

if mc is None:
mc = MCConnector(miss)
obs = RobustObserver(mc)
else:
mc.setMissionXML(miss)
return mc, obs
from common import init_mission


def getInvSafe(obs, item_type):
Expand All @@ -61,9 +19,8 @@ class TestCraft(unittest.TestCase):

@classmethod
def setUpClass(cls, *args, **kwargs):
start = 316.5, 5375.5
start = (-108.0, -187.0)
mc, obs = init_mission(None, start_x=start[0], start_y=start[1])
mc, obs = init_mission(None, start_x=start[0], start_z=start[1], seed='5', forceReset='true')
cls.mc = mc
cls.obs = obs
assert mc.safeStart()
Expand Down
2 changes: 1 addition & 1 deletion tests/vereya/test_mob.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestData(BaseTest):
@classmethod
def setUpClass(cls, *args, **kwargs):
start = (-151.0, -213.0)
mc, obs = init_mission(None, start_x=start[0], start_y=start[1], forceReset='true', seed='43')
mc, obs = init_mission(None, start_x=start[0], start_z=start[1], forceReset='true', seed='43')


cls.mc = mc
Expand Down
2 changes: 1 addition & 1 deletion tests/vereya/test_motion_mob.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestMotionMob(TestMotion):
@classmethod
def setUpClass(cls, *args, **kwargs):
start = (-151.0, -213.0)
mc, obs = init_mission(None, start_x=start[0], start_y=start[1], forceReset='true', seed='43')
mc, obs = init_mission(None, start_x=start[0], start_z=start[1], forceReset='true', seed='43')
cls.mc = mc
cls.obs = obs
assert mc.safeStart()
Expand Down
Loading

0 comments on commit 7ffdb64

Please sign in to comment.