本文整理汇总了Python中pysc2.lib.actions.FUNCTIONS属性的典型用法代码示例。如果您正苦于以下问题:Python actions.FUNCTIONS属性的具体用法?Python actions.FUNCTIONS怎么用?Python actions.FUNCTIONS使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类pysc2.lib.actions
的用法示例。
在下文中一共展示了actions.FUNCTIONS属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def main(unused_argv):
"""Print the valid actions."""
feats = features.Features(
# Actually irrelevant whether it's feature or rgb size.
features.AgentInterfaceFormat(
feature_dimensions=features.Dimensions(
screen=FLAGS.screen_size,
minimap=FLAGS.minimap_size)))
action_spec = feats.action_spec()
flattened = 0
count = 0
for func in action_spec.functions:
if FLAGS.hide_specific and actions.FUNCTIONS[func.id].general_id != 0:
continue
count += 1
act_flat = 1
for arg in func.args:
for size in arg.sizes:
act_flat *= size
flattened += act_flat
print(func.str(True))
print("Total base actions:", count)
print("Total possible actions (flattened):", flattened)
示例2: raw_unit_command
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def raw_unit_command(self, ability_id, unit_tags, pos=None, target=None):
"""Send a raw unit command."""
if isinstance(ability_id, str):
ability_id = actions.FUNCTIONS[ability_id].ability_id
action = sc_pb.Action()
cmd = action.action_raw.unit_command
cmd.ability_id = ability_id
if isinstance(unit_tags, (list, tuple)):
cmd.unit_tags.extend(unit_tags)
else:
cmd.unit_tags.append(unit_tags)
if pos:
cmd.target_world_space_pos.x = pos[0]
cmd.target_world_space_pos.y = pos[1]
elif target:
cmd.target_unit_tag = target
response = self._controller.act(action)
for result in response.result:
assert result == sc_error.Success
示例3: raw_unit_command
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def raw_unit_command(self, player, ability_id, unit_tags, pos=None,
target=None):
"""Issue a raw unit command."""
if isinstance(ability_id, str):
ability_id = actions.FUNCTIONS[ability_id].ability_id
action = sc_pb.Action()
cmd = action.action_raw.unit_command
cmd.ability_id = ability_id
if isinstance(unit_tags, (list, tuple)):
cmd.unit_tags.extend(unit_tags)
else:
cmd.unit_tags.append(unit_tags)
if pos:
cmd.target_world_space_pos.x = pos[0]
cmd.target_world_space_pos.y = pos[1]
elif target:
cmd.target_unit_tag = target
response = self._controllers[player].act(action)
for result in response.result:
self.assertEqual(result, sc_error.Success)
示例4: _init_valid_functions
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def _init_valid_functions(action_dimensions):
"""Initialize ValidFunctions and set up the callbacks."""
sizes = {
"screen": tuple(int(i) for i in action_dimensions.screen),
"screen2": tuple(int(i) for i in action_dimensions.screen),
"minimap": tuple(int(i) for i in action_dimensions.minimap),
}
types = actions.Arguments(*[
actions.ArgumentType.spec(t.id, t.name, sizes.get(t.name, t.sizes))
for t in actions.TYPES])
functions = actions.Functions([
actions.Function.spec(f.id, f.name, tuple(types[t.id] for t in f.args))
for f in actions.FUNCTIONS])
return actions.ValidActions(types, functions)
示例5: step
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def step(self, obs):
super(DefeatRoaches, self).step(obs)
if FUNCTIONS.Attack_screen.id in obs.observation.available_actions:
player_relative = obs.observation.feature_screen.player_relative
roaches = _xy_locs(player_relative == _PLAYER_ENEMY)
if not roaches:
return FUNCTIONS.no_op()
# Find the roach with max y coord.
target = roaches[numpy.argmax(numpy.array(roaches)[:, 1])]
return FUNCTIONS.Attack_screen("now", target)
if FUNCTIONS.select_army.id in obs.observation.available_actions:
return FUNCTIONS.select_army("select")
return FUNCTIONS.no_op()
示例6: fl_action
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def fl_action(self, obs, act, *args):
return self._controller.act(self._features.transform_action(
obs.observation, actions.FUNCTIONS[act](*args), skip_available=True))
示例7: test_general_actions
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def test_general_actions(self):
run_config = run_configs.get()
with run_config.start(want_rgb=False) as controller:
map_inst = maps.get("Simple64")
create = sc_pb.RequestCreateGame(
realtime=False, disable_fog=False,
local_map=sc_pb.LocalMap(map_path=map_inst.path,
map_data=map_inst.data(run_config)))
create.player_setup.add(type=sc_pb.Participant)
create.player_setup.add(
type=sc_pb.Computer, race=sc_common.Random, difficulty=sc_pb.VeryEasy)
join = sc_pb.RequestJoinGame(race=sc_common.Random,
options=sc_pb.InterfaceOptions(raw=True))
controller.create_game(create)
controller.join_game(join)
abilities = controller.data().abilities
errors = []
for f in actions.FUNCTIONS:
if abilities[f.ability_id].remaps_to_ability_id != f.general_id:
errors.append("FUNCTIONS %s/%s has abilitiy %s, general %s, expected "
"general %s" % (
f.id, f.name, f.ability_id, f.general_id,
abilities[f.ability_id].remaps_to_ability_id))
for f in actions.RAW_FUNCTIONS:
if abilities[f.ability_id].remaps_to_ability_id != f.general_id:
errors.append(
"RAW_FUNCTIONS %s/%s has abilitiy %s, general %s, expected "
"general %s" % (
f.id, f.name, f.ability_id, f.general_id,
abilities[f.ability_id].remaps_to_ability_id))
print("\n".join(errors))
self.assertFalse(errors)
示例8: select
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def select(func, unit_type):
return lambda o: actions.FUNCTIONS.select_point('select', func(unit_type, o))
示例9: assertAvail
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def assertAvail(self, expected):
actual = self.features.available_actions(self.obs)
actual_names = {actions.FUNCTIONS[i].name for i in actual}
self.assertEqual(actual_names, set(expected) | self.always_expected)
示例10: testValidFunctionsAreConsistent
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def testValidFunctionsAreConsistent(self):
feats = features.Features(features.AgentInterfaceFormat(
feature_dimensions=RECTANGULAR_DIMENSIONS))
valid_funcs = feats.action_spec()
for func_def in valid_funcs.functions:
func = actions.FUNCTIONS[func_def.id]
self.assertEqual(func_def.id, func.id)
self.assertEqual(func_def.name, func.name)
self.assertEqual(len(func_def.args), len(func.args)) # pylint: disable=g-generic-assert
示例11: testSpecificActionsAreReversible
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def testSpecificActionsAreReversible(self):
"""Test that the `transform_action` and `reverse_action` are inverses."""
feats = features.Features(features.AgentInterfaceFormat(
feature_dimensions=RECTANGULAR_DIMENSIONS,
hide_specific_actions=False))
action_spec = feats.action_spec()
for func_def in action_spec.functions:
for _ in range(10):
func_call = self.gen_random_function_call(action_spec, func_def.id)
sc2_action = feats.transform_action(
None, func_call, skip_available=True)
func_call2 = feats.reverse_action(sc2_action)
sc2_action2 = feats.transform_action(
None, func_call2, skip_available=True)
if func_def.id == actions.FUNCTIONS.select_rect.id:
# Need to check this one manually since the same rect can be
# defined in multiple ways.
def rect(a):
return point.Rect(point.Point(*a[1]).floor(),
point.Point(*a[2]).floor())
self.assertEqual(func_call.function, func_call2.function)
self.assertEqual(len(func_call.arguments), len(func_call2.arguments)) # pylint: disable=g-generic-assert
self.assertEqual(func_call.arguments[0], func_call2.arguments[0])
self.assertEqual(rect(func_call.arguments),
rect(func_call2.arguments))
else:
self.assertEqual(func_call, func_call2, msg=sc2_action)
self.assertEqual(sc2_action, sc2_action2)
示例12: testCanPickleFunctionCall
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def testCanPickleFunctionCall(self):
func = actions.FUNCTIONS.select_point("select", [1, 2])
self.assertEqual(func, pickle.loads(pickle.dumps(func)))
示例13: step
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def step(self, action):
"""Apply actions, step the world forward, and return observations"""
if FLAGS.verbose:
print('{} AVAILABLE ACTIONS.'.format(len(self.observation_cur.available_actions)))
# Uncomment to print individual available actions.
# for a in observation_cur.available_actions:
# print(actions.FUNCTIONS[a])
chosen_func_id = np.random.choice(self.observation_cur.available_actions)
if FLAGS.verbose:
chosen_action = actions.FUNCTIONS[chosen_func_id]
print('Chosen action: ', chosen_func_id, '~~', chosen_action)
# Uncomment to print "template" args of chosen action:
# allargs = self.action_spec.functions[chosen_func_id].args
# for arg in allargs:
# print(arg); print('arg sizes ', arg.sizes)
chosen_args = [[np.random.randint(0, size) for size in arg.sizes] for arg in self.action_spec.functions[chosen_func_id].args]
if FLAGS.verbose:
print('Chosen args: ', chosen_args)
action = actions.FunctionCall(chosen_func_id, chosen_args)
obs = super(Environment, self).step([action])
self.observation_cur = obs[0].observation
r = obs[0].reward
done = obs[0].step_type == pysc2environment.StepType.LAST
self.episode_reward += r
time.sleep(FLAGS.pause)
return obs, r, done, {}
示例14: step
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def step(self, obs):
super(SentryForceField, self).step(obs)
if _FORCE_FIELD in obs.observation["available_actions"]:
player_relative = obs.observation.feature_screen.player_relative
hydralisk_y, hydralisk_x = (player_relative == _PLAYER_HOSTILE).nonzero()
if not hydralisk_y.any():
return FUNCTIONS.no_op()
index = numpy.argmax(hydralisk_y)
target = [hydralisk_x[index], hydralisk_y[index]]
return FUNCTIONS.Effect_ForceField_screen("now", target)
elif _SELECT_ARMY in obs.observation["available_actions"]:
return FUNCTIONS.select_army("select")
else:
return FUNCTIONS.no_op()
示例15: __init__
# 需要导入模块: from pysc2.lib import actions [as 别名]
# 或者: from pysc2.lib.actions import FUNCTIONS [as 别名]
def __init__(
self,
map_name='MoveToBeacon',
render=False,
reset_done=True,
max_ep_len=None,
spatial_dim=16,
step_mul=8,
obs_features=None,
action_ids=ACTIONS_MINIGAMES
):
super().__init__(map_name, render, reset_done, max_ep_len)
self.step_mul = step_mul
self.spatial_dim = spatial_dim
self._env = None
# sensible action set for all minigames
if not action_ids or action_ids in [ACTIONS_MINIGAMES, ACTIONS_MINIGAMES_ALL]:
action_ids = [0, 1, 2, 3, 4, 6, 7, 12, 13, 42, 44, 50, 91, 183, 234, 309, 331, 332, 333, 334, 451, 452, 490]
# some additional actions for minigames (not necessary to solve)
if action_ids == ACTIONS_MINIGAMES_ALL:
action_ids += [11, 71, 72, 73, 74, 79, 140, 168, 239, 261, 264, 269, 274, 318, 335, 336, 453, 477]
# full action space, including outdated / unusable to current race / usable only in certain cases
if action_ids == ACTIONS_ALL:
action_ids = [f.id for f in actions.FUNCTIONS]
# by default use majority of obs features, except for some that are unnecessary for minigames
# e.g. race-specific like creep and shields or redundant like player_id
if not obs_features:
obs_features = {
'screen': ['player_relative', 'selected', 'visibility_map', 'unit_hit_points_ratio', 'unit_density'],
'minimap': ['player_relative', 'selected', 'visibility_map', 'camera'],
# available actions should always be present and in first position
'non-spatial': ['available_actions', 'player']}
self.act_wrapper = ActionWrapper(spatial_dim, action_ids)
self.obs_wrapper = ObservationWrapper(obs_features, action_ids)