Professional Documents
Culture Documents
BECOB236 Code
BECOB236 Code
BECOB236 Code
Set Operations
In [1]: b1=[1,0.15,0.3,0.5,0]
b2=[1,0.6,0.2,0.1,0]
In [2]: union=[]
for i in range(len(b1)):
union.append(max(b1[i],b2[i]))
In [3]: print(union)
In [4]: intersct=[]
for i in range(len(b1)):
intersct.append(min(b1[i],b2[i]))
print(intersct)
In [5]: comp1=[]
for i in range(len(b1)):
comp1.append(1-b1[i])
print(comp1)
In [6]: comp2=[]
for i in range(len(b2)):
comp2.append(1-b2[i])
print(comp2)
In [7]: diff12=[]
for i in range(len(b1)):
diff12.append(min(b1[i],comp2[i]))
print(diff12)
In [8]: diff21=[]
for i in range(len(b1)):
diff21.append(min(b2[i],comp1[i]))
print(diff21)
1 of 3 26-09-2020, 18:01
asign1 https://notebooks.gesis.org/binder/jupyter/user/ipython-ipython-in-depth...
In [9]: cartesianprod=[]
for i in range(len(b1)):
z=[]
cartesianprod.append(z)
for i in range(len(b1)):
for j in range(len(b1)):
cartesianprod[i].append(min(b1[i],b2[j]))
print(cartesianprod)
[[1, 0.6, 0.2, 0.1, 0], [0.15, 0.15, 0.15, 0.1, 0], [0.3, 0.3, 0.2,
0.1, 0], [0.5, 0.5, 0.2, 0.1, 0], [0, 0, 0, 0, 0]]
In [11]: x=[1,2,3,4,5]
plt.plot(x,b1,label="set 1")
plt.plot(x,b2,label="set 2")
plt.legend()
2 of 3 26-09-2020, 18:01
asign1 https://notebooks.gesis.org/binder/jupyter/user/ipython-ipython-in-depth...
In [12]: plt.plot(x,union)
In [13]: plt.plot(x,intersct)
In [14]: r1=[[0.6,0.3],[0.2,0.9]]
r2=[[1,0.5,0.3],[0.8,0.4,0.7]]
t=[]
for i in range(len(r1)):
z=[]
t.append(z)
for i in range(len(r1)):
for j in range(len(r2)):
t[i].append(min(r1[i],r2[j]))
print(t)
In [ ]:
3 of 3 26-09-2020, 18:01
Single Layer Perceptron
import numpy as np
def unit_step(v):
""" Heavyside Step function. v must be a scalar """
if v >= 0:
return 1
else:
return 0
def NOT_percep(x):
return perceptron(x, w=-1, b=0.5)
print("NOT(0) = {}".format(NOT_percep(0)))
print("NOT(1) = {}".format(NOT_percep(1)))
def AND_percep(x):
w = np.array([1, 1])
b = -1.5
return perceptron(x, w, b)
print("AND(1, 1) = "+str(AND_percep(example1_and)))
print("AND(1, 0) = "+str(AND_percep(example2_and)))
print("AND(0, 1) = "+str(AND_percep(example3_and)))
print("AND(0, 0) = "+str(AND_percep(example4_and)))
def OR_percep(x):
w = np.array([1, 1])
b = -0.5
return perceptron(x, w, b)
****************OUTPUT****************
NOT(0) = 1
NOT(1) = 0
AND(1, 1) = 1
AND(1, 0) = 0
AND(0, 1) = 0
AND(0, 0) = 0
OR(1, 1) = 1
OR(1, 0) = 1
OR(0, 1) = 1
OR(0, 0) = 0
PSO on Rosenbrock function
x_max = 10 * np.ones(2)
x_min = -1 * x_max
# now run the optimization, pass a=1 and b=100 as a tuple assigned to args
# for c1=c2 = 2
GA on Rosenbrock function
from __future__ import division
import random
'''
Abbreviations:
'''
GEN_SIZE = 100
GEN_COUNT = 100
BOUNDS = ((-100, 100), (-100, 100))
def random_generation():
generation = []
for _ in xrange(GEN_SIZE):
generation.append(random_point)
return generation
def rosenbrock(solution):
def inverse(value):
if value == 0:
return 1
else:
return 1 / value
def fitness(solution):
return inverse(rosenbrock(solution))
assert total != 0
def weighted_choice(items):
n = random.uniform(0, weight_total)
if n < weight:
return item
n = n - weight
return item
def crossover(solution1, solution2):
def mutate(solution):
mutation_threshold = 0.2
for i in range(len(solution)):
return mutated_sol
if __name__ == "__main__":
cur_gen_count = 0
gens = []
gen = random_generation()
gens.append(gen)
cur_gen_count += 1
fitness_scores = []
fitness_score = fitness(sol)
fitness_scores.append(fitness_score)
total_value = 0
total_value += score
probas = []
probas.append(proba)
weighted_gen = []
for i, sol in enumerate(gen):
gen = []
cur_gen_count += 1
parent1 = weighted_choice(weighted_gen)
parent2 = weighted_choice(weighted_gen)
child1 = mutate(child1)
child2 = mutate(child2)
gen.append(child1)
gen.append(child2)
gens.append(gen)
weighted_gen = []
fitness_scores = []
fitness_score = fitness(sol)
fitness_scores.append(fitness_score)
total_value = 0
total_value += score
probas = []
probas.append(proba)
weighted_gen = []
print("\nSUMMARY")
fittest_sol = gen[0]
fitness_max = fitness(gen[0])
sol_fitness = fitness(sol)
fittest_sol = sol
fitness_max = sol_fitness
exit(0)