Example #1
0
import file_handler
import k_means

path = 'Dataset1.csv'


if __name__ == '__main__':
    X, Y = file_handler.read_from_file(path)

    K = 4
    iterations = 30

    data = list(zip(X, Y))
    errors = []

    clusters, centers, data_from_cluster = \
                k_means.specifying_clusters(data, K, iterations)
    k_means.draw_data(data, centers, data_from_cluster, K, iterations)

    k_means.compute_error_for_some_k(data, K, iterations)
import Chromosome
import file_handler as fh
import random
import numpy as np
import matplotlib.pyplot as plt


data = fh.read_from_file('Dataset/Dataset1.csv')

Mu = 10
# Todo change Mu's coefficient below to attain the best result
Lambda = 7*Mu
crossover_probability = 0.4
population_size = Mu
chromosome_length = 2
iteration_size = 100
lr = 0.001
minFit = []
maxFit = []
avgFit = []



def generate_initial_population():
    list_of_chromosomes = []
    for i in range(0, population_size):
        ch = Chromosome.Chromosome(chromosome_length, -10, 10)
        list_of_chromosomes.append(ch)
    return list_of_chromosomes

Example #3
0
def normalize_list(p):
    for i in p:
        i.normalize()
    return p


if __name__ == '__main__':
    # Todo -- Use Methods In a proper arrangement

    points = []
    pop = []
    new_parents = []
    offspring = []
    # read points from data set
    points = file_handler.read_from_file(file_num)
    Mu = len(points)
    Lambda = int(Lambda_coefficient * Mu)

    # obviously generate random population
    pop = generate_initial_population(chro_len=2,
                                      min_val=min_val,
                                      max_val=max_val)
    # eval population scores
    pop = evaluate_new_generation(pop, points)

    for i in range(number_of_generations):
        # choose new parents
        new_parents = generate_new_seed(pop)
        # cross over of new parents to make the offspring
        offspring = parent_crossover(new_parents)
import random

from file_handler import read_from_file
from network1 import Network1
from network2 import Network2
from plot import categorize_data, plot

data = read_from_file()
x0, y0, x1, y1 = categorize_data(data)
plot(x0, y0, x1, y1)

random.shuffle(data)
k = int(len(data) * 4 / 5)
training_data = data[0:k]
test_data = data[k + 1:len(data)]
print("network 1")
net = Network1(training_data, test_data)
net.SGD(3000, 3)
print("network 2")
net = Network2(training_data, test_data)
net.SGD(3000, 3)
Example #5
0
from Chromosome import Chromosome
from ES import process_genes, get_best_gene
from plot import plot_result
from file_handler import read_from_file
import numpy as np
input_dots = read_from_file("./Dataset/Dataset1.csv")
chromosome = Chromosome(500, -.1, .1)
#.903 .51
fitness_array = []
while sorted(chromosome.evaluate(input_dots), reverse=True)[0] < 10:
    chromosome, fitness_array = process_genes(chromosome, input_dots, 10**-1,
                                              39, fitness_array)
print(sorted(chromosome.evaluate(input_dots), reverse=True)[0])
a, b = get_best_gene(chromosome, input_dots)
print(a, "  ", b)
plot_result(input_dots, a, b, fitness_array)


def find_loss(a, b, input_dots):
    z_array = []
    loss_count = 0
    for dot in input_dots:
        add_new = True
        zprim = dot[0] * a + dot[1] * b
        for z in z_array:
            if np.abs(zprim - z) < .01:
                add_new = False
                loss_count += 1
                break
        if add_new:
            z_array.append(zprim)
Example #6
0
    newGeneration = []
    for i in range(len(Mu)):
        best, index = Q_tournament(parents)
        newGeneration.append(best)
        parents.pop(index)
    return newGeneration


if __name__ == '__main__':
    MuSize = 10
    crossover_probability = 0.4
    # N = 100
    N = 100
    min_ab = 0
    max_ab = 1
    x, y = read_from_file()
    chromosome_length = len(x)
    # chromosome_length = 10

    # ps = 1
    # c = 0.8

    alpha = 0.5
    Smin = 1
    k = 0.125
    Mu = generate_initial_population(chromosome_length, min_ab, max_ab, x, y)

    max_node = max(Mu, key=lambda node: node.fitness)
    min_node = min(Mu, key=lambda node: node.fitness)
    avg_fitness = sum(c.fitness for c in Mu) / len(Mu)
    print("t=0 best fitness: " + str(max_node.fitness) + " worst: " +