From 25ba23a58d68b2f2d247bf834802de6e6dc2debd Mon Sep 17 00:00:00 2001 From: Alinson S Xavier Date: Thu, 30 Jan 2020 13:09:45 -0600 Subject: [PATCH] Minor updates to knapsack docs; add challenge --- docs/problems.md | 13 +++++++------ miplearn/branching.py | 6 +++--- miplearn/problems/knapsack.py | 21 +++++++++++++++++++-- mkdocs.yml | 2 +- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/docs/problems.md b/docs/problems.md index 4ebe298..5fa04f6 100644 --- a/docs/problems.md +++ b/docs/problems.md @@ -73,25 +73,26 @@ Given a set of $n$ items and $m$ types of resources (also called *knapsacks*), t The class `MultiKnapsackGenerator` can be used to generate random instances of this problem. The number of items $n$ and knapsacks $m$ are sampled from the user-provided probability distributions `n` and `m`. The weights $w_{ij}$ are sampled independently from the provided distribution `w`. The capacity of knapsack $i$ is set to $$ - \alpha_i \sum_{j=1}^n w_{ij} + b_i = \alpha_i \sum_{j=1}^n w_{ij} $$ where $\alpha_i$, the tightness ratio, is sampled from the provided probability distribution `alpha`. To make the instances more challenging, the costs of the items -are linearly correlated to their average weights. More specifically, the weight of each +are linearly correlated to their average weights. More specifically, the price of each item $j$ is set to: $$ - \sum_{i=1}^m \frac{w_{ij}}{m} + K u_j, + p_j = \sum_{i=1}^m \frac{w_{ij}}{m} + K u_j, $$ where $K$, the correlation coefficient, and $u_j$, the correlation multiplier, are sampled -from the provided probability distributions `K` and `u`. Note that $K$ is only sample once for each generated instance. +from the provided probability distributions `K` and `u`. If `fix_w=True` is provided, then $w_{ij}$ are kept the same in all generated instances. This also implies that $n$ and $m$ are kept fixed. Although the prices and capacities are derived from $w_{ij}$, as long as `u` and `K` are not constants, the generated instances will still not be completely identical. If a probability distribution `w_jitter` is provided, then item weights will be set to $w_{ij} + \gamma_{ij}$ where $\gamma_{ij}$ is sampled from `w_jitter`. When combined with `fix_w=True`, this argument may be used to generate instances where the weight of each item is roughly the same, but not exactly identical, across all instances. The prices of the items and the capacities of the knapsacks will be calculated as above, but using these perturbed weights instead. -!!! note - Random generator based on *A. Freville and G. Plateau, **An efficient preprocessing procedure for the multidimensional knapsack problem**, Discrete Applied Mathematics 49 (1994) 189–212*. \ No newline at end of file +!!! note "References" + * Freville, Arnaud, and Gérard Plateau. *An efficient preprocessing procedure for the multidimensional 0–1 knapsack problem.* Discrete applied mathematics 49.1-3 (1994): 189-212. + * Fréville, Arnaud. *The multidimensional 0–1 knapsack problem: An overview.* European Journal of Operational Research 155.1 (2004): 1-21. \ No newline at end of file diff --git a/miplearn/branching.py b/miplearn/branching.py index 1eb9ee6..40c2561 100644 --- a/miplearn/branching.py +++ b/miplearn/branching.py @@ -39,10 +39,10 @@ class BranchPriorityComponent(Component): "%s/scripts/branchpriority.jl" % src_dirname, model_file.name, priority_file.name], - check=True) + check=True, + capture_output=True) self._merge(np.genfromtxt(priority_file.name, - delimiter=',', - dtype=int)) + delimiter=',')) def fit(self, solver): diff --git a/miplearn/problems/knapsack.py b/miplearn/problems/knapsack.py index 5b3c69e..a451c60 100644 --- a/miplearn/problems/knapsack.py +++ b/miplearn/problems/knapsack.py @@ -10,6 +10,22 @@ from scipy.stats import uniform, randint, bernoulli from scipy.stats.distributions import rv_frozen +class ChallengeA: + def __init__(self, seed=0): + np.random.seed(seed) + self.gen = MultiKnapsackGenerator(n=randint(low=50, high=51), + m=randint(low=3, high=4), + w=uniform(loc=0.0, scale=200.0), + K=uniform(loc=1.0, scale=0.0), + u=uniform(loc=1.0, scale=0.0), + alpha=uniform(loc=0.25, scale=0.0), + fix_w=True, + w_jitter=uniform(loc=-10.0, scale=20.0), + ) + self.training_instances = self.gen.generate(300) + self.test_instances = self.gen.generate(50) + + class MultiKnapsackInstance(Instance): """Representation of the Multidimensional 0-1 Knapsack Problem. @@ -74,6 +90,7 @@ class MultiKnapsackGenerator: alpha=uniform(loc=0.25, scale=0.0), fix_w=False, w_jitter=randint(low=0, high=1), + seed=None, ): """Initialize the problem generator. @@ -114,9 +131,9 @@ class MultiKnapsackGenerator: Probability distribution for the number of items (or variables) m: rv_discrete Probability distribution for the number of knapsacks (or constraints) - w: rv_discrete + w: rv_continuous Probability distribution for the item weights - K: rv_discrete + K: rv_continuous Probability distribution for the profit correlation coefficient u: rv_continuous Probability distribution for the profit multiplier diff --git a/mkdocs.yml b/mkdocs.yml index b3e22aa..5d0c202 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,6 +1,6 @@ site_name: MIPLearn theme: cinder -copyright: Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved. +copyright: Copyright (C) 2019-2020 Argonne National Laboratory repo_url: https://github.com/iSoron/miplearn nav: - Home: index.md