-
-
Save ispapadakis/5b300438c733cad48dd9cd73db3edae6 to your computer and use it in GitHub Desktop.
Solving OpenAI's Cartpole with a very simple PID controller in 35 lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import gym | |
def sigmoid(x): | |
return 1.0 / (1.0 + np.exp(-x)) | |
env = gym.make('CartPole-v1') | |
desired_state = np.array([0, 0, 0, 0]) | |
desired_mask = np.array([0, 0, 1, 0]) | |
P, I, D = 0.1, 0.01, 0.5 | |
for i_episode in range(20): | |
state = env.reset() | |
integral = 0 | |
derivative = 0 | |
prev_error = 0 | |
for t in range(500): | |
env.render() | |
error = state - desired_state | |
integral += error | |
derivative = error - prev_error | |
prev_error = error | |
pid = np.dot(P * error + I * integral + D * derivative, desired_mask) | |
action = sigmoid(pid) | |
action = np.round(action).astype(np.int32) | |
state, reward, done, info = env.step(action) | |
if done: | |
print("Episode finished after {} timesteps".format(t+1)) | |
break | |
env.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment