使用scipy.optimize.minimize编写目标函数的排障问题
桥梁预制护栏参数化设计优化问题
问题背景
目标是将预制护栏拆分为最少数量的独特类型,分为普通护栏与带灯杆护栏两类(附三跨桥梁示意图)。
参数定义
- 带灯杆护栏宽度为w
- 带灯杆护栏的均匀间距为s
- 灯杆起始点由变量a定义
- 桥梁跨径为数组spans
- x为所有距离需满足整除的基准宽度
核心思路
尽可能多使用宽度为x的护栏,最小化跨径内的剩余填充长度。需确保数组m中的所有距离(含灯杆间间距s-w)均能被x整除,引入arr数组元素补偿非整除的剩余长度。
实现代码
from scipy.optimize import minimize import numpy as np s = 12500 spans = np.array([25450, 25100, 25450]) ejs = np.zeros(len(spans)-1) # expansion or separation joints . ignore for now min_bw, max_bw = 0, 1200 #light pole barrier width consts = abs((len(spans) * 2 - 1)) # Number of distance elements [b,c,d...] init = np.array([1500, 1500, 6250]) #x, w, a + w/2 n = (spans - s / 2) // s # consecutive spacings of barriers for a given span. At times we have more m = np.ones(len(spans) * 2) # the length of the equation array that has to be divisible def objective(arr, n, s, spans, ejs): x = arr[0] w = arr[1] a = arr[2] # (the offset is until the mid axis of the light pole) m = np.ones(len(spans) * 2 + 1) # We make room for the last constraint h=(s-w) m[0] = a - w / 2 for i in range(1, len(spans) * 2): j = (i - 1) // 2 if i % 2 == 0: m[i] = (s - w) - (m[i - 1] + ejs[j] + arr[2 + i]) else: m[i] = spans[j] - (m[i - 1] + n[j] * s + w + arr[2 + i]) m[-1] = s - w # We also need the light distance between two poles to be divisible by x #print(m) return np.sum(m%x) #Bounds bounds = np.array([(1500, 2100), (1500, 2600), (3000, s)]) extras = np.repeat([(min_bw, max_bw)], consts, axis=0) # Add the bounds for each additional element all_bounds = np.concatenate((bounds, extras), axis=0) initial = np.pad(init, (0, consts), 'constant', constant_values=min_bw) sol = minimize(fun=objective, x0=initial, bounds=all_bounds, method='Nelder-Mead', args=(n, s, spans, ejs)) print(np.round(sol.x)) #m = [ 5500. , 5950., 5050. ,6050. ,4950. , 6500. ,11000.] #the print m statment
当前问题
np.sum(m%x)无法返回0,整除约束未满足,但优化函数显示成功终止,希望得到解决思路。
内容的提问来源于stack exchange,提问作者Dragos Seculin
相关产品推荐
相关产品推荐

