Source code for ddg.optimize.ifs._optimize

""" Basic optimization functions for IndexedFaceSet functionals.

Note that for more control of the optimization process, you will have to
use the function wrappers provided in `ddg.optimize.ifs` and call
scipy.optimize.minimize/least_squares explicitly.

For more information on the required signature and examples for functionals see
`ddg.optimize.ifs.functionals`.
"""
import scipy.optimize as opt

from ._wrappers import _flatten_input_ifs_function_remove
from .utils import attribute_helper


[docs]def minimize(ifs, functional, cell_type, *attr_name, boundary=[]): """Quick access for scipy.optimize.minimize for IndexedFaceSet functionals. Parameters ---------- ifs : IndexedFaceSet indexed face set to optimize on functional : Callable functional to optimize for cell_type : {'verts', 'edges', 'faces'} cell type of the parameter attribute *attr_name names of attributes to use in the functional. attr_name[0] is regarded as the only parameter attribute boundary : list (default = []) list of boundary cells to ignore Returns ------- res : scipy.optimize.OptimizeResult See Also -------- least_squares, wrap_functional Notes ----- Similar to `wrap_functional` values for the boundary cells are removed from the signature of the flattened functional before the optimization step. """ array, mask, _, shape = attribute_helper(ifs, cell_type, attr_name[0], boundary) wrapped = _flatten_input_ifs_function_remove( ifs, functional, array, shape, mask, *attr_name ) res = opt.minimize(wrapped, array[mask].flatten()) array[mask] = res.x.reshape(shape) return res
[docs]def least_squares(ifs, functional, cell_type, *attr_name, boundary=[]): """Quick access for scipy.optimize.least_squares for IndexedFaceSet functionals. Parameters ---------- ifs : IndexedFaceSet indexed face set to optimize on functional : Callable functional to optimize for cell_type : {'verts', 'edges', 'faces'} cell type of the parameter attribute *attr_name names of attributes to use in the functional. attr_name[0] is regarded as the only parameter attribute boundary : list (default = []) list of boundary cells to ignore Returns ------- res : scipy.optimize.OptimizeResult See Also -------- minimize, wrap_functional Notes ----- Similar to `wrap_functional` values for the boundary cells are removed from the signature of the flattened functional before the optimization step. """ array, mask, _, shape = attribute_helper(ifs, cell_type, attr_name[0], boundary) wrapped = _flatten_input_ifs_function_remove( ifs, functional, array, shape, mask, *attr_name ) res = opt.least_squares(wrapped, array[mask].flatten()) array[mask] = res.x.reshape(shape) return res