我在组织流程数据时遇到了问题。要求是我们要有N艘拖船(用于运输大型船的船)和N艘要由这些拖船获得的船。每艘船都需要特定数量的拖船,所以我们有这样的东西:
setClass("tugboat", slots=list(name = "character"))
tugboat1 <- new("tugboat", name = "tugboat 1")
tugboat2 <- new("tugboat", name = "tugboat 2")
tugboat3 <- new("tugboat", name = "tugboat 3")
setClass("ship", slots=list(name = "character", tugboats_required = "numeric"))
ship1 <- new("ship", name = "Titanic", tugboats_required = 1)
ship2 <- new("ship", name = "Black Pearl", tugboats_required = 1)
ship3 <- new("ship", name = "Interceptor", tugboats_required = 1)
以为拖船不能同时参与两艘船的情况下,我可以用来装载所有可能场景的结构和逻辑,例如:
场景1 :ship1 +拖船1,ship2 +拖船2,ship3 +拖船3
场景2 :ship1 +拖船1,ship2 +拖船3,ship3 +拖船2
场景3 :ship1 +拖船2,ship2 +拖船1,ship3 +拖船3
场景4 :ship1 +拖船2,ship2 +拖船3,ship3 +拖船
场景5 :ship1 +拖船3,ship2 +拖船1,ship3 +拖船2
场景6 :ship1 +拖船3,ship2 +拖船1,ship3 +拖船1
在上面的示例中,我考虑每艘船仅使用一艘拖船(tugboats_required = 1),但是例如,如果此数字为2,则该船的方案应有2艘拖船。示例:
setClass("tugboat", slots=list(name = "character"))
tugboat1 <- new("tugboat", name = "tugboat 1")
tugboat2 <- new("tugboat", name = "tugboat 2")
tugboat3 <- new("tugboat", name = "tugboat 3")
setClass("ship", slots=list(name = "character", tugboats_required = "numeric"))
ship1 <- new("ship", name = "Titanic", tugboats_required = 1)
ship2 <- new("ship", name = "Black Pearl", tugboats_required = 2)
场景1 :船1 +拖船1,船2 +拖船2 +拖船3
场景2 :船1 +拖船2,船2 +拖船1 +拖船3
场景3 :船1 +拖船3,船2 +拖船1 +拖船2
问题不在于数学,而是在对象声明后组织数据。