Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14 adding time dependence to reward function #29

Merged
merged 28 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
__pycache__
Q-Learning.iml
misc.xml
modules.xml
vcs.xml
src/qlearning.egg-info
.coverage
.tox
.DS_Store
.pytest_cache
.mypy_cache
.vscode
build
build
2 changes: 1 addition & 1 deletion src/qlearning/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import smallExampleQAgent
from src.qlearning import smallExampleQAgent
import numpy as np

if __name__ == "__main__":
Expand Down
36 changes: 25 additions & 11 deletions src/qlearning/qagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from abc import abstractmethod


class QAgent():
class QAgent:
@abstractmethod
def getPlayableActions(self, currentState, differentials, timestep):
print("AAAAA")
Expand All @@ -28,24 +28,38 @@ def qlearning(self, rewards_new, iterations, end_state):
if current_state == end_state:
continue
playable_actions = self.getPlayableActions(
current_state, self.differentials, self.dt)
temporal_difference = rewards_new[current_state] + self.gamma * \
np.amax(self.q[playable_actions]) - \
self.q[current_state]
self.q[current_state] += self.alpha * \
temporal_difference
current_state, self.differentials, self.dt
)
temporal_difference = (
rewards_new[current_state]
+ self.gamma * np.amax(self.q[playable_actions])
- self.q[current_state]
)
self.q[current_state] += self.alpha * temporal_difference

def reset_matrix(self, rewards_new, iterations, end_state, dimensions):
shape = tuple([len(self.q)] * dimensions)
self.q: np.ndarray = np.zeros(shape)
QAgent.qlearning(self, rewards_new, iterations, end_state)

def alter_matrix(self, rewards_new, iterations, end_state, scale):
rewards_new = rewards_new * scale
QAgent.qlearning(self, rewards_new, iterations, end_state)

def get_optimal_route(self, start_state, end_state):
route = [start_state]
next_state = start_state
while next_state != end_state:
playable_actions = self.getPlayableActions(
next_state, self.differentials, self.dt)
next_state, self.differentials, self.dt
)
t1 = self.q[playable_actions]
t2 = np.argmax(self.q[playable_actions])
t3 = playable_actions[0]
next_state = playable_actions[0][np.argmax(
self.q[playable_actions])]
next_state = playable_actions[0][np.argmax(self.q[playable_actions])]
if next_state in route:
route.append(next_state)
break
route.append(next_state)

return route
Expand All @@ -62,4 +76,4 @@ def __init__(self, alpha, gamma, rewards, dt):
self.alpha = alpha
self.rewards = rewards
self.q, self.differentials = self.getStateMatrix()
self.dt = dt
self.dt = dt
26 changes: 15 additions & 11 deletions src/qlearning/smallExampleQAgent.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
from qagent import QAgent
from src.qlearning import qagent
import numpy as np


class SmallExampleQAgent(QAgent):
class SmallExampleQAgent(qagent.QAgent):
def __init__(self, alpha, gamma, rewards):
super().__init__(alpha, gamma, rewards, 1)

def getPlayableActions(self, currentState, differentials, timestep):
playableActions = np.array([[0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 0]])
playableActions = np.array(
[
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 0],
]
)

return np.nonzero(playableActions[currentState])

Expand Down
Binary file added state_prediction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 116 additions & 11 deletions tests/testingfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,122 @@
"""
dummy test case just to see if testing configurations work
"""
import unittest
import numpy as np
from src.qlearning import smallExampleQAgent

# Example import
from src.qlearning.qagent import QAgent

def dummy_test():
assert 1 > 0
class TestQAgent(unittest.TestCase):

def setUp(self):
self.alpha = 0.1
self.gamma = 0.9
self.rewards = np.zeros((9,))
self.agent = smallExampleQAgent.SmallExampleQAgent(
self.alpha, self.gamma, self.rewards
)

def dummy_test2():
assert 2 != 0
def test_q_learning(self):
iterations = 1000
end_state = 8

self.agent.qlearning(self.rewards, iterations, end_state)

def test_q_learning_2(self):
iterations = 0
end_state = 0

self.agent.qlearning(self.rewards, iterations, end_state)

def test_q_learning_3(self):
rewards = np.random.rand(
9,
)
iterations = 1000
end_state = 5

self.agent.qlearning(rewards, iterations, end_state)

def test_reset_matrix(self):
iterations = 1000
end_state = 8
dimensions = 1

self.agent.reset_matrix(self.rewards, iterations, end_state, dimensions)
self.assertFalse(np.any(self.agent.q))

def test_reset_matrix_2(self):
rewards = np.random.rand(
9,
)
iterations = 1000
end_state = 8
dimensions = 1

self.agent.reset_matrix(rewards, iterations, end_state, dimensions)
self.assertTrue(np.any(self.agent.q))

def test_reset_matrix_3(self):
rewards = np.random.rand(
9,
)
iterations = 1000
end_state = 1
dimensions = 1

self.agent.reset_matrix(rewards, iterations, end_state, dimensions)
self.assertTrue(np.any(self.agent.q))

def test_alter_matrix(self):
iterations = 1000
end_state = 8
scale = 0.5

self.agent.alter_matrix(self.rewards, iterations, end_state, scale)
self.assertFalse(np.any(self.agent.q))

def test_alter_matrix_2(self):
rewards = np.random.rand(
9,
)
iterations = 1000
end_state = 8
scale = 0.5

self.agent.alter_matrix(rewards, iterations, end_state, scale)
self.assertTrue(np.any(self.agent.q))

def test_alter_matrix_3(self):
rewards = np.random.rand(
9,
)
iterations = 1000
end_state = 5
scale = 0.1

self.agent.alter_matrix(rewards, iterations, end_state, scale)
self.assertTrue(np.any(self.agent.q))

def test_training(self):
iterations = 1000
start_state = 0
end_state = 8

route = self.agent.training(start_state, end_state, iterations)
print(route)

def test_training2(self):
iterations = 1000
start_state = 1
end_state = 5

route = self.agent.training(start_state, end_state, iterations)
print(route)

def test_get_optimal_route(self):
start_state = 0
end_state = 8
route = self.agent.get_optimal_route(start_state, end_state)
self.assertTrue(len(route) > 0, "The route is empty.")
print(f"Route: {route}")


if __name__ == "__main__":
dummy_test()
dummy_test2()
unittest.main()
Loading