AIES LAB PROGRAM (1) (2) .Docx 20240926 204425 0000
AIES LAB PROGRAM (1) (2) .Docx 20240926 204425 0000
global N
N=4
def printSolution(board):
for i in range(N):
for j in range(N):
print()
for i in range(col):
if board[row][i] == 1:
return False
if board[i][j] == 1:
return False
if board[i][j] == 1:
return False
return True
if col >= N:
return True
for i in range(N):
if isSafe(board, i, col):
board[i][col] = 1
return True
board[i][col] = 0
return False
def solveNQ():
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
if solveNQUtil(board, 0) == False:
return False
printSolution(board)
return True
solveNQ()
OUTPUT
0010
1000
0001
0100
PROGRAM
v = 14
visited = [False] * n
pq = PriorityQueue()
pq.put((0, actual_Src))
visited[actual_Src] = True
u = pq.get()[1]
if u == target:
break
for v, c in graph[u]:
if visited[v] == False:
visited[v] = True
pq.put((c, v))
print()
graph[x].append((y, cost))
graph[y].append((x, cost))
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 13, 2)
source = 0
target = 9
best_first_search(source, target, v)
OUTPUT:
013289
PROGRAM
def TowerOfHanoi(n , source, destination, auxiliary): if n==1: print ("Move disk 1 from
source",source,"to destination",destination) return TowerOfHanoi(n-1, source, auxiliary,
destination) print ("Move disk",n,"from source",source,"to destination",destination) TowerOfHanoi(n-
1, auxiliary, destination, source)n = 4TowerOfHanoi(n,'A','B','C')
OUTPUT
Move disk 1 from source A to destination CMove disk 2 from source A to destination BMove disk 1 from
source C to destination BMove disk 3 from source A to destination CMove disk 1 from source B to
destination AMove disk 2 from source B to destination CMove disk 1 from source A to destination
CMove disk 4 from source A to destination BMove disk 1 from source C to destination BMove disk 2
from source C to destination AMove disk 1 from source B to destination AMove disk 3 from source C to
destination BMove disk 1 from source A to destination CMove disk 2 from source A to destination
BMove disk 1 from source C to destination B
PROGRAM
def waterJugSolver(amt1, amt2): if (amt1 == aim and amt2 == 0) or (amt2 == aim and amt1 == 0):
print(amt1, amt2) return True if visited[(amt1, amt2)] == False: print(amt1, amt2)
visited[(amt1, amt2)] = True return (waterJugSolver(0, amt2) or waterJugSolver(amt1, 0) or
waterJugSolver(jug1, amt2) or waterJugSolver(amt1, jug2) or
waterJugSolver(amt1 + min(amt2, (jug1-amt1)), amt2 - min(amt2, (jug1-amt1))) or
waterJugSolver(amt1 - min(amt1, (jug2-amt2)), amt2 + min(amt1, (jug2-amt2)))) else:
return Falseprint("Steps: ")waterJugSolver(0, 0)
OUTPUT
Steps:0 04 04 30 33 03 34 20 2
PROGRAM
open_set = set(start_node)
closed_set = set()
g = {}
parents = {}
g[start_node] = 0
parents[start_node] = start_node
while len(open_set) > 0:
n = None
for v in open_set:
n=v
pass
else:
open_set.add(m)
parents[m] = n
else:
parents[m] = n
if m in closed_set:
closed_set.remove(m)
open_set.add(m)
if n == None:
return None
if n == stop_node:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
return path
open_set.remove(n)
closed_set.add(n)
return None
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None
def heuristic(n):
H_dist = {
'A': 11,
'B': 6,
'C': 5,
'D': 7,
'E': 3,
'F': 6,
'G': 5,
'H': 3,
'I': 1,
'J': 0
return H_dist[n]
Graph_nodes = {
aStarAlgo('A', 'J')
OUTPUT
import math
class GameState:
self.board = board
self.player_turn = player_turn
def print_board(self):
print(' | '.join(cell if cell != '' else ' ' for cell in row))
print('-' * 5)
def evaluate(self):
win_conditions = [
return 10
return -10
return 0
return 0
def generate_possible_moves(self):
moves = []
for i in range(3):
for j in range(3):
if self.board[i][j] == '':
return moves
def is_terminal(self):
return self.evaluate() != 0 or all(cell != '' for row in self.board for cell in row)
def minimax(state, depth, alpha, beta, maximizing_player):
if state.is_terminal() or depth == 0:
return state.evaluate()
if maximizing_player:
max_eval = -math.inf
break
return max_eval
else:
min_eval = math.inf
break
return min_eval
best_move = None
for move in state.generate_possible_moves():
if (state.player_turn and eval > best_eval) or (not state.player_turn and eval < best_eval):
best_eval = eval
best_move = move
return best_move
current_board = [
initial_state.print_board()
depth = 3
if move:
else:
OUTPUT
X| |
-----
| |
-----
| |
-----
import random
def generate_initial_state():
def heuristic(state):
conflicts = 0
for i in range(8):
conflicts += 1
return conflicts
def generate_successors(state):
successors = []
if col != state[row]:
new_state = state[:]
new_state[row] = col
successors.append(new_state)
return successors
def first_choice_hill_climbing(max_iterations=1000):
iteration = 0
current_state = generate_initial_state()
current_heuristic = heuristic(current_state)
successors = generate_successors(current_state)
next_heuristic = heuristic(next_state)
break
current_state = next_state
current_heuristic = next_heuristic
if current_heuristic == 0:
return current_state
iteration += 1
return None
def print_state(state):
board[state[row]][row] = '1'
print(' '.join(row))
print()
solution = first_choice_hill_climbing(max_iterations=5000)
if solution:
print("Solution found:")
print_state(solution)
else:
Solution found:
10000000
00000010
00001000
00000001
01000000
00010000
00000100
00100000
PROGRAM
return False
return True
if len(assignment) == len(regions):
return assignment
assignment[region] = color
if result:
return result
del assignment[region]
return None
return None
assignment = {}
if solution:
else:
adjacency_list = {
print_solution(solution)
OUTPUT
import random
word = random.choice(words)
guesses = ''
turns = 12
failed = 0
for char in word:
if char in guesses:
else:
failed += 1
print()
if failed == 0:
print("You Win!")
break
guesses += guess
turns -= 1
print("Wrong!")
if turns == 0:
print("You Lose")
guess a character:g
Wrong
_
_
guess a character:r
guess a character:t
Wrong
guess a character:p
p_
guess a character:l
pl_
guess a character:y
pl_
y_
guess a character:a
play_
guess a character:e
player
You Win
import numpy as np
import random
def create_board():
[0, 0, 0],
[0, 0, 0]])
def possibilities(board):
l = []
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return l
selection = possibilities(board)
current_loc = random.choice(selection)
board[current_loc] = player
return board
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[x, y] != player:
win = False
break
if win:
return True
return False
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[y, x] != player:
win = False
break
if win:
return True
return False
win = True
for x in range(len(board)):
if board[x, x] != player:
win = False
break
if win:
return True
win = True
for x in range(len(board)):
y = len(board) - 1 - x
if board[x, y] != player:
win = False
break
return win
def evaluate(board):
winner = 0
winner = player
break
def play_game():
board = create_board()
winner = 0
counter = 1
print("Starting board:")
print(board)
sleep(2)
while winner == 0:
print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return winner
winner = play_game()
if winner == -1:
else:
[[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 1]
[0 0 0]]
[[0 0 0]
[0 0 1]
[0 0 2]]
[[0 0 0]
[1 0 1]
[0 0 2]]
[[0 0 0]
[1 2 1]
[0 0 2]]
Board after 5 move
[[0 0 0]
[1 2 1]
[0 1 2]]
[[0 0 2]
[1 2 1]
[0 1 2]]
[[1 0 2]
[1 2 1]
[0 1 2]]
[[1 0 2]
[1 2 1]
[2 1 2]]
Winner is: 2
PROGRAM:
if substitution is None:
substitution = {}
if term1 == term2:
return substitution
if is_variable(term1):
if is_variable(term2):
if term1[0] != term2[0]:
return None
if len(term1) != len(term2):
return None
if substitution is None:
return None
return substitution
return None
def is_variable(term):
def is_compound(term):
return isinstance(term, list) and len(term) > 0
if var in substitution:
if value in substitution:
return None
substitution[var] = value
return substitution
if var == term:
return True
if is_variable(term):
if is_compound(term):
return False
term1 = ['king','x']
term2 = ['king','john']
print("Substitution:", substitution)
OUTPUT:
Substitution: {'x': 'john'}
PROGRAM:
derived_facts = set(facts)
changes = True
while changes:
changes = False
derived_facts.add(consequent)
changes = True
return derived_facts
if goal in facts:
return True
return False
if consequent == goal:
if all(backward_chaining(a, rules, facts, depth + 1) for a in antecedent):
return True
return False
rules = [
(['A'], 'B'),
(['D'], 'E')
goal = 'E'
diagnoses = set()
changes = True
while changes:
changes = False
diagnoses.add(consequent)
changes = True
return diagnoses
def get_user_symptoms():
return symptoms
def print_diagnosis(diagnoses):
if diagnoses:
print("\nPossible diagnoses:")
print(f"- {diagnosis}")
else:
rules = [
(['severe memory loss', 'difficulty with daily tasks'], 'Severe Cognitive Impairment')
]
user_symptoms = get_user_symptoms()
print_diagnosis(possible_diagnoses)
OUTPUT:
Possible diagnoses:
- Alzheimer's Disease
PROGRAM:
def get_user_symptoms():
symptoms = {
'fever': None,
'cough': None,
'fatigue': None,
'headache': None,
'chills': None,
'nausea': None,
'vomiting': None,
'diarrhea': None
while True:
break
else:
return symptoms
def diagnose_influenza(symptoms):
if (
symptoms['fever'] and
symptoms['cough'] and
symptoms['fatigue'] and
symptoms['headache'] and
symptoms['chills'] and
not symptoms['diarrhea']
):
else:
return "Based on your symptoms, it is less likely that you have influenza.
def main():
user_symptoms = get_user_symptoms()
diagnosis = diagnose_influenza(user_symptoms)
print(diagnosis)
if __name__ == "__main__":
main()
OUTPUT:
import pandas as pd
import numpy as np
demographic_parity_difference,
equalized_odds_difference,
selection_rate,
np.random.seed(42)
data_size = 1000
data = {
df = pd.DataFrame(data)
y = df['loan_approved']
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f"Accuracy: {accuracy:.2f}")
Accuracy:0.75
Odds Difference:0.08
Intercept 0.17
Prediction_local{0.55 0.45]
Right :0