- Timestamp:
- 11/05/12 16:46:36 (4 years ago)
- Location:
- branches/UQ/math/legacy
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/UQ/math/legacy/TEST_OUQ_1dSurr_CxCy.py
r585 r586 16 16 crossover = 0.9 17 17 percent_change = 0.9 18 smearing = 0.001 18 19 19 20 … … 27 28 # the differential evolution optimizer 28 29 ####################################################################### 29 def optimize(cost,_bounds,_constraints ):30 def optimize(cost,_bounds,_constraints,stepmon=None,initial=None): 30 31 from mystic.solvers import DifferentialEvolutionSolver2 31 32 from mystic.termination import ChangeOverGeneration as COG … … 36 37 #random_seed(123) 37 38 38 stepmon = VerboseMonitor(1) 39 #stepmon = Monitor() 39 if stepmon is None: 40 stepmon = Monitor() 41 #stepmon = VerboseMonitor(1) 40 42 evalmon = Monitor() 41 43 … … 44 46 45 47 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) 47 50 solver.SetStrictRanges(min=lb,max=ub) 48 51 solver.SetEvaluationLimits(maxiter,maxfun) … … 68 71 # maximize the function 69 72 ####################################################################### 70 def maximize(params,npts,bounds ):73 def maximize(params,npts,bounds,printmon=None,**kwds): 71 74 72 75 from mystic.math.dirac_measure import scenario … … 158 161 159 162 # 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) 161 165 162 166 if MINMAX == 1: … … 164 168 else: 165 169 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 168 176 169 177 return solved, func_max … … 215 223 bounds = (lower_bounds,upper_bounds) 216 224 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") 231 243 232 244 param_string = "[" … … 247 259 param_string = param_string[:-2] + "]" 248 260 249 print " parameters: %s" % param_string250 print " lower bounds: %s" % lower_bounds251 print " upper bounds: %s" % upper_bounds261 printmon.info(" parameters: %s" % param_string) 262 printmon.info(" lower bounds: %s" % lower_bounds) 263 printmon.info(" upper bounds: %s" % upper_bounds) 252 264 # print " ..." 253 265 … … 255 267 #npts = (nx,ny,nz) 256 268 #bounds = (lower_bounds,upper_bounds) 257 solved, diameter = maximize(pars,npts,bounds) 269 solved, diameter = maximize(pars,npts,bounds, \ 270 printmon=printmon,initial=initial) 258 271 259 272 from numpy import array … … 261 274 c = scenario() 262 275 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)) 267 286 #print "solved: %s" % str( c.flatten(all=True) ) 268 287 269 print 288 printmon.info("fails valid wrt model:\n%s" % \ 270 289 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]) 274 293 275 294 try: _Cx = max(Cx) … … 278 297 Ry = graphical_distance(model, c, ytol=Cy, xtol=Cx, cutoff=0.0, 279 298 imax=0, hausdorff=hausdorff) 280 print "vertical_distance: %s <= %s" % (Ry, Cy + _Cx)299 printmon.info("vertical_distance: %s <= %s" % (Ry, Cy + _Cx)) 281 300 Rv = graphical_distance(model, c, ytol=Cy, xtol=Cx, cutoff=0.0, 282 301 imax=imax_c, hausdorff=hausdorff) 283 print "graphical_distance: %s <= %s" % (Rv, Cy)302 printmon.info("graphical_distance: %s <= %s" % (Rv, Cy)) 284 303 285 304 # EOF -
branches/UQ/math/legacy/TEST_OUQ_1dSurr_Cy.py
r585 r586 16 16 crossover = 0.9 17 17 percent_change = 0.9 18 smearing = 0.001 18 19 19 20 … … 27 28 # the differential evolution optimizer 28 29 ####################################################################### 29 def optimize(cost,_bounds,_constraints ):30 def optimize(cost,_bounds,_constraints,stepmon=None,initial=None): 30 31 from mystic.solvers import DifferentialEvolutionSolver2 31 32 from mystic.termination import ChangeOverGeneration as COG … … 36 37 #random_seed(123) 37 38 38 stepmon = VerboseMonitor(1) 39 #stepmon = Monitor() 39 if stepmon is None: 40 stepmon = Monitor() 41 #stepmon = VerboseMonitor(1) 40 42 evalmon = Monitor() 41 43 … … 44 46 45 47 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) 47 50 solver.SetStrictRanges(min=lb,max=ub) 48 51 solver.SetEvaluationLimits(maxiter,maxfun) … … 68 71 # maximize the function 69 72 ####################################################################### 70 def maximize(params,npts,bounds ):73 def maximize(params,npts,bounds,printmon=None,**kwds): 71 74 72 75 from mystic.math.dirac_measure import scenario … … 158 161 159 162 # 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) 161 165 162 166 if MINMAX == 1: … … 164 168 else: 165 169 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 168 176 169 177 return solved, func_max … … 214 222 bounds = (lower_bounds,upper_bounds) 215 223 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") 230 242 231 243 param_string = "[" … … 246 258 param_string = param_string[:-2] + "]" 247 259 248 print " parameters: %s" % param_string249 print " lower bounds: %s" % lower_bounds250 print " upper bounds: %s" % upper_bounds260 printmon.info(" parameters: %s" % param_string) 261 printmon.info(" lower bounds: %s" % lower_bounds) 262 printmon.info(" upper bounds: %s" % upper_bounds) 251 263 # print " ..." 252 264 … … 254 266 #npts = (nx,ny,nz) 255 267 #bounds = (lower_bounds,upper_bounds) 256 solved, diameter = maximize(pars,npts,bounds) 268 solved, diameter = maximize(pars,npts,bounds, \ 269 printmon=printmon,initial=initial) 257 270 258 271 from numpy import array … … 260 273 c = scenario() 261 274 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)) 266 285 #print "solved: %s" % str( c.flatten(all=True) ) 267 286 268 print 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]) 272 291 273 292 try: _Cx = max(Cx) … … 275 294 from mystic.math.paramtrans import graphical_distance 276 295 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)) 278 297 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)) 280 299 281 300 # EOF
Note: See TracChangeset
for help on using the changeset viewer.