Ejemplo n.º 1
0
    def solve_all(self, f1, trace):
        """ Returns an upperset in UR. You want to project
            it to R1 to use as the output. """
        dp0 = self.dp1
        R = dp0.get_res_space()
        R1 = R[0]
        UR = UpperSets(R)

        # we consider a set of iterates
        # we start from the bottom
        trace.log('Iterating in UR = %s' % UR.__str__())

        s0 = R.Us(R.get_minimal_elements())
        S = [
            KleeneIteration(s=s0,
                            s_converged=R.Us(set()),
                            r=upperset_project(s0, 0),
                            r_converged=R1.Us(set()))
        ]

        for i in range(1, 1000000):  # XXX
            with trace.iteration(i) as t:
                si_prev = S[-1].s
                si_next, converged = solve_f_iterate(dp0, f1, R, si_prev, t)
                iteration = KleeneIteration(s=si_next,
                                            s_converged=converged,
                                            r=upperset_project(si_next, 0),
                                            r_converged=upperset_project(
                                                converged, 0))
                S.append(iteration)

                t.log('R = %s' % UR.format(si_next))

                if do_extra_checks():
                    try:
                        UR.check_leq(si_prev, si_next)
                    except NotLeq as e:
                        msg = 'Loop iteration invariant not satisfied.'
                        raise_wrapped(Exception,
                                      e,
                                      msg,
                                      si_prev=si_prev,
                                      si_next=si_next,
                                      dp=self.dp1)

                t.values(state=S[-1])

                if UR.leq(si_next, si_prev):
                    t.log('Breaking because converged (iteration %s) ' % i)
                    #t.log(' solution is %s' % (UR.format(sip)))
                    # todo: add reason why interrupted
                    break

        trace.values(type='loop2', UR=UR, R=R, dp=self, iterations=S)

        res_all = S[-1].s
        res_r1 = upperset_project(res_all, 0)
        result = dict(res_all=res_all, res_r1=res_r1)

        return result
Ejemplo n.º 2
0
    def solve_all(self, f1, trace):
        """ Returns an upperset in UR. You want to project
            it to R1 to use as the output. """
        dp0 = self.dp1
        R = dp0.get_res_space()
        R1 = R[0]
        UR = UpperSets(R)

        # we consider a set of iterates
        # we start from the bottom
        trace.log('Iterating in UR = %s' % UR.__str__())
        
        s0 = R.Us(R.get_minimal_elements()) 
        S = [KleeneIteration(s=s0, s_converged=R.Us(set()),
                                r=upperset_project(s0, 0),
                                r_converged=R1.Us(set()))]
            
        for i in range(1, 1000000):  # XXX
            with trace.iteration(i) as t:
                si_prev = S[-1].s
                si_next, converged = solve_f_iterate(dp0, f1, R, si_prev, t)
                iteration = KleeneIteration(s=si_next, 
                                            s_converged=converged,
                                            r=upperset_project(si_next, 0),
                                            r_converged=upperset_project(converged, 0))
                S.append(iteration)
                
                t.log('R = %s' % UR.format(si_next))

                if do_extra_checks():
                    try:
                        UR.check_leq(si_prev, si_next)
                    except NotLeq as e:
                        msg = 'Loop iteration invariant not satisfied.'
                        raise_wrapped(Exception, e, msg, si_prev=si_prev, 
                                      si_next=si_next, dp=self.dp1)
                
                t.values(state=S[-1])

                if UR.leq(si_next, si_prev):
                    t.log('Breaking because converged (iteration %s) ' % i)
                    #t.log(' solution is %s' % (UR.format(sip)))
                    # todo: add reason why interrupted
                    break

        trace.values(type='loop2', UR=UR, R=R, dp=self, iterations=S)
        
        res_all = S[-1].s
        res_r1 = upperset_project(res_all, 0)
        result = dict(res_all=res_all, res_r1=res_r1)
        
        return result
Ejemplo n.º 3
0
def less_resources2(ua, ub):
    """
    
        ua must be <= ub
    """
    Pa = ua.P
    Pb = ub.P

    if not isinstance(Pa, PosetProduct) or not isinstance(Pb, PosetProduct):
        raise NotImplementedError((Pa, Pb))

    tu = get_types_universe()

    matches = []
    for i, P in enumerate(Pa.subs):
        for j, Q in enumerate(Pb.subs):
            if j in matches: continue
            if tu.leq(P, Q):
                matches.append(j)
                break
        else:
            # msg = 'Could not find match.'
            return False

    # now we have found an embedding

    # first we create a projection for Pb
    m1 = MuxMap(F=Pb, coords=matches)

    ub2 = upperset_project_map(ub, m1)
    Pb2 = ub2.P
    UPb2 = UpperSets(Pb2)

    # now we create the embedding
    A_to_B, _ = tu.get_embedding(Pa, Pb2)
    ua2 = upperset_project_map(ua, A_to_B)

    print('Pa: %s' % Pa)
    print('Pb2:  %s' % Pb2)
    print('ua2: %s' % ua2)
    print('ub2: %s' % ub2)

    return UPb2.leq(ua2, ub2)