-
Notifications
You must be signed in to change notification settings - Fork 4
/
yang.py
69 lines (54 loc) · 1.64 KB
/
yang.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import math
from networkx.utils import py_random_state
from geco.mips.knapsack.generic import knapsack
@py_random_state("seed")
def yang_params(n, seed=0):
"""
Generates knapsack instance params according to [1].
Parameters
----------
n: int
Number of items
seed: integer, random_state, or None
Indicator of random number generation state
Returns
-------
profits: list[int]
Profit of each item
weights: list[int]
Weight of each item
capacity: float
Capacity of knapsack
References
----------
.. [1] Yu Yang, Natashia Boland, Bistra Dilkina, Martin Savelsbergh,
"Learning Generalized Strong Branching for Set Covering,
Set Packing, and 0-1 Knapsack Problems", 2020.
"""
def draw_value():
return seed.randint(1, 10 * n)
profits = [draw_value() for _ in range(n)]
weights = [draw_value() for _ in range(n)]
capacity = math.floor(sum(weights) / 5)
return profits, weights, capacity
@py_random_state("seed")
def yang_instance(n, seed=0):
"""
Generates knapsack instance according to [1].
Parameters
----------
n: int
Number of items
seed: integer, random_state, or None
Indicator of random number generation state
Returns
-------
model: scip.Model
A pyscipopt model of the generated instance
References
----------
.. [1] Yu Yang, Natashia Boland, Bistra Dilkina, Martin Savelsbergh,
"Learning Generalized Strong Branching for Set Covering,
Set Packing, and 0-1 Knapsack Problems", 2020.
"""
return knapsack(*yang_params(n, seed))