我有一个用例,我必须将两个约束流合并为一个约束流。在 SQL 中,这类似于
UNION ALL
。但是,我似乎找不到办法这样做。
虽然我的确切用例太大而无法在这里实用,但我可以尝试在这里进行解释。我们拥有一系列频谱权利,代表在特定地理区域使用特定范围无线电频谱的专有权利:
public class SpectrumRight {
private int geometryId;
private int lowerFrequency;
private int upperFrequency;
...
}
我们还有代表我们现有频谱权组合的问题事实
...
@ProblemFactCollectionProperty
public List<SpectrumRight> existingPortfolio;
...
然后,我们还有一系列可供拍卖的频谱权,打包成“CallSign”,这基本上只是预先打包的频谱权集合(可能有不同的频率或不同的地理区域):
@PlanningEntity
public class AuctionLicense {
@PlanningId
private String callSign;
@PlanningVariable(...)
private boolean buy;
private List<SpectrumRight> spectrumRights;
...
}
在我们的约束流中,我尝试汇总现有投资组合中的所有频谱权利,以及在拍卖中购买的许可证中传达的所有频谱权利,以便计算有关组合频谱权利的改善。
private Constraint computeAggregatedSpectrum(ConstraintFactory factor) {
return factory.from(AuctionLicense.class)
.filter(AuctionLicense::isBuy)
.map(AuctionLicense::getSpectrumRights)
.union(SpectrumRights.class) /*<-from existingPortfolio in Solution*/
.groupBy(toSortedSet())
.map(MetricsCalculator::computeAggregatedSpectrumMhzPops)
.reward(...);
}
但是,我似乎找不到将两个 ConstraintStreams 附加或合并在一起的方法。有没有可以使用的流操作?或者其他解决方法?
这听起来像是 API 差距。
给定即将购买的频谱权
s1
的流[A, B, C]
和现有频谱权s2
的流[D, E]
:
s1.join(s2)
的 join() 会给出 [ {A,D}, {A,E}, {B,D}, {B,E}, {C,D}, {C,E} ]
流,这不是您需要的。s1.union(s2)
需要返回 [A, B, C, D, E]
。当前的 ConstraintStreams API 中尚不存在此操作。让我们创建一个 jira。
从 Timefold Solver 1.3.0 开始,现在有一种连接流的方法。它称为 concat,可用于约束流的所有基数。