mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Implement component.fit, component.fit_xy
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
import numpy as np
|
||||
from typing import Any, List, Union, TYPE_CHECKING, Tuple, Dict
|
||||
|
||||
from miplearn.extractors import InstanceIterator
|
||||
from miplearn.instance import Instance
|
||||
from miplearn.types import LearningSolveStats, TrainingSample
|
||||
|
||||
@@ -13,7 +14,7 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
class Component(ABC):
|
||||
class Component:
|
||||
"""
|
||||
A Component is an object which adds functionality to a LearningSolver.
|
||||
|
||||
@@ -130,12 +131,6 @@ class Component(ABC):
|
||||
"""
|
||||
return
|
||||
|
||||
def fit(
|
||||
self,
|
||||
training_instances: Union[List[str], List[Instance]],
|
||||
) -> None:
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
def xy_sample(
|
||||
instance: Any,
|
||||
@@ -147,6 +142,40 @@ class Component(ABC):
|
||||
"""
|
||||
return {}, {}
|
||||
|
||||
def xy_instances(
|
||||
self,
|
||||
instances: Union[List[str], List[Instance]],
|
||||
) -> Tuple[Dict, Dict]:
|
||||
x_combined: Dict = {}
|
||||
y_combined: Dict = {}
|
||||
for instance in InstanceIterator(instances):
|
||||
for sample in instance.training_data:
|
||||
x_sample, y_sample = self.xy_sample(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]
|
||||
return x_combined, y_combined
|
||||
|
||||
def fit(
|
||||
self,
|
||||
training_instances: Union[List[str], 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_xy(
|
||||
self,
|
||||
x: Dict[str, np.ndarray],
|
||||
y: Dict[str, np.ndarray],
|
||||
) -> None:
|
||||
return
|
||||
|
||||
def iteration_cb(
|
||||
self,
|
||||
solver: "LearningSolver",
|
||||
|
||||
@@ -105,19 +105,11 @@ class PrimalSolutionComponent(Component):
|
||||
) -> Dict[Hashable, np.ndarray]:
|
||||
return self._build_x_y_dict(instances, self._extract_variable_features)
|
||||
|
||||
def y(
|
||||
def fit_xy(
|
||||
self,
|
||||
instances: Union[List[str], List[Instance]],
|
||||
) -> Dict[Hashable, np.ndarray]:
|
||||
return self._build_x_y_dict(instances, self._extract_variable_labels)
|
||||
|
||||
def fit(
|
||||
self,
|
||||
training_instances: Union[List[str], List[Instance]],
|
||||
n_jobs: int = 1,
|
||||
x: Dict[str, np.ndarray],
|
||||
y: Dict[str, np.ndarray],
|
||||
) -> None:
|
||||
x = self.x(training_instances)
|
||||
y = self.y(training_instances)
|
||||
for category in x.keys():
|
||||
clf = self.classifier_factory()
|
||||
thr = self.threshold_factory()
|
||||
@@ -322,8 +314,11 @@ class PrimalSolutionComponent(Component):
|
||||
x[category] = []
|
||||
y[category] = []
|
||||
features: Any = instance.get_variable_features(var, idx)
|
||||
assert isinstance(features, list)
|
||||
if "LP solution" in sample and sample["LP solution"] is not None:
|
||||
features += [sample["LP solution"][var][idx]]
|
||||
lp_value = sample["LP solution"][var][idx]
|
||||
if lp_value is not None:
|
||||
features += [sample["LP solution"][var][idx]]
|
||||
x[category] += [features]
|
||||
y[category] += [[opt_value < 0.5, opt_value >= 0.5]]
|
||||
return x, y
|
||||
|
||||
Reference in New Issue
Block a user