Пример #1
0
 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
Пример #2
0
    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
Пример #3
0
    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
Пример #4
0
def test_cc(termination, seed=None):
    term = termination(mask=seed)
    collapse = ct.collapsed(term(solver, True))
    mask = list(collapse.values())[-1]
    # full results as mask
    _term = termination(mask=mask)
    assert not ct.collapsed(_term(solver, True))
    # update mask with full results
    _term = ma.update_mask(term, collapse)
    assert mask == ma.get_mask(_term)
    assert not ct.collapsed(_term(solver, True))
Пример #5
0
def test_cc(termination, seed=None):
    term = termination(mask=seed)
    collapse = ct.collapsed(term(solver, True))
    mask = collapse.values()[-1]
    # full results as mask
    _term = termination(mask=mask)
    assert not ct.collapsed(_term(solver, True))
    # update mask with full results
    _term = ma.update_mask(term, collapse)
    assert mask == ma.get_mask(_term)
    assert not ct.collapsed(_term(solver, True))
Пример #6
0
    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
Пример #7
0
    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
Пример #8
0
    # 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
Пример #9
0
    _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