class InformedPlanner(GenericGraphPlanner): """ This one knows that some plans are redundant """ def __init__(self, diffeo_structure_params, pre_expand=[], *args, **kwargs): super(InformedPlanner, self).__init__(*args, **kwargs) self.diffeo_structure_params = diffeo_structure_params self.pre_expand = pre_expand def __str__(self): return 'InformedPlanner(%s)' % (self.__strparams__()) def __strparams__(self): # TODO: use classes p = GenericGraphPlanner.__strparams__(self) return p @contract(dds=DiffeoSystem) def init(self, id_dds, dds): self.ds = DiffeoStructure(dds=dds, **self.diffeo_structure_params) self.original_dds = dds dds_expanded = diffeosystem_expand(dds, pr=self.ds.get_plan_reducer(), heuristics=self.pre_expand) # TODO: recompute structure self.info('pre_expand: %s' % str(self.pre_expand)) self.info('Expanded to %s from %s actions' % (len(dds_expanded.actions), len(dds.actions))) super(InformedPlanner, self).init(id_dds, dds_expanded) def plan(self, *args, **kwargs): # we need to translate back the result according to the original plan result = super(InformedPlanner, self).plan(*args, **kwargs) if result.success: plan_extended = result.plan self.info('Solution found in extended space: %s' % str(plan_extended)) plan_simple = self.get_dds().plan_with_simple_actions( plan_extended) self.info('With simple actions: %s' % str(plan_simple)) result.plan = plan_simple return result @contract(report=Report) def init_report(self, report): """ Creates a report for the initialization phase. """ super(InformedPlanner, self).init_report(report) self.ds.display(report.section('diffeo_structure')) def get_plan_reducer(self): return self.ds.get_plan_reducer()
class InformedPlanner(GenericGraphPlanner): """ This one knows that some plans are redundant """ def __init__(self, diffeo_structure_params, pre_expand=[], *args, **kwargs): super(InformedPlanner, self).__init__(*args, **kwargs) self.diffeo_structure_params = diffeo_structure_params self.pre_expand = pre_expand def __str__(self): return "InformedPlanner(%s)" % (self.__strparams__()) def __strparams__(self): # TODO: use classes p = GenericGraphPlanner.__strparams__(self) return p @contract(dds=DiffeoSystem) def init(self, id_dds, dds): self.ds = DiffeoStructure(dds=dds, **self.diffeo_structure_params) self.original_dds = dds dds_expanded = diffeosystem_expand(dds, pr=self.ds.get_plan_reducer(), heuristics=self.pre_expand) # TODO: recompute structure self.info("pre_expand: %s" % str(self.pre_expand)) self.info("Expanded to %s from %s actions" % (len(dds_expanded.actions), len(dds.actions))) super(InformedPlanner, self).init(id_dds, dds_expanded) def plan(self, *args, **kwargs): # we need to translate back the result according to the original plan result = super(InformedPlanner, self).plan(*args, **kwargs) if result.success: plan_extended = result.plan self.info("Solution found in extended space: %s" % str(plan_extended)) plan_simple = self.get_dds().plan_with_simple_actions(plan_extended) self.info("With simple actions: %s" % str(plan_simple)) result.plan = plan_simple return result @contract(report=Report) def init_report(self, report): """ Creates a report for the initialization phase. """ super(InformedPlanner, self).init_report(report) self.ds.display(report.section("diffeo_structure")) def get_plan_reducer(self): return self.ds.get_plan_reducer()
class DiffeoSystemBounds2: def __init__(self, id_dds, dds, tolerance, collapse_threshold, min_visibility, debug_it, max_it): self.dds = dds self.ds = DiffeoStructure(dds, tolerance) collapse_metric = DiffeoActionL2iwNormalized(self.ds) self.cover = DiffeoCoverExp(id_dds=id_dds, dds=self.dds, plan_reducer=self.ds.get_plan_reducer(), collapse_metric=collapse_metric, #collapse_threshold=collapse_threshold, collapse_threshold=0.001, max_depth=3, debug_iterations=debug_it, max_iterations=max_it) self.cover.set_min_visibility(min_visibility) self.cover.go() # #self.make_bases(self.dds, self.ds) def make_bases(self, dds, ds): n = len(dds.actions) # # Create all combinations of n of all elements # print('Creating all combinations of len %s' % n) # bplans = plans_of_max_length(n, n) # print('Created %d ' % len(bplans)) # minimal, mmap = ds.get_minimal_equiv_set(bplans) # print('Minimal %d ' % len(minimal)) ## print minimal # self.find_non_red_plans(nactions=n, length=3, threshold=0.05) self.make_closure(nactions=n, length=3, threshold=0.05) @dp_memoize_instance def plan_distance_norm(self, plan1, plan2): dn = self.cover.plan_distance(plan1, plan2, diffeoaction_distance_L2_infow) / self.ds.scalew return dn def minimum_dist_to_set(self, plan, plans): d = [self.plan_distance_norm(plan, p) for p in plans] i = np.argmin(d) return plans[i], min(d) def plan_inverse(self, plan): l = [self.ds.plan_reducer.action_get_inverse(a) for a in reversed(plan)] return tuple(l) def make_commutator(self, plan1, plan2): com = plan1 + plan2 + self.plan_inverse(plan1) + self.plan_inverse(plan2) com = self.ds.plan_reducer.get_canonical(com) return com def make_closure(self, nactions, length, threshold): if length == 1: return [(a,) for a in range(nactions)] + [()] prev = self.make_closure(nactions, length - 1, threshold) generated = [] for prev1, prev2 in itertools.product(prev, prev): if prev1 == prev2: continue com = self.make_commutator(prev1, prev2) #print('[%s, %s] -> %s ' % (prev1, prev2, com)) _, md = self.minimum_dist_to_set(com, prev + generated) #print('%s md %s to %s' % (com, md, closest)) if md < threshold: #print('%s matches %s' % (com, closest)) continue else: generated.append(com) print('Length %d generated %d:' % (length, len(generated))) for _, g in enumerate(generated): print(' - %s' % str(g)) return generated + prev def find_non_red_plans(self, nactions, length, threshold): if length == 0: return [()] prev = self.find_non_red_plans(nactions, length - 1, threshold) cur = [] cur.extend(prev) for p0 in prev: if len(p0) != length - 1: # only build in those from the previous level continue for action in range(nactions): if action in p0: # no repeated actions continue p1 = p0 + (action,) closest, md = self.minimum_dist_to_set(p1, cur) print('%s md %s to %s' % (p1, md, closest)) if md < threshold: print('%s matches %s' % (p1, closest)) continue else: cur.append(p1) print('Of length %d, found: %s' % (length, len(cur))) return cur def display(self, report): #@UnusedVariable with report.subsection('draw_graph') as r: self.cover.draw_graph(r) def display_products(self, report, nsteps): for a in self.dds_hard.actions: f = report.figure(a.label, cols=nsteps) A = a for k in range(nsteps): A = DiffeoAction.compose(A, a) rgb = A.get_diffeo2d_forward().get_rgb_info() f.data_rgb('%s_%s' % (a.label, k), rgb)