Changeset 586 for branches


Ignore:
Timestamp:
11/05/12 16:46:36 (4 years ago)
Author:
mmckerns
Message:

TEST_OUQ_1dSurr* demonstrates passing in a monitor for all print statements

Location:
branches/UQ/math/legacy
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/UQ/math/legacy/TEST_OUQ_1dSurr_CxCy.py

    r585 r586  
    1616crossover = 0.9 
    1717percent_change = 0.9 
     18smearing = 0.001 
    1819 
    1920 
     
    2728# the differential evolution optimizer 
    2829####################################################################### 
    29 def optimize(cost,_bounds,_constraints): 
     30def optimize(cost,_bounds,_constraints,stepmon=None,initial=None): 
    3031  from mystic.solvers import DifferentialEvolutionSolver2 
    3132  from mystic.termination import ChangeOverGeneration as COG 
     
    3637 #random_seed(123) 
    3738 
    38   stepmon = VerboseMonitor(1) 
    39  #stepmon = Monitor() 
     39  if stepmon is None: 
     40    stepmon = Monitor() 
     41   #stepmon = VerboseMonitor(1) 
    4042  evalmon = Monitor() 
    4143 
     
    4446 
    4547  solver = DifferentialEvolutionSolver2(ndim,npop) 
    46   solver.SetRandomInitialPoints(min=lb,max=ub) 
     48  if initial: solver.SetInitialPoints(initial,radius=smearing) 
     49  else: solver.SetRandomInitialPoints(min=lb,max=ub) 
    4750  solver.SetStrictRanges(min=lb,max=ub) 
    4851  solver.SetEvaluationLimits(maxiter,maxfun) 
     
    6871# maximize the function 
    6972####################################################################### 
    70 def maximize(params,npts,bounds): 
     73def maximize(params,npts,bounds,printmon=None,**kwds): 
    7174 
    7275  from mystic.math.dirac_measure import scenario 
     
    158161 
    159162  # maximize 
    160   solved, func_max, func_evals = optimize(cost,(lb,ub),constraints) 
     163  solved, func_max, func_evals = optimize(cost,(lb,ub),constraints, \ 
     164                                                       printmon,**kwds) 
    161165 
    162166  if MINMAX == 1: 
     
    164168  else: 
    165169    tag = 'maximum' # sup 
    166   print "func_%s: %s" % (tag,func_max) 
    167   print "func_evals: %s" % func_evals 
     170  if printmon: 
     171    printmon.info("func_%s: %s" % (tag,func_max)) 
     172    printmon.info("func_evals: %s" % func_evals) 
     173  else: 
     174    print "func_%s: %s" % (tag,func_max) 
     175    print "func_evals: %s" % func_evals 
    168176 
    169177  return solved, func_max 
     
    215223  bounds = (lower_bounds,upper_bounds) 
    216224 
    217   print "...SETTINGS..." 
    218   print "npop = %s" % npop 
    219   print "maxiter = %s" % maxiter 
    220   print "maxfun = %s" % maxfun 
    221   print "convergence_tol = %s" % convergence_tol 
    222   print "crossover = %s" % crossover 
    223   print "percent_change = %s" % percent_change 
    224   print "..............\n" 
    225  
    226   print " model: f(x) = %s(x)" % function_name 
    227   print " target: %s" % str(target) 
    228   print " error: %s" % str(error) 
    229   print " npts: %s" % str((nx,ny,nz)) 
    230   print "..............\n" 
     225  initial = None # can provide a list of initial values for rv 
     226 
     227  from mystic.monitors import VerboseMonitor, LoggingMonitor 
     228  printmon = VerboseMonitor(1) 
     229  printmon.info("...SETTINGS...") 
     230  printmon.info("npop = %s" % npop) 
     231  printmon.info("maxiter = %s" % maxiter) 
     232  printmon.info("maxfun = %s" % maxfun) 
     233  printmon.info("convergence_tol = %s" % convergence_tol) 
     234  printmon.info("crossover = %s" % crossover) 
     235  printmon.info("percent_change = %s" % percent_change) 
     236  printmon.info("..............\n") 
     237 
     238  printmon.info(" model: f(x) = %s(x)" % function_name) 
     239  printmon.info(" target: %s" % str(target)) 
     240  printmon.info(" error: %s" % str(error)) 
     241  printmon.info(" npts: %s" % str((nx,ny,nz))) 
     242  printmon.info("..............\n") 
    231243 
    232244  param_string = "[" 
     
    247259  param_string = param_string[:-2] + "]" 
    248260 
    249   print " parameters: %s" % param_string 
    250   print " lower bounds: %s" % lower_bounds 
    251   print " upper bounds: %s" % upper_bounds 
     261  printmon.info(" parameters: %s" % param_string) 
     262  printmon.info(" lower bounds: %s" % lower_bounds) 
     263  printmon.info(" upper bounds: %s" % upper_bounds) 
    252264# print " ..." 
    253265 
     
    255267 #npts = (nx,ny,nz) 
    256268 #bounds = (lower_bounds,upper_bounds) 
    257   solved, diameter = maximize(pars,npts,bounds) 
     269  solved, diameter = maximize(pars,npts,bounds, \ 
     270                              printmon=printmon,initial=initial) 
    258271 
    259272  from numpy import array 
     
    261274  c = scenario() 
    262275  c.load(solved,npts) 
    263   print "solved: [wx,x]\n%s" % array(zip(c[0].weights,c[0].coords)) 
    264   print "solved: [wy,y]\n%s" % array(zip(c[1].weights,c[1].coords)) 
    265   print "solved: [wz,z]\n%s" % array(zip(c[2].weights,c[2].coords)) 
    266   print "solved: [Y]\n%s" % array(c.values) 
     276  if isinstance(printmon, LoggingMonitor): 
     277    printmon.info("solved[wx,x]: %s" % zip(c[0].weights,c[0].coords)) 
     278    printmon.info("solved[wy,y]: %s" % zip(c[1].weights,c[1].coords)) 
     279    printmon.info("solved[wz,z]: %s" % zip(c[2].weights,c[2].coords)) 
     280    printmon.info("solved[Y]: %s" % c.values) 
     281  else: 
     282    printmon.info("solved: [wx,x]\n%s" % array(zip(c[0].weights,c[0].coords))) 
     283    printmon.info("solved: [wy,y]\n%s" % array(zip(c[1].weights,c[1].coords))) 
     284    printmon.info("solved: [wz,z]\n%s" % array(zip(c[2].weights,c[2].coords))) 
     285    printmon.info("solved: [Y]\n%s" % array(c.values)) 
    267286 #print "solved: %s" % str( c.flatten(all=True) ) 
    268287 
    269   print "fails valid wrt model:\n%s" % \ 
     288  printmon.info("fails valid wrt model:\n%s" % \ 
    270289        c.valid_wrt_model(model, xtol=Cx, ytol=Cy, blamelist=True, \ 
    271                                  imax=imax_c, hausdorff=hausdorff) 
    272   print "mean(y): %s >= %s" % (str( c.get_mean_value() ), y_mean - y_mean_error) 
    273   print "sum_wts: %s == 1.0" % [sum(w) for w in c.wts] 
     290                                 imax=imax_c, hausdorff=hausdorff)) 
     291  printmon.info("mean(y): %s >= %s" % (str( c.get_mean_value() ), y_mean - y_mean_error)) 
     292  printmon.info("sum_wts: %s == 1.0" % [sum(w) for w in c.wts]) 
    274293 
    275294  try: _Cx = max(Cx) 
     
    278297  Ry = graphical_distance(model, c, ytol=Cy, xtol=Cx, cutoff=0.0, 
    279298                          imax=0, hausdorff=hausdorff) 
    280   print "vertical_distance: %s <= %s" % (Ry, Cy + _Cx) 
     299  printmon.info("vertical_distance: %s <= %s" % (Ry, Cy + _Cx)) 
    281300  Rv = graphical_distance(model, c, ytol=Cy, xtol=Cx, cutoff=0.0, 
    282301                          imax=imax_c, hausdorff=hausdorff) 
    283   print "graphical_distance: %s <= %s" % (Rv, Cy) 
     302  printmon.info("graphical_distance: %s <= %s" % (Rv, Cy)) 
    284303 
    285304# EOF 
  • branches/UQ/math/legacy/TEST_OUQ_1dSurr_Cy.py

    r585 r586  
    1616crossover = 0.9 
    1717percent_change = 0.9 
     18smearing = 0.001 
    1819 
    1920 
     
    2728# the differential evolution optimizer 
    2829####################################################################### 
    29 def optimize(cost,_bounds,_constraints): 
     30def optimize(cost,_bounds,_constraints,stepmon=None,initial=None): 
    3031  from mystic.solvers import DifferentialEvolutionSolver2 
    3132  from mystic.termination import ChangeOverGeneration as COG 
     
    3637 #random_seed(123) 
    3738 
    38   stepmon = VerboseMonitor(1) 
    39  #stepmon = Monitor() 
     39  if stepmon is None: 
     40    stepmon = Monitor() 
     41   #stepmon = VerboseMonitor(1) 
    4042  evalmon = Monitor() 
    4143 
     
    4446 
    4547  solver = DifferentialEvolutionSolver2(ndim,npop) 
    46   solver.SetRandomInitialPoints(min=lb,max=ub) 
     48  if initial: solver.SetInitialPoints(initial,radius=smearing) 
     49  else: solver.SetRandomInitialPoints(min=lb,max=ub) 
    4750  solver.SetStrictRanges(min=lb,max=ub) 
    4851  solver.SetEvaluationLimits(maxiter,maxfun) 
     
    6871# maximize the function 
    6972####################################################################### 
    70 def maximize(params,npts,bounds): 
     73def maximize(params,npts,bounds,printmon=None,**kwds): 
    7174 
    7275  from mystic.math.dirac_measure import scenario 
     
    158161 
    159162  # maximize 
    160   solved, func_max, func_evals = optimize(cost,(lb,ub),constraints) 
     163  solved, func_max, func_evals = optimize(cost,(lb,ub),constraints, \ 
     164                                                       printmon,**kwds) 
    161165 
    162166  if MINMAX == 1: 
     
    164168  else: 
    165169    tag = 'maximum' # sup 
    166   print "func_%s: %s" % (tag,func_max) 
    167   print "func_evals: %s" % func_evals 
     170  if printmon: 
     171    printmon.info("func_%s: %s" % (tag,func_max)) 
     172    printmon.info("func_evals: %s" % func_evals) 
     173  else: 
     174    print "func_%s: %s" % (tag,func_max) 
     175    print "func_evals: %s" % func_evals 
    168176 
    169177  return solved, func_max 
     
    214222  bounds = (lower_bounds,upper_bounds) 
    215223 
    216   print "...SETTINGS..." 
    217   print "npop = %s" % npop 
    218   print "maxiter = %s" % maxiter 
    219   print "maxfun = %s" % maxfun 
    220   print "convergence_tol = %s" % convergence_tol 
    221   print "crossover = %s" % crossover 
    222   print "percent_change = %s" % percent_change 
    223   print "..............\n" 
    224  
    225   print " model: f(x) = %s(x)" % function_name 
    226   print " target: %s" % str(target) 
    227   print " error: %s" % str(error) 
    228   print " npts: %s" % str((nx,ny,nz)) 
    229   print "..............\n" 
     224  initial = None # can provide a list of initial values for rv 
     225 
     226  from mystic.monitors import VerboseMonitor, LoggingMonitor 
     227  printmon = VerboseMonitor(1) 
     228  printmon.info("...SETTINGS...") 
     229  printmon.info("npop = %s" % npop) 
     230  printmon.info("maxiter = %s" % maxiter) 
     231  printmon.info("maxfun = %s" % maxfun) 
     232  printmon.info("convergence_tol = %s" % convergence_tol) 
     233  printmon.info("crossover = %s" % crossover) 
     234  printmon.info("percent_change = %s" % percent_change) 
     235  printmon.info("..............\n") 
     236 
     237  printmon.info(" model: f(x) = %s(x)" % function_name) 
     238  printmon.info(" target: %s" % str(target)) 
     239  printmon.info(" error: %s" % str(error)) 
     240  printmon.info(" npts: %s" % str((nx,ny,nz))) 
     241  printmon.info("..............\n") 
    230242 
    231243  param_string = "[" 
     
    246258  param_string = param_string[:-2] + "]" 
    247259 
    248   print " parameters: %s" % param_string 
    249   print " lower bounds: %s" % lower_bounds 
    250   print " upper bounds: %s" % upper_bounds 
     260  printmon.info(" parameters: %s" % param_string) 
     261  printmon.info(" lower bounds: %s" % lower_bounds) 
     262  printmon.info(" upper bounds: %s" % upper_bounds) 
    251263# print " ..." 
    252264 
     
    254266 #npts = (nx,ny,nz) 
    255267 #bounds = (lower_bounds,upper_bounds) 
    256   solved, diameter = maximize(pars,npts,bounds) 
     268  solved, diameter = maximize(pars,npts,bounds, \ 
     269                              printmon=printmon,initial=initial) 
    257270 
    258271  from numpy import array 
     
    260273  c = scenario() 
    261274  c.load(solved,npts) 
    262   print "solved: [wx,x]\n%s" % array(zip(c[0].weights,c[0].coords)) 
    263   print "solved: [wy,y]\n%s" % array(zip(c[1].weights,c[1].coords)) 
    264   print "solved: [wz,z]\n%s" % array(zip(c[2].weights,c[2].coords)) 
    265   print "solved: [Y]\n%s" % array(c.values) 
     275  if isinstance(printmon, LoggingMonitor): 
     276    printmon.info("solved[wx,x]: %s" % zip(c[0].weights,c[0].coords)) 
     277    printmon.info("solved[wy,y]: %s" % zip(c[1].weights,c[1].coords)) 
     278    printmon.info("solved[wz,z]: %s" % zip(c[2].weights,c[2].coords)) 
     279    printmon.info("solved[Y]: %s" % c.values) 
     280  else: 
     281    printmon.info("solved: [wx,x]\n%s" % array(zip(c[0].weights,c[0].coords))) 
     282    printmon.info("solved: [wy,y]\n%s" % array(zip(c[1].weights,c[1].coords))) 
     283    printmon.info("solved: [wz,z]\n%s" % array(zip(c[2].weights,c[2].coords))) 
     284    printmon.info("solved: [Y]\n%s" % array(c.values)) 
    266285 #print "solved: %s" % str( c.flatten(all=True) ) 
    267286 
    268   print "fails valid wrt model:\n%s" % \ 
    269         c.valid_wrt_model(model, xtol=Cx, ytol=Cy, blamelist=True, imax=imax_c) 
    270   print "mean(y): %s >= %s" % (str( c.get_mean_value() ), y_mean - y_mean_error) 
    271   print "sum_wts: %s == 1.0" % [sum(w) for w in c.wts] 
     287  printmon.info("fails valid wrt model:\n%s" % \ 
     288        c.valid_wrt_model(model, xtol=Cx, ytol=Cy, blamelist=True, imax=imax_c)) 
     289  printmon.info("mean(y): %s >= %s" % (str( c.get_mean_value() ), y_mean - y_mean_error)) 
     290  printmon.info("sum_wts: %s == 1.0" % [sum(w) for w in c.wts]) 
    272291 
    273292  try: _Cx = max(Cx) 
     
    275294  from mystic.math.paramtrans import graphical_distance 
    276295  Ry = graphical_distance(model, c, ytol=Cy, xtol=Cx, cutoff=0.0, imax=0) 
    277   print "vertical_distance: %s <= %s" % (Ry, Cy + _Cx) 
     296  printmon.info("vertical_distance: %s <= %s" % (Ry, Cy + _Cx)) 
    278297  Rv = graphical_distance(model, c, ytol=Cy, xtol=Cx, cutoff=0.0, imax=imax_c) 
    279   print "graphical_distance: %s <= %s" % (Rv, Cy) 
     298  printmon.info("graphical_distance: %s <= %s" % (Rv, Cy)) 
    280299 
    281300# EOF 
Note: See TracChangeset for help on using the changeset viewer.