class_docstr: .. include:: /global.rst
  
  Please refer to |nelder-mead| for details about this algorithm.
  
  >>> s = NelderMead(x=1, y=1, xtol=1e-8) #doctest:+SKIP
  >>> best_pars, _ = s.optimize(lambda x, y: -x**2 - y**2) #doctest:+SKIP
  >>> [math.fabs(best_pars['x']) < 1e-8, math.fabs(best_pars['y']) < 1e-8]  #doctest:+SKIP
  [True, True]
class_docstr_tok: ["..","include",":",":","\/global.rst","Please","refer","to","|nelder-mead|","for","details","about","this","algorithm",".",">",">",">","s","=","NelderMead","(","x=1",",","y=1",",","xtol=1e-8",")","#","doctest",":","+SKIP",">",">",">","best_pars",",","_","=","s.optimize","(","lambda","x",",","y",":","-x","*","*","2","-","y","*","*","2",")","#","doctest",":","+SKIP",">",">",">","[","math.fabs","(","best_pars","[","'","x","'","]",")","<","1e-8",",","math.fabs","(","best_pars","[","'","y","'","]",")","<","1e-8","]","#","doctest",":","+SKIP","[","True",",","True","]"]
code: def _solve(self, func, x0):
      def f(x):
          return func(list(x))
  
      x0 = array.array("f", x0)
      N = len(x0)
  
      vertices = [x0]
      values = [f(x0)]
  
      # defaults taken from Wikipedia and SciPy
      alpha = 1.0
      gamma = 2.0
      rho = -0.5
      sigma = 0.5
      nonzdelt = 0.05
      zdelt = 0.00025
  
      # generate vertices
      for k in range(N):
          vert = vertices[0][:]
          if vert[k] != 0:
              vert[k] = (1 + nonzdelt) * vert[k]
          else:
              vert[k] = zdelt
  
          vertices.append(vert)
          values.append(f(vert))
  
      niter = 1
      while niter < self.max_iter:
          # sort vertices by ftion value
          vertices, values = NelderMead.sort_vertices(vertices, values)
  
          # check for convergence
          if abs(values[0] - values[-1]) <= self.ftol:
              break
  
          niter += 1
  
          # compute center of gravity
          x0 = NelderMead.simplex_center(vertices[:-1])
  
          # reflect
          xr = NelderMead.reflect(x0, vertices[-1], alpha)
          fxr = f(xr)
          if values[0] < fxr < values[-2]:
              vertices[-1] = xr
              values[-1] = fxr
              continue
  
          # expand
          if fxr < values[0]:
              xe = NelderMead.reflect(x0, vertices[-1], gamma)
              fxe = f(xe)
              if fxe < fxr:
                  vertices[-1] = xe
                  values[-1] = fxe
              else:
                  vertices[-1] = xr
                  values[-1] = fxr
              continue
  
          # contract
          xc = NelderMead.reflect(x0, vertices[-1], rho)
          fxc = f(xc)
          if fxc < values[-1]:
              vertices[-1] = xc
              values[-1] = fxc
              continue
  
          # reduce
          for idx in range(1, len(vertices)):
              vertices[idx] = NelderMead.reflect(
                  vertices[0], vertices[idx], sigma
              )
              values[idx] = f(vertices[idx])
  
      return list(
          vertices[min(enumerate(values), key=op.itemgetter(1))[0]]
      )
  
file_path: optunity/wrappers/R/inst/optunity/solvers/NelderMead.py
imports: import array
  import operator
  from ..None import functions
  from .solver_registry import register_solver
  from .util import Solver, _copydoc
  from .None import util
label_desc: Use image node_id 7 for calling the NelderMead obj's underlying member method code with example usage: obj._solve(func, x0) and returns: list, func