Scipy Optimization: ошибка с ограничениями - PullRequest
0 голосов
/ 26 февраля 2020

Я использую scipy для решения следующих проблем: enter image description here

Я получаю сообщение об ошибке при вызове функции минимизации из библиотеки scipy. Я считаю, что мои переменные и целевая функция определены правильно, но я не уверен насчет границ и ограничений. Спасибо за вашу помощь !!

Код:

import pandas as pd
import numpy as np
import scipy as sp
from scipy.optimize import minimize
import matplotlib.pyplot as plt
%matplotlib inline
step=80
f1load=[44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48,38,32,44,48,53,28,32,36,41,48,38,32,38,34,44,36,41,48]
fload=f1load[0:step+1]
i1load=[40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40]
iload=i1load[0:step+1]
load1=np.array(iload)-np.array(fload)
load1
load2=load1.tolist()
load=load2
x = np.zeros(80,dtype='i')
x = x.tolist()
y = np.zeros(80,dtype='f')
y = y.tolist()
def objective(x,load):
    obj2 = 0
#     obj1=sum(((load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i])) for i in range (n)))
    for i in range(step):
        obj1 = (load[i+1]-(6*x[i]))*(load[i+1]-(6*x[i]))
        obj2 = obj2 + obj1
    return obj2
iSOC = 50 #initial state of charge
tSOC = 65 #final state of charge
def constraint1(y):
    y1 = y[0]
    return y1 - iSOC
def constraint2(y):
    yend = y[step]
    return yend - tSOC
def constraint3(y,x):
    for i in range(step):
        con3[i] = y[i+1] - y[i] - 3.75*x[i]
    return con3
b = (-1.5,1.5)
bnds = (b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,\
        b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,\
        b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,\
        b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,\
        b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,\
        b,b,b,b,b)
con1 = {'type' : 'eq' , 'fun' : constraint1}
con3 = {'type' : 'eq' , 'fun' : constraint3}
con2 = {'type' : 'ineq' , 'fun' : constraint2}
cons = [con1,con3,con2]
sol = minimize(objective, x, method = 'SLSQP',\
              bounds = bnds, constraints = cons)

Полная ошибка трассировки стека:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-b961b5feff89> in <module>
      1 sol = minimize(objective, x, method = 'SLSQP',\
----> 2               bounds = bnds, constraints = cons)

~/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    616     elif meth == 'slsqp':
    617         return _minimize_slsqp(fun, x0, args, jac, bounds,
--> 618                                constraints, callback=callback, **options)
    619     elif meth == 'trust-constr':
    620         return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp,

~/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/slsqp.py in _minimize_slsqp(func, x0, args, jac, bounds, constraints, maxiter, ftol, iprint, disp, eps, callback, **unknown_options)
    311     # meq, mieq: number of equality and inequality constraints
    312     meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args']))
--> 313               for c in cons['eq']]))
    314     mieq = sum(map(len, [atleast_1d(c['fun'](x, *c['args']))
    315                for c in cons['ineq']]))

~/opt/anaconda3/lib/python3.7/site-packages/scipy/optimize/slsqp.py in <listcomp>(.0)
    311     # meq, mieq: number of equality and inequality constraints
    312     meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args']))
--> 313               for c in cons['eq']]))
    314     mieq = sum(map(len, [atleast_1d(c['fun'](x, *c['args']))
    315                for c in cons['ineq']]))

TypeError: constraint3() missing 1 required positional argument: 'x'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...