def __init__(self, fs): fs = tuple(fs) self.fs = fs mcdp_dev_warning('add promotion to SpaceProduct') dom = PosetProduct(tuple(fi.get_domain() for fi in fs)) cod = PosetProduct(tuple(fi.get_codomain() for fi in fs)) Map.__init__(self, dom=dom, cod=cod)
def __init__(self, F, value): F.belongs(value) self.limit = value R = PosetProduct(()) I = PosetProduct(()) PrimitiveDP.__init__(self, F=F, R=R, I=I)
def get(matches, used, Ps, get_P, Pa, ua): others = list(set(used) - set(matches)) extra = PosetProduct(tuple(map(get_P, others))) print('extra for Pa: %s' % extra) Pa_comp = PosetProduct(Pa.subs + extra.subs) print('Pa_comp: %s' % Pa_comp) extra_minimals = extra.get_minimal_elements() m_matches = matches + others s = set() R = set() for m1, m2 in itertools.product(ua.minimals, extra_minimals): m = m1 + m2 s.add(m) r = [None] * len(used) for i, a in enumerate(used): S1 = Pa_comp.subs[m_matches.index(a)] s1 = m[m_matches.index(a)] S2 = get_P(a) r[i] = express_value_in_isomorphic_space(S1, s1, S2) r = tuple(r) R.add(r) Pa_comp_lb = Pa_comp.Us(s) print('Pa_comp_lb: %s' % Pa_comp_lb) Ps_a = Ps.Us(R) return Ps_a
def __init__(self, F, Rs): for _ in Rs: check_isinstance(_, Nat) check_isinstance(F, Nat) R = PosetProduct(Rs) M = PosetProduct((F, R)) PrimitiveDP.__init__(self, F=F, R=R, I=M)
def check_products(): F1 = R_Weight F2 = R_Time F3 = R_Energy M, pack, unpack = get_product_compact(F1, F2, F3) print(M) s = pack(F1.get_top(), F2.get_bottom(), F3.get_top()) print(s) u = unpack(s) assert_equal(u, s) F1 = R_Time F2 = PosetProduct(()) F3 = R_Energy F = PosetProduct((F1, F2, F3)) print('F: %s' % F) M, pack, unpack = get_product_compact(F1, F2, F3) print('M: %s' % M) element = (F1.get_top(), F2.get_bottom(), F3.get_top()) print('elements: %s' % F.format(element)) s = pack(*element) print('packed: %s' % str(s)) u = unpack(s) print('depacked: %s' % str(u)) assert_equal(u, element)
def __init__(self, F, Rs, nl): for _ in Rs: check_isinstance(_, (Rcomp, RcompUnits)) check_isinstance(F, (Rcomp, RcompUnits)) R = PosetProduct(Rs) M = PosetProduct((F, R)) PrimitiveDP.__init__(self, F=F, R=R, I=M) self.nl = nl
def DPLoop2_1(): F1 = parse_poset('N') R1 = parse_poset('J') F2 = parse_poset('m') R2 = F2 F = PosetProduct((F1, F2)) R = PosetProduct((R1, R2)) dp = Template(F, R) return DPLoop2(dp)
def __init__(self, F, Rs): if not len(Rs) == 2: raise ValueError(Rs) for _ in Rs: check_isinstance(_, Nat) check_isinstance(F, Nat) R = PosetProduct(Rs) M = PosetProduct((F, R)) PrimitiveDP.__init__(self, F=F, R=R, I=M)
def __init__(self, F, Rs, n): R = PosetProduct(Rs) M = PosetProduct((F, R)) self.F = F self.Rs = Rs PrimitiveDP.__init__(self, F=F, R=R, I=M) self.R = R self.n = n
def ndps_parallel(ndps): dps = [ndp.get_dp() for ndp in ndps] dp = make_parallel_n(dps) F = dp.get_fun_space() R = dp.get_res_space() fnames = [] ftypes = [] rnames = [] rtypes = [] coords_postfix = [] coords_prefix = [] for i, ndp_i in enumerate(ndps): fnames_i = ndp_i.get_fnames() if not fnames_i: coords_prefix.append([]) else: mine = [] for j, fn in enumerate(fnames_i): ft = ndp_i.get_ftype(fn) F0_index = len(fnames) mine.append(F0_index) fnames.append(fn) ftypes.append(ft) if len(mine) == 1: mine = mine[0] coords_prefix.append(mine) rnames_i = ndp_i.get_rnames() for j, rn in enumerate(rnames_i): rt = ndp_i.get_rtype(rn) if len(rnames_i) == 1: coords_postfix.append(i) else: coords_postfix.append((i, j)) rnames.append(rn) rtypes.append(rt) F0 = PosetProduct(ftypes) prefix = Mux(F0, coords_prefix) assert F == prefix.get_res_space() R0 = PosetProduct(rtypes) postfix = Mux(R, coords_postfix) assert R0 == postfix.get_res_space() res_dp = make_series(make_series(prefix, dp), postfix) from mocdp.comp.connection import simplify_if_only_one_name res_dp, fnames, rnames = simplify_if_only_one_name(res_dp, fnames, rnames) res = SimpleWrap(res_dp, fnames, rnames) return res
def __init__(self, F, values): if do_extra_checks(): for value in values: F.belongs(value) self.limit = LowerSet(values, F) R = PosetProduct(()) M = PosetProduct(()) PrimitiveDP.__init__(self, F=F, R=R, I=M)
def Mux5(): """ One with a 1: <a, *> -> a """ N = parse_poset('Nat') One = PosetProduct(()) P = PosetProduct((N, One)) coords = 0 return Mux(P, coords)
def __init__(self, dps): Fs = [_.get_fun_space() for _ in dps] Rs = [_.get_res_space() for _ in dps] Ms = [_.get_imp_space() for _ in dps] F = PosetProduct(tuple(Fs)) R = PosetProduct(tuple(Rs)) self.Ms = Ms self.dps = tuple(dps) self.M, _, _ = self._get_product() PrimitiveDP.__init__(self, F=F, R=R, I=self.M)
def __init__(self, F, Rs): for _ in Rs: check_isinstance(_, (Rcomp, RcompUnits)) check_isinstance(F, (Rcomp, RcompUnits)) self.Rs = Rs R = PosetProduct(Rs) tu = get_types_universe() if not tu.equal(Rs[0], Rs[1]) or not tu.equal(F, Rs[0]): msg = 'InvPlus only available for consistent units.' raise_desc(DPInternalError, msg, F=F, Rs=Rs) M = PosetProduct((F, R)) PrimitiveDP.__init__(self, F=F, R=R, I=M)
def add_extra(lb, P, v): P.belongs(v) P1 = lb.P if not isinstance(P1, PosetProduct): P1 = PosetProduct((P1, )) minimals = set((a, ) for a in lb.minimals) else: minimals = lb.minimals P1b = PosetProduct(P1.subs + (P, )) def mm(x): return x + (v, ) l1b = P1b.Us(map(mm, minimals)) return l1b
def battery_complete(): assert R_Time != R_Energy assert R_Time != R_Power assert R_Energy != R_Power assert PosetProduct((R_Time, R_Time)) != PosetProduct((R_Time, R_Energy)) N = Single('navigate') F = PosetProduct((PosetProduct((R_Weight_g, N)), R_Weight_g)) dpB = Mux(F, [[(0, 0), 1], (0, 1)]) assert dpB.get_fun_space() == F dpA = Parallel(Sum(R_Weight_g), Identity(N)) series([dpB, dpA]) dp1 = Parallel(Mobility(), TimeEnergyTradeoff()) series([dpA, dp1]) dp2 = Mux(F=dp1.get_res_space(), coords=[[0, (1, 0)], (1, 1)]) series([dp1, dp2]) dp4 = Parallel(Sum(R_Power), Identity(R_Time)) series([dp2, dp4]) dp4b = Mux(dp4.get_res_space(), [1, [1, 0]]) series([dp4, dp4b]) e_from_tp = Product(R_Time, R_Power, R_Energy) from .dp_bat import BatteryDP battery = BatteryDP(energy_density=100.0) dp7 = Parallel(Identity(R_Time), make_series(e_from_tp, battery)) series([dp4b, dp7]) dps = series([dpB, dpA, dp1, dp2, dp4, dp4b, dp7]) # dp = DPLoop(dps) return dps
def __init__(self, F0): F = PosetProduct((F0, F0)) m1 = CheckOrder(F0) m2 = MuxMap(F, coords=0) # extract second coordinate h = MapComposition((m1, m2)) hd = SecondTop(F0) WrapAMap.__init__(self, h, hd)
def get_common(ua, ub): Pa = ua.P Pb = ub.P if not isinstance(Pa, PosetProduct) or not isinstance(Pb, PosetProduct): raise NotImplementedError((Pa, Pb)) # first, it might be that they have different spaces # let's find out how to match them tu = get_types_universe() # for each i in Pa, we will match it to the first matches1 = [] for i, P in enumerate(Pa.subs): for j, Q in enumerate(Pb.subs): if ('B', j) in matches1: continue if tu.leq(P, Q): matches1.append(('B', j)) break else: matches1.append(('A', i)) matches2 = [] for j, Q in enumerate(Pb.subs): if ('B', j) in matches1: # used by somebody matches2.append(('B', j)) else: for i, P in enumerate(Pa.subs): if matches1[i] is not None: continue if tu.leq(Q, P): matches2.append(('A', i)) break else: matches2.append(('B', j)) print('matches1: %s' % matches1) print('matches2: %s' % matches2) used = sorted(set(matches1 + matches2)) def get_P(_): (which, index) = _ if which == 'A': return Pa.subs[index] if which == 'B': return Pb.subs[index] assert False Ps = PosetProduct(tuple(map(get_P, used))) print('used: %s' % used) print('Ps: %s' % Ps) # now we need to complete the first Ps_a = get(matches1, used, Ps, get_P, Pa, ua) Ps_b = get(matches2, used, Ps, get_P, Pb, ub) print('Ps_a: %s' % Ps_a) print('Ps_b: %s' % Ps_b) S = UpperSets(Ps) return S, Ps_a, Ps_b
def _execute(self, dp1, dp2): from mcdp_dp.dp_loop2 import DPLoop2 assert isinstance(dp1, Mux) assert isinstance(dp2, DPLoop2) # I want to create this: # P -- |m| --- (Px) # F2 -- Id --- F2 if dp1.coords == [()]: coords = [[0], 1] else: raise_desc(NotImplementedError, "not implemented", dp1=dp1, dp2=dp2) dp0 = dp2.dp1 F = dp0.get_fun_space() F2 = F[1] F1 = F[0] P = F1[0] m1F = PosetProduct((P, F2)) # print('m1F: %s' % m1F) m1 = Mux(m1F, coords) # print 'm1', m1.tree_long() res = DPLoop2(make_series(m1, dp0)) return res
def abstract(self): if not self.context.names: # this means that there are nor children, nor functions nor resources dp = Mux(PosetProduct(()), ()) ndp = SimpleWrap(dp, fnames=[], rnames=[]) return ndp try: self.check_fully_connected() except NotConnected as e: msg = 'Cannot abstract because not all subproblems are connected.' raise_wrapped(DPSemanticError, e, msg, exc=sys.exc_info(), compact=True) from mocdp.comp.composite_abstraction import cndp_abstract res = cndp_abstract(self) assert isinstance(res, SimpleWrap), type(res) assert res.get_fnames() == self.context.fnames assert res.get_rnames() == self.context.rnames return res
def get_dummy(fnames, rnames): base = (Rcomp(),) F = PosetProduct(base * len(fnames)) R = PosetProduct(base * len(rnames)) if len(fnames) == 1: F = F[0] if len(rnames) == 1: R = R[0] if len(fnames) == 1: fnames = fnames[0] if len(rnames) == 1: rnames = rnames[0] return SimpleWrap(Dummy(F, R), fnames, rnames)
def plot(self, pylab, axis, space, value, params={}): params0 = dict(color_shadow=[1.0, 0.8, 0.8], markers='k.', markers_params={}) params0.update(params) color_shadow = params0['color_shadow'] markers = params0['markers'] markers_params = params0['markers_params'] self.check_plot_space(space) # tu = get_types_universe() # P_TO_S, _ = tu.get_embedding(space.P, self.S) points2d = [self.toR2(self.P_to_S(_)) for _ in value.minimals] R2 = PosetProduct((Rcomp(), Rcomp())) class MyUpperSet(UpperSet): def __init__(self, minimals, P): self.minimals = frozenset(minimals) self.P = P v = MyUpperSet(points2d, P=R2) plot_upset_R2(pylab, v, axis, color_shadow=color_shadow, markers=markers, marker_params=markers_params)
def check_compose2_generic(): actuation = dpwrap(Mobility(), 'weight', 'actuation_power') battery = dpwrap((BatteryDP(energy_density=100.0)), 'capacity', 'battery_weight') times = dpwrap((Product(R_Time, R_Power, R_Energy)), ['mission_time', 'power'], 'energy') c1 = Connection('actuation', 'actuation_power', 'times', 'power') c2 = Connection('times', 'energy', 'battery', 'capacity') c3 = Connection('battery', 'battery_weight', 'actuation', 'weight') y = dpgraph(dict(actuation=actuation, times=times, battery=battery), [c1, c2, c3], split=[]) print y.desc() assert y.get_fnames() == ['mission_time'], y.get_fnames() assert y.get_rnames() == [], y.get_rnames() check_ftype(y, 'mission_time', R_Time) dp = y.get_dp() funsp = dp.get_fun_space() ressp = dp.get_res_space() assert funsp == R_Time, funsp assert ressp == PosetProduct(()), ressp
def ndp_templatize(ndp, mark_as_template=False): """ Creates a template based on the interface of the ndp. The dp is Dummy. The ndp is either - OnlyTemplate (placeholder, drawn with dashed lines) [using mark_as_template] - Templatized (drawn with solid black line) Copies attributes: ATTR_LOAD_NAME """ fnames = ndp.get_fnames() ftypes = ndp.get_ftypes(fnames) rnames = ndp.get_rnames() rtypes = ndp.get_rtypes(rnames) if len(fnames) == 1: fnames = fnames[0] F = ftypes[0] else: F = PosetProduct(tuple(ftypes)) if len(rnames) == 1: rnames = rnames[0] R = rtypes[0] else: R = PosetProduct(tuple(rtypes)) dp = Dummy(F, R) if mark_as_template: klass = OnlyTemplate # raise Exception() else: klass = Templatized res = klass(dp, fnames, rnames) att = MCDPConstants.ATTR_LOAD_NAME if hasattr(ndp, att): x = getattr(ndp, att) setattr(res, att, x) return res
def __init__(self, n): self.n = n N = Nat() dom = PosetProduct((N, ) * n) cod = N Map.__init__(self, dom=dom, cod=cod) self.top = cod.get_top() self.n = n
def __init__(self, Fs, R): """ Should be all Rcomp or all RcompUnits """ for _ in Fs: check_isinstance(_, (Rcomp, RcompUnits)) check_isinstance(R, (Rcomp, RcompUnits)) self.F = dom = PosetProduct(Fs) self.R = cod = R Map.__init__(self, dom=dom, cod=cod)
def __init__(self, Tmax, W0, rho): self.Tmax = Tmax self.W0 = W0 self.rho = rho F = PosetProduct((R_Energy, R_Time)) R = R_Weight_g M = SpaceProduct(()) PrimitiveDP.__init__(self, F=F, R=R, M=M)
def convert_string_query(ndp, query, context): """ Converts a string query to a value that can be passed to the DP. Example: f = bind(ndp, dict(power='100mW')) f == 0.1 dp.solve(f) """ # first: make sure the names are the same fnames = ndp.get_fnames() fnames2 = set(query) if set(fnames) != fnames2: msg = 'Missing values in query or too many values.' raise_desc(ValueError, msg, fnames=fnames, query=query) fds = [] Fds = [] tu = get_types_universe() for fname in fnames: q = query[fname] vu = parse_constant(q, context) fds.append(vu.value) Fds.append(vu.unit) F0 = ndp.get_ftype(fname) if not tu.leq(vu.unit, F0): msg = 'Invalid value for %r: %s does not cast to %s.' % (fname, vu, F0) raise_desc(ValueError, msg) Fd = PosetProduct(tuple(Fds)) fd = tuple(fds) if len(fnames) == 1: Fd = Fd[0] fd = fd[0] else: Fd = Fd fd = fd F = ndp.get_ftypes(fnames) if len(fnames) == 1: F = F[0] tu.check_leq(Fd, F) A_to_B, _ = tu.get_embedding(Fd, F) fg = A_to_B(fd) #print('Fd: %s' % Fd.format(fd)) #print('F: %s' % F.format(fg)) return fg
def pmin3(): n = 200000 ndim = 2 Pbase = wrap_with_counts(Rcomp()) N2 = PosetProduct((Pbase, ) * ndim) print('Using random antichain') Ps = get_random_points(n, ndim, m=1000) run_all(N2, Ps)
def eval_constant_MakeTuple(op, context): ops = get_odd_ops(unwrap_list(op.ops)) constants = [eval_constant(_, context) for _ in ops] # TODO: generic product Fs = [_.unit for _ in constants] vs = [_.value for _ in constants] F = PosetProduct(tuple(Fs)) v = tuple(vs) F.belongs(v) return ValueWithUnits(v, F)