Multi-Input Multi-Output in Genetic algorithm (python) - Stack Overflow most recent 30 from stackoverflow.com 2026-04-14T08:08:07Z https://stackoverflow.com/feeds/question/64943711 https://creativecommons.org/licenses/by-sa/4.0/rdf https://stackoverflow.com/q/64943711 1 Multi-Input Multi-Output in Genetic algorithm (python) Hoàng Văn Hiếu https://stackoverflow.com/users/13626287 2020-11-21T13:40:57Z 2022-03-29T14:44:04Z <p>I wrote a GA program with python with 1 input, output and it works fine. But I want to find a solution with more input and output but I don't know how.</p> <p><a href="https://drive.google.com/file/d/1YFv9GSKqmw9X4QnH_vSnO7FPslPBGE0x/view?usp=sharing" rel="nofollow noreferrer">Example from https://pygad.readthedocs.io/: </a></p> <p>Given function: <code>y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6</code> with <code>input(x1:x6)=(4,-2,3.5,5,-11,-4.7)</code> and <code>y=44</code></p> <p><code>solution = (w1:w6)</code></p> <p>But I want to find a solution with more input and output like <code>input1 = (1,5,-3,5,-1,-4)</code>, <code>y1 = 50</code>.</p> https://stackoverflow.com/questions/64943711/-/65866809#65866809 1 Answer by Ahmed Gad for Multi-Input Multi-Output in Genetic algorithm (python) Ahmed Gad https://stackoverflow.com/users/5426539 2021-01-24T03:14:10Z 2021-01-24T03:14:10Z <p>Thanks for using <a href="https://pygad.readthedocs.io" rel="nofollow noreferrer">PyGAD</a>.</p> <p>You can find the example you are looking for at <a href="https://github.com/ahmedfgad/GeneticAlgorithmPython/blob/master/example.py" rel="nofollow noreferrer">this script</a>. Here is the code:</p> <pre class="lang-py prettyprint-override"><code>import pygad import numpy &quot;&quot;&quot; Given the following function: y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6 where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=44 What are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize this function. &quot;&quot;&quot; function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs. desired_output = 44 # Function output. def fitness_func(solution, solution_idx): # Calculating the fitness value of each solution in the current population. # The fitness function calulates the sum of products between each input and its corresponding weight. output = numpy.sum(solution*function_inputs) # The value 0.000001 is used to avoid the Inf value when the denominator numpy.abs(output - desired_output) is 0.0. fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001) return fitness fitness_function = fitness_func num_generations = 100 # Number of generations. num_parents_mating = 10 # Number of solutions to be selected as parents in the mating pool. # To prepare the initial population, there are 2 ways: # 1) Prepare it yourself and pass it to the initial_population parameter. This way is useful when the user wants to start the genetic algorithm with a custom initial population. # 2) Assign valid integer values to the sol_per_pop and num_genes parameters. If the initial_population parameter exists, then the sol_per_pop and num_genes parameters are useless. sol_per_pop = 20 # Number of solutions in the population. num_genes = len(function_inputs) init_range_low = -2 init_range_high = 5 parent_selection_type = &quot;sss&quot; # Type of parent selection. keep_parents = -1 # Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing. crossover_type = &quot;single_point&quot; # Type of the crossover operator. # Parameters of the mutation operation. mutation_type = &quot;random&quot; # Type of the mutation operator. mutation_percent_genes = 10 # Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists or when mutation_type is None. last_fitness = 0 def callback_generation(ga_instance): global last_fitness print(&quot;Generation = {generation}&quot;.format(generation=ga_instance.generations_completed)) print(&quot;Fitness = {fitness}&quot;.format(fitness=ga_instance.best_solution()[1])) print(&quot;Change = {change}&quot;.format(change=ga_instance.best_solution()[1] - last_fitness)) last_fitness = ga_instance.best_solution()[1] # Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor. ga_instance = pygad.GA(num_generations=num_generations, num_parents_mating=num_parents_mating, fitness_func=fitness_function, sol_per_pop=sol_per_pop, num_genes=num_genes, init_range_low=init_range_low, init_range_high=init_range_high, parent_selection_type=parent_selection_type, keep_parents=keep_parents, crossover_type=crossover_type, mutation_type=mutation_type, mutation_percent_genes=mutation_percent_genes, on_generation=callback_generation) # Running the GA to optimize the parameters of the function. ga_instance.run() # After the generations complete, some plots are showed that summarize the how the outputs/fitenss values evolve over generations. ga_instance.plot_result() # Returning the details of the best solution. solution, solution_fitness, solution_idx = ga_instance.best_solution() print(&quot;Parameters of the best solution : {solution}&quot;.format(solution=solution)) print(&quot;Fitness value of the best solution = {solution_fitness}&quot;.format(solution_fitness=solution_fitness)) print(&quot;Index of the best solution : {solution_idx}&quot;.format(solution_idx=solution_idx)) prediction = numpy.sum(numpy.array(function_inputs)*solution) print(&quot;Predicted output based on the best solution : {prediction}&quot;.format(prediction=prediction)) if ga_instance.best_solution_generation != -1: print(&quot;Best fitness value reached after {best_solution_generation} generations.&quot;.format(best_solution_generation=ga_instance.best_solution_generation)) </code></pre> <p>If you have any questions, please let me know!</p>