0

I need to constrain a variable that is argument of optimization below a certain value EQ<1000. #function that produces the variable is the following:

def fEQ(iEQ, iC, iTATM, index):
    if index==0:
       return iC[0]
    else:
       return iEQ[0]/psi*iTATM[index]**2

t = np.arange(1, 101)
NT = len(t)
EQ=np.zeros(NT)

#The objective function (omitted some functions to make some clarity - they are all interrelated in reality)

def fOBJ(x,sign,iI, iCPRICE,iEQ,iPERIODU,iCEMUTOTPER,iRI,iNT):

iMIU = x[0:NT]
iS = x[NT:(2*NT)]

for i in range(iNT):
    iCPRICE[i] = fCPRICE(iMIU,i)
    iI[i] = fI(iS,iY,i)
    iEQ[i]=fEQ(iEQ,iC,iTATM,i)
    iPERIODU[i] = fPERIODU(iC,iEQ,il,i)
    iCEMUTOTPER[i] = fCEMUTOTPER(iPERIODU,il,i)
    iRI = fRI(iCPC,i)
    
resUtility = np.zeros(1)
fUTILITY(iCEMUTOTPER, resUtility)

return sign*resUtility[0]

#Optimization

result = opt.minimize(fOBJ, x_start, args=(-1.0,CPRICE,I,EQ,PERIODU,CEMUTOTPER,RI,NT), method='SLSQP',bounds = tuple(bnds),options={'disp': True, 'maxiter':1000 }, constraints={'type':'ineq','fun':lambda EQ: EQ[NT]-1000})

Output of the optimization is "Positive directional derivative for linesearch (Exit mode 8)", it cannot find an optimum. Hovewer I have the suspect that my constraint is not fully working. Any suggestions?

tf_247
  • 1
  • 1
  • Can you please show a minimum working example? – NNN Oct 08 '20 at 11:34
  • @Nachiket what do you mean exactly? If I remove the constraint from the objective function the optimization works.Could you tell me if it is correctly specified? The variable EQ in any case results higher than the constraint. (It has the same value in the constrained and unconstrained optimazation) – tf_247 Oct 08 '20 at 12:04
  • I think your example is too long to debug, at least for me. Could you please make a smaller example that still reproduces the error? – NNN Oct 08 '20 at 12:06
  • Made some edits, reducing the objective function, maybe it is slightly clearer. – tf_247 Oct 08 '20 at 12:20
  • Opened a new question with MWE: https://stackoverflow.com/questions/64504648/variable-constraint-sci-py-minimize – tf_247 Oct 23 '20 at 19:04

0 Answers0