Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy minimize: How to pass args to both the objective and the constraint

My MWE is as follows

def obj(e, p):
    S = f(e) + g(p)
    return S

I would like to minimize this function over only e and pass p as an argument to the function. However, I also would like a constraint that depends on p and e that is of the form p + e < 1

I tried

cons = {'type': 'ineq',
       'fun': lambda e, p: -e -p + 1,
       'args': (p)}

And then, I try to minimize this for the case of p = 0.5

minimize(obj, initial_guess, method = 'SLSQP', args = 0.5, constraints = cons)

but this doesn't work. I get the error name 'p' is not defined in the line where I define cons. How do I pass the argument p to both the objective function and the constraint?


Full code below

from scipy.optimize import minimize
from scipy.stats import entropy
import numpy as np

#Create a probability vector
def p_vector(x):
    v = np.array([x, 1-x])
    return v


#Write the objective function 
def obj(e, p):
    S = -1*entropy(p_vector(p + e), base = 2) 
    return S

##Constraints
cons = {'type': 'ineq',
       'fun': lambda e: -p - e + 1,
       'args': (p,)
       }

initial_guess = 0

result = minimize(obj, initial_guess, method = 'SLSQP', args = (0.5, ), constraints = cons)
print(result)
like image 959
user1936752 Avatar asked Feb 09 '19 23:02

user1936752


1 Answers

Okay, I figured that it's a mix of syntax errors on my part and how arguments should be passed. For those who may have the same question, I will post an answer here.

The objective function is obj(e, p). We only want to minimize e so we create a tuple of the other arguments arguments = (0.5,). That is, a specific value of p=0.5 is set. Next define the constraint function

def prob_bound(e, p):
    return -e - p + 1

Now one writes the constraints dictionary to be

cons = ({'type': 'ineq',
       'fun': prob_bound,
       'args': arguments       
       })

And finally, one calls the minimizer

result = minimize(obj, initial_guess, method = 'SLSQP', args = arguments, constraints = cons)
like image 81
user1936752 Avatar answered Oct 13 '22 01:10

user1936752