|
|
@ -2,14 +2,13 @@
|
|
|
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
|
|
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
|
|
|
# Released under the modified BSD license. See COPYING.md for more details.
|
|
|
|
# Released under the modified BSD license. See COPYING.md for more details.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import gzip
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
import pickle
|
|
|
|
import pickle
|
|
|
|
import gzip
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
from tqdm.auto import tqdm
|
|
|
|
from tqdm.auto import tqdm
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
@ -48,10 +47,10 @@ class Extractor(ABC):
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def split_variables(instance):
|
|
|
|
def split_variables(instance):
|
|
|
|
assert hasattr(instance, "lp_solution")
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
result = {}
|
|
|
|
for var_name in instance.lp_solution:
|
|
|
|
lp_solution = instance.training_data[0]["LP solution"]
|
|
|
|
for index in instance.lp_solution[var_name]:
|
|
|
|
for var_name in lp_solution:
|
|
|
|
|
|
|
|
for index in lp_solution[var_name]:
|
|
|
|
category = instance.get_variable_category(var_name, index)
|
|
|
|
category = instance.get_variable_category(var_name, index)
|
|
|
|
if category is None:
|
|
|
|
if category is None:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
@ -71,6 +70,7 @@ class VariableFeaturesExtractor(Extractor):
|
|
|
|
):
|
|
|
|
):
|
|
|
|
instance_features = instance.get_instance_features()
|
|
|
|
instance_features = instance.get_instance_features()
|
|
|
|
var_split = self.split_variables(instance)
|
|
|
|
var_split = self.split_variables(instance)
|
|
|
|
|
|
|
|
lp_solution = instance.training_data[0]["LP solution"]
|
|
|
|
for (category, var_index_pairs) in var_split.items():
|
|
|
|
for (category, var_index_pairs) in var_split.items():
|
|
|
|
if category not in result:
|
|
|
|
if category not in result:
|
|
|
|
result[category] = []
|
|
|
|
result[category] = []
|
|
|
@ -78,7 +78,7 @@ class VariableFeaturesExtractor(Extractor):
|
|
|
|
result[category] += [
|
|
|
|
result[category] += [
|
|
|
|
instance_features.tolist()
|
|
|
|
instance_features.tolist()
|
|
|
|
+ instance.get_variable_features(var_name, index).tolist()
|
|
|
|
+ instance.get_variable_features(var_name, index).tolist()
|
|
|
|
+ [instance.lp_solution[var_name][index]]
|
|
|
|
+ [lp_solution[var_name][index]]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
for category in result:
|
|
|
|
for category in result:
|
|
|
|
result[category] = np.array(result[category])
|
|
|
|
result[category] = np.array(result[category])
|
|
|
@ -97,14 +97,15 @@ class SolutionExtractor(Extractor):
|
|
|
|
disable=len(instances) < 5,
|
|
|
|
disable=len(instances) < 5,
|
|
|
|
):
|
|
|
|
):
|
|
|
|
var_split = self.split_variables(instance)
|
|
|
|
var_split = self.split_variables(instance)
|
|
|
|
|
|
|
|
if self.relaxation:
|
|
|
|
|
|
|
|
solution = instance.training_data[0]["LP solution"]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
solution = instance.training_data[0]["Solution"]
|
|
|
|
for (category, var_index_pairs) in var_split.items():
|
|
|
|
for (category, var_index_pairs) in var_split.items():
|
|
|
|
if category not in result:
|
|
|
|
if category not in result:
|
|
|
|
result[category] = []
|
|
|
|
result[category] = []
|
|
|
|
for (var_name, index) in var_index_pairs:
|
|
|
|
for (var_name, index) in var_index_pairs:
|
|
|
|
if self.relaxation:
|
|
|
|
v = solution[var_name][index]
|
|
|
|
v = instance.lp_solution[var_name][index]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
v = instance.solution[var_name][index]
|
|
|
|
|
|
|
|
if v is None:
|
|
|
|
if v is None:
|
|
|
|
result[category] += [[0, 0]]
|
|
|
|
result[category] += [[0, 0]]
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -121,7 +122,7 @@ class InstanceFeaturesExtractor(Extractor):
|
|
|
|
np.hstack(
|
|
|
|
np.hstack(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
instance.get_instance_features(),
|
|
|
|
instance.get_instance_features(),
|
|
|
|
instance.lp_value,
|
|
|
|
instance.training_data[0]["LP value"],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
for instance in InstanceIterator(instances)
|
|
|
|
for instance in InstanceIterator(instances)
|
|
|
@ -137,13 +138,22 @@ class ObjectiveValueExtractor(Extractor):
|
|
|
|
def extract(self, instances):
|
|
|
|
def extract(self, instances):
|
|
|
|
if self.kind == "lower bound":
|
|
|
|
if self.kind == "lower bound":
|
|
|
|
return np.array(
|
|
|
|
return np.array(
|
|
|
|
[[instance.lower_bound] for instance in InstanceIterator(instances)]
|
|
|
|
[
|
|
|
|
|
|
|
|
[instance.training_data[0]["Lower bound"]]
|
|
|
|
|
|
|
|
for instance in InstanceIterator(instances)
|
|
|
|
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if self.kind == "upper bound":
|
|
|
|
if self.kind == "upper bound":
|
|
|
|
return np.array(
|
|
|
|
return np.array(
|
|
|
|
[[instance.upper_bound] for instance in InstanceIterator(instances)]
|
|
|
|
[
|
|
|
|
|
|
|
|
[instance.training_data[0]["Upper bound"]]
|
|
|
|
|
|
|
|
for instance in InstanceIterator(instances)
|
|
|
|
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if self.kind == "lp":
|
|
|
|
if self.kind == "lp":
|
|
|
|
return np.array(
|
|
|
|
return np.array(
|
|
|
|
[[instance.lp_value] for instance in InstanceIterator(instances)]
|
|
|
|
[
|
|
|
|
|
|
|
|
[instance.training_data[0]["LP value"]]
|
|
|
|
|
|
|
|
for instance in InstanceIterator(instances)
|
|
|
|
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|