from pygad import pygad
def fitness_func(bin_param):
return sum(bin_param)
parameters = {
'num_generations': 10,
'population_size': 10,
'num_parents': 5,
'num_genes': 5,
'gene_mutation_prob': 0.05,
'parent_mating_prob': 0.8,
'num_elites': 3,
'stop_condition': 1,
'fitness_func': fitness_func
}
import PyGAD
def func_fitness(x):
func_result = sum(x)
return func_result
x = [5,5,5,5]
print(func_fitness(x))
# import the library
import pandas as pd
# import the dataset
dataframe = pd.read_csv("/content/sample_data/california_housing_test.csv")
# show the data
dataframe.head()
import pygad as pg
import numpy as np
# Defining the fitness function. This function receives a list of bit-strings (i.e. the chromosome).
def fitness_func(chromosome):
# The chromosome is a numpy array of 32-bits. Therefore, we can compute the floating-point number as follows.
chromosome_sum = np.sum(chromosome)
# We return the fitness value (the objective value) of the chromosome.
return chromosome_sum
# The solver performs the minimization of the fitness function. Therefore, the lower the objective value
# the better the solution.
# The user can specify the `number_of_bits` which is the number of bits per chromosome.
solver = pg.genetic_algorithm(population_size=50, number_of_generations=50, fitness_func=fitness_func,
number_of_bits=32)
# Solving the problem (running the optimization process).
solver.run()
# Getting the best solution after running the optimization process.
best_solution
from pygad import GA
import numpy as np
# Define the function that the GA will optimise.
def fitness_function(solution):
x, y = solution
return (x - 2) ** 2 + (y - 2) ** 2
ga = GA(num_generations=50,
sol_per_pop=50,
num_parents_mating=10,
fitness_func=fitness_function,
crossover_prob=0.7,
mutation_prob=0.1,
lower_bound=[-100, -100],
upper_bound=[100, 100])
ga.run()
print(ga.best_solution())
print(ga.best_solution_fitness())
import pygad
def func(x):
return x[0] ** 2 + x[1] ** 2
best_solution, best_fitness = pygad.solve(func=func, num_generations=100, num_parents_mating=4,
sol_per_pop=8, num_genes=2)
import pgmpy
from pgmpy.models import BayesianModel
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination
import numpy as np
familiar_model = BayesianModel([('luz', 'persona'), ('perro', 'persona')])
cpd_luz = TabularCPD(variable='luz', variable_card=2,
values=np.array([[0.5, 0.9], [0.5, 0.1]]),
evidence=['persona'], evidence_card=[2])
cpd_perro = TabularCPD(variable='perro', variable_card=2,
values=np.array([[0.9, 0.1], [0.1, 0.9]]),
evidence=['persona'], evidence_card=[2])
cpd_persona = TabularCPD(variable='persona', variable_card=2,
values=np.array([[0.9, 0.2], [
from pygad import pygad
def fitness_function(solution):
"""
This is a fitness function which returns the solution's value as is.
"""
return solution
# Initializing the PyGAD class with the following parameters:
# 1. sol_per_pop: This is the number of solutions in the population (e.g. number of chromosomes).
# 2. num_generations: This is the number of generations (or iterations).
# 3. num_parents_mating: The number of solutions to be selected as parents in the mating process.
# 4. fitness_func: This is the fitness function which is used to evaluate the solutions in the population.
# 5. sol_dimension: This is the dimension of each solution in the population (e.g. the number of parameters of the problem)
# 6. maximize: Set to True if the problem is to be maximized. Otherwise, set to False.
# 7. mutation_percent_genes: The percentage of genes to mutate in the mutation process.
num_generations = 5
sol_per_pop = 8
import PyGAD as pg
import random
# Defining the fitness function for the genetic algorithm to optimize.
def fitness_func(x):
return (x[0] - 5)**2 + (x[1] - 5)**2 + (x[2] - 5)**2 + (x[3] - 5)**2 + (x[4] - 5)**2
# Defining the lower and upper bounds for the variables.
lower_bound = [0, 0, 0, 0, 0]
upper_bound = [10, 10, 10, 10, 10]
# Initializing the genetic algorithm to solve the problem.
ga_instance = pg.genetic_algorithm(parameters_ga)
# Running the genetic algorithm to solve the problem.
ga_instance.run()
# Getting the best solution after running the genetic algorithm.
best_solution, best_fitness, fitness_list = ga_instance.best_solution()
import pygad as pg
sol = pg.solve(func)
print(sol.best_solution)
print(sol.best_solution_fitness)