现在可以自动生成行为树扩展动作
This commit is contained in:
parent
3f1f8d9d06
commit
9a3143cafc
|
@ -7,8 +7,7 @@ class Act(Bahavior):
|
|||
def __init__(self,*args):
|
||||
super().__init__(*args)
|
||||
|
||||
def get_conds(self):
|
||||
pre = set()
|
||||
add = set()
|
||||
de = set()
|
||||
return pre, add, de
|
||||
@classmethod
|
||||
def get_info(self,*arg):
|
||||
return None
|
||||
|
||||
|
|
|
@ -19,7 +19,13 @@ class Bahavior(ptree.behaviour.Behaviour):
|
|||
if len(args)>0:
|
||||
name = f'{name}({",".join(list(args))})'
|
||||
self.name = name
|
||||
#get valid args
|
||||
# self.valid_arg_list = []
|
||||
# lines = self.valid_params.strip().splitlines()
|
||||
# for line in lines:
|
||||
# self.valid_arg_list.append((x.strip for x in line.split(",")))
|
||||
self.args = args
|
||||
|
||||
super().__init__(self.name)
|
||||
|
||||
def _update(self) -> ptree.common.Status:
|
||||
|
|
|
@ -3,17 +3,27 @@ from typing import Any
|
|||
from robowaiter.behavior_lib._base.Act import Act
|
||||
from robowaiter.behavior_lib._base.Behavior import Status
|
||||
|
||||
class MakeCoffee(Act):
|
||||
class Make(Act):
|
||||
can_be_expanded = True
|
||||
num_args = 1
|
||||
valid_args = (
|
||||
"Coffee",
|
||||
)
|
||||
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
self.target_obj = self.args[0]
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_info(cls,arg):
|
||||
info = None
|
||||
if arg == "Coffee":
|
||||
info = {
|
||||
"add": {f'On(Coffee,Table)'},
|
||||
}
|
||||
return info
|
||||
|
||||
@property
|
||||
def cond_sets(self):
|
||||
pre = {"At(Robot,Bar)"}
|
||||
add = {"At(Coffee,Bar)"}
|
||||
de = {}
|
||||
return pre,add,de
|
||||
|
||||
def _update(self) -> ptree.common.Status:
|
||||
op_type = 1
|
|
@ -4,9 +4,25 @@ from robowaiter.behavior_lib._base.Act import Act
|
|||
from robowaiter.algos.navigate.DstarLite.navigate import Navigator
|
||||
|
||||
class MoveTo(Act):
|
||||
can_be_expanded = True
|
||||
num_args = 1
|
||||
valid_args = (
|
||||
"Bar",
|
||||
"Table",
|
||||
)
|
||||
|
||||
def __init__(self, target_place):
|
||||
super().__init__(target_place)
|
||||
self.target_place = target_place
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_info(self,arg):
|
||||
info = {
|
||||
"add": {f'At(Robot,{arg})'},
|
||||
}
|
||||
return info
|
||||
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
|
||||
def _update(self) -> ptree.common.Status:
|
||||
# self.scene.test_move()
|
||||
|
|
|
@ -6,7 +6,6 @@ class At(Cond):
|
|||
can_be_expanded = True
|
||||
num_params = 2
|
||||
valid_params = '''
|
||||
Coffee, Bar
|
||||
Robot, Bar
|
||||
'''
|
||||
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import py_trees as ptree
|
||||
from typing import Any
|
||||
from robowaiter.behavior_lib._base.Cond import Cond
|
||||
|
||||
class On(Cond):
|
||||
can_be_expanded = True
|
||||
num_params = 2
|
||||
valid_params = '''
|
||||
Robot, Bar
|
||||
'''
|
||||
|
||||
def __init__(self,*args):
|
||||
super().__init__(*args)
|
||||
|
||||
|
||||
def _update(self) -> ptree.common.Status:
|
||||
# if self.scene.status?
|
||||
arg_str = self.arg_str
|
||||
|
||||
if f'At({arg_str})' in self.scene.state["condition_set"]:
|
||||
return ptree.common.Status.SUCCESS
|
||||
else:
|
||||
return ptree.common.Status.FAILURE
|
||||
|
||||
# if self.scene.state['chat_list'] == []:
|
||||
# return ptree.common.Status.FAILURE
|
||||
# else:
|
||||
# return ptree.common.Status.SUCCESS
|
|
@ -1,5 +1,7 @@
|
|||
import io
|
||||
import contextlib
|
||||
import os
|
||||
import importlib.util
|
||||
|
||||
from robowaiter.utils.bt.load import load_bt_from_ptml,find_node_by_name,print_tree_from_root
|
||||
from robowaiter.utils.bt.visitor import StatusVisitor
|
||||
|
@ -9,7 +11,7 @@ from robowaiter.behavior_tree.obtea.opt_bt_exp_main import BTOptExpInterface
|
|||
|
||||
from robowaiter.behavior_lib.act.DelSubTree import DelSubTree
|
||||
from robowaiter.behavior_lib._base.Sequence import Sequence
|
||||
|
||||
from robowaiter.utils.bt.load import load_behavior_tree_lib
|
||||
|
||||
class Robot(object):
|
||||
scene = None
|
||||
|
@ -67,15 +69,30 @@ class Robot(object):
|
|||
print("当前行为树为:")
|
||||
print_tree_from_root(self.bt.root)
|
||||
|
||||
# 获取所有action的pre,add,del列表
|
||||
def collect_action_nodes(self):
|
||||
action_list = [
|
||||
Action(name='MakeCoffee', pre={'At(Robot,CoffeeMachine)'},
|
||||
add={'At(Coffee,Bar)'}, del_set=set(), cost=1),
|
||||
Action(name='MoveTo(Table)', pre={'At(Robot,Bar)'},
|
||||
add={'At(Robot,Table)'}, del_set=set(), cost=1),
|
||||
Action(name='ExploreEnv()', pre={'At(Robot,Bar)'},
|
||||
add={'EnvExplored()'}, del_set=set(), cost=1),
|
||||
]
|
||||
action_list = []
|
||||
behavior_dict = load_behavior_tree_lib()
|
||||
for cls in behavior_dict["act"].values():
|
||||
if cls.can_be_expanded:
|
||||
if cls.num_args == 0:
|
||||
action_list.append(Action(name=cls.__name__,**cls.get_info()))
|
||||
if cls.num_args == 1:
|
||||
for arg in cls.valid_args:
|
||||
action_list.append(Action(name=cls.__name__, **cls.get_info(arg)))
|
||||
if cls.num_args > 1:
|
||||
for args in cls.valid_args:
|
||||
action_list.append(Action(name=cls.__name__,**cls.get_info(*args)))
|
||||
|
||||
print(action_list)
|
||||
# action_list = [
|
||||
# Action(name='MakeCoffee', pre={'At(Robot,CoffeeMachine)'},
|
||||
# add={'At(Coffee,Bar)'}, del_set=set(), cost=1),
|
||||
# Action(name='MoveTo(Table)', pre={'At(Robot,Bar)'},
|
||||
# add={'At(Robot,Table)'}, del_set=set(), cost=1),
|
||||
# Action(name='ExploreEnv()', pre=set(),
|
||||
# add={'EnvExplored()'}, del_set=set(), cost=1),
|
||||
# ]
|
||||
return action_list
|
||||
|
||||
def step(self):
|
||||
|
|
|
@ -2,11 +2,4 @@ import os
|
|||
|
||||
from robowaiter.utils import *
|
||||
from robowaiter.utils import *
|
||||
|
||||
|
||||
|
||||
|
||||
def get_root_path():
|
||||
return os.path.abspath(
|
||||
os.path.join(__file__, "../../..")
|
||||
)
|
||||
from robowaiter.utils.basic import get_root_path
|
|
@ -0,0 +1,6 @@
|
|||
import os
|
||||
|
||||
def get_root_path():
|
||||
return os.path.abspath(
|
||||
os.path.join(__file__, "../../..")
|
||||
)
|
|
@ -1,6 +1,8 @@
|
|||
import py_trees as ptree
|
||||
from robowaiter.behavior_tree.ptml import ptmlCompiler
|
||||
|
||||
import os
|
||||
import importlib.util
|
||||
from robowaiter.utils.basic import get_root_path
|
||||
|
||||
def load_bt_from_ptml(scene, ptml_path, behavior_lib_path):
|
||||
ptml_bt = ptmlCompiler.load(scene, ptml_path, behavior_lib_path)
|
||||
|
@ -48,6 +50,40 @@ def find_node_by_name(tree, name):
|
|||
|
||||
|
||||
|
||||
def get_classes_from_folder(folder_path):
|
||||
cls_dict = {}
|
||||
for filename in os.listdir(folder_path):
|
||||
if filename.endswith('.py'):
|
||||
# 构建模块的完整路径
|
||||
module_path = os.path.join(folder_path, filename)
|
||||
# 获取模块名(不含.py扩展名)
|
||||
module_name = os.path.splitext(filename)[0]
|
||||
|
||||
# 动态导入模块
|
||||
spec = importlib.util.spec_from_file_location(module_name, module_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
# 获取模块中定义的所有类
|
||||
for name, obj in module.__dict__.items():
|
||||
if isinstance(obj, type):
|
||||
cls_dict[module_name] = obj
|
||||
|
||||
return cls_dict
|
||||
|
||||
|
||||
def load_behavior_tree_lib():
|
||||
root_path = get_root_path()
|
||||
type_list = ["act","cond"]
|
||||
behavior_dict = {}
|
||||
for type in type_list:
|
||||
path = os.path.join(root_path,"robowaiter","behavior_lib",type)
|
||||
behavior_dict[type] = get_classes_from_folder(path)
|
||||
|
||||
return behavior_dict
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(load_behavior_tree_lib())
|
||||
# class BehaviorTree(ptree):
|
||||
# def __init__(self):
|
||||
# super().__init__()
|
|
@ -1,7 +1,3 @@
|
|||
selector{
|
||||
cond EnvExplored()
|
||||
selector{
|
||||
cond At(Robot,Bar)
|
||||
act ExploreEnv()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue