Skip to content

Commit

Permalink
first implementation of dfs, bfs, id, a*, ida*
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnbin89 committed Oct 12, 2020
0 parents commit 397624e
Show file tree
Hide file tree
Showing 7 changed files with 503 additions and 0 deletions.
128 changes: 128 additions & 0 deletions .gitignore
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
8 changes: 8 additions & 0 deletions README.md
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.
22 changes: 22 additions & 0 deletions setup.py
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 added src/__init__.py
Empty file.
124 changes: 124 additions & 0 deletions src/blind_search.py
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
Loading

0 comments on commit 397624e

Please sign in to comment.