Last active
July 15, 2022 17:34
-
-
Save HenryJia/23db12d61546054aa43f8dc587d9dc2c 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 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
with this change you can see how every state's component is taken into calculations
also try this following
state = env.reset()
this snippet disable fps limit