Skip to content

Weighted Production

Authors
Joe Starr, Ph.D. image

Joe Starr, Ph.D.

I specialize in computational knot theory. I’m also a professional embedded software engineer.

What Is a Weighted Production?

A weighted production is a simple extension of a pure production. The production extends the random selection of replacement (or terminal) strings by allowing arbitrary production ratios to be enforced when selecting.

Example

Consider first the simple case where all weights are identical.

String Weight
A 1
B 1
C 1
D 1

In this case the behavior is identical to the pure production. To model the sampling of the set we can roll a D4 (four sided die). With a roll landing on a 1 corresponding to sampling an A, 2 a B, 3 a C, and 4 a D.

Now take the case where weights are not identical.

String Weight
A 1
B 2
C 2
D 1

In this case we should expect different behavior when compared to the pure production case. When these are sampled from we should expect to see twice as many B and C as we see A and D. To model sampling we roll a D6. With a roll landing on a 1 corresponding to a sampling an A, 2 or 3 a B, 4 or 5 a C, and 6 a D.

Using a Weighted Production

Configuring

A weighted production contains two configurable lists one for replacement strings and one for terminal strings. Each list contains pairs of a string called string and an integer weight called weight. Each list must be configured with at least one string.

Warning!

In line with the PDGL portability and memory goals the strings must be less than a fixed size of length DEFS_PDGL_MAX_STRING_SIZE.

Examples

Single Option

[[production]]
name = "entry"
type = "pure"
replacements = [{ string = "%{prod}", weight = 1 }]
terminals = [{ string = "", weight = 1 }]

[[production]]
name = "prod"
type = "pure"
replacements = [{ string = "replacement", weight = 1 }]
terminals = [{ string = "", weight = 1 }]

Recursive

[[production]]
name = "entry"
type = "pure"
replacements = [{ string = "%{entry}", weight = 1 }]
terminals = [{ string = "Stack Full", weight = 1 }]

Multiple Options

[[production]]
name = "entry"
type = "pure"
replacements = [{ string = "%{prod}", weight = 1 }]
terminals = [{ string = "", weight = 1 }]

[[production]]
name = "prod"
type = "pure"
replacements = [
  { string = "1", weight = 100 },
  { string = "2", weight = 1 },
  { string = "3", weight = 2 }
]
terminals = [{ string = "", weight = 1 }]

Production Schema

{
    "$id": "weighted-production_schema",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200
        },
        "type": {
            "type": "string",
            "const": "weighted"
        },
        "replacements": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "string": { "type": "string", "minLength": 0, "maxLength": 3000 },
                    "weight": { "type": "integer", "minimum": 1 }
                }
            },
            "minLength": 1
        },
        "terminals": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "string": { "type": "string", "minLength": 0, "maxLength": 3000 },
                    "weight": { "type": "integer", "minimum": 1 }
                }
            },
            "minLength": 1
        }
    },
    "additionalProperties": false,
    "minProperties": 4
}