第一次二维排样:从矩形开始
本课通过业务示例学习输入、求解和结果解析。先按环境页准备依赖和后端。
1. 业务场景
把 R1(4 × 3 m,6 件)和 R2(2 × 2 m,8 件)排入 20 × 12 m 的标准板,允许旋转,不加刀缝。每件 1 kg,板材承重 10000 kg。
净面积为 104 m²,小于一张板的 240 m²;但面积足够不证明几何可放置。物品目录、需求和板型必须使用一致的 ID,不能按尺寸把不同订单合并。
2. 构造输入并调用框架
Kotlin 与 Rust 的入口差异
Kotlin 示例完整构造物品、板型和请求,并调用公开应用服务。Rust 的 solve_layout 接收业务输入,以及受限主问题和最终整数求解的两个后端适配器。当前 Rust 服务接口固定为 f64 + Meter,物品重量与长度还共用单位类型,接入时应显式处理这项单位限制。StandardRmpSolver/StandardFinalSolver 只是闭包适配器,不是自动连接 SCIP 的实现。
示例包含导入和错误处理。Kotlin 的 runExample 接收应用提供的 ColumnGenerationSolver,下一节给出启动入口。
package docs.examples.bpp2d
import fuookami.ospf.kotlin.math.algebra.number.*
import fuookami.ospf.kotlin.quantities.quantity.times
import fuookami.ospf.kotlin.quantities.unit.*
import fuookami.ospf.kotlin.framework.solver.ColumnGenerationSolver
import fuookami.ospf.kotlin.framework.geometry2d.*
import fuookami.ospf.kotlin.framework.bpp2d.domain.item.model.*
import fuookami.ospf.kotlin.framework.bpp2d.application.service.*
import fuookami.ospf.kotlin.utils.functional.*
private fun <T> Ret<T>.getOrThrow(): T = when (this) {
is Ok -> value
is Failed -> error("${error.code}: ${error.message}")
is Fatal -> error(errors.joinToString { "${it.code}: ${it.message}" })
}
suspend fun runExample(solver: ColumnGenerationSolver) {
fun rectangle(id: String, width: Double, height: Double) = ActualItem(
id = ItemId(id), name = id,
shape = rectangleShape2(FltX(width) * Meter, FltX(height) * Meter).getOrThrow(),
allowRotate = true, weight = FltX.one * Kilogram
)
val items = listOf(rectangle("R1", 4.0, 3.0) to UInt64(6), rectangle("R2", 2.0, 2.0) to UInt64(8))
val sheet = SheetType(
width = FltX(20.0) * Meter, height = FltX(12.0) * Meter,
maxLoad = FltX(10000.0) * Kilogram, typeCode = SheetTypeId("STD"), isMain = true
)
val request = ColumnGenerationApplicationRequest(
items = items.toMap(), itemCatalog = items.associate { it.first.id to it.first },
sheetTypes = listOf(sheet), iterationLimit = 12, maxColumnsPerIteration = 12
)
val response = ColumnGenerationApplicationService(solver = solver).solve(request).getOrThrow()
val packing = response.packing ?: error("No deliverable packing; inspect the solve result")
val bins = packing.aggregation.bins
val counts = bins.flatMap { it.items }.groupingBy { it.placement.itemId }.eachCount()
// #region result
println("final=${response.result.finalSolved}, limited=${response.result.terminatedByIterationLimit}")
println("kpi=${response.kpi}, placed=$counts")
for (bin in bins) {
println("sheet=${bin.name}, type=${bin.type.typeCode}")
for (item in bin.items) {
val p = item.placement
println("${p.itemId}: (${p.x}, ${p.y}) -> (${p.maxX().getOrThrow()}, ${p.maxY().getOrThrow()}), rotated=${p.rotated}")
}
}
packing.remnants.forEach { println("${it.sheetName}: free=${it.rectangle}, area=${it.area}") }
// #endregion result
for ((item, demand) in items) {
check(UInt64(counts[item.id.value] ?: 0) == demand) { "Demand mismatch: ${item.id}" }
}
}use ospf_rust_base::error::Ret;
use ospf_rust_framework_bpp2d::application::{
algorithm::{ColumnGenerationFinalSolver, ColumnGenerationRmpSolver},
config::ColumnGenerationConfig,
service::ColumnGenerationApplicationService,
state::{ColumnGenerationInput, ColumnGenerationResult},
};
use ospf_rust_quantities::unit::Meter;
// The caller supplies normalized domain input and BOTH real solver adapters.
// This is an application boundary, not a standalone optimizer implementation.
pub fn solve_layout(
input: ColumnGenerationInput<f64, Meter>,
rmp: Box<dyn ColumnGenerationRmpSolver>,
final_solver: Box<dyn ColumnGenerationFinalSolver>,
) -> Ret<ColumnGenerationResult<f64, Meter>> {
let demands: Vec<_> = input.items.iter().map(|(item, count)| (item.id.clone(), *count as u64)).collect();
let result = ColumnGenerationApplicationService::new().solve(
input, &ColumnGenerationConfig::default(), Some(rmp), Some(final_solver),
)?;
println!("lp solves={}, final solved={}", result.lp_solved_times, result.final_solved);
// #region result
println!("limited={}, objective={:?}", result.terminated_by_iteration_limit, result.final_objective);
if let Some(packing) = &result.packing {
println!("counts={:?}, occupied={:?}, remnants={:?}", packing.item_amounts(), packing.occupied_area(), packing.remnant_area());
for sheet in &packing.sheets {
println!("sheet={}, type={:?}", sheet.name, sheet.sheet_type.type_code);
for item in &sheet.items {
let p = &item.placement;
println!("{:?}: ({:?}, {:?}) -> ({:?}, {:?}), rotated={}", p.item_id, p.x(), p.y(), p.max_x()?, p.max_y()?, p.rotated);
}
for free in &sheet.remnants { println!("free={:?}, area={:?}", free.rectangle, free.area); }
}
let counts = packing.item_amounts();
for (id, demand) in demands { assert_eq!(counts.get(&id).copied().unwrap_or(0), demand, "Demand mismatch: {id:?}"); }
}
// #endregion result
Ok(result)
}3. 在自己的程序中启动
保留包声明,将 Kotlin 示例放入应用源码目录。下面的独立入口调用本课函数;需添加对应领域的集成依赖、Gurobi 插件和协程依赖,配置匹配的原生库与许可证。
import docs.examples.bpp2d.runExample
import kotlinx.coroutines.runBlocking
import fuookami.ospf.kotlin.core.solver.gurobi.GurobiColumnGenerationSolver
fun main() = runBlocking {
runExample(GurobiColumnGenerationSolver())
}Rust 调用方应先统一输入单位,实现受限主问题和最终整数求解的后端适配器,再将它们传入 solve_layout。这两个适配器需要应用自行实现。
示例在命令行程序中通过抛出异常或返回错误结束失败调用。接入服务端时,将错误转换为结构化响应,并保留求解报告和诊断信息。求解器正常结束但未找到可行解时,按报告返回相应状态。
4. 读懂并验收结果
| 读取内容 | Kotlin | Rust |
|---|---|---|
| 启用板材与物品 | packing.aggregation.bins → name / type / items | packing.sheets → name / sheet_type / items |
| 坐标与旋转 | placement.x / y / maxX() / maxY() / rotated | placement.x() / y() / max_x() / max_y() / rotated |
| 余料与 KPI | packing.remnants、response.kpi | sheet.remnants、packing.item_amounts() / occupied_area() / remnant_area() |
| 终止范围 | response.result.finalSolved / terminatedByIterationLimit / terminatedByTimeLimit | result.final_solved / terminated_by_iteration_limit / terminated_by_time_limit |
结果语义
本入口没有 complete。finalSolved 表示执行了最终求解,预算终止标记说明搜索范围;几何方案与精确需求件数仍要独立复核。余料是按板名关联的空闲矩形区域,不是已经入库的库存。
先确认 LP 和最终整数阶段实际执行,再逐 ID 核对 R1=6、R2=8。随后检查每件在板内、两件不内部重叠、旋转许可和间距。示例中的件数检查只是第一道结果检查,不能替代几何复核,也不能据此宣称全局最优。
5. 修改一个业务条件
只把 R1 的 allowRotate 改为 false,保留需求,再比较候选与坐标。下一次再增加正刀缝;不要同时改板型、朝向和间距,否则很难解释变化。