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
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
def repr_h_map(self): LF = LowerSets(self.F) return 'f ⟼ {⟨⟩} if f ∈ %s, else ø' % LF.format(self.limit)
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