如何在此优化中包含不等式?

时间:2016-09-21 20:55:26

标签: python numpy optimization scipy

我正在使用scipy.optimize.minimize来最小化基于18个总方程的飞机机翼上的阻力。我对18个变量也有10个约束。当我运行代码时,我收到错误消息

  

ValueError:除了连接轴之外的所有输入数组维度必须完全匹配

我认为这是因为约束数组的大小和变量数组的大小不同;但是,我不确定使用类似的东西在现有变量上包含约束的最佳方法。

from __future__ import division
import os
import numpy as np
import scipy as sp
from scipy.optimize import minimize
import matplotlib.pyplot as plt

np.set_printoptions(suppress=True)
os.system('clear')

S = 6
W = 10
rho = 0.0023769                                                                                
mu = 3.38*10**(-7)

def func(x):
    """ Drag function to minimize """
    return (1/2)*rho*x[1]**2*x[12]*x[3]


def func_deriv(x):

    dDdv = rho*x[1]*x[12]*x[3]
    dDdCD = (1/2)*rho*x[1]**2*x[3]
    dDdS = (1/2)*rho*x[1]**2*x[12]
    return np.array([ dDdv, dDdCD, dDdS ])


# Put the constraints here
cons = ({'type': 'eq', # Calculates the required lift coefficient, CL [-]
     'fun' : lambda x: np.array([x[0] - 2*W/(rho*x[1]**2*x[3])]),
     'jac' : lambda x: np.array([1,(4*W)/(rho*x[1]**3*x[3]),0,(2*W)/(rho*x[1]**2*x[3]**2),0,0,0,0,0,0,0,0,0,0,0,0,0,0])},
    {'type': 'eq', # Calculates FAA ground speed, vFAA [ft/s]
     'fun' : lambda x: np.array([x[1] - x[2]*np.cos(x[13])]),
     'jac' : lambda x: np.array([0,1,-np.cos(x[13]),0,0,0,0,0,0,0,0,0,0,x[2]*np.sin(x[13]),0,0,0,0])},
    {'type': 'eq', # Calculates the span, b [ft]
     'fun' : lambda x: np.array([x[4] - x[3]/((1+x[14])*x[5])]),
     'jac' : lambda x: np.array([0,0,0,-1/((1+x[14])*x[5]),1,x[3]/((1+x[14])*x[5]**2),0,0,0,0,0,0,0,0,0,x[3]/((1+x[14])**2*x[5]),0,0])},
    {'type': 'eq', # Calculates the tip chord, ct [ft]
     'fun' : lambda x: np.array([x[6] - x[14]*x[5]]),
     'jac' : lambda x: np.array([0,0,0,0,0,-x[14],1,0,0,0,0,0,0,0,-x[5],0,0,0])},
    {'type': 'eq', # Calculates the Reynolds Number, Re [-]
     'fun' : lambda x: np.array([x[7] - (rho*x[1]*x[6])/mu]),
     'jac' : lambda x: np.array([0,-(rho*x[6])/mu,0,0,0,0,-(rho*x[1])/mu,1,0,0,0,0,0,0,0,0,0,0])},
    {'type': 'eq', # Calculates the FAA wing area, S [ft^2]
     'fun' : lambda x: np.array([x[3] - 1838*W/(x[2]**2)]),
     'jac' : lambda x: np.array([0,0,3676*W/x[3]**3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0])},
    {'type': 'eq', # Calculates the induced drag coefficient, CDi [-]
     'fun' : lambda x: np.array([x[10] - x[0]**2/(np.pi*x[9]*x[8])]),
     'jac' : lambda x: np.array([(-2*x[0])/(np.pi*x[9]*x[8]),0,0,0,0,0,0,0,x[0]**2/(np.pi*x[9]*x[8]**2),x[0]**2/(np.pi*x[9]**2*x[8]),1,0,0,0,0,0,0,0])},
    {'type': 'eq', # Calculates the parasitic drag coefficient, CDo [-]
     'fun' : lambda x: np.array([x[10] - x[11]]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0])},
    {'type': 'eq', # Calculates the aspect ratio, [-]
     'fun' : lambda x: np.array([x[9] - x[4]**2 / x[3]]),
     'jac' : lambda x: np.array([0,0,0,x[4]**2/x[3]**2,-2*x[4] / x[3],0,0,0,0,1,0,0,0,0,0,0,0,0])},
    {'type': 'eq', # Calculates the total drag coefficient, CDt [-]
     'fun' : lambda x: np.array([x[12] - x[10] - x[11]]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,0,-1,-1,1,0,0,0,0,0])},
    {'type': 'eq',
     'fun' : lambda x: np.array([x[1] - (4/(np.pi*x[9]*x[8]*x[11]))**(1/4)*np.sqrt((W/x[5])/rho)]),
     'jac' : lambda x: np.array([0,1,0,0,0, \
                                 (-W*(1/(x[9]*x[8]*x[11])))/(np.sqrt(2)*np.pi**(1/4)*rho*x[5]**2*np.sqrt(W/(rho*x[5]))), \
                                 0,0, \
                                 -np.sqrt(W/(rho*x[5]))/(2*np.sqrt(2)*np.pi**(1/4)*x[9]*x[8]**2*x[11]*(1/x[9]*x[8]*x[11])**(3/4)), \
                                 -np.sqrt(W/(rho*x[5]))/(2*np.sqrt(2)*np.pi**(1/4)*x[8]*x[9]**2*x[11]*(1/x[9]*x[8]*x[11])**(3/4)),
                                 0, \
                                 -np.sqrt(W/(rho*x[5]))/(2*np.sqrt(2)*np.pi**(1/4)*x[9]*x[11]**2*x[8]*(1/x[9]*x[8]*x[11])**(3/4)), \
                                 0,0,0,0,0,0])},
    {'type': 'eq', # Calculates the quarter chord sweep angle [rad]
     'fun' : lambda x: np.array([x[17] - np.arctan((.25*(x[6]-x[5]) + x[4]*np.tan(x[13]))/x[4])]),
     'jac' : lambda x: np.array([0,0,0,0,\
                                 -((np.tan(x[13])/x[4])-(x[4]*np.tan(x[13])+0.25*(x[6]-x[5]))/(x[4]**2))/((x[4]*np.tan(x[13])+0.25*(x[6]-x[5]))**2/(x[4]**2)+1), \
                                 0.25/(x[4]*((x[4]*np.tan(x[13])+0.25*(x[6]-x[5]))**2/(x[4]**2)+1)), \
                                 -0.25/(x[4]*((x[4]*np.tan(x[13])+0.25*(x[6]-x[5]))**2/(x[4]**2)+1)), \
                                 0,0,0,0,0,0,\
                                 -(1/np.cos(x[13]))/((x[4]*np.tan(x[13])+0.25*(x[6]-x[5]))**2/(x[4]**2)+1), \
                                 0,0,0,1])},
    {'type': 'eq', # Calculates the change in taper ratio [-]
     'fun' : lambda x: np.array([x[15] + 0.357 - 0.45*np.exp(0.0375*x[17])]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,-0.016875*np.exp(0.0375*x[17])])},
    {'type': 'eq', # Calculates the Nita function for e
     'fun' : lambda x: np.array([x[16] - 0.0524*(x[14]-x[15])**4 - 0.1500*(x[14]-x[15])**3 + 0.1659*(x[14]-x[15])**2 - 0.0706*(x[14]-x[15]) + 0.0119]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0, \
                                 0.2096*(x[14]-x[15])**3 - 0.45*(x[14]-x[15])**2 + 0.3318*(x[14]-x[15]) - 0.0706, \
                                 -0.2096*(x[14]-x[15])**3 + 0.45*(x[14]-x[15])**2 - 0.3318*(x[14]-x[15]) + 0.0706,
                                 1,0])},
    {'type': 'eq', # Calculates the Oswald efficiency factor, e [-]
     'fun' : lambda x: np.array([x[8] - 1/(1+x[16]*x[9])]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,1,1/(1+x[16]*x[9]**2), \
     0,0,0,0,0,0,1/(1+x[16]**2*x[9]),0])},
    {'type':'ineq', # Ensures that sweep > 0
     'fun' : lambda x: np.array([x[14]]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0])},
    {'type':'ineq', # Ensures that sweep < 1.1
     'fun' : lambda x: np.array([np.pi/4 - x[0]]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0])},
    {'type':'ineq', # Ensures that CL > 0
     'fun' : lambda x: np.array([x[0]]),
     'jac' : lambda x: np.array([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])},
    {'type':'ineq', # Ensures that CL < 1.1
     'fun' : lambda x: np.array([1.1 - x[0]]),
     'jac' : lambda x: np.array([-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])},
    {'type':'ineq', # Ensures that tr > 0.2
     'fun' : lambda x: np.array([x[14] - 0.2]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0])},
    {'type':'ineq', # Ensures that tr < 1
     'fun' : lambda x: np.array([1 - x[14]]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0])},
    {'type':'ineq', # Ensures that ground speed > 25 kts
     'fun' : lambda x: np.array([x[2] - 42.2]),
     'jac' : lambda x: np.array([0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])},
    {'type':'ineq', # Ensures tha A > 4
     'fun' : lambda x: np.array([x[9] - 4]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0])},
    {'type':'ineq', # Ensures tha A < 20
     'fun' : lambda x: np.array([20 - x[9]]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0])},
    {'type': 'ineq', # Set Re > 100k to avoid Reynold's drag
     'fun' : lambda x: np.array([x[7]-100000]),
     'jac' : lambda x: np.array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0])})


res = minimize(func, [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], args=(), jac=func_deriv,
           constraints=cons, method='SLSQP', options={'disp': True})


print res.x

我对编码比较新,所以这可能完全错了,但我很确定它很接近。

0 个答案:

没有答案