Please write a genetic algorithm in Python to calculate the maximum value of function f (x)=0.4+sinc (4x)+1.1sinc (4x+2)+0.8sinc (X-2)+0.7sinc (6x-4) between -2 and 2. The function solution is represented by a 16 bit binary number. The population size is 30, the crossover probability is 0.3, the mutation probability is 0.01, and the number of iterations is 400
时间: 2024-03-27 15:41:26 浏览: 72
Sure, here's an implementation of a genetic algorithm in Python to maximize the given function:
```python
import random
import math
# Define the fitness function
def fitness_function(chromosome):
x = decode_chromosome(chromosome)
return 0.4 + math.sin(4*x) / (4*x) + 1.1*math.sin(4*x+2) / (4*x+2) + 0.8*math.sin(x-2) / (x-2) + 0.7*math.sin(6*x-4) / (6*x-4)
# Define the chromosome decoding function
def decode_chromosome(chromosome):
x = int(chromosome, 2)
return -2 + 4 * x / (2**16 - 1)
# Define the random initialization function
def generate_chromosome():
return bin(random.randint(0, (2**16 - 1)))[2:].zfill(16)
# Define the selection function
def selection(population, fitnesses):
total_fitness = sum(fitnesses)
probabilities = [fitness / total_fitness for fitness in fitnesses]
selected = random.choices(population, probabilities, k=2)
return selected[0], selected[1]
# Define the crossover function
def crossover(chromosome1, chromosome2, probability):
if random.random() < probability:
point = random.randint(1, 15)
child1 = chromosome1[:point] + chromosome2[point:]
child2 = chromosome2[:point] + chromosome1[point:]
return child1, child2
else:
return chromosome1, chromosome2
# Define the mutation function
def mutation(chromosome, probability):
mutated = ''
for bit in chromosome:
if random.random() < probability:
mutated += '0' if bit == '1' else '1'
else:
mutated += bit
return mutated
# Set up the GA parameters
population_size = 30
crossover_prob = 0.3
mutation_prob = 0.01
num_iterations = 400
# Initialize the population
population = [generate_chromosome() for i in range(population_size)]
# Run the GA
for i in range(num_iterations):
# Evaluate the fitness of each chromosome
fitnesses = [fitness_function(chromosome) for chromosome in population]
# Select two parents
parent1, parent2 = selection(population, fitnesses)
# Perform crossover
child1, child2 = crossover(parent1, parent2, crossover_prob)
# Perform mutation
child1 = mutation(child1, mutation_prob)
child2 = mutation(child2, mutation_prob)
# Replace two worst chromosomes with new children
fitnesses = [fitness_function(chromosome) for chromosome in population]
worst1 = fitnesses.index(min(fitnesses))
fitnesses[worst1] = float('inf')
worst2 = fitnesses.index(min(fitnesses))
population[worst1], population[worst2] = child1, child2
# Find the best solution
fitnesses = [fitness_function(chromosome) for chromosome in population]
best_index = fitnesses.index(max(fitnesses))
best_chromosome = population[best_index]
best_x = decode_chromosome(best_chromosome)
best_fitness = fitness_function(best_chromosome)
print(f"Best solution: x={best_x}, f(x)={best_fitness}")
```
This code initializes a population of 30 random chromosomes, evaluates their fitness using the given fitness function, and performs selection, crossover, and mutation operations to create new children. The two worst chromosomes in the population are then replaced by the new children, and this process is repeated for 400 iterations. Finally, the best solution is found by selecting the chromosome with the highest fitness value.
Note that this implementation uses binary representation of the chromosome, where each chromosome is a 16-bit binary string. The `decode_chromosome` function is used to convert this binary string into a floating-point value between -2 and 2, which is then used to evaluate the fitness of the chromosome.
阅读全文