Minwei Kong commited on
Nest task folders under tasks/ (part 10)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- tasks/mansini2020/gurobi_feasi_result/large_feasi_result_3.json +3 -0
- tasks/mansini2020/gurobi_feasi_result/large_feasi_result_4.json +3 -0
- tasks/mansini2020/gurobi_feasi_result/large_feasi_result_5.json +3 -0
- tasks/mansini2020/gurobi_feasi_result/tiny_feasi_result.json +3 -0
- tasks/mansini2020/gurobi_solution/large_solution_1.json +3 -0
- tasks/mansini2020/gurobi_solution/large_solution_2.json +3 -0
- tasks/mansini2020/gurobi_solution/large_solution_3.json +3 -0
- tasks/mansini2020/gurobi_solution/large_solution_4.json +3 -0
- tasks/mansini2020/gurobi_solution/large_solution_5.json +3 -0
- tasks/mansini2020/gurobi_solution/tiny_solution.json +3 -0
- tasks/mansini2020/gurobi_solution_log/large_log_1.jsonl +3 -0
- tasks/mansini2020/gurobi_solution_log/large_log_2.jsonl +3 -0
- tasks/mansini2020/gurobi_solution_log/large_log_3.jsonl +3 -0
- tasks/mansini2020/gurobi_solution_log/large_log_4.jsonl +3 -0
- tasks/mansini2020/gurobi_solution_log/large_log_5.jsonl +3 -0
- tasks/mansini2020/gurobi_solution_log/tiny_log.jsonl +3 -0
- tasks/mansini2020/instance/large_instance_1.json +3 -0
- tasks/mansini2020/instance/large_instance_2.json +3 -0
- tasks/mansini2020/instance/large_instance_3.json +3 -0
- tasks/mansini2020/instance/large_instance_4.json +3 -0
- tasks/mansini2020/instance/large_instance_5.json +3 -0
- tasks/mansini2020/instance/tiny_instance.json +3 -0
- tasks/mansini2020/instance_schema.json +21 -0
- tasks/mansini2020/mathematical_formulation.md +36 -0
- tasks/mansini2020/problem_description.txt +5 -0
- tasks/mansini2020/solution_logger.py +61 -0
- tasks/mansini2020/solution_schema.json +11 -0
- tasks/martello1999/feasibility_check.py +335 -0
- tasks/martello1999/gurobi_code.py +104 -0
- tasks/martello1999/gurobi_feasi_result/large_feasi_result_1.json +3 -0
- tasks/martello1999/gurobi_feasi_result/large_feasi_result_2.json +3 -0
- tasks/martello1999/gurobi_feasi_result/large_feasi_result_3.json +3 -0
- tasks/martello1999/gurobi_feasi_result/large_feasi_result_4.json +3 -0
- tasks/martello1999/gurobi_feasi_result/large_feasi_result_5.json +3 -0
- tasks/martello1999/gurobi_feasi_result/tiny_feasi_result.json +3 -0
- tasks/martello1999/gurobi_solution/large_solution_1.json +3 -0
- tasks/martello1999/gurobi_solution/large_solution_2.json +3 -0
- tasks/martello1999/gurobi_solution/large_solution_3.json +3 -0
- tasks/martello1999/gurobi_solution/large_solution_4.json +3 -0
- tasks/martello1999/gurobi_solution/large_solution_5.json +3 -0
- tasks/martello1999/gurobi_solution/tiny_solution.json +3 -0
- tasks/martello1999/gurobi_solution_log/large_log_1.jsonl +3 -0
- tasks/martello1999/gurobi_solution_log/large_log_2.jsonl +3 -0
- tasks/martello1999/gurobi_solution_log/large_log_3.jsonl +3 -0
- tasks/martello1999/gurobi_solution_log/large_log_4.jsonl +3 -0
- tasks/martello1999/gurobi_solution_log/large_log_5.jsonl +3 -0
- tasks/martello1999/gurobi_solution_log/tiny_log.jsonl +3 -0
- tasks/martello1999/instance/large_instance_1.json +3 -0
- tasks/martello1999/instance/large_instance_2.json +3 -0
- tasks/martello1999/instance/large_instance_3.json +3 -0
tasks/mansini2020/gurobi_feasi_result/large_feasi_result_3.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/mansini2020/gurobi_feasi_result/large_feasi_result_4.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/mansini2020/gurobi_feasi_result/large_feasi_result_5.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/mansini2020/gurobi_feasi_result/tiny_feasi_result.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/mansini2020/gurobi_solution/large_solution_1.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d05dedc083374878ca23d9ec7eb1857a317baacac3fdd01e6e55c83a743872e2
|
| 3 |
+
size 8569644
|
tasks/mansini2020/gurobi_solution/large_solution_2.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9e72e790c1a4bfcd89cfc31b0229067a7c5975ebfb5f180f82762167633e84b9
|
| 3 |
+
size 8547809
|
tasks/mansini2020/gurobi_solution/large_solution_3.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b0447759795d7b45dca72e5db7d11eee987803ec5782f0aa6430e6958da5ec1a
|
| 3 |
+
size 8569657
|
tasks/mansini2020/gurobi_solution/large_solution_4.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:371d1d5565f71eee09d2a58067fcf94bad317b7f143cabc580da07631a63f5f5
|
| 3 |
+
size 8508149
|
tasks/mansini2020/gurobi_solution/large_solution_5.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:afe599c1451937803404f5d762b2bf48b140f50ab6c49452236a0160e0c9137f
|
| 3 |
+
size 8568147
|
tasks/mansini2020/gurobi_solution/tiny_solution.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2558a9fd26d6a9c8b6f211218b382ba542e89fda9b88aeb9d7ad34ff75b38bd1
|
| 3 |
+
size 11161
|
tasks/mansini2020/gurobi_solution_log/large_log_1.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:23e2aa9ff84af840609086d6f681ee422b0733173254687f0f35139e7949eb0c
|
| 3 |
+
size 47
|
tasks/mansini2020/gurobi_solution_log/large_log_2.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aef55475a513ae597eb5ae13256b28653e7ae1fd7dff1a111aeaefd27b718831
|
| 3 |
+
size 45
|
tasks/mansini2020/gurobi_solution_log/large_log_3.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d2ad79952cccd9d2e698b12c9d2772fadb08776c911e28783048d3b5c5d8708b
|
| 3 |
+
size 46
|
tasks/mansini2020/gurobi_solution_log/large_log_4.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a7bb98b13be52ad9ce10fa3f1c758df99edc189a561fe08d5d27414c2c94a1b2
|
| 3 |
+
size 46
|
tasks/mansini2020/gurobi_solution_log/large_log_5.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:748e934f423bf37ce19aa3dc3bb9dbfc7e76ddc82ff232a6f33927b430d7b607
|
| 3 |
+
size 46
|
tasks/mansini2020/gurobi_solution_log/tiny_log.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:79386943474b72d1052d12237d34a605052e30c38ea1febe87652e77bd946c96
|
| 3 |
+
size 43
|
tasks/mansini2020/instance/large_instance_1.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:48636ff73d6abbfadecfbaf139fb1efe7ed7bb4cc26b0d567c06bc8239367189
|
| 3 |
+
size 561182775
|
tasks/mansini2020/instance/large_instance_2.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:50d2f7bd54d359b21bd95ba786f9b8e2df8ee83b7d70036862ba65096f6084fb
|
| 3 |
+
size 561111750
|
tasks/mansini2020/instance/large_instance_3.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:21b65d8394089ff854e473f8ff05bde41e7acd515aebd7c1dd35189aedc7707a
|
| 3 |
+
size 561156593
|
tasks/mansini2020/instance/large_instance_4.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:19e854a39212c5493cd498958c311904eeb55f3e0d74a6e8cb18d6ecf4c6fb86
|
| 3 |
+
size 561156106
|
tasks/mansini2020/instance/large_instance_5.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fb246dbe15d50007549a40a6dfacde94426572cb2e9dccaa83a68febd7efefd2
|
| 3 |
+
size 561155680
|
tasks/mansini2020/instance/tiny_instance.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:db2322e7c2ec25f81d5d0f0aaa2a3d14cd495bafea0cb3d0a353934d6f00e69d
|
| 3 |
+
size 134638
|
tasks/mansini2020/instance_schema.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"parameters": {
|
| 3 |
+
"n_groups": "<int> Number of disjoint groups from which exactly one item must be selected.",
|
| 4 |
+
"m_resources": "<int> Number of shared resources, each with a limited capacity.",
|
| 5 |
+
"n_items_per_group": "<int> Number of candidate items available within each group.",
|
| 6 |
+
"n_bar_total_items": "<int> Total number of candidate items across all groups."
|
| 7 |
+
},
|
| 8 |
+
"capacities": "<list[int], length m_resources> Maximum available amount of each shared resource.",
|
| 9 |
+
"groups": [
|
| 10 |
+
{
|
| 11 |
+
"group_id": "<int> Zero-based identifier of this group.",
|
| 12 |
+
"items": [
|
| 13 |
+
{
|
| 14 |
+
"item_id": "<int> Zero-based identifier of this item within its group.",
|
| 15 |
+
"profit": "<int> Reward earned when this item is selected.",
|
| 16 |
+
"weights": "<list[int], length m_resources> Amount of each shared resource consumed by this item."
|
| 17 |
+
}
|
| 18 |
+
]
|
| 19 |
+
}
|
| 20 |
+
]
|
| 21 |
+
}
|
tasks/mansini2020/mathematical_formulation.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Original Formulation (Eqs. 1–4)
|
| 2 |
+
|
| 3 |
+
## Sets and Indices
|
| 4 |
+
|
| 5 |
+
- $\{G_1, G_2, \ldots, G_n\}$: a collection of $n$ disjoint groups (classes) of items, with $G_q \cap G_l = \emptyset$ for all $q \neq l$.
|
| 6 |
+
|
| 7 |
+
- Each group $G_i$ has $n_i = |G_i|$ items, denoted by ordered pairs $(i, j)$, $i = 1,\ldots,n$, $j = 1,\ldots,n_i$.
|
| 8 |
+
|
| 9 |
+
- $\tilde{N}$: set of all items, $|\tilde{N}| = \tilde{n} = \sum_{i=1}^{n} n_i$.
|
| 10 |
+
|
| 11 |
+
- $k = 1,\ldots,m$: index for the $m$ resources.
|
| 12 |
+
|
| 13 |
+
## Parameters
|
| 14 |
+
|
| 15 |
+
- $p_{ij} \geq 0$: profit of item $(i,j) \in \tilde{N}$.
|
| 16 |
+
|
| 17 |
+
- $w_{ij}^{k} \geq 0$: amount of resource $k$ required by item $(i,j)$.
|
| 18 |
+
|
| 19 |
+
- $c_k \geq 0$: capacity of resource $k$, for $k=1,\ldots,m$.
|
| 20 |
+
|
| 21 |
+
## Decision Variables
|
| 22 |
+
|
| 23 |
+
- $x_{ij} \in \{0,1\}$ for each $(i,j) \in \tilde{N}$: 1 if item $(i,j)$ is selected, 0 otherwise.
|
| 24 |
+
|
| 25 |
+
## Objective and Constraints
|
| 26 |
+
|
| 27 |
+
$$\begin{align}
|
| 28 |
+
\max \quad & z = \sum_{i=1}^{n} \sum_{j=1}^{n_i} p_{ij}\, x_{ij}
|
| 29 |
+
\tag{1} \\
|
| 30 |
+
\text{s.t.}\quad & \sum_{i=1}^{n} \sum_{j=1}^{n_i} w_{ij}^{k}\, x_{ij} \leq c_k
|
| 31 |
+
&& k = 1,\ldots,m, \tag{2} \\
|
| 32 |
+
& \sum_{j=1}^{n_i} x_{ij} = 1
|
| 33 |
+
&& i = 1,\ldots,n, \tag{3} \\
|
| 34 |
+
& x_{ij} \in \{0,1\}
|
| 35 |
+
&& (i,j) \in \tilde{N}. \tag{4}
|
| 36 |
+
\end{align}$$
|
tasks/mansini2020/problem_description.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Problem Description
|
| 2 |
+
|
| 3 |
+
A collection of items is partitioned into a number of disjoint groups. Each group contains a specified number of items, and every item belongs to exactly one group. There are also several shared resources, each with a given capacity. For every item, the input data specifies a profit and, for each resource, the amount of that resource the item consumes. All profits and resource consumptions are nonnegative, and every resource capacity is nonnegative.
|
| 4 |
+
|
| 5 |
+
The task is to select exactly one item from each group so as to maximize the total profit of the selected items. The selection must respect all resource limits: for each resource, the sum of the consumptions of the selected items across all groups must not exceed that resource's capacity. Because exactly one item must be chosen per group, omitting a group entirely or picking more than one item from any single group is not allowed.
|
tasks/mansini2020/solution_logger.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Convergence logger for optimization algorithms.
|
| 3 |
+
|
| 4 |
+
Records incumbent solutions with timestamps to a JSONL file.
|
| 5 |
+
This module is provided to LLM-generated programs — they only need to call
|
| 6 |
+
`log(objective_value)` whenever a better feasible solution is found.
|
| 7 |
+
|
| 8 |
+
Usage in generated code:
|
| 9 |
+
from solution_logger import SolutionLogger
|
| 10 |
+
logger = SolutionLogger(log_path, sense="minimize") # or "maximize"
|
| 11 |
+
# ... inside algorithm loop:
|
| 12 |
+
logger.log(objective_value)
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
import time
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class SolutionLogger:
|
| 20 |
+
def __init__(self, log_path, sense="minimize"):
|
| 21 |
+
"""
|
| 22 |
+
Args:
|
| 23 |
+
log_path: Path to the JSONL output file.
|
| 24 |
+
sense: "minimize" or "maximize".
|
| 25 |
+
"""
|
| 26 |
+
self.log_path = log_path
|
| 27 |
+
self.sense = sense
|
| 28 |
+
self.start_time = time.time()
|
| 29 |
+
self.best_obj = None
|
| 30 |
+
self.min_interval = 0.1 # seconds, avoid excessive writes
|
| 31 |
+
|
| 32 |
+
self._last_log_time = 0.0
|
| 33 |
+
# Clear the file
|
| 34 |
+
with open(self.log_path, "w") as f:
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
def log(self, objective_value):
|
| 38 |
+
"""Record a new incumbent if it improves on the best known."""
|
| 39 |
+
if objective_value is None:
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
# Check if this is an improvement
|
| 43 |
+
if self.best_obj is not None:
|
| 44 |
+
if self.sense == "minimize" and objective_value >= self.best_obj:
|
| 45 |
+
return
|
| 46 |
+
if self.sense == "maximize" and objective_value <= self.best_obj:
|
| 47 |
+
return
|
| 48 |
+
|
| 49 |
+
elapsed = time.time() - self.start_time
|
| 50 |
+
|
| 51 |
+
# Throttle writes
|
| 52 |
+
if self.best_obj is not None and elapsed - self._last_log_time < self.min_interval:
|
| 53 |
+
self.best_obj = objective_value
|
| 54 |
+
return
|
| 55 |
+
|
| 56 |
+
self.best_obj = objective_value
|
| 57 |
+
self._last_log_time = elapsed
|
| 58 |
+
|
| 59 |
+
with open(self.log_path, "a") as f:
|
| 60 |
+
f.write(json.dumps({"time": round(elapsed, 3),
|
| 61 |
+
"objective_value": objective_value}) + "\n")
|
tasks/mansini2020/solution_schema.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"objective_value": "<float> Total profit obtained from the chosen items across all groups.",
|
| 3 |
+
"selected_items": [
|
| 4 |
+
{
|
| 5 |
+
"group_id": "<int> Identifier of the group from which an item has been picked.",
|
| 6 |
+
"item_id": "<int> Identifier of the item chosen from the corresponding group.",
|
| 7 |
+
"profit": "<int> Profit contributed by the chosen item.",
|
| 8 |
+
"weights": "<list[int], length m_resources> Amount of each resource consumed by the chosen item."
|
| 9 |
+
}
|
| 10 |
+
]
|
| 11 |
+
}
|
tasks/martello1999/feasibility_check.py
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Feasibility checker for the 0-1 Knapsack Problem (KP).
|
| 3 |
+
Based on: Martello, Pisinger, and Toth (1999)
|
| 4 |
+
|
| 5 |
+
Hard constraints (from formulation section, top to bottom):
|
| 6 |
+
Constraint 1 (1b): sum_{j=1}^{n} w_j x_j <= c
|
| 7 |
+
Constraint 2 (1c): x_j in {0, 1}, j = 1, ..., n
|
| 8 |
+
Constraint 3 (obj): reported objective_value must equal
|
| 9 |
+
sum_{j=1}^{n} p_j x_j (Tier-C anti-exploit check)
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import argparse
|
| 13 |
+
import json
|
| 14 |
+
|
| 15 |
+
# FRONTIEROR_RELEASE_CONTRACT_V2
|
| 16 |
+
_FRONTIEROR_REQUIRED_SOLUTION_FIELDS = ('objective_value', 'selected_items')
|
| 17 |
+
_FRONTIEROR_NONEMPTY_SOLUTION_FIELDS = ()
|
| 18 |
+
_FRONTIEROR_DERIVED_FIELD_RULES = ()
|
| 19 |
+
_FRONTIEROR_CONDITIONAL_FIELD_RULES = ()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _frontieror_validate_solution_contract(solution):
|
| 23 |
+
"""Return a structured rejection when the public solution contract is absent."""
|
| 24 |
+
violations = []
|
| 25 |
+
if not isinstance(solution, dict):
|
| 26 |
+
violations.append("solution must be a JSON object")
|
| 27 |
+
else:
|
| 28 |
+
missing = [
|
| 29 |
+
field
|
| 30 |
+
for field in _FRONTIEROR_REQUIRED_SOLUTION_FIELDS
|
| 31 |
+
if field not in solution or solution[field] is None
|
| 32 |
+
]
|
| 33 |
+
if missing:
|
| 34 |
+
violations.append(
|
| 35 |
+
"missing required solution field(s): " + ", ".join(missing)
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
for discriminator, expected_value, fields in _FRONTIEROR_CONDITIONAL_FIELD_RULES:
|
| 39 |
+
if solution.get(discriminator) != expected_value:
|
| 40 |
+
continue
|
| 41 |
+
conditional_missing = [
|
| 42 |
+
field
|
| 43 |
+
for field in fields
|
| 44 |
+
if field not in solution or solution[field] is None
|
| 45 |
+
]
|
| 46 |
+
if conditional_missing:
|
| 47 |
+
violations.append(
|
| 48 |
+
"missing conditionally required solution field(s): "
|
| 49 |
+
+ ", ".join(conditional_missing)
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
empty = [
|
| 53 |
+
field
|
| 54 |
+
for field in _FRONTIEROR_NONEMPTY_SOLUTION_FIELDS
|
| 55 |
+
if field in solution and solution[field] in (None, [], {})
|
| 56 |
+
]
|
| 57 |
+
if empty:
|
| 58 |
+
violations.append(
|
| 59 |
+
"empty required decision witness field(s): " + ", ".join(empty)
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
for target, operation, source, source_key in _FRONTIEROR_DERIVED_FIELD_RULES:
|
| 63 |
+
try:
|
| 64 |
+
reported = float(solution[target])
|
| 65 |
+
if operation == "length":
|
| 66 |
+
expected = float(len(solution[source]))
|
| 67 |
+
elif operation == "mapping_value":
|
| 68 |
+
expected = float(solution[source][source_key])
|
| 69 |
+
else:
|
| 70 |
+
raise ValueError("unknown derived-field operation")
|
| 71 |
+
except (KeyError, TypeError, ValueError, OverflowError):
|
| 72 |
+
violations.append(
|
| 73 |
+
f"cannot derive {target} from decision witness field {source}"
|
| 74 |
+
)
|
| 75 |
+
continue
|
| 76 |
+
if (
|
| 77 |
+
reported != reported
|
| 78 |
+
or expected != expected
|
| 79 |
+
or reported in (float("inf"), float("-inf"))
|
| 80 |
+
or expected in (float("inf"), float("-inf"))
|
| 81 |
+
or abs(reported - expected) > 1e-6
|
| 82 |
+
):
|
| 83 |
+
violations.append(
|
| 84 |
+
f"{target}={reported} does not match the decision-derived "
|
| 85 |
+
f"value {expected}"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if "objective_value" in _FRONTIEROR_REQUIRED_SOLUTION_FIELDS:
|
| 89 |
+
objective = solution.get("objective_value")
|
| 90 |
+
if (
|
| 91 |
+
isinstance(objective, bool)
|
| 92 |
+
or not isinstance(objective, (int, float))
|
| 93 |
+
or objective != objective
|
| 94 |
+
or objective in (float("inf"), float("-inf"))
|
| 95 |
+
):
|
| 96 |
+
violations.append("objective_value must be a finite JSON number")
|
| 97 |
+
|
| 98 |
+
if not violations:
|
| 99 |
+
return None
|
| 100 |
+
return {
|
| 101 |
+
"feasible": False,
|
| 102 |
+
"violated_constraints": ["solution_schema"],
|
| 103 |
+
"violations": violations,
|
| 104 |
+
"violation_magnitudes": [],
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def _frontieror_write_unexpected_rejection(error):
|
| 109 |
+
"""Turn malformed candidate/checker exceptions into a normal rejection."""
|
| 110 |
+
import json as _frontieror_json
|
| 111 |
+
import sys as _frontieror_sys
|
| 112 |
+
|
| 113 |
+
result_path = None
|
| 114 |
+
for index, argument in enumerate(_frontieror_sys.argv):
|
| 115 |
+
if argument == "--result_path" and index + 1 < len(_frontieror_sys.argv):
|
| 116 |
+
result_path = _frontieror_sys.argv[index + 1]
|
| 117 |
+
break
|
| 118 |
+
if argument.startswith("--result_path="):
|
| 119 |
+
result_path = argument.split("=", 1)[1]
|
| 120 |
+
break
|
| 121 |
+
if not result_path:
|
| 122 |
+
return False
|
| 123 |
+
try:
|
| 124 |
+
with open(result_path, "w") as result_handle:
|
| 125 |
+
_frontieror_json.dump(
|
| 126 |
+
{
|
| 127 |
+
"feasible": False,
|
| 128 |
+
"violated_constraints": ["malformed_solution"],
|
| 129 |
+
"violations": [
|
| 130 |
+
"checker rejected malformed candidate: "
|
| 131 |
+
+ type(error).__name__
|
| 132 |
+
+ ": "
|
| 133 |
+
+ str(error)[:500]
|
| 134 |
+
],
|
| 135 |
+
"violation_magnitudes": [],
|
| 136 |
+
},
|
| 137 |
+
result_handle,
|
| 138 |
+
indent=2,
|
| 139 |
+
)
|
| 140 |
+
except Exception:
|
| 141 |
+
return False
|
| 142 |
+
return True
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
TOL = 1e-5
|
| 146 |
+
EPS = 1e-5
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def check_feasibility(instance, solution):
|
| 150 |
+
n = instance["n"]
|
| 151 |
+
capacity = instance["capacity"]
|
| 152 |
+
items = instance["items"]
|
| 153 |
+
|
| 154 |
+
# Build lookup from item id to item data
|
| 155 |
+
item_map = {item["id"]: item for item in items}
|
| 156 |
+
valid_ids = set(item_map.keys())
|
| 157 |
+
|
| 158 |
+
selected = solution.get("selected_items", [])
|
| 159 |
+
|
| 160 |
+
violated_constraints = set()
|
| 161 |
+
violations = []
|
| 162 |
+
violation_magnitudes = []
|
| 163 |
+
|
| 164 |
+
# -------------------------------------------------------------------------
|
| 165 |
+
# Constraint 2 (1c): x_j in {0, 1} — each selected item must be a valid
|
| 166 |
+
# item id, appear at most once, and be within {1, ..., n}.
|
| 167 |
+
# -------------------------------------------------------------------------
|
| 168 |
+
seen = set()
|
| 169 |
+
invalid_items = []
|
| 170 |
+
duplicate_items = []
|
| 171 |
+
for item_id in selected:
|
| 172 |
+
if item_id not in valid_ids:
|
| 173 |
+
invalid_items.append(item_id)
|
| 174 |
+
if item_id in seen:
|
| 175 |
+
duplicate_items.append(item_id)
|
| 176 |
+
seen.add(item_id)
|
| 177 |
+
|
| 178 |
+
if invalid_items:
|
| 179 |
+
violated_constraints.add(2)
|
| 180 |
+
violations.append(
|
| 181 |
+
f"Selected items not in instance: {invalid_items}"
|
| 182 |
+
)
|
| 183 |
+
for item_id in invalid_items:
|
| 184 |
+
# violation_amount = 1 per invalid item (binary domain violation)
|
| 185 |
+
violation_magnitudes.append({
|
| 186 |
+
"constraint": 2,
|
| 187 |
+
"lhs": float(item_id),
|
| 188 |
+
"rhs": 0.0,
|
| 189 |
+
"raw_excess": 1.0,
|
| 190 |
+
"normalizer": max(abs(0.0), EPS),
|
| 191 |
+
"ratio": 1.0 / max(abs(0.0), EPS),
|
| 192 |
+
})
|
| 193 |
+
|
| 194 |
+
if duplicate_items:
|
| 195 |
+
violated_constraints.add(2)
|
| 196 |
+
violations.append(
|
| 197 |
+
f"Duplicate items in selection: {duplicate_items}"
|
| 198 |
+
)
|
| 199 |
+
for item_id in duplicate_items:
|
| 200 |
+
violation_magnitudes.append({
|
| 201 |
+
"constraint": 2,
|
| 202 |
+
"lhs": 2.0,
|
| 203 |
+
"rhs": 1.0,
|
| 204 |
+
"raw_excess": 1.0,
|
| 205 |
+
"normalizer": max(abs(1.0), EPS),
|
| 206 |
+
"ratio": 1.0 / max(abs(1.0), EPS),
|
| 207 |
+
})
|
| 208 |
+
|
| 209 |
+
# -------------------------------------------------------------------------
|
| 210 |
+
# Constraint 1 (1b): sum_{j=1}^{n} w_j x_j <= c
|
| 211 |
+
# -------------------------------------------------------------------------
|
| 212 |
+
total_weight = 0.0
|
| 213 |
+
for item_id in seen:
|
| 214 |
+
if item_id in item_map:
|
| 215 |
+
total_weight += item_map[item_id]["weight"]
|
| 216 |
+
|
| 217 |
+
lhs = total_weight
|
| 218 |
+
rhs = float(capacity)
|
| 219 |
+
violation_amount = lhs - rhs # for <= constraint: how much LHS exceeds RHS
|
| 220 |
+
|
| 221 |
+
if violation_amount > TOL:
|
| 222 |
+
violated_constraints.add(1)
|
| 223 |
+
violations.append(
|
| 224 |
+
f"Capacity exceeded: total weight {lhs} > capacity {rhs}"
|
| 225 |
+
)
|
| 226 |
+
normalizer = max(abs(rhs), EPS)
|
| 227 |
+
violation_magnitudes.append({
|
| 228 |
+
"constraint": 1,
|
| 229 |
+
"lhs": lhs,
|
| 230 |
+
"rhs": rhs,
|
| 231 |
+
"raw_excess": violation_amount,
|
| 232 |
+
"normalizer": normalizer,
|
| 233 |
+
"ratio": violation_amount / normalizer,
|
| 234 |
+
})
|
| 235 |
+
|
| 236 |
+
# -------------------------------------------------------------------------
|
| 237 |
+
# Constraint 3 (obj): reported objective_value must equal
|
| 238 |
+
# sum_{j=1}^{n} p_j x_j. Tier-C defense against LLM exploits that lie
|
| 239 |
+
# about the objective value while passing the constraint checks.
|
| 240 |
+
# -------------------------------------------------------------------------
|
| 241 |
+
reported_obj = solution.get("objective_value")
|
| 242 |
+
if reported_obj is not None:
|
| 243 |
+
try:
|
| 244 |
+
reported = float(reported_obj)
|
| 245 |
+
except (TypeError, ValueError):
|
| 246 |
+
reported = None
|
| 247 |
+
if reported is not None:
|
| 248 |
+
# Recompute over unique, valid selected items (matches weight sum)
|
| 249 |
+
true_obj = 0.0
|
| 250 |
+
for item_id in seen:
|
| 251 |
+
if item_id in item_map:
|
| 252 |
+
true_obj += float(item_map[item_id]["profit"])
|
| 253 |
+
abs_diff = abs(reported - true_obj)
|
| 254 |
+
# Profits are positive integers; an integer mismatch by >=1 fires.
|
| 255 |
+
# Add a tiny relative slack for large magnitudes (true_obj can be
|
| 256 |
+
# ~1e10), so float round-trips never cause false positives.
|
| 257 |
+
tol = max(0.5, 1e-9 * abs(true_obj))
|
| 258 |
+
if abs_diff > tol:
|
| 259 |
+
violated_constraints.add(3)
|
| 260 |
+
violations.append(
|
| 261 |
+
f"Objective consistency violated: reported "
|
| 262 |
+
f"objective_value={reported} differs from recomputed "
|
| 263 |
+
f"sum_j(p_j*x_j)={true_obj} "
|
| 264 |
+
f"(|diff|={abs_diff:.3g}, tol={tol:.3g})"
|
| 265 |
+
)
|
| 266 |
+
normalizer = max(abs(true_obj), EPS)
|
| 267 |
+
violation_magnitudes.append({
|
| 268 |
+
"constraint": 3,
|
| 269 |
+
"lhs": reported,
|
| 270 |
+
"rhs": true_obj,
|
| 271 |
+
"raw_excess": abs_diff,
|
| 272 |
+
"normalizer": normalizer,
|
| 273 |
+
"ratio": abs_diff / normalizer,
|
| 274 |
+
})
|
| 275 |
+
|
| 276 |
+
# -------------------------------------------------------------------------
|
| 277 |
+
# Build result
|
| 278 |
+
# -------------------------------------------------------------------------
|
| 279 |
+
feasible = len(violated_constraints) == 0
|
| 280 |
+
result = {
|
| 281 |
+
"feasible": feasible,
|
| 282 |
+
"violated_constraints": sorted(violated_constraints),
|
| 283 |
+
"violations": violations,
|
| 284 |
+
"violation_magnitudes": violation_magnitudes,
|
| 285 |
+
}
|
| 286 |
+
return result
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
def main():
|
| 290 |
+
parser = argparse.ArgumentParser(
|
| 291 |
+
description="Feasibility checker for 0-1 Knapsack Problem "
|
| 292 |
+
"(Martello, Pisinger, Toth 1999)"
|
| 293 |
+
)
|
| 294 |
+
parser.add_argument("--instance_path", type=str, required=True,
|
| 295 |
+
help="Path to the JSON instance file")
|
| 296 |
+
parser.add_argument("--solution_path", type=str, required=True,
|
| 297 |
+
help="Path to the JSON solution file")
|
| 298 |
+
parser.add_argument("--result_path", type=str, required=True,
|
| 299 |
+
help="Path for the output feasibility result JSON")
|
| 300 |
+
args = parser.parse_args()
|
| 301 |
+
|
| 302 |
+
with open(args.instance_path, "r") as f:
|
| 303 |
+
instance = json.load(f)
|
| 304 |
+
with open(args.solution_path, "r") as f:
|
| 305 |
+
solution = json.load(f)
|
| 306 |
+
_frontieror_contract_result = _frontieror_validate_solution_contract(solution)
|
| 307 |
+
if _frontieror_contract_result is not None:
|
| 308 |
+
with open(args.result_path, "w") as _frontieror_result_handle:
|
| 309 |
+
json.dump(
|
| 310 |
+
_frontieror_contract_result,
|
| 311 |
+
_frontieror_result_handle,
|
| 312 |
+
indent=2,
|
| 313 |
+
)
|
| 314 |
+
return
|
| 315 |
+
|
| 316 |
+
result = check_feasibility(instance, solution)
|
| 317 |
+
|
| 318 |
+
with open(args.result_path, "w") as f:
|
| 319 |
+
json.dump(result, f, indent=2)
|
| 320 |
+
|
| 321 |
+
status = "FEASIBLE" if result["feasible"] else "INFEASIBLE"
|
| 322 |
+
print(f"Result: {status}")
|
| 323 |
+
if not result["feasible"]:
|
| 324 |
+
for v in result["violations"]:
|
| 325 |
+
print(f" - {v}")
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
if __name__ == "__main__":
|
| 329 |
+
try:
|
| 330 |
+
main()
|
| 331 |
+
except Exception as _frontieror_unexpected_error:
|
| 332 |
+
if not _frontieror_write_unexpected_rejection(
|
| 333 |
+
_frontieror_unexpected_error
|
| 334 |
+
):
|
| 335 |
+
raise
|
tasks/martello1999/gurobi_code.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gurobi implementation of the 0-1 Knapsack Problem (KP).
|
| 3 |
+
Based on: Martello, Pisinger, and Toth (1999)
|
| 4 |
+
"Dynamic Programming and Strong Bounds for the 0-1 Knapsack Problem"
|
| 5 |
+
Management Science 45(3): 414-424
|
| 6 |
+
|
| 7 |
+
Formulation (1):
|
| 8 |
+
maximize z = sum_{j=1}^{n} p_j x_j
|
| 9 |
+
subject to sum_{j=1}^{n} w_j x_j <= c
|
| 10 |
+
x_j in {0, 1}, j = 1, ..., n
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import argparse
|
| 14 |
+
import json
|
| 15 |
+
import gurobipy as gp
|
| 16 |
+
from gurobipy import GRB
|
| 17 |
+
import os as _os, sys as _sys
|
| 18 |
+
import time
|
| 19 |
+
# Walk up from this file's directory to find repo root (containing scripts/).
|
| 20 |
+
_GUROBI_CODE_START_TIME = time.time()
|
| 21 |
+
_repo = _os.path.dirname(_os.path.abspath(__file__))
|
| 22 |
+
while _repo != _os.path.dirname(_repo) and not _os.path.isdir(_os.path.join(_repo, 'scripts', 'utils')):
|
| 23 |
+
_repo = _os.path.dirname(_repo)
|
| 24 |
+
if _os.path.isdir(_os.path.join(_repo, 'scripts', 'utils')):
|
| 25 |
+
_sys.path.insert(0, _repo)
|
| 26 |
+
try:
|
| 27 |
+
from scripts.utils.gurobi_log_helper import install_gurobi_logger
|
| 28 |
+
except ImportError:
|
| 29 |
+
def install_gurobi_logger(log_path): # no-op fallback when scripts/ unavailable
|
| 30 |
+
pass
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def solve_knapsack(instance_path, solution_path, time_limit):
|
| 34 |
+
# Load instance
|
| 35 |
+
with open(instance_path, "r") as f:
|
| 36 |
+
data = json.load(f)
|
| 37 |
+
|
| 38 |
+
n = data["n"]
|
| 39 |
+
capacity = data["capacity"]
|
| 40 |
+
items = data["items"]
|
| 41 |
+
|
| 42 |
+
profits = [item["profit"] for item in items]
|
| 43 |
+
weights = [item["weight"] for item in items]
|
| 44 |
+
|
| 45 |
+
# Build model
|
| 46 |
+
model = gp.Model("knapsack_01")
|
| 47 |
+
model.setParam("Threads", 1)
|
| 48 |
+
model.setParam("TimeLimit", time_limit)
|
| 49 |
+
model.setParam("OutputFlag", 1)
|
| 50 |
+
|
| 51 |
+
# Decision variables: x_j in {0, 1}
|
| 52 |
+
x = model.addVars(n, vtype=GRB.BINARY, name="x")
|
| 53 |
+
|
| 54 |
+
# Objective: maximize sum p_j * x_j
|
| 55 |
+
model.setObjective(
|
| 56 |
+
gp.quicksum(profits[j] * x[j] for j in range(n)),
|
| 57 |
+
GRB.MAXIMIZE
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Constraint: sum w_j * x_j <= c
|
| 61 |
+
model.addConstr(
|
| 62 |
+
gp.quicksum(weights[j] * x[j] for j in range(n)) <= capacity,
|
| 63 |
+
name="capacity"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Solve
|
| 67 |
+
model.optimize()
|
| 68 |
+
|
| 69 |
+
# Extract solution
|
| 70 |
+
result = {"objective_value": None, "selected_items": []}
|
| 71 |
+
|
| 72 |
+
if model.SolCount > 0:
|
| 73 |
+
result["objective_value"] = round(model.ObjVal)
|
| 74 |
+
result["selected_items"] = [
|
| 75 |
+
items[j]["id"] for j in range(n) if x[j].X > 0.5
|
| 76 |
+
]
|
| 77 |
+
else:
|
| 78 |
+
result["objective_value"] = 0
|
| 79 |
+
result["selected_items"] = []
|
| 80 |
+
|
| 81 |
+
# Write solution
|
| 82 |
+
with open(solution_path, "w") as f:
|
| 83 |
+
result["runtime"] = time.time() - _GUROBI_CODE_START_TIME
|
| 84 |
+
json.dump(result, f, indent=2)
|
| 85 |
+
|
| 86 |
+
print(f"Objective value: {result['objective_value']}")
|
| 87 |
+
print(f"Solution written to: {solution_path}")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
parser = argparse.ArgumentParser(
|
| 92 |
+
description="Solve 0-1 Knapsack Problem using Gurobi"
|
| 93 |
+
)
|
| 94 |
+
parser.add_argument("--instance_path", type=str, required=True,
|
| 95 |
+
help="Path to the JSON instance file")
|
| 96 |
+
parser.add_argument("--solution_path", type=str, required=True,
|
| 97 |
+
help="Path for the output solution JSON file")
|
| 98 |
+
parser.add_argument("--time_limit", type=int, required=True,
|
| 99 |
+
help="Maximum solver runtime in seconds")
|
| 100 |
+
parser.add_argument("--log_path", type=str, default=None, help="Path to log incumbent solutions")
|
| 101 |
+
args = parser.parse_args()
|
| 102 |
+
install_gurobi_logger(args.log_path)
|
| 103 |
+
|
| 104 |
+
solve_knapsack(args.instance_path, args.solution_path, args.time_limit)
|
tasks/martello1999/gurobi_feasi_result/large_feasi_result_1.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/martello1999/gurobi_feasi_result/large_feasi_result_2.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/martello1999/gurobi_feasi_result/large_feasi_result_3.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/martello1999/gurobi_feasi_result/large_feasi_result_4.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/martello1999/gurobi_feasi_result/large_feasi_result_5.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/martello1999/gurobi_feasi_result/tiny_feasi_result.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3269392edcbb7720882e77caadb88b4a9cf2fda04cf482cebd23baace4a531fd
|
| 3 |
+
size 102
|
tasks/martello1999/gurobi_solution/large_solution_1.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1296c00ed28e79f1671725129476eebb007c74a8193f004970c90c4297a402d6
|
| 3 |
+
size 63806716
|
tasks/martello1999/gurobi_solution/large_solution_2.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:518a647e46fe180aa933561a2a59dd3b931530ba22e7f0a5e68c4baa4f391d22
|
| 3 |
+
size 80079975
|
tasks/martello1999/gurobi_solution/large_solution_3.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4a8c26ece18b9a696284ff68659ce90cc252a8682443695a540395b447ae7693
|
| 3 |
+
size 66330793
|
tasks/martello1999/gurobi_solution/large_solution_4.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b877be81dd7d3bf81ae1b9444408674bf20bd9cd7faf86aac74f8e38463f2002
|
| 3 |
+
size 90683375
|
tasks/martello1999/gurobi_solution/large_solution_5.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:81fccb416ccfebfec47eb2df9a43b07764a2cc9309db91193e50fcaee598c0fe
|
| 3 |
+
size 90693970
|
tasks/martello1999/gurobi_solution/tiny_solution.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c71515c0d9635751f49c2b4963c21b66f9b3b022136725f86c3f15be6273e3a9
|
| 3 |
+
size 335
|
tasks/martello1999/gurobi_solution_log/large_log_1.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:99e82e84bae1a7306917bb59ee5722ffe12328509ecf5dd0b49ab57c1af0a9ea
|
| 3 |
+
size 51
|
tasks/martello1999/gurobi_solution_log/large_log_2.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:243420c0f21c2cdb7bab2f6f5dba1543985d2695825d058e6592b74ff47b1ad3
|
| 3 |
+
size 52
|
tasks/martello1999/gurobi_solution_log/large_log_3.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eb1c6065a53036dd0fe1d4e7c65d6845756f4d60747a24910652d66f77dc1bd0
|
| 3 |
+
size 52
|
tasks/martello1999/gurobi_solution_log/large_log_4.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dafbea693415f1af3eab45342d228359c1358e9383adedde9729c4a393437939
|
| 3 |
+
size 52
|
tasks/martello1999/gurobi_solution_log/large_log_5.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:458cfbf573fb5d6847239dcfaf41d0b1b8e8f5c40493da8f0a2a53c4eb140b1c
|
| 3 |
+
size 51
|
tasks/martello1999/gurobi_solution_log/tiny_log.jsonl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:24cf240d348bbc12d7a457028e07073652903340cf6596b024231e8d22565d42
|
| 3 |
+
size 44
|
tasks/martello1999/instance/large_instance_1.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e09cc29b5c2514960d9746f8e6d7afb4d1949a0fba08b10c7f00507bf60c3c87
|
| 3 |
+
size 777820124
|
tasks/martello1999/instance/large_instance_2.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:981732aed63a47c23ebba870b384c19774dc5a3d1875a29d1e7ecdff0f12666e
|
| 3 |
+
size 786670683
|
tasks/martello1999/instance/large_instance_3.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6ef33692340bf5cee1738fa430f31eb245406ee186a0d74ba5aa93072b01841c
|
| 3 |
+
size 787005356
|