如何生成具有特定模式的二进制矩阵?
我有一个大小为 m×n 的二进制矩阵。 下面给出的是一个示例二进制矩阵(实际矩阵要大得多):
1010001
1011011
1111000
0100100
给定 p = m*n,我有 2^p 种可能的矩阵配置。 我想得到一些满足某些规则的模式。 例如:
- 我希望第 j 列中不少于 k 个单元格为零
- 我希望第 i 行单元格值的总和大于给定的数字 Ai
- 我希望一列中至少有 g 个单元格连续为 1
- 等等...
如何在不按顺序检查所有 2^p 组合的情况下获得严格满足这些约束的模式?
就我而言,p 可以是像 2400 这样的数字,给出大约 2.96476e+722 种可能的组合。
I have a binary matrix of size m-by-n. Given below is a sample binary matrix (the real matrix is much larger):
1010001
1011011
1111000
0100100
Given p = m*n, I have 2^p possible matrix configurations. I would like to get some patterns which satisfy certain rules. For example:
- I want not less than k cells in the jth column as zero
- I want the sum of cell values of the ith row greater than a given number Ai
- I want at least g cells in a column continuously as one
- etc....
How can I get such patterns satisfying these constraints strictly without sequentially checking all the 2^p combinations?
In my case, p can be a number like 2400, giving approximately 2.96476e+722 possible combinations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您有足够的约束,则可以尝试探索所有可能的矩阵:
如果约束的数量较少,则此方法将花费太多时间。
在这种情况下,对于您作为示例给出的约束类型,模拟退火可能是一个不错的选择解决方案。
您必须设计一个能量函数,当满足所有约束时,该函数为高能量函数。 这将是这样的:
If you have enough constraints, exploring all possible matrices could be attempted:
If the number of constraints is low, this approach will take too much time.
In this case, for the kind of constraints you gave as examples, simulated annealing may be a good solution.
You must design an energy function, high when all constraints are met. That would be something like that:
如果所有约束都与列相关(如问题中的情况),那么您可以找到所有可能的有效列并检查矩阵中的每一列是否都在该集合中。 (即,当您独立考虑每一列时,您会大大减少可能性的数量。)
If all the contraints relate to columns (as is the case in the question), then you can find all possible valid columns and check that each column in the matrix is in this set. (i.e. when you consider each column independently, you reduce the number of possibilities a lot.)
我可能离这里很远,但我记得曾经用某种遗传算法做过类似的事情。
I might be way off here, but I remember doing something similar once with some genetic algorithm.
查看伪布尔约束(也称为 0-1 整数规划)。
Check out pseudo boolean constraints (also called 0-1 integer programming).
如果您的约束集足够复杂,这实际上是不可能的。 您可以尝试使用随机优化器(例如模拟退火、粒子群优化或遗传算法)来找到可行的解决方案。
但是,如果您可以为此类问题生成一个(非随机)解决方案,那么通常您可以通过对现有解决方案进行随机排列来生成其他解决方案。
This is virtually impossible if your constraint set is complex enough. You might try to use a stochastic optimizer, like simulated annealing, particle swarm optimization, or a genetic algorithm to find a feasible solution.
However, if you can generate one (non-random) solution to such a problem, then often you can generate others by random permutations made to the existing solution.
生成此类二元矩阵的一种方法是根据给定的约束执行重复的行和列操作,而不是迭代所有 2^p 组合。 作为示例,我将发布一些代码,这些代码将根据上面列出的三个约束生成一个矩阵:
初始化:
第一次开始通过初始化一些参数:
辅助函数:
接下来,创建几个函数来生成与上面的约束 1 和 3 匹配的列向量:
函数 make_column 将首先创建一个具有最小数量的逻辑列向量0 个元素,其余元素设置为 1(使用函数 正确和错误)。 该向量将对其元素进行随机重新排序,直到它包含大于或等于所需最小长度的序列。 这是使用randomize_column函数完成的。 使用 RANDPERM 函数对向量进行随机重新排序生成随机索引顺序。 使用 检测序列在 0 和 1 之间切换的边缘DIFF 函数。 然后使用边的索引来查找最长序列的长度(使用 查找和MAX< /a>)。
生成矩阵列:
通过上述两个函数,我们现在可以生成一个至少满足约束 1 和 3 的初始二元矩阵:
满足行和约束:
当然,现在我们必须确保满足约束 2。 我们可以使用 SUM 函数对每一行求和:
如果rowSum的任何元素小于我们想要的最小行总和,我们将不得不调整一些列值来补偿。 您可以通过多种不同的方式来修改列值。 我在这里举一个例子:
这段代码将循环,直到所有行的总和大于或等于我们想要的最小总和。 首先,使用 MIN. 接下来,找到该行中零的索引,并随机选择其中一个作为要修改的列的索引(columnIndex)。 使用make_column,不断生成新的列向量,直到给定行中的 0 变为 1。然后更新二进制矩阵中的该列并计算新的行总和。
摘要:
对于相对较小的 10×10 二进制矩阵以及给定的约束,上述代码通常在不超过几秒钟的时间内完成。 有了更多的限制,事情当然会变得更加复杂。 根据您选择约束的方式,可能没有可能的解决方案(例如,将 minRowSum 设置为 6 将导致上述代码永远收敛到解决方案)。
希望这将为您提供一个起点,开始使用矢量化运算生成您想要的矩阵类型。
Instead of iterating over all 2^p combinations, one way you could generate such binary matrices is by performing repeated row- and column-wise operations based on the given constraints you have. As an example, I'll post some code that will generate a matrix based on the three constraints you have listed above:
Initializations:
First start by initializing a few parameters:
Helper functions:
Next, create a couple of functions for generating column vectors that match constraints 1 and 3 from above:
The function make_column will first create a logical column vector with the minimum number of 0 elements and the remaining elements set to 1 (using the functions TRUE and FALSE). This vector will undergo random reordering of its elements until it contains a sequence of ones greater than or equal to the desired minimum length of ones. This is done using the randomize_column function. The vector is randomly reordered using the RANDPERM function to generate a random index order. The edges where the sequence switches between 0 and 1 are detected using the DIFF function. The indices of the edges are then used to find the length of the longest sequence of ones (using FIND and MAX).
Generate matrix columns:
With the above two functions we can now generate an initial binary matrix that will at least satisfy constraints 1 and 3:
Satisfy the row sum constraint:
Of course, now we have to ensure that constraint 2 is satisfied. We can sum across each row using the SUM function:
If any elements of rowSum are less than the minimum row sum we want, we will have to adjust some column values to compensate. There are a number of different ways you could go about modifying column values. I'll give one example here:
This code will loop until all the row sums are greater than or equal to the minimum sum we want. First, the index of the row with the smallest sum (rowIndex) is found using MIN. Next, the indices of the zeroes in that row are found and one of them is randomly chosen as the index of a column to modify (columnIndex). Using make_column, a new column vector is continuously generated until the 0 in the given row becomes a 1. That column in the binary matrix is then updated and the new row sum is computed.
Summary:
For a relatively small 10-by-10 binary matrix, and the given constraints, the above code usually completes in no more than a few seconds. With more constraints, things will of course get more complicated. Depending on how you choose your constraints, there may be no possible solution (for example, setting minRowSum to 6 will cause the above code to never converge to a solution).
Hopefully this will give you a starting point to begin generating the sorts of matrices you want using vectorized operations.