def _(params): import mystic._signal as signal print("plotting params: %s" % params) pylab.errorbar(binsc, histo, sqrt(histo), fmt='b+') x = arange(xmin, xmax, (0.1* binwidth)) pylab.plot(x, pdf(x)*N,'b:') pylab.plot(x, F(params)(x)*N,'r-') pylab.xlabel('E (GeV)') pylab.ylabel('Counts') try: show() except ImportError: pylab.show() if solver is not None: signal.signal(signal.SIGINT, signal.Handler(solver))
def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): """Minimize a 'cost' function with given termination conditions. Uses an optimization algorithm to find the minimum of a function of one or more variables. Args: cost (func, default=None): the function to be minimized: ``y = cost(x)``. termination (termination, default=None): termination conditions. ExtraArgs (tuple, default=None): extra arguments for cost. sigint_callback (func, default=None): callback function for signal handler. callback (func, default=None): function to call after each iteration. The interface is ``callback(xk)``, with xk the current parameter vector. disp (bool, default=False): if True, print convergence messages. Returns: None """ # process and activate input settings if 'sigint_callback' in kwds: self.sigint_callback = kwds['sigint_callback'] del kwds['sigint_callback'] else: self.sigint_callback = None settings = self._process_inputs(kwds) # set up signal handler #FIXME: sigint doesn't behave well in parallel self._EARLYEXIT = False #XXX: why not use EARLYEXIT singleton? # activate signal handler #import threading as thread #mainthread = isinstance(thread.current_thread(), thread._MainThread) #if mainthread: #XXX: if not mainthread, signal will raise ValueError import mystic._signal as signal if self._handle_sigint: signal.signal(signal.SIGINT, signal.Handler(self)) # register: cost, termination, ExtraArgs cost = self._bootstrap_objective(cost, ExtraArgs) if termination is not None: self.SetTermination(termination) #XXX: self.Step(cost, termination, ExtraArgs, **settings) ? # run the optimizer to termination self._Solve(cost, ExtraArgs, **settings) # restore default handler for signal interrupts if self._handle_sigint: signal.signal(signal.SIGINT, signal.default_int_handler) return
def _(params): import mystic._signal as signal print("plotting params: %s" % params) # because of the log ordinate axis, will draw errorbars the dumb way plt.semilogy(data[:, 0], data[:, 1], 'k.') for i, j in data: plt.semilogy([i, i], [j - sqrt(j), j + sqrt(j)], 'k-') plt.grid() plt.xlabel('Time (s)') plt.ylabel('Number of counts') x = arange(15, 900, 5) f = F(params) plt.plot(x, f(x), linestyle) if solver is not None: signal.signal(signal.SIGINT, signal.Hander(solver))
def _(params): import mystic._signal as signal print("plotting params: %s" % params) # because of the log ordinate axis, will draw errorbars the dumb way plt.semilogy(data[:,0],data[:,1],'k.') for i, j in data: plt.semilogy([i, i], [j-sqrt(j), j+sqrt(j)],'k-') plt.grid() plt.xlabel('Time (s)') plt.ylabel('Number of counts') x = arange(15, 900, 5) f = F(params) plt.plot(x, f(x), linestyle) if solver is not None: signal.signal(signal.SIGINT, signal.Hander(solver))
def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): """Minimize a 'cost' function with given termination conditions. Uses an optimization algorithm to find the minimum of a function of one or more variables. Args: cost (func, default=None): the function to be minimized: ``y = cost(x)``. termination (termination, default=None): termination conditions. ExtraArgs (tuple, default=None): extra arguments for cost. sigint_callback (func, default=None): callback function for signal handler. callback (func, default=None): function to call after each iteration. The interface is ``callback(xk)``, with xk the current parameter vector. disp (bool, default=False): if True, print convergence messages. Returns: None """ # process and activate input settings if 'sigint_callback' in kwds: self.sigint_callback = kwds['sigint_callback'] del kwds['sigint_callback'] else: self.sigint_callback = None settings = self._process_inputs(kwds) disp = settings['disp'] if 'disp' in settings else False # set up signal handler self._EARLYEXIT = False #XXX: why not use EARLYEXIT singleton? # activate signal handler #import threading as thread #mainthread = isinstance(thread.current_thread(), thread._MainThread) #if mainthread: #XXX: if not mainthread, signal will raise ValueError import mystic._signal as signal if self._handle_sigint: signal.signal(signal.SIGINT, signal.Handler(self)) # register: cost, termination, ExtraArgs cost = self._bootstrap_objective(cost, ExtraArgs) if termination is not None: self.SetTermination(termination) #XXX: self.Step(cost, termination, ExtraArgs, **settings) ? # the main optimization loop stop = False while not stop: stop = self.Step(**settings) #XXX: remove need to pass settings? continue # if collapse, then activate any relevant collapses and continue self.__stop__ = stop #HACK: avoid re-evaluation of Termination while self._collapse and self.Collapse(disp=disp): del self.__stop__ #HACK stop = False while not stop: stop = self.Step(** settings) #XXX: move Collapse inside of Step? continue self.__stop__ = stop #HACK del self.__stop__ #HACK # restore default handler for signal interrupts if self._handle_sigint: signal.signal(signal.SIGINT, signal.default_int_handler) return
def Solve(self, cost, termination=None, ExtraArgs=(), **kwds): """Minimize a 'cost' function with given termination conditions. Description: Uses an ensemble of optimizers to find the minimum of a function of one or more variables. Inputs: cost -- the Python function or method to be minimized. Additional Inputs: termination -- callable object providing termination conditions. ExtraArgs -- extra arguments for cost. Further Inputs: sigint_callback -- callback function for signal handler. callback -- an optional user-supplied function to call after each iteration. It is called as callback(xk), where xk is the current parameter vector. [default = None] disp -- non-zero to print convergence messages. [default = 0] """ # process and activate input settings if 'sigint_callback' in kwds: self.sigint_callback = kwds['sigint_callback'] del kwds['sigint_callback'] else: self.sigint_callback = None settings = self._process_inputs(kwds) disp = settings['disp'] if 'disp' in settings else False echo = settings['callback'] if 'callback' in settings else None # for key in settings: # exec "%s = settings['%s']" % (key,key) if disp in ['verbose', 'all']: verbose = True else: verbose = False #------------------------------------------------------------- from mystic.python_map import python_map if self._map != python_map: #FIXME: EvaluationMonitor fails for MPI, throws error for 'pp' from mystic.monitors import Null evalmon = Null() else: evalmon = self._evalmon fcalls, cost = wrap_function(cost, ExtraArgs, evalmon) # set up signal handler #self._EARLYEXIT = False # activate signal_handler #import threading as thread #mainthread = isinstance(thread.current_thread(), thread._MainThread) #if mainthread: #XXX: if not mainthread, signal will raise ValueError import mystic._signal as signal if self._handle_sigint: signal.signal(signal.SIGINT, signal.Handler(self)) # register termination function if termination is not None: self.SetTermination(termination) # get the nested solver instance solver = self._AbstractEnsembleSolver__get_solver_instance() #------------------------------------------------------------- # generate starting points initial_values = self._InitialPoints() # run optimizer for each grid point from copy import deepcopy as _copy op = [_copy(solver) for i in range(len(initial_values))] #cf = [cost for i in range(len(initial_values))] vb = [verbose for i in range(len(initial_values))] cb = [echo for i in range(len(initial_values))] #XXX: remove? at = self.id if self.id else 0 # start at self.id id = range(at,at+len(initial_values)) # generate the local_optimize function def local_optimize(solver, x0, rank=None, disp=False, callback=None): from copy import deepcopy as _copy from mystic.tools import isNull solver.id = rank solver.SetInitialPoints(x0) if solver._useStrictRange: #XXX: always, settable, or sync'd ? solver.SetStrictRanges(min=solver._strictMin, \ max=solver._strictMax) # or lower,upper ? solver.Solve(cost, disp=disp, callback=callback) sm = solver._stepmon em = solver._evalmon if isNull(sm): sm = ([],[],[],[]) else: sm = (_copy(sm._x),_copy(sm._y),_copy(sm._id),_copy(sm._info)) if isNull(em): em = ([],[],[],[]) else: em = (_copy(em._x),_copy(em._y),_copy(em._id),_copy(em._info)) return solver, sm, em # map:: solver = local_optimize(solver, x0, id, verbose) results = list(self._map(local_optimize, op, initial_values, id, \ vb, cb, **self._mapconfig)) # save initial state self._AbstractSolver__save_state() #XXX: HACK TO GET CONTENT OF ALL MONITORS # reconnect monitors; save all solvers from mystic.monitors import Monitor while results: #XXX: option to not save allSolvers? skip this and _copy _solver, _stepmon, _evalmon = results.pop() sm = Monitor() sm._x,sm._y,sm._id,sm._info = _stepmon _solver._stepmon.extend(sm) del sm em = Monitor() em._x,em._y,em._id,em._info = _evalmon _solver._evalmon.extend(em) del em self._allSolvers[len(results)] = _solver del results, _solver, _stepmon, _evalmon #XXX: END HACK # get the results with the lowest energy self._bestSolver = self._allSolvers[0] bestpath = self._bestSolver._stepmon besteval = self._bestSolver._evalmon self._total_evals = self._bestSolver.evaluations for solver in self._allSolvers[1:]: self._total_evals += solver.evaluations # add func evals if solver.bestEnergy < self._bestSolver.bestEnergy: self._bestSolver = solver bestpath = solver._stepmon besteval = solver._evalmon # return results to internals self.population = self._bestSolver.population #XXX: pointer? copy? self.popEnergy = self._bestSolver.popEnergy #XXX: pointer? copy? self.bestSolution = self._bestSolver.bestSolution #XXX: pointer? copy? self.bestEnergy = self._bestSolver.bestEnergy self.trialSolution = self._bestSolver.trialSolution #XXX: pointer? copy? self._fcalls = self._bestSolver._fcalls #XXX: pointer? copy? self._maxiter = self._bestSolver._maxiter self._maxfun = self._bestSolver._maxfun # write 'bests' to monitors #XXX: non-best monitors may be useful too self._stepmon = bestpath #XXX: pointer? copy? self._evalmon = besteval #XXX: pointer? copy? self.energy_history = None self.solution_history = None #from mystic.tools import isNull #if isNull(bestpath): # self._stepmon = bestpath #else: # for i in range(len(bestpath.y)): # self._stepmon(bestpath.x[i], bestpath.y[i], self.id) # #XXX: could apply callback here, or in exec'd code #if isNull(besteval): # self._evalmon = besteval #else: # for i in range(len(besteval.y)): # self._evalmon(besteval.x[i], besteval.y[i]) #------------------------------------------------------------- # restore default handler for signal interrupts if self._handle_sigint: signal.signal(signal.SIGINT, signal.default_int_handler) # log any termination messages msg = self.Terminated(disp=disp, info=True) if msg: self._stepmon.info('STOP("%s")' % msg) # save final state self._AbstractSolver__save_state(force=True) return
def Solve(self, cost, termination=None, ExtraArgs=(), **kwds): """Minimize a 'cost' function with given termination conditions. Uses an ensemble of optimizers to find the minimum of a function of one or more variables. Args: cost (func, default=None): the function to be minimized: ``y = cost(x)``. termination (termination, default=None): termination conditions. ExtraArgs (tuple, default=None): extra arguments for cost. sigint_callback (func, default=None): callback function for signal handler. callback (func, default=None): function to call after each iteration. The interface is ``callback(xk)``, with xk the current parameter vector. disp (bool, default=False): if True, print convergence messages. Returns: None """ # process and activate input settings if 'sigint_callback' in kwds: self.sigint_callback = kwds['sigint_callback'] del kwds['sigint_callback'] else: self.sigint_callback = None settings = self._process_inputs(kwds) disp = settings['disp'] if 'disp' in settings else False echo = settings['callback'] if 'callback' in settings else None # for key in settings: # exec "%s = settings['%s']" % (key,key) if disp in ['verbose', 'all']: verbose = True else: verbose = False #------------------------------------------------------------- from mystic.python_map import python_map if self._map != python_map: #FIXME: EvaluationMonitor fails for MPI, throws error for 'pp' from mystic.monitors import Null evalmon = Null() else: evalmon = self._evalmon fcalls, cost = wrap_function(cost, ExtraArgs, evalmon) # set up signal handler #self._EARLYEXIT = False # activate signal_handler #import threading as thread #mainthread = isinstance(thread.current_thread(), thread._MainThread) #if mainthread: #XXX: if not mainthread, signal will raise ValueError import mystic._signal as signal if self._handle_sigint: signal.signal(signal.SIGINT, signal.Handler(self)) # register termination function if termination is not None: self.SetTermination(termination) # get the nested solver instance solver = self._AbstractEnsembleSolver__get_solver_instance() #------------------------------------------------------------- # generate starting points initial_values = self._InitialPoints() # run optimizer for each grid point from copy import deepcopy as _copy op = [_copy(solver) for i in range(len(initial_values))] #cf = [cost for i in range(len(initial_values))] vb = [verbose for i in range(len(initial_values))] cb = [echo for i in range(len(initial_values))] #XXX: remove? at = self.id if self.id else 0 # start at self.id id = range(at, at + len(initial_values)) # generate the local_optimize function def local_optimize(solver, x0, rank=None, disp=False, callback=None): from copy import deepcopy as _copy from mystic.tools import isNull solver.id = rank solver.SetInitialPoints(x0) if solver._useStrictRange: #XXX: always, settable, or sync'd ? solver.SetStrictRanges(min=solver._strictMin, \ max=solver._strictMax) # or lower,upper ? solver.Solve(cost, disp=disp, callback=callback) sm = solver._stepmon em = solver._evalmon if isNull(sm): sm = ([], [], [], []) else: sm = (_copy(sm._x), _copy(sm._y), _copy(sm._id), _copy(sm._info)) if isNull(em): em = ([], [], [], []) else: em = (_copy(em._x), _copy(em._y), _copy(em._id), _copy(em._info)) return solver, sm, em # map:: solver = local_optimize(solver, x0, id, verbose) results = list(self._map(local_optimize, op, initial_values, id, \ vb, cb, **self._mapconfig)) # save initial state self._AbstractSolver__save_state() #XXX: HACK TO GET CONTENT OF ALL MONITORS # reconnect monitors; save all solvers from mystic.monitors import Monitor while results: #XXX: option to not save allSolvers? skip this and _copy _solver, _stepmon, _evalmon = results.pop() sm = Monitor() sm._x, sm._y, sm._id, sm._info = _stepmon _solver._stepmon.extend(sm) del sm em = Monitor() em._x, em._y, em._id, em._info = _evalmon _solver._evalmon.extend(em) del em self._allSolvers[len(results)] = _solver del results, _solver, _stepmon, _evalmon #XXX: END HACK # get the results with the lowest energy self._bestSolver = self._allSolvers[0] bestpath = self._bestSolver._stepmon besteval = self._bestSolver._evalmon self._total_evals = self._bestSolver.evaluations for solver in self._allSolvers[1:]: self._total_evals += solver.evaluations # add func evals if solver.bestEnergy < self._bestSolver.bestEnergy: self._bestSolver = solver bestpath = solver._stepmon besteval = solver._evalmon # return results to internals self.population = self._bestSolver.population #XXX: pointer? copy? self.popEnergy = self._bestSolver.popEnergy #XXX: pointer? copy? self.bestSolution = self._bestSolver.bestSolution #XXX: pointer? copy? self.bestEnergy = self._bestSolver.bestEnergy self.trialSolution = self._bestSolver.trialSolution #XXX: pointer? copy? self._fcalls = self._bestSolver._fcalls #XXX: pointer? copy? self._maxiter = self._bestSolver._maxiter self._maxfun = self._bestSolver._maxfun # write 'bests' to monitors #XXX: non-best monitors may be useful too self._stepmon = bestpath #XXX: pointer? copy? self._evalmon = besteval #XXX: pointer? copy? self.energy_history = None self.solution_history = None #from mystic.tools import isNull #if isNull(bestpath): # self._stepmon = bestpath #else: # for i in range(len(bestpath.y)): # self._stepmon(bestpath.x[i], bestpath.y[i], self.id) # #XXX: could apply callback here, or in exec'd code #if isNull(besteval): # self._evalmon = besteval #else: # for i in range(len(besteval.y)): # self._evalmon(besteval.x[i], besteval.y[i]) #------------------------------------------------------------- # restore default handler for signal interrupts if self._handle_sigint: signal.signal(signal.SIGINT, signal.default_int_handler) # log any termination messages msg = self.Terminated(disp=disp, info=True) if msg: self._stepmon.info('STOP("%s")' % msg) # save final state self._AbstractSolver__save_state(force=True) return
def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): """Minimize a 'cost' function with given termination conditions. Description: Uses an optimization algorithm to find the minimum of a function of one or more variables. Inputs: cost -- the Python function or method to be minimized. Additional Inputs: termination -- callable object providing termination conditions. ExtraArgs -- extra arguments for cost. Further Inputs: sigint_callback -- callback function for signal handler. callback -- an optional user-supplied function to call after each iteration. It is called as callback(xk), where xk is the current parameter vector. [default = None] disp -- non-zero to print convergence messages. """ # process and activate input settings if 'sigint_callback' in kwds: self.sigint_callback = kwds['sigint_callback'] del kwds['sigint_callback'] else: self.sigint_callback = None settings = self._process_inputs(kwds) disp = settings['disp'] if 'disp' in settings else False # set up signal handler self._EARLYEXIT = False #XXX: why not use EARLYEXIT singleton? # activate signal handler #import threading as thread #mainthread = isinstance(thread.current_thread(), thread._MainThread) #if mainthread: #XXX: if not mainthread, signal will raise ValueError import mystic._signal as signal if self._handle_sigint: signal.signal(signal.SIGINT, signal.Handler(self)) # register: cost, termination, ExtraArgs cost = self._bootstrap_objective(cost, ExtraArgs) if termination is not None: self.SetTermination(termination) #XXX: self.Step(cost, termination, ExtraArgs, **settings) ? # the main optimization loop stop = False while not stop: stop = self.Step(**settings) #XXX: remove need to pass settings? continue # if collapse, then activate any relevant collapses and continue self.__stop__ = stop #HACK: avoid re-evaluation of Termination while self._collapse and self.Collapse(disp=disp): del self.__stop__ #HACK stop = False while not stop: stop = self.Step(**settings) #XXX: move Collapse inside of Step? continue self.__stop__ = stop #HACK del self.__stop__ #HACK # restore default handler for signal interrupts if self._handle_sigint: signal.signal(signal.SIGINT, signal.default_int_handler) return
def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): """Minimize a 'cost' function with given termination conditions. Uses an optimization algorithm to find the minimum of a function of one or more variables. Args: cost (func, default=None): the function to be minimized: ``y = cost(x)``. termination (termination, default=None): termination conditions. ExtraArgs (tuple, default=None): extra arguments for cost. sigint_callback (func, default=None): callback function for signal handler. callback (func, default=None): function to call after each iteration. The interface is ``callback(xk)``, with xk the current parameter vector. disp (bool, default=False): if True, print convergence messages. Returns: None """ # process and activate input settings if 'sigint_callback' in kwds: self.sigint_callback = kwds['sigint_callback'] del kwds['sigint_callback'] else: self.sigint_callback = None settings = self._process_inputs(kwds) disp = settings['disp'] if 'disp' in settings else False # set up signal handler self._EARLYEXIT = False #XXX: why not use EARLYEXIT singleton? # activate signal handler #import threading as thread #mainthread = isinstance(thread.current_thread(), thread._MainThread) #if mainthread: #XXX: if not mainthread, signal will raise ValueError import mystic._signal as signal if self._handle_sigint: signal.signal(signal.SIGINT, signal.Handler(self)) # register: cost, termination, ExtraArgs cost = self._bootstrap_objective(cost, ExtraArgs) if termination is not None: self.SetTermination(termination) #XXX: self.Step(cost, termination, ExtraArgs, **settings) ? # the main optimization loop stop = False while not stop: stop = self.Step(**settings) #XXX: remove need to pass settings? continue # if collapse, then activate any relevant collapses and continue self.__stop__ = stop #HACK: avoid re-evaluation of Termination while self._collapse and self.Collapse(disp=disp): del self.__stop__ #HACK stop = False while not stop: stop = self.Step(**settings) #XXX: move Collapse inside of Step? continue self.__stop__ = stop #HACK del self.__stop__ #HACK # restore default handler for signal interrupts if self._handle_sigint: signal.signal(signal.SIGINT, signal.default_int_handler) return