Python Forum

Full Version: GRG Nonlinear maximize
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Looking for the equivalent of a GRG nonlinear excel solver for Python.  It is to determine the optimal stock plan.

As input there would be one array.

There will be a second array that would be based calculation on input values in first array. However there is a final sum single value of this array. The objective is to maximize this value, by populating the elements of the second array.

In excel Max(value), after setting the equations on the second array, works well. We need this implemented now in Python.

I reviewed some scipy packages. Was unable to get something.

There is a Choco solver in Java that does similar.
Have you seen this blog? https://docs.scipy.org/doc/scipy-0.13.0/...onlin.html

No mention of generalized reduced gradient however
Yes, had visited this blog, amazingly good information.

However, I need to populate the array, it is not root. Two dimensional array, one axis of stocks and other axis of Accounts. The values in this array should be such that the sum is maximum.
Code so far.... Having issue with right package.

import scipy as sp
import scipy.optimize as optimize
import numpy as np
import numpy.matlib as mb
np.set_printoptions(threshold=np.nan)
with open('H:\Projects\HH\dat\srcd1.txt', 'rb') as f:
    line = next(f) # read 1 line
    n = len(line.split('~'))
    
HHFlag=np.loadtxt(fname="H:\Projects\HH\dat\srcd1.txt",dtype=np.str, delimiter='~', skiprows=1, usecols=(1, ))
SSVal= np.loadtxt(fname="H:\Projects\HH\dat\srcd1.txt",dtype=np.int64, delimiter='~', skiprows=1, usecols=range(2,n-1) )
SSs= np.loadtxt(fname="H:\Projects\HH\dat\srcd1.txt", dtype=np.str, delimiter='~', skiprows=1, usecols=(0,) )
SSLns= np.loadtxt(fname="H:\Projects\HH\dat\srcd1.txt", dtype=np.int64, delimiter='~', skiprows=1, usecols=(n-1,))
MrgnLn=SSVal[5,:]
x,y=SSVal.shape


SSLns=np.delete(SSLns,5,axis=0)
SSVal= np.delete(SSVal,(5),axis=0)
SSLnAlcn= np.zeros(shape=(x-1,y),dtype=np.int64)
HHSS = []
for m in range(0,x-1):
    if HHFlag[m]=='Y':
       HHSS.append(SSVal[m,:])
HHSSs=np.array(HHSS)

HHSS = []
for m in range(0,x-1):
    if HHFlag[m]=='N':
       HHSS.append(SSVal[m,:])
NHHSSs=np.array(HHSS)

HH=np.sum(HHSSs,axis=0)
NHH=np.sum(NHHSSs,axis=0) 
Cl=MrgnLn*1.4

def Func1(SSLnAlcnP):  
    return SSLnAlcn.sum

    
def ConsSSLn(x):
    return np.subtract(SSLns, np.sum(SSLnAlcn,axis=1))
def ConsCl(x):
    return np.subtract(Cl,np.sum(x,axis=0))

cons=({'type':'ineq','fun':ConsSSLn},
      {'type':'ineq','fun':ConsCl}
        )

FinalVal= optimize.minimize(Func1, method='COBYLA',constraints=cons,args=SSLnAlcn) 
print FinalVal