Package: gridworld-continuous
GridWorld ContinuousA simple continuous gridworld environment constructor.Usagefromgridworld_continuous.envsimportFourRoomsfromgridworld_continuous.eventsimportSquareGoldgold=SquareGold(loc=(9.5,9.5))events=[gold]env=FourRooms(events=events)env.render()Demo Functionfrommatplotlibimportpyplotasplt,animationdefanimate(configs,interval=50):"""helper function to create demo animations"""fig,ax=plt.subplots()_,ax,objects,patches=env.render(ax=ax)count=0deffunc(i):nonlocalcountenv.set_config(*configs[i])ax.clear()env.render(ax=ax)fig.canvas.draw()ifi:ax.set_xlabel(info)count+=configs[i][2]['correction']isnotNoneifcount:ax.set_title(f'hit wall{count}times')ax.set_xticks([])ax.set_yticks([])plt.close()anim=animation.FuncAnimation(fig,func,len(configs),interval=interval)returnanimMoving towards goal until terminationimportnumpyasnpnp.random.seed(12)state=env.reset()terminal=env.terminalconfigs=[env.get_config()]whilenotterminal:action=np.clip(gold.loc-state,-0.05,0.05)reward,state,terminal,info=env.step(action)configs.append(env.get_config())anim=animate(configs)anim.save('four-rooms-get-gold.gif')Upon reaching the gold state, the finalinfovalue is:{'correction': None, 'flag': 'gold'}Theinfodictionary will always contain two entries:correction : an array which needed to be added to the action array to ensure we do not go into a wallflag : a string which indicates which special object we may have collided with orNoneMoving towards a wallnp.random.seed(12)state=env.reset()configs=[env.get_config()]count=0whilecount<20:reward,state,terminal,info=env.step((-0.05,0.02))configs.append(env.get_config())count+=info['correction']isnotNoneanim=animate(configs,interval=100)anim.save('four-rooms-to-wall.gif')Multi-Goal conditionSuppose we would like two gold locations and termination only happens when both are collected. When a special state is reached, it's flag is appended to a list of flags within the environment. By creating our own terminal function, we can use the list of flags to determine when we have reached a terminal condition. In the below example, we need to have reached two states collecting the flag 'gold' twice.defterminal_func(flags):returnflags.count('gold')==2locs=[(9.5,9.5),(2,2)]events=[SquareGold(loc=loc)forlocinlocs]initial=(8.9,8.9)env=FourRooms(events=events,initial_states=[initial],terminal_func=terminal_func)env.reset()# two subplots for before and after gold collectionfig,ax=plt.subplots(1,2,figsize=(12,5))# before gold collectionenv.render(ax[0])# step into gold location and confirm not a terminal stateaction=(0.4,0.4)reward,state,terminal,info=env.step(action)# after gold collectionenv.render(ax[1])sup=fig.suptitle(f'reward ={reward}\nstate ={state}\nterminal ={terminal}\ninfo ={info}',y=1.15)# remove ticksforaxesinax:axes.set_xticks([])axes.set_yticks([])ax[0].set_title(f'state ={initial}\naction ={action}')ax[1].set_title(f'state ={tuple(env.state)}\n')Custom LayoutUsers can create custom layouts by creating their own layout in string. Walls are denoted with the '#' character.fromgridworld_continuous.envimportBaseEnvcustom="""##############     #     ##     #     ##     #     ##     #     ##           ##############"""env=BaseEnv(custom)env.render()plt.savefig('custom.png',dpi=400)