|
|
|
@ -25,44 +25,7 @@ class Component(EnforceOverrides):
|
|
|
|
|
strategy.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def before_solve_lp_old(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
|
instance: Instance,
|
|
|
|
|
model: Any,
|
|
|
|
|
stats: LearningSolveStats,
|
|
|
|
|
features: Features,
|
|
|
|
|
training_data: TrainingSample,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Method called by LearningSolver before the root LP relaxation is solved.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
solver: LearningSolver
|
|
|
|
|
The solver calling this method.
|
|
|
|
|
instance: Instance
|
|
|
|
|
The instance being solved.
|
|
|
|
|
model
|
|
|
|
|
The concrete optimization model being solved.
|
|
|
|
|
stats: LearningSolveStats
|
|
|
|
|
A dictionary containing statistics about the solution process, such as
|
|
|
|
|
number of nodes explored and running time. Components are free to add
|
|
|
|
|
their own statistics here. For example, PrimalSolutionComponent adds
|
|
|
|
|
statistics regarding the number of predicted variables. All statistics in
|
|
|
|
|
this dictionary are exported to the benchmark CSV file.
|
|
|
|
|
features: miplearn.features.Features
|
|
|
|
|
Features describing the model.
|
|
|
|
|
training_data: TrainingSample
|
|
|
|
|
A dictionary containing data that may be useful for training machine
|
|
|
|
|
learning models and accelerating the solution process. Components are
|
|
|
|
|
free to add their own training data here. For example,
|
|
|
|
|
PrimalSolutionComponent adds the current primal solution. The data must
|
|
|
|
|
be pickable.
|
|
|
|
|
"""
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def before_solve_lp(
|
|
|
|
|
def after_solve_lp(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
|
instance: Instance,
|
|
|
|
@ -71,26 +34,8 @@ class Component(EnforceOverrides):
|
|
|
|
|
sample: Sample,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Method called by LearningSolver before the root LP relaxation is solved.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
solver: LearningSolver
|
|
|
|
|
The solver calling this method.
|
|
|
|
|
instance: Instance
|
|
|
|
|
The instance being solved.
|
|
|
|
|
model
|
|
|
|
|
The concrete optimization model being solved.
|
|
|
|
|
stats: LearningSolveStats
|
|
|
|
|
A dictionary containing statistics about the solution process, such as
|
|
|
|
|
number of nodes explored and running time. Components are free to add
|
|
|
|
|
their own statistics here. For example, PrimalSolutionComponent adds
|
|
|
|
|
statistics regarding the number of predicted variables. All statistics in
|
|
|
|
|
this dictionary are exported to the benchmark CSV file.
|
|
|
|
|
sample: miplearn.features.Sample
|
|
|
|
|
An object containing data that may be useful for training machine
|
|
|
|
|
learning models and accelerating the solution process. Components are
|
|
|
|
|
free to add their own training data here.
|
|
|
|
|
Method called by LearningSolver after the root LP relaxation is solved.
|
|
|
|
|
See before_solve_lp for a description of the parameters.
|
|
|
|
|
"""
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
@ -109,7 +54,7 @@ class Component(EnforceOverrides):
|
|
|
|
|
"""
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def after_solve_lp(
|
|
|
|
|
def after_solve_mip(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
|
instance: Instance,
|
|
|
|
@ -118,12 +63,12 @@ class Component(EnforceOverrides):
|
|
|
|
|
sample: Sample,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Method called by LearningSolver after the root LP relaxation is solved.
|
|
|
|
|
Method called by LearningSolver after the MIP is solved.
|
|
|
|
|
See before_solve_lp for a description of the parameters.
|
|
|
|
|
"""
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def before_solve_mip_old(
|
|
|
|
|
def after_solve_mip_old(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
|
instance: Instance,
|
|
|
|
@ -133,12 +78,12 @@ class Component(EnforceOverrides):
|
|
|
|
|
training_data: TrainingSample,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Method called by LearningSolver before the MIP is solved.
|
|
|
|
|
Method called by LearningSolver after the MIP is solved.
|
|
|
|
|
See before_solve_lp for a description of the parameters.
|
|
|
|
|
"""
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def before_solve_mip(
|
|
|
|
|
def before_solve_lp(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
|
instance: Instance,
|
|
|
|
@ -147,12 +92,30 @@ class Component(EnforceOverrides):
|
|
|
|
|
sample: Sample,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Method called by LearningSolver before the MIP is solved.
|
|
|
|
|
See before_solve_lp for a description of the parameters.
|
|
|
|
|
Method called by LearningSolver before the root LP relaxation is solved.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
solver: LearningSolver
|
|
|
|
|
The solver calling this method.
|
|
|
|
|
instance: Instance
|
|
|
|
|
The instance being solved.
|
|
|
|
|
model
|
|
|
|
|
The concrete optimization model being solved.
|
|
|
|
|
stats: LearningSolveStats
|
|
|
|
|
A dictionary containing statistics about the solution process, such as
|
|
|
|
|
number of nodes explored and running time. Components are free to add
|
|
|
|
|
their own statistics here. For example, PrimalSolutionComponent adds
|
|
|
|
|
statistics regarding the number of predicted variables. All statistics in
|
|
|
|
|
this dictionary are exported to the benchmark CSV file.
|
|
|
|
|
sample: miplearn.features.Sample
|
|
|
|
|
An object containing data that may be useful for training machine
|
|
|
|
|
learning models and accelerating the solution process. Components are
|
|
|
|
|
free to add their own training data here.
|
|
|
|
|
"""
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def after_solve_mip_old(
|
|
|
|
|
def before_solve_lp_old(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
|
instance: Instance,
|
|
|
|
@ -162,12 +125,34 @@ class Component(EnforceOverrides):
|
|
|
|
|
training_data: TrainingSample,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Method called by LearningSolver after the MIP is solved.
|
|
|
|
|
See before_solve_lp for a description of the parameters.
|
|
|
|
|
Method called by LearningSolver before the root LP relaxation is solved.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
solver: LearningSolver
|
|
|
|
|
The solver calling this method.
|
|
|
|
|
instance: Instance
|
|
|
|
|
The instance being solved.
|
|
|
|
|
model
|
|
|
|
|
The concrete optimization model being solved.
|
|
|
|
|
stats: LearningSolveStats
|
|
|
|
|
A dictionary containing statistics about the solution process, such as
|
|
|
|
|
number of nodes explored and running time. Components are free to add
|
|
|
|
|
their own statistics here. For example, PrimalSolutionComponent adds
|
|
|
|
|
statistics regarding the number of predicted variables. All statistics in
|
|
|
|
|
this dictionary are exported to the benchmark CSV file.
|
|
|
|
|
features: miplearn.features.Features
|
|
|
|
|
Features describing the model.
|
|
|
|
|
training_data: TrainingSample
|
|
|
|
|
A dictionary containing data that may be useful for training machine
|
|
|
|
|
learning models and accelerating the solution process. Components are
|
|
|
|
|
free to add their own training data here. For example,
|
|
|
|
|
PrimalSolutionComponent adds the current primal solution. The data must
|
|
|
|
|
be pickable.
|
|
|
|
|
"""
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def after_solve_mip(
|
|
|
|
|
def before_solve_mip(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
|
instance: Instance,
|
|
|
|
@ -176,56 +161,44 @@ class Component(EnforceOverrides):
|
|
|
|
|
sample: Sample,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Method called by LearningSolver after the MIP is solved.
|
|
|
|
|
Method called by LearningSolver before the MIP is solved.
|
|
|
|
|
See before_solve_lp for a description of the parameters.
|
|
|
|
|
"""
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def sample_xy_old(
|
|
|
|
|
def before_solve_mip_old(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
|
instance: Instance,
|
|
|
|
|
sample: TrainingSample,
|
|
|
|
|
) -> Tuple[Dict, Dict]:
|
|
|
|
|
"""
|
|
|
|
|
Returns a pair of x and y dictionaries containing, respectively, the matrices
|
|
|
|
|
of ML features and the labels for the sample. If the training sample does not
|
|
|
|
|
include label information, returns (x, {}).
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def sample_xy(
|
|
|
|
|
self,
|
|
|
|
|
instance: Optional[Instance],
|
|
|
|
|
sample: Sample,
|
|
|
|
|
) -> Tuple[Dict, Dict]:
|
|
|
|
|
model: Any,
|
|
|
|
|
stats: LearningSolveStats,
|
|
|
|
|
features: Features,
|
|
|
|
|
training_data: TrainingSample,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Returns a pair of x and y dictionaries containing, respectively, the matrices
|
|
|
|
|
of ML features and the labels for the sample. If the training sample does not
|
|
|
|
|
include label information, returns (x, {}).
|
|
|
|
|
Method called by LearningSolver before the MIP is solved.
|
|
|
|
|
See before_solve_lp for a description of the parameters.
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def xy_instances_old(
|
|
|
|
|
self,
|
|
|
|
|
instances: List[Instance],
|
|
|
|
|
) -> Tuple[Dict, Dict]:
|
|
|
|
|
x_combined: Dict = {}
|
|
|
|
|
y_combined: Dict = {}
|
|
|
|
|
def evaluate_old(self, instances: List[Instance]) -> List:
|
|
|
|
|
ev = []
|
|
|
|
|
for instance in instances:
|
|
|
|
|
instance.load()
|
|
|
|
|
for sample in instance.training_data:
|
|
|
|
|
xy = self.sample_xy_old(instance, sample)
|
|
|
|
|
if xy is None:
|
|
|
|
|
continue
|
|
|
|
|
x_sample, y_sample = xy
|
|
|
|
|
for cat in x_sample.keys():
|
|
|
|
|
if cat not in x_combined:
|
|
|
|
|
x_combined[cat] = []
|
|
|
|
|
y_combined[cat] = []
|
|
|
|
|
x_combined[cat] += x_sample[cat]
|
|
|
|
|
y_combined[cat] += y_sample[cat]
|
|
|
|
|
ev += [self.sample_evaluate_old(instance, sample)]
|
|
|
|
|
instance.free()
|
|
|
|
|
return x_combined, y_combined
|
|
|
|
|
return ev
|
|
|
|
|
|
|
|
|
|
def fit(
|
|
|
|
|
self,
|
|
|
|
|
training_instances: List[Instance],
|
|
|
|
|
) -> None:
|
|
|
|
|
x, y = self.xy_instances(training_instances)
|
|
|
|
|
for cat in x.keys():
|
|
|
|
|
x[cat] = np.array(x[cat])
|
|
|
|
|
y[cat] = np.array(y[cat])
|
|
|
|
|
self.fit_xy(x, y)
|
|
|
|
|
|
|
|
|
|
def fit_old(
|
|
|
|
|
self,
|
|
|
|
@ -286,6 +259,37 @@ class Component(EnforceOverrides):
|
|
|
|
|
) -> None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def sample_evaluate_old(
|
|
|
|
|
self,
|
|
|
|
|
instance: Instance,
|
|
|
|
|
sample: TrainingSample,
|
|
|
|
|
) -> Dict[Hashable, Dict[str, float]]:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
def sample_xy(
|
|
|
|
|
self,
|
|
|
|
|
instance: Optional[Instance],
|
|
|
|
|
sample: Sample,
|
|
|
|
|
) -> Tuple[Dict, Dict]:
|
|
|
|
|
"""
|
|
|
|
|
Returns a pair of x and y dictionaries containing, respectively, the matrices
|
|
|
|
|
of ML features and the labels for the sample. If the training sample does not
|
|
|
|
|
include label information, returns (x, {}).
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def sample_xy_old(
|
|
|
|
|
self,
|
|
|
|
|
instance: Instance,
|
|
|
|
|
sample: TrainingSample,
|
|
|
|
|
) -> Tuple[Dict, Dict]:
|
|
|
|
|
"""
|
|
|
|
|
Returns a pair of x and y dictionaries containing, respectively, the matrices
|
|
|
|
|
of ML features and the labels for the sample. If the training sample does not
|
|
|
|
|
include label information, returns (x, {}).
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def user_cut_cb(
|
|
|
|
|
self,
|
|
|
|
|
solver: "LearningSolver",
|
|
|
|
@ -294,18 +298,43 @@ class Component(EnforceOverrides):
|
|
|
|
|
) -> None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def evaluate_old(self, instances: List[Instance]) -> List:
|
|
|
|
|
ev = []
|
|
|
|
|
def xy_instances(
|
|
|
|
|
self,
|
|
|
|
|
instances: List[Instance],
|
|
|
|
|
) -> Tuple[Dict, Dict]:
|
|
|
|
|
x_combined: Dict = {}
|
|
|
|
|
y_combined: Dict = {}
|
|
|
|
|
for instance in instances:
|
|
|
|
|
instance.load()
|
|
|
|
|
for sample in instance.training_data:
|
|
|
|
|
ev += [self.sample_evaluate_old(instance, sample)]
|
|
|
|
|
for sample in instance.samples:
|
|
|
|
|
x_sample, y_sample = self.sample_xy(instance, sample)
|
|
|
|
|
for cat in x_sample.keys():
|
|
|
|
|
if cat not in x_combined:
|
|
|
|
|
x_combined[cat] = []
|
|
|
|
|
y_combined[cat] = []
|
|
|
|
|
x_combined[cat] += x_sample[cat]
|
|
|
|
|
y_combined[cat] += y_sample[cat]
|
|
|
|
|
instance.free()
|
|
|
|
|
return ev
|
|
|
|
|
return x_combined, y_combined
|
|
|
|
|
|
|
|
|
|
def sample_evaluate_old(
|
|
|
|
|
def xy_instances_old(
|
|
|
|
|
self,
|
|
|
|
|
instance: Instance,
|
|
|
|
|
sample: TrainingSample,
|
|
|
|
|
) -> Dict[Hashable, Dict[str, float]]:
|
|
|
|
|
return {}
|
|
|
|
|
instances: List[Instance],
|
|
|
|
|
) -> Tuple[Dict, Dict]:
|
|
|
|
|
x_combined: Dict = {}
|
|
|
|
|
y_combined: Dict = {}
|
|
|
|
|
for instance in instances:
|
|
|
|
|
instance.load()
|
|
|
|
|
for sample in instance.training_data:
|
|
|
|
|
xy = self.sample_xy_old(instance, sample)
|
|
|
|
|
if xy is None:
|
|
|
|
|
continue
|
|
|
|
|
x_sample, y_sample = xy
|
|
|
|
|
for cat in x_sample.keys():
|
|
|
|
|
if cat not in x_combined:
|
|
|
|
|
x_combined[cat] = []
|
|
|
|
|
y_combined[cat] = []
|
|
|
|
|
x_combined[cat] += x_sample[cat]
|
|
|
|
|
y_combined[cat] += y_sample[cat]
|
|
|
|
|
instance.free()
|
|
|
|
|
return x_combined, y_combined
|
|
|
|
|