Skip to content

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bam4d committed Mar 23, 2022
2 parents 5d76b3b + daa5a98 commit eb5bd4b
Show file tree
Hide file tree
Showing 366 changed files with 4,342 additions and 1,980 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. mac/linux/windows]
- Version [e.g. 1.2.36]
- Version [e.g. 1.3.0]

**Additional context**
Add any other context about the problem here.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10.0)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Minimum OS X deployment version")
project(Griddly VERSION 1.2.36)
project(Griddly VERSION 1.3.0)

set(BINARY ${CMAKE_PROJECT_NAME})

Expand Down
20 changes: 9 additions & 11 deletions bindings/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace griddly {

PYBIND11_MODULE(python_griddly, m) {
m.doc() = "Griddly python bindings";
m.attr("version") = "1.2.36";
m.attr("version") = "1.3.0";

#ifndef NDEBUG
spdlog::set_level(spdlog::level::debug);
Expand All @@ -35,6 +35,7 @@ PYBIND11_MODULE(python_griddly, m) {
gdy.def("get_avatar_object", &Py_GDYWrapper::getAvatarObject);
gdy.def("create_game", &Py_GDYWrapper::createGame);
gdy.def("get_level_count", &Py_GDYWrapper::getLevelCount);
gdy.def("get_observer_type", &Py_GDYWrapper::getObserverType);


py::class_<Py_GameWrapper, std::shared_ptr<Py_GameWrapper>> game_process(m, "GameProcess");
Expand Down Expand Up @@ -62,12 +63,13 @@ PYBIND11_MODULE(python_griddly, m) {
game_process.def("get_width", &Py_GameWrapper::getWidth);
game_process.def("get_height", &Py_GameWrapper::getHeight);

// Tile Size (only used in some observers)
game_process.def("get_tile_size", &Py_GameWrapper::getTileSize);

// Observation shapes
game_process.def("get_global_observation_shape", &Py_GameWrapper::getGlobalObservationShape);
game_process.def("get_player_observation_shape", &Py_GameWrapper::getPlayerObservationShape);
game_process.def("get_global_observation_description", &Py_GameWrapper::getGlobalObservationDescription);

// Tile size of the global observer
game_process.def("get_tile_size", &Py_GameWrapper::getTileSize);
game_process.def("observe", &Py_GameWrapper::observe);

// Enable the history collection mode
Expand Down Expand Up @@ -100,19 +102,14 @@ PYBIND11_MODULE(python_griddly, m) {
// Release resources for vulkan stuff
game_process.def("release", &Py_GameWrapper::release);

// Create an entity observer given a configuration of the entities and the custom variables that we want to view in the features
game_process.def("get_entity_observer", &Py_GameWrapper::createEntityObserver, py::arg("config")=py::dict());

game_process.def("seed", &Py_GameWrapper::seedRandomGenerator);

py::class_<Py_EntityObserverWrapper, std::shared_ptr<Py_EntityObserverWrapper>> entityObserver(m, "EntityObserver");
entityObserver.def("observe", &Py_EntityObserverWrapper::observe);

py::class_<Py_StepPlayerWrapper, std::shared_ptr<Py_StepPlayerWrapper>> player(m, "Player");
player.def("step", &Py_StepPlayerWrapper::stepSingle);
player.def("step_multi", &Py_StepPlayerWrapper::stepMulti);
player.def("observe", &Py_StepPlayerWrapper::observe);
player.def("get_tile_size", &Py_StepPlayerWrapper::getTileSize);
player.def("get_observation_description", &Py_StepPlayerWrapper::getObservationDescription);


py::enum_<ObserverType> observer_type(m, "ObserverType");
Expand All @@ -121,12 +118,13 @@ PYBIND11_MODULE(python_griddly, m) {
observer_type.value("ISOMETRIC", ObserverType::ISOMETRIC);
observer_type.value("VECTOR", ObserverType::VECTOR);
observer_type.value("ASCII", ObserverType::ASCII);
observer_type.value("ENTITY", ObserverType::ENTITY);
observer_type.value("NONE", ObserverType::NONE);

py::class_<NumpyWrapper<uint8_t>, std::shared_ptr<NumpyWrapper<uint8_t>>>(m, "Observation", py::buffer_protocol())
.def_buffer([](NumpyWrapper<uint8_t> &m) -> py::buffer_info {
return py::buffer_info(
m.getData(),
&m.getData(),
m.getScalarSize(),
py::format_descriptor<uint8_t>::format(),
m.getShape().size(),
Expand Down
150 changes: 0 additions & 150 deletions bindings/wrapper/EntityObserverWrapper.cpp

This file was deleted.

8 changes: 6 additions & 2 deletions bindings/wrapper/GDYWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class Py_GDYWrapper {
return gdyFactory_->getLevelCount();
}

ObserverType& getObserverType(std::string observerName) {
return gdyFactory_->getNamedObserverType(observerName);
}

py::dict getActionInputMappings() const {
const auto& actionInputsDefinitions = gdyFactory_->getActionInputsDefinitions();
py::dict py_actionInputsDefinitions;
Expand Down Expand Up @@ -79,8 +83,8 @@ class Py_GDYWrapper {
return py_actionInputsDefinitions;
}

std::shared_ptr<Py_GameWrapper> createGame(ObserverType globalObserverType) {
return std::make_shared<Py_GameWrapper>(Py_GameWrapper(globalObserverType, gdyFactory_));
std::shared_ptr<Py_GameWrapper> createGame(std::string globalObserverName) {
return std::make_shared<Py_GameWrapper>(Py_GameWrapper(globalObserverName, gdyFactory_));
}

private:
Expand Down
57 changes: 21 additions & 36 deletions bindings/wrapper/GameWrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <spdlog/spdlog.h>

#include "../../src/Griddly/Core/TurnBasedGameProcess.hpp"
#include "EntityObserverWrapper.cpp"
#include "NumpyWrapper.cpp"
#include "StepPlayerWrapper.cpp"
#include "WrapperCommon.cpp"

namespace griddly {

Expand Down Expand Up @@ -31,9 +31,9 @@ class ValidActionNode {

class Py_GameWrapper {
public:
Py_GameWrapper(ObserverType globalObserverType, std::shared_ptr<GDYFactory> gdyFactory) : gdyFactory_(gdyFactory) {
Py_GameWrapper(std::string globalObserverName, std::shared_ptr<GDYFactory> gdyFactory) : gdyFactory_(gdyFactory) {
std::shared_ptr<Grid> grid = std::make_shared<Grid>(Grid());
gameProcess_ = std::make_shared<TurnBasedGameProcess>(TurnBasedGameProcess(globalObserverType, gdyFactory, grid));
gameProcess_ = std::make_shared<TurnBasedGameProcess>(TurnBasedGameProcess(globalObserverName, gdyFactory, grid));
spdlog::debug("Created game process wrapper");
}

Expand All @@ -47,10 +47,11 @@ class Py_GameWrapper {
return gameProcess_;
}

std::shared_ptr<Py_StepPlayerWrapper> registerPlayer(std::string playerName, ObserverType observerType) {
auto observer = gdyFactory_->createObserver(gameProcess_->getGrid(), observerType);

std::shared_ptr<Py_StepPlayerWrapper> registerPlayer(std::string playerName, std::string observerName) {
// auto observerName = Observer::getDefaultObserverName(observerType);
auto nextPlayerId = ++playerCount_;
auto observer = gdyFactory_->createObserver(gameProcess_->getGrid(), observerName, gdyFactory_->getPlayerCount(), nextPlayerId);

auto player = std::make_shared<Py_StepPlayerWrapper>(Py_StepPlayerWrapper(nextPlayerId, playerName, observer, gdyFactory_, gameProcess_));
players_.push_back(player);
gameProcess_->addPlayer(player->unwrapped());
Expand Down Expand Up @@ -176,29 +177,12 @@ class Py_GameWrapper {
gameProcess_->reset();
}

std::vector<uint32_t> getGlobalObservationShape() const {
auto observer = gameProcess_->getObserver();
if (observer == nullptr) {
return {};
} else {
return observer->getShape();
}
py::object getGlobalObservationDescription() const {
return wrapObservationDescription(gameProcess_->getObserver());
}

std::vector<uint32_t> getPlayerObservationShape() const {
return players_[0]->getObservationShape();
}

std::shared_ptr<NumpyWrapper<uint8_t>> observe() {
auto observer = gameProcess_->getObserver();

if (observer == nullptr) {
throw std::invalid_argument("No global observer configured");
}

auto observationData = observer->update();

return std::make_shared<NumpyWrapper<uint8_t>>(NumpyWrapper<uint8_t>(observer->getShape(), observer->getStrides(), observationData));
py::object observe() {
return wrapObservation(gameProcess_->getObserver());
}

py::tuple stepParallel(py::buffer stepArray) {
Expand All @@ -225,9 +209,9 @@ class Py_GameWrapper {

auto externalActionNames = gdyFactory_->getExternalActionNames();

std::vector<int32_t> playerRewards;
bool terminated;
py::dict info;
std::vector<int32_t> playerRewards{};
bool terminated = false;
py::dict info{};

for (int p = 0; p < playerSize; p++) {
std::string actionName;
Expand Down Expand Up @@ -281,8 +265,13 @@ class Py_GameWrapper {
}

std::array<uint32_t, 2> getTileSize() const {
auto tileSize = gameProcess_->getObserver()->getTileSize();
return {(uint32_t)tileSize[0], (uint32_t)tileSize[1]};
auto vulkanObserver = std::dynamic_pointer_cast<VulkanObserver>(gameProcess_->getObserver());
if (vulkanObserver == nullptr) {
return {0, 0};
} else {
auto tileSize = vulkanObserver->getTileSize();
return {(uint32_t)tileSize[0], (uint32_t)tileSize[1]};
}
}

void enableHistory(bool enable) {
Expand Down Expand Up @@ -426,10 +415,6 @@ class Py_GameWrapper {
return gameProcess_->getGrid()->getAllObjectVariableNames();
}

std::shared_ptr<Py_EntityObserverWrapper> createEntityObserver(py::dict entityObserverConfig) {
return std::make_shared<Py_EntityObserverWrapper>(Py_EntityObserverWrapper(entityObserverConfig, gdyFactory_, gameProcess_));
}

void seedRandomGenerator(uint32_t seed) {
gameProcess_->seedRandomGenerator(seed);
}
Expand Down
6 changes: 3 additions & 3 deletions bindings/wrapper/NumpyWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ namespace griddly {
template <class Scalar>
class NumpyWrapper {
public:
NumpyWrapper(std::vector<uint32_t> shape, std::vector<uint32_t> strides, Scalar* data)
NumpyWrapper(const std::vector<uint32_t> shape, const std::vector<uint32_t> strides, Scalar& data)
: shape_(shape), strides_(strides), data_(data) {
}

std::vector<uint32_t> getShape() const { return shape_; }
std::vector<uint32_t> getStrides() const { return strides_; }
uint32_t getScalarSize() const { return sizeof(Scalar); }
Scalar* getData() const { return data_; }
Scalar& getData() const { return data_; }

private:
const std::vector<uint32_t> shape_;
const std::vector<uint32_t> strides_;
Scalar* data_;
Scalar& data_;
};
} // namespace griddly
Loading

0 comments on commit eb5bd4b

Please sign in to comment.