SearchProblemsAI is a library of AI agents for Search Problems.
Project description
AI-Agents-for-Search-Problems
A search problem is defined as a graph of node (states) and weighted edges (action leading from one state to another and costing a cost) where the goal is to find a certain goal state while minimizing the cost if possible. A good example is the pathfinding problem where your agent want to find the shortest path to a certain location. These problems can be partially solved using easy to implement graph algorithms adapted to the model-free nature of those problems (we don't know every states but rather discover them during the exploration of the state space).
Package SearchProblemsAI
An implementation in python of some algorithms for search problems such as A*, that can be applied to any problem object that follow the SearchProblem interface.
The main feature is to define relatively quickly your concrete search problem as a subclass of the interface SearchProblem in order to solve it using already implemented algorithms. Some of those algorithms including BFS, DFS, Uniform Cost Search (Dijkstra ) and A* are already implemented.
Installation :
Run this command to install the package SearchProblemsAI:
pip install SearchProblemsAI
Using the package
An example of how to use those algorithms once you have defined your SearchProblem can be found in example/navigation.py or here :
from SearchProblemsAI.search_problems import FindCandyProblem
from SearchProblemsAI.search_algorithms import DFS, BFS, UCS, A_star
from SearchProblemsAI.utils import manhattan_distance
#Define problem
problem = FindCandyProblem(side_lenght=10)
print("Start state:")
print(problem.get_start_state())
#Define algorithm solving it, solve it
print("Solving...")
def h(state):
return manhattan_distance(state.pos, problem.goal_pos)
list_of_actions = A_star(heuristic = h).solve(problem)
#Test the solution
print("\nTesting solution :", list_of_actions)
problem.apply_solution(list_of_actions)
The search problems have to be deterministic : one action in one state always leads to exactly one state.
Define your search problem
from SearchProblemsAI.SearchProblem import State, SearchProblem
class YourState(State):
pass
class YourSearchProblem(SearchProblem):
pass
You need to first define your state class that must inherit the State class. A state represent a state of the problem with every information you are giving to the agent. Please implements the following methods, as well as the str method eventually :
__hash__() : a state should be hashable
__eq__(state : State) : two state should be comparable
Then you must define your search problem class. Every search problem class must inherit the class SearchProblem and implements the following methods :
get_start_state() : return the initial state
is_goal_state(state : State) : return whether the state is a goal state
get_actions(state : State) : return the list of actions available in a given state
get_transition(state : State, action : object) : return a tuple (next_state, cost_of_action) that represents the transition
A path finding problem can be found in search_problem/FindCandy.py as an example.
Define your search algorithm
Some algorithms are already implemented.
from SearchProblemsAI.search_algorithms import DFS_treeSearch, DFS, BFS, DepthLimitedDFS, IDDFS, UCS, A_star
But you can also define other search algorithms by inheriting the SearchAlgorithm class and implements the abstract required methods. The interface to respect is only to implement the method solve(problem : SearchProblem) that return a list of actions to solve the problem.
Solve your search problem
Once it is done, you can solve your problem by using method .solve() on an instance of your search problem class.
problem = YourSearchProblem(*args)
list_of_actions = DFS().solve(problem)
problem.apply_solution(list_of_actions)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file SearchProblemsAI-2.0.tar.gz
.
File metadata
- Download URL: SearchProblemsAI-2.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef532e6e83e44d37495627b14c71d49a867175032f1689bec3a7d1e94523c000 |
|
MD5 | b4283cb05d03cef60d9133359454b60b |
|
BLAKE2b-256 | d6492ea45a02b4606a56270cd81a22b2e4c07046c80b0a8b152f5a1f19cb1fa5 |
File details
Details for the file SearchProblemsAI-2.0-py3-none-any.whl
.
File metadata
- Download URL: SearchProblemsAI-2.0-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bd86aa47c02fd7054166c5125813a42dac948bac10a49664a69cc8d7e0d7fb7 |
|
MD5 | b5a91c2087a7d9d4ae2d8072423106ca |
|
BLAKE2b-256 | 490d6fec09bd07014b31b53f0940413453459e2c3a292195e85ee47410ec1184 |