import os

import ctw.ctw_parser as ps
import solve_tw
import tw_utils

path = ""

instances = [
('pignet2-pp-minor-0009.dgf',8),
('celar03-pp2.dgf',14),
('link-pp-minor-037.dgf',13),
('munin4-pp-005.dgf',8),
('celar07-pp.dgf-007.dgf',5),
('water-wpp.dgf',9),
('celar08-pp-003.dgf',16),
('celar07-pp.dgf-006.dgf',5),
('BN_93-pp.dgf',22),
('pignet2-pp-minor-0012.dgf',11),
('link-pp-minor-045.dgf',13),
('pathfinder-pp.dgf',6),
('link-pp-minor-056.dgf',13),
('celar08pp-pp.dgf-034.dgf',16),
('pignet2-pp-minor-0011.dgf',10),
('BN_15-pp-minor-18.dgf',15),
('C5+.dgf',2),
('BN_14-pp-minor-19.dgf',14),
('munin2-pp-011.dgf',7),
('BN_15-pp-minor-13.dgf',12),
('link-pp-minor-063.dgf',13),
('link-pp-minor-015.dgf',12),
('celar06-wpp.dgf',11),
('link-pp-minor-016.dgf',12),
('pignet2-pp-minor-0014.dgf',13),
('1kth_graph.dimacs',20),
('link-pp-minor-052.dgf',13),
('celar08pp-pp.dgf-032.dgf',8),
('munin-kgo-pp-002.dgf',5),
('celar08pp-pp.dgf-016.dgf',4),
('celar06.dgf',11),
('bcs01.graph-005.dgf',13),
('ship-ship-wpp.dgf',8),
('BN_15-pp-minor-16.dgf',14),
('diabetes-pp-001.dgf',4),
('water-pp.dgf',9),
('BN_14-pp-minor-10.dgf',9),
('BN_14-pp-minor-09.dgf',8),
('celar03-pp2.dgf-010.dgf',4),
('BN_15-pp-minor-32.dgf',22),
('pignet2-pp-minor-0029.dgf',28),
('oesoca+-hugin-wpp.dgf',11),
('BN_87-pp.dgf',22),
('celar08pp-pp.dgf-015.dgf',4),
('BN_15-pp-minor-31.dgf',22),
('celar07-pp.dgf-014.dgf',11),
('celar07-pp.dgf-011.dgf',5),
('celar05-wpp.dgf-010.dgf',4),
('link-pp-minor-051.dgf',13),
('link-pp-minor-006.dgf',5),
('BN_14-pp-minor-24.dgf',17),
('celar08pp-pp.dgf-030.dgf',9),
('pignet2-pp-minor-0027.dgf',26),
('link-pp-minor-034.dgf',13),
('celar09-pp-002.dgf',16),
('link-pp-minor-013.dgf',11),
('munin2-pp-004.dgf',5),
('link-pp-minor-048.dgf',13),
('celar03-wpp.dgf',14),
('BN_95-pp.dgf',17),
('BN_14-pp-minor-05.dgf',4),
('link-pp-minor-008.dgf',7),
('celar07-pp.dgf-004.dgf',4),
('celar11-pp-003.dgf',15),
('ship-ship-pp-003.dgf',4),
('celar07-pp-002.dgf',7),
('celar07-pp.dgf-017.dgf',16),
('celar05-wpp.dgf',15),
('link-pp-minor-036.dgf',13),
('celar11-pp.dgf',15),
('oesoca+-pp.dgf',11),
('celar08pp-pp.dgf-018.dgf',4),
('link-pp-minor-009.dgf',8),
('celar03-pp2.dgf-008.dgf',4),
('pignet2-pp-minor-0017.dgf',16),
('anna-pp.dgf',12),
('munin2-pp-009.dgf',6),
('oow-solo-pp-001.dgf',6),
('link-pp-minor-064.dgf',13),
('celar06-pp.dgf',11),
('graph06pp-pp.dgf-001.dgf',4),
('celar05-pp-001.dgf',11),
('BN_15-pp-minor-21.dgf',17),
('link-pp-minor-032.dgf',13),
('link-pp-minor-043.dgf',13),
('celar05-pp-002.dgf',15),
('link-pp-minor-072.dgf',13),
('BN_15-pp-minor-22.dgf',18),
('celar03-pp2.dgf-006.dgf',4),
('BN_95.dgf',17),
('celar08pp-pp.dgf-010.dgf',4),
('celar08pp-pp.dgf-029.dgf',9),
('games120-pp.dgf-003.dgf',9),
('BN_14-pp-minor-11.dgf',10),
('weeduk.dgf',7),
    ('1fjl_graph-pp.dimacs-001.dgf',5),
('link-pp-minor-067.dgf',13),
('oow-solo-pp-002.dgf',6),
('myciel4.dgf',10),
('mildew-wpp.dgf',4),
('oesoca+-hugin-pp.dgf',11),
('celar08pp-pp.dgf-026.dgf',7),
('BN_15-pp-minor-15.dgf',14),
('barley-pp-001.dgf',7),
('celar03-pp2.dgf-005.dgf',4),
('BN_15-pp-minor-27.dgf',20),
('oow-trad-wpp.dgf',6),
('link-pp-minor-020.dgf',13),
('link-pp-minor-028.dgf',13),
('celar05-wpp.dgf-013.dgf',4),
('celar11-pp-004.dgf',6),
('celar08-pp-002.dgf',8),
('BN_14-pp-minor-07.dgf',6),
('1ubq.dgf',12),
('pignet2-pp-minor-0013.dgf',12),
('munin2-pp-006.dgf',5),
('pigs-pp-001.dgf',9),
('celar08pp-pp.dgf-025.dgf',6),
('celar07pp-pp.dgf-011.dgf',7),
('munin_kgo_complete-pp.dgf',5),
('link-pp-minor-079.dgf',13),
('link-pp-minor-068.dgf',13),
('1b0n_graph.dimacs-005.dgf',7),
('link-pp-minor-065.dgf',13),
('celar07-pp-001.dgf',16),
('munin1-wpp.dgf',11),
('BN_83-pp.dgf',20),
('wilson-hugin.dgf',3),
('1fjl_graph-pp.dimacs-002.dgf',7),
('celar08pp-pp.dgf-024.dgf',6),
('BN_92-pp.dgf',22),
('BN_15-pp-minor-26.dgf',19),
('munin2-pp-005.dgf',5),
('celar01-pp-003.dgf',15),
('barley-pp.dgf',7),
('celar08pp-pp.dgf-023.dgf',5),
('celar03-pp-001.dgf',14),
('celar05-wpp.dgf-008.dgf',4),
('1bx7_graph-pp.dimacs',11),
('oow-trad-pp.dgf',6),
('link-pp-minor-031.dgf',13),
('BN_91-pp.dgf',22),
('celar03-pp2.dgf-009.dgf',4),
('munin-kgo-pp-001.dgf',5),
('link-pp-minor-077.dgf',13),
('BN_14-pp-minor-28.dgf',18),
('BN_14-pp-minor-14.dgf',12),
('link-pp-minor-053.dgf',13),
('1en2_graph-pp.dimacs',16),
('1b0n_graph.dimacs-003.dgf',7),
('MCSTestGraph.dgf',3),
('link-pp-minor-075.dgf',13),
('BN_90-pp.dgf',22),
('link-pp-minor-019.dgf',13),
('link-pp-minor-078.dgf',13),
('link-pp-minor-066.dgf',13),
('graph06pp-pp.dgf-003.dgf',4),
('BN_14-pp-minor-08.dgf',7),
('celar08pp-pp.dgf-008.dgf',4),
('link-pp-minor-057.dgf',13),
('pr107.tsp-pp.dgf',7),
('1g6x_graph.dimacs',19),
('pignet2-pp-minor-0007.dgf',6),
('link-pp-minor-044.dgf',13),
('BN_15-pp-minor-25.dgf',19),
('MCSTestGraph2.dgf',3),
('BN_14-pp-minor-26.dgf',17),
('link-pp-minor-054.dgf',13),
('graph06pp-pp.dgf-004.dgf',6),
('pigs-pp.dgf',9),
('link-pp-minor-012.dgf',11),
('BN_16-pp.dgf-015.dgf',11),
('vsd-hugin.dgf',4),
('link-pp-minor-017.dgf',12),
('celar07pp-pp.dgf-012.dgf',16),
('BN_15-pp-minor-28.dgf',21),
('diabetes-pp-002.dgf',4),
('BN_86-pp.dgf',22),
('link-pp-minor-071.dgf',13),
('BN_14-pp-minor-30.dgf',19),
('munin4-pp-002.dgf',8),
('link-pp-minor-041.dgf',13),
('mainuk.dgf',7),
('link-pp-minor-080.dgf',13),
('munin4-pp-004.dgf',8),
('celar03-pp2.dgf-013.dgf',6),
('munin3-pp-002.dgf',5),
('celar07pp-pp.dgf-003.dgf',5),
('BN_15-pp-minor-20.dgf',16),
('pignet2-pp-minor-0010.dgf',9),
('myciel2.dgf',2),
('BN_78.dgf',13),
('oesoca+-hugin.dgf',11),
    ('1kth_graph-pp.dimacs',20),
('link-pp-minor-047.dgf',13),
('BN_14-pp-minor-21.dgf',16),
('link-pp-minor-005.dgf',4),
('celar09-pp-001.dgf',8),
('link-pp-minor-022.dgf',13),
('BN_15-pp-minor-29.dgf',21),
    ('1i07_graph.dimacs',15),
('celar05-wpp.dgf-014.dgf',4),
('munin3-pp.dgf',7),
('BN_28.dgf',5),
('munin2-pp-012.dgf',6),
('pignet2-pp-minor-0006.dgf',5),
('link-pp-minor-060.dgf',13),
('csf.dgf',6),
('link-pp-minor-046.dgf',13),
('BN_78-pp.dgf',13),
('munin1-pp-001.dgf',11),
('BN_15-pp-minor-23.dgf',18),
('BN_15-pp-minor-11.dgf',10),
('BN_80-pp.dgf',20),
('link-pp-minor-014.dgf',11),
('celar07pp-pp.dgf-006.dgf',5),
('barley.dgf',7),
('link-pp-minor-042.dgf',13),
('link-pp-minor-039.dgf',13),
('celar07pp-pp.dgf-002.dgf',4),
('oow-trad-pp-001.dgf',6),
('pignet2-pp-minor-0020.dgf',19),
('pignet2-pp-minor-0031.dgf',30),
('mainuk-pp.dgf',6),
('celar01-pp-001.dgf',15),
('celar05-wpp.dgf-009.dgf',4),
('link-pp-minor-023.dgf',13),
('pignet2-pp-minor-0022.dgf',21),
('BN_14-pp-minor-18.dgf',14),
('link-pp-minor-083.dgf',13),
('link-pp-minor-059.dgf',13),
('celar08pp-pp.dgf-022.dgf',5),
('celar08pp-pp.dgf-020.dgf',4),
('celar05-pp.dgf',15),
('BN_15-pp-minor-06.dgf',5),
('celar05-wpp.dgf-018.dgf',8),
('BN_89-pp.dgf',22),
('celar07-pp.dgf-015.dgf',11),
('oow-trad.dgf',6),
('munin4-pp-001.dgf',8),
('celar07pp-pp.dgf-010.dgf',4),
('celar07-pp.dgf-009.dgf',5),
('celar07pp-pp.dgf-009.dgf',6),
('link-pp-minor-038.dgf',13),
('celar04-pp-002.dgf',16),
('link-pp-minor-049.dgf',13),
('munin2-pp-001.dgf',4),
('celar08-pp-001.dgf',16),
('celar08pp-pp.dgf-033.dgf',16),
('alarm.dgf',4),
('celar02.dgf',10),
('BN_14-pp-minor-25.dgf',17),
('pignet2-pp-minor-0024.dgf',23),
('link-pp-minor-087.dgf',13),
('link-pp-minor-058.dgf',13),
('graph06pp-pp.dgf-002.dgf',4),
('link-pp-minor-040.dgf',13),
('myciel3.dgf',5),
('BN_14-pp-minor-27.dgf',18),
('bcs01.graph-003.dgf',4),
('pignet2-pp-minor-0030.dgf',29),
('celar05-wpp.dgf-016.dgf',5),
('link-pp-minor-011.dgf',10),
('celar05-wpp.dgf-017.dgf',7),
('celar07-pp.dgf-005.dgf',4),
('celar08pp-pp.dgf-019.dgf',4),
('celar07pp-pp.dgf-004.dgf',5),
('celar08pp-pp.dgf-013.dgf',4),
('link-pp-minor-061.dgf',13),
('link-pp-minor-069.dgf',13),
('celar05-wpp.dgf-012.dgf',4),
('ship-ship-pp-002.dgf',8),
('celar07-pp.dgf-010.dgf',5),
('bcs01.graph-002.dgf',4),
('link-pp-minor-074.dgf',13),
('fungiuk.dgf',4),
('link-pp-minor-073.dgf',13),
('oesoca.dgf',3),
('BN_15-pp-minor-12.dgf',11),
('bcs01-pp.dgf',13),
('bcs01.graph-004.dgf',4),
('pignet2-pp-minor-0021.dgf',20),
('lin105.tsp-pp.dgf',9),
('oesoca42.dgf',3),
    ('1ubq-pp.dgf',12),
('BN_79-pp.dgf',13),
('1igq_graph-pp.dimacs',23),
('pignet2-pp-minor-0032.dgf',31),
('ship-ship-pp.dgf',8),
('celar03-pp2.dgf-015.dgf',14),
('petersen.dgf',4),
('BN_14-pp-minor-16.dgf',13),
('link-pp-minor-007.dgf',6),
('pignet2-pp-minor-0023.dgf',22),
('BN_14-pp-minor-17.dgf',13),
('pignet2-pp-minor-0005.dgf',4),
('oow_solo-wpp.dgf',6),
('BN_14-pp-minor-20.dgf',15),
('celar07-pp.dgf-016.dgf',7),
('1en2_graph.dimacs',16),
('BN_14-pp-minor-15.dgf',12),
('celar07-pp.dgf-012.dgf',4),
('munin2-pp-003.dgf',5),
('celar08pp-pp.dgf-021.dgf',4),
('celar07-pp.dgf-008.dgf',5),
('barley-wpp.dgf',7),
('celar08pp-pp.dgf-027.dgf',8),
('1b0n_graph.dimacs-001.dgf',6),
('celar11-pp-001.dgf',10),
('BN_88-pp.dgf',22),
('pignet2-pp-minor-0019.dgf',18),
('celar08pp-pp.dgf-007.dgf',4),
('celar01-pp-002.dgf',10),
('BN_14-pp-minor-22.dgf',16),
('1fjl_graph-pp.dimacs-003.dgf',8),
('BN_15-pp-minor-17.dgf',15),
('celar08pp-pp.dgf-012.dgf',4),
('link-pp-minor-055.dgf',13),
('celar10-pp-001.dgf',8),
('munin4-pp-003.dgf',8),
('link-pp-minor-024.dgf',13),
('BN_14-pp-minor-13.dgf',11),
('link-pp-minor-025.dgf',13),
('link-pp-minor-081.dgf',13),
('link-pp-minor-085.dgf',13),
('pignet2-pp-minor-0028.dgf',27),
('BN_94.dgf',17),
('1b0n_graph.dimacs-002.dgf',7),
('rat99.tsp-pp.dgf',9),
('BN_15-pp-minor-10.dgf',9),
('celar03-pp2.dgf-012.dgf',5),
('link-pp-minor-026.dgf',13),
('celar07pp-pp.dgf-005.dgf',5),
('celar05-wpp.dgf-019.dgf',11),
('munin3-pp-003.dgf',7),
('BN_29.dgf',5),
('celar08pp-pp.dgf-009.dgf',4),
('pignet2-pp-minor-0016.dgf',15),
('celar07-pp.dgf-013.dgf',6),
('oow_bas.dgf',4),
('bcs01.graph',13),
('celar08pp-pp.dgf-017.dgf',4),
('link-pp-minor-033.dgf',13),
('BN_15-pp-minor-30.dgf',22),
('eil51.tsp.dgf',8),
('munin2-pp-002.dgf',4),
('celar03-pp2.dgf-011.dgf',4),
('oow_solo.dgf',6),
('BN_15-pp-minor-09.dgf',8),
('BN_82-pp.dgf',20),
('pignet2-pp-minor-0008.dgf',7),
('celar02-pp.dgf',10),
('bcs01.graph-001.dgf',4),
('celar02-wpp.dgf',10),
('celar03-pp2.dgf-007.dgf',4),
('link-pp-minor-070.dgf',13),
('1b0n_graph.dimacs-004.dgf',7),
('link-pp-minor-010.dgf',9),
('celar07pp-pp.dgf-007.dgf',5),
('BN_79.dgf',13),
('celar08pp-pp.dgf-031.dgf',9),
('celar04-pp-001.dgf',10),
('link-pp-minor-030.dgf',13),
('C5.dgf',2),
('BN_15-pp-minor-07.dgf',6),
('oow_solo-pp.dgf',6),
('munin2-pp-007.dgf',7),
('BN_94-pp.dgf',17),
('celar11-pp-002.dgf',7),
('celar06pp.dgf',11),
('munin3-pp-001.dgf',5),
('link-pp-minor-082.dgf',13),
('link-pp-minor-027.dgf',13),
('celar05-wpp.dgf-011.dgf',4),
('celar10-pp-002.dgf',16),
('celar08pp-pp.dgf-028.dgf',9),
('BN_81-pp.dgf',20),
('link-pp-minor-062.dgf',13),
('BN_14-pp-minor-29.dgf',19),
('celar05-wpp.dgf-015.dgf',5),
('pignet2-pp-minor-0018.dgf',17),
('oow_bas-wpp.dgf',4),
('link-pp-minor-029.dgf',13),
('oesoca+.dgf',11),
('BN_15-pp-minor-05.dgf',4),
('BN_85-pp.dgf',20),
('link-pp-minor-050.dgf',13),
('BN_15-pp-minor-14.dgf',13),
('munin2-pp-008.dgf',7),
('celar03-pp2.dgf-014.dgf',7),
('celar03-pp.dgf',14),
('link-pp-minor-021.dgf',13),
('BN_15-pp-minor-08.dgf',7),
('link-pp-minor-076.dgf',13),
('BN_14-pp-minor-12.dgf',10),
('celar07pp-pp.dgf-008.dgf',5),
('BN_15-pp-minor-19.dgf',16),
('pignet2-pp-minor-0025.dgf',24),
('munin4-pp-006.dgf',8),
('ship-ship-pp-001.dgf',4),
('munin1-pp.dgf',11),
('ship-ship.dgf',8),
('celar08pp-pp.dgf-011.dgf',4),
('BN_84-pp.dgf',20),
('pignet2-pp-minor-0026.dgf',25),
('BN_15-pp-minor-24.dgf',18),
('celar11-wpp.dgf',15),
('link-pp-minor-084.dgf',13),
('link-pp-minor-018.dgf',12),
('celar08pp-pp.dgf-014.dgf',4),
('water-pp-001.dgf',9),
('munin2-pp-010.dgf',7),
('water.dgf',9),
('pignet2-pp-minor-0015.dgf',14),
('BN_14-pp-minor-23.dgf',16),
('mildew.dgf',4),
('BN_14-pp-minor-06.dgf',5),
('celar05-wpp.dgf-020.dgf',15),
('1bx7_graph.dimacs',11),
('link-pp-minor-035.dgf',13),
('david-pp.dgf',13),
('munin_kgo_complete-wpp.dgf',6),
('celar07pp-pp.dgf',16),
('1i07_graph-pp.dimacs',15)
    ]

filename = f"{os.getpid()}_encoding.txt"
fileout = f"{os.getpid()}_result.txt"

results = []
for name, tw in instances:
    g, _ = ps.parse(os.path.join(path, name))
    _, ordering = solve_tw.solve(g, filename, fileout, timeout=3600, target_tw=tw)

    bags, _, _ = tw_utils.ordering_to_decomp(g, ordering)

    _, cv1 = ps.parse(os.path.join(path, name + ".1"))
    _, cv2 = ps.parse(os.path.join(path, name + ".05"))

    c1 = max(len(b & cv1) for b in bags.values())
    c2 = max(len(b & cv2) for b in bags.values())
    results.append((name, c1, c2))

    print(results[-1])

for name, c1, c2 in results:
    print(f"{name};{c1}:{c2}")