简体   繁体   中英

solving a minimization problem using scipy.optimize in python

I have a question regarding solving a minimization problem using scipy.optimize in python. I have an 1-D array ( x ) containing about 2000 elements as the variables of this problem, and a list of {constraint,dict} elements as the constraints of the optimization problem.
The issue is that I want to calculate the sum of a large number of variables within a for loop to define objective function and the constraints. I have attached a simple example of my code below, but it should be considered that according to the complication of my code, it is not possible to do the calculation without for loops. However, by doing this, I face with this error: 'function' object is not iterable .

def objective(x):
    sum = 0
    for i in range(1, 1000):
        sum += x[i]
    return sum

def constraints(x):
    constraint = []
    for i in range(1, 1000):
        sum = 0
        for j in range(1, 100):
            sum += j*x[i]
        constraint.append({'type': 'ineq', 'fun': 10 - sum})
    return constraint
sol = minimize(objective, x0, method='slsqp', constraints=constraints)
 

looked at the video you referenced, think you may have misunderstood a couple of things. I'll start with just a basic example to get you started.

If we take the below code:

from scipy.optimize import minimize

def objective(x):
    x1=x[0]
    x2=x[1]
    x3=x[2]
    x4=x[3]
    return x1+x2+x3+x4

x0=(1,1,1,1)
solution=minimize(objective,x0)    

Simple setup. You see the python function objective is returning a math function . That math function is what you are minimizing. Note how all your variables are defined by calling the value of a list of values (x0 in this case). Thus x1 is x0[0], x2 is x0[1], etc.

In your above example, you don't define x0, and you don't have an equation. Finally, sum is a python function (it takes the sum of a list). Instead, to do what you wish to do, lets rewrite the above using a for loop now:

from scipy.optimize import minimize
import numpy as np

def objective(x):
    equation = 0
    for i in range(4):
        equation += x[i]
    return equation

x0=np.ones(4)
solution=minimize(objective,x0)

This will give you the exact same output as above; however now you can see I've changed sum to equation (now you won't get issues with built in python functions), and you've now defined your variables). I'm not going to go through all your code, but I hope this example clears enough up that it gets you going.

If you want 1000s values of x, just give x0 an array of 1000 values.

Edit:

To resolve the issue in the 2nd part of your code:

Presuming you fixed using sum. Again, constraints is a dict where you input a function (mathematical). I don't get the error that you are getting, so I don't know where that is coming from. But you can create a constraint function using your above example.

def constraints(x):
    constraint = []
    for i in range(4):
        value = 0
        for j in range(4):
            value += 10-j*x[i]
        constraint.append(value)
    return constraint


x0=np.ones(4)
con={'type': 'ineq', 'fun': constraints}
sol = minimize(objective, x0, method='slsqp', constraints=con)

Edit 2: You can use the exact same ideology above to create a 2nd constraint:

from scipy.optimize import minimize
import numpy as np

def objective(x):
    equation = 0
    for i in range(4):
        equation += x[i]
    return equation

def constraints(x):
    constraint = []
    for i in range(4):
        value = 0
        for j in range(4):
            value += 10-j*x[i]
        constraint.append(value)
    return constraint

def constraints2(x):
    constraint2 = []
    for a in range(4):
        value = 0
        for b in range(4):
            value += 5-a*x[b]
        constraint2.append(value)
    return constraint2

x0=np.ones(4)
con={'type': 'ineq', 'fun': constraints}
con2={'type': 'eq', 'fun': constraints2}
combined=[con,con2]
sol = minimize(objective, x0, method='slsqp', constraints=combined)

This works without any problems or errors, so again, I don't see the problem you are having.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM