Skip to content

Main content

Random 0/1 Knapsack Instance Generator

Generate integer weights and values for a single-capacity 0/1 knapsack, with an input-order feasible selection witness, not an optimum; weight and value units are caller-defined.

Loading tool…

Description

Generate integer weights and values for a single-capacity 0/1 knapsack, with an input-order feasible selection witness, not an optimum; weight and value units are caller-defined.

An optimization instance defines choices, constraints, and an objective. A feasible witness demonstrates that its stated constraints can be met; it establishes optimality only when the tool actually computes and verifies an optimum.

Generate integer weights and values for a single-capacity 0/1 knapsack, with an input-order feasible selection witness, not an optimum; weight and value units are caller-defined.

When to use this tool

Use this generator to create reproducible knapsack instance cases from the supplied model or bank. Inspect the returned evidence before using cases in a test or exercise.

How to use the tool and how it works

  1. Supply itemCount, capacity, minimumWeight, maximumWeight, minimumValue, maximumValue in the field shapes below. Set seed when you need an explicit replay.
  2. Run the tool and correct any input error. For generated controls, distinguish a successful fixture containing an invalid case from failure to generate the fixture.
  3. Read items, capacity, selected, totalWeight, totalValue. Use the conventions below and keep answer-bearing fields separate when presenting an exercise.

Draw positive integer item weights and values, then scan items in their original order and select each that fits remaining capacity. Return selected IDs and exact total weight and value as a feasible zero-or-one packing witness.1

Input fieldMeaning and unitsAccepted input
itemCountItem CountRequired. Safe integer; minimum 1; maximum 512.
capacityCapacityRequired. Safe integer; minimum 1; maximum 9007199254740991.
minimumWeightMinimum WeightRequired. Safe integer; minimum 1; maximum 9007199254740991.
maximumWeightMaximum WeightRequired. Safe integer; minimum 1; maximum 9007199254740991.
minimumValueMinimum ValueRequired. Safe integer; minimum 0; maximum 9007199254740991.
maximumValueMaximum ValueRequired. Safe integer; minimum 0; maximum 9007199254740991.
seedOptional numeric seed; takes precedence over context randomness. Without either, use the fixed tool.io default seed.Optional. Safe integer.
Output fieldInterpretationReturned shape
itemsItemsRequired. Array of Object; no unknown keys.; minimum items 1; maximum items 512.
items[].idLiteral identifier used to match records, independently of their display labels.Required. Safe integer; minimum 0; maximum 511.
items[].weightWeightRequired. Safe integer; minimum 1; maximum 9007199254740991.
items[].valueValueRequired. Safe integer; minimum 0; maximum 9007199254740991.
capacityCapacityRequired. Safe integer; minimum 1; maximum 9007199254740991.
selectedSelectedRequired. Array of Safe integer; minimum 0; maximum 511.; maximum items 512.
totalWeightTotal WeightRequired. Safe integer; minimum 0; maximum 9007199254740991.
totalValueTotal ValueRequired. Safe integer; minimum 0; maximum 9007199254740991.
randomnessReplay metadata: source is seed or context; seed is the normalized 32-bit seed or null for a context source.Required. Object; no unknown keys.
randomness.seedOptional numeric replay seed; normalization and precedence are explained below.Required. Safe integer. or Null.
randomness.sourceSourceRequired. Text; one of "context", "seed".

An explicit safe-integer seed takes precedence over injected context randomness and is normalized to an unsigned 32-bit integer. With no seed and no context source, replay uses the fixed tool.io default seed. A context source must return finite values from zero inclusive to one exclusive; its advancing state must be retained separately. randomness records the source and normalized seed, or null for context. These generators are not cryptographic random sources.

json
{
  "input": {
    "itemCount": 5,
    "capacity": 12,
    "minimumWeight": 1,
    "maximumWeight": 8,
    "minimumValue": 10,
    "maximumValue": 50,
    "seed": 42
  },
  "output": {
    "items": [
      {
        "id": 0,
        "weight": 5,
        "value": 28
      },
      {
        "id": 1,
        "weight": 7,
        "value": 37
      },
      {
        "id": 2,
        "weight": 2,
        "value": 31
      },
      {
        "id": 3,
        "weight": 3,
        "value": 35
      },
      {
        "id": 4,
        "weight": 7,
        "value": 29
      }
    ],
    "capacity": 12,
    "selected": [
      0,
      1
    ],
    "totalWeight": 12,
    "totalValue": 65,
    "randomness": {
      "seed": 42,
      "source": "seed"
    }
  }
}

Limitations and assumptions

  • Every possible item weight must individually fit capacity. The greedy witness is not an optimal knapsack solution and does not sort by value or value-to-weight ratio.
  • A replay requires the same complete input and implementation. Randomized construction does not guarantee uniform coverage of every feasible output; finite samples can repeat unless a distinct-selection rule is explicitly stated.
  • The field tables state structural bounds; cross-field constraints and bounded search or output budgets also apply. Unknown keys are rejected where the object contract disallows them. Input errors are distinct from null fields and deliberate invalid controls in a successful output.

Alternative or Complementary methods

For a supplied example, inspect the returned construction or delegated check directly. To evaluate a consumer, also test cases chosen for its own boundaries; a generated finite corpus does not establish exhaustive coverage.

References

  1. Tool behavior and example: Random 0/1 Knapsack Instance Generator — tool.io

Similar or alternative tools

  • Random Interval Scheduling Challenge Generator

    Generate synthetic positive-duration half-open jobs and solve the declared count or weight objective exactly with finish-time dynamic programming, retaining one optimum with exclusion on objective ties.

  • Random Job-Shop Scheduling Instance Generator

    Generate positive integer processing times and a per-job random machine permutation with a feasible job-priority schedule witness; no makespan optimality claim.