Soft Computing
Soft Computing
Practical No: 1
A) Design a simple linear neural network model.
code:
import numpy
a= float(input("\n Enter the first input a = "))
wa = float(input("\n Enter the Weight Value for first input wa = "))
c= float(input("\n Enter the first input c= "))
wc = float(input("\n Enter the Weight Value for first input wc = "))
if(net<0):
out = 0
elif (net >=0) & (net <= 1 ):
out = net
else :
out = 1
print (" output " , out)
Output:
B) Calculate the output of neural net using both binary and bipolar sigmoidal function.
code:
Practical No: 2
print( " for the ", num_ip, "inputs calculate the net input using Yin x1W1 + X2W2")
x1 = []
x2 = []
for j in range(0,num_ip):
ele1 = int(input(" X1 = "))
ele2 = int(input(" X2 = "))
x1.append(ele1)
x2.append(ele2)
print("\n X1 = " , x1)
print("X2 = ", x2)
n = x1 * w1
m = x2 * w2
Yin = []
for i in range(0,num_ip):
Yin.append(n[i]+m[i])
print("Yin = ", Yin)
Yin = []
for i in range (0, num_ip):
Yin.append(n[i]-m[i])
print("after assuming one weignt as excitatory and other as inhibitoryu Yin = " , Yin)
Y = []
for i in range(0, num_ip):
if (Yin[i]>=1) :
ele = 1
Y.append(ele)
if (Yin[i]< 1 ) :
ele=0
Y.append(ele)
print("Y = ", Y )
#------------------
Output:
import numpy as np
print('Enter weights')
w11 = int(input('Weight w11='))
w12 = int(input('Weight w12='))
w21 = int(input('Weight w21='))
w22 = int(input('Weight w22='))
v1 = int(input('weight v1='))
v2 = int(input('weight v2='))
print('Enter Threshold Value')
theta = int(input('theta='))
x1 = np.array([0, 0, 1, 1])
x2 = np.array([0, 1, 0, 1])
z = np.array([0, 1, 1, 0])
con = 1
y1 = np.zeros((4,))
y2 = np.zeros((4,))
y = np.zeros((4,))
while con == 1:
zin1 = np.zeros((4,))
zin2 = np.zeros((4,))
zin1 = x1*w11+x2*w21
zin2 = x1*w21+x2*w22
print("z1", zin1)
print("z2", zin2)
for i in range(0, 4):
if zin1[i] >= theta:
y1[i] = 1
else:
y1[i] = 0
if zin2[i] >= theta:
y2[i] = 1
else:
y2[i] = 0
yin = np.array([])
yin = y1*v1+y2*v2
Output:
Practical No: 3
A) Write a program to implement Hebb's rule.
code:
import numpy as np
x1=np.array([1,-1,-1,1,-1,-1,1,1,1])
x2=np.array([1,-1,1,1,-1,1,1,1,1])
b=0
y=np.array([1,-1])
wtold=np.zeros((9,))
wtnew=np.zeros((9,))
wtnew=wtnew.astype(int)
wtold=wtold.astype(int)
bais=0
print("First input with target = 1")
for i in range(0,9):
wtnew[i]=wtold[i]+x1[i]*y[0]
wtold=wtnew
b=b+y[0]
print("new wt=",wtnew)
print("Bias value=",b)
print("new wt=",wtnew)
print("Bias value",b)
Output:
#supervised learning
import numpy as np
import time
np.set_printoptions(precision=2)
x=np.zeros((3,))
weights=np.zeros((3,))
desired=np.zeros((3,))
actual=np.zeros((3,))
for i in range(0,3):
x[i]=float(input("Initial inputs:"))
for i in range(0,3):
weights[i]=float(input("Initial weights:"))
for i in range(0,3):
desired[i]=float(input("Desired output:"))
while True:
if np.array_equal(desired,actual):
break #no change
else:
for i in range(0,3):
weights[i]=weights[i]+a*(desired[i]-actual[i])
actual=x*weights
print("weights",weights)
print("actual",actual)
print("desired",desired)
print("*"*30)
print("Final output")
print("Corrected weights",weights)
print("actual",actual)
print("desired",desired)
Output:
Practical No: 4
A) Write a program for Back Propagation Algorithm.
code:
import math
a0 = -1
t=-1
w10=float(input("Enter weights first network:"))
b10=float(input("Enter base first network:"))
w20=float(input("Enter weights second network:"))
b20=float(input("Enter base second network:"))
c=float(input("Enter learning coefficent:"))
n1=float(w10*c+b10)
a1=math.tanh(n1)
n2=float(w20*a1+b20)
a2=math.tanh(float(n2))
e=t-a2
s2=2*(1-a2*a2)*e
s1=(1-a1*a1)*w20*s2
w21=w20-(c*s2*a1)
w11=w10-(c*s1*a0)
b21=b20-(c*s2)
b11=b10-(c*s1)
print("The updated weight of first network w11=",w11)
print("The udated weight of second network w21=",w21)
print("The updated base of first network b10=",b10)
print("The updated base of second network b20=",b20)
Output:
code:
import numpy as np
import decimal
import math
np.set_printoptions(precision=2)
v1=np.array([0.6,0.3])
v2=np.array([-0.1,0.4])
w=np.array([-0.2,0.4,0.1])
b1=0.3
b2=0.5
x1=0
x2=1
alpha=0.25
print("calculate net input to z1 layer")
zin1=round(b1+ x1*v1[0]+x2*v2[0],4)
print("z1=",round(zin1,3))
print("calculate net input to z2 layer")
zin2=round(b2+x1*v1[1],4)
print("z2=",round(zin2,4))
print("Apply activation function to calculate output")
z1=1/(1+math.exp(-zin1))
z1=round(z1,4)
z2=1/(1+math.exp(-zin2))
z2=round(z2,4)
print("z1=",z1)
print("z2=",z2)
print("calculate net input to output layer")
yin=w[0]+z1*w[1]+z2*w[2]
print("yin=",yin)
print("calculate net output")
y=1/(1+math.exp(-yin))
print("y=",y)
fyin=y *(1-y)
dk=(1-y)*fyin
print("dk",dk)
dw1=alpha * dk * z1
dw2=alpha * dk * z2
dw0=alpha * dk
print("compute error portion in delta")
din1=dk* w[1]
din2=dk* w[2]
print("din1=",din1)
print("din2=",din2)
print("error in delta")
fzin1=z1 *(1-z1)
print("fzin1",fzin1)
d1=din1* fzin1
fzin2=z2*(1-z2)
print("fzin2,fzin2")
d2=din2* fzin2
print("d1=",d1)
print("d2=",d2)
print("Changes in weights between input and hidden layer")
dv11=alpha * d1 * x1
print("dv11=",dv11)
dv21=alpha * d1 * x2
print("dv21=",dv21)
dv01=alpha * d1
print("dv01=",dv01)
dv12=alpha * d2 * x1
print("dv12=",dv12)
dv22=alpha * d2 * x2
print("dv22=",dv22)
dv02=alpha * d2
print("dv02=",dv02)
print("Final weights of network")
v1[0]=v1[0]+dv11
v1[1]=v1[1]+dv12
print("v=",v1)
v2[0]=v2[0]+dv21
v2[1]=v2[1]+dv22
print("v2",v2)
w[1]=w[1]+dw1
w[2]=w[2]+dw2
b1=b1+dv01
b2=b2+dv02
w[0]=w[0]+dw0
print("w=",w)
print("bias b1=",b1, " b2=",b2)
Output:
Practical No: 5
A) Write a program for Hopfield Network.
code:
Output:
B) Write a program for Radial Basis function
code:
class RBF:
def __init__(self, indim, numCenters, outdim):
self.indim = indim
self.outdim = outdim
self.numCenters = numCenters
self.centers = [random.uniform(-1, 1, indim) for i in range(numCenters)]
self.beta = 8
self.W = random.random((self.numCenters, self.outdim))
if __name__ == '__main__':
# ----- 1D Example ------------------------------------------------
n = 100
x = mgrid[-1:1:complex(0,n)].reshape(n, 1)
# set y and add random noise
y = sin(3*(x+0.5)**3 - 1)
# y += random.normal(0, 0.1, y.shape)
# rbf regression
rbf = RBF(1, 10, 1)
rbf.train(x, y)
z = rbf.test(x)
# plot rbfs
plt.plot(rbf.centers, zeros(rbf.numCenters), 'gs')
for c in rbf.centers:
# RF prediction lines
cx = arange(c-0.7, c+0.7, 0.01)
cy = [rbf._basisfunc(array([cx_]), array([c])) for cx_ in cx]
plt.plot(cx, cy, '-', color='gray', linewidth=0.2)
plt.xlim(-1.2, 1.2)
plt.show()
Output:
Practical No: 6
A) Implement Kohonen Self Organizing Map
code:
# som_iris.py
# SOM for Iris dataset
# Anaconda3 5.2.0 (Python 3.6.5)
# ==================================================================
import numpy as np
import matplotlib.pyplot as plt
# note: if this fails, try >pip uninstall matplotlib
# and then >pip install matplotlib
defeuc_dist(v1, v2):
return np.linalg.norm(v1 - v2)
defmost_common(lst, n):
# lst is a list of values 0 . . n
if len(lst) == 0: return -1
counts = np.zeros(shape=n, dtype=np.int)
for i in range(len(lst)):
counts[lst[i]] += 1
return np.argmax(counts)
# ==================================================================
def main():
# 0. get started
np.random.seed(1)
Dim = 4
Rows = 30; Cols = 30
RangeMax = Rows + Cols
LearnMax = 0.5
StepsMax = 5000
# 1. load data
print("\nLoading Iris data into memory \n")
#Change Location of DataSet
data_file =
"C:/Users/User-10/Documents/Dimple_Baroliya/Soft_Computing/prac6/iris_data_012.txt"
data_x = np.loadtxt(data_file, delimiter=",", usecols=range(0,4),
dtype=np.float64)
data_y = np.loadtxt(data_file, delimiter=",", usecols=[4],
dtype=np.int64)
# option: normalize data
t = np.random.randint(len(data_x))
(bmu_row, bmu_col) = closest_node(data_x, t, map, Rows, Cols)
for i in range(Rows):
for j in range(Cols):
if manhattan_dist(bmu_row, bmu_col, i, j) <curr_range:
map[i][j] = map[i][j] + curr_rate * \
(data_x[t] - map[i][j])
print("SOM construction complete \n")
# 3. construct U-Matrix
print("Constructing U-Matrix from SOM")
u_matrix = np.zeros(shape=(Rows,Cols), dtype=np.float64)
for i in range(Rows):
for j in range(Cols):
v = map[i][j] # a vector
sum_dists = 0.0; ct = 0
u_matrix[i][j] = sum_dists / ct
print("U-Matrix constructed \n")
# display U-Matrix
plt.imshow(u_matrix, cmap='gray') # black = close = clusters
plt.show()
# ==================================================================
if __name__=="__main__":
main()
Output:
B) Implement Adaptive resonance theory
Practical No: 7
A) Line Separation
Output:
Practical No: 8
A) Implement Membership and Identity Operators | in, not in.
def overlapping(list1, list2):
c=0
d=0
for i in list1:
c+=1
for i in list2:
d+=1
for i in range (0,c):
for j in range(0,d):
if(list1[i]==list2[j]):
return 1
return 0
list1=[1,2,3,4,5]
list2=[4,6,7,8,9]
list3=[6,7,8,9,10]
if(overlapping(list1,list2)):
print("overlapping")
else:
print("not overlapping")
if(overlapping(list1,list3)):
print("overlapping")
else:
print("not overlapping")
Output:
Output:
Practical No: 9
Install mdules:
pip install fuzzywuzzy
Output:
Install mdules
pip install scikit-fuzzy
python -m pip install --upgrade pip
pip install matplotlib
Output:
Practical No: 10
A) Write a python to Implementation of Simple genetic algorithm.
import random
class Individual(object):
'''
Class representing individual in population
'''
def __init__(self, chromosome):
self.chromosome = chromosome
self.fitness = self.cal_fitness()
@classmethod
defmutated_genes(self):
'''
create random genes for mutation
'''
global GENES
gene = random.choice(GENES)
return gene
@classmethod
defcreate_gnome(self):
'''
create chromosome or string of genes
'''
global TARGET
gnome_len = len(TARGET)
return [self.mutated_genes() for _ in range(gnome_len)]
defcal_fitness(self):
'''
Calculate fitness score, it is the number of
characters in string which differ from target
string.
'''
global TARGET
fitness = 0
for gs, gt in zip(self.chromosome, TARGET):
if gs != gt: fitness+= 1
return fitness
# Driver code
def main():
global POPULATION_SIZE
#current generation
generation = 1
found = False
population = []
population = new_generation
generation += 1
if __name__ == '__main__':
main()
Output:
B) Write a python to Create two classes: City and Fitness using Genetic algorithm.
Code:
import numpy as np, random, operator, pandas as pd, matplotlib.pyplot as plt
class City:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
class Fitness:
def __init__(self, route):
self.route = route
self.distance = 0
self.fitness= 0.0
defrouteDistance(self):
if self.distance ==0:
pathDistance = 0
for i in range(0, len(self.route)):
fromCity = self.route[i]
toCity = None
if i + 1 <len(self.route):
toCity = self.route[i + 1]
else:
toCity = self.route[0]
pathDistance += fromCity.distance(toCity)
self.distance = pathDistance
return self.distance
defrouteFitness(self):
if self.fitness == 0:
self.fitness = 1 / float(self.routeDistance())
return self.fitness
defcreateRoute(cityList):
route = random.sample(cityList, len(cityList))
return route
definitialPopulation(popSize, cityList):
population = []
defrankRoutes(population):
fitnessResults = {}
for i in range(0,len(population)):
fitnessResults[i] = Fitness(population[i]).routeFitness()
return sorted(fitnessResults.items(), key = operator.itemgetter(1), reverse = True)
defmatingPool(population, selectionResults):
matingpool = []
for i in range(0, len(selectionResults)):
index = selectionResults[i]
matingpool.append(population[index])
return matingpool
defbreedPopulation(matingpool, eliteSize):
children = []
length = len(matingpool) - eliteSize
pool = random.sample(matingpool, len(matingpool))
for i in range(0,eliteSize):
children.append(matingpool[i])
for i in range(0, length):
child = breed(pool[i], pool[len(matingpool)-i-1])
children.append(child)
return children
city1 = individual[swapped]
city2 = individual[swapWith]
individual[swapped] = city2
individual[swapWith] = city1
return individual
defmutatePopulation(population, mutationRate):
mutatedPop = []
cityList = []
for i in range(0,25):
cityList.append(City(x=int(random.random() * 200), y=int(random.random() * 200)))
plt.plot(progress)
plt.ylabel('Distance')
plt.xlabel('Generation')
plt.show()
Output: