mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Remove PerVariableTransformer
This commit is contained in:
@@ -3,13 +3,10 @@
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
from . import Component
|
||||
from .transformers import PerVariableTransformer
|
||||
from .extractors import Extractor
|
||||
from abc import ABC, abstractmethod
|
||||
from sklearn.neighbors import KNeighborsRegressor
|
||||
import numpy as np
|
||||
from p_tqdm import p_map
|
||||
|
||||
|
||||
from tqdm.auto import tqdm
|
||||
from joblib import Parallel, delayed
|
||||
import multiprocessing
|
||||
@@ -18,7 +15,6 @@ class BranchPriorityComponent(Component):
|
||||
def __init__(self,
|
||||
node_limit=1_000,
|
||||
):
|
||||
self.transformer = PerVariableTransformer()
|
||||
self.pending_instances = []
|
||||
self.x_train = {}
|
||||
self.y_train = {}
|
||||
@@ -28,7 +24,7 @@ class BranchPriorityComponent(Component):
|
||||
def before_solve(self, solver, instance, model):
|
||||
assert solver.is_persistent, "BranchPriorityComponent requires a persistent solver"
|
||||
from gurobipy import GRB
|
||||
var_split = self.transformer.split_variables(instance, model)
|
||||
var_split = Extractor.split_variables(instance, model)
|
||||
for category in var_split.keys():
|
||||
if category not in self.predictors.keys():
|
||||
continue
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
# MIPLearn, an extensible framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
import numpy as np
|
||||
from pyomo.core import Var
|
||||
|
||||
|
||||
class PerVariableTransformer:
|
||||
"""
|
||||
Class that converts a miplearn.Instance into a matrix of features that is suitable
|
||||
for training machine learning models that make one decision per decision variable.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def transform_instance(self, instance, var_index_pairs):
|
||||
instance_features = self._get_instance_features(instance)
|
||||
variable_features = self._get_variable_features(instance, var_index_pairs)
|
||||
return np.vstack([
|
||||
np.hstack([instance_features, vf])
|
||||
for vf in variable_features
|
||||
])
|
||||
|
||||
@staticmethod
|
||||
def _get_instance_features(instance):
|
||||
features = instance.get_instance_features()
|
||||
assert isinstance(features, np.ndarray)
|
||||
return features
|
||||
|
||||
@staticmethod
|
||||
def _get_variable_features(instance, var_index_pairs):
|
||||
features = []
|
||||
expected_shape = None
|
||||
for (var, index) in var_index_pairs:
|
||||
vf = instance.get_variable_features(var, index)
|
||||
assert isinstance(vf, np.ndarray)
|
||||
if expected_shape is None:
|
||||
assert len(vf.shape) == 1
|
||||
expected_shape = vf.shape
|
||||
else:
|
||||
assert vf.shape == expected_shape
|
||||
features += [vf]
|
||||
return np.array(features)
|
||||
|
||||
@staticmethod
|
||||
def transform_solution(var_index_pairs):
|
||||
y = []
|
||||
for (var, index) in var_index_pairs:
|
||||
y += [[1 - var[index].value, var[index].value]]
|
||||
return np.array(y)
|
||||
|
||||
@staticmethod
|
||||
def split_variables(instance, model):
|
||||
result = {}
|
||||
for var in model.component_objects(Var):
|
||||
for index in var:
|
||||
category = instance.get_variable_category(var, index)
|
||||
if category is None:
|
||||
continue
|
||||
if category not in result.keys():
|
||||
result[category] = []
|
||||
result[category] += [(var, index)]
|
||||
return result
|
||||
@@ -3,7 +3,6 @@
|
||||
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||
|
||||
from . import Component
|
||||
from .transformers import PerVariableTransformer
|
||||
from .extractors import *
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
@@ -134,7 +133,6 @@ class WarmStartComponent(Component):
|
||||
mode="exact",
|
||||
):
|
||||
self.mode = mode
|
||||
self.transformer = PerVariableTransformer()
|
||||
self.x_train = {}
|
||||
self.y_train = {}
|
||||
self.predictors = {}
|
||||
|
||||
Reference in New Issue
Block a user