Farama-Foundation / Minigrid

Simple and easily configurable grid world environments for reinforcement learning
https://minigrid.farama.org/
Other
2.09k stars 604 forks source link

[Proposal] ANSI color support #293

Open patham9 opened 1 year ago

patham9 commented 1 year ago

Proposal

Currently when print(env) is used, it already prints an ASCII representation whereby each cell is printed as two letters AB, A being the cell type, and B being the cell color. Most terminal nowadays or at least for a few decades) support ANSI colors, so it would be good to have ANSI color support (using the second letter as color code instead of printing it), ideally as an optional flag.

Motivation

Personally I don't like Matplotlib visualization except for paper output, as it's overkill to render to an image to then open a window and display a grid compared to just printing it to the shell, hence this proposal. But there is another aspect: in the long-run it might also be good to make optional the dependencies which have nothing to do with the environment simulation itself (especially matplotlib, pillow, which are behemoths with native components which are known to cause portability issues on regular basis). Having a good ASCII renderer with ANSI colors could be a first step in this direction.

Pitch

I have implemented a POC, please let me know if I should create a PR of the following: https://gist.github.com/patham9/2d2c0c125bc683ba600778123bed4b0a This is how it looks like in Termux on Android: minigrid2

Alternatives

Alternatively rendering could also be done with a different library, but separating rendering from Minigrid will make using the library less convenient and might cause potential version conflicts in the future, so I don't recommend it. In any case the grid logic should be clearly separated from the rendering code and libraries needed for rendering should be optional.

pseudo-rnd-thoughts commented 1 year ago

Hey, we would certainly we interested in this. Personally, I wouldn't modify the __str__ function which is how the current string representation is done but rather with another function like pprint.

As the rendering, yes, I agree. We were interested in adding pygame by rendering which should be significantly faster than the current implementation with matplotlib

patham9 commented 1 year ago

Cool, I will create a PR accordingly! And I agree, it will be better if it will be a separate print function pprint or if you prefer something more verbose but telling: printANSIColored.

Regarding Pygame: unfortunately it isn't very portable, but if it provides speed benefits and can be added as a non-mandatory component, it might be good for Windows/Mac/Ubuntu users at least.