def q2(): a = View("a", 100) b = View("b", 50) c = View("c", 75) d = View("d", 20) e = View("e", 30) f = View("f", 40) g = View("g", 1) h = View("h", 10) a.add_children([b, c]) b.add_children([d, e]) c.add_children([e, f]) d.add_children([g]) e.add_children([g, h]) f.add_children([h]) greedy = GreedySelect(root=a, views=[a, b, c, d, e, f, g, h]) views = greedy.run(3)
max_gain = gain max_view = v assert max_view is not None s.append(max_view) gains = [str(g) for g in gains] print(f"Choice {i + 1}") print([str(v) for v in print_views]) print(gains) print(f"Select: {max_view.name}") print("==========================") return s if __name__ == '__main__': a = View("a", 200) b = View("b", 100) c = View("c", 150) d = View("d", 120) e = View("e", 80) f = View("f", 70) g = View("g", 55) h = View("h", 90) i = View("i", 40) j = View("j", 10) k = View("k", 15) l = View("l", 20) m = View("m", 5) a.add_children([b, c, d]) b.add_children([e, f, g])
def test_one(self): psc = View("psc", 6, is_materialized=True) pc = View("pc", 4) ps = View("ps", 0.8) sc = View("sc", 2) p = View("p", 0.2) s = View("s", 0.01) c = View("c", 0.1) psc.add_children([pc, ps, sc]) pc.add_children([p, c]) ps.add_children([p, s]) sc.add_children([s, c]) self.assertEqual(psc.cost, 6) self.assertEqual(pc.cost, 6) self.assertEqual(ps.cost, 6) self.assertEqual(sc.cost, 6) self.assertEqual(p.cost, 6) self.assertEqual(s.cost, 6) self.assertEqual(c.cost, 6) sc.is_materialized = True self.assertEqual(psc.cost, 6) self.assertEqual(pc.cost, 6) self.assertEqual(ps.cost, 6) self.assertEqual(sc.cost, 2) self.assertEqual(p.cost, 6) self.assertEqual(s.cost, 2) self.assertEqual(c.cost, 2)
def test_view_gain2(self): psc = View("psc", 6, is_materialized=True) pc = View("pc", 4) ps = View("ps", 0.8) sc = View("sc", 2) p = View("p", 0.2) s = View("s", 0.01) c = View("c", 0.1) psc.add_children([pc, ps, sc]) pc.add_children([p, c]) ps.add_children([p, s]) sc.add_children([s, c]) view_gain = ViewGain(root=psc) gain = view_gain.calculate_gain([sc, psc, ps], [psc]) self.assertEqual(gain, 23.6)
return ViewGain.calculate_total_cost_util(self.root, []) @staticmethod def calculate_total_cost_util(view: "View", calculated_view: List["View"]) -> float: total_cost = 0 for c in view.children: if c not in calculated_view: total_cost += ViewGain.calculate_total_cost_util(c, calculated_view) calculated_view.append(c) total_cost += view.cost return total_cost if __name__ == '__main__': psc = View("psc", 6) pc = View("pc", 4) ps = View("ps", 0.8) sc = View("sc", 2) p = View("p", 0.2) s = View("s", 0.01) c = View("c", 0.1) psc.add_children([pc, ps, sc]) pc.add_children([p, c]) ps.add_children([p, s]) sc.add_children([s, c]) view_gain = ViewGain(root=psc) gain = view_gain.calculate_gain([sc, psc], [psc]) print(gain)