Archived
1
0
This repository has been archived on 2025-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
dlmo-model/pareto.py

54 lines
1.6 KiB
Python

import minizinc
def pareto_front(instance, objectives):
front = []
result = None
with instance.branch() as instance:
failed = False
while not failed:
result = instance.solve()
# print(result)
# print("----------------------------------------")
if result == None or result.status == minizinc.Status.UNSATISFIABLE:
failed = True
else:
to_remove = []
for i in range(len(front)):
if all([result[o] <= front[i][o] for o in objectives]):
to_remove.append(i)
# print(to_remove[::-1])
for r in to_remove[::-1]:
front.pop(r)
instance.add_string(
"constraint "
+ " \/ ".join([f"{o} < {result[o]}" for o in objectives])
+ ";\n"
)
front.append(result)
return front
if __name__ == "__main__":
inst = minizinc.Instance(
minizinc.Solver.lookup("chuffed"), minizinc.Model("dlmo.mzn")
)
inst.add_file("full.dzn")
front = pareto_front(inst, ["total_tests", "total_not_detected"])
for sol in front:
print(
",".join(
[
str(sol["total_tests"]),
str(sol["total_not_detected"]),
str(sol["first_sample"]),
str(sol["num_samples"]),
]
)
)
print("----------------------------------------------------")
for sol in front:
print(sol)