0

Solving a non-convex optimization with inequality constraints computed from the decision variable and some additional inputs using a non-linear function. The constraints (schematically, just for example) can be put into one function that returns a vector output, e.g., using some arbitrary inequality:

extras  = 0.25
kwdsbas = dict(extras = extras)
@with_penalty(quadratic_inequality, kwds = kwdsbas)
def cc(x, extras): # <= 0 
    # some code to evaluate a number of inequalities -> vector 
    temp = np.array([np.sum(x)**2-extras/2, np.sum(x)**0.5-extras])
    return temp

Now, when I try to evaluate the penalty for some x = [1,2,3,4,5], I getting an error as follows

x = [1,2,3,4,5]
cc(x)
Traceback (most recent call last):

  File "<ipython-input-17-0b59507ea199>", line 1, in <module>
    cc(x)

  File "/Users/vilkov/anaconda3/lib/python3.7/site-packages/mystic/penalty.py", line 352, in func
    return float(2*_k)*max(0., pf)**2 + f(x, *argz, **kwdz) #XXX: use 2*k or k=200?

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Because I have several thousands of inequalities to put into penalty, I cannot put each of them into a separate function. Moreover, all inequalities are computed jointly using some efficient algorithm, and using one function makes lots of sense. When I try to use symbolic solver, it basically crashes due to too many inequalities and variables (>500 inequalities and >1000 decision variables).

Thus, the question: how to use a vector-valued function output to specify mystic penalty?

  • You shouldn't return an array from the penalty, it should be a list. Secondly, if you are looking to add a lot of symbolic equations/inequalities, you possibly can compose them as done here: https://stackoverflow.com/a/59203695/2379433 (see bottom of the answer). And if the 500 inequalities start to choke the symbolic solver, you can break them up into chunks, and then use one of mystic's `and_` methods to join the constraints/penalties. – Mike McKerns Dec 18 '19 at 18:26
  • If you post (or otherwise show me, here or on GitHub) a more detailed description of your issue, I can give you a less generalized suggestion. – Mike McKerns Dec 18 '19 at 18:28
  • For returning a list, I get the error `File "/home/user/.local/lib/python3.10/site-packages/mystic/penalty.py", line 388, in func return float(2*_k)*max(0., pf)**2 + f(x, *argz, **kwdz) #XXX: use 2*k or k=200? TypeError: '>' not supported between instances of 'list' and 'float'` – Dan Doe Jul 11 '22 at 07:35

0 Answers0