-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first implementation of dfs, bfs, id, a*, ida*
- Loading branch information
0 parents
commit 397624e
Showing
7 changed files
with
503 additions
and
0 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,128 @@ | ||
.DS_Store | ||
.idea | ||
|
||
# Created by https://www.gitignore.io/api/python | ||
# Edit at https://www.gitignore.io/?templates=python | ||
|
||
### Python ### | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# celery beat schedule file | ||
celerybeat-schedule | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
### Python Patch ### | ||
.venv/ | ||
|
||
# End of https://www.gitignore.io/api/python |
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,8 @@ | ||
# Example Package | ||
|
||
pip install -e ~/Documents/PythonWorkspaces/AI/SEARCH/general_solution | ||
|
||
|
||
This is a simple example package. You can use | ||
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/) | ||
to write your content. |
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,22 @@ | ||
import setuptools | ||
|
||
with open("README.md", "r") as fh: | ||
long_description = fh.read() | ||
|
||
setuptools.setup( | ||
name="search-jbin", # Replace with your own username | ||
version="0.0.1", | ||
author="jbin", | ||
author_email="[email protected]", | ||
description="A library of searh algorithms" | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="", | ||
packages=setuptools.find_packages(), | ||
classifiers=[ | ||
"Programming Language :: Python :: 3", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
], | ||
python_requires='>=3.6', | ||
) |
Empty file.
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,124 @@ | ||
from data_structs import Node, StackFrontier, QueueFrontier | ||
|
||
|
||
def depth_first_search(**kwargs): | ||
state_space = kwargs.get('state_space', None) | ||
actions = kwargs.get('actions', None) | ||
start = kwargs.get('start', None) | ||
goal = kwargs.get('goal', None) | ||
show_explored = kwargs.get('show_explored', False) | ||
depth = kwargs.get('depth', None) #this is used only when dfs is called in iterative deepening to a certain depth | ||
|
||
#print("Depth: {}".format(depth)) | ||
|
||
num_explored = 0 | ||
|
||
if depth == None: | ||
start = Node(state=start, parent=None, action=None) | ||
else: #this is used only when dfs is called in iterative deepening to a certain depth | ||
start = Node(state=start, parent=None, action=None, enable_depth=True) | ||
|
||
frontier = StackFrontier() | ||
frontier.add(start) | ||
explored = set() | ||
print("dfs in pip") | ||
while True: | ||
|
||
if frontier.empty(): | ||
return None | ||
|
||
node = frontier.remove() | ||
print('In frontier') | ||
num_explored += 1 | ||
|
||
if node.state == goal: | ||
actions_to_goal = [] | ||
states_to_goal = [] | ||
while node.parent is not None: | ||
actions_to_goal.append(node.action) | ||
states_to_goal.append(node.state) | ||
node = node.parent | ||
actions_to_goal.reverse() | ||
states_to_goal.reverse() | ||
solution = (actions_to_goal, states_to_goal) | ||
print(solution) | ||
if show_explored: | ||
return solution, explored | ||
else: | ||
return solution | ||
#return solution | ||
|
||
|
||
explored.add(node.state) | ||
if depth == None: | ||
for action, state in actions(node.state): | ||
if not frontier.contains_state(state) and state not in explored: | ||
child = Node(state=state, parent=node, action=action) | ||
frontier.add(child) | ||
else: #this is used only when dfs is called in iterative deepening to a certain depth | ||
if node.depth <= depth: | ||
for action, state in actions(node.state): | ||
if not frontier.contains_state(state) and state not in explored: | ||
child = Node(state=state, parent=node, action=action, enable_depth=True) | ||
print("Child depth: {}".format(depth)) | ||
frontier.add(child) | ||
|
||
|
||
|
||
|
||
def breadth_first_search(**kwargs): | ||
state_space = kwargs.get('state_space', None) | ||
actions = kwargs.get('actions', None) | ||
start = kwargs.get('start', None) | ||
goal = kwargs.get('goal', None) | ||
|
||
num_explored = 0 | ||
start = Node(state=start, parent=None, action=None) | ||
frontier = QueueFrontier() | ||
frontier.add(start) | ||
explored = set() | ||
|
||
while True: | ||
|
||
if frontier.empty(): | ||
return None | ||
|
||
node = frontier.remove() | ||
#print('In frontier') | ||
num_explored += 1 | ||
|
||
if node.state == goal: | ||
actions_to_goal = [] | ||
states_to_goal = [] | ||
while node.parent is not None: | ||
actions_to_goal.append(node.action) | ||
states_to_goal.append(node.state) | ||
node = node.parent | ||
actions_to_goal.reverse() | ||
states_to_goal.reverse() | ||
solution = (actions_to_goal, states_to_goal) | ||
return solution | ||
|
||
explored.add(node.state) | ||
|
||
for action, state in actions(node.state): | ||
if not frontier.contains_state(state) and state not in explored: | ||
child = Node(state=state, parent=node, action=action) | ||
frontier.add(child) | ||
|
||
|
||
|
||
def iterative_deepening(**kwargs): | ||
state_space = kwargs.get('state_space', None) | ||
actions = kwargs.get('actions', None) | ||
start = kwargs.get('start', None) | ||
goal = kwargs.get('goal', None) | ||
show_explored = kwargs.get('show_explored', False) | ||
|
||
depth = 1 | ||
solution = None | ||
while solution == None: | ||
solution = depth_first_search(actions=actions, start=start, goal=goal, depth=depth, show_explored=show_explored) | ||
depth += 1 | ||
print(solution) | ||
return solution |
Oops, something went wrong.