logistics-rules-to-optimization

$npx mdskill add elizaOS/eliza/logistics-rules-to-optimization

Use this skill when the problem statement gives operational rules in words and the agent must turn them into an optimization model.

SKILL.md

.github/skills/logistics-rules-to-optimizationView on GitHub ↗
---
name: logistics-rules-to-optimization
description: Translate logistics and operations rules into optimization variables and constraints. Use when an operations problem describes vehicles, routes, depots, pickups, dropoffs, inventory, capacity, assignments, time windows, service targets, penalties, resource limits, or other business rules that need to become an optimization model.
---

# Logistics Rules To Optimization

Use this skill when the problem statement gives operational rules in words and the agent must turn them into an optimization model.

The goal is not only routing. The same translation pattern applies to transportation, dispatch, rebalancing, warehouse moves, staffing, scheduling, assignment, capacity planning, production, and service-level problems.

## Rule Translation Workflow

1. List the entities.
   - Examples: vehicles, locations, depots, jobs, workers, machines, products, arcs, time periods.

2. Choose the decision state.
   - Binary variables for yes/no choices.
   - Integer variables for counts, loads, inventory, units moved.
   - Continuous variables for time, flow, cost, utilization, or fractional quantities.

3. Convert each business rule into one of these patterns.
   - Conservation: what enters equals what leaves, plus/minus changes.
   - Capacity: quantity cannot exceed a limit.
   - Linking: a quantity is allowed only if a binary decision is active.
   - Assignment: exactly one, at most one, or at least one choice.
   - Sequence: if one action follows another, update load/time/state.
   - Compatibility: prohibit impossible combinations.
   - Soft penalty: add slack for unmet demand or violation cost.

4. Add the objective last.
   - Keep named components such as travel cost, labor cost, inventory penalty, unmet demand penalty.

5. Extract and independently validate the answer.
   - Recompute routes, loads, assignments, inventory, penalties, and objective from the output data.

## Variable Patterns

### Selection and Assignment

Use binary variables when an option is selected.

```python
x = {(i, j): model.addVar(vtype="B", name=f"x_{i}_{j}") for i in I for j in J}
```

Common rules:

```python
# each item i assigned to exactly one option j
for i in I:
    model.addCons(quicksum(x[i, j] for j in J) == 1)

# option j can handle at most capacity[j] items
for j in J:
    model.addCons(quicksum(x[i, j] for i in I) <= capacity[j])
```

### Route Arcs

Use binary arc variables when the order of visits matters.

```python
x = {
    (v, i, j): model.addVar(vtype="B", name=f"x_{v}_{i}_{j}")
    for v in vehicles
    for i, j in arcs
}
```

Use `x[v, i, j] = 1` to mean vehicle/resource `v` goes directly from node `i` to node `j`.

### Visit Indicator

Define visit from route arcs instead of creating a second binary unless the model needs it repeatedly.

```python
visit = quicksum(x[v, i, j] for j in to_nodes if j != i)
```

If a standalone variable is useful:

```python
visit = {(v, i): model.addVar(vtype="B", name=f"visit_{v}_{i}") for v in vehicles for i in locations}

for v in vehicles:
    for i in locations:
        model.addCons(visit[v, i] == quicksum(x[v, i, j] for j in to_nodes if j != i))
```

### Quantity, Load, Inventory, and Time

```python
load = {(v, i): model.addVar(vtype="I", lb=0, ub=vehicle_capacity, name=f"load_{v}_{i}") for v in vehicles for i in nodes}
service = {(v, i): model.addVar(vtype="I", lb=-vehicle_capacity, ub=vehicle_capacity, name=f"service_{v}_{i}") for v in vehicles for i in locations}
inventory = {(i, t): model.addVar(vtype="I", lb=0, ub=storage_capacity[i], name=f"inventory_{i}_{t}") for i in locations for t in periods}
arrival = {(v, i): model.addVar(vtype="C", lb=0, name=f"arrival_{v}_{i}") for v in vehicles for i in nodes}
```

Use integer variables for physical unit counts when the output must be integer-valued.

## Common Logistics Rules

| Business Rule | Variable Choice | Constraint Pattern |
| --- | --- | --- |
| Choose exactly one option | `x[i,j]` binary | `sum_j x[i,j] == 1` |
| Choose at most one option | `x[i,j]` binary | `sum_j x[i,j] <= 1` |
| Open facility before assigning to it | `open[j]`, `assign[i,j]` binary | `assign[i,j] <= open[j]` |
| Resource capacity | quantity variable | `sum_i q[i,j] <= capacity[j]` |
| Quantity only if selected | `q[i]`, `use[i]` | `q[i] <= M * use[i]` |
| Fixed cost if used | `use[i]` binary | add `fixed_cost[i] * use[i]` to objective |
| Mutually exclusive modes | mode binaries | `sum_m mode[i,m] <= 1` |
| Incompatible pair | two binaries | `x[a] + x[b] <= 1` |
| Demand must be met | flow/quantity | `supply_to[i] >= demand[i]` |
| Demand may be unmet | nonnegative slack | `served[i] + unmet[i] >= demand[i]` |
| Absolute deviation penalty | nonnegative slack | `actual-target <= dev`, `target-actual <= dev` |
| Inventory balance | inventory variables | `inv[t+1] = inv[t] + inbound - outbound` |
| Station/storage upper bound | inventory variable | `inv[i,t] <= capacity[i]` |
| Cannot remove unavailable stock | move variable | `outbound[i,t] <= inv[i,t]` |
| Vehicle starts at depot | arc variables | `sum_j x[v, START, j] == use_vehicle[v]` |
| Vehicle ends at depot | arc variables | `sum_i x[v, i, END] == use_vehicle[v]` |
| Route continuity | arc variables | `incoming[v,i] == outgoing[v,i]` |
| Visit at most once | arc variables | `outgoing[v,i] <= 1` |
| Split service allowed | arc/quantity variables | omit global single-visit; aggregate quantities over resources |
| Time window | arrival variable | `earliest[i] <= arrival[v,i] <= latest[i]` when visited |
| Travel time propagation | arc + arrival | `arrival[j] >= arrival[i] + service_time[i] + travel[i,j] - M(1-x[i,j])` |
| Precedence | start/arrival variables | `start[b] >= finish[a]` |
| Route duration limit | arc variables | `sum travel[i,j] * x[v,i,j] <= max_duration[v]` |

## Constraint Examples

### Capacity

```python
for r in resources:
    model.addCons(quicksum(amount[i, r] for i in items) <= capacity[r])
```

### Quantity Allowed Only When Active

Use the tightest possible `M`.

```python
for i in items:
    model.addCons(quantity[i] <= upper_bound[i] * use[i])
```

### Soft Demand Satisfaction

```python
unmet = {i: model.addVar(vtype="I", lb=0, name=f"unmet_{i}") for i in customers}

for i in customers:
    model.addCons(served[i] + unmet[i] >= demand[i])

penalty_cost = quicksum(penalty[i] * unmet[i] for i in customers)
```

### Absolute Target Deviation

Never use Python `abs()` on solver expressions.

```python
dev = {i: model.addVar(vtype="C", lb=0, name=f"dev_{i}") for i in items}

for i in items:
    model.addCons(actual[i] - target[i] <= dev[i])
    model.addCons(target[i] - actual[i] <= dev[i])
```

### Depot Start and End

If every vehicle must be used:

```python
for v in vehicles:
    model.addCons(quicksum(x[v, START, j] for j in locations) == 1)
    model.addCons(quicksum(x[v, i, END] for i in locations) == 1)
```

If vehicles are optional:

```python
use_vehicle = {v: model.addVar(vtype="B", name=f"use_vehicle_{v}") for v in vehicles}

for v in vehicles:
    model.addCons(quicksum(x[v, START, j] for j in locations) == use_vehicle[v])
    model.addCons(quicksum(x[v, i, END] for i in locations) == use_vehicle[v])
```

### Route Continuity and At-Most-Once Visits

```python
for v in vehicles:
    for i in locations:
        incoming = quicksum(x[v, j, i] for j in from_nodes if j != i)
        outgoing = quicksum(x[v, i, j] for j in to_nodes if j != i)

        model.addCons(incoming == outgoing)
        model.addCons(outgoing <= 1)
```

This means vehicle `v` visits location `i` no more than once. It does not prevent a different vehicle from also visiting `i`.

### Global Single-Visit Rule

Use only when the real rule forbids split service across vehicles/resources.

```python
for i in locations:
    model.addCons(
        quicksum(x[v, i, j] for v in vehicles for j in to_nodes if j != i) <= 1
    )
```

Do not add this rule when a large pickup/dropoff target may need multiple vehicles.

### Load or State Transition Along Selected Arcs

If `state[j] = state[i] + change[j]` when arc `(i, j)` is used:

```python
M = 2 * vehicle_capacity

for v in vehicles:
    for i, j in arcs:
        change_at_j = service[v, j] if isinstance(j, int) else 0
        model.addCons(load[v, j] - load[v, i] - change_at_j <= M * (1 - x[v, i, j]))
        model.addCons(load[v, j] - load[v, i] - change_at_j >= -M * (1 - x[v, i, j]))
```

This pattern works for load, arrival time, battery charge, inventory state, and other route-dependent state variables. Pick `M` from real variable bounds.

### Time Windows

```python
for v in vehicles:
    for i in locations:
        visit_i = quicksum(x[v, i, j] for j in to_nodes if j != i)
        model.addCons(arrival[v, i] >= earliest[i] - horizon * (1 - visit_i))
        model.addCons(arrival[v, i] <= latest[i] + horizon * (1 - visit_i))

    for i, j in arcs:
        if j in locations:
            model.addCons(
                arrival[v, j] >= arrival[v, i] + service_time.get(i, 0) + travel_time[i, j] - horizon * (1 - x[v, i, j])
            )
```

## Inventory Pickup/Dropoff Pattern

For rebalancing or material movement, define one signed service variable. Recommended convention:

- `service[v, i] > 0`: pickup from location `i`, vehicle load increases, location inventory decreases.
- `service[v, i] < 0`: dropoff to location `i`, vehicle load decreases, location inventory increases.

```python
service = {
    (v, i): model.addVar(vtype="I", lb=-vehicle_capacity, ub=vehicle_capacity, name=f"service_{v}_{i}")
    for v in vehicles
    for i in locations
}

for v in vehicles:
    for i in locations:
        visit_i = quicksum(x[v, i, j] for j in to_nodes if j != i)
        model.addCons(service[v, i] <= vehicle_capacity * visit_i)
        model.addCons(service[v, i] >= -vehicle_capacity * visit_i)

for i in locations:
    net_change = quicksum(service[v, i] for v in vehicles)
    free_space = storage_capacity[i] - initial_inventory[i]

    model.addCons(net_change <= initial_inventory[i])  # pickup cannot exceed stock
    model.addCons(net_change >= -free_space)           # dropoff cannot exceed space
```

If the target is a desired net pickup/dropoff:

```python
unmet = {i: model.addVar(vtype="I", lb=0, name=f"unmet_{i}") for i in locations}

for i in locations:
    net_change = quicksum(service[v, i] for v in vehicles)
    model.addCons(net_change - target[i] <= unmet[i])
    model.addCons(target[i] - net_change <= unmet[i])
```

Extract pickup/dropoff output as:

```python
picked_up = max(service_value, 0)
dropped_off = max(-service_value, 0)
```

## Objective Assembly

Build named components:

```python
travel_cost = quicksum(distance[i, j] * x[v, i, j] for v in vehicles for i, j in arcs)
fixed_cost = quicksum(vehicle_fixed_cost[v] * use_vehicle[v] for v in vehicles)
penalty_cost = quicksum(penalty[i] * unmet[i] for i in customers)

model.setObjective(travel_cost + fixed_cost + penalty_cost, "minimize")
```

More from elizaOS/eliza

SkillDescription
ac-branch-pi-modelAC branch pi-model power flow equations (P/Q and |S|) with transformer tap ratio and phase shift, matching `acopf-math-model.md` and MATPOWER branch fields. Use when computing branch flows in either direction, aggregating bus injections for nodal balance, checking MVA (rateA) limits, computing branch loading %, or debugging sign/units issues in AC power flow.
academic-pdf-redactionRedact text from PDF documents for blind review anonymization
ada-plan-view-accessibilityUse when checking simplified ADA-derived plan-view bathroom accessibility constraints such as turning space, door clear width, toilet centerline, grab bars, and lavatory knee/toe clearance.
analyze-ciAnalyze failed GitHub Action jobs for a pull request.
architectural-dxf-extractionUse when extracting plan-view architectural geometry from DXF files with semantic CAD layers, especially when outputs must normalize rooms, doors, fixtures, clearances, and grab bars into machine-checkable JSON.
attitude-controller-plannerUse this skill when implementing the inner control loop for a quadrotor — attitude (roll/pitch/yaw) PID control and attitude planning (converting desired acceleration to desired Euler angles). Covers gain layout, integral reset pattern, and the attitude planner inverse kinematics.
azure-bgpAnalyze and resolve BGP oscillation and BGP route leaks in Azure Virtual WAN–style hub-and-spoke topologies (and similar cloud-managed BGP environments). Detect preference cycles, identify valley-free violations, and propose allowed policy-level mitigations while rejecting prohibited fixes.
box-least-squaresBox Least Squares (BLS) periodogram for detecting transiting exoplanets and eclipsing binaries. Use when searching for periodic box-shaped dips in light curves. Alternative to Transit Least Squares, available in astropy.timeseries. Based on Kovács et al. (2002).
browser-testingVERIFY your changes work. Measure CLS, detect theme flicker, test visual stability, check performance. Use BEFORE and AFTER making changes to confirm fixes. Includes ready-to-run scripts: measure-cls.ts, detect-flicker.ts
cache-policy-comparisonCompare and implement eviction policies (LRU, LFU, FIFO, S3FIFO, ARC) for bounded-capacity caches. Use when choosing or implementing an eviction policy for a buffer pool, page cache, CDN edge, or LLM KV cache, or when writing a replay simulator that supports multiple policies. Clarifies recency vs frequency semantics, queue topology, saturating counters, ghost buffers, and the second-chance rule that distinguishes modern FIFO-family policies from classic LRU.