Esempio 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
Esempio 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
Esempio n. 3
0
def get_new_lowerbounds(context, name, lower_bounds):
    connections = context.connections
    ndp = context.names[name]
    fnames = ndp.get_fnames()
    
    def get_lb_for_fname(fname):
        cf = CFunction(name, fname)
        is_connected, cresource = get_connection_to_function(connections, cf)
        if is_connected:
            lb = lower_bounds[cresource]
        else:
            F = context.get_ftype(cf)
            lb = F.Us(F.get_minimal_elements())

#         print('lb for %r: %s' % (fname, lb))
        return lb

    lbs = []
    for fname in fnames:
        lb = get_lb_for_fname(fname)
        lbs.append(lb)
    
    if len(fnames) == 1:
        lbF = lbs[0]
    else:
        lbF = upperset_product_multi(tuple(lbs))

    dp = ndp.get_dp()

    ur = dp.solveU(lbF)
#     print('Solving with %s -> %s ' % (lbF, ur))

    lower_bounds_new = {}
    rnames = ndp.get_rnames()
    if len(rnames) == 1:
        cr = CResource(name, rnames[0])
        lower_bounds_new[cr] = ur
    else:
        for i, rname in enumerate(rnames):
            uri = upperset_project(ur, i)
            cr = CResource(name, rname)
            lower_bounds_new[cr] = uri

    return lower_bounds_new
Esempio n. 4
0
def get_new_lowerbounds(context, name, lower_bounds):
    connections = context.connections
    ndp = context.names[name]
    fnames = ndp.get_fnames()

    def get_lb_for_fname(fname):
        cf = CFunction(name, fname)
        is_connected, cresource = get_connection_to_function(connections, cf)
        if is_connected:
            lb = lower_bounds[cresource]
        else:
            F = context.get_ftype(cf)
            lb = F.Us(F.get_minimal_elements())

        #         print('lb for %r: %s' % (fname, lb))
        return lb

    lbs = []
    for fname in fnames:
        lb = get_lb_for_fname(fname)
        lbs.append(lb)

    if len(fnames) == 1:
        lbF = lbs[0]
    else:
        lbF = upperset_product_multi(tuple(lbs))

    dp = ndp.get_dp()

    ur = dp.solveU(lbF)
    #     print('Solving with %s -> %s ' % (lbF, ur))

    lower_bounds_new = {}
    rnames = ndp.get_rnames()
    if len(rnames) == 1:
        cr = CResource(name, rnames[0])
        lower_bounds_new[cr] = ur
    else:
        for i, rname in enumerate(rnames):
            uri = upperset_project(ur, i)
            cr = CResource(name, rname)
            lower_bounds_new[cr] = uri

    return lower_bounds_new