Re: Optimization "AMOEBA" [message #68019] |
Thu, 24 September 2009 06:23  |
Nicki
Messages: 14 Registered: September 2009
|
Junior Member |
|
|
On 24 Sep., 10:13, Wox <s...@nomail.com> wrote:
>>> If there is for a
>>> specific N, a_max and R_i a "NaN" it won't stop the approximation,
>>> will it?!(i hope you know what i mean))
>
>> Okay, I added the other variables now and it works... and also the 3D
>> plot works...
>> If I have 3 Variables now, what do i need to add to the ploting
>> section? And is it possible to get the results for a second minimum as
>> well (if there is another local minimum?)
>
> I'm just wondering what you want to achieve here. Why do you want to
> use AMOEBA and not POWELL for example? Do you need to do this for 1
> function only or are there more? Do you always know the box
> constraints and is it always a box?
I thought about trying powell as well...maybe i should do this...what
would be the advantages of powell?
i actually have more equations, but kind of put them into each other,
eliminated a few parameters and got one superlong equation with five
variables...
i'm having box constraints for every parameter...
|
|
|
|
Re: Optimization "AMOEBA" [message #68021 is a reply to message #68020] |
Thu, 24 September 2009 00:56   |
Wout De Nolf
Messages: 194 Registered: October 2008
|
Senior Member |
|
|
On Wed, 23 Sep 2009 19:13:13 -0700 (PDT), Nicki
<nickireiter87@yahoo.de> wrote:
> Okay, I added the other variables now and it works... and also the 3D
> plot works...
> If I have 3 Variables now, what do i need to add to the ploting
> section? And is it possible to get the results for a second minimum as
> well (if there is another local minimum?)
I just included the surface plot to see whether the maximum found by
amoeba (within the box constraints) made any sense.
If you want to check this for more than 2 parameters, you can do
something like changing one of the parameters at a time while keeping
the others fixed at their optimized position (i.e. where the function
is maximal):
; Plot
n=200
b=P0-dP
inc=(2*dP)/n
y=fltarr(n)
for j=0,n_elements(P0)-1 do begin
x=b[j]+inc[j]*indgen(n)
P=xmax
for i=0,n-1 do begin
P[j]=x[i]
y[i]=-func(P,/NOCONSTR)
endfor
window,j
plot,x,y
plots,xmax[[j,j]],[!y.crange[0],ymax],psym=-2
endfor
As for the second local minimum: I guess you need to play with your
box-constraints to get one or the other.
|
|
|
Re: Optimization "AMOEBA" [message #68022 is a reply to message #68021] |
Thu, 24 September 2009 00:37   |
Wout De Nolf
Messages: 194 Registered: October 2008
|
Senior Member |
|
|
On Wed, 23 Sep 2009 18:04:46 -0700 (PDT), Nicki
<nickireiter87@yahoo.de> wrote:
> Wuhuu, thank you. The last part does not work though (plot), i just
> commented it out and try to fix it later since the plot is right now
> not that important.
> so now, just that i understand everything...What do I need to change
> and take care of when i also have a 3rd, 4th and maybe even a 5th
> variable?? I don't have to change the function constraint, do i? (the
> next thing will be letting a_max go from 20 to 80.
Constraint will work with any number of variables. To understand what
this function does, try this:
x=indgen(100)-50
P0=50.
dP=10.
plot,x,P0+2.*dP/!pi*atan(x),/ys
The parameter x is always projected between 40 and 60.
Thinking about it, you should probably start AMOEBA with P0=[0,0.]
R=AMOEBA(1.0e-5,SCALE=dP, P0=[0,0.], FUNCTION_VALUE=fval)
> If there is for a
> specific N, a_max and R_i a "NaN" it won't stop the approximation,
> will it?!(i hope you know what i mean))
The constraint function puts box-constraints around each parameter. If
you don't have a box (i.e. the boundaries depend on the value of other
parameters) you need a more complicated constraint function.
|
|
|
Re: Optimization "AMOEBA" [message #68023 is a reply to message #68022] |
Wed, 23 September 2009 19:13   |
Nicki
Messages: 14 Registered: September 2009
|
Junior Member |
|
|
On 24 Sep., 11:04, Nicki <nickireite...@yahoo.de> wrote:
> Wuhuu, thank you. The last part does not work though (plot), i just
> commented it out and try to fix it later since the plot is right now
> not that important.
> so now, just that i understand everything...What do I need to change
> and take care of when i also have a 3rd, 4th and maybe even a 5th
> variable?? I don't have to change the function constraint, do i? (the
> next thing will be letting a_max go from 20 to 80. If there is for a
> specific N, a_max and R_i a "NaN" it won't stop the approximation,
> will it?!(i hope you know what i mean))
Okay, I added the other variables now and it works... and also the 3D
plot works...
If I have 3 Variables now, what do i need to add to the ploting
section? And is it possible to get the results for a second minimum as
well (if there is another local minimum?)
|
|
|
|
Re: Optimization "AMOEBA" [message #68029 is a reply to message #68024] |
Wed, 23 September 2009 06:41   |
Wout De Nolf
Messages: 194 Registered: October 2008
|
Senior Member |
|
|
On Wed, 23 Sep 2009 04:49:42 -0700 (PDT), Jeremy Bailin
<astroconst@gmail.com> wrote:
> I seem to remember Craig mentioning that implementing a constraint
> using a brick wall like that can give you problems.
Yes, for non-linear least squares refinement. See CONSTRAINT in code
below.
FUNCTION CONSTRAINT,Pin
; P must be with ]P0-dP,P0+dP[
COMMON FUNC_XY, P0, dP
return,P0+2*dP/!pi*atan(Pin)
END
FUNCTION FUNC, Pin, NOCONSTR=NOCONSTR
; NOCONSTR is just for plotting
if keyword_set(NOCONSTR) then P=Pin $
else P=CONSTRAINT(Pin)
z=120.0
N_rows=3.0
d_fov=67
mu=438.689
R_tot=1.5
a_max=30
N=P[1]
R_i=P[0]
;a_max=P[2]
x=N*tan(a_max/2.0*!pi/180.0)*1.1/N_rows/!pi-1.0
a=2.0*asin((1.0/x+1.0)*d_fov/2/z)*180/!pi
d=sqrt(R_tot^2-x^2*(R_i)^2)/(x+1)-alog(2)/mu*tan(a/2*!pi/180 )
S=N*(1+x)^2/16/x^2/z^2*(d^2+2/mu*d*tan(a/2*!pi/180)+2/mu^2*( tan(a/2*!pi/180))^2)*100
if ~finite(s) then stop
RETURN, -S
END
pro test34
COMMON FUNC_XY, P0, dP
P0=[0.3, 60]
dP=[0.5,15]
R=AMOEBA(1.0e-5,SCALE=dP, P0=P0, FUNCTION_VALUE=fval)
if r[0] eq -1 then begin
print,'No minimum found.'
return
endif
xmax=CONSTRAINT(r[0:1])
ymax=-fval[0]
PRINT, 'x_Value:', xmax
PRINT, 'maximum:', ymax
; Plot
n=200
L=P0-dP
H=P0+dP
inc1=(2*dP[0])/n
inc2=(2*dP[1])/n
b1=L[0]
b2=L[1]
x=b1+inc1*indgen(n)
y=b2+inc2*indgen(n)
z=fltarr(n,n)
for i=0,n-1 do $
for j=0,n-1 do $
z[i,j]=-func([x[i],y[j]],/NOCONSTR)
isurface,z,x,y
end
|
|
|
Re: Optimization "AMOEBA" [message #68031 is a reply to message #68029] |
Wed, 23 September 2009 04:49   |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Sep 23, 3:54 am, Wox <s...@nomail.com> wrote:
> On Tue, 22 Sep 2009 16:30:45 -0700 (PDT), Nicki
>
> <nickireite...@yahoo.de> wrote:
>
>>> Set a breakpoint in your FUNC and check for NaN's. For example:
>>> "a=NaN" when P=[0.5,30].
>
>> But still... If I set my scale to let's say [0.5, 0.5] for [0.55, 60.]
>> it should definitely work. however i get -0.2000 for P[0] which is not
>> even in the set range for P[0] (it goes from 0.05 to 1 (see above))
>> However I have no idea why... Somebody can help me out?
>
> FUNC will still return NaN at some point! Add the following line to
> FUNC to check it yourself:
>
> FUNCTION FUNC, P
> ...
> if ~finite(s) then stop
> RETURN, -S
> END
>
> I see what your problem is now. The P0 and SCALE define an initial
> simplex and by no means constraint the space in which the minimum
> should lay. See:http://www.nrbook.com/a/bookcpdf/c10-4.pdf
>
> Maybe you can do something like this when P gets out of its range:
>
> FUNCTION FUNC, P
> ...
> if ~finite(s) then return,!values.F_INFINITY
> RETURN, -S
> END
>
> Does that work?
I seem to remember Craig mentioning that implementing a constraint
using a brick wall like that can give you problems. I think he
advocated having a slightly smoother but still very steep transition
from the allowed values to the extremely high values in the forbidden
region. I'm sure it was in the context of his routines, but I wouldn't
be surprised if amoeba behaved similarly.
-Jeremy.
|
|
|
Re: Optimization "AMOEBA" [message #68034 is a reply to message #68031] |
Wed, 23 September 2009 00:54   |
Wout De Nolf
Messages: 194 Registered: October 2008
|
Senior Member |
|
|
On Tue, 22 Sep 2009 16:30:45 -0700 (PDT), Nicki
<nickireiter87@yahoo.de> wrote:
>
>
>> Set a breakpoint in your FUNC and check for NaN's. For example:
>> "a=NaN" when P=[0.5,30].
>
> But still... If I set my scale to let's say [0.5, 0.5] for [0.55, 60.]
> it should definitely work. however i get -0.2000 for P[0] which is not
> even in the set range for P[0] (it goes from 0.05 to 1 (see above))
> However I have no idea why... Somebody can help me out?
FUNC will still return NaN at some point! Add the following line to
FUNC to check it yourself:
FUNCTION FUNC, P
...
if ~finite(s) then stop
RETURN, -S
END
I see what your problem is now. The P0 and SCALE define an initial
simplex and by no means constraint the space in which the minimum
should lay. See: http://www.nrbook.com/a/bookcpdf/c10-4.pdf
Maybe you can do something like this when P gets out of its range:
FUNCTION FUNC, P
...
if ~finite(s) then return,!values.F_INFINITY
RETURN, -S
END
Does that work?
|
|
|
|
|
Re: Optimization "AMOEBA" [message #68093 is a reply to message #68074] |
Fri, 18 September 2009 06:05   |
Nicki
Messages: 14 Registered: September 2009
|
Junior Member |
|
|
O thanks so much for that... Just tried it also with my more
complicated function and it works... But now it's getting tricky:)
I have a 2nd varible...
this is set to 60 at the beginning and should vary plus/minus 30.
However my IDL cannot do it, it can only do scale 1, everything that
is more than one does not work out.....
That's the code:
FUNCTION FUNC, P
z=120.0
N_rows=3.0
d_fov=67
mu=438.689
R_tot=1.5
a_max=30
N=P[1]
R_i=P[0]
;a_max=P[2]
x=N*tan(a_max/2.0*!pi/180.0)*1.1/N_rows/!pi-1.0
a=2.0*asin((1.0/x+1.0)*d_fov/2/z)*180/!pi
d=sqrt(R_tot^2-x^2*(R_i)^2)/(x+1)-alog(2)/mu*tan(a/2*!pi/180 )
S=N*(1+x)^2/16/x^2/z^2*(d^2+2/mu*d*tan(a/2*!pi/180)+2/mu^2*( tan(a/2*!
pi/180))^2)*100
RETURN, -S
END
R=AMOEBA(1.e-1, SCALE=[0.50, 30.], P0=[0.55, 60.],
FUNCTION_VALUE=fval)
;Check for convergence:
;IF N_ELEMENTS(R) EQ 1 THEN MESSAGE, 'AMOEBA failed to converge'
; Print results:
PRINT, 'R_i, N:', r, $
'function value: ', fval[0]
END
|
|
|
|
Re: Optimization "AMOEBA" [message #68097 is a reply to message #68096] |
Fri, 18 September 2009 04:45   |
Wout De Nolf
Messages: 194 Registered: October 2008
|
Senior Member |
|
|
On Fri, 18 Sep 2009 02:53:29 -0700 (PDT), Nicki
<nickireiter87@yahoo.de> wrote:
> Hey!
>
> I need some help with amoeba (i'm a total IDL beginner...). For a
> start (get to understand how it works) I want to solve a pretty
> simple, 1-dim. problem. I'm having a parabola -x^2+4x+9 and I want to
> get the maximum with the help of AMOEBA. How do I do that? (Let's say
> starting point P0=5 and scale is 4).
> This is what it looks like now:
>
>
>
> FUNCTION FUNC, P
> x=P[0]
> y=-x^2.+4.*x+5.
> ydes=9.1
> RETURN, MIN(ydes^2-y^2)
> END
>
> R=AMOEBA(1.0e-1, SCALE=5, P0=0.05, FUNCTION_VALUE=fval)
>
> PRINT, 'x_Value:', r, $
> 'error:', fval[0]
>
> END
>
> As a result i get for the x_value -1 and for the error - Inf.... But
> why?! I mean the maximum of the function is at x=2 and this is within
> the scale...
> Maybe somebody can give me some help
It should be more like the code below. I never used this, but AMOEBA
seems to search for the minimum of a scalar field f(x,y,z,...).
In your example:
f(x) = -x^2.+4.*x+5.
If "FUNC" returns -f(x) it will search for the minimum of -f(x) which
is the same as finding the maximum of f(x).
FUNCTION FUNC, P
x=P[0]
y=-x^2.+4.*x+5.
RETURN, -y[0]
END
pro test
R=AMOEBA(1.0e-1,SCALE=10,P0=[0.01], FUNCTION_VALUE=fval)
xmax=r[0]
ymax=-fval[0]
PRINT, 'x_Value:', xmax
PRINT, 'maximum:', ymax
window
x=0.1*findgen(80)-2
y=-x^2.+4.*x+5.
plot,x,y
plots,xmax[[0,0]],[!y.crange[0],ymax],psym=-2
plots,[!x.crange[0],xmax],ymax[[0,0]],psym=-2
end
|
|
|
|
Re: Optimization "AMOEBA" [message #68253 is a reply to message #68218] |
Wed, 07 October 2009 16:15  |
Nicki
Messages: 14 Registered: September 2009
|
Junior Member |
|
|
Alright, so far everything is working--thanks!
BUT another thing... what if I want one variable to vary and it needs
to be an integer, so let's say it should be either 3, 4 or 5 ? What do
i have to write for dP ?
|
|
|