Hello,
I'm using the nlopt library for shape optimizations and I encountered one thing, I don't understand when using the MMA algorithm. I'm using python and I set the verbose option, so that I can see, wether the optimization is actually in a outer or inner iteration. When I run my script the "last-optimum-value" is sometimes changed even during inner iterations. To make my problem replicable, I changed the python example (from the instruction homepage) a little bit. The second iteration seems to be an inner iteration (according to the verbose output), but the last-optimum-value is changed though. For the later inner optimizations the "last-optimum-value" stays constant during the inner iterations and is only changed, if the routine goes to the next outer iteration. Can someone please explain to be, what's the difference between the first and the later inner iterations? In my "real" script, I get those differences more often, so I would be really interested, how to handle this. Or is there any other way to only get only the results of the outer iterations?
My script is:
import nlopt
from numpy import *
from numpy import *
last_value = 0
def myfunc(x, grad,opt):
global last_value
print "last_optimum: ",opt.last_optimum_value()
print "last_value: ", last_value
if grad.size > 0:
grad[0] = 0.5 / sqrt(x[1]+x[0])
grad[1] = 1. / sqrt(x[1]+x[0])
global last_value
print "last_optimum: ",opt.last_optimum_value()
print "last_value: ", last_value
if grad.size > 0:
grad[0] = 0.5 / sqrt(x[1]+x[0])
grad[1] = 1. / sqrt(x[1]+x[0])
value = sqrt(2*x[1]+x[0])
last_value = value
return value
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]
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]
opt = nlopt.opt(nlopt.LD_MMA, 2)
opt.set_lower_bounds([-float('inf'), 0])
opt.set_min_objective(lambda x,grad: myfunc(x,grad,opt))
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()
opt.set_lower_bounds([-float('inf'), 0])
opt.set_min_objective(lambda x,grad: myfunc(x,grad,opt))
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()
And the output is (the "strange" block is marked with >>>):
last_optimum: inf
value: 0
MMA dual converged in 121 iterations to g=3.53025:
MMA y[0]=0.0196046, gc[0]=-1.21559e-07
last_optimum: 3.54823899984
value: 3.54823899984
MMA outer iteration: rho -> 0.1
MMA rhoc[0] -> 0.1
MMA dual converged in 5 iterations to g=3.26691:
MMA y[0]=0, gc[0]=-4.63943
last_optimum: 3.43722787098
value: 3.43722787098
>> MMA inner iteration: rho -> 0.1
>> MMA rhoc[0] -> 0.571234
>> MMA dual converged in 3 iterations to g=2.98558:
>> MMA y[0]=0, gc[0]=-3.64974
>> last_optimum: 3.17785132948
>> value: 3.17785132948
MMA outer iteration: rho -> 0.01
MMA rhoc[0] -> 0.0571234
MMA sigma[0] -> 1.2
MMA dual converged in 21 iterations to g=2.62559:
MMA y[0]=0, gc[0]=-2.59586
last_optimum: 2.88772110789
value: 2.88772110789
MMA outer iteration: rho -> 0.001
MMA rhoc[0] -> 0.00571234
MMA sigma[0] -> 1.44
MMA dual converged in 24 iterations to g=2.34282:
MMA y[0]=0, gc[0]=-2.21818
last_optimum: 2.58964491131
value: 2.58964491131
MMA outer iteration: rho -> 0.0001
MMA rhoc[0] -> 0.000571234
MMA sigma[0] -> 1.008
MMA dual converged in 18 iterations to g=2.03449:
MMA y[0]=0, gc[0]=-1.46884
last_optimum: 2.31172012122
value: 2.31172012122
MMA outer iteration: rho -> 1e-05
MMA rhoc[0] -> 5.71234e-05
MMA sigma[0] -> 1.2096
MMA dual converged in 19 iterations to g=1.68448:
MMA y[0]=0, gc[0]=-0.840215
last_optimum: 2.00356651624
value: 2.00356651624
MMA outer iteration: rho -> 1e-05
MMA rhoc[0] -> 1e-05
MMA sigma[0] -> 1.45152
MMA dual converged in 17 iterations to g=1.29454:
MMA y[0]=0, gc[0]=-0.387253
last_optimum: 1.64538418438
value: 1.64538418438
MMA outer iteration: rho -> 1e-05
MMA rhoc[0] -> 1e-05
MMA sigma[0] -> 1.74182
MMA dual converged in 19 iterations to g=0.97831:
MMA y[0]=0.137234, gc[0]=-1.14166e-08
last_optimum: 1.24450619258
value: 1.24450619258
MMA inner iteration: rho -> 0.0001
MMA rhoc[0] -> 0.0001
MMA dual converged in 9 iterations to g=0.978311:
MMA y[0]=0.137231, gc[0]=5.29731e-09
last_optimum: 1.24450619258
value: 0.983729907521
MMA inner iteration: rho -> 0.001
MMA rhoc[0] -> 0.001
MMA dual converged in 11 iterations to g=0.97832:
MMA y[0]=0.137207, gc[0]=1.66104e-09
last_optimum: 1.24450619258
value: 0.983730068514
MMA inner iteration: rho -> 0.01
MMA rhoc[0] -> 0.01
MMA dual converged in 15 iterations to g=0.978407:
MMA y[0]=0.136963, gc[0]=1.1548e-09
last_optimum: 1.24450619258
value: 0.983731841618
MMA inner iteration: rho -> 0.1
MMA rhoc[0] -> 0.1
MMA dual converged in 19 iterations to g=0.979277:
MMA y[0]=0.134537, gc[0]=-1.6376e-08
last_optimum: 1.24450619258
value: 0.983749563924
MMA inner iteration: rho -> 0.711397
MMA rhoc[0] -> 1
MMA dual converged in 24 iterations to g=0.985372:
MMA y[0]=0.118547, gc[0]=-3.37097e-08
last_optimum: 1.24450619258
value: 0.983926142494
MMA inner iteration: rho -> 0.819799
MMA rhoc[0] -> 2.22422
MMA dual converged in 15 iterations to g=0.987433:
MMA y[0]=0.116067, gc[0]=-1.0253e-09
last_optimum: 1.24450619258
value: 0.985653432498
MMA inner iteration: rho -> 0.965095
MMA rhoc[0] -> 2.22422
MMA dual converged in 28 iterations to g=0.957398:
MMA y[0]=0.236908, gc[0]=7.42123e-09
last_optimum: 0.987897204822
value: 0.987897204822
MMA inner iteration: rho -> 9.65095
MMA rhoc[0] -> 3.89261
MMA dual converged in 19 iterations to g=0.957701:
MMA y[0]=0.234694, gc[0]=-1.92852e-08
last_optimum: 0.987897204822
value: 0.962328325705
MMA inner iteration: rho -> 96.5095
MMA rhoc[0] -> 3.89261
MMA dual converged in 27 iterations to g=0.962232:
MMA y[0]=0.262382, gc[0]=4.16301e-10
last_optimum: 0.962339659936
value: 0.962339659936
MMA inner iteration: rho -> 965.095
MMA rhoc[0] -> 5.07658
MMA dual converged in 20 iterations to g=0.96225:
MMA y[0]=0.262553, gc[0]=-8.49272e-10
last_optimum: 0.962250443517
value: 0.962250443517
MMA inner iteration: rho -> 965.095
MMA rhoc[0] -> 5.07658
MMA dual converged in 3 iterations to g=0.96225:
MMA y[0]=0.262553, gc[0]=-8.49272e-10
last_optimum: 0.962250443517
value: 0.962250448258
MMA outer iteration: rho -> 96.5095
MMA rhoc[0] -> 0.507658
MMA sigma[0] -> 2.09019
MMA dual converged in 3 iterations to g=0.962251:
MMA y[0]=0.262553, gc[0]=-1.19374e-08
last_optimum: 0.962250443517
value: 0.962250448258
MMA inner iteration: rho -> 96.5095
MMA rhoc[0] -> 4.05603
MMA dual converged in 3 iterations to g=0.96225:
MMA y[0]=0.262553, gc[0]=-8.73215e-09
last_optimum: 0.962250443517
value: 0.962250492504
MMA inner iteration: rho -> 96.5095
MMA rhoc[0] -> 5.328
MMA dual converged in 3 iterations to g=0.96225:
MMA y[0]=0.262553, gc[0]=-6.56179e-09
last_optimum: 0.962250443517
value: 0.96225048428
optimum at 0.333333331997 0.296296292026
minimum value = 0.962250443517
result code = 4
value: 0
MMA dual converged in 121 iterations to g=3.53025:
MMA y[0]=0.0196046, gc[0]=-1.21559e-07
last_optimum: 3.54823899984
value: 3.54823899984
MMA outer iteration: rho -> 0.1
MMA rhoc[0] -> 0.1
MMA dual converged in 5 iterations to g=3.26691:
MMA y[0]=0, gc[0]=-4.63943
last_optimum: 3.43722787098
value: 3.43722787098
>> MMA inner iteration: rho -> 0.1
>> MMA rhoc[0] -> 0.571234
>> MMA dual converged in 3 iterations to g=2.98558:
>> MMA y[0]=0, gc[0]=-3.64974
>> last_optimum: 3.17785132948
>> value: 3.17785132948
MMA outer iteration: rho -> 0.01
MMA rhoc[0] -> 0.0571234
MMA sigma[0] -> 1.2
MMA dual converged in 21 iterations to g=2.62559:
MMA y[0]=0, gc[0]=-2.59586
last_optimum: 2.88772110789
value: 2.88772110789
MMA outer iteration: rho -> 0.001
MMA rhoc[0] -> 0.00571234
MMA sigma[0] -> 1.44
MMA dual converged in 24 iterations to g=2.34282:
MMA y[0]=0, gc[0]=-2.21818
last_optimum: 2.58964491131
value: 2.58964491131
MMA outer iteration: rho -> 0.0001
MMA rhoc[0] -> 0.000571234
MMA sigma[0] -> 1.008
MMA dual converged in 18 iterations to g=2.03449:
MMA y[0]=0, gc[0]=-1.46884
last_optimum: 2.31172012122
value: 2.31172012122
MMA outer iteration: rho -> 1e-05
MMA rhoc[0] -> 5.71234e-05
MMA sigma[0] -> 1.2096
MMA dual converged in 19 iterations to g=1.68448:
MMA y[0]=0, gc[0]=-0.840215
last_optimum: 2.00356651624
value: 2.00356651624
MMA outer iteration: rho -> 1e-05
MMA rhoc[0] -> 1e-05
MMA sigma[0] -> 1.45152
MMA dual converged in 17 iterations to g=1.29454:
MMA y[0]=0, gc[0]=-0.387253
last_optimum: 1.64538418438
value: 1.64538418438
MMA outer iteration: rho -> 1e-05
MMA rhoc[0] -> 1e-05
MMA sigma[0] -> 1.74182
MMA dual converged in 19 iterations to g=0.97831:
MMA y[0]=0.137234, gc[0]=-1.14166e-08
last_optimum: 1.24450619258
value: 1.24450619258
MMA inner iteration: rho -> 0.0001
MMA rhoc[0] -> 0.0001
MMA dual converged in 9 iterations to g=0.978311:
MMA y[0]=0.137231, gc[0]=5.29731e-09
last_optimum: 1.24450619258
value: 0.983729907521
MMA inner iteration: rho -> 0.001
MMA rhoc[0] -> 0.001
MMA dual converged in 11 iterations to g=0.97832:
MMA y[0]=0.137207, gc[0]=1.66104e-09
last_optimum: 1.24450619258
value: 0.983730068514
MMA inner iteration: rho -> 0.01
MMA rhoc[0] -> 0.01
MMA dual converged in 15 iterations to g=0.978407:
MMA y[0]=0.136963, gc[0]=1.1548e-09
last_optimum: 1.24450619258
value: 0.983731841618
MMA inner iteration: rho -> 0.1
MMA rhoc[0] -> 0.1
MMA dual converged in 19 iterations to g=0.979277:
MMA y[0]=0.134537, gc[0]=-1.6376e-08
last_optimum: 1.24450619258
value: 0.983749563924
MMA inner iteration: rho -> 0.711397
MMA rhoc[0] -> 1
MMA dual converged in 24 iterations to g=0.985372:
MMA y[0]=0.118547, gc[0]=-3.37097e-08
last_optimum: 1.24450619258
value: 0.983926142494
MMA inner iteration: rho -> 0.819799
MMA rhoc[0] -> 2.22422
MMA dual converged in 15 iterations to g=0.987433:
MMA y[0]=0.116067, gc[0]=-1.0253e-09
last_optimum: 1.24450619258
value: 0.985653432498
MMA inner iteration: rho -> 0.965095
MMA rhoc[0] -> 2.22422
MMA dual converged in 28 iterations to g=0.957398:
MMA y[0]=0.236908, gc[0]=7.42123e-09
last_optimum: 0.987897204822
value: 0.987897204822
MMA inner iteration: rho -> 9.65095
MMA rhoc[0] -> 3.89261
MMA dual converged in 19 iterations to g=0.957701:
MMA y[0]=0.234694, gc[0]=-1.92852e-08
last_optimum: 0.987897204822
value: 0.962328325705
MMA inner iteration: rho -> 96.5095
MMA rhoc[0] -> 3.89261
MMA dual converged in 27 iterations to g=0.962232:
MMA y[0]=0.262382, gc[0]=4.16301e-10
last_optimum: 0.962339659936
value: 0.962339659936
MMA inner iteration: rho -> 965.095
MMA rhoc[0] -> 5.07658
MMA dual converged in 20 iterations to g=0.96225:
MMA y[0]=0.262553, gc[0]=-8.49272e-10
last_optimum: 0.962250443517
value: 0.962250443517
MMA inner iteration: rho -> 965.095
MMA rhoc[0] -> 5.07658
MMA dual converged in 3 iterations to g=0.96225:
MMA y[0]=0.262553, gc[0]=-8.49272e-10
last_optimum: 0.962250443517
value: 0.962250448258
MMA outer iteration: rho -> 96.5095
MMA rhoc[0] -> 0.507658
MMA sigma[0] -> 2.09019
MMA dual converged in 3 iterations to g=0.962251:
MMA y[0]=0.262553, gc[0]=-1.19374e-08
last_optimum: 0.962250443517
value: 0.962250448258
MMA inner iteration: rho -> 96.5095
MMA rhoc[0] -> 4.05603
MMA dual converged in 3 iterations to g=0.96225:
MMA y[0]=0.262553, gc[0]=-8.73215e-09
last_optimum: 0.962250443517
value: 0.962250492504
MMA inner iteration: rho -> 96.5095
MMA rhoc[0] -> 5.328
MMA dual converged in 3 iterations to g=0.96225:
MMA y[0]=0.262553, gc[0]=-6.56179e-09
last_optimum: 0.962250443517
value: 0.96225048428
optimum at 0.333333331997 0.296296292026
minimum value = 0.962250443517
result code = 4
Best regards!
Frederik
_______________________________________________ NLopt-discuss mailing list [email protected] http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss
