class_docstr: Test the 2-dimensional rosenbrock optimization problem.
class_docstr_tok: ["Test","the","2-dimensional","rosenbrock","optimization","problem","."]
code: def _run_solver(self, early_terminate=False, **kwds):
      from mystic.monitors import Monitor
      import numpy
      from mystic.tools import random_seed
  
      seed = (
          111 if self.maxiter is None else 321
      )  # XXX: good numbers...
      random_seed(seed)
      esow = Monitor()
      ssow = Monitor()
  
      solver = self.solver
      solver.SetRandomInitialPoints(min=self.min, max=self.max)
      if self.usebounds:
          solver.SetStrictRanges(self.min, self.max)
      if self.uselimits:
          solver.SetEvaluationLimits(self.maxiter, self.maxfun)
      if self.useevalmon:
          solver.SetEvaluationMonitor(esow)
      if self.usestepmon:
          solver.SetGenerationMonitor(ssow)
      #### run solver, but trap output
      _stdout = trap_stdout()
      solver.Solve(self.costfunction, self.term, **kwds)
      out = release_stdout(_stdout)
      ################################
      sol = solver.Solution()
  
      iter = 1
      # if self.uselimits and self.maxiter == 0: iter=0
      # sanity check solver internals
      self.assertTrue(
          solver.generations == len(solver._stepmon._y) - iter
      )
      self.assertTrue(
          list(solver.bestSolution) == solver._stepmon.x[-1]
      )  # XXX
      self.assertTrue(solver.bestEnergy == solver._stepmon.y[-1])
      self.assertTrue(solver.solution_history == solver._stepmon.x)
      self.assertTrue(solver.energy_history == solver._stepmon.y)
      if self.usestepmon:
          self.assertTrue(ssow.x == solver._stepmon.x)
          self.assertTrue(ssow.y == solver._stepmon.y)
          self.assertTrue(ssow._y == solver._stepmon._y)
      if self.useevalmon:
          self.assertTrue(solver.evaluations == len(solver._evalmon._y))
          self.assertTrue(esow.x == solver._evalmon.x)
          self.assertTrue(esow.y == solver._evalmon.y)
          self.assertTrue(esow._y == solver._evalmon._y)
  
      # Fail appropriately for solver/termination mismatch
      if early_terminate:
          self.assertTrue(solver.generations < 2)
          warn = "Warning: Invalid termination condition (nPop < 2)"
          self.assertTrue(warn in out)
          return
  
      g = solver.generations
      calls = [(g + 1) * self.NP, (2 * g) + 1]
      iters = [g]
      # Test early terminations
      if self.uselimits and self.maxfun == 0:
          calls += [1, 20]  # XXX: scipy*
          iters += [1]  # XXX: scipy*
          self.assertTrue(solver.evaluations in calls)
          self.assertTrue(solver.generations in iters)
          return
      if self.uselimits and self.maxfun == 1:
          calls += [1, 20]  # XXX: scipy*
          iters += [1]  # XXX: scipy*
          self.assertTrue(solver.evaluations in calls)
          self.assertTrue(solver.generations in iters)
          return
      if self.uselimits and self.maxiter == 0:
          calls += [1, 20]  # XXX: scipy*
          iters += [1]  # XXX: scipy*
          self.assertTrue(solver.evaluations in calls)
          self.assertTrue(solver.generations in iters)
          return
      if self.uselimits and self.maxiter == 1:
          calls += [20]  # Powell's
          self.assertTrue(solver.evaluations in calls)
          self.assertTrue(solver.generations in iters)
          return
      if self.uselimits and self.maxiter and 2 <= self.maxiter <= 5:
          calls += [52, 79, 107, 141]  # Powell's
          self.assertTrue(solver.evaluations in calls)
          self.assertTrue(solver.generations in iters)
          return
  
      # Verify solution is close to exact
      # print(sol)
      for i in range(len(sol)):
          self.assertAlmostEqual(sol[i], self.exact[i], self.precision)
      return
  
imports: import sys
  from io import StringIO
  import unittest
  from math import *
  from mystic.math import almostEqual
label_desc: Use image node_id 2 for calling the TestRosenbrock obj's underlying member method code with example usage: obj._run_solver(early_terminate) without return types