Example #1
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)
Example #2
0
def check_solve_f_chain(id_dp, dp):
    with primitive_dp_test(id_dp, dp):

        from mcdp_posets.utils import poset_check_chain

        F = dp.get_fun_space()

        f_chain = F.get_test_chain(n=8)
        poset_check_chain(F, f_chain)

        try:
            trchain = map(dp.solve, f_chain)
        except NotSolvableNeedsApprox:
            return try_with_approximations(id_dp, dp, check_solve_f_chain)

        R = dp.get_res_space()
        UR = UpperSets(R)
        try:
            poset_check_chain(UR, trchain)
        except ValueError as e:
            msg = 'The map solve() for %r is not monotone.' % id_dp
            raise_wrapped(Exception,
                          e,
                          msg,
                          f_chain=f_chain,
                          trchain=trchain,
                          compact=True)
Example #3
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)
Example #4
0
def check_poset1_chain(id_poset, poset):
    try:
        chain = poset.get_test_chain(n=5)
        poset_check_chain(poset, chain)
    except Uninhabited:
        # list exceptions that can be empty
        if isinstance(poset, FinitePoset):
            return
        raise Exception('%s %s is Uninhabited' % (id_poset, poset))
        

    for a in chain:
        poset.check_equal(a, a)
        m = poset.meet(a, a)
        poset.check_equal(m, a)
        m = poset.join(a, a )
        poset.check_equal(a, a)

    for a, b in itertools.combinations(chain, 2):
        try:
            poset.check_equal(a, b)
        except NotEqual:
            pass
        else:
            msg = 'I expected that a != b, but this is violated.'
            raise_desc(Exception, msg, a=a, b=b, poset=poset, chain=chain)

    for i, j in itertools.combinations(range(len(chain)), 2):
        if i > j:
            i, j = j, i
        
        e1 = chain[i]
        e2 = chain[j]
        
        print('Comparing e1 = {} and e2 = {}'.format(poset.format(e1), poset.format(e2)))
        
        poset.check_leq(e1, e2)
        try: 
            poset.check_leq(e2, e1)
        except NotLeq:
            pass
        
        meet1 = poset.meet(e1, e2)
        meet2 = poset.meet(e2, e1)
        
        print('meet1: {}'.format(meet1))
        print('meet2: {}'.format(meet2))

        join1 = poset.join(e1, e2)
        join2 = poset.join(e2, e1)

        print('join1: {}'.format(join1))
        print('join2: {}'.format(join2))
        
        poset.check_equal(meet1, e1)
        poset.check_equal(meet2, e1)
        poset.check_equal(join1, e2)
        poset.check_equal(join2, e2)
Example #5
0
def check_posets_misc1():
    try:
        poset_check_chain(Nat(), [2, 1])
    except ValueError:
        pass
    else:
        assert False

    try:
        check_minimal([2, 1], Nat())
    except ValueError:
        pass
    else:
        assert False

    try:
        check_maximal([2, 1], Nat())
    except ValueError:
        pass
    else:
        assert False
Example #6
0
def check_solve_f_chain(id_dp, dp):
    with primitive_dp_test(id_dp, dp):
    
        from mcdp_posets.utils import poset_check_chain
    
        F = dp.get_fun_space()
    
        f_chain = F.get_test_chain(n=8)
        poset_check_chain(F, f_chain)
    
        try:
            trchain = map(dp.solve, f_chain)
        except NotSolvableNeedsApprox:
            return try_with_approximations(id_dp, dp, check_solve_f_chain)
    
        R = dp.get_res_space()
        UR = UpperSets(R)
        try:
            poset_check_chain(UR, trchain)
        except ValueError as e:
            msg = 'The map solve() for %r is not monotone.' % id_dp
            raise_wrapped(Exception, e, msg, f_chain=f_chain, 
                          trchain=trchain, compact=True)
Example #7
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
Example #8
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