Skip to content

Instantly share code, notes, and snippets.

@kstrempel
Last active January 27, 2021 16:01
Show Gist options
  • Save kstrempel/3b2f707c2f9019e55f4721b3a80cfbf2 to your computer and use it in GitHub Desktop.
Save kstrempel/3b2f707c2f9019e55f4721b3a80cfbf2 to your computer and use it in GitHub Desktop.
Running OpenAI with in Clojure and libpython-clj
(ns pyclojure.core
(:gen-class)
(:require [libpython-clj.require :refer [require-python]]
[libpython-clj.python :as py]))
; import gym
; env = gym.make('CartPole-v0')
; for i_episode in range(20):
; observation = env.reset()
; for t in range(100):
; env.render()
; print(observation)
; action = env.action_space.sample()
; observation, reward, done, info = env.step(action)
; if done:
; print("Episode finished after {} timesteps".format(t+1))
; break
; env.close()
(require-python 'gym)
(defn run-cartpole []
(let [env (gym/make "CartPole-v0")]
(try
(dotimes [_ 20]
(do
(py/$a env reset)
(loop [t 0]
(py/$a env render)
(let [action (-> env
(py/$. action_space)
(py/$a sample))
result (py/$a env step action)
[observation reward done info] (seq result)]
(do
(println observation)
(if done
(println (str "Episode finished after " (inc t) " timesteps"))
(if (< t 100)
(recur (inc t)))))))))
(finally
(py/$a env close)))))
(defn -main []
(run-cartpole))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment