I am working on writing a repeatable script that uses the scipy optimize package. As part of this, you need to create functions of constraints. I am wanting to create a repeatable script that allows me to create the right number of constraint functions, based on a variable number of inputs.
For example:
def constraintx1(x):
return (x2c2*(x[1]**2))+(x1c2*x[1]) + maxresponsec2
def constraintx2(x):
return (x2c3*(x[2]**2))+(x1c3*x[2]) + maxresponsec3
def constraintx3(x):
return (x2c4*(x[3]**2))+(x1c4*x[3]) + maxresponsec4
constraints = [{'type':'ineq', 'fun':constraintx1},
{'type':'ineq', 'fun':constraintx2},
{'type':'ineq', 'fun':constraintx3}]
all the x2c2, x1c2 and maxresponsec2 are coming from an input table defined earlier as curvestable.
These are some of my constraint functions, for 3 input variables. However, with another project, I might need to repeat this for 12 variables and I am hoping to create a loop that will generate the right number of constraint functions, based on a counter. I have been looking around but havent been able to find anything yet. I am hoping that there is something along the lines of:
numberofvariables = len(someinput)
constraints = []
for g in range(0,numberofvariables):
def constraintg (x):
return curvestable.iloc[g,1]*(x[0]**2))+(curvestable.iloc[g,2]*x[0]) + curvestable.iloc[g,4]
constraints = constraints.append([{'type':'ineq', 'fun':constraintg}])
next
I also need to point out I am an extreme amateur in coding so not sure if this is possible or not.