Professional Documents
Culture Documents
A1 = [1.0,0.15,0.3,0.5,0]
A2 = [1,0.6,0.2,0.1,0]
union = []
for i in range(len(A1)):
union.append(max(A1[i],A2[i]))
print("Union: ",union)
intersection = []
for i in range(len(A1)):
intersection.append(min(A1[i],A2[i]))
print("Intersection : ",intersection)
compliment1 = []
for i in range(len(A1)):
compliment1.append(1-A1[i])
print("Compliment 1 : ",compliment1)
compliment2 = []
for i in range(len(A1)):
compliment2.append(1-A2[i])
print("Compliment 2 : ",compliment2)
difference1 = []
for i in range(len(A1)):
difference1.append(min(A1[i],compliment2[i]))
print("Difference 1 : ",difference1)
difference2 = []
for i in range(len(A1)):
difference2.append(min(A2[i],compliment1[i]))
print("Difference 2 : ",difference2)
for i in range(len(A1)):
print()
for j in range(len(A2)):
print(min(A1[i],A2[j]),end=' ' )
print()
import numpy as np
class Perceptron(object):
self.W = np.zeros(input_size+1)
self.epochs = epochs
self.lr = lr
z = self.W.T.dot(x)
#print(z)
a = self.activation_fn(z)
return a
for _ in range(self.epochs):
for i in range(d.shape[0]):
x = np.insert(X[i], 0, 1)
y = self.predict(x)
#print(x)
#print(y)
e = d[i] - y
#print(e)
print(self.W)
if __name__ == '__main__':
#---AND/OR---
'''
X = np.array([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
#AND
d = np.array([0, 0, 0, 1])
#OR
#d = np.array([0, 1, 1, 1])
perceptron = Perceptron(input_size=2)
'''
#-----------
#----NOT----
X = np.array([[0], [1]])
d = np.array([1, 0])
perceptron = Perceptron(input_size=1)
#-----------
perceptron.fit(X, d)
print(perceptron.W)
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)