def Collapse(self, disp=False): """if solver has terminated by collapse, apply the collapse (unless both collapse and "stop" are simultaneously satisfied) """ #XXX: return True for "collapse and continue" and False otherwise? collapses = self.Collapsed(disp=disp, info=True) if collapses: # stop if any Termination is not from Collapse stop = getattr(self, '__stop__', self.Terminated(info=True)) stop = not all(k.startswith("Collapse") for k in stop.split("; ")) if stop: return {} #XXX: self._collapse = False ? else: stop = True if collapses: # then stomach a bunch of module imports (yuck) import mystic.tools as to import mystic.termination as mt import mystic.constraints as cn import mystic.mask as ma # get collapse conditions #XXX: efficient? 4x loops over collapses state = mt.state(self._termination) npts = getattr(self._stepmon, '_npts', None) #XXX: default? #conditions = [cn.impose_at(*to.select_params(self,collapses[k])) if state[k].get('target') is None else cn.impose_at(collapses[k],state[k].get('target')) for k in collapses if k.startswith('CollapseAt')] #conditions += [cn.impose_as(collapses[k],state[k].get('offset')) for k in collapses if k.startswith('CollapseAs')] #randomize = False conditions = []; _conditions = []; conditions_ = [] for k in collapses: #FIXME: these should be encapsulted in termination instance if k.startswith('CollapseAt'): t = state[k] t = t['target'] if 'target' in t else None if t is None: t = cn.impose_at(*to.select_params(self,collapses[k])) else: t = cn.impose_at(collapses[k],t) conditions.append(t) elif k.startswith('CollapseAs'): t = state[k] t = t['offset'] if 'offset' in t else None _conditions.append(cn.impose_as(collapses[k],t)) elif k.startswith(('CollapseCost','CollapseGrad')): t = state[k] t = t['clip'] if 'clip' in t else True conditions_.append(cn.impose_bounds(collapses[k],clip=t)) #randomize = True conditions.extend(_conditions) conditions.extend(conditions_) del _conditions; del conditions_ # get measure collapse conditions if npts: #XXX: faster/better if comes first or last? conditions += [cn.impose_measure( npts, [collapses[k] for k in collapses if k.startswith('CollapsePosition')], [collapses[k] for k in collapses if k.startswith('CollapseWeight')] )] # update termination and constraints in solver constraints = to.chain(*conditions)(self._constraints) termination = ma.update_mask(self._termination, collapses) self.SetConstraints(constraints) self.SetTermination(termination) #if randomize: self.SetInitialPoints(self.population[0]) #print(mt.state(self._termination).keys()) #return bool(collapses) and not stop return collapses
def Collapse(self, disp=False): """if solver has terminated by collapse, apply the collapse (unless both collapse and "stop" are simultaneously satisfied) """ #XXX: return True for "collapse and continue" and False otherwise? collapses = self.Collapsed(disp=disp, info=True) if collapses: # stop if any Termination is not from Collapse stop = getattr(self, '__stop__', self.Terminated(info=True)) stop = not all(k.startswith("Collapse") for k in stop.split("; ")) if stop: return {} #XXX: self._collapse = False ? else: stop = True if collapses: # then stomach a bunch of module imports (yuck) import mystic.tools as to import mystic.termination as mt import mystic.constraints as cn import mystic.mask as ma # get collapse conditions #XXX: efficient? 4x loops over collapses state = mt.state(self._termination) npts = getattr(self._stepmon, '_npts', None) #XXX: default? #conditions = [cn.impose_at(*to.select_params(self,collapses[k])) if state[k].get('target') is None else cn.impose_at(collapses[k],state[k].get('target')) for k in collapses if k.startswith('CollapseAt')] #conditions += [cn.impose_as(collapses[k],state[k].get('offset')) for k in collapses if k.startswith('CollapseAs')] #randomize = False conditions = []; _conditions = []; conditions_ = [] for k in collapses: if k.startswith('CollapseAt'): t = state[k] t = t['target'] if 'target' in t else None if t is None: t = cn.impose_at(*to.select_params(self,collapses[k])) else: t = cn.impose_at(collapses[k],t) conditions.append(t) elif k.startswith('CollapseAs'): t = state[k] t = t['offset'] if 'offset' in t else None _conditions.append(cn.impose_as(collapses[k],t)) elif k.startswith('CollapseCost'): t = state[k] t = t['clip'] if 'clip' in t else True conditions_.append(cn.impose_bounds(collapses[k],clip=t)) #randomize = True conditions.extend(_conditions) conditions.extend(conditions_) del _conditions; del conditions_ # get measure collapse conditions if npts: #XXX: faster/better if comes first or last? conditions += [cn.impose_measure( npts, [collapses[k] for k in collapses if k.startswith('CollapsePosition')], [collapses[k] for k in collapses if k.startswith('CollapseWeight')] )] # update termination and constraints in solver constraints = to.chain(*conditions)(self._constraints) termination = ma.update_mask(self._termination, collapses) self.SetConstraints(constraints) self.SetTermination(termination) #if randomize: self.SetInitialPoints(self.population[0]) #print(mt.state(self._termination).keys()) #return bool(collapses) and not stop return collapses
def __collapse_constraints(self, state, collapses): """get updated constraints for the given state and collapses""" import mystic.tools as to import mystic.constraints as cn # get collapse conditions #XXX: efficient? 4x loops over collapses npts = getattr(self._stepmon, '_npts', None) #XXX: default? #conditions = [cn.impose_at(*to.select_params(self,collapses[k])) if state[k].get('target') is None else cn.impose_at(collapses[k],state[k].get('target')) for k in collapses if k.startswith('CollapseAt')] #conditions += [cn.impose_as(collapses[k],state[k].get('offset')) for k in collapses if k.startswith('CollapseAs')] #randomize = False conditions = [] _conditions = [] conditions_ = [] for k in collapses: #FIXME: these should be encapsulted in termination instance if k.startswith('CollapseAt'): t = state[k] t = t['target'] if 'target' in t else None if t is None: t = cn.impose_at(*to.select_params(self, collapses[k])) else: t = cn.impose_at(collapses[k], t) conditions.append(t) elif k.startswith('CollapseAs'): t = state[k] t = t['offset'] if 'offset' in t else None _conditions.append(cn.impose_as(collapses[k], t)) elif k.startswith(('CollapseCost', 'CollapseGrad')): t = state[k] t = t['clip'] if 'clip' in t else True conditions_.append(cn.impose_bounds(collapses[k], clip=t)) #randomize = True conditions.extend(_conditions) conditions.extend(conditions_) del _conditions del conditions_ # get measure collapse conditions if npts: #XXX: faster/better if comes first or last? conditions += [ cn.impose_measure(npts, [ collapses[k] for k in collapses if k.startswith('CollapsePosition') ], [ collapses[k] for k in collapses if k.startswith('CollapseWeight') ]) ] # get updated constraints return to.chain(*conditions)(self._constraints)
shift = 0 scale = 0 step = 200 args = { #'smooth': 0, 'method': 'thin_plate', 'extrap': False, 'arrays': False, } #surface.doit(bounds, stop, step=step) #'multiquadric','inverse','gaussian','linear','cubic','quintic','thin_plate' # impose data filter (so X >= 0 and Y >= 0) from mystic.filters import generate_mask, generate_filter from mystic.constraints import impose_bounds _bounds = impose_bounds((0.0, None))(lambda x:x) filter = generate_filter(generate_mask(_bounds, _bounds)) # from numpy import round as _round ############# # get trajectories surface.Sample(bounds, stop, all=all, filter=filter)#, constraints=_round) print("TOOK: %s" % (time.time() - start)) # exit() # get interpolated function surface.Interpolate(**args) # check extrema #XXX: put _min,_max in Interpolate? (downsampled) f = lambda x,z: (z,surface.surrogate(*x)) print("min: {}; min@f: {}".format(*f(*surface._min()))) print("max: {}; max@f: {}".format(*f(*surface._max())))