-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pre aula sobre ambiente nao deterministico
- Loading branch information
Showing
7 changed files
with
149 additions
and
18 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from IPython.display import clear_output | ||
import gymnasium as gym | ||
import numpy as np | ||
from QLearning import QLearning | ||
from numpy import loadtxt | ||
import warnings | ||
warnings.simplefilter("ignore") | ||
|
||
def print_action(action): | ||
if action == 0: | ||
print("LEFT") | ||
elif action == 1: | ||
print("DOWN") | ||
elif action == 2: | ||
print("RIGHT") | ||
elif action == 3: | ||
print("UP") | ||
|
||
# exemplo de ambiente nao determinístico | ||
env = gym.make('FrozenLake-v1', render_mode='ansi').env | ||
|
||
qlearn = QLearning(env, alpha=0.1, gamma=0.99, epsilon=0.8, epsilon_min=0.01, epsilon_dec=1, episodes=20_000) | ||
q_table = qlearn.train('data/q-table-frozen-lake-qlearning.csv','results/frozen_lake_qlearning','results/rewards_frozen_lake_qlearning.csv') | ||
#q_table = loadtxt('data/q-table-frozen-lake-qlearning.csv', delimiter=',') | ||
|
||
env = gym.make('FrozenLake-v1', render_mode='human').env | ||
|
||
(state, _) = env.reset() | ||
epochs = 0 | ||
rewards = 0 | ||
done = False | ||
|
||
while not done: | ||
action = np.argmax(q_table[state]) | ||
print_action(action) | ||
state, reward, done, _, info = env.step(action) | ||
epochs += 1 | ||
rewards += reward | ||
|
||
print("\n") | ||
print("Timesteps taken: {}".format(epochs)) | ||
print("Rewards: {}".format(rewards)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from IPython.display import clear_output | ||
import gymnasium as gym | ||
import numpy as np | ||
from Sarsa import Sarsa | ||
from numpy import loadtxt | ||
import warnings | ||
warnings.simplefilter("ignore") | ||
|
||
def print_action(action): | ||
if action == 0: | ||
print("LEFT") | ||
elif action == 1: | ||
print("DOWN") | ||
elif action == 2: | ||
print("RIGHT") | ||
elif action == 3: | ||
print("UP") | ||
|
||
# exemplo de ambiente nao determinístico | ||
env = gym.make('FrozenLake-v1', render_mode='ansi').env | ||
|
||
# only execute the following lines if you want to create a new q-table | ||
#qlearn = Sarsa(env, alpha=0.2, gamma=0.95, epsilon=0.8, epsilon_min=0.0001, epsilon_dec=0.9999, episodes=50000) | ||
#q_table = qlearn.train('data/q-table-frozen-lake-sarsa.csv','results/frozen_lake_sarsa') | ||
q_table = loadtxt('data/q-table-frozen-lake-sarsa.csv', delimiter=',') | ||
|
||
env = gym.make('FrozenLake-v1', render_mode='human').env | ||
(state, _) = env.reset() | ||
epochs = 0 | ||
rewards = 0 | ||
done = False | ||
|
||
while not done: | ||
action = np.argmax(q_table[state]) | ||
print_action(action) | ||
state, reward, done, _, info = env.step(action) | ||
rewards += reward | ||
epochs += 1 | ||
|
||
print("\n") | ||
print("Timesteps taken: {}".format(epochs)) | ||
print("Rewards: {}".format(rewards)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import gymnasium as gym | ||
env = gym.make("FrozenLake-v1", render_mode='ansi', is_slippery=True).env | ||
|
||
# | ||
# is_slippery=False torna o ambiente deterministico | ||
# | ||
|
||
print(env.observation_space.n) | ||
print(env.action_space.n) | ||
|
||
# inicializando o ambiente | ||
state = env.reset() | ||
|
||
# imprimindo o ambiente | ||
print(env.render()) | ||
|
||
# eh possivel fazer env.render(mode='ansi') para imprimir no terminal | ||
|
||
print('Executando algumas acoes') | ||
|
||
print('\n\n') | ||
|
||
print('indo para baixo') | ||
state, reward, done, truncated, info = env.step(1) | ||
print(env.render()) | ||
print(reward, done) | ||
|
||
print('indo para baixo') | ||
state, reward, done, _, _ = env.step(1) | ||
print(env.render()) | ||
print(reward, done) | ||
|
||
print('indo para direita') | ||
state, reward, done, _, info = env.step(2) | ||
print(env.render()) | ||
print(reward, done) | ||
|
||
print('indo para direita') | ||
state, reward, done, _, info = env.step(2) | ||
print(env.render()) | ||
print(reward, done) | ||
|
||
print('indo para baixo') | ||
state, reward, done, _, info = env.step(1) | ||
print(env.render()) | ||
print(reward, done) | ||
|
||
print('indo para direita') | ||
state, reward, done, _, info = env.step(2) | ||
print(env.render()) | ||
print(reward, done) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Projeto intermediário 2024/2 | ||
# Projeto intermediário | ||
|
||
## Descrição | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Path planning em robótica | ||
|
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