0% found this document useful (0 votes)
9 views3 pages

Ai Prolog - Algo

The document outlines the implementation of various AI-based problems using SWI-Prolog, focusing on logic programming techniques. It includes algorithms for family relations, basic arithmetic, factorial, Fibonacci, Tower of Hanoi, 8-puzzle solver, N-Queens problem, Traveling Salesman problem, and Water Jug problem. Each algorithm is designed to produce expected outputs effectively, demonstrating successful application of Prolog programming.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Ai Prolog - Algo

The document outlines the implementation of various AI-based problems using SWI-Prolog, focusing on logic programming techniques. It includes algorithms for family relations, basic arithmetic, factorial, Fibonacci, Tower of Hanoi, 8-puzzle solver, N-Queens problem, Traveling Salesman problem, and Water Jug problem. Each algorithm is designed to produce expected outputs effectively, demonstrating successful application of Prolog programming.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Aim:

To implement and solve various AI-based problems using SWI-Prolog, demonstrating logic
programming through family relations, arithmetic, puzzles, optimization, and search
problems.

Individual Algorithms

1. Family Facts & Queries

1. Define parent/2 facts for family relations.

2. Define male/1 and female/1 for gender.

3. Create rules: father/2 and mother/2.

4. Define grandparent/2 via two parent relations.

5. Create sibling/2 if they share a parent and are not the same.

6. Query relationships using defined rules.

2a. Basic Arithmetic

1. Define add/3 to perform X + Y.

2. Define subtract/3 to perform X - Y.

3. Define multiply/3 to perform X * Y.

4. Define divide/3, checking Y ≠ 0.

5. Call respective predicates for operations.

6. Display the result.

2b. Factorial

1. Base case: factorial(0, 1).

2. Recursive case: N > 0.

3. Decrease N by 1 (N1).

4. Call factorial(N1, R1).

5. Multiply: Result = N * R1.

6. Return Result.
2c. Fibonacci

1. Base cases: fibonacci(0, 0) and fibonacci(1, 1).

2. For N > 1, compute N1 = N - 1 and N2 = N - 2.

3. Get F(N1) = R1 and F(N2) = R2.

4. Sum: Result = R1 + R2.

5. Return Result.

6. Use recursion until base cases.

3. Tower of Hanoi

1. Base: move 1 disk from Source to Destination.

2. For N > 1, compute M = N - 1.

3. Move M disks to Auxiliary.

4. Move 1 disk to Destination.

5. Move M disks from Auxiliary to Destination.

6. Repeat recursively.

4. 8-Puzzle Solver

1. Define goal state list.

2. Create move rules using swap positions.

3. Replace tiles based on zero’s index.

4. Use bfs/3 for state exploration.

5. Track visited states.

6. Return path when goal is found.

5. N-Queens Problem

1. Generate list 1 to N (range).

2. Use permutation/2 to arrange queens.


3. Check for safe placement using safe/1.

4. Ensure no two queens share diagonal.

5. Recursively validate positions.

6. Output a valid arrangement.

6. Traveling Salesman Problem

1. Define distance edges between cities.

2. Get all city permutations.

3. Form complete tour by appending start.

4. Calculate cost of path.

5. Store each (Path, Cost).

6. Find path with minimum cost.

7. Water Jug Problem

1. Define valid jug operations (fill, empty, pour).

2. Specify goal state (e.g., 2 liters in Jug 1).

3. Implement move/3 for all actions.

4. Use BFS to explore states.

5. Track visited and avoid cycles.

6. Return path to goal.

Result:

Each Prolog program using SWI-Prolog, producing the expected outputs such as family
relationship queries, computed values, solved puzzles, or optimized solutions was
implemented successfully.

You might also like