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]
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
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
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