I have a nonlinear optimization problem which makes use of 3 decision variables, one of these variables is a single number (t), one is a vector with index i (S_i) and one is a matrix (Q_i,j) with indices i and j. I'm currently trying to use scipy.optimize.minimize
to model and solve my problem but I can't get it to work.
Because of the multiple indices, constraints must often hold for all values of some index. My actual model is very large so let's consider this example which we assume is nonlinear:
Decision variable: Q_i,j
Objective:
minimize Sum over all i and all j, Q_i,j
Constraint_1:
Q_i,j / 2 >= 10 for all i, for all j
Current code I try to use:
from scipy.optimize import minimize
import numpy as np
I = 5
J = 5
x0 = np.zeros(I*J)
def obj(Q_ij):
Q_ijcp = np.reshape(Q_ijcp,(I,J))
return sum(Q_ij[i,j] for i in range(I) for j in range(J))
def cons_1(Q_ij):
Q_ijcp = np.reshape(Q_ijcp,(I,J))
return (Q_ij[i,j] / 2 - 10 for i in range(I) for j in range(J))
b = (0, 100)
bounds = []
for i in range(I):
for j in range(J):
bounds.append(b)
constraint1 = {"type": "ineq", "fun": cons_1}
constraints = [constraint1]
solution = minimize(obj, x0=x0, bounds=bounds, constraints=constraints, method='SLSQP')
Based on the user guide I found that for each constraint one must make a definition such that it can be entered into the solver which I try above but it does not work, how can I model this such that I don't have to make a definition for each possible i and j value in Constraint_1? (Such that I don't end up with 5 * 5 constraints as I=J=5)
Or are there any other packages with good documentation/examples which are easier to use in the case of using vectors and matrices with constraints for their indices?