Wednesday, April 6, 2022

Implementation of Bayes Belief Network

 Python Code:

import pgmpy.models

import pgmpy.inference

import networkx as nx

import pylab as plt

model = pgmpy.models.BayesianModel([('Guest', 'Monty'), ('Price', 'Monty')])

cpd_guest = pgmpy.factors.discrete.TabularCPD('Guest', 3, [[0.33, 0.33, 0.33]])

cpd_price = pgmpy.factors.discrete.TabularCPD('Price', 3, [[0.33, 0.33, 0.33]])

cpd_monty = pgmpy.factors.discrete.TabularCPD('Monty', 3, [[0, 0, 0, 0, 0.5, 1, 0, 1, 0.5], 

                                                           [0.5, 0, 1, 0, 0, 0, 1, 0, 0.5], 

                                                           [0.5, 1, 0, 1, 0.5, 0, 0, 0, 0]], 

                                              evidence=['Guest', 'Price'], 

                                              evidence_card=[3, 3])

model.add_cpds(cpd_guest, cpd_price, cpd_monty)

model.check_model()

print('Probability distribution, P(Guest)')

print(cpd_guest)

print()

print('Probability distribution, P(Price)')

print(cpd_price)

print()

print('Joint probability distribution, P(Monty | Guest, Price)')

print(cpd_monty)

print()

nx.draw(model, with_labels=True)

plt.savefig('Data.png')

plt.close()

infer = pgmpy.inference.VariableElimination(model)

posterior_probability = infer.query(['Price'], evidence={'Guest': 0, 'Monty': 2})

print('Posterior probability, Guest(0) and Monty(2)')

print(posterior_probability)

print()


Output:





Tuesday, April 5, 2022

Planning Programming

 Planning is one on the basic tasks in Artificial Intelligence. It is an essential component of applications like Robotic Control or Goal achieving applications of AI. It is basically the task of decision making in such applications. Goal Stack Planning is one such algorithm that is implemented in AI applications to evaluate the likelihood of completion of a specific task.

Key features of its representation are-

  • Stack to maintain the action and satisfy the goal.  
  • Knowledge base to maintain the current state, actions.

Goal stack planning resembles a node in a search tree. Starting from a specific node the branches is created only when there is a possibility of taking a further action.


Algorithm: Goal Stack Planning

  1. Push the original goal on the stack.
  2. Repeat step a to d until the stack becomes empty.
    1. If TOP is a compound goal, push its unfinished subgoals onto the stack. 
    1. If TOP is a single unfinished goal then, replace it with an action and push the action’s precondition on the stack to satisfy the condition.
    1. If TOP is an action,
      1. Pop the action
      1. Execute the action
      1. update the knowledge base with effects of the action
    1. If TOP is a satisfied goal, pop it.
Python Code:

tab = []
result = []
goalList = ["a", "b", "c", "d", "e"]


def parSolution(N):
    for i in range(N):
        if goalList[i] != result[i]:
            return False
    return True


def Onblock(index, count):

    # break point of recursive call
    if count == len(goalList)+1:
        return True
    # copy tab of index value to result
    block = tab[index]
    # stack block
    result.append(block)
    print(result)
    if parSolution(count):
        print("Pushed a result solution ")
        # remove block from tab
        tab.remove(block)
        Onblock(0, count + 1)
    else:
        print("result solution not possible, back to the tab")
        # pop out if no partial solution
        result.pop()
        Onblock(index+1, count)


def Ontab(problem):
    # check if everything in stack is on the tab
    if len(problem) != 0:
        tab.append(problem.pop())
        Ontab(problem)
    # if everything is on the tab the we return true
    else:
        return True


def goal_stack_planing(problem):
    # pop problem and put in tab
    Ontab(problem)
    # print index and number of blocks on result stack
    if Onblock(0, 1):
        print(result)


if __name__ == "__main__":
    problem = ["c", "a", "e", "d", "b"]
    print("Goal Problem")
    for k, j in zip(goalList, problem):
        print(k+"    "+j)
    goal_stack_planing(problem)
    print("result Solution")
    print(result)


 Output:



The advantage of this technique is that the generated plans display complete sequence of operations. It starts from original unfinished goal and at each step complete set of sequential operations for the next incomplete goal are generated. A database that describes the current situation. Use of stack in this code allow to backtrack and follow an unfinished task – part of the current sub goal.



Implementation of Bayes Belief Network

 Python Code: import pgmpy.models import pgmpy.inference import networkx as nx import pylab as plt model = pgmpy.models.BayesianModel([('...