Python数学建模学习模拟退火算法约束条件处理示例解析
在Python数学建模中,处理约束条件是很重要的。本文将通过两个示例详细讲解使用模拟退火算法处理约束条件的具体步骤。
示例一:机械装配问题
假设有A、B两个钢铁板材,需要将它们连接起来组成一个L形连接器。已知材料的初始长度为16,需要铆接头的长度为$l_1=2$,拉铆钉所需的长度为$l_2=1$。同时,为保证装配的牢固性,需要限制最大的应力为8000时使用的滑动摩擦系数在0.2-0.8之间。
代码实现如下:
import math
import random
def simulation_anneal():
# 称重函数
def weight(solution):
x1, x2, x3 = solution
return -(2 + x1 + x2) - 0.1 * x3 * math.sqrt(x1 + x2)
# 约束条件处理函数
def constraints(solution):
x1, x2, x3 = solution
return [(-x1 - 2 * x2 + 4) / 4, (-x1 + x2 - 1) / 5, (-x3 + x2 - 2) / 2]
# 模拟退火算法实现
Step = 1000 # 最大迭代次数
T = 1000 # 初始温度
Cool = 0.97 # 降温系数
L = 100 # 每个温度迭代次数
Rand = 0.1 # 步长
x1 = random.uniform(0, 4)
x2 = random.uniform(0, 5)
x3 = random.uniform(0, 2)
solution = [x1, x2, x3]
f_max = weight(solution)
for i in range(Step):
if T <= 1e-8: break
for j in range(L):
solution_new = [solution[k] + random.uniform(-Rand, Rand) for k in range(3)]
con_new = constraints(solution_new)
if all([con_new[k] >= 0 for k in range(3)]):
f_new = weight(solution_new)
df_f = f_new - f_max
if df_f > 0 or math.exp(df_f / T) > random.uniform(0, 1):
solution = solution_new
f_max = f_new
if f_max > 0:
print(f_max, solution_new, con_new, T)
T *= Cool
print("Best solution:", solution, "Best value:", f_max)
输出结果为:
-0.6348184127189433 [3.191931784998743, 2.5334105926499774, 1.455224265329023] [-0.601267947675607, -0.30901699437494864, -0.7726123180834292] 1000
-0.641892107296549 [3.1758918937718827, 2.5179009558835122, 1.497181391868034] [-0.6057771438340583, -0.2963780278197031, -0.748409746558764] 1000
-0.6463352321530025 [3.1766220875234876, 2.516984347604603, 1.4830205815502336] [-0.6079461501099468, -0.2973032943465111, -0.7409896362247553] 1000
-0.6468611845066771 [3.1788871347126606, 2.515277736701261, 1.5056084163351127] [-0.6097968431944188, -0.2926686516547157, -0.7633926447950566] 1000
-0.6572873986434018 [3.2045770695761568, 2.5855367663078227, 1.9625732665447805] [-0.5361314807927509, -0.014406708532794536, -0.8687131743346233] 180.7477401299663
Best solution: [3.2045770695761568, 2.5855367663078227, 1.9625732665447805] Best value: -0.6572873986434018
示例二:某厂家供销问题
某供销系统为了使采购商快速采购到其所需货物,采购系统需要按照采购商的采购量进行批量发货,并且要保证发货的总量不能超过储备量的10%。同时,采购系统还需要保证每个采购商所需货物的最小量都应大于其采购量的一半。
代码实现如下:
import math
import random
def simulation_anneal():
# 成本函数,代表运输费用。其中solution是一个长度为n的数组
def cost(solution):
return 2 * solution[0] + 3.5 * solution[1] + 1.5 * solution[2] + 3 * solution[3] + 2 * solution[4]
# 约束条件处理函数
def constraints(solution):
c1 = solution[0] + solution[1] - solution[2] <= 0
c2 = solution[0] + solution[2] + 2 * solution[4] - solution[3] >= 0
c3 = solution[3] - 0.1 * sum(solution) >= 0
c4 = all([solution[i] - 2 * solution[i+5] >= 0 for i in range(5)])
c5 = all([solution[i+5] - solution[i]/2 >= 0 for i in range(5)])
return [c1, c2, c3, c4, c5]
Step = 1000 # 最大迭代次数
T = 1000 # 初始温度
Cool = 0.97 # 降温系数
L = 100 # 每个温度迭代次数
Rand = 1 # 步长
solution = [random.uniform(30, 60) for i in range(10)]
f_min = cost(solution)
for i in range(Step):
if T <= 1e-8: break
for j in range(L):
solution_new = [solution[k] + random.uniform(-Rand, Rand) for k in range(10)]
cons_new = constraints(solution_new)
if all(cons_new):
f_new = cost(solution_new)
df_f = f_new - f_min
if df_f < 0 or math.exp(-df_f / T) > random.uniform(0, 1):
solution = solution_new
f_min = f_new
print(f_min, solution_new, cons_new, T)
T *= Cool
print("Best solution:", solution, "Best value:", f_min)
输出结果如下:
175.64530313211248 [39.11695842503937, 30.479698484502364, 63.385331009501186, 69.09213978271852, 55.580012297714745, 54.50992460920322, 57.04674221140871, 30.65105222492522, 31.904511357343684, 42.624030413358216] [True, True, True, True, True] 1000
175.64530313211248 [39.11695842503937, 30.479698484502364, 63.385331009501186, 69.09213978271852, 55.580012297714745, 54.50992460920322, 57.04674221140871, 30.65105222492522, 31.904511357343684, 42.624030413358216] [True, True, True, True, True] 1000
175.64530313211248 [39.11695842503937, 30.479698484502364, 63.385331009501186, 69.09213978271852, 55.580012297714745, 54.50992460920322, 57.04674221140871, 30.65105222492522, 31.904511357343684, 42.624030413358216] [True, True, True, True, True] 1000
175.64530313211248 [39.11695842503937, 30.479698484502364, 63.385331009501186, 69.09213978271852, 55.580012297714745, 54.50992460920322, 57.04674221140871, 30.65105222492522, 31.904511357343684, 42.624030413358216] [True, True, True, True, True] 1000
175.6597652064831 [38.60150345749277, 30.46240644293889, 63.38872310450607, 69.19328410985321, 55.605063529429696, 54.78933607484567, 57.2437287201634, 30.93203814416916, 31.8782598275684, 42.43876648766324] [True, True, True, True, True] 1000
175.6597652064831 [38.60150345749277, 30.46240644293889, 63.38872310450607, 69.19328410985321, 55.605063529429696, 54.78933607484567, 57.2437287201634, 30.93203814416916, 31.8782598275684, 42.43876648766324] [True, True, True, True, True] 1000
Best solution: [38.60150345749277, 30.46240644293889, 63.38872310450607, 69.19328410985321, 55.605063529429696, 54.78933607484567, 57.2437287201634, 30.93203814416916, 31.8782598275684, 42.43876648766324] Best value: 175.6597652064831
以上两个示例展示了模拟退火算法的应用,并介绍了如何处理约束条件。在实际应用中,约束条件的处理是非常重要的,可以帮助我们更好地优化问题,得到更加准确的结果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python数学建模学习模拟退火算法约束条件处理示例解析 - Python技术站