how to generate all possible combinations of a 14x

2020-04-21 01:14发布

I'm working on a problem and one solution would require an input of every 14x10 matrix that is possible to be made up of 1's and 0's... how can I generate these so that I can input every possible 14x10 matrix into another function? Thank you!

Added March 21: It looks like I didn't word my post appropriately. Sorry. What I'm trying to do is optimize the output of 10 different production units (given different speeds and amounts of downtime) for several scenarios. My goal is to place blocks of downtime to minimized the differences in production on a day-to-day basis. The amount of downtime and frequency each unit is allowed is given. I am currently trying to evaluate a three week cycle, meaning every three weeks each production unit is taken down for a given amount of hours. I was asking the computer to determine the order the units would be taken down based on the constraint that the lines come down only once every 3 weeks and the difference in daily production is the smallest possible. My first approach was to use Excel (as I tried to describe above) and it didn't work (no suprise there)... where 1- running, 0- off and when these are summed to calculate production. The calculated production is subtracted from a set max daily production. Then, these differences were compared going from Mon-Tues, Tues-Wed, etc for a three week time frame and minimized using solver. My next approach was to write a Matlab code where the input was a tolerance (set allowed variation day-to-day). Is there a program that already does this or an approach to do this easiest? It seems simple enough, but I'm still thinking through the different ways to go about this. Any insight would be much appreciated.

11条回答
孤傲高冷的网名
2楼-- · 2020-04-21 01:42

Trying this:

import numpy
for i in xrange(int(1e9)): a = numpy.random.random_integers(0,1,(14,10))

(which is much, much, much smaller than what you require) should be enough to convince you that this is not feasible. It also shows you how to calculate one, or few, such random matrices even up to a million is pretty fast).

EDIT: changed to xrange to "improve speed and memory requirements" :)

查看更多
家丑人穷心不美
3楼-- · 2020-04-21 01:51

This is absolutely impossible! The number of possible matrices is 2140, which is around 1.4e42. However, consider the following...

  • If you were to generate two 14-by-10 matrices at random, the odds that they would be the same are 1 in 1.4e42.
  • If you were to generate 1 billion unique 14-by-10 matrices, then the odds that the next one you generate would be the same as one of those would still be exceedingly slim: 1 in 1.4e33.
  • The default random number stream in MATLAB uses a Mersenne twister algorithm that has a period of 219936-1. Therefore, the random number generator shouldn't start repeating itself any time this eon.

Your approach should be thus:

  • Find a computer no one ever wants to use again.
  • Give it as much storage space as possible to save your results.
  • Install MATLAB on it and fire it up.
  • Start computing matrices at random like so:

    while true
      newMatrix = randi([0 1],14,10);
      %# Process the matrix and output your results to disk
    end
    
  • Walk away

Since there are so many combinations, you don't have to compare newMatrix with any of the previous matrices since the length of time before a repeat is likely to occur is astronomically large. Your processing is more likely to stop due to other reasons first, such as (in order of likely occurrence):

  • You run out of disk space to store your results.
  • There's a power outage.
  • Your computer suffers a fatal hardware failure.
  • You pass away.
  • The Earth passes away.
  • The Universe dies a slow heat death.

NOTE: Although I injected some humor into the above answer, I think I have illustrated one useful alternative. If you simply want to sample a small subset of the possible combinations (where even 1 billion could be considered "small" due to the sheer number of combinations) then you don't have to go through the extra time- and memory-consuming steps of saving all of the matrices you've already processed and comparing new ones to it to make sure you aren't repeating matrices. Since the odds of repeating a combination are so low, you could safely do this:

for iLoop = 1:whateverBigNumberYouWant
  newMatrix = randi([0 1],14,10);  %# Generate a new matrix
  %# Process the matrix and save your results
end
查看更多
乱世女痞
4楼-- · 2020-04-21 01:55

Generating Every possible matrix of 1's and 0's for 14*10 would generate 2**140 matrixes. I don't believe you would have enough lifetime for this. I don't know, if the sun would still shine before you finish that. This is why it is impossible to generate all those matrices. You must look for some other solution, this looks like a brute force.

查看更多
时光不老,我们不散
5楼-- · 2020-04-21 01:56

Are you sure you want every possible 14x10 matrix? There are 140 elements in each matrix, and each element can be on or off. Therefore there are 2^140 possible matrices. I suggest you reconsider what you really want.

Edit: I noticed you mentioned in a comment that you are trying to minimize something. There is an entire mathematical field called optimization devoted to doing this type of thing. The reason this field exists is because quite often it is not possible to exhaustively examine every solution in anything resembling a reasonable amount of time.

查看更多
ゆ 、 Hurt°
6楼-- · 2020-04-21 01:57

You don't have to iterate over this:

def everyPossibleMatrix(x,y):
    N=x*y
    for i in range(2**N):
        b="{:0{}b}".format(i,N)
        yield '\n'.join(b[j*x:(j+1)*x] for j in range(y))
查看更多
地球回转人心会变
7楼-- · 2020-04-21 01:59

Here is an efficient way to do get started Matlab:

First generate all 1024 possible rows of length 10 containing only zeros and ones:

dec2bin(0:2^10-1)

Now you have all possible rows, and you can sample from them as you wish. For example by calling the following line a few times:

randperm(1024,14)
查看更多
登录 后发表回答