Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for combining different diversity measures and constraint… #189

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/minizinc/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _add_diversity_to_opt_model(
def _add_diversity_to_div_model(
inst: minizinc.Instance,
vars: List[Dict[str, Any]],
obj_sense: str,
div_anns: Dict[str, Any],
gap: Union[int, float],
sols: Dict[str, Any],
):
Expand Down Expand Up @@ -190,15 +190,38 @@ def _add_diversity_to_div_model(
f"array [1..{len(prevsol)}] of var {varprevtype}: dist_{varname} :: output = [{distfun}({varname}, {varprevname}[sol,{dotdots}]) | sol in 1..{len(prevsol)}];\n"
)

# Add minimum distance to the diversity distance measurement in the model code
if var["lb"] != "infinity":
inst.add_string(
f"constraint forall(sol in 1..{len(prevsol)})( dist_{varname}[sol] >= {var['lb']});"
)

# Add maximum distance to the diversity distance measurement in the model code
if var["ub"] != "infinity":
inst.add_string(
f"constraint forall(sol in 1..{len(prevsol)})( dist_{varname}[sol] <= {var['ub']});"
)

obj_sense = div_anns["objective"]["sense"]
aggregator = (
div_anns["aggregator"] if div_anns["aggregator"] != "" else "sum"
)
combinator = (
div_anns["combinator"] if div_anns["combinator"] != "" else "sum"
)

# Add the bound on the objective.
if obj_sense == "-1":
inst.add_string(f"constraint div_orig_objective <= {gap};\n")
elif obj_sense == "1":
inst.add_string(f"constraint div_orig_objective >= {gap};\n")

# Add new objective: maximize diversity.
dist_sum = "+".join([f'sum(dist_{var["name"]})' for var in vars])
inst.add_string(f"solve maximize {dist_sum};\n")
div_combinator = ", ".join(
[f'{var["coef"]} * dist_{var["name"]}[sol]' for var in vars]
)
dist_total = f"{aggregator}([{combinator}([{div_combinator}]) | sol in 1..{len(prevsol)}])"
inst.add_string(f"solve maximize {dist_total};\n")

return inst

Expand Down
10 changes: 6 additions & 4 deletions src/minizinc/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,11 @@ async def diverse_solutions(
with inst.branch() as child:
# Add constraints to the model that sets the decision variables to the reference solution, if provided
if reference_solution:
if isinstance(reference_solution, Result) and is_dataclass(
reference_solution.solution
) and not isinstance(reference_solution.solution, type):
if (
isinstance(reference_solution, Result)
and is_dataclass(reference_solution.solution)
and not isinstance(reference_solution.solution, type)
):
solution_obj = asdict(reference_solution.solution)
else:
assert isinstance(reference_solution, dict)
Expand Down Expand Up @@ -392,7 +394,7 @@ async def diverse_solutions(
child = _add_diversity_to_div_model(
child,
variables,
obj_anns["sense"],
div_anns,
max_gap,
prev_solutions,
)
Expand Down