Reformulation¶
- class formulation_bench.reformulation.Reformulation(a, b, is_reformulation)[source]¶
A pair of MILP formulations with a reformulation label.
Consists of two MILP formulations
aandband a booleanis_reformulationlabel indicating whetherbis a reformulation ofa. The formal definition of reformulation is given in Reformulation. Positive entries (is_reformulation=True) are accompanied by a Lean 4 proof whose path is accessible via thelean_proof_pathattribute; negative entries have no proof andlean_proof_pathresolves toNone.- Attributes:
- a
Formulation The base formulation.
- b
Formulation The reformulation candidate.
- is_reformulationbool
Trueiffbis a reformulation ofa.- path
pathlib.Path Resolved absolute path to this pair’s directory.
- lean_proof_path
pathlib.PathorNone For positive entries, the path to the accompanying Lean 4 proof file. For negative entries,
Nonesince no proof exists.- parameter_map
ParameterMap The loaded
map.json, which states how each parameter ofbis computed from the parameters ofa.
- a
Examples
Formulation
bof p12 | Traveling Salesman Problem (TSP) is a reformulation of formulationa:>>> from formulation_bench import Dataset >>> ds = Dataset("dataset") >>> reform = ds.reformulations[73] # corresponds to p12.a -> p12.b >>> reform.a.problem.name 'Traveling Salesman Problem (TSP)' >>> reform.b.problem.name 'Traveling Salesman Problem (TSP)' >>> reform.b.constraints[-1].description # cutting plane added by p12.b 'Depot-Exit Position Bound (EC1)...' >>> reform.is_reformulation True
- gen_map_py()[source]¶
Generate a Python script computing
b’s parameters froma’s.The script is generated from the
pythoncode snippets of this pair’smap.json. The resulting script takes the path toa’sparameters.jsonand the path to writeb’s as positional arguments.Examples
Generate the parameter-map script for the
atobpair of p1 | Amusement Park Ticket Machines, whose parameters are a renaming:>>> from formulation_bench import Dataset >>> ds = Dataset("dataset") >>> reform = ds.reformulations[0] # corresponds to p1.a -> p1.b >>> script = reform.gen_map_py() >>> print(script) import argparse import json def main(params_path: str, output_path: str) -> None: with open(params_path, "r") as f: data = json.load(f) # Source Parameters CashMachineProcessingRate = data["CashMachineProcessingRate"] ... # Parameter Map A = CashMachineProcessingRate ...
- run_map(input_path=None, output_path=None)[source]¶
Write this pair’s
map.pyand run it.The script generated by
gen_map_py()is written tomap.pyin the pair’s directory, then applied toa’s parameters to produceb’s.- Parameters:
- input_path
strorpathlib.Path, optional Path to a
parameters.jsonholding formulationa’s parameters. Defaults toparameters.jsonina’s formulation directory.- output_path
strorpathlib.Path, optional Path to write the mapped parameters. Defaults to
parameters.jsonin this pair’s directory.
- input_path
Examples
Map formulation
a’s parameters to formulationb’s for p1 | Amusement Park Ticket Machines:>>> import json >>> from formulation_bench import Dataset >>> ds = Dataset("dataset") >>> reform = ds.reformulations[0] # corresponds to p1.a -> p1.b >>> reform.a.run_gen_params() >>> params = json.load(open(reform.a.path / "parameters.json")) >>> params["CashMachineProcessingRate"] # a's parameter 20 >>> reform.run_map() >>> params = json.load(open(reform.path / "parameters.json")) >>> params["A"] # b's name for "CashMachineProcessingRate" 20
- class formulation_bench.models.ParameterMap(parameters, definitions, metadata)[source]¶
A mapping from one formulation’s parameters to another’s.
Loaded from the
map.jsonof a reformulation pair. See map.json for the file schema.- Attributes:
- parameters
dict[str,Expression] One entry per parameter of the target formulation, computing it from the source formulation’s parameters: An entry may reference any parameter defined before it.
- definitions
dict[str,Expression] Optional intermediate quantities computed before the parameters. Used when several parameters share a derivation.
- metadata
dict[str,Any] Free-form metadata about the map. Typically a
notesfield.
- parameters
- render_markdown()[source]¶
Render this parameter map in Markdown.
The output is produced by rendering the following Jinja template. The
notespassed to this template are themetadata.notesof the map.# Parameter Map {% for note in notes %} {{ note }} {% endfor %} {% if definitions %} ## Definitions {% for name, d in definitions.items() %} - **{{ name }}** $${{ d.formulation }}$$ {% endfor %} {% endif %} ## Parameters {% for name, d in parameters.items() %} - **{{ name }}** $${{ d.formulation }}$$ {% endfor %}
- Returns:
- markdown
str The rendered Markdown string.
- markdown
Examples
Render the map carrying formulation
aof p12 | Traveling Salesman Problem (TSP) to formulationb:>>> from formulation_bench import Dataset >>> ds = Dataset("dataset") >>> pmap = ds.reformulations[73].parameter_map >>> print(pmap.render_markdown()) # Parameter Map Formulation `b` has the same parameters as formulation `a`; the map is the identity. ## Parameters - **n** $$n = n$$ - **c** $$c_{ij} = c_{ij}$$