mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
LearningSolver: Keep original H5 file unmodified
This commit is contained in:
11
CHANGELOG.md
11
CHANGELOG.md
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [0.4.0] - Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Add ML strategies for user cuts
|
||||||
|
- Add ML strategies for lazy constraints
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- LearningSolver.solve no longer generates HDF5 files; use a collector instead.
|
||||||
|
|
||||||
## [0.3.0] - 2023-06-08
|
## [0.3.0] - 2023-06-08
|
||||||
|
|
||||||
This is a complete rewrite of the original prototype package, with an entirely new API, focused on performance, scalability and flexibility.
|
This is a complete rewrite of the original prototype package, with an entirely new API, focused on performance, scalability and flexibility.
|
||||||
|
|||||||
@@ -695,7 +695,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Accessing the solution\n",
|
"## Accessing the solution\n",
|
||||||
"\n",
|
"\n",
|
||||||
"In the example above, we used `LearningSolver.solve` together with data files to solve both the training and the test instances. The optimal solutions were saved to HDF5 files in the train/test folders, and could be retrieved by reading theses files, but that is not very convenient. In the following example, we show how to build and solve a Pyomo model entirely in-memory, using our trained solver."
|
"In the example above, we used `LearningSolver.solve` together with data files to solve both the training and the test instances. In the following example, we show how to build and solve a Pyomo model entirely in-memory, using our trained solver."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -592,7 +592,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Accessing the solution\n",
|
"## Accessing the solution\n",
|
||||||
"\n",
|
"\n",
|
||||||
"In the example above, we used `LearningSolver.solve` together with data files to solve both the training and the test instances. The optimal solutions were saved to HDF5 files in the train/test folders, and could be retrieved by reading theses files, but that is not very convenient. In the following example, we show how to build and solve a JuMP model entirely in-memory, using our trained solver."
|
"In the example above, we used `LearningSolver.solve` together with data files to solve both the training and the test instances. In the following example, we show how to build and solve a JuMP model entirely in-memory, using our trained solver."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -712,7 +712,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Accessing the solution\n",
|
"## Accessing the solution\n",
|
||||||
"\n",
|
"\n",
|
||||||
"In the example above, we used `LearningSolver.solve` together with data files to solve both the training and the test instances. The optimal solutions were saved to HDF5 files in the train/test folders, and could be retrieved by reading theses files, but that is not very convenient. In the following example, we show how to build and solve a Pyomo model entirely in-memory, using our trained solver."
|
"In the example above, we used `LearningSolver.solve` together with data files to solve both the training and the test instances. In the following example, we show how to build and solve a Pyomo model entirely in-memory, using our trained solver."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from typing import List, Any, Union, Dict, Callable, Optional
|
|||||||
from miplearn.h5 import H5File
|
from miplearn.h5 import H5File
|
||||||
from miplearn.io import _to_h5_filename
|
from miplearn.io import _to_h5_filename
|
||||||
from miplearn.solvers.abstract import AbstractModel
|
from miplearn.solvers.abstract import AbstractModel
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
|
||||||
class LearningSolver:
|
class LearningSolver:
|
||||||
@@ -25,15 +26,20 @@ class LearningSolver:
|
|||||||
model: Union[str, AbstractModel],
|
model: Union[str, AbstractModel],
|
||||||
build_model: Optional[Callable] = None,
|
build_model: Optional[Callable] = None,
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
|
h5_filename, mode = NamedTemporaryFile().name, "w"
|
||||||
if isinstance(model, str):
|
if isinstance(model, str):
|
||||||
h5_filename = _to_h5_filename(model)
|
|
||||||
assert build_model is not None
|
assert build_model is not None
|
||||||
|
old_h5_filename = _to_h5_filename(model)
|
||||||
model = build_model(model)
|
model = build_model(model)
|
||||||
assert isinstance(model, AbstractModel)
|
assert isinstance(model, AbstractModel)
|
||||||
else:
|
|
||||||
h5_filename = NamedTemporaryFile().name
|
# If the instance has an associate H5 file, we make a temporary copy of it,
|
||||||
|
# then work on that copy. We keep the original file unmodified
|
||||||
|
if exists(old_h5_filename):
|
||||||
|
shutil.copy(old_h5_filename, h5_filename)
|
||||||
|
mode = "r+"
|
||||||
|
|
||||||
stats: Dict[str, Any] = {}
|
stats: Dict[str, Any] = {}
|
||||||
mode = "r+" if exists(h5_filename) else "w"
|
|
||||||
with H5File(h5_filename, mode) as h5:
|
with H5File(h5_filename, mode) as h5:
|
||||||
model.extract_after_load(h5)
|
model.extract_after_load(h5)
|
||||||
if not self.skip_lp:
|
if not self.skip_lp:
|
||||||
|
|||||||
Reference in New Issue
Block a user