All Courses

# Artificial Intelligence With Python

Rishi Baghel

2 years ago

##### Table of Content
• Idea
• Step by step instructions to Solve Problems with Logic Programming
• Realities
• Rules
• Useful Packages in python programming
• Checking for Prime Numbers
• Solving Puzzles

## Introduction

In this section, we will center rational programming and how it helps in Artificial Intelligence.
We definitely realize that rationale is the investigation of standards of right-thinking or in straightforward words, it is the investigation of what comes after what. For instance, in the event that two articulations are valid, at that point we can induce any third explanation from it.

## Idea

Rationale Programming is the blend of two words, rationale, and programming. Rationale Programming is a programming worldview in which the issues are communicated as realities and rules by program proclamations however inside an arrangement of formal rationale. Much the same as other programming ideal models like item situated, useful, revelatory, and procedural, and so on., it is likewise a specific method to move toward programming.

## Step by step instructions to Solve Problems with Logic Programming

Rationale Programming utilizes realities and rules for taking care of the issue. That is the reason they are known as the structure squares of Logic Programming. An objective should be determined for each program in rationale programming. To see how an issue can be fathomed in rationale programming, we have to think about the structure squares − Facts and Rules −

## Realities

As a matter of fact, each rationale program needs realities to work with so it can accomplish the given objective. Realities fundamentally are genuine explanations about the program and information. For instance, Delhi is the capital of India.

## Rules

All  things considered, rules are the limitations which permit us to make decisions about the difficult area. Rules fundamentally composed as sensible provisos to communicate different realities. For instance, on the off chance that we are assembling any game, at that point all the guidelines must be characterized.
Rules are essential to take care of any issue in Logic Programming. Rules are essentially obvious end result which can communicate the realities. Keeping is the punctuation of rule −
A∶− B1,B2,...,Bn.
Here, An is the head and B1, B2, ... Bn is the body.
For instance − ancestor(X,Y) :- father(X,Y).
ancestor(X,Z) :- father(X,Y), ancestor(Y,Z).
This can be perused as, for each X and Y, if X is the dad of Y and Y is a precursor of Z, X is the predecessor of Z. For each X and Y, X is the precursor of Z, if X is the dad of Y and Y is a predecessor of Z.

## Introducing Useful Packages in python programming

For beginning rationale programming in Python, we have to introduce the accompanying two bundles −

## Kanren

It gives us an approach to streamlining the manner in which we made code for business rationale. It lets us express the rationale as far as rules and realities. The accompanying order will assist you with introducing kanren

## SymPy

SymPy is a Python library for representative arithmetic. It means to turn into a full-included PC polynomial math framework (CAS) while keeping the code as straightforward as conceivable so as to be fathomable and effectively extensible. The accompanying order will assist you with introducing SymPy −

## Instances of Logic Programming

Followings are a few models which can be tackled by rationale programming −

## Coordinating numerical articulations

In reality, we can locate obscure qualities by utilizing rationale programming in an extremely compelling manner. The accompanying Python code will assist you with coordinating a numerical articulation –
Consider bringing in the accompanying bundles first –
from kanren import run, var, fact
from kanren.assoccomm import eq_assoccomm as eq
from kanren.assoccomm import commutative, associative
We have to characterize the numerical tasks which we are going to utilize −
mul = 'mul'
Both expansion and duplication are informative procedures. Consequently, we have to determine it and this should be possible as follows –
``````fact(commutative, mul)
fact(associative, mul)
It is obligatory to characterize factors; this should be possible as follows –
a, b = var('a'), var('b')
We have to coordinate the articulation with the first example. We have the accompanying unique example, which is essentially (5+a)*b −
Original_pattern = (mul, (add, 5, a), b)
We have the accompanying two articulations to coordinate with the first example –
exp1 = (mul, 2, (add, 3, 1))
Yield can be printed with the accompanying order –
Subsequent to running this code, we will get the accompanying yield −
The main yield speaks to the qualities for an and b. The principal articulation coordinated the first example and restored the qualities for an and b yet the subsequent articulation didn't coordinate the first example thus nothing has been returned.

## Checking for Prime Numbers

With the assistance of rationale programming, we can locate the prime numbers from a rundown of numbers and can likewise produce prime numbers. The Python code given beneath will locate the prime number from a rundown of numbers and will likewise produce the initial 10 prime numbers. Let us initially consider bringing in the accompanying bundles –
``````from kanren import isvar, run, membero
from kanren.core import success, fail, goaleval, condeseq, eq, var
from sympy.ntheory.generate import prime, isprime
import itertools as it``````
Presently, we will characterize a capacity called prime_check which will check the prime numbers dependent on the given numbers as information.
``````def prime_check(x):
if isvar(x):
&nbsp;&nbsp; return condeseq([(eq,x,p)] for p in map(prime, it.count(1)))
else:
&nbsp;&nbsp; return success if isprime(x) else fail``````
Presently, we have to announce a variable which will be utilized –
``````x = var()
print((set(run(0,x,(membero,x,(12,14,15,19,20,21,22,23,29,30,41,44,52,62,65,85)),
(prime_check,x)))))
print((run(10,x,prime_check(x))))``````
The output of the above code will be as follows –

## Solving Puzzles

Rationale programming can be utilized to take care of numerous issues like 8-puzzles, Zebra puzzle, Sudoku, N-sovereign, and so on. Here we are taking a case of a variation of Zebra puzzle which is as per the following –
``````There are five houses.
The English man lives in the red house.
The Swede has a dog.
The Dane drinks tea.
The green house is immediately to the left of the white house.
They drink coffee in the green house.
The man who smokes Pall Mall has birds.
In the yellow house they smoke Dunhill.
In the middle house they drink milk.
The Norwegian lives in the first house.
The man who smokes Blend lives in the house next to the house with cats.
In a house next to the house where they have a horse, they smoke Dunhill.
The man who smokes Blue Master drinks beer.
The German smokes Prince.
The Norwegian lives next to the blue house.
They drink water in a house next to the house where they smoke Blend.``````
We are fathoming it for the inquiry who claims zebra with the assistance of Python.
Let us import the fundamental bundles –
``````from kanren import *
from kanren.core import lall
import time``````
Presently, we have to characterize two capacities − left() and next() to check whose house is gone out –
``````def left(q, p, list):
&nbsp;&nbsp; return membero((q,p), zip(list, list[1:]))
def next(q, p, list):
&nbsp;&nbsp; return conde([left(q, p, list)], [left(p, q, list)])``````
Now, we will declare a variable house as follows –
houses = var()
We have to characterize the standards with the assistance of lall bundle as follows.
There are 5 houses –
``````rules_zebraproblem = lall(
&nbsp;&nbsp; (eq, (var(), var(), var(), var(), var()), houses),

&nbsp;&nbsp; (membero,('Englishman', var(), var(), var(), 'red'), houses),
&nbsp;&nbsp; (membero,('Swede', var(), var(), 'dog', var()), houses),
&nbsp;&nbsp; (membero,('Dane', var(), 'tea', var(), var()), houses),
&nbsp;&nbsp; (left,(var(), var(), var(), var(), 'green'),
&nbsp;&nbsp; (var(), var(), var(), var(), 'white'), houses),
&nbsp;&nbsp; (membero,(var(), var(), 'coffee', var(), 'green'), houses),
&nbsp;&nbsp; (membero,(var(), 'Pall Mall', var(), 'birds', var()), houses),
&nbsp;&nbsp; (membero,(var(), 'Dunhill', var(), var(), 'yellow'), houses),
&nbsp;&nbsp; (eq,(var(), var(), (var(), var(), 'milk', var(), var()), var(), var()), houses),
&nbsp;&nbsp; (eq,(('Norwegian', var(), var(), var(), var()), var(), var(), var(), var()), houses),
&nbsp;&nbsp; (next,(var(), 'Blend', var(), var(), var()),
&nbsp;&nbsp; (var(), var(), var(), 'cats', var()), houses),
&nbsp;&nbsp; (next,(var(), 'Dunhill', var(), var(), var()),
&nbsp;&nbsp; (var(), var(), var(), 'horse', var()), houses),
&nbsp;&nbsp; (membero,(var(), 'Blue Master', 'beer', var(), var()), houses),
&nbsp;&nbsp; (membero,('German', 'Prince', var(), var(), var()), houses),
&nbsp;&nbsp; (next,('Norwegian', var(), var(), var(), var()),
&nbsp;&nbsp; (var(), var(), var(), var(), 'blue'), houses),
&nbsp;&nbsp; (next,(var(), 'Blend', var(), var(), var()),
&nbsp;&nbsp; (var(), var(), 'water', var(), var()), houses),
&nbsp;&nbsp; (membero,(var(), var(), var(), 'zebra', var()), houses))``````
Now, run the solver with the preceding constraints −
solutions = run(0, houses, rules_zebraproblem)
With the assistance of the accompanying code, we can separate the yield from the solver –
The accompanying code will help print the arrangement –