第一次板材套裁:需求、图案与工艺
本课通过业务示例学习输入、求解和结果解析。先按环境页准备依赖和后端。
1. 业务场景
把矩形产品从定尺板材中裁出。Kotlin 为 100 × 60 m 板、40 × 30 m 产品、需求 3 件;Rust 为 10 × 8 m 板、2 × 2 m 产品、需求 20 件、原料可用 100 张。先用自由排样、零刀缝。尺寸是便于手算的教学量值,不是生产规格。
两种输入都能构造一张板的可行排布。Kotlin 显式声明产品、件数需求和工艺;Rust 用 Csp2dProblem::single 构造单产品问题。它们不是同一个数值基准。
2. 构造输入并调用框架
Kotlin 与 Rust 的入口差异
Kotlin 的 Csp2dMilp 接入真实求解器。Rust 默认 Csp2dSolvers::new().solve_milp 使用启发式主问题,不需要原生后端;方法名不是原生执行或最优证明。需要真实优化时使用 Csp2dMasterSolver 接口。
示例包含导入和错误处理。Kotlin 的 runExample 接收应用提供的 ColumnGenerationSolver,下一节给出启动入口。
package docs.examples.csp2d
import fuookami.ospf.kotlin.math.algebra.number.*
import fuookami.ospf.kotlin.quantities.quantity.times
import fuookami.ospf.kotlin.quantities.unit.Meter
import fuookami.ospf.kotlin.framework.solver.ColumnGenerationSolver
import fuookami.ospf.kotlin.framework.csp2d.application.model.*
import fuookami.ospf.kotlin.framework.csp2d.application.service.*
import fuookami.ospf.kotlin.framework.csp2d.domain.material.model.*
import fuookami.ospf.kotlin.framework.csp2d.infrastructure.CuttingProcess2
import fuookami.ospf.kotlin.utils.functional.*
suspend fun runExample(solver: ColumnGenerationSolver) {
val sheet = Material2(
id = materialId2Of("SHEET"), name = "sheet",
width = FltX(100.0) * Meter, height = FltX(60.0) * Meter
)
val product = Product2(
id = productId2Of("P1"), name = "P1",
width = FltX(40.0) * Meter, height = FltX(30.0) * Meter
)
val problem = Csp2dProblem(
materials = listOf(sheet), products = listOf(product),
demands = listOf(ProductDemand2(product = product, demand = DemandValue2.Amount(UInt64(3)))),
kerf = FltX.zero * Meter, process = CuttingProcess2.Freeform,
config = Csp2dConfiguration(maxCandidatesPerIteration = 12, iterationLimit = 4)
)
when (val result = Csp2dMilp(solver).solve(problem)) {
is Ok -> {
val solution = result.value
// #region result
println("covered=${solution.covers(problem)}, sheets=${solution.sheetCount}, length=${solution.consumedLength}")
println("coverage=${solution.coverage}, used=${solution.usedArea}, waste=${solution.wasteArea}, utilization=${solution.utilization}")
for (usage in solution.usages) {
check(usage.plan.validate(problem.kerf) is Ok) { "Invalid cutting pattern" }
println("plan=${usage.plan.id}, material=${usage.plan.pattern.material.id}, usages=${usage.usage}")
usage.plan.pattern.placements.forEach { println("${it.itemId}: (${it.x}, ${it.y}), width=${it.width}, height=${it.height}") }
println("cut tree=${usage.plan.pattern.cutTree}")
}
check(solution.covers(problem)) { "Demand is not fully covered" }
// #endregion result
}
is Failed -> error("${result.error.code}: ${result.error.message}")
is Fatal -> error(result.errors.joinToString { "${it.code}: ${it.message}" })
}
}use ospf_rust_base::error::Ret;
use ospf_rust_framework_csp2d::application::{Csp2dProblem, Csp2dSolvers};
use ospf_rust_framework_csp2d::domain::material::{
Material2, Product2, material_id2, product_id2,
};
use ospf_rust_quantities::quantity::Quantity;
use ospf_rust_quantities::unit::derived::Meter;
fn meters(value: f64) -> Quantity<f64, Meter> {
Quantity::new_ct(value)
}
pub fn run_example() -> Ret<()> {
let material = Material2::sheet(
material_id2("m1"), "sheet", meters(10.0), meters(8.0), 100,
);
let product = Product2::new(product_id2("a"), "a", meters(2.0), meters(2.0));
let problem = Csp2dProblem::single(material, product, 20, meters(0.0));
// The default service uses a heuristic master solver, not a native optimizer.
let solution = Csp2dSolvers::new().solve_milp(problem)?;
// #region result
println!("covered={}, coverage={:?}, termination={:?}", solution.fully_covered, solution.coverage, solution.termination);
for (plan, count) in &solution.usage {
plan.validate(&meters(0.0))?;
println!("plan={}, material={}, usages={}, per usage={:?}", plan.id, plan.pattern.material.id, count, plan.product_amounts());
for p in &plan.pattern.placements { println!("{:?}: ({:?}, {:?}), size=({:?}, {:?})", p.item_id, p.x(), p.y(), p.width(), p.height()); }
println!("cut tree={:?}", plan.pattern.cut_tree);
}
println!("material usage: {}", solution.total_material_usage());
println!("waste area: {}", solution.total_waste_area());
assert!(solution.fully_covered, "Demand is not fully covered");
// #endregion result
Ok(())
}3. 在自己的程序中启动
保留包声明,将 Kotlin 示例放入应用源码目录。下面的独立入口调用本课函数;需添加对应领域的集成依赖、Gurobi 插件和协程依赖,配置匹配的原生库与许可证。
import docs.examples.csp2d.runExample
import kotlinx.coroutines.runBlocking
import fuookami.ospf.kotlin.core.solver.gurobi.GurobiColumnGenerationSolver
fun main() = runBlocking {
runExample(GurobiColumnGenerationSolver())
}将 Rust 示例函数放入 src/csp2d.rs,在 src/main.rs 使用以下入口。依赖与 工具链 见环境页;同步示例不启用 async。
mod csp2d;
fn main() -> ospf_rust_base::error::Ret<()> {
csp2d::run_example()
}示例在命令行程序中通过抛出异常或返回错误结束失败调用。接入服务端时,将错误转换为结构化响应,并保留求解报告和诊断信息。求解器正常结束但未找到可行解时,按报告返回相应状态。
4. 读懂并验收结果
| 读取内容 | Kotlin | Rust |
|---|---|---|
| 图案、原料、使用次数 | solution.usages → plan.pattern / plan.material / usage | solution.usage → (plan.pattern, count) |
| 放置与切分树 | pattern.placements、pattern.cutTree | pattern.placements、pattern.cut_tree |
| 产出与原料消耗 | coverage / sheetCount / consumedLength / wasteArea | coverage / material_usage / sheet_assignment / total_waste_area() |
| 覆盖与终止 | solution.covers(problem)、iterations | fully_covered / termination / failure_message |
结果语义
covers(problem) / fully_covered 只回答需求是否覆盖,不表示候选图案集合完整或全局最优。每个图案的坐标只描述单次执行,单次产出、余料和卷材段长都要乘使用次数。自由排样的切分树允许为空。
分四层验收:需求覆盖、原料用量、几何合法、工艺合法。图案数量不是板材张数,应汇总使用量。自由排样不要求切分树;断头台切割需要有效切分树及级数、区域和刀缝检查。默认 Rust 场景的手算参照为覆盖 20 件、用一张板、净面积余量为零;实际输出仍应复核。
5. 修改一个业务条件
先只增加正刀缝,原先刚好铺满的图案可能失效。再改两级断头台切割并重新生成图案,而不是给原坐标附加工艺标签。最后再切换卷材,并把主要用量从张数改为消耗长度。