Professional Documents
Culture Documents
A742 Exp No 08 Aisc
A742 Exp No 08 Aisc
Name: Naitik Jain Div.: - BE-A Roll No: 742 SUBJECT: -AISC
EXPERIMENT: To study and implement Mc-culloch Pitt model for AND gate and
OR gate using python.
PUNCTUALITY (2)
PERFORMANCE (4)
Q&A (2)
JOURNAL WRITING (2)
TOTAL MARKS (10)
Date of Assignment:
Date of Submission:
EXPERIMENT NO:08
Aim: Implementation of Mc-culloch Pitt model for AND gate and OR gate using
python.
Theory:
Now the whole point was to sum the inputs. If an input is one, and is excitatory
in nature, it adds one. If it was one, and was inhibitory, it subtracted one from the
sum. This is done for all inputs, and a final sum is calculated.
Now, if this final sum is less than some value (which you decide, say T), then the
output is zero. Otherwise, the output is one.
In the figure, I represented things with named variables. The variables w1, w2
and w3 indicate which input is excitatory, and which one is inhibitory. These are
called "weights". So, in this model, if a weight is 1, it is an excitatory input. If it
is
-1, it is an inhibitory input.
x1, x2, and x3 represent the inputs. There could be more (or less) inputs if
required. And accordingly, there would be more 'w's to indicate if that particular
input is excitatory or inhibitory.
LABORATORY: AISC LAB 2
(Permanently Affiliated to University of Mumbai)
Department of Computer Engineering
Now, if you think about it, you can calculate the sum using the 'x's and 'w's...
something like this:
sum = x1w1 + x2w2 + x3w3 + ...
AND Function
An AND function neuron would only fire when ALL the inputs are ON i.e., g(x)
≥ 3 here.
OR Function
NOR Function
For a NOR neuron to fire, we want ALL the inputs to be 0 so the thresholding
parameter should also be 0 and we take them all as inhibitory input.
NOT Function
For a NOT neuron, 1 output 0 and 0 outputs 1. So, we take the input as an
inhibitory input and set the thresholding parameter to 0.
XOR Function
It is sometimes called XOR gate or exclusive gate. It gives a true output when
the number of true inputs is odd. If both the inputs are true and both are false then
the output is false.
Now let's look at a non-linear Boolean function i.e., you cannot draw a line to
separate positive inputs from the negative ones.
Notice that the fourth equation contradicts the second and the third equation.
Point is, there are no perceptron solutions for non-linearly separated data. So the
key takeaway is that a single perceptron cannot learn to separate the data that are
nonlinear in nature.
Code: -
import pandas as pd
from IPython.display import display, HTML, clear_output
from tkinter import *
import turtle
import webbrowser
from tempfile import NamedTemporaryFile
#import dfgui
global gate_sel
import time
def df_window(df):
with NamedTemporaryFile(delete=False, suffix='.html') as f:
df.to_html(f)
webbrowser.open(f.name)
def make_truth_table(w1,w2,th):
t = pd.DataFrame(index = None)
t['Input 1']=[0,0,1,1]
t['Input 2']=[0,1,0,1]
t['Response']=[1 if 0*w1+0*w2>=th else 0,1 if 0*w1+1*w2>=th else 0,1 if
1*w1+0*w2>=th else 0,1 if 1*w1+1*w2>=th else 0]
print(t)
#dfgui.shw(t)
#df_window(t)
#display(HTML(t.to_html()))
def get_output(w1,w2,x1,x2,th):
if w1*x1 + w2*x2 >= th:
return 1
else :
return 0
def get_weights():
root = Tk()
root.title('Pick Weights')
root.geometry("500x250") #You want the size of the app to be 500x500
#root.resizable(1500, 1500)
root.option_add("*Label.Font", "courier 24 bold")
Label(root, text = "Enter Weight 1").grid(row = 0, sticky = W)
trtl.penup()
trtl.goto(-100,150)
trtl.pendown()
trtl.write("{0} GATE".format(gate), font=("Arial", 24, "bold"))
trtl.penup()
trtl.goto(-150,90)
trtl.pendown()
trtl.forward(120) #top line
trtl.penup()
trtl.goto(-130,110)
trtl.pendown()
trtl.write("w1 = {0}".format(w1), font=("Arial", 16, "normal"))
trtl.penup() #moving the pen up
trtl.goto(-150,50)
trtl.pendown()
trtl.forward(120) #top line
trtl.penup() #moving the pen down
trtl.goto(-130,20)
trtl.pendown()
trtl.write("w2 = {0}".format(w2), font=("Arial", 16, "normal"))
trtl.penup()
trtl.goto(-220,80)
trtl.pendown()
trtl.write("x1 = {0}".format(x1), font=("Arial", 16, "normal"))
trtl.penup()
trtl.goto(-220,40)
trtl.pendown()
trtl.write("x2 = {0}".format(x2), font=("Arial", 16, "normal"))
if output == 1:
trtl.fillcolor("green")
else:
trtl.fillcolor("red")
trtl.penup()
trtl.goto(5,-20)
trtl.pendown()
trtl.write("\u03F4 = {0}".format(th), font=("Arial", 16, "normal"))
trtl.penup() #moving the pen up
trtl.begin_fill()
trtl.penup() #moving the pen up
trtl.goto(30,10)
trtl.pendown()
trtl.circle(60) #drawing circle with radius 60 pixels
trtl.end_fill()
trtl.penup() #moving the pen down
trtl.goto(90,70)
trtl.pendown()
trtl.forward(120)
trtl.penup()
trtl.goto(110,90)
trtl.pendown()
trtl.write("output={0}".format(output), font=("Arial", 16, "normal"))
time.sleep(3)
#turtle.bye()
screen.clear()
#screen.mainloop()
if(close):
screen.bye()
onceMore = True
while(onceMore==True):
#clear_output()
master = Tk()
var = IntVar()
var.set(1)
def quit_loop():
#print("Selection:",var.get())
global selection
selection = var.get()
master.destroy()
def quit_loop_2():
#print("Selection:",var.get())
global selection2
selection2 = var1.get()
nextOne.destroy()
master.title("CHOOSE AN OPTION")
master.geometry("500x250")
master.option_add("*Label.Font", "courier 24 bold")
Label(master, text = "CHOOSE A GATE TO IMPLEMENT:").grid(row=0, sticky=W)
Radiobutton(master, text = "AND", font="courier 24 bold",variable=var, value =
1).grid(row=1, sticky=W)
Radiobutton(master, text = "OR", font="courier 24 bold", variable=var, value =
2).grid(row=2, sticky=W)
Button(master, text = "SUBMIT", font="courier 24 bold",
command=quit_loop).grid(row=5, sticky=W)
#Button(master, text = "OK", command=quit_loop).grid(row=5, sticky=W)
#button = Button(master, text="Okay", command=master.destroy)
#button.pack()
master.mainloop()
if selection == 1:
#print ("My Value is equal to one.")
gate_sel=1
print("AND Gate")
draw_network(1,1,2,0,0,'AND')
draw_network(1,1,2,0,1,'AND')
draw_network(1,1,2,1,0,'AND')
draw_network(1,1,2,1,1,'AND',close=True)
make_truth_table(1,1,2)
elif selection == 2:
gate_sel=2
print("OR Gate")
draw_network(2,2,2,0,0,'OR')
draw_network(2,2,2,0,1,'OR')
draw_network(2,2,2,1,0,'OR')
draw_network(2,2,2,1,1,'OR',close=True)
make_truth_table(2,2,2)
#print ("My value is equal to two.")
elif selection == 3:
gate_sel=3
print("AND-NOT Gate")
draw_network(2,-1,2,0,0,'AND-NOT')
draw_network(2,-1,2,0,1,'AND-NOT')
draw_network(2,-1,2,1,0,'AND-NOT')
draw_network(2,-1,2,1,1,'AND-NOT',close=True)
make_truth_table(2,-1,2)
elif selection == 4:
gate_sel=4
get_weights()
weights=[int(x) for x in weights]
print("CUSTOM Gate")
draw_network(weights[0],weights[1],weights[2],0,0,'CUSTOM')
draw_network(weights[0],weights[1],weights[2],0,1,'CUSTOM')
draw_network(weights[0],weights[1],weights[2],1,0,'CUSTOM')
draw_network(weights[0],weights[1],weights[2],1,1,'CUSTOM',close=True)
make_truth_table(weights[0],weights[1],weights[2])
nextOne = Tk()
var1 = IntVar()
var1.set(1)
nextOne.title("Would you like to try again?")
nextOne.geometry("500x250")
nextOne.option_add("*Label.Font", "courier 24 bold")
Radiobutton(nextOne, text = "YES", font="courier 24 bold", variable=var1, value =
1).grid(row=1, sticky=W)
Radiobutton(nextOne, text = "NO", font="courier 24 bold", variable=var1, value =
2).grid(row=2, sticky=W)
Button(nextOne, text = "SUBMIT", command=quit_loop_2).grid(row=3, sticky=W)
nextOne.mainloop()
if(selection2==1):
print("\n")
onceMore=True
else:
print("\nHope you liked this :)")
onceMore=False
Output:
Conclusion:
Thus, we have successfully studied and implemented Mc-culloch Pitt Model for
AND gate and OR gate using python.