Beispiel #1
0
def main():

    neurons = 13
    connections = 100
    # 3 is the dimension of N
    nbits = 3 * connections * bitsNeededToNumber(neurons)

    def fitness(gene):
        N = np.reshape(
            bitsToBytes(np.reshape(gene, (-1, bitsNeededToNumber(neurons)))),
            (-1, 3))
        score = 0
        score -= sum(N[:, 1] == N[:, 2])

        # print(score)
        return score

    ga = GeneticAlgorithm.GA(gene_size=nbits,
                             population_size=10,
                             epochs=1000,
                             maximization=True,
                             ephoc_generations=10)

    ga.debug = False
    ga.verbose = True

    best, pop, score = ga.run(fitness, multiple=True)
Beispiel #2
0
    def __init__(self, in_count, max_number=100):
        self.in_count = in_count
        self.activations_bits = bitsNeededToNumber(
            len(ActivationFunctions.activations()))
        self.nbits = (in_count*bitsNeededToNumber(max_number)) + \
            self.activations_bits
        self.weight_subtractor = max_number / 2
        self.ga = GA(gene_size=self.nbits,
                     population_size=1000,
                     maximization=False,
                     epochs=50,
                     ephoc_generations=10)

        print("""
            GeneticNeuron Start: 
                Number Of Inputs: {in_count}
                ...
        """.format_map({"in_count": self.in_count}))
Beispiel #3
0
    def fitness(gene):
        N = np.reshape(
            bitsToBytes(np.reshape(gene, (-1, bitsNeededToNumber(neurons)))),
            (-1, 3))
        score = 0
        score -= sum(N[:, 1] == N[:, 2])

        # print(score)
        return score
Beispiel #4
0
def main():


    genesize = 2*bitsNeededToNumber(1000)
    nbits = int(round(genesize/2))

    

    def fitness(gene):
        # print(gene)
        # print(nbits)
        one = bitsToBytes(gene[:, 0:nbits])
        two = bitsToBytes(gene[:, nbits:])
        score = np.sum([one, two], axis=0)

        # print(one,two,score)
        return score

    print('nbits:', nbits)
    expected = 0
    # print('DesiredValue:', expected)

    ga = GeneticAlgorithm.GA(genesize, population_size=10,
                             epochs=4, maximization=False)

    ga.debug = False
    ga.verbose = True

    best, pop, score = ga.run(fitness,multiple=True)
    
    # print(ga.history["population"])

    def evaluate(gene):
        print('==========Evaluation=============')
        one_bits = gene[:, 0:nbits]
        # print(one_bits.shape)
        one = bitsToBytes(one_bits)
        two = bitsToBytes(gene[:, nbits:])
        score = np.sum([one, two], axis=0)

        # print(one,two,score)
        print('Achieved: ', score, 'Expected:', expected)
        
        return score
    print('BEST: ', best)
    evaluate(np.array([best]))
Beispiel #5
0
    def run(self, data, in_count):
        self.in_count = in_count
        self.out_count = data.shape[1]-self.in_count
        self.weigth_power_factor_bits = bitsNeededToNumber(5)
        self.weight_subtractor = self.max_number/2
        
        self.weights_matrix_lines = self.out_count+self.max_hidded_neurons
        self.bias_bits = self.weights_matrix_lines*bitsNeededToNumber(self.max_number)
        self.activations_bits =self.weights_matrix_lines*bitsNeededToNumber(len(ActivationFunctions.activations()))
        self.weights_matrix_columns = self.max_hidded_neurons+self.in_count
        self.weights_count = self.weights_matrix_lines \
                                * self.weights_matrix_columns
        self.weights_bits = self.weights_count \
                                * bitsNeededToNumber(self.max_number)

        self.nbits =  self.activations_bits \
                    + self.weigth_power_factor_bits \
                    + self.bias_bits \
                    + self.weights_bits

        self.ga = GA(gene_size=self.nbits,
                     population_size=self.population_size,
                     epochs=self.epochs,
                     ephoc_generations=self.ephoc_generations,
                     selection_count=self.selection_count)

        self.values_shift = self.max_hidded_neurons
        self.neuron_weight_position_shift = self.out_count
        print("""
            GeneticNeuron Start: 
                Number Of Inputs: {in_count}
                Number of bits for each gene: {nbits}
                Number of bits used for select activation func: {activation_bits}
                Number of classes: {n_classes} 
                Number of hidden neurons: {hidden_neurons}
                Weights matrix lines: {weights_matrix_lines} (out+hidden)
                Weights matrix columns: {weights_matrix_columns} (in+hidden)
                Values shift: {values_shift}
                weight shift: {weight_shift}
                
        """.format_map({ \
                "in_count": self.in_count,
                "nbits": self.nbits, \
                "activation_bits": self.activations_bits, \
                "n_classes": self.out_count,\
                "weights_matrix_lines": self.weights_matrix_lines, \
                "weights_matrix_columns": self.weights_matrix_columns, \
                "hidden_neurons": self.max_hidded_neurons,
                "values_shift":self.values_shift,
                "weight_shift":self.neuron_weight_position_shift}))

        

        if(self.out_count<=0):
            raise GeneticNeuronConfigurationError(
                """
                    The number of inputs({in_count}) on neuron does not match with data.shape. 
                    It must be greater than data.shape[1]. 
                    Perhaps you passed the datasite with wrong number of features ({features}).
                """.format(features=data.shape[1],in_count=self.in_count),errors=None)
        
        def fitness(genes):
            return self.classify(genes,data)[1] # 1 - for the error
        best_genes = self.ga.run(fitness, multiple=False)[0]
        self.best_genes =  best_genes