Exemplo n.º 1
0
    def trainGA(self,
                train_data,
                target_data,
                cf,
                loss_fn=None,
                verbose=False):
        """ Train web with Genetic Algorithm """

        train_data, target_data = self.check_cuda(train_data, target_data)

        self.check_graph()

        # prepare config object with information of web, # of genes, partitions, genomes, etc
        self.prepare_config_obj(cf, loss_fn)
        # initialize genepool
        genepool = Evolution.GenePool(cf)
        # stores which indices of self.parameters to change during training
        self.set_dict_indices_from_pool(genepool.pool[0])

        # np arrays to save genePools, outputs and fitness
        geneArray = np.zeros((cf.generations, cf.genomes, cf.genes))
        outputArray = np.zeros((cf.generations, cf.genomes,
                                train_data.shape[0], self.nr_output_vertices))
        fitnessArray = np.zeros((cf.generations, cf.genomes))

        # Temporary arrays, overwritten each generation
        fitnessTemp = np.zeros((cf.genomes, cf.fitnessavg))
        outputAvg = torch.zeros(cf.fitnessavg,
                                train_data.shape[0],
                                self.nr_output_vertices,
                                device=self.cuda)
        outputTemp = torch.zeros(cf.genomes,
                                 train_data.shape[0],
                                 self.nr_output_vertices,
                                 device=self.cuda)

        for i in range(cf.generations):
            for j in range(cf.genomes):
                for avgIndex in range(cf.fitnessavg):
                    # update parameters of each network
                    self.set_parameters_from_pool(genepool.pool[j])
                    self.forward(train_data)
                    outputAvg[avgIndex] = self.get_output()
                    # use negative loss as fitness for genepool.NextGen()
                    fitnessTemp[j, avgIndex] = -cf.Fitness(
                        outputAvg[avgIndex], target_data).item()

                outputTemp[j] = outputAvg[np.argmin(fitnessTemp[j])]

            genepool.fitness = fitnessTemp.min(1)  # Save best fitness

            # Save generation data
            geneArray[i, :, :] = genepool.pool
            outputArray[i, :, :] = outputTemp
            fitnessArray[i, :] = genepool.fitness

            if verbose:
                print("Generation nr. " + str(i + 1) + " completed")
                print("Best fitness: " + str(-max(genepool.fitness)))

            genepool.NextGen()

        return geneArray, outputArray, fitnessArray
Exemplo n.º 2
0
fitnessTemp = np.zeros((cf.genomes, cf.fitnessavg))
outputAvg = np.zeros((cf.fitnessavg, len(x[0])))
outputTemp = np.zeros((cf.genomes, len(x[0])))
controlVoltages = np.zeros(cf.genes)

# Initialize save directory
saveDirectory = SaveLib.createSaveDirectory(cf.filepath, cf.name)

# Initialize main figure
mainFig = PlotBuilder.initMainFigEvolution(cf.genes, cf.generations, cf.genelabels, cf.generange)

# Initialize instruments
ivvi = InstrumentImporter.IVVIrack.initInstrument()

# Initialize genepool
genePool = Evolution.GenePool(cf)

#%% Measurement loop

for i in range(cf.generations):
    for j in range(cf.genomes):
        # Set the DAC voltages
        for k in range(cf.genes-1):
            controlVoltages[k] = genePool.MapGenes(
                                    cf.generange[k], genePool.pool[j, k])
        InstrumentImporter.IVVIrack.setControlVoltages(ivvi, controlVoltages)
        time.sleep(1)  # Wait after setting DACs

        # Set the input scaling
        x_scaled = x * genePool.MapGenes(cf.generange[-1], genePool.pool[j, -1])
Exemplo n.º 3
0
'''This test is for testing the Evolution module. The fitness function is
1/(1 + y.T*y), where y is a genome. In other words, the GA should arrive at
(unique) optimal solution y=0 with fitness = 1'''

import numpy as np
import matplotlib.pyplot as plt
import config_evolution_test as cf
import SkyNEt.modules.Evolution as evo

# Initialize config object
conf_obj = cf.experiment_config()

# Initialize genepool
pool = evo.GenePool(conf_obj)

# Initialize plotting variables
fitness = np.zeros((conf_obj.generations))

# Evolution loop
for i in range(conf_obj.generations):
    for j in range(pool.genomes):
        pool.fitness[j] = conf_obj.Fitness(pool.pool[j].T)
    fitness[i] = max(pool.fitness)  # Save fitness for plotting
    print('Highest fitness of generation ' + str(i) + ': ' +
          str(max(pool.fitness)))
    print('Best genome of generation ' + str(i) + ': ' +
          str(pool.pool[np.argmax(pool.fitness)]))
    pool.NextGen()

passed_test = np.linalg.norm(pool.pool[0] - 0) < 1E-12