Skip to content

Your First Slitting Plan: Demand and Plan Usage ​

Work through business input, solving and result extraction with this example. Prepare dependencies and backends using Setup.

1. Business Scenario ​

Produce 0.8 m wide strips with demand of three rolls from stock with width range 0.5–1.5 m, without a machine configuration. Three is a roll count, not 3 m or three pattern usages. Rust additionally limits available stock batches to three.

Declare roll demand explicitly with ProductDemand.legacyRoll / legacy_roll. Production is each pattern contribution multiplied by its usage. Three generated candidates do not mean three produced rolls.

2. Construct Input and Call the Framework ​

Kotlin and Rust Entry Points

Kotlin uses an actual injected solver and returns a structured Csp1dSolution directly, not Ret. The Rust default entry needs no native optimizer but is heuristic. The name Milp does not establish native MILP execution; configure the master-solver interface separately when needed.

The examples include imports and error handling. The application supplies Kotlin ColumnGenerationSolver to runExample; the next section shows the launcher.

kt
package docs.examples.csp1d

import fuookami.ospf.kotlin.math.algebra.number.*
import fuookami.ospf.kotlin.quantities.quantity.*
import fuookami.ospf.kotlin.quantities.unit.Meter
import fuookami.ospf.kotlin.framework.solver.ColumnGenerationSolver
import fuookami.ospf.kotlin.framework.csp1d.application.model.*
import fuookami.ospf.kotlin.framework.csp1d.application.service.*
import fuookami.ospf.kotlin.framework.csp1d.domain.material.model.*

suspend fun runExample(solver: ColumnGenerationSolver) {
    fun meters(value: Double) = Quantity(Flt64(value), Meter)
    val product = Product(
        id = productIdOf("P1"), name = "strip", width = listOf(meters(0.8))
    )
    val material = Material(
        id = materialIdOf("M1"), name = "stock",
        widthRange = WidthRange(
            width = QuantityRange(lowerBound = meters(0.5), upperBound = meters(1.5)),
            step = meters(0.1)
        )
    )
    val problem = Csp1dProblem(
        products = listOf(product), materials = listOf(material), machines = emptyList(),
        demands = listOf(ProductDemand.legacyRoll(product, Flt64(3.0)))
    )
    // This API returns a structured solution, not Ret<Csp1dSolution>.
    val solution = Csp1dMilp<Flt64>(solver).solve(problem)
    // #region result
    println("status=${solution.status}, failure=${solution.failureMessage}, kpi=${solution.kpi}")
    println("unmet=${solution.produce.unmetDemands}")
    for (usage in solution.produce.cuttingPlans) {
        val plan = usage.plan
        println("plan=${plan.id}, material=${plan.material.id}, usages=${usage.amount}, rest=${plan.restWidth}")
        plan.slices.forEach { println("width=${it.width}, pieces per usage=${it.amount}") }
        plan.demandContributions.forEach { println("${it.product.id}: per usage=${it.quantity}, multiplier=${usage.amount}") }
    }
    check(solution.produce.unmetDemands.isEmpty()) { "Demand is not fully covered" }
    // #endregion result
}
rs
use ospf_rust_framework_csp1d::{Csp1dMilp, Csp1dProblem};
use ospf_rust_framework_csp1d::domain::material::{Material, Product, ProductDemand, WidthRange};
use ospf_rust_quantities::{Quantity, unit::{CTUnit, Meter, Unit}};

fn meters(value: f64) -> Quantity<f64, Unit> {
    Quantity::new(value, Meter::INSTANT.clone())
}

pub fn run_example() {
    let product = Product {
        id: "P1".into(), name: "strip".into(), width: vec![meters(0.8)],
        length: None, unit_weight: None, weight: None,
        max_over_produce_length: None, dynamic_length: false,
    };
    let material = Material {
        id: "M1".into(), name: "stock".into(),
        width_range: WidthRange::new(meters(0.5), meters(1.5)),
        length: None, unit_weight: None, machine_id: None, available_batches: 3,
    };
    let demand = ProductDemand::legacy_roll(product.clone(), 3.0);
    let problem = Csp1dProblem::new(vec![product], vec![material], vec![], vec![demand]);
    // The default implementation is heuristic; this does not invoke native MILP.
    let solution = Csp1dMilp::<f64>::default().solve(problem, None);
    // #region result
    println!("status={:?}, failure={:?}, kpi={:?}", solution.status, solution.failure_message, solution.kpi);
    println!("unmet={:?}", solution.produce.unmet_demands);
    for usage in &solution.produce.cutting_plans {
        println!("plan={}, material={}, usages={}, rest={:?}", usage.plan.id, usage.plan.material.id, usage.amount, usage.plan.rest_width());
        for slice in &usage.plan.slices { println!("width={:?}, pieces per usage={}", slice.width, slice.amount); }
        for c in &usage.plan.demand_contributions { println!("{}: per usage={:?}, multiplier={}", c.product.id, c.quantity, usage.amount); }
    }
    assert!(solution.produce.unmet_demands.is_empty(), "Demand is not fully covered");
    // #endregion result
}

3. Launch from Your Application ​

Place the Kotlin example in application sources, retaining its package. This standalone launcher calls the lesson function. Add the domain starter, Gurobi plugin and coroutines, and configure matching native libraries and a license.

kotlin
import docs.examples.csp1d.runExample
import kotlinx.coroutines.runBlocking
import fuookami.ospf.kotlin.core.solver.gurobi.GurobiColumnGenerationSolver

fun main() = runBlocking {
    runExample(GurobiColumnGenerationSolver())
}

Place the Rust example in src/csp1d.rs and use this launcher in src/main.rs. See Setup for dependencies and toolchain; do not enable async for these synchronous examples.

rust
mod csp1d;

fn main() {
    csp1d::run_example();
}

At the CLI boundary these examples throw or return errors. A server should translate them at its request boundary and retain diagnostics. Preserve reports when no feasible solution was found rather than inventing a technical failure or an empty successful plan.

4. Read and Validate the Result ​

ReadKotlinRust
Plans and usage countssolution.produce.cuttingPlans → plan / amountsolution.produce.cutting_plans → plan / amount
Material and remaining widthplan.material.id / slices / restWidthplan.material.id / slices / rest_width()
Contribution per executionplan.demandContributions → product.id / quantityplan.demand_contributions → product.id / quantity
Shortfall and statusproduce.unmetDemands, status / failureMessage / kpiproduce.unmet_demands, status / failure_message / kpi

Result Semantics

Aggregate production as Σ contribution per execution × usage. Plan count is not production. Remaining-width helpers report width, not placement intervals. There is no pool complete; inspect status, shortfalls and column-generation traces.

Read status, failure and unmet demand before selected plans and usages. Recalculate P1 production in rolls and verify pattern widths and available batches. A nonempty candidate pool does not establish coverage; partial output is not a complete delivery.

5. Change One Business Condition ​

Change demand from three rolls to four and check stock and contributions. Next try weight demand: changing a label to kg is insufficient; supply unit weight, roll length and conversion data, then update acceptance criteria.

Continue to the next business scenario and integration checks. Consult repository verification when checking source behavior.