-
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.
added multiple enemy feature to env and solved discrete env.
- Loading branch information
Showing
2 changed files
with
249 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Solution to custom environment" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from myenv import ENVIRONMENT\n", | ||
"import numpy as np\n", | ||
"import torch\n", | ||
"import cv2\n", | ||
"import random" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"HM_EPISODES = 25000\n", | ||
"MOVE_PENALTY = 1\n", | ||
"ENEMY_PENALTY = 300\n", | ||
"FOOD_REWARD = 25\n", | ||
"epsilon = 0.9\n", | ||
"EPS_DECAY = 0.9998 \n", | ||
"SHOW_EVERY = 1000 \n", | ||
"DISPLAY_EVERY= 500\n", | ||
"SIZE = 10\n", | ||
"LEARNING_RATE = 0.1\n", | ||
"DISCOUNT = 0.95\n", | ||
"total_reward = 0" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_q_table(start_q_table=None,size=10,action=4):\n", | ||
" \n", | ||
" if start_q_table is None:\n", | ||
" q_table = np.random.randn(size,size,action)\n", | ||
" print(q_table.size)\n", | ||
"\n", | ||
" else:\n", | ||
" with open(start_q_table, \"rb\") as f:\n", | ||
" q_table = pickle.load(f)\n", | ||
" \n", | ||
" return q_table\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"env = ENVIRONMENT(diagonal=False,size=10,num_enemy = 3, num_food = 1)\n", | ||
"q = get_q_table(size=10)\n", | ||
"# Test Environmet by rendering once\n", | ||
"\n", | ||
"print(env.startover())\n", | ||
"\n", | ||
"for i in range(100):\n", | ||
" print(env.step(np.random.randint(0,4)))\n", | ||
" env.render()\n", | ||
" cv2.waitKey(100)\n", | ||
"cv2.destroyAllWindows()\n", | ||
"\n", | ||
"print(env.startover())\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# for i in range(1):\n", | ||
"# print(env.step(2))\n", | ||
"# env.render()\n", | ||
"# cv2.waitKey(0) \n", | ||
"# cv2.destroyAllWindows()\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Improve q-value lookup table\n", | ||
"for episode in range(HM_EPISODES):\n", | ||
" \n", | ||
" state, reward, done = env.startover(newpos=True)\n", | ||
" \n", | ||
" while not done:\n", | ||
" \n", | ||
" current_q = q[state[0],state[1],:]\n", | ||
" \n", | ||
" if random.random() > epsilon:\n", | ||
" \n", | ||
" action = np.argmax(current_q)\n", | ||
"\n", | ||
" else:\n", | ||
" action = np.random.randint(0,4)\n", | ||
"\n", | ||
" next_state, (next_reward, done) = env.step(action)\n", | ||
" total_reward += next_reward\n", | ||
" future_q = q[next_state[0],next_state[1],:]\n", | ||
" q[state[0],state[1],action] = (1 - LEARNING_RATE) * current_q[action] + LEARNING_RATE * ( next_reward + DISCOUNT * max(future_q) - current_q[action])\n", | ||
"\n", | ||
" if done and next_reward == 100:\n", | ||
" q[state[0],state[1] :] = 0\n", | ||
"\n", | ||
"# if done and next_reward == -100:\n", | ||
"# print('Hell i fucked!')\n", | ||
" \n", | ||
" if episode%SHOW_EVERY == 0:\n", | ||
" env.render()\n", | ||
" cv2.waitKey(100)\n", | ||
" \n", | ||
" state = next_state\n", | ||
" \n", | ||
" cv2.destroyAllWindows()\n", | ||
" epsilon *= EPS_DECAY\n", | ||
" \n", | ||
" if episode%DISPLAY_EVERY == 0:\n", | ||
" print('Episode: ',episode,'state:',state,'| Total Average Reward:', total_reward/500,'| Epsilon:', epsilon)\n", | ||
" total_reward= 0" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"## TESTING\n", | ||
"# import time\n", | ||
"# with open(f\"qtable-{int(time.time())}.pickle\", \"wb\") as f:\n", | ||
"# pickle.dump(q_table, f)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# from myenv import ENVIRONMENT\n", | ||
"# import cv2 \n", | ||
"# env = ENVIRONMENT(num_enemy = 3, size= 20)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# env.render()\n", | ||
"# cv2.waitKey(0)\n", | ||
"# cv2.destroyAllWindows()\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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