def _masked_collapse(termination): """reformat the termination mask as a collapse dict termination is a termination condition """ from mystic.termination import state return dict((i,j['mask']) for (i,j) in state(termination).items() if i.startswith('Collapse') and j['mask']) #XXX: pop value if (-inf,inf)?
def __collapse_termination(self, collapses): """get (initial state, resulting termination) for the give collapses""" import mystic.termination as mt import mystic.mask as ma state = mt.state(self._termination) termination = ma.update_mask(self._termination, collapses) return state, termination
def no_mask(termination): """build dict of termination info {with mask: without mask} termination is a termination condition (with collapse) """ from mystic.termination import state return dict((k,''.join([k.split('{',1)[0],str(v)])) for (k,v) in state(termination).items() if v.pop('mask',None) or k.startswith('Collapse'))
def get_mask(condition): #FIXME: gets None if is None *and* if no mask '''get mask from termination condition''' condition = _term.state(condition) if not condition: return None if isinstance(condition, tuple): return None condition = condition.popitem()[-1] return condition['mask'] if 'mask' in condition else None
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 SetTermination(self, termination): # disp ? """set the termination conditions""" #XXX: validate that termination is a 'condition' ? self._termination = termination self._collapse = False if termination is not None: from mystic.termination import state self._collapse = any(key.startswith('Collapse') for key in state(termination).iterkeys()) return
def SetTermination(self, termination): # disp ? """set the termination conditions input:: - termination = termination conditions to check against""" #XXX: validate that termination is a 'condition' ? self._termination = termination self._collapse = False if termination is not None: from mystic.termination import state stop = state(termination) stop = getattr(stop, 'iterkeys', stop.keys)() self._collapse = any(key.startswith('Collapse') for key in stop) return
def Collapse(self, disp=False): """if solver has terminated by collapse, apply the collapse""" collapses = self.Collapsed(disp=disp, info=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')] 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)) conditions.extend(_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) #print(mt.state(self._termination).keys()) return collapses
def _extend_mask(condition, mask): '''extend the mask in the termination condition with the given mask''' if mask is None: return condition kwds = _term.state(condition).popitem()[-1] # short-circiut: if no kwds['mask'], then abort if not kwds.has_key('mask'): return _term.type(condition)(**kwds) # extend current mask _mask = kwds['mask'] if not _mask: # mask is None, {}, set(), or () kwds['mask'] = mask elif type(_mask) is set: # assumes mask is set kwds['mask'].update(mask) elif type(_mask) is dict: # assumes mask is dict for k,v in mask.iteritems(): _mask.setdefault(k,v).update(v) kwds['mask'] = _mask else: # assumes mask is tuple (or list) kwds['mask'] = type(_mask)(_mask[i]+mask[i] for i in range(2)) return _term.type(condition)(**kwds)
def Collapse(self, disp=False): """if solver has terminated by collapse, apply the collapse""" collapses = self.Collapsed(disp=disp, info=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')] 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)) conditions.extend(_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) #print(mt.state(self._termination).keys()) return collapses
def _extend_mask(condition, mask): '''extend the mask in the termination condition with the given mask''' if mask is None: return condition kwds = _term.state(condition).popitem()[-1] # short-circiut: if no kwds['mask'], then abort if 'mask' not in kwds: return _term.type(condition)(**kwds) # extend current mask _mask = kwds['mask'] if not _mask: # mask is None, {}, set(), or () kwds['mask'] = mask elif type(_mask) is set: # assumes mask is set kwds['mask'].update(mask) elif type(_mask) is dict: # assumes mask is dict try: #XXX: better way to distinguish bounds collapse from not? for k,v in getattr(mask, 'iteritems', mask.items)(): _mask.setdefault(k,v).update(v) kwds['mask'] = _mask except AttributeError: # are list/tuple not set/dict, _mask is included kwds['mask'] = mask else: # assumes mask is tuple (or list) kwds['mask'] = type(_mask)(_mask[i]+mask[i] for i in range(2)) return _term.type(condition)(**kwds)
def _extend_mask(condition, mask): '''extend the mask in the termination condition with the given mask''' if mask is None: return condition kwds = _term.state(condition).popitem()[-1] # short-circiut: if no kwds['mask'], then abort if 'mask' not in kwds: return _term.type(condition)(**kwds) # extend current mask _mask = kwds['mask'] if not _mask: # mask is None, {}, set(), or () kwds['mask'] = mask elif type(_mask) is set: # assumes mask is set kwds['mask'].update(mask) elif type(_mask) is dict: # assumes mask is dict try: #XXX: better way to distinguish bounds collapse from not? for k, v in getattr(mask, 'iteritems', mask.items)(): _mask.setdefault(k, v).update(v) kwds['mask'] = _mask except AttributeError: # are list/tuple not set/dict, _mask is included kwds['mask'] = mask else: # assumes mask is tuple (or list) kwds['mask'] = type(_mask)(_mask[i] + mask[i] for i in range(2)) return _term.type(condition)(**kwds)
_term = ma.update_mask(term, collapse) assert mask == ma.get_mask(_term) assert not ct.collapsed(_term(solver, True)) for term in (mt.CollapseAt, mt.CollapseAs): test_cc(term) for term in (mt.CollapseWeight, mt.CollapsePosition): for seed in (None, {}, set(), ()): test_cc(term, seed) ############################################## stop = mt.Or((mt.ChangeOverGeneration(), \ mt.CollapseWeight(), \ mt.CollapsePosition())) message = stop(solver, True) _stop = ma.update_mask(stop, ct.collapsed(message)) message = _stop(solver, True) assert message collapse = ct.collapsed(message) assert not collapse stop_ = ma.update_mask(_stop, collapse) assert mt.state(stop_) == mt.state(_stop) # EOF
# update mask with full results _term = ma.update_mask(term, collapse) assert mask == ma.get_mask(_term) assert not ct.collapsed(_term(solver, True)) for term in (mt.CollapseAt, mt.CollapseAs): test_cc(term) for term in (mt.CollapseWeight, mt.CollapsePosition): for seed in (None, {}, set(), ()): test_cc(term, seed) ############################################## stop = mt.Or((mt.ChangeOverGeneration(), \ mt.CollapseWeight(), \ mt.CollapsePosition())) message = stop(solver, True) _stop = ma.update_mask(stop, ct.collapsed(message)) message = _stop(solver, True) assert message collapse = ct.collapsed(message) assert not collapse stop_ = ma.update_mask(_stop, collapse) assert mt.state(stop_) == mt.state(_stop) # EOF
def _replace_mask(condition, mask): '''replace the mask in the termination condition with the given mask''' kwds = _term.state(condition).popitem()[-1] if 'mask' in kwds: kwds['mask'] = mask return _term.type(condition)(**kwds)
def _replace_mask(condition, mask): '''replace the mask in the termination condition with the given mask''' kwds = _term.state(condition).popitem()[-1] if kwds.has_key('mask'): kwds['mask'] = mask return _term.type(condition)(**kwds)