How to use the gfootball.env.player_base.PlayerBase function in gfootball

To help you get started, we’ve selected a few gfootball examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google-research / football / gfootball / env / players / replay.py View on Github external
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Player with actions coming from specific game replay."""

from gfootball.env import player_base
from gfootball.env import script_helpers


class Player(player_base.PlayerBase):
  """Player with actions coming from specific game replay."""

  def __init__(self, player_config, env_config):
    player_base.PlayerBase.__init__(self, player_config)
    self._can_play_right = True
    self._replay = script_helpers.ScriptHelpers().load_dump(player_config['path'])
    self._step = 0
    self._player = player_config['index']

  def take_action(self, observations):
    if self._step == len(self._replay):
      print("Replay finished.")
      exit(0)
    actions = self._replay[self._step]['debug']['action'][
        self._player:self.num_controlled_players() + self._player]
    self._step += 1
github google-research / football / gfootball / env / players / bot.py View on Github external
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Sample bot player."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from gfootball.env import football_action_set
from gfootball.env import player_base
import numpy as np


class Player(player_base.PlayerBase):

  def __init__(self, player_config, env_config):
    assert env_config["action_set"] == 'full'
    player_base.PlayerBase.__init__(self, player_config)
    self._observation = None
    self._last_action = football_action_set.action_idle
    self._shoot_distance = 0.15
    self._pressure_enabled = False

  def _object_distance(self, object1, object2):
    """Computes distance between two objects."""
    return np.linalg.norm(np.array(object1) - np.array(object2))

  def _direction_action(self, delta):
    """For required movement direction vector returns appropriate action."""
    all_directions = [
github google-research / football / gfootball / env / players / ppo2_cnn.py View on Github external
$POLICY should be one of: cnn, impala_cnn, gfootball_impala_cnn.
"""

from baselines.common.policies import build_policy
from gfootball.env import football_action_set
from gfootball.env import observation_preprocessing
from gfootball.env import player_base
from gfootball.examples import models  
import gym
import joblib
import numpy as np
import tensorflow.compat.v1 as tf


class Player(player_base.PlayerBase):
  """An agent loaded from PPO2 cnn model checkpoint."""

  def __init__(self, player_config, env_config):
    player_base.PlayerBase.__init__(self, player_config)

    self._action_set = (env_config['action_set']
                        if 'action_set' in env_config else 'default')
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    self._sess = tf.Session(config=config)
    self._player_prefix = 'player_{}'.format(player_config['index'])
    stacking = 4 if player_config.get('stacked', True) else 1
    policy = player_config.get('policy', 'cnn')
    self._stacker = ObservationStacker(stacking)
    with tf.variable_scope(self._player_prefix):
      with tf.variable_scope('ppo2_model'):
github google-research / football / gfootball / env / players / agent.py View on Github external
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Agent player controlled by the training policy and using step/reset API."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from gfootball.env import player_base


class Player(player_base.PlayerBase):

  def __init__(self, player_config, env_config):
    player_base.PlayerBase.__init__(self, player_config)
    assert player_config['player_agent'] == 0, 'Only one \'agent\' player allowed'
    self._action = None

  def set_action(self, action):
    self._action = action

  def take_action(self, observations):
    return self._action
github google-research / football / gfootball / env / controller_base.py View on Github external
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Base controller class."""

from gfootball.env import football_action_set
from gfootball.env import player_base


class Controller(player_base.PlayerBase):
  """Base controller class."""

  def __init__(self, player_config, env_config):
    player_base.PlayerBase.__init__(self, player_config)
    self._active_actions = {}
    self._env_config = env_config
    self._last_action = football_action_set.action_idle
    self._last_direction = football_action_set.action_idle
    self._current_direction = football_action_set.action_idle

  def _check_action(self, action, active_actions):
    """Compare (and update) controller's state with the set of active actions.

    Args:
      action: Action to check
      active_actions: Set of all active actions