On Mar 24, 1:55 pm, josea.malp...@gmail.com wrote:
> I am using the genetic algorithms code by R. Dimeo (www.ncnr.nist.gov/
> staff/dimeo/idl_programs.html)
> for the optimization of image classification. I have tried to pass
> data (image matrices) to the function to by optimaze (test_func_1)
> from the main program.
Wow...there's a second user of this implementation of the GA! Let's
see if I can help with this.
You *should* be passing these data in via the functargs keyword as you
suspected. The way you have the code written in your posting though
has you passing the pointers in as parameters. The code example below
illustrates how you can pass in one array into a simple minimization
problem in 1-d. Specifically a pointer to the array is passed into
the routine. Compile the code below and type test_ga_min_fun.
Admittedly the syntax is probably not obvious. Sorry for posting all
of this code....but I hope it helps.
Rob Dimeo
; *********************************** ;
pro test_ga_iterproc,func, $
p, $
iter, $
interrupt, $
functargs = functargs, $
oref = oga, $
_Extra = extra
compile_opt hidden,idl2
oga->get_property,ave_fitness = ave_fitness
x = 1+indgen(iter+1)
y = ave_fitness[0:iter]
tvlct,r,g,b,/get
rnew = reverse(r) & gnew = reverse(g) & bnew = reverse(b)
tvlct,rnew,gnew,bnew
wset,extra.winpix
plot,[x],[y],psym = -4,title = 'Function evaluation',xtitle =
'Generation', $
ytitle = '<F(p)>'
wset,extra.winvis
device,copy = [0,0,!d.x_size,!d.y_size,0,0,extra.winpix]
tvlct,r,g,b
end
; ******************************************* ;
function ga_f1,p,_EXTRA = extra
x = p[0]
; Extract the array from the pointer passed in as
; a keyword.
a = *extra.aptr
z = 3.*total(a)*(1.-x)^2
return,z
end
; ******************************************* ;
function my_obj_fun,z,_Extra = extra
return,(extra.iter+1.) * (max(z)-z)
end
; ******************************************* ;
pro test_ga_min_fun
; Uses the simple genetic algorithm to minimize a function
; of a single variable.
; Create an array that we'll pass into the function to be minimized.
; Specifically create a pointer.
a = findgen(5)
aptr = ptr_new(a)
prange = [-9.0,9.0]
ofun = 'my_obj_fun'
func = 'ga_f1'
quiet = 0B
if ~quiet then begin
xsize = 400 & ysize = 400
window,0,xsize = xsize,ysize = ysize
winvis = 0
window,/free,/pixmap,xsize = xsize,ysize = ysize
winpix = !d.window
iterargs = {winvis:winvis,winpix:winpix}
iterproc = 'test_ga_iterproc'
endif
ftol = 1.e-2
ft = {aptr:aptr} ; These are the arguments that will be passed into
the function
; we are minimizing.
p = rmd_ga( ftol, $
function_value = function_value, $
function_name = func, $
functargs = ft, $
prange = prange, $
ncalls = ncalls, $
quiet = quiet, $
objective_function = ofun, $
pcross = 0.95, $
gene_length = 25, $
pmutate = 0.01, $
stretch_factor = 1., $
itmax = 100, $
iterproc = iterproc, $
iterargs = iterargs, $
npop = 100)
if ~quiet then wdelete,winpix
this_format = '(f15.3)'
print,'Best parameters: '+strtrim(string(p[0],format = this_format),2)
print,'Function value: '+strtrim(string(function_value,format =
this_format),2)
ptr_free,aptr
end
|