Why is my genetic algorithm not improving?

188 Views Asked by At

The fitness output is always 0 when my genetic algorithm is training. This should be normal at first but it does this for the entire duration of the training and does not improve at all. I have the training of the genetic algorithm set up so that it trains on small increments of data at a time instead of the entire training data array. The reason I am doing this is because I want the genetic algorithm to train on the most resent data last. This is a simplified version of what I am trying...

def trainModels(coin):

    global endSliceTraining, startSliceTraining # These are also used in the fitness function

    torch_ga = pygad.torchga.TorchGA(model=NeuralNetworkModel, num_solutions=10)

    while endSliceTraining < len(trainingData):

        ga_instance = pygad.GA(num_generations=10,
                            num_parents_mating=2,
                            initial_population=torch_ga.population_weights,
                            fitness_func=fitness_func,
                            parent_selection_type="sss", 
                            mutation_type="random",
                            mutation_by_replacement=True)
        
        ga_instance.run()

        solution, solution_fitness, solution_idx = ga_instance.best_solution()
 
        startSliceTraining += 1
        endSliceTraining += 1
    
def fitness_func(solution, solution_idx):
    global startSliceTraining, endSliceTraining
    
    stats = numpy.array(trainingData)

    statsSlice = stats[startSliceTraining:endSliceTraining]

    for stats in statsSlice:
        action = [0,0,0]
        stats = torch.tensor(stats, dtype=torch.float)
        prediction = pygad.torchga.predict(model=NeuralNetworks[currentCoinIndex],       solution=solution, data=stats)
        move = torch.argmax(prediction).item()
        action[move] = 1
        
        """
        I deleted the complicated part here. This area was not the problem. I hope what's above in this function is understandable
        """
    return ...

I'm thinking that maybe my parameters in the pygad.GA are wrong or that perhaps for some reason the Neural Network is not being transfered over to be used in the next set of data but I don't know.

Help would be appreciated, thank you!

0

There are 0 best solutions below