|
|
|
@ -17,21 +17,25 @@ class ChallengeA:
|
|
|
|
|
- K = 500, u ~ U(0., 1.)
|
|
|
|
|
- alpha = 0.25
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self,
|
|
|
|
|
seed=42,
|
|
|
|
|
n_training_instances=500,
|
|
|
|
|
n_test_instances=50):
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
seed=42,
|
|
|
|
|
n_training_instances=500,
|
|
|
|
|
n_test_instances=50,
|
|
|
|
|
):
|
|
|
|
|
|
|
|
|
|
np.random.seed(seed)
|
|
|
|
|
self.gen = MultiKnapsackGenerator(n=randint(low=250, high=251),
|
|
|
|
|
m=randint(low=10, high=11),
|
|
|
|
|
w=uniform(loc=0.0, scale=1000.0),
|
|
|
|
|
K=uniform(loc=500.0, scale=0.0),
|
|
|
|
|
u=uniform(loc=0.0, scale=1.0),
|
|
|
|
|
alpha=uniform(loc=0.25, scale=0.0),
|
|
|
|
|
fix_w=True,
|
|
|
|
|
w_jitter=uniform(loc=0.95, scale=0.1),
|
|
|
|
|
)
|
|
|
|
|
self.gen = MultiKnapsackGenerator(
|
|
|
|
|
n=randint(low=250, high=251),
|
|
|
|
|
m=randint(low=10, high=11),
|
|
|
|
|
w=uniform(loc=0.0, scale=1000.0),
|
|
|
|
|
K=uniform(loc=500.0, scale=0.0),
|
|
|
|
|
u=uniform(loc=0.0, scale=1.0),
|
|
|
|
|
alpha=uniform(loc=0.25, scale=0.0),
|
|
|
|
|
fix_w=True,
|
|
|
|
|
w_jitter=uniform(loc=0.95, scale=0.1),
|
|
|
|
|
)
|
|
|
|
|
np.random.seed(seed + 1)
|
|
|
|
|
self.training_instances = self.gen.generate(n_training_instances)
|
|
|
|
|
|
|
|
|
@ -51,10 +55,7 @@ class MultiKnapsackInstance(Instance):
|
|
|
|
|
same size and items don't shuffle around.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
|
prices,
|
|
|
|
|
capacities,
|
|
|
|
|
weights):
|
|
|
|
|
def __init__(self, prices, capacities, weights):
|
|
|
|
|
assert isinstance(prices, np.ndarray)
|
|
|
|
|
assert isinstance(capacities, np.ndarray)
|
|
|
|
|
assert isinstance(weights, np.ndarray)
|
|
|
|
@ -69,44 +70,53 @@ class MultiKnapsackInstance(Instance):
|
|
|
|
|
def to_model(self):
|
|
|
|
|
model = pe.ConcreteModel()
|
|
|
|
|
model.x = pe.Var(range(self.n), domain=pe.Binary)
|
|
|
|
|
model.OBJ = pe.Objective(rule=lambda model: sum(model.x[j] * self.prices[j]
|
|
|
|
|
for j in range(self.n)),
|
|
|
|
|
sense=pe.maximize)
|
|
|
|
|
model.OBJ = pe.Objective(
|
|
|
|
|
rule=lambda model: sum(model.x[j] * self.prices[j] for j in range(self.n)),
|
|
|
|
|
sense=pe.maximize,
|
|
|
|
|
)
|
|
|
|
|
model.eq_capacity = pe.ConstraintList()
|
|
|
|
|
for i in range(self.m):
|
|
|
|
|
model.eq_capacity.add(sum(model.x[j] * self.weights[i,j]
|
|
|
|
|
for j in range(self.n)) <= self.capacities[i])
|
|
|
|
|
model.eq_capacity.add(
|
|
|
|
|
sum(model.x[j] * self.weights[i, j] for j in range(self.n))
|
|
|
|
|
<= self.capacities[i]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
def get_instance_features(self):
|
|
|
|
|
return np.hstack([
|
|
|
|
|
np.mean(self.prices),
|
|
|
|
|
self.capacities,
|
|
|
|
|
])
|
|
|
|
|
return np.hstack(
|
|
|
|
|
[
|
|
|
|
|
np.mean(self.prices),
|
|
|
|
|
self.capacities,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_variable_features(self, var, index):
|
|
|
|
|
return np.hstack([
|
|
|
|
|
self.prices[index],
|
|
|
|
|
self.weights[:, index],
|
|
|
|
|
])
|
|
|
|
|
return np.hstack(
|
|
|
|
|
[
|
|
|
|
|
self.prices[index],
|
|
|
|
|
self.weights[:, index],
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def get_variable_category(self, var, index):
|
|
|
|
|
# return index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MultiKnapsackGenerator:
|
|
|
|
|
def __init__(self,
|
|
|
|
|
n=randint(low=100, high=101),
|
|
|
|
|
m=randint(low=30, high=31),
|
|
|
|
|
w=randint(low=0, high=1000),
|
|
|
|
|
K=randint(low=500, high=500),
|
|
|
|
|
u=uniform(loc=0.0, scale=1.0),
|
|
|
|
|
alpha=uniform(loc=0.25, scale=0.0),
|
|
|
|
|
fix_w=False,
|
|
|
|
|
w_jitter=uniform(loc=1.0, scale=0.0),
|
|
|
|
|
round=True,
|
|
|
|
|
):
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
n=randint(low=100, high=101),
|
|
|
|
|
m=randint(low=30, high=31),
|
|
|
|
|
w=randint(low=0, high=1000),
|
|
|
|
|
K=randint(low=500, high=500),
|
|
|
|
|
u=uniform(loc=0.0, scale=1.0),
|
|
|
|
|
alpha=uniform(loc=0.25, scale=0.0),
|
|
|
|
|
fix_w=False,
|
|
|
|
|
w_jitter=uniform(loc=1.0, scale=0.0),
|
|
|
|
|
round=True,
|
|
|
|
|
):
|
|
|
|
|
"""Initialize the problem generator.
|
|
|
|
|
|
|
|
|
|
Instances have a random number of items (or variables) and a random number of knapsacks
|
|
|
|
@ -168,10 +178,13 @@ class MultiKnapsackGenerator:
|
|
|
|
|
assert isinstance(w, rv_frozen), "w should be a SciPy probability distribution"
|
|
|
|
|
assert isinstance(K, rv_frozen), "K should be a SciPy probability distribution"
|
|
|
|
|
assert isinstance(u, rv_frozen), "u should be a SciPy probability distribution"
|
|
|
|
|
assert isinstance(alpha, rv_frozen), "alpha should be a SciPy probability distribution"
|
|
|
|
|
assert isinstance(
|
|
|
|
|
alpha, rv_frozen
|
|
|
|
|
), "alpha should be a SciPy probability distribution"
|
|
|
|
|
assert isinstance(fix_w, bool), "fix_w should be boolean"
|
|
|
|
|
assert isinstance(w_jitter, rv_frozen), \
|
|
|
|
|
"w_jitter should be a SciPy probability distribution"
|
|
|
|
|
assert isinstance(
|
|
|
|
|
w_jitter, rv_frozen
|
|
|
|
|
), "w_jitter should be a SciPy probability distribution"
|
|
|
|
|
|
|
|
|
|
self.n = n
|
|
|
|
|
self.m = m
|
|
|
|
@ -211,13 +224,14 @@ class MultiKnapsackGenerator:
|
|
|
|
|
K = self.K.rvs()
|
|
|
|
|
w = w * np.array([self.w_jitter.rvs(n) for _ in range(m)])
|
|
|
|
|
alpha = self.alpha.rvs(m)
|
|
|
|
|
p = np.array([w[:,j].sum() / m + K * u[j] for j in range(n)])
|
|
|
|
|
b = np.array([w[i,:].sum() * alpha[i] for i in range(m)])
|
|
|
|
|
p = np.array([w[:, j].sum() / m + K * u[j] for j in range(n)])
|
|
|
|
|
b = np.array([w[i, :].sum() * alpha[i] for i in range(m)])
|
|
|
|
|
if self.round:
|
|
|
|
|
p = p.round()
|
|
|
|
|
b = b.round()
|
|
|
|
|
w = w.round()
|
|
|
|
|
return MultiKnapsackInstance(p, b, w)
|
|
|
|
|
|
|
|
|
|
return [_sample() for _ in range(n_samples)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -225,6 +239,7 @@ class KnapsackInstance(Instance):
|
|
|
|
|
"""
|
|
|
|
|
Simpler (one-dimensional) Knapsack Problem, used for testing.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, weights, prices, capacity):
|
|
|
|
|
self.weights = weights
|
|
|
|
|
self.prices = prices
|
|
|
|
@ -234,23 +249,29 @@ class KnapsackInstance(Instance):
|
|
|
|
|
model = pe.ConcreteModel()
|
|
|
|
|
items = range(len(self.weights))
|
|
|
|
|
model.x = pe.Var(items, domain=pe.Binary)
|
|
|
|
|
model.OBJ = pe.Objective(expr=sum(model.x[v] * self.prices[v] for v in items),
|
|
|
|
|
sense=pe.maximize)
|
|
|
|
|
model.eq_capacity = pe.Constraint(expr=sum(model.x[v] * self.weights[v]
|
|
|
|
|
for v in items) <= self.capacity)
|
|
|
|
|
model.OBJ = pe.Objective(
|
|
|
|
|
expr=sum(model.x[v] * self.prices[v] for v in items), sense=pe.maximize
|
|
|
|
|
)
|
|
|
|
|
model.eq_capacity = pe.Constraint(
|
|
|
|
|
expr=sum(model.x[v] * self.weights[v] for v in items) <= self.capacity
|
|
|
|
|
)
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
def get_instance_features(self):
|
|
|
|
|
return np.array([
|
|
|
|
|
self.capacity,
|
|
|
|
|
np.average(self.weights),
|
|
|
|
|
])
|
|
|
|
|
return np.array(
|
|
|
|
|
[
|
|
|
|
|
self.capacity,
|
|
|
|
|
np.average(self.weights),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_variable_features(self, var, index):
|
|
|
|
|
return np.array([
|
|
|
|
|
self.weights[index],
|
|
|
|
|
self.prices[index],
|
|
|
|
|
])
|
|
|
|
|
return np.array(
|
|
|
|
|
[
|
|
|
|
|
self.weights[index],
|
|
|
|
|
self.prices[index],
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GurobiKnapsackInstance(KnapsackInstance):
|
|
|
|
@ -258,6 +279,7 @@ class GurobiKnapsackInstance(KnapsackInstance):
|
|
|
|
|
Simpler (one-dimensional) knapsack instance, implemented directly in Gurobi
|
|
|
|
|
instead of Pyomo, used for testing.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, weights, prices, capacity):
|
|
|
|
|
super().__init__(weights, prices, capacity)
|
|
|
|
|
|
|
|
|
@ -268,9 +290,11 @@ class GurobiKnapsackInstance(KnapsackInstance):
|
|
|
|
|
model = gp.Model("Knapsack")
|
|
|
|
|
n = len(self.weights)
|
|
|
|
|
x = model.addVars(n, vtype=GRB.BINARY, name="x")
|
|
|
|
|
model.addConstr(gp.quicksum(x[i] * self.weights[i]
|
|
|
|
|
for i in range(n)) <= self.capacity,
|
|
|
|
|
"eq_capacity")
|
|
|
|
|
model.setObjective(gp.quicksum(x[i] * self.prices[i]
|
|
|
|
|
for i in range(n)), GRB.MAXIMIZE)
|
|
|
|
|
model.addConstr(
|
|
|
|
|
gp.quicksum(x[i] * self.weights[i] for i in range(n)) <= self.capacity,
|
|
|
|
|
"eq_capacity",
|
|
|
|
|
)
|
|
|
|
|
model.setObjective(
|
|
|
|
|
gp.quicksum(x[i] * self.prices[i] for i in range(n)), GRB.MAXIMIZE
|
|
|
|
|
)
|
|
|
|
|
return model
|
|
|
|
|