示例#1
0
文件: evaluation.py 项目: rusi/mcdp
def check_evaluate(id_dp, dp):
    """ Test for PrimitiveDP:evaluate() """
    print('Testing %s: %s' % (id_dp, dp))
    F = dp.get_fun_space()
    R = dp.get_res_space()
    UR = UpperSets(R)
    LF = LowerSets(F)
    M = dp.get_imp_space()

    # We get a random m
    m0 = M.witness()

    try:
        lf, ur = dp.evaluate(m0)
    except NotSolvableNeedsApprox:
        return

    UR.belongs(ur)
    LF.belongs(lf)

    if not lf.maximals or not ur.minimals:
        msg = 'The point m0 gave empty lf, ur.'
        raise_desc(Exception,
                   msg,
                   lf=lf,
                   ur=ur,
                   m0=M.format(m0),
                   M=M,
                   dp=dp.repr_long())

    # take one possible feasible pair
    _f = list(lf.maximals)[0]
    _r = list(ur.minimals)[0]
示例#2
0
文件: solve_meat.py 项目: rusi/mcdp
def solve_meat_solve_rtof(trace, ndp, dp, r, intervals, max_steps, exp_advanced):
    F = dp.get_fun_space()
    LF = LowerSets(F)

    res = dp.solve_r(r)
    fnames = ndp.get_fnames()
    x = ", ".join(fnames)
    # todo: add better formatting
    trace.log('Maximal functionality possible: %s = %s' % (x, LF.format(res)))

    return res, trace
示例#3
0
def eval_constant_lowersetfromcollection(op, context):
    x = eval_constant(op.value, context)
    v = x.value
    u = x.unit
    S = u.S
    maximals = poset_minima(v.elements, S.leq)
    value = LowerSet(maximals, S)
    unit = LowerSets(S)
    if do_extra_checks():
        unit.belongs(value)
    vu = ValueWithUnits(value, unit)
    return vu
示例#4
0
def eval_constant_lowersetfromcollection(op, context):
    x = eval_constant(op.value, context)
    v = x.value
    u = x.unit
    S = u.S
    maximals = poset_minima(v.elements, S.leq)
    value = LowerSet(maximals, S)
    unit = LowerSets(S)
    if do_extra_checks():
        unit.belongs(value)
    vu = ValueWithUnits(value, unit)
    return vu
示例#5
0
def check_solve_r_chain(id_dp, dp):
    with primitive_dp_test(id_dp, dp):

        from mcdp_posets.utils import poset_check_chain

        R = dp.get_res_space()
        F = dp.get_fun_space()
        LF = LowerSets(F)

        r_chain = R.get_test_chain(n=8)
        poset_check_chain(R, r_chain)

        try:
            lfchain = map(dp.solve_r, r_chain)
        except NotSolvableNeedsApprox:
            return try_with_approximations(id_dp, dp, check_solve_r_chain)

        try:
            # now, notice that we need to reverse this
            lfchain_reversed = list(reversed(lfchain))
            poset_check_chain(LF, lfchain_reversed)
        except ValueError as e:
            msg = 'The map solve_r() for %r is not monotone.' % id_dp
            raise_wrapped(Exception,
                          e,
                          msg,
                          r_chain=r_chain,
                          lfchain=lfchain,
                          lfchain_reversed=lfchain_reversed,
                          compact=True)
示例#6
0
def eval_solve_r(op, context):
    check_isinstance(op, CDP.SolveRModel)
    from .eval_ndp_imp import eval_ndp

    ndp = eval_ndp(op.model, context)
    dp = ndp.get_dp()
    r0 = eval_constant(op.r, context)
    F = dp.get_fun_space()
    R = dp.get_res_space()

    tu = get_types_universe()
    try:
        tu.check_leq(r0.unit, R)
    except NotLeq as e:
        msg = 'Input not correct.'
        raise_wrapped(DPSemanticError, e, msg, compact=True)
    r = r0.cast_value(R)

    res = dp.solve_r(r)
    try:
        LF = LowerSets(F)
        return ValueWithUnits(res, LF)
    except NotBelongs as e:
        msg = 'Invalid result of solve_r().'
        raise_desc(DPInternalError, msg, res=res, dp=dp.repr_long())
示例#7
0
    def solve_r(self, r):
        l2 = self.dp2.solve_r(r)

        if do_extra_checks():
            F2 = self.dp2.get_fun_space()
            LF2 = LowerSets(F2)
            LF2.belongs(l2)

        maxs = set([])
        
        # todo: express as operation on antichains
        for l in l2.maximals:    
            v = self.dp1.solve_r(l)
            maxs.update(v.maximals)

        F = self.get_fun_space()
        maximals = poset_maxima(maxs, F.leq)

        lf = LowerSet(maximals, F)
        return lf
示例#8
0
def check_lang84():  # TODO: rename to LF

    # In LF,
    F = parse_poset('m')
    LF = LowerSets(F)

    lf0 = F.Ls(set([]))
    lf1 = F.L(0.0)
    lf2 = F.L(5.0)
    lf3 = F.L(F.get_top())

    # the bottom is lf3
    LF.check_leq(lf3, lf2)
    LF.check_leq(lf3, lf1)
    LF.check_leq(lf3, lf0)

    LF.check_leq(lf2, lf1)
    LF.check_leq(lf2, lf0)

    LF.check_leq(lf1, lf0)

    pass
示例#9
0
def check_lang84(): # TODO: rename to LF
    
    # In LF,
    F = parse_poset('m')
    LF = LowerSets(F)
    
    
    lf0 = F.Ls(set([]))
    lf1 = F.L(0.0)
    lf2 = F.L(5.0)
    lf3 = F.L(F.get_top())
    
    # the bottom is lf3
    LF.check_leq(lf3, lf2)
    LF.check_leq(lf3, lf1)
    LF.check_leq(lf3, lf0)
    
    LF.check_leq(lf2, lf1)
    LF.check_leq(lf2, lf0)
    
    LF.check_leq(lf1, lf0)
    
    pass
示例#10
0
文件: dual.py 项目: AndreaCensi/mcdp
def dual01_chain(id_dp, dp):
    try:
        with primitive_dp_test(id_dp, dp):
            print('Starting testing with %r' % id_dp)
            # get a chain of resources
            F = dp.get_fun_space()
            R = dp.get_res_space()
            
            # try to solve
            try: 
                dp.solve(F.witness())
                dp.solve_r(R.witness())
            except NotSolvableNeedsApprox:
                print('NotSolvableNeedsApprox - doing  lower bound ')
                n = 5
                dpL, dpU = get_dp_bounds(dp, nl=n, nu=n)
                dual01_chain(id_dp+'_L%s'%n, dpL)
                dual01_chain(id_dp+'_U%s'%n, dpU)
                return
            
            LF = LowerSets(F)
            UR = UpperSets(R)
        
            rchain = R.get_test_chain(n=8)
            poset_check_chain(R, rchain)
        
            try:
                lfchain = list(map(dp.solve_r, rchain))
                for lf in lfchain:
                    LF.belongs(lf)
            except NotSolvableNeedsApprox as e:
                print('skipping because %s'  % e)
                return
        
            try:
                poset_check_chain(LF, list(reversed(lfchain)))
                
            except ValueError as e:
                msg = 'The results of solve_r() are not a chain.'
                raise_wrapped(Exception, e, msg, chain=rchain, lfchain=lfchain)
        
            # now, for each functionality f, 
            # we know that the corresponding resource should be feasible
            
            for lf, r in zip(lfchain, rchain):
                print('')
                print('r: %s' % R.format(r))
                print('lf = h*(r) = %s' % LF.format(lf))
                
                for f in lf.maximals:
                    print('  f = %s' % F.format(f))
                    f_ur = dp.solve(f)
                    print('  f_ur = h(f) =  %s' % UR.format(f_ur))
                     
                    try:
                        f_ur.belongs(r)
                    except NotBelongs as e:
                        
                        try:
                            Rcomp.tolerate_numerical_errors = True
                            f_ur.belongs(r)
                            logger.info('In this case, there was a numerical error')
                            logger.info('Rcomp.tolerate_numerical_errors = True solved the problem')                    
                        except:
                            msg = ''
                            raise_wrapped(AssertionError, e, msg,
                                          lf=lf, r=r, f_ur=f_ur,
                                          r_repr=r.__repr__(),
                                          f_ur_minimals=f_ur.minimals.__repr__())
    finally:
        Rcomp.tolerate_numerical_errors = False
示例#11
0
文件: dp_limit.py 项目: rusi/mcdp
 def repr_h_map(self):
     LF = LowerSets(self.F)
     return 'f ⟼ {⟨⟩} if f ∈ %s, else ø' % LF.format(self.limit)
示例#12
0
    def process_rtof(self, e, string, do_approximations, nl, nu):
        mcdp_library = library_from_env(e)
        parsed = mcdp_library.parse_constant(string)

        space = parsed.unit
        value = parsed.value

        ndp, dp = self.get_ndp_dp_e(e)

        R = dp.get_res_space()
        LF = LowerSets(dp.get_fun_space())

        try:
            r = parsed.cast_value(R)
        except NotLeq:
            msg = 'Space %s cannot be converted to %s' % (parsed.unit, R)
            raise DPSemanticError(msg)

        logger.info('query rtof: %s ...' % R.format(r))
        tracer = Tracer(logger=logger)

        max_steps = 10000
        intervals = False

        res = {}
        if do_approximations:

            dpl, dpu = get_dp_bounds(dp, nl, nu)

            result_l, _trace = solve_meat_solve_rtof(tracer, ndp, dpl, r,
                                                     intervals, max_steps,
                                                     False)

            result_u, trace = solve_meat_solve_rtof(tracer, ndp, dpu, r,
                                                    intervals, max_steps,
                                                    False)

            data = dict(result_l=result_l, result_u=result_u, dpl=dpl, dpu=dpu)

            res['output_result'] = 'Lower: %s\nUpper: %s' % (
                LF.format(result_l), LF.format(result_u))
        else:
            try:
                result, trace = solve_meat_solve_rtof(tracer, ndp, dp, r,
                                                      intervals, max_steps,
                                                      False)
            except NotSolvableNeedsApprox:
                msg = 'The design problem has infinite antichains. Please use approximations.'
                raise NeedsApprox(msg)

            data = dict(result=result, dp=dp)
            res['output_result'] = LF.format(result)

        e = cgi.escape

        res['output_space'] = e(space.__repr__() + '\n' + str(type(space)))
        res['output_raw'] = e(value.__repr__() + '\n' + str(type(value)))
        res['output_formatted'] = e(space.format(value))
        res['output_trace'] = str(trace)

        return data, res
示例#13
0
def eval_space_makelowersets(r, context):
    P = eval_space(r.space, context)
    return LowerSets(P)
示例#14
0
def dual01_chain(id_dp, dp):
    try:
        with primitive_dp_test(id_dp, dp):
            print('Starting testing with %r' % id_dp)
            # get a chain of resources
            F = dp.get_fun_space()
            R = dp.get_res_space()
            
            # try to solve
            try: 
                dp.solve(F.witness())
                dp.solve_r(R.witness())
            except NotSolvableNeedsApprox:
                print('NotSolvableNeedsApprox - doing  lower bound ')
                n = 5
                dpL, dpU = get_dp_bounds(dp, nl=n, nu=n)
                dual01_chain(id_dp+'_L%s'%n, dpL)
                dual01_chain(id_dp+'_U%s'%n, dpU)
                return
            
            LF = LowerSets(F)
            UR = UpperSets(R)
        
            rchain = R.get_test_chain(n=8)
            poset_check_chain(R, rchain)
        
            try:
                lfchain = list(map(dp.solve_r, rchain))
                for lf in lfchain:
                    LF.belongs(lf)
            except NotSolvableNeedsApprox as e:
                print('skipping because %s'  % e)
                return
        
            try:
                poset_check_chain(LF, list(reversed(lfchain)))
                
            except ValueError as e:
                msg = 'The results of solve_r() are not a chain.'
                raise_wrapped(Exception, e, msg, chain=rchain, lfchain=lfchain)
        
            # now, for each functionality f, 
            # we know that the corresponding resource should be feasible
            
            for lf, r in zip(lfchain, rchain):
                print('')
                print('r: %s' % R.format(r))
                print('lf = h*(r) = %s' % LF.format(lf))
                
                for f in lf.maximals:
                    print('  f = %s' % F.format(f))
                    f_ur = dp.solve(f)
                    print('  f_ur = h(f) =  %s' % UR.format(f_ur))
                     
                    try:
                        f_ur.belongs(r)
                    except NotBelongs as e:
                        
                        try:
                            Rcomp.tolerate_numerical_errors = True
                            f_ur.belongs(r)
                            logger.info('In this case, there was a numerical error')
                            logger.info('Rcomp.tolerate_numerical_errors = True solved the problem')                    
                        except:
                            msg = ''
                            raise_wrapped(AssertionError, e, msg,
                                          lf=lf, r=r, f_ur=f_ur,
                                          r_repr=r.__repr__(),
                                          f_ur_minimals=f_ur.minimals.__repr__())
    finally:
        Rcomp.tolerate_numerical_errors = False