Skip to content

Commit

Permalink
Merge pull request #6 from rradules/item-gathering
Browse files Browse the repository at this point in the history
Item gathering added to main
  • Loading branch information
rradules authored Nov 7, 2023
2 parents 47b171b + 6f955f3 commit 6bb6ef6
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 42 deletions.
41 changes: 0 additions & 41 deletions main.py

This file was deleted.

2 changes: 1 addition & 1 deletion momadm_benchmarks/envs/beach_domain/beach_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def step(self, actions):
# Obs: agent type, section id, section capacity, section consumption, % of agents of current type
observations = {agent: None for agent in self.agents}
# Note that agents only receive the reward after the last timestep
rewards = {self.agents[i]: np.array([0, 0], dtype=np.float32) for _ in range(self.num_agents)}
rewards = {self.agents[i]: np.array([0, 0], dtype=np.float32) for i in range(self.num_agents)}

for i, agent in enumerate(self.agents):
observations[agent] = self._get_obs(i, section_consumptions, section_agent_types)
Expand Down
13 changes: 13 additions & 0 deletions momadm_benchmarks/envs/item_gathering/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Item Gathering environment.
Johan Källström and Fredrik Heintz. 2019. Tunable Dynamics in Agent-Based Simulation using Multi-Objective
Reinforcement Learning. Presented at the Adaptive and Learning Agents Workshop at AAMAS 2019.
https://liu.diva-portal.org/smash/record.jsf?pid=diva2%3A1362933&dswid=9018
Notes:
- In contrast to the original environment, the observation space is a 2D array of integers, i.e.,
the map of the environment, where each integer represents either agents (1 for the agent receiving the observation,
2 for the other agents) or items (3, 4, etc., depending on the number of items).
- The number of agents and items is configurable, by providing an initial map.
- If no initial map is provided, the environment uses a default map
"""
54 changes: 54 additions & 0 deletions momadm_benchmarks/envs/item_gathering/asset_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""disgracefully stolen.
https://stackoverflow.com/questions/73956440/python-to-change-image-into-a-color-that-is-not-one-of-the-images-dominant-colo
"""

import os

import numpy as np
from PIL import Image


def get_colored(asset: str, count: int):
"""Generating random colored visual assets for dynamic environment variables.
Args:
asset: `str` value. Either "item" or "agent".
count: `int` value. The amount of total agents/items in the environment.
Returns:
numpy.ndarray with RGB values for the new colored assets.
"""
assert asset == "item" or asset == "agent", "You must specify an asset to get colored versions of."

path = os.path.dirname(os.path.realpath(__file__))
seed = 1 if asset == "item" else 2
rng = np.random.default_rng(seed) # local seed
im = Image.open(f"{path}/assets/{asset}.png").convert("RGBA")
alpha = im.getchannel("A")
im = im.convert("RGB")

for i in range(count - 1): # subtracting the default asset
rgb = rng.random((3, 3))
matrix = (rgb[0, 0], rgb[1, 0], rgb[2, 0], 0, rgb[0, 1], rgb[1, 1], rgb[2, 1], 0, rgb[0, 2], rgb[1, 2], rgb[2, 2], 0)

# Apply above transform, reinsert alpha and save
colored_im = im.convert("RGB", matrix)
colored_im.putalpha(alpha)
colored_im.save(f"{path}/assets/colored/{asset}{i}.png")


def del_colored(asset: str, count: int):
"""Deleting the previously random colored visual assets.
Args:
asset: `str` value. Either "item" or "agent".
count: `int` value. The amount of total agents/items in the environment.
"""
assert asset == "item" or asset == "agent", "You must specify an asset to delete colored versions of."

path = os.path.dirname(os.path.realpath(__file__))

for i in range(count - 1): # subtracting the default asset
os.remove(f"{path}/assets/colored/{asset}{i}.png")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6bb6ef6

Please sign in to comment.