⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree_min_ui.pro

📁 该程序是基于IDL平台下开法的应用随机搜索树进行搜索的优化算法实例!
💻 PRO
📖 第 1 页 / 共 2 页
字号:

; Initialize algorithm control parameters
iter = 0L
obest = obj_new()
best_eval = 1e12
ocurrent = ostart
best_iter = 0L
num_nodes = 0L
; Start the main iteration loop
while iter le itmax do begin
   if vis_plot then wait,delay_time
   ; Is the current node a terminal node?
   if ocurrent->terminated() then begin
      ocurrent->get_property,parms = parms
      ; Create new nodes.  Note that the node values
      ; are actually calculated when the new node objects
      ; are instantiated using the add_nodes method.
      next_index = num_nodes+1

      ret = ocurrent->add_nodes(next_index)
      num_nodes = num_nodes+ocurrent->count()
      ; For each new node, get the best node value.
      nchildren = ocurrent->count()
      ochild = ocurrent->get(/all)
      level_values = fltarr(nchildren)
      for i = 0,nchildren-1 do begin
         ochild[i]->get_property,node_value = node_value,level = level,parms = parms,id = id
         ocurrent->get_property,parms = parent_parms
         level_values[i] = node_value
         ret = ochild[i]->go_to_the_top(/decrease)
      endfor
      min_level_value = min(level_values,imin)
      ; How does this compare with the current best?
      if min_level_value lt best_eval then begin
         ; Replace the current best node with this one
         obest = ((*pstate).obest = ochild[imin])
         best_eval = min_level_value
         best_iter = iter
         ; Traverse back up the tree and increment each branch as we go.
         ret = obest->go_to_the_top(/increase)
         if vis_plot then begin
            obest->get_property,parms = best_parms
            ; Now go to the top from this current best node and
            ; retrieve the branch probabilities
            current_node = obest
            current_node->get_property,parent_node = parent

            while obj_valid(parent) do begin
               ; Draw the line with the appropriate thickness to the parent
               ; node.
               parent->get_property,         parms = parent_parms
               current_node->get_property,   parms = this_parms,           $
                                             branch_value = branch_value,  $
                                             level = level

               current_node = parent
               current_node->get_property, parent_node = parent
            endwhile

         endif
      endif
      tree_min_plot_refresh,event,iter = iter
      iter++
      ocurrent = ostart

   endif else begin

      ; Calculate the relative probabilities of all branches to the
      ; available nodes.
      nchildren = ocurrent->count()
      ochild = ocurrent->get(/all)
      branch_prob = fltarr(nchildren)
      for i = 0,nchildren-1 do begin
         ochild[i]->get_property,node_value = node_value, $
                                 branch_value = branch_value
         branch_prob[i] = branch_value
      endfor
      branch_prob = branch_prob/total(branch_prob)
      regions = [0.0,total(branch_prob,/cum)]
      select_index = value_locate(regions,randomu(s,1))
      ocurrent = ochild[select_index]
      ret = ocurrent->add_visit()

   endelse

endwhile

; If a good solution has been found then plot it on the
; function that was to be minimized.
if obj_valid(obest) then begin
   obest->get_property,parms = parms,node_value = node_value,func = func
   wset,(*pstate).winpix
   plots,[parms[0]],[call_function(func,parms[0], $
      _Extra = {equation:equation})],psym = 8,symsize = 2.0,thick = 2.0, $
      color = (*pstate).red
   wset,(*pstate).winvis
   device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pstate).winpix]
endif
label_id = widget_info(event.top,find_by_uname = 'TREE_LABEL')
num_nodes = 3+2*itmax
this_string = strtrim(string(num_nodes),2)+' nodes'
widget_control,label_id,set_value = this_string
tree_min_plot_refresh,event
end
; *********************************** ;
pro tree_min_zoom,event
compile_opt hidden,idl2
widget_control,event.top,get_uvalue = pstate
case event.type of
0: begin    ; button press
      (*pstate).mouse = event.press
      if (*pstate).mouse eq 4 then (*pstate).autoscale = 1B
      if (*pstate).mouse eq 1 then begin
         (*pstate).xbox[0] = event.x
         (*pstate).ybox[0] = event.y
         (*pstate).autoscale = 0B
      endif
   end
1: begin ; button release
    if (*pstate).mouse eq 1 then begin
      xll = (*pstate).xbox[0] < (*pstate).xbox[1]
      yll = (*pstate).ybox[0] < (*pstate).ybox[1]
      w = abs((*pstate).xbox[1] - (*pstate).xbox[0])
      h = abs((*pstate).ybox[1] - (*pstate).ybox[0])
      xur = xll + w
      yur = yll + h
      ll = convert_coord(xll,yll,/device,/to_data)
      ur = convert_coord(xur,yur,/device,/to_data)
      (*pstate).xrange = [ll[0],ur[0]]
      (*pstate).yrange = [ll[1],ur[1]]
      tree_min_plot_refresh,event
      (*pstate).mouse = 0B
    endif
    if (*pstate).mouse eq 4 then begin
      (*pstate).autoscale = 1B
      wset,(*pstate).winvis
      device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pstate).winpix]
      tree_min_plot_refresh,event
      (*pstate).mouse = 0B
    endif
   end
2: begin ; mouse motion
      if (*pstate).mouse eq 1 then begin
         (*pstate).xbox[1] = event.x
         (*pstate).ybox[1] = event.y
         xc = [(*pstate).xbox[0],event.x,event.x,$
              (*pstate).xbox[0],$
              (*pstate).xbox[0]]
         yc = [(*pstate).ybox[0],(*pstate).ybox[0],$
              event.y,event.y,$
              (*pstate).ybox[0]]
         wset,(*pstate).winvis
         device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pstate).winpix]
         plots,xc,yc,/device,thick = 2.0,color = (*pstate).green, $
            linestyle = 2
         empty
      endif
   end
else:
endcase
end
; *********************************** ;
pro tree_min_ui_event,event
compile_opt hidden,idl2
widget_control,event.top,get_uvalue = pstate
uname = widget_info(event.id,/uname)
case uname of
'QUIT':  widget_control,event.top,/destroy
'MINIMIZE': tree_min_calc,event
'PRANGE':   $
   begin
      tree_min_gen_fun,event
      tree_min_plot_refresh,event
   end
'EQ_CHOICE':   $
   begin
      tree_min_gen_fun,event
      tree_min_plot_refresh,event
   end
'FUNC_MIN': $
   begin
      tree_min_gen_fun,event
      tree_min_plot_refresh,event
   end
'ZOOM':     tree_min_zoom,event
else:
endcase
end
; *********************************** ;
function tree_min_ui_color,rgb
compile_opt hidden,idl2
color = rgb[0] + (rgb[1] * 2L^8) + (rgb[2] * 2L^16)
return,color
end
; *********************************** ;
pro tree_min_ui
compile_opt hidden,idl2
; Widget definition module
; Initialize the colors
loadct,0,/silent
device,decomposed = 1
green = tree_min_ui_color([0,255,0])
red = tree_min_ui_color([255,0,0])
blue = tree_min_ui_color([0,0,255])
yellow = tree_min_ui_color([255,255,0])
black = tree_min_ui_color([0,0,0])
white = tree_min_ui_color([255,255,255])

; Create a filled circle symbol
nth = 10
th = (findgen(nth))*(2.0*!pi)/(nth)
usersym,cos(th),sin(th),/fill

title = 'Stochastic tree minimization visualization'
if !version.release ge 6.1 then $
   tlb = widget_base(/row,title = title,/tlb_frame_attr,/tab_mode)   $
else  $
   tlb = widget_base(/row,title = title,/tlb_frame_attr)
ctrl_base = widget_base(tlb,/col,/align_top,/frame)
fxsize = 5
void = cw_field(ctrl_base,title = 'Parameter range',  $
   value = '-2.,20.',/col,   $
   /return_events,uname = 'PRANGE',xsize = fxsize)

void = cw_field(ctrl_base,title = '# iterations',value = '25',   $
   /return_events,uname = 'ITERATIONS',xsize = fxsize,/col)
void = cw_field(ctrl_base,title = 'Branch probability decay width',  $
   value = '3.0', /return_events,uname = 'DECAY_WIDTH',xsize = fxsize,/col)
void = cw_field(ctrl_base,title = 'Branch probability increment', $
   value = '10',/return_events,uname = 'INCREMENT',xsize = fxsize,/col)
void = cw_field(ctrl_base,title = 'Update time',uname = 'UPDATE', $
   /return_events,xsize = fxsize,/col,value = '0.001')
text = '0 nodes'
void = widget_label(ctrl_base,value = text,xsize = 100,uname='TREE_LABEL')
void = widget_button(ctrl_base,value = 'Minimize',uname = 'MINIMIZE')
void = widget_button(ctrl_base,value = 'Quit',uname = 'QUIT')

xsize = 600 & ysize = 500
plot_base = widget_base(tlb,/col,/frame,/align_top)
this_eq = 'y = 2.0+sin(5.*x)*exp(-0.5*((x-2.)/4.0)^2)'

eq_list = ['y=2.+(1.0-x*exp(-0.5*((x-2.0)/0.5)^2))+(1.0-exp(-0.5*((x-15.0)/2.0)^2))',  $
         'y=2.+cos(3.*x)*exp(-0.5*((x-2.0)/2.0)^2)',this_eq]
void = widget_combobox(plot_base,/editable,value = eq_list,xsize = xsize, $
   uname = 'EQ_CHOICE')
win = widget_draw(plot_base,xsize = xsize,ysize = ysize, $
   /motion_events,/button_events,uname = 'ZOOM')
widget_control,tlb,/realize
widget_control,win,get_value = winvis
window,/free,/pixmap,xsize = xsize,ysize = ysize
winpix = !d.window
pdata = ptr_new(/allocate_heap)
state =  {  winvis:winvis,             $
            green:green,               $
            white:white,               $
            black:black,               $
            yellow:yellow,             $
            red:red,                   $
            blue:blue,                 $
            autoscale:1B,              $
            mouse:0B,                  $
            pdata:pdata,               $
            ostart:obj_new(),          $
            obest:obj_new(),           $
            xrange:fltarr(2),          $
            autoyrange:fltarr(2),      $
            yrange:fltarr(2),          $
            xbox:intarr(2),            $
            ybox:intarr(2),            $
            winpix:winpix              }

pstate = ptr_new(state)
widget_control,tlb,set_uvalue = pstate

reg_name = 'tree_min_ui'
cleanup = 'tree_min_ui_cleanup'
handler = 'tree_min_ui_event'
xmanager,reg_name,tlb,cleanup = cleanup,  $
   event_handler = handler,/no_block

pevent = {event,id:win,top:tlb,handler:0L}
tree_min_gen_fun,pevent
tree_min_plot_refresh,pevent

end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -