Skip to content

Commit 00e29a6

Browse files
committed
2 parents 6e4f0c6 + 08f346f commit 00e29a6

20 files changed

+165849
-80
lines changed

.github/workflows/py_lint.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
# Checkout the code base #
3737
##########################
3838
- name: Checkout Code
39-
uses: actions/checkout@v2
39+
uses: actions/checkout@v3
4040
with:
4141
# Full git history is needed to get a proper list of changed files within `super-linter`
4242
fetch-depth: 0
@@ -45,7 +45,7 @@ jobs:
4545
# Run Linter against code base #
4646
################################
4747
- name: Lint Code Base
48-
uses: github/super-linter@v3
48+
uses: github/super-linter@v5
4949
env:
5050
VALIDATE_ALL_CODEBASE: false
5151
VALIDATE_PYTHON: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Jun 4 18:02:01 2021
4+
5+
@author: Ayush
6+
"""
7+
8+
import gym
9+
import numpy as np
10+
from IPython.display import clear_output
11+
12+
env = gym.make('Taxi-v3')
13+
14+
episodes = 10
15+
for episode in range(1,episodes):
16+
state = env.reset()
17+
done= False
18+
score=0
19+
20+
while not done:
21+
env.render()
22+
state,reward,done,info = env.step(env.action_space.sample())
23+
score += reward
24+
clear_output(wait=True)
25+
print('Episode: {}\nScore: {}'.format(episode, score))
26+
env.close()
27+
28+
#Creating Q-Table
29+
actions = env.action_space.n
30+
state = env.observation_space.n
31+
32+
q_table = np.zeros((state,actions))
33+
34+
#q_table.shape
35+
#q_table
36+
37+
#Parameters for Q-Learning
38+
num_episodes = 10000
39+
max_steps_per_episode =1000
40+
learning_rate=0.01
41+
discount_rate = 0.99
42+
exploration_rate=1
43+
max_exploration_rate = 1
44+
min_exploration_rate = 0.01
45+
exploration_decay_rate= 0.01
46+
47+
rewards_all_episodes = []
48+
49+
#Q-Learning Algorithm
50+
import random
51+
for episode in range(num_episodes):
52+
state = env.reset()
53+
done = False
54+
reward_current_episode = 0
55+
56+
for step in range(max_steps_per_episode):
57+
#Exploration vs Exploitation trade-off
58+
exploration_threshold = random.uniform(0,1)
59+
if exploration_threshold > exploration_rate:
60+
action = np.argmax(q_table[state,:])
61+
else:
62+
action = env.action_space.sample()
63+
new_state,reward,done,info = env.step(action)
64+
65+
#Update Q-Table
66+
q_table[state,action] = q_table[state,action]*(1-learning_rate)+ learning_rate*(reward + discount_rate * np.max(q_table[new_state, :]))
67+
state=new_state
68+
reward_current_episode += reward
69+
70+
if done== True:
71+
break
72+
exploration_rate = min_exploration_rate + \
73+
(max_exploration_rate- min_exploration_rate) * np.exp(-exploration_decay_rate * episode)
74+
rewards_all_episodes.append(reward_current_episode)
75+
print("***** Training Finished *****")
76+
77+
q_table
78+
79+
#Calculate and print average reward per thousand episodes
80+
rewards_per_thousand_episodes = np.split(np.array(rewards_all_episodes), num_episodes/1000)
81+
count = 1000
82+
83+
print("Average per thousand episodes")
84+
85+
for r in rewards_per_thousand_episodes:
86+
print(count, ":", str(sum(r/1000)))
87+
count+=1000
88+
89+
# Visualize Agent
90+
import time
91+
for episode in range(3):
92+
status = env.reset()
93+
done = False
94+
print("Episode is: "+ str(episode))
95+
time.sleep(1)
96+
97+
for step in range(max_steps_per_episode):
98+
clear_output(wait=True)
99+
env.render()
100+
time.sleep(.4)
101+
102+
action = np.argmax(q_table[state,:])
103+
104+
new_state, reward, done, info = env.step(action)
105+
106+
if done:
107+
clear_output(wait=True)
108+
env.render()
109+
if reward == 1:
110+
print("****Reached Goal****")
111+
time.sleep(2)
112+
clear_output(wait=True)
113+
else:
114+
print("****Failed****")
115+
time.sleep(2)
116+
clear_output(wait=True)
117+
118+
break
119+
state=new_state
120+
env.close()
121+
122+
123+

Autonomous-Taxi-Agent/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Q-Learning with Taxi-v3 Environment
2+
3+
This repository contains code for implementing the Q-Learning algorithm using the Taxi-v3 environment from the OpenAI Gym.
4+
5+
## Prerequisites
6+
7+
To run this code, you need the following dependencies:
8+
9+
- Python 3.x
10+
- Gym: `pip install gym`
11+
- NumPy: `pip install numpy`
12+
13+
## Getting Started
14+
15+
1. Clone the repository: `git clone https://github.com/your_username/your_repository.git`
16+
2. Navigate to the cloned repository: `cd your_repository`
17+
18+
## Running the Code
19+
20+
1. Open the Python script `q_learning_taxi.py`.
21+
2. Configure the number of episodes, learning parameters, and other settings as needed.
22+
3. Run the script: `python q_learning_taxi.py`.
23+
24+
## Understanding the Code
25+
26+
The code performs the following steps:
27+
28+
1. Imports the necessary libraries and initializes the Taxi-v3 environment.
29+
2. Runs a specified number of episodes, where each episode represents a learning iteration.
30+
3. Resets the environment for each episode and plays the game until completion.
31+
4. Renders the environment to visualize the game.
32+
5. Selects actions randomly for exploration or based on the learned Q-values for exploitation.
33+
6. Updates the Q-table based on the Q-Learning algorithm.
34+
7. Adjusts the exploration rate over time to balance exploration and exploitation.
35+
8. Stores the rewards obtained in each episode.
36+
9. Prints the Q-table after training.
37+
10. Calculates and prints the average reward per thousand episodes.
38+
11. Visualizes the agent's performance in a few test episodes.
39+
40+
41+
42+
## Acknowledgments
43+
44+
- [OpenAI Gym](https://gym.openai.com/)
45+
46+
Feel free to modify and adapt this code according to your needs.
47+

Lane Detection/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# lane_detection
2+
This project uses OpenCV to detect the lanes on the road.

Lane Detection/laneDetection.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import cv2
2+
import numpy as np
3+
4+
def canny(img):
5+
if img is None:
6+
cap.release()
7+
cv2.destroyAllWindows()
8+
exit()
9+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
10+
kernel = 5
11+
blur = cv2.GaussianBlur(gray,(kernel, kernel),0)
12+
canny = cv2.Canny(gray, 50, 150)
13+
return canny
14+
15+
def region_of_interest(canny):
16+
height = canny.shape[0]
17+
width = canny.shape[1]
18+
mask = np.zeros_like(canny)
19+
triangle = np.array([[
20+
(200, height),
21+
(800, 350),
22+
(1200, height),]], np.int32)
23+
cv2.fillPoly(mask, triangle, 255)
24+
masked_image = cv2.bitwise_and(canny, mask)
25+
return masked_image
26+
27+
def houghLines(cropped_canny):
28+
return cv2.HoughLinesP(cropped_canny, 2, np.pi/180, 100,
29+
np.array([]), minLineLength=40, maxLineGap=5)
30+
31+
def addWeighted(frame, line_image):
32+
return cv2.addWeighted(frame, 0.8, line_image, 1, 1)
33+
34+
def display_lines(img,lines):
35+
line_image = np.zeros_like(img)
36+
if lines is not None:
37+
for line in lines:
38+
for x1, y1, x2, y2 in line:
39+
cv2.line(line_image,(x1,y1),(x2,y2),(0,0,255),10)
40+
return line_image
41+
42+
def make_points(image, line):
43+
slope, intercept = line
44+
y1 = int(image.shape[0])
45+
y2 = int(y1*3.0/5)
46+
x1 = int((y1 - intercept)/slope)
47+
x2 = int((y2 - intercept)/slope)
48+
return [[x1, y1, x2, y2]]
49+
50+
def average_slope_intercept(image, lines):
51+
left_fit = []
52+
right_fit = []
53+
if lines is None:
54+
return None
55+
for line in lines:
56+
for x1, y1, x2, y2 in line:
57+
fit = np.polyfit((x1,x2), (y1,y2), 1)
58+
slope = fit[0]
59+
intercept = fit[1]
60+
if slope < 0:
61+
left_fit.append((slope, intercept))
62+
else:
63+
right_fit.append((slope, intercept))
64+
left_fit_average = np.average(left_fit, axis=0)
65+
right_fit_average = np.average(right_fit, axis=0)
66+
left_line = make_points(image, left_fit_average)
67+
right_line = make_points(image, right_fit_average)
68+
averaged_lines = [left_line, right_line]
69+
return averaged_lines
70+
71+
cap = cv2.VideoCapture("test1.mp4")
72+
while(cap.isOpened()):
73+
_, frame = cap.read()
74+
canny_image = canny(frame)
75+
cropped_canny = region_of_interest(canny_image)
76+
# cv2.imshow("cropped_canny",cropped_canny)
77+
78+
lines = houghLines(cropped_canny)
79+
averaged_lines = average_slope_intercept(frame, lines)
80+
line_image = display_lines(frame, averaged_lines)
81+
combo_image = addWeighted(frame, line_image)
82+
cv2.imshow("result", combo_image)
83+
84+
if cv2.waitKey(1) & 0xFF == ord('q'):
85+
break
86+
87+
cap.release()
88+
cv2.destroyAllWindows()
89+

Lane Detection/test1.mp4

7.61 MB
Binary file not shown.

0 commit comments

Comments
 (0)