0

I'm trying to solve an optimization problem using scipy. There's a huge database that I re-arranged in order to use some of the rows as parameters in the optimization problem. Then I take the sum of each entry and multiply by x variables in order to create constraints that must be greater than "arb" (which is just a new single variable). When I try to solve it I get TypeError: constraint1() missing 2 required positional arguments: 'x' and 'row1'.

path = r'mypath/*.csv'

#Extract data
all_rec = iglob(path, recursive=True)
df = (pd.read_csv(f, usecols=[4]) for f in all_rec)
list = pd.concat(df, axis=1, ignore_index=True)

#Extract arrays
t=0
row1 = list.iloc[t+5] - list.iloc[t]
row2 = list.iloc[t+10] - list.iloc[t]
row3 = list.iloc[t+15] - list.iloc[t]
row4 = list.iloc[t+20] - list.iloc[t]

#LP Problem
def objective(arb, sign=-1.0):
    return sign*arb

def constraint1(arb,x,row1):
    for i in range(len(list.columns)):
        sum_cons1 = sum(row1[i]*x[i]) - arb
    return sum_cons1

def constraint2(arb,x,row2):
    for i in range(len(list.columns)):
        sum_cons2 = sum(row2[i]*x[i]) - arb
    return sum_cons2

def constraint3(arb,x,row3):
    for i in range(len(list.columns)):
        sum_cons3 = sum(row3[i]*x[i]) - arb
    return sum_cons3

def constraint4(arb,x,row4):
    for i in range(len(list.columns)):
        sum_cons4 = sum(row4[i]*x[i]) - arb
    return sum_cons4

con1 = {'type': 'ineq', 'fun': constraint1}
con2 = {'type': 'ineq', 'fun': constraint2}
con3 = {'type': 'ineq', 'fun': constraint3}
con4 = {'type': 'ineq', 'fun': constraint4}

cons = ([con1,con2,con3,con4])
solution = minimize(objective,x0=2500.0,method='SLSQP',constraints=cons)
print(solution)
ldjr28
  • 1
  • 1
  • In the objective and constrain functions, `arb` is the optimization variable, which has the initial value of `x0`. But where is `x` and `row#` supposed to come from? The globals `row#` won't be passed as arguments. What is `x`? `minimize` takes an `args` tuple, that can be used to passed extra arguments to these functions. – hpaulj Nov 13 '19 at 02:09

1 Answers1

0

It seems that the constraint function must accept a single argument x and have an output which is the distance from the constraint. For an inequality constraint the minimizer will attempt to keep the returned values in the non-negative range and for an equality it will try to bring it to 0. Your functions however require 3 arguments: x,arb and row.

It seems that you want the row1,row2,row3 and row4 to be a global variables, so there's no need for it to be an argument, the minimizer doesn't even know what these variables are. And so does the arb argument, did you mean to define it globally?

iliar
  • 932
  • 6
  • 11
  • No, I meant to define it just within the optimization problem as the objective function, which in this case is just this variable. You are right with the row argument, I don't need it as an argument, but still, x is marked as 'required positional argument'. I'm trying to build within the constraint an array of x number of variables to do the sum-product with the parameters in my database. – ldjr28 Nov 13 '19 at 05:56
  • 1
    In any case, the minimizers expects a function of a single argument. You can't just expect it to guess what x is or what arb is and pass it to your function. I'm not sure if I understand but perhaps instead of - arb just write - x ? – iliar Nov 13 '19 at 06:24