Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Missionaries and Cannibals Problem Solution with BFS and DFS Implementations #909

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

UditNayak
Copy link

Issue: #500

Issue Name: Request to get Community Solution for Exercise 'ex_6' in '3-Solving-Problems-By-Searching'

Description:

This PR addresses the solution for Exercise 'ex_6' in the '3-Solving-Problems-By-Searching' section. The code provides a solution to the classic Missionaries and Cannibals problem using both Breadth-First Search (BFS) and Depth-First Search (DFS) strategies. The implementation involves creating a structure to represent the state of the problem and defining methods to handle state transitions and validity checks.

Changes Made:

  1. Problem State Representation:

    • Introduced RiverSide class to represent the state of missionaries and cannibals on each side of the river.
    • Added methods to handle state transitions (__add__, __sub__), validity checks (isValid), and possible actions (action).
  2. Custom Graph-Search BFS Implementation:

    • Implemented a custom BFS method in the Mc class to solve the problem.
    • Utilized the Queue module to manage the frontier in the BFS search.
    • Added a solve method to run the BFS and check for the solution.
  3. DFS Integration:

    • Integrated DFS using the depth_first_graph_search method from the provided search module.
    • Added a solve2 method to run the DFS and check for the solution.
  4. Action and Result Handling:

    • Defined actions method in the Mc class to generate possible actions from the current state.
    • Defined result method to compute the resulting state after applying an action.
  5. Goal Testing:

    • Implemented goal_test method to check if the current state matches the goal state.

Testing:

  • Provided a main function to test the problem and verify the solution using both BFS and DFS methods.

Notes:

  • The custom BFS implementation was derived independently before discovering the general implementation available in the aima-python repository.
  • The solution ensures that state transitions are valid and adheres to the problem constraints.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant