def check_uncertainty2(): ndp = parse_ndp(""" mcdp { provides f1 [N] f1 <= Uncertain(1 N, 2 N) } """) dp = ndp.get_dp() dpl, dpu = get_dp_bounds(dp, 1, 1) R = dp.get_res_space() UR = UpperSets(R) f0 = 0.0 # N sl = dpl.solve(f0) su = dpu.solve(f0) UR.check_leq(sl, su) print sl print su f0 = 1.5 # N sl = dpl.solve(f0) su = dpu.solve(f0) UR.check_leq(sl, su) print sl print su feasible = UpperSet(set([()]), R) infeasible = UpperSet(set([]), R) sl_expected = feasible su_expected = infeasible print sl_expected print su_expected UR.check_equal(sl, sl_expected) UR.check_equal(su, su_expected)
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
def check_uncertainty2(): ndp = parse_ndp(""" mcdp { provides f1 [N] f1 <= Uncertain(1 N, 2 N) } """) dp = ndp.get_dp() dpl, dpu = get_dp_bounds(dp, 1, 1) R = dp.get_res_space() UR = UpperSets(R) f0 = 0.0 # N sl = dpl.solve(f0) su = dpu.solve(f0) UR.check_leq(sl, su) print sl print su f0 = 1.5 # N sl = dpl.solve(f0) su = dpu.solve(f0) UR.check_leq(sl, su) print sl print su feasible = UpperSet(set([()]), R) infeasible = UpperSet(set([]), R) sl_expected = feasible su_expected = infeasible print sl_expected print su_expected UR.check_equal(sl, sl_expected) UR.check_equal(su, su_expected)
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
def check_uncertainty1(): ndp = parse_ndp(""" mcdp { requires r1 [USD] r1 >= Uncertain(1 USD, 2USD) } """) dp = ndp.get_dp() dpl, dpu = get_dp_bounds(dp, 1, 1) UR = UpperSets(dp.get_res_space()) f = () sl = dpl.solve(f) su = dpu.solve(f) UR.check_leq(sl, su)
def check_uncertainty1(): ndp = parse_ndp(""" mcdp { requires r1 [USD] r1 >= Uncertain(1 USD, 2USD) } """) dp = ndp.get_dp() dpl, dpu = get_dp_bounds(dp, 1, 1) UR = UpperSets(dp.get_res_space()) f = () sl = dpl.solve(f) su = dpu.solve(f) UR.check_leq(sl, su)
def check_solve_top_bottom(id_dp, dp): print('Testing %s: %s' % (id_dp, dp)) F = dp.get_fun_space() R = dp.get_res_space() UR = UpperSets(R) print('F: %s' % F) print('R: %s' % R) I = dp.get_imp_space() M = dp.get_imp_space() print('I: %s' % I) print('M: %s' % M) try: f_top = F.get_top() f_bot = F.get_bottom() except NotBounded: return print('⊥ = %s' % F.format(f_bot)) print('⊤ = %s' % F.format(f_top)) try: ur0 = dp.solve(f_bot) ur1 = dp.solve(f_top) except NotSolvableNeedsApprox: return print('f(%s) = %s' % (f_bot, ur0)) print('f(%s) = %s' % (f_top, ur1)) print('Checking that the order is respected') try: UR.check_leq(ur0, ur1) except NotLeq as e: msg = 'Not true that f(⊥) ≼ f(⊤).' raise_wrapped(Exception, e, msg, ur0=ur0, ur1=ur1) # get implementations for ur0 for r in ur0.minimals: ms = dp.get_implementations_f_r(f_bot, r) for m in ms: M.belongs(m)
def check_uncertainty5(): s = """ mcdp { provides capacity [Wh] requires mass [kg] required mass * Uncertain(100 Wh/kg, 120 Wh/kg) >= provided capacity }""" ndp = parse_ndp(s) dp = ndp.get_dp() R = dp.get_res_space() UR = UpperSets(R) dpl, dpu = get_dp_bounds(dp, 1000, 1000) f0 = 1.0 # J sl = dpl.solve(f0) su = dpu.solve(f0) UR.check_leq(sl, su)
def check_solve_top_bottom(id_dp, dp): print('Testing %s: %s' % (id_dp, dp)) F = dp.get_fun_space() R = dp.get_res_space() UR = UpperSets(R) print('F: %s' % F) print('R: %s' % R) I = dp.get_imp_space() M = dp.get_imp_space() print('I: %s' % I) print('M: %s' % M) try: f_top = F.get_top() f_bot = F.get_bottom() except NotBounded: return print('⊥ = %s' % F.format(f_bot)) print('⊤ = %s' % F.format(f_top)) try: ur0 = dp.solve(f_bot) ur1 = dp.solve(f_top) except NotSolvableNeedsApprox: return print('f(%s) = %s' % (f_bot, ur0)) print('f(%s) = %s' % (f_top, ur1)) print('Checking that the order is respected') try: UR.check_leq(ur0, ur1) except NotLeq as e: msg = 'Not true that f(⊥) ≼ f(⊤).' raise_wrapped(Exception, e, msg, ur0=ur0,ur1=ur1) # get implementations for ur0 for r in ur0.minimals: ms = dp.get_implementations_f_r(f_bot, r) for m in ms: M.belongs(m)
def new_uncertainty_semantics01(): s = """ mcdp { a = instance mcdp { provides f = between 1W and 2W } provides f using a } """ ndp = parse_ndp(s) dp = ndp.get_dp() dpl, dpu = get_dp_bounds(dp, 1, 1) rl = dpl.solve(1.5) ru = dpu.solve(1.5) feasible = lambda l: len(l.minimals) > 0 UR = UpperSets(dp.get_res_space()) UR.check_leq(rl, ru) assert feasible(rl) assert not feasible(ru)
def check_uncertainty5(): s = """ mcdp { provides capacity [Wh] requires mass [kg] required mass * Uncertain(100 Wh/kg, 120 Wh/kg) >= provided capacity }""" ndp = parse_ndp(s) dp = ndp.get_dp() R = dp.get_res_space() UR = UpperSets(R) dpl, dpu = get_dp_bounds(dp, 1000, 1000) f0 = 1.0 # J sl = dpl.solve(f0) su = dpu.solve(f0) UR.check_leq(sl, su) print sl print su
def check_uncertainty3(): s = """ mcdp { provides capacity [J] requires mass [kg] required mass * Uncertain(2 J/kg, 3 J/kg) >= provided capacity } """ ndp = parse_ndp(s) dp = ndp.get_dp() R = dp.get_res_space() UR = UpperSets(R) dpl, dpu = get_dp_bounds(dp, 100, 100) f0 = 1.0 # J sl = dpl.solve(f0) su = dpu.solve(f0) print sl print su UR.check_leq(sl, su) real_lb = UpperSet(set([0.333333]), R) real_ub = UpperSet(set([0.500000]), R) # now dpl will provide a lower bound from below UR.check_leq(sl, real_lb) # and dpu will provide the upper bound from above UR.check_leq(real_ub, su)
def check_uncertainty3(): s = """ mcdp { provides capacity [J] requires mass [kg] required mass * Uncertain(2 J/kg, 3 J/kg) >= provided capacity } """ ndp = parse_ndp(s) dp = ndp.get_dp() R = dp.get_res_space() UR = UpperSets(R) dpl, dpu = get_dp_bounds(dp, 100, 100) f0 = 1.0 # J sl = dpl.solve(f0) su = dpu.solve(f0) print sl print su UR.check_leq(sl, su) real_lb = UpperSet(set([0.333333]), R) real_ub = UpperSet(set([0.500000]), R) # now dpl will provide a lower bound from below UR.check_leq(sl, real_lb) # and dpu will provide the upper bound from above UR.check_leq(real_ub, su)
def check_power8(): # TODO: move to ther files ndp = parse_ndp(""" mcdp { requires a [dimensionless] requires b [dimensionless] provides c [dimensionless] a + b >= c } """) dp = ndp.get_dp() print(dp.repr_long()) nl = 5 nu = 5 dpL, dpU = get_dp_bounds(dp, nl, nu) print(dpL.repr_long()) print(dpU.repr_long()) f = 10.0 UR = UpperSets(dp.get_res_space()) Rl = dpL.solve(f) Ru = dpU.solve(f) assert_equal(len(Rl.minimals), nl) assert_equal(len(Ru.minimals), nu) print('Rl: %s' % UR.format(Rl)) print('Ru: %s' % UR.format(Ru)) UR.check_leq(Rl, Ru) import numpy as np for x in np.linspace(0, f, 100): y = f - x p = (x, y) Rl.belongs(p)