6
6

More than 5 years have passed since last update.

# OpenMDAOでラテン超方格サンプリング

Last updated at Posted at 2016-11-29
1 / 7

# ラテン超方格サンプリング(LHS)とは

インストール
pip install pyDOE


>from pyDOE import lhs
>lhs(2,5,"c")
array([[ 0.3,  0.7],
[ 0.1,  0.1],
[ 0.5,  0.9],
[ 0.9,  0.5],
[ 0.7,  0.3]])


さらに引数"c"を指定しなければ区間内からランダムに抽出される.
こんな感じである.

# OpenMDAOを用いた放物面のラテン超方格サンプリング


\begin{align}
& f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 \\
{\rm subject \: to} \: \: \:&  -50.0\leq x \leq 50.0 \\
&  -50.0\leq y \leq 50.0
\end{align}


# Componentの準備

paraboloid.py
from openmdao.api import Component
class Paraboloid(Component):
""" Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """
def __init__(self):
super(Paraboloid, self).__init__()
def solve_nonlinear(self, params, unknowns, resids):
"""f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """
x = params['x']; y = params['y']
unknowns['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0


# Problem(問題）の設定

doe_paraboloid.py
#!/bin/pyhton

from openmdao.api import IndepVarComp, Group, Problem,  SqliteRecorder
from paraboloid import Paraboloid
from openmdao.drivers.latinhypercube_driver import OptimizedLatinHypercubeDriver
from openmdao.core.mpi_wrap import MPI

if MPI: # pragma: no cover
# if you called this script with 'mpirun', then use the petsc data passing
from openmdao.core.petsc_impl import PetscImpl as impl
else:
# if you didn't use mpirun, then use the numpy data passing
from openmdao.api import BasicImpl as impl

top = Problem(impl=impl)
root = top.root = Group()

top.driver = OptimizedLatinHypercubeDriver(num_samples=100, seed=0, population=20, \
generations=4, norm_method=2, num_par_doe=5)


doe_paraboloid.py続き
top.driver.add_objective('f_xy')

recorder = SqliteRecorder('doe_paraboloid')
recorder.options['record_params'] = True
recorder.options['record_unknowns'] = True
recorder.options['record_resids'] = False

top.setup()
top.run()

top.cleanup()


OptimizedLatinHypercubeDriverの引数にはサンプル数は100,ランダムシード0, GAの個体20,世代4,GAの中で各DOEポイント間の距離を正規化するノルム(np.linalg.normで使用),num_par_doeで並列化数が指定されている.

ターミナルで下記を実行する

mpirun -np 5 python doe_paraboloid.py


# 結果の読み込み

IPython
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import sqlitedict

db =sqlitedict.SqliteDict("doe_paraboloid","iterations")
res = np.array([[0.,0.,0.]] * len(db.items()))
for i, v in enumerate(db.items()):
res[i,0] = v[1]["Unknowns"]["x"]
res[i,1] = v[1]["Unknowns"]["y"]
res[i,2] = v[1]["Unknowns"]["f_xy"]

x = np.arange(-50, 50, 4)
y = np.arange(-50, 50, 4)
X, Y = np.meshgrid(x, y)
Z = (X-3.0)**2 + X*Y + (Y+4.0)**2 - 3.0

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_wireframe(X,Y,Z)
ax.scatter3D(res[:,0],res[:,1],res[:,2],c="r", marker="o")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f_xy')
plt.show()


6
6
0

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
6
6