Skip to content

Instantly share code, notes, and snippets.

@denis-bz
Created September 30, 2018 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denis-bz/d0a43ef79bae3550dd8e9d36cf956208 to your computer and use it in GitHub Desktop.
Save denis-bz/d0a43ef79bae3550dd8e9d36cf956208 to your computer and use it in GitHub Desktop.
Trivial sketch of a function of n variables: vary one at a time
# from: Coord-sketch.py
# 30 Sep 2018 18:15 in ~bz/py/opt/testfuncs/smt Denis-iMac 10.10.5
versions: numpy 1.15.2 scipy 1.1.0 python 2.7.15 mac 10.10.5
================================================================================
coord_sketch: k * dim func() values, varying one coordinate at a time,
give a cheap starting point for further optimization
especially if func() is monotone, up or down, in each coordinate.
# func: torsion_vibration f(x0): 17.9 x0: [2 10 11.7 8 3.9 12 1.82 12 6.2 2.25 3 0.28 14 4 0.1]
# coord low high jmin func() varying that one coordinate - func(x0)
0 1.8 2.2: 0 [-0.448 -0.215 0 0.199 0.384]
1 9 11: 4 [0.423 0.209 0 -0.205 -0.404]
2 10.53 12.87: 0 [-0.448 -0.215 0 0.199 0.384]
3 7.2 8.8: 4 [0.467 0.226 0 -0.212 -0.411]
4 3.51 4.29: 0 [-0.454 -0.223 0 0.215 0.422]
5 10.8 13.2: 4 [0.819 0.452 0 -0.537 -1.15]
6 1.6425 2.0075: 0 [-0.0464 -0.022 0 0.02 0.0383]
7 10.8 13.2: 4 [0.0422 0.021 0 -0.0209 -0.0417]
8 5.58 6.82: 0 [-0.0464 -0.022 0 0.02 0.0383]
9 2.025 2.475: 0 [-0.454 -0.223 0 0.215 0.422]
10 2.7 3.3: 4 [0.321 0.163 0 -0.167 -0.338]
11 0.252 0.308: 4 [0.321 0.163 0 -0.167 -0.338]
12 12.6 15.4: 4 [1.55 0.816 0 -0.859 -1.72]
13 3.6 4.4: 4 [0.571 0.284 0 -0.279 -0.553]
14 0.09 0.11: 4 [0.571 0.284 0 -0.279 -0.553]
fmin: 12.1 xmin: [1.8 11 10.5 8.8 3.51 13.2 1.64 13.2 5.58 2.02 3.3 0.308 15.4 4.4 0.11]
# func: water_flow f(x0): 70.9 x0: [0.1 2.50e+04 8.93e+04 1.05e+03 89.5 760 1.4e+03 1.1e+04]
# coord low high jmin func() varying that one coordinate - func(x0)
0 0.05 0.15: 0 [-53.1 -30.9 0 39.5 87.6]
1 100 50000: 4 [0.171 0.0212 0 -0.0125 -0.0213]
2 63070 115600: 0 [-0.00016 -6.61e-05 0 4.92e-05 8.72e-05]
3 990 1110: 0 [-14.7 -7.33 0 7.33 14.7]
4 63.1 116: 0 [-0.16 -0.0662 0 0.0493 0.0874]
5 700 820: 4 [14.7 7.33 0 -7.33 -14.7]
6 1120 1680: 4 [17.6 7.83 0 -6.41 -11.8]
7 9855 12045: 0 [-7.05 -3.53 0 3.52 7.05]
fmin: 7.82 xmin: [0.05 5e+04 6.31e+04 990 63.1 820 1.68e+03 9.86e+03]
#!/usr/bin/env python2
""" coord_sketch: k * dim func() values, varying one coordinate at a time,
give a cheap starting point for further optimization
especially if func() is monotone, up or down, in each coordinate.
"""
from __future__ import division
import sys
import numpy as np
# from https://github.com/SMTorg/smt Surrogate Modeling Toolbox
# not so hot
from torsion_vibration import torsion_vibration # 15d, monotone
from water_flow import water_flow # 8d, monotone
np.set_printoptions( threshold=20, edgeitems=10, linewidth=140,
formatter = dict( float = lambda x: "%.3g" % x )) # float arrays %.3g
print "\n" + 80 * "="
print __doc__
#...............................................................................
def coord_sketch( func, lo, hi, k=5, x0=None ):
assert len(lo) == len(hi), [len(lo), len(hi)]
assert np.all( np.isfinite( [lo, hi] )), [lo, hi]
dim = len(lo)
if x0 is None:
x0 = (lo + hi) / 2
f0 = func( x0 )
print "\n# func: %s f(x0): %.3g x0: %s " % (
func.__name__, f0, x0 )
print "# coord low high jmin func() varying that one coordinate - func(x0)"
xmin = np.NaN * np.ones(dim)
for j in range( dim ):
f = []
x = x0.copy()
xgrid = np.linspace( lo[j], hi[j], k ) # uniform, Chebyshev ?
for x[j] in xgrid:
f.append( func( x ))
f = np.array( f )
jmin = f.argmin()
xmin[j] = xgrid[jmin]
print "%2d %8g %8g: %2d %s " % (
j, lo[j], hi[j], jmin, f - f0 )
fmin = func( xmin )
print "fmin: %.3g xmin: %s" % (fmin, xmin)
return fmin, xmin
np.random.seed( 3 )
#...............................................................................
for func in [torsion_vibration, water_flow]:
coord_sketch( func, func.lo, func.hi )
#!/usr/bin/env python2
"""
Author: Dr. Mohamed Amine Bouhlel <mbouhlel@umich.edu>
Dr. John T. Hwang <hwangjt@umich.edu>
This package is distributed under New BSD license.
Torsion vibration problem from:
Liu, H., Xu, S., & Wang, X. Sampling strategies and metamodeling techniques for engineering design: comparison and application. In ASME Turbo Expo 2016: Turbomachinery Technical Conference and Exposition. American Society of Mechanical Engineers.
June, 2016.
Wang, L., Beeson, D., Wiggs, G., and Rayasam, M. A Comparison of Metamodeling Methods Using Practical Industry Requirements. In Proceedings of the 47th AIAA/ASME/ASCE/AHS/ASC structures, structural dynamics, and materials conference, Newport, RI, pp. AIAA
2006-1811.
"""
# https://github.com/SMTorg/smt $lopy/smt-master/smt/problems/torsion_vibration.py
from __future__ import division
import numpy as np
def torsion_vibration( x ):
x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14 = x
x2 *= 1e6 # bad scaling ? no diff
x4 *= 1e6
x8 *= 1e6
K1 = np.pi*x2*x0/(32*x1)
K2 = np.pi*x8*x6/(32*x7)
K3 = np.pi*x4*x9/(32*x3)
M1 = x11*np.pi*x10*x5/(4*9.80665)
M2 = x14*np.pi*x13*x12/(4*9.80665)
J1 = 0.5*M1*(x5/2)**2
J2 = 0.5*M2*(x12/2)**2
a = 1
b = -((K1+K2)/J1+(K2+K3)/J2)
c = (K1*K2+K2*K3+K3*K1)/(J1*J2)
return np.sqrt((-b - np.sqrt(b**2-4*a*c)) / (2*a)) / (2*np.pi)
torsion_vibration.dim = 15
torsion_vibration.lo, torsion_vibration.hi = np.array([
[1.8, 9, 10.530000, 7.2, 3.510000, 10.8, 1.6425, 10.8, 5.580000, 2.025, 2.7, 0.252, 12.6, 3.6, 0.09],
[2.2, 11, 12.870000, 8.8, 4.290000, 13.2, 2.0075, 13.2, 6.820000, 2.475, 3.3, 0.308, 15.4, 4.4, 0.11]
])
# L-BFGS-B percentbounds: [ 0 100 0 100 0 100 0 100 0 0 100 100 100 100 100]
#...............................................................................
if __name__ == "__main__":
import sys
from etc import numpyutil as nu
# from random_in_box import random_in_box, func_in_box
np.set_printoptions( threshold=20, edgeitems=10, linewidth=140,
formatter = dict( float=nu.numstr ))
print "\n" + 80 * "="
dim = 15
nrand = 1000 # 14.4 .5 .6 .6 .8
# to change these params, run this.py a=1 b=None c='"str"' ... in sh or ipython
for arg in sys.argv[1:]:
exec( arg ) # name not defined error: forgot '' ?
np.random.seed( 3 )
# box = random_in_box( nrand, torsion_vibration.lo, torsion_vibration.hi )
# ff, xx = func_in_box( torsion_vibration, box )
#!/usr/bin/env python2
"""
Author: Dr. Mohamed Amine Bouhlel <mbouhlel@umich.edu>
Dr. John T. Hwang <hwangjt@umich.edu>
This package is distributed under New BSD license.
Water flow problem from:
Liu, H., Xu, S., & Wang, X. Sampling strategies and metamodeling techniques for engineering design:
June, 2016.
Morris, M. D., Mitchell, T. J., and Ylvisaker, D. Bayesian Design and Analysis of Computer Experime
"""
# https://github.com/SMTorg/smt $lopy/smt-master/smt/problems/water_flow.py
from __future__ import division
import numpy as np
def water_flow( x ):
x0,x1,x2,x3,x4,x5,x6,x7 = x
return (2 * np.pi * x2 * (x3 - x5)
/ (np.log(x1 / x0) * (1 + 2 * x6
* x2 / (np.log(x1 / x0) * x0** 2 * x7)
+ x2 / x4 )))
water_flow.ndim = 8
water_flow.lo, water_flow.hi = np.array([
[0.05, 100, 63070, 990, 63.1, 700, 1120, 9855],
[0.15, 50000, 115600, 1110, 116, 820, 1680, 12045] ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment