我试图用一些优先约束生成给定变量的所有序列。例如,如果我们有五个对象[1 2 3 4 5]
,那么有5! = 125
个方法来置换这些对象。但是,如果我们施加一些优先约束,例如:
只有这些可能性仍然存在:
1 2 3 5 4,
1 2 3 4 5,
1 2 5 4 3,
1 2 5 3 4,
1 3 4 5 2,
1 3 4 2 5,
1 3 2 5 4,
1 3 2 4 5,
与六个对象一样,带有约束的代码是: -
allCombs = perms(1:objects);
constraintsFor1 = union(findKAfterNRows(allCombs,2,1),findKAfterNRows(allCombs,3,1));
constraintsFor1 = intersect(constraintsFor1,find(allCombs(:,1)==1));
constraintsFor4 = union(findKAfterNRows( allCombs, 4,5 ),findKAfterNRows( allCombs, 4,3 ));
constraintsFor5 = union(findKAfterNRows( allCombs, 5,2 ),findKAfterNRows( allCombs, 5,4 ));
constraintsFor6 = union(findKAfterNRows( allCombs, 6,4 ),findKAfterNRows( allCombs, 6,5 ));
resultRows = intersect(constraintsFor1,constraintsFor4);
resultRows = intersect(resultRows,constraintsFor5);
resultRows = intersect(resultRows,constraintsFor6);
output = allCombs(resultRows,:);