Hello,

I'm using the Python bindings of NLOPT for derivative based local optimization.
For bound-constrained problems the optimization works fine (using
nlopt.LD_MMA and nlopt.LD_LBFGS).
I now want to solve  a problem with a simple linear inequality
constraint  `0 >=  sum(x) - 10`.
Since I have also some nonlinear equality constraints the only option
seems to be nlopt.LD_AUGLAG (or nlopt.LD_AUGLAG_EQ if I introduce an
additional slack variable).

However, apparently I'm missing something.
1) The problem is differentiable but nlopt failed to find a feasible
solution after hundreds of iterations.
By infeasible I mean that sum(x) is about 100 but it should satisfy
10>= sum(x).  I'm pretty confident that the function evaluation and
the gradient/Jacobian evaluation is correct.
2) nlopt.LD_MMA also didn't find a feasible solution

Since it is impossible to post the original problem here I have
adapted the tutorial example and tried to optimize it with
nlopt.LD_AUGLAG.

Code and output below
------------------------------ start test script
-------------------------------------------


import nlopt
from numpy import *

def myfunc(x, grad):
    if grad.size > 0:
        grad[0] = 0.0
        grad[1] = 0.5 / sqrt(x[1])
    return sqrt(x[1])

def myconstraint(x, grad, a, b):
    if grad.size > 0:
        grad[0] = 3 * a * (a*x[0] + b)**2
        grad[1] = -1.0
    return (a*x[0] + b)**3 - x[1]


def test_LD_MMA():
    """ works """

    opt = nlopt.opt(nlopt.LD_MMA, 2)
    opt.set_lower_bounds([-float('inf'), 0])
    opt.set_min_objective(myfunc)
    opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,2,0), 1e-8)
    opt.add_inequality_constraint(lambda x,grad:
myconstraint(x,grad,-1,1), 1e-8)
    opt.set_xtol_rel(1e-4)
    x = opt.optimize([1.234, 5.678])
    minf = opt.last_optimum_value()
    print "optimum at ", x[0],x[1]
    print "minimum value = ", minf
    print "result code = ", opt.last_optimize_result()

def test_LD_AUGLAG():
    """ doesn't work """

    opt = nlopt.opt(nlopt.LD_AUGLAG, 2)

    opt.set_lower_bounds([-float('inf'), 0])
    opt.set_maxeval(1000)
    opt.set_min_objective(myfunc)
    opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,2,0), 1e-8)
    opt.add_inequality_constraint(lambda x,grad:
myconstraint(x,grad,-1,1), 1e-8)
    opt.set_xtol_rel(1e-4)
    x = opt.optimize([1.234, 5.678])
    minf = opt.last_optimum_value()
    print "optimum at ", x[0],x[1]
    print "minimum value = ", minf
    print "result code = ", opt.last_optimize_result()


def test_LD_AUGLAG_with_set_local_optimizer():
    """ doesn't work """

    opt = nlopt.opt(nlopt.LD_AUGLAG, 2)
    opt.set_local_optimizer(nlopt.LD_MMA)

    opt.set_lower_bounds([-float('inf'), 0])
    opt.set_min_objective(myfunc)
    opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,2,0), 1e-8)
    opt.add_inequality_constraint(lambda x,grad:
myconstraint(x,grad,-1,1), 1e-8)
    opt.set_xtol_rel(1e-4)
    x = opt.optimize([1.234, 5.678])
    minf = opt.last_optimum_value()
    print "optimum at ", x[0],x[1]
    print "minimum value = ", minf
    print "result code = ", opt.last_optimize_result()

------------------------------ end test script
-------------------------------------------


output of  test_LD_MMA():
------------------------------------

optimum at  0.333333331366 0.296296292697
minimum value =  0.544331050646
result code =  4

output of test_LD_AUGLAG():
------------------------------------------

optimum at  0.440450564807 0.0
minimum value =  0.0
result code =  5

output of test_LD_AUGLAG_with_set_local_optimizer():
--------------------------------------------------------------------------------

Traceback (most recent call last):
  File "nlopt_test.py", line 70, in <module>
    test_LD_AUGLAG_with_set_local_optimizer()
  File "nlopt_test.py", line 54, in test_LD_AUGLAG_with_set_local_optimizer
    opt.set_local_optimizer(nlopt.LD_MMA)
  File 
"/home/b45ch1/workspace/tmp/nlopt-2.1.2/lib/python2.6/site-packages/nlopt.py",
line 258, in set_local_optimizer
    def set_local_optimizer(*args): return _nlopt.opt_set_local_optimizer(*args)
TypeError: in method 'opt_set_local_optimizer', argument 2 of type
'nlopt::opt const &'


Any input and help would be most welcome.

I'm using NLOPT version:
python -c"import nlopt; print nlopt.version_major(),
nlopt.version_minor(), nlopt.version_bugfix()"
2 2 1


best regards,
Sebastian

_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss

Reply via email to