def ndp_make(ndp, imp_dict, context): """ A top-down make function. imp_dict = {name1: value, name2: value, ...} """ if isinstance(ndp, CompositeNamedDP): children_artifact = {} for child_name, child in cndp_get_name_ndp_notfunres(ndp): if not child_name in imp_dict: if isinstance(child, SimpleWrap) and isinstance( child.dp, (Mux, IdentityDP)): # Muxes and identities could be optimized away # and disappear continue elif isinstance(child, CompositeNamedDP) and not child.get_name2ndp(): # if the CompositeNamedDP is empty, then we don't have any output # here. however we know that the child_imp is only: child_imp = {} # and we can create it on the fly: children_artifact[child_name] = ndp_make( child, child_imp, context) continue else: msg = 'Could not find artifact for child.' raise_desc(DPInternalError, msg, child_name=child_name, imp_dict=imp_dict, child=child.repr_long()) else: child_imp = imp_dict[child_name] children_artifact[child_name] = ndp_make( child, child_imp, context) return run_make(ndp, children_artifact, context) elif isinstance(ndp, SimpleWrap): return run_make(ndp, imp_dict, context) elif isinstance(ndp, NamedDPCoproduct): for label in ndp.labels: if label in imp_dict: index = ndp.labels.index(label) child = ndp.ndps[index] value = imp_dict[label] return run_make(child, value, context) assert False elif isinstance(ndp, LabelerNDP): msg = 'Bug: you should not run make() on the labeled version.' raise_desc(DPInternalError, msg) else: raise NotImplementedError(type(ndp))
def opt_basic_7(): libnames = ['actuation'] library = get_test_library2(libnames) # outdir = 'out/opt_basic_7' # cache_dir = os.path.join(outdir, 'cache') library.use_tmp_cache() ndp = library.load_ndp('duckiebot_sol00') min_functions = { 'motion': '< `RigidBodyID: rb1, 0.1 m/s, 10 minutes >', } _flabels, F0s, f0s = _parse_dict(min_functions, library) names = [name for name, _ndp in cndp_get_name_ndp_notfunres(ndp)] print('names: %s' % names) to_remove = list(names) to_remove.remove('dagu_chassis') # to_remove.remove('battery_ravpower') # to_remove.remove('USBMicroCharging') from mcdp_opt_tests.ndp_utils import cndp_remove_one_child for n in to_remove: ndp = cndp_remove_one_child(ndp, n) print ndp if len(F0s) > 1: F = PosetProduct(F0s) f0 = f0s else: F = F0s[0] f0 = f0s[0] dp = ndp.get_dp() F1 = dp.get_fun_space() f1 = express_value_in_isomorphic_space(F, f0, F1) _res = dp.solve(f1)
def simplify_identities(ndp): """ Removes the identites from the NDP. Useful for visualization. """ def can_simplify(child): if len(child.get_fnames()) != 1: return False if len(child.get_rnames()) != 1: return False if not isinstance(child, SimpleWrap): return False if not isinstance(child.dp, IdentityDP): return False return True try: ndp.check_fully_connected() connected = True except NotConnected: connected = False ndp = ndp.__copy__() for name, child in list(cndp_get_name_ndp_notfunres(ndp)): if not can_simplify(child): continue # (prev) -> (name) -> (succ) prev = succ = None # prevs = [] # succs = [] for c in ndp.context.connections: if c.dp2 == name: prev = c.dp1 prev_s = c.s1 # prevs.append((c.dp1, c.s1)) if c.dp1 == name: succ = c.dp2 succ_s = c.s2 # succs.append((c.dp2, c.s2)) # it has exactly 1 forward and 1 succ connections, # not to itself ok = prev is not None and succ is not None and prev != succ if not ok: continue # remove those two connections = [c for c in ndp.context.connections if c.dp1 != name and c.dp2 != name] ndp.context.connections = connections c = Connection(dp1=prev, s1=prev_s, dp2=succ, s2=succ_s) ndp.context.add_connection(c) del ndp.context.names[name] res = ndp.__copy__() if connected: try: res.check_fully_connected() except NotConnected as e: msg = "Result is not fully connected." raise_wrapped(DPInternalError, e, msg, compact=True) # res=res.repr_long(), return res
def simplify_identities(ndp): """ Removes the identites from the NDP. Useful for visualization. """ def can_simplify(child): if len(child.get_fnames()) != 1: return False if len(child.get_rnames()) != 1: return False if not isinstance(child, SimpleWrap): return False if not isinstance(child.dp, IdentityDP): return False return True try: ndp.check_fully_connected() connected = True except NotConnected: connected = False ndp = ndp.__copy__() for name, child in list(cndp_get_name_ndp_notfunres(ndp)): if not can_simplify(child): continue # (prev) -> (name) -> (succ) prev = succ = None # prevs = [] # succs = [] for c in ndp.context.connections: if c.dp2 == name: prev = c.dp1 prev_s = c.s1 # prevs.append((c.dp1, c.s1)) if c.dp1 == name: succ = c.dp2 succ_s = c.s2 # succs.append((c.dp2, c.s2)) # it has exactly 1 forward and 1 succ connections, # not to itself ok = prev is not None and succ is not None and prev != succ if not ok: continue # remove those two connections = [ c for c in ndp.context.connections if c.dp1 != name and c.dp2 != name ] ndp.context.connections = connections c = Connection(dp1=prev, s1=prev_s, dp2=succ, s2=succ_s) ndp.context.add_connection(c) del ndp.context.names[name] res = ndp.__copy__() if connected: try: res.check_fully_connected() except NotConnected as e: msg = 'Result is not fully connected.' raise_wrapped( DPInternalError, e, msg, # res=res.repr_long(), compact=True) return res