第一次卷材分切:需求与方案使用量
本课通过业务示例学习输入、求解和结果解析。先按环境页准备依赖和后端。
1. 业务场景
生产宽 0.8 m 的产品,需求 3 卷。原料幅宽范围 0.5–1.5 m,先不配置机器。这里的 3 是卷数,不是长度 3 m,也不是方案使用次数。Rust 示例另设原料可用批次为 3。
使用 ProductDemand.legacyRoll / legacy_roll 显式声明卷数。每个方案有自己的产品产出,实际产量是“单次产出 × 使用次数”的汇总;生成了三个候选不代表已经生产三卷。
2. 构造输入并调用框架
Kotlin 与 Rust 的入口差异
Kotlin 使用实际求解器的普通入口,并直接返回结构化 Csp1dSolution,不是 Ret。Rust 默认入口无需原生求解器,但内部使用启发式,不能从方法名 Milp 推断调用了原生 MILP。需要原生主问题时再配置对应求解器接口。
示例包含导入和错误处理。Kotlin 的 runExample 接收应用提供的 ColumnGenerationSolver,下一节给出启动入口。
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
}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. 在自己的程序中启动
保留包声明,将 Kotlin 示例放入应用源码目录。下面的独立入口调用本课函数;需添加对应领域的集成依赖、Gurobi 插件和协程依赖,配置匹配的原生库与许可证。
import docs.examples.csp1d.runExample
import kotlinx.coroutines.runBlocking
import fuookami.ospf.kotlin.core.solver.gurobi.GurobiColumnGenerationSolver
fun main() = runBlocking {
runExample(GurobiColumnGenerationSolver())
}将 Rust 示例函数放入 src/csp1d.rs,在 src/main.rs 使用以下入口。依赖与 工具链 见环境页;同步示例不启用 async。
mod csp1d;
fn main() {
csp1d::run_example();
}示例在命令行程序中通过抛出异常或返回错误结束失败调用。接入服务端时,将错误转换为结构化响应,并保留求解报告和诊断信息。求解器正常结束但未找到可行解时,按报告返回相应状态。
4. 读懂并验收结果
| 读取内容 | Kotlin | Rust |
|---|---|---|
| 方案和使用次数 | solution.produce.cuttingPlans → plan / amount | solution.produce.cutting_plans → plan / amount |
| 原料与余宽 | plan.material.id / slices / restWidth | plan.material.id / slices / rest_width() |
| 单次产出 | plan.demandContributions → product.id / quantity | plan.demand_contributions → product.id / quantity |
| 欠产与状态 | produce.unmetDemands、status / failureMessage / kpi | produce.unmet_demands、status / failure_message / kpi |
结果语义
按 Σ 单次产出 × 使用次数 汇总产出;不要把方案列表长度当产量。restWidth / rest_width() 返回余宽而非一组放置坐标。该结果没有候选集合 complete;部分结果应结合状态、欠产与列生成迭代记录判断。
先读取 status、失败信息与 unmetDemands / unmet_demands,再读取选中方案及使用量。按卷数重算 P1 的产出,复核每个方案幅宽与原料批次;候选集合非空不等于覆盖需求,部分结果也不能直接作为完整交付。
5. 修改一个业务条件
先把需求从 3 卷改成 4 卷,重新检查可用批次与方案产出。再尝试重量需求:不能只把标签从“卷”改成“kg”,还需给出单位重量、卷长等转换依据,并同步修改验收标准。