Skip to content

Commit

Permalink
Add new environment: CollectGems (#37)
Browse files Browse the repository at this point in the history
* add CollectGems environment

* add gif for CollectGems environment

* move Gem object definition to objects.jl
  • Loading branch information
Sid-Bhatia-0 authored Oct 9, 2020
1 parent cd69b41 commit b78a830
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ play(w;file_name="example.gif",frame_rate=5)

<img src="https://github.com/JuliaReinforcementLearning/GridWorlds.jl/raw/master/docs/src/assets/img/DoorKey.gif" width="300px">

- [x] CollectGems

<img src="https://github.com/JuliaReinforcementLearning/GridWorlds.jl/raw/master/docs/src/assets/img/CollectGems.gif" width="300px">

### Needs improvement

- [ ] Add test cases
Expand Down
Binary file added docs/src/assets/img/CollectGems.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions src/envs/collectgems.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export CollectGems

mutable struct CollectGems <: AbstractGridWorld
world::GridWorldBase{Tuple{Empty,Wall,Gem}}
agent_pos::CartesianIndex{2}
agent::Agent
num_gem_init::Int
num_gem_current::Int
end

function CollectGems(;n=8, agent_start_pos=CartesianIndex(2,2), agent_start_dir=RIGHT)
objects = (EMPTY, WALL, GEM)
w = GridWorldBase(objects, n, n)

w[EMPTY, 2:n-1, 2:n-1] .= true
w[WALL, [1,n], 1:n] .= true
w[WALL, 1:n, [1,n]] .= true

w[GEM, 1:n, 1:n] .= false
num_gem_init = n - 1
num_gem_current = num_gem_init

gem_placed = 0
while gem_placed < num_gem_init
gem_pos = CartesianIndex(rand(2:n-1), rand(2:n-1))
if (gem_pos == agent_start_pos) || (w[GEM, gem_pos] == true)
continue
else
w[GEM, gem_pos] = true
w[EMPTY, gem_pos] = false
gem_placed = gem_placed + 1
end
end

CollectGems(w, agent_start_pos, Agent(dir=agent_start_dir), num_gem_init, num_gem_current)
end

function (w::CollectGems)(::MoveForward)
dir = get_dir(w.agent)
dest = dir(w.agent_pos)
if !w.world[WALL, dest]
w.agent_pos = dest
if w.world[GEM, dest]
w.world[GEM, dest] = false
w.world[EMPTY, dest] = true
w.num_gem_current = w.num_gem_current - 1
end
end
w
end
1 change: 1 addition & 0 deletions src/envs/envs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include("empty.jl")
include("fourrooms.jl")
include("gotodoor.jl")
include("doorkey.jl")
include("collectgems.jl")
9 changes: 7 additions & 2 deletions src/objects.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export COLORS, MOVE_FORWARD, TURN_LEFT, TURN_RIGHT, UP, DOWN, LEFT, RIGHT, LRUD, EMPTY, WALL, GOAL
export MoveForward, AbstractObject, Empty, Wall, Goal, Door, Agent
export COLORS, MOVE_FORWARD, TURN_LEFT, TURN_RIGHT, UP, DOWN, LEFT, RIGHT, LRUD, EMPTY, WALL, GOAL, GEM
export MoveForward, AbstractObject, Empty, Wall, Goal, Door, Gem, Agent
export get_color

using Crayons
Expand Down Expand Up @@ -79,6 +79,11 @@ Key(c) = Key{c}()
Base.convert(::Type{Char}, ::Key) = ''
get_color(::Key{C}) where C = C

struct Gem <: AbstractObject end
const GEM = Gem()
Base.convert(::Type{Char}, ::Gem) = ''
get_color(::Gem) = :magenta

Base.@kwdef mutable struct Agent <: AbstractObject
color::Symbol=:red
dir::LRUD
Expand Down

2 comments on commit b78a830

@findmyway
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/22734

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.0.1 -m "<description of version>" b78a8308f9a83f256120806a92f92a6d31f85286
git push origin v0.0.1

Please sign in to comment.