Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 283710365
  • Loading branch information
qstanczyk committed Dec 4, 2019
1 parent 1dc8ea4 commit 52cfcab
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 43 deletions.
2 changes: 1 addition & 1 deletion gfootball/env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def create_environment(env_name='',
composed of 4 planes of size 'channel_dimensions'.
Its size is then 'channel_dimensions'x4 (or 'channel_dimensions'x16 when
stacked is True).
The first plane P holds the position of players on the left team
The first plane P holds the position of players on the left
team, P[y,x] is 255 if there is a player at position (x,y), otherwise,
its value is 0.
The second plane holds in the same way the position of players
Expand Down
4 changes: 2 additions & 2 deletions gfootball/env/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ def update(self, config):
def ScenarioConfig(self):
return self._scenario_cfg

def NewScenario(self):
def NewScenario(self, inc = 1):
if 'episode_number' not in self._values:
self._values['episode_number'] = 0
self._values['episode_number'] += 1
self._values['episode_number'] += inc
self._scenario_values = {}
from gfootball.env import scenario_builder
self._scenario_cfg = scenario_builder.Scenario(self).ScenarioConfig()
19 changes: 11 additions & 8 deletions gfootball/env/football_env_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ class FootballEnvCore(object):
def __init__(self, config):
global _unused_engines
self._config = config
self._sticky_actions = football_action_set.get_sticky_actions(config)
self._use_rendering_engine = False
if _unused_engines:
self._env = _unused_engines.pop()
else:
self._env = libgame.GameEnv()
self._env.game_config.physics_steps_per_frame = config['physics_steps_per_frame']
self._sticky_actions = football_action_set.get_sticky_actions(config)
self._use_rendering_engine = False
# Reset is needed here to make sure render() API call before reset() API
# call works fine (get/setState makes sure env. config is the same).
self.reset(inc=0)

def _reset(self, animations):
def _reset(self, animations, inc):
global _unused_engines
global _unused_rendering_engine
assert (self._env.state == GameState.game_created or
Expand All @@ -70,21 +73,21 @@ def _reset(self, animations):
self._step = 0
self._observation = None
self._info = None
self._config.NewScenario()
self._config.NewScenario(inc=inc)
if self._env.state == GameState.game_created:
self._env.start_game()
self._env.reset(self._config.ScenarioConfig(), animations)
self._env.state = GameState.game_running

def reset(self):
def reset(self, inc=1):
"""Reset environment for a new episode using a given config."""
self._episode_start = timeit.default_timer()
self._action_set = football_action_set.get_action_set(self._config)
trace = observation_processor.ObservationProcessor(self._config)
self._cumulative_reward = 0
self._step_count = 0
self._trace = trace
self._reset(self._env.game_config.render)
self._reset(self._env.game_config.render, inc=inc)
while not self._retrieve_observation():
self._env.step()
return True
Expand Down Expand Up @@ -363,15 +366,15 @@ def render(self, mode):
if not self._env.game_config.render:
if not self._use_rendering_engine:
if self._env.state != GameState.game_created:
state = self.get_state()
state = self.get_state("")
self.close()
if _unused_rendering_engine:
self._env = _unused_rendering_engine
_unused_rendering_engine = None
else:
self._env = libgame.GameEnv()
self._rendering_in_use()
self._reset(False)
self._reset(animations=False, inc=0)
self.set_state(state)
# We call render twice, as the first call has bad camera position.
self._env.render(False)
Expand Down
21 changes: 14 additions & 7 deletions gfootball/env/football_env_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def check_determinism(self, extensive=False):
for episode in range(1 if extensive else 2):
hash_value = compute_hash(env, actions, extensive)
if extensive:
self.assertEqual(hash_value, 4203104251)
self.assertEqual(hash_value, 2258127135)
elif episode % 2 == 0:
self.assertEqual(hash_value, 716323440)
else:
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_render(self):
for _ in range(10):
o, _, _, _ = env.step(football_action_set.action_right)
hash = observation_hash(o, hash)
self.assertEqual(hash, 307836701)
self.assertEqual(hash, 845594868)
env.close()

def test_dynamic_render(self):
Expand Down Expand Up @@ -292,17 +292,24 @@ def test_player_order_invariant(self):
@parameterized.parameters(range(1))
def test_setstate(self, seed):
"""Checks setState functionality."""
cfg = config.Config({
cfg1 = config.Config({
'level': 'tests.symmetric',
'game_engine_random_seed': seed
'game_engine_random_seed': seed,
'reverse_team_processing' : False
})
env1 = football_env.FootballEnv(cfg)
env2 = football_env.FootballEnv(cfg)
cfg2 = config.Config({
'level': 'tests.symmetric',
'game_engine_random_seed': seed + 10,
'reverse_team_processing' : False
})
env1 = football_env.FootballEnv(cfg1)
env2 = football_env.FootballEnv(cfg2)
initial_obs = env1.reset()
env2.reset()
initial_state = env1.get_state()
env2.set_state(initial_state)
random.seed(seed)
actions = len(football_action_set.get_action_set(cfg))
actions = len(football_action_set.get_action_set(cfg1))
first_action = random.randint(0, actions - 1)
first_obs, _, _, _ = env1.step(first_action)
_, _, _, _ = env2.step(first_action)
Expand Down
2 changes: 2 additions & 0 deletions gfootball/env/remote_football_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ def _reset_with_retries(self, request):
return stub.StartGame(request, timeout=10*60)
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.DEADLINE_EXCEEDED:
time_to_sleep = 1
continue
logging.warning('Exception during request: %s', e)
logging.warning('Sleeping for %d seconds', time_to_sleep)
time.sleep(time_to_sleep)
if time_to_sleep < 1000:
time_to_sleep *= 2
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def run(self):

setup(
name='gfootball',
version='2.0.2',
version='2.0.3',
description=('Google Research Football - RL environment based on '
'open-source game Gameplay Football'),
author='Google LLC',
Expand Down
15 changes: 13 additions & 2 deletions third_party/gfootball_engine/src/cmake/backtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <signal.h>
#include "backtrace.h"
#include <stdlib.h>
#include <unistd.h>

void print_stacktrace() {
void *array[10];
size_t size;
size = backtrace(array, 10);
backtrace_symbols_fd(array, size, STDERR_FILENO);
}


void handler(int sig) {
print_stacktrace();
printf("Error: signal %d:\n", sig);
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion third_party/gfootball_engine/src/cmake/backtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#ifndef _CMAKE_BACKTRACE_H_
#define _CMAKE_BACKTRACE_H_

static inline void print_stacktrace() { }
void print_stacktrace();

void install_stacktrace();

Expand Down
3 changes: 2 additions & 1 deletion third_party/gfootball_engine/src/game_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void GameEnv::setConfig(ScenarioConfig& scenario_config) {

void GameEnv::start_game() {
assert(context == nullptr);
install_stacktrace();
std::cout.precision(17);
context = new GameContext();
ContextHolder c(this);
Expand Down Expand Up @@ -348,8 +349,8 @@ void GameEnv::step() {
void GameEnv::ProcessState(EnvState* state) {
state->process(&this->state, sizeof(this->state));
state->process(waiting_for_game_count);
context->gameTask->GetMatch()->ProcessState(state);
context->ProcessState(state);
context->gameTask->GetMatch()->ProcessState(state);
}

void GameEnv::render(bool swap_buffer) {
Expand Down
12 changes: 11 additions & 1 deletion third_party/gfootball_engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,17 @@ void Tracker::verify_snapshot(long pos, int line, const char* file,

void GameContext::ProcessState(EnvState* state) {
state->process((void*)&rng, sizeof(rng));
// scenario_config->ProcessState(state);
if (state->Load()) {
EnvState reader(game, "");
game->scenario_config.ProcessStateConstant(&reader);
if (reader.GetState() != state->GetState().substr(state->getpos(),
reader.GetState().length())) {
Log(e_FatalError, "football", "set_state",
"Current environment scenario != scenario in the state.");
}
}
game->scenario_config.ProcessStateConstant(state);
game->scenario_config.ProcessState(state);
#ifdef FULL_VALIDATION
anims->ProcessState(state);
#endif
Expand Down
12 changes: 7 additions & 5 deletions third_party/gfootball_engine/src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ struct ScenarioConfig {
}
return leftDistance < rightDistance;
}
void ProcessState(EnvState* state) {
void ProcessStateConstant(EnvState* state) {
state->process(ball_position);
int size = left_team.size();
state->process(size);
Expand All @@ -151,18 +151,20 @@ struct ScenarioConfig {
state->process(right_agents);
state->process(use_magnet);
state->process(offsides);
state->process(real_time);
state->process(game_engine_random_seed);
state->process(reverse_team_processing);
state->process(left_team_difficulty);
state->process(right_team_difficulty);
state->process(deterministic);
state->process(end_episode_on_score);
state->process(end_episode_on_possession_change);
state->process(end_episode_on_out_of_play);
state->process(game_duration);
}

}
void ProcessState(EnvState* state) {
state->process(real_time);
state->process(game_engine_random_seed);
state->process(reverse_team_processing);
}
};

enum GameState {
Expand Down
8 changes: 8 additions & 0 deletions third_party/gfootball_engine/src/onthepitch/match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,14 @@ void Match::UpdateIngameCamera() {
}

void Match::ProcessState(EnvState* state) {
if (state->getConfig()->reverse_team_processing) {
std::swap(first_team, second_team);
}
state->process(first_team);
state->process(second_team);
if (state->getConfig()->reverse_team_processing) {
std::swap(first_team, second_team);
}
bool team_0_mirror = teams[0]->isMirrored();
bool team_1_mirror = teams[1]->isMirrored();
bool ball_mirror =
Expand Down
4 changes: 2 additions & 2 deletions third_party/gfootball_engine/src/onthepitch/match.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ class Match {

MatchData *matchData;
Team *teams[2];
const int first_team = 0;
const int second_team = 1;
int first_team = 0;
int second_team = 1;
bool ball_mirrored = false;

Officials *officials;
Expand Down
16 changes: 4 additions & 12 deletions third_party/gfootball_engine/src/onthepitch/teamAIcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,9 @@ void TeamAIController::Process() {
if (lineX * team->GetDynamicSide() - allowSlackDistance >
deepestDanger * team->GetDynamicSide()) {
DO_VALIDATION;
//printf("previous: %f\n", deepestDanger);
deepestDanger = lineX - allowSlackDistance * team->GetDynamicSide();
//printf("now: %f\n", deepestDanger);
}

/*
if (team->GetID() == 0) SetRedDebugPilon(Vector3(deepestDanger, 0, 0));
if (team->GetID() == 1) SetYellowDebugPilon(Vector3(deepestDanger, 0, 0));
*/

offsideTrapX = deepestDanger;
// disable
//offsideTrapX = pitchHalfW * team->GetSide();
Expand Down Expand Up @@ -424,9 +417,9 @@ Vector3 TeamAIController::GetAdaptedFormationPosition(
// maybe setting offside trap this way doesn't work too well: after all, defensive positioning stuff is done after this, thereby diminishing trap sometimes.
// on the other hand, maybe this is a good basic trap setup, and we could apply the trap with the applyoffsidetrap function after the defensive positioning as well. (done from the def/mid strategies code)
if (backXBound * team->GetDynamicSide() >
GetOffsideTrapX() * team->GetDynamicSide())
backXBound = GetOffsideTrapX();

GetOffsideTrapX() * team->GetDynamicSide()) {
backXBound = clamp(GetOffsideTrapX(), -pitchHalfW, pitchHalfW);
}
float xFocus = 0.0f;
float xFocusStrength = 0.0f;
float yFocus = ballY * 1.0f;
Expand Down Expand Up @@ -1307,8 +1300,7 @@ void TeamAIController::ProcessState(EnvState *state) {
DO_VALIDATION;
state->process(taker);
state->process(static_cast<void*>(&setPieceType), sizeof(setPieceType));
//baseTeamTactics.ProcessState(state);
//teamTacticsModMultipliers.ProcessState(state);
baseTeamTactics.ProcessState(state);
liveTeamTactics.ProcessState(state);
state->process(offensivenessBias);
state->process(teamHasPossession);
Expand Down

0 comments on commit 52cfcab

Please sign in to comment.