三分搜索算法python
时间: 2023-11-09 18:03:01 浏览: 56
三分搜索算法是一种用于在单峰函数中寻找极值的算法。其基本思想是将区间分成三个部分,然后判断目标值在哪个部分,进而缩小搜索范围。以下是一个简单的 Python 实现:
```python
def ternary_search(l, r, f):
while r - l > 1e-9:
m1 = l + (r - l) / 3
m2 = r - (r - l) / 3
if f(m1) < f(m2):
l = m1
else:
r = m2
return f(l)
```
其中 `l` 和 `r` 是搜索区间的左右端点,`f` 是目标函数。函数返回值为函数在搜索区间内的极值。
相关问题
分冶算法 python代码
分治算法(Divide and Conquer Algorithm)是一种将问题分解为更小的子问题,并将这些子问题合并为原始问题的解的方法。
以下是一个使用分治算法的Python代码示例,解决传入列表nums的最大子数组和问题:
```python
def max_subarray(nums):
if len(nums) == 1:
return nums[0]
mid = len(nums) // 2
left_max = max_subarray(nums[:mid]) # 递归求左侧子数组的最大和
right_max = max_subarray(nums[mid:]) # 递归求右侧子数组的最大和
# 计算跨越中间位置的子数组的最大和
cross_max = nums[mid]
left_sum = 0
for i in range(mid-1, -1, -1): # 从中间位置向左遍历
left_sum += nums[i]
cross_max = max(cross_max, left_sum)
right_sum = 0
for i in range(mid+1, len(nums)): # 从中间位置向右遍历
right_sum += nums[i]
cross_max = max(cross_max, right_sum)
return max(left_max, right_max, cross_max) # 返回三个最大和中的最大值
# 示例调用
nums = [-2,1,-3,4,-1,2,1,-5,4]
result = max_subarray(nums)
print(result)
```
以上代码通过递归的方式将原始问题分解为更小的子问题,然后再通过合并子问题的解来获得原始问题的解。在每一次递归中,通过分别计算左侧子数组、右侧子数组和跨越中间位置的子数组的最大和,然后返回三者中的最大值。最终得到的结果即为问题的解。
差分进化算法改进python
差分进化算法(DE)是一种优化算法,用于解决函数优化问题。它通过模拟生物进化的过程,通过变异和交叉操作来搜索最优解。在Python中,可以使用numpy库来实现差分进化算法。
以下是一个简单的差分进化算法的Python实现示例:
```python
import numpy as np
def differential_evolution(fitness_func, bounds, pop_size=50, F=0.8, CR=0.9, max_iter=100):
# 初始化种群
pop = np.random.uniform(bounds[:, 0], bounds[:, 1], (pop_size, len(bounds)))
best_solution = None
best_fitness = float('inf')
for i in range(max_iter):
for j in range(pop_size):
# 选择三个不同的个体作为变异向量
candidates = [idx for idx in range(pop_size) if idx != j]
a, b, c = np.random.choice(candidates, 3, replace=False)
# 变异操作
mutant = pop[a] + F * (pop[b] - pop[c])
# 交叉操作
trial = np.copy(pop[j])
for k in range(len(bounds)):
if np.random.rand() < CR:
trial[k] = mutant[k]
# 选择操作
trial_fitness = fitness_func(trial)
if trial_fitness < best_fitness:
best_solution = trial
best_fitness = trial_fitness
if trial_fitness <= fitness_func(pop[j]):
pop[j] = trial
return best_solution, best_fitness
# 示例适应度函数
def fitness_func(x):
return np.sum(x**2)
# 示例边界
bounds = np.array([[-5, 5], [-5, 5]])
# 运行差分进化算法
best_solution, best_fitness = differential_evolution(fitness_func, bounds)
print("Best solution:", best_solution)
print("Best fitness:", best_fitness)
```
这个示例中,我们定义了一个适应度函数`fitness_func`,它计算了解的适应度值。然后,我们定义了变量的边界`bounds`,用于限制解的取值范围。最后,我们调用`differential_evolution`函数来运行差分进化算法,并输出最佳解和最佳适应度值。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)