Skip to content

Commit

Permalink
Updated map generation
Browse files Browse the repository at this point in the history
  • Loading branch information
david-istvan committed May 24, 2024
1 parent cbfd1a9 commit 389dc55
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ A framework for human-informed reinforcement learning by subjective logic
# How to use
:warning: All scripts to be run from the root directory. :warning:
- Generate a map by running `python .\src\map_tools.py --generate --render --size [SIZE] --seed [SEED]` -- Replace `[SIZE]` and `[SEED]` with the values (int) you need. The `--render` flag is optional.
- Generate a map by running `python .\src\map_tools.py (--generate --render --size [SIZE] --seed [SEED]) | -default` -- Replace `[SIZE]` and `[SEED]` with the values (int) you need. The `--render` flag is optional. When run with the `-default` option, the default 4x4 map will be generated.
- Create an opinion file with the following name: `opinions-[SIZE]x[SIZE]-seed[SEED].txt` (e.g., `opinions-6x6-seed10.txt`)
- The opinion file can be generated by running `python .\src\opinion_tools.py --size [SIZE] --seed [SEED] -g [ALL|HOLES]`. `ALL` will generate opinions for all cells; `HOLES` will generate opinions for the holes and the goal. Opinion values for frozen tiles in `ALL`: +1 if no neighboring holes; 0 if one neighboring hole; -1 otherwise.
- Run the experiment using `python .\src\runner.py`. Optional parameters:
Expand Down
Binary file added maps/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified maps/lake-12x12-seed63.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified maps/lake-12x12-seed63.xlsx
Binary file not shown.
37 changes: 24 additions & 13 deletions src/map_tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import argparse
import gymnasium as gym
import gym
import imageio
import logging
import os
Expand Down Expand Up @@ -154,33 +154,44 @@ def parse_map(self, size, seed):

return map_desc

def render_map(self, size, seed):
def render_default_map(self):
map_desc = ["SFFF", "FHFH", "FFFH", "HFFG"]
imgfile = os.path.abspath(f'{self._FILES_PATH}/default.png')
self.render_map_from_description(map_desc, imgfile)

def render_random_map(self, size, seed):
map_desc = self.parse_map(size, seed)
imgfile = os.path.abspath(f'{self._FILES_PATH}/{self.get_file_name(size, seed)}.png')
self.render_map_from_description(map_desc, imgfile)

def render_map_from_description(self, map_desc, imgfile):
env = gym.make('FrozenLake-v1', desc=map_desc, render_mode='rgb_array')
env.reset()
img = env.render()
imgfile = os.path.abspath(f'{self._FILES_PATH}/{self.get_file_name(size, seed)}.png')
imageio.imwrite(imgfile, img)
env.close()

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--generate', action='store_true')
parser.add_argument('--render', action='store_true')
parser.add_argument('--default', action='store_true')
parser.add_argument('--size')
parser.add_argument('--seed')

options = parser.parse_args()

assert options.size
assert options.seed
size = int(options.size)
seed = int(options.seed)

map_tools = MapTools('./maps')
if(options.generate):
map_tools.generate_map(size, seed)
if(options.render):
map_tools.render_map(size, seed)
if(options.default):
map_tools.render_default_map()
else:
raise Exception('Either --generate or --render (or both) should be chosen.')
assert options.size
assert options.seed
size = int(options.size)
seed = int(options.seed)
if(options.generate):
map_tools.generate_map(size, seed)
if(options.render):
map_tools.render_random_map(size, seed)
else:
raise Exception('Either --generate or --render (or both) should be chosen.')

0 comments on commit 389dc55

Please sign in to comment.