python 优化算法
时间: 2024-01-18 10:18:24 浏览: 80
python中的优化
以下是几种常见的Python优化算法的示例:
1. 梯度下降法(Gradient Descent)
```python
def gradient_descent(f, df, x0, learning_rate, num_iterations):
x = x0
for i in range(num_iterations):
gradient = df(x)
x -= learning_rate * gradient
return x
# 示例:求解函数 f(x) = x^2 的最小值
f = lambda x: x**2
df = lambda x: 2*x
x0 = 1.0
learning_rate = 0.1
num_iterations = 100
result = gradient_descent(f, df, x0, learning_rate, num_iterations)
print("最小值:", result)
```
2. 共轭梯度法(Conjugate Gradient)
```
import numpy as np
def conjugate_gradient(A, b, x0, num_iterations):
x = x0
r = b - np.dot(A, x)
p = r
for i in range(num_iterations):
Ap = np.dot(A, p)
alpha = np.dot(r, r) / np.dot(p, Ap)
x += alpha * p
r_new = r - alpha * Ap
beta = np.dot(r_new, r_new) / np.dot(r, r)
p = r_new + beta * p
r = r_new
return x
# 示例:求解线性方程组 Ax = b
A = np.array([[3, 2], [2, 6]])
b = np.array([2, -8])
x0 = np.array([0, 0])
num_iterations = 10
result = conjugate_gradient(A, b, x0, num_iterations)
print("解:", result)
```
3. 遗传算法(Genetic Algorithm)
```python
import random
def genetic_algorithm(fitness_func, num_generations, population_size, mutation_rate):
population = [random.randint(0, 1) for _ in range(population_size)]
for _ in range(num_generations):
fitness_scores = [fitness_func(individual) for individual in population]
new_population = []
for _ in range(population_size):
parent1, parent2 = random.choices(population, weights=fitness_scores, k=2)
child = crossover(parent1, parent2)
child = mutate(child, mutation_rate)
new_population.append(child)
population = new_population
best_individual = max(population, key=fitness_func)
return best_individual
# 示例:求解二进制字符串中1的个数最大的个体
def fitness_func(individual):
return sum(individual)
def crossover(parent1, parent2):
crossover_point = random.randint(1, len(parent1) - 1)
child = parent1[:crossover_point] + parent2[crossover_point:]
return child
def mutate(individual, mutation_rate):
mutated_individual = []
for gene in individual:
if random.random() < mutation_rate:
mutated_individual.append(1 - gene)
else:
mutated_individual.append(gene)
return mutated_individual
num_generations = 100
population_size = 100
mutation_rate = 0.01
result = genetic_algorithm(fitness_func, num_generations, population_size, mutation_rate)
print("最优个体:", result)
```
阅读全文