Javid Babayev
Javid Babayev

Reputation: 53

PyGAD GA - why there is mismatch in solutions and fitness arrays size?

I am using pygad, for GA, to find combination of solutions which would satisfy conditions. I have got a code, which runs 15 generations with 40 populations. When GA stops running, the size of solutions array is 640 where as fitness array is 600. I am looking for a single array which would have solutions for all trials with fitness array next to it.

Question: (a) is there any command which would bring those two groups together (b) if there is not such a command, then why fitness function size is less that "solutions", array?

Upvotes: 0

Views: 891

Answers (1)

Ahmed Gad
Ahmed Gad

Reputation: 866

This was a bug that is solved in PyGAD 2.16.1. Please consider updating the library to the latest release.

pip install --upgrade pygad

This is an example where a single array called solutions_fitness_arr that concatenates the fitness value at the end of each solution.

import numpy
import pygad

function_inputs = [4,-2,3.5,5,-11,-4.7]
desired_output = 44

def fitness_func(solution, solution_idx):
    output = numpy.sum(solution*function_inputs)
    fitness = 1.0 / numpy.abs(output - desired_output)
    return fitness

ga_instance = pygad.GA(num_generations=15,
                       num_parents_mating=4,
                       fitness_func=fitness_func,
                       sol_per_pop=40,
                       num_genes=6,
                       save_solutions=True,
                       suppress_warnings=True)

ga_instance.run()

print(ga_instance.solutions.shape)
print(len(ga_instance.solutions_fitness))

solutions_fitness_arr = numpy.zeros(shape=(ga_instance.solutions.shape[0], ga_instance.solutions.shape[1]+1))
solutions_fitness_arr[:, :6] = ga_instance.solutions.copy()
solutions_fitness_arr[:, 6] = ga_instance.solutions_fitness

Upvotes: 1

Related Questions