Posts

Showing posts from November, 2024
 import numpy as np import pandas as pd  import csv from pgmpy.estimators import MaximumLikelihoodEstimator from pgmpy.models import BayesianModel from pgmpy.inference import VariableElimination heartDisease = pd.read_csv(r'C:/Users/lab5heart.csv') heartDisease = heartDisease.replace('?',np.nan) print('Sample instances from the dataset are given below') print(heartDisease.head()) print('\n Attributes and datatypes')  print(heartDisease.dtypes) model = BayesianModel([('age','heartdisease'),('sex','heartdisease'),('exang','heartdisease'),('cp','heartdisease'),('heartdisease', 'restecg'),('heartdisease','chol')]) print('\n Learning CPD using Maximum likelihood estimators') model.fit(heartDisease,estimator=MaximumLikelihoodEstimator) print('\n Inferencing with Bayesian Network:')  HeartDiseasetest_infer = VariableElimination(model) print('\n 1....
 SuccList ={'A':[('B',3),('C',2)], 'B':[('A',5),('C',2),('D',2),('E',3)], 'C':[('A',5),('B',3),('F',2),('G',4)], 'D':[('H',1),('I',99)], 'F': [('J',99)], 'G':[('K',99),('L',3)]} Start='A' Goal='E' Closed = list() SUCCESS=True FAILURE=False State=FAILURE def MOVEGEN(N):     New_list=list()     if N in SuccList.keys():         New_list=SuccList[N]     return New_list def GOALTEST(N):     if N == Goal:         return True     else:         return False def APPEND(L1,L2):     New_list=list(L1)+list(L2)     return New_list def SORT(L):     L.sort(key = lambda x: x[1])     return L def BestFirstSearch():     OPEN=[[Start,5]]     CLOSED=list()     global State     global Closed     while (len(OPEN) != 0) and (State != SUCCESS):         print("-------------")     ...
 def aStarAlgo(start_node, stop_node): 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: if n == None or g[v] + heuristic(v) <g[n] + heuristic(n): n=v if n stop_node or Graph_nodes[n] == None: pass else: for (m, weight) in get_neighbors(n): open_set.add(m) if m not in open_set and m not in closed_set: parents[m] = n g[m] = g[n] + weight else: if g[m]g[n] + weight: g[m] = g[n] + weight parents[m] = n if m in closed_set: closed_set.remove(m) open_set.add(m) if n == None: print('Path does not exist!") return None if n == stop_node: path = [] while parents[n] != n: path.append(n) n = parents[n] path.append(start_node) path.reverse() print('Path found: {}'.format(path)) return path open_set.remove(n) closed_set.add(n) print('Path does not exist!") return None def get_neighbors(v): if v in Graph_nodes: return Graph_nodes[v] else: return None de...
  Initial state: (0, 0) means both jugs are empty. % Goal state: (2, _), where the first jug has exactly 2 liters of water. % The capacity of the jugs capacity(4, 3). % Define possible actions: fill, empty, pour from one jug to another. % Fill the 4-liter jug action((X, Y), (4, Y)) :-     capacity(4, _),     X < 4. % Fill the 3-liter jug action((X, Y), (X, 3)) :-     capacity(_, 3),     Y < 3. % Empty the 4-liter jug action((X, Y), (0, Y)) :-     X > 0. % Empty the 3-liter jug action((X, Y), (X, 0)) :-     Y > 0. % Pour water from the 4-liter jug into the 3-liter jug action((X, Y), (NewX, NewY)) :-     capacity(_, MaxY),     X > 0,     Y < MaxY,     Transfer is min(X, MaxY - Y),     NewX is X - Transfer,     NewY is Y + Transfer. % Pour water from the 3-liter jug into the 4-liter jug action((X, Y), (NewX, NewY)) :-     capacity(MaxX, _)...
  move(1,X,Y,_):-     write('Move top disk from '),     write(X),     write(' to '),     write(Y),     nl. move(N,X,Y,Z) :-     N>1,     M is N-1,     move(M,X,Z,Y),     move(1,X,Y,_),     move(M,Z,Y,X). output move(3,source,dest,interm). Move top disk from source to dest Move top disk from source to interm Move top disk from dest to interm Move top disk from source to dest Move top disk from interm to source Move top disk from interm to dest Move top disk from source to dest true .