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条回答
Root(大扎)
2楼-- · 2020-04-21 02:02

Depending on what you want to accomplish with the generated matrices, you might be better off generating a random sample and running a number of simulations. Something like:

matrix_samples = []
# generate 10 matrices
for i in range(10):
    sample = numpy.random.binomial(1, .5, 14*10)
    sample.shape = (14, 10)
    matrix_samples.append(sample)

You could do this a number of times to see how results vary across simulations. Of course, you could also modify the code to ensure that there are no repeats in a sample set, again depending on what you're trying to accomplish.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-04-21 02:05

The actual implementation depends heavily on how you want to represent matrices… But assuming the matrix can be represented by a 14 * 10 = 140 element list:

from itertools import product
for matrix in product([0, 1], repeat=140):
    # ... do stuff with the matrix ...

Of course, as other posters have noted, this probably isn't what you want to do… But if it really is what you want to do, that's the best code (given your requirements) to do it.

查看更多
Evening l夕情丶
4楼-- · 2020-04-21 02:05

I was actually much more pessimistic to begin with, but consider:

from math import log, e

def timeInYears(totalOpsNeeded=2**140, currentOpsPerSecond=10**9, doublingPeriodInYears=1.5):
    secondsPerYear = 365.25 * 24 * 60 * 60
    doublingPeriodInSeconds = doublingPeriodInYears * secondsPerYear
    k = log(2,e) / doublingPeriodInSeconds  # time-proportionality constant
    timeInSeconds = log(1 + k*totalOpsNeeded/currentOpsPerSecond, e) / k
    return timeInSeconds / secondsPerYear

if we assume that computer processing power continues to double every 18 months, and you can currently do a billion combinations per second (optimistic, but for sake of argument) and you start today, your calculation will be complete on or about April 29th 2137.

查看更多
干净又极端
5楼-- · 2020-04-21 02:07

Instead of just suggesting the this is unfeasible, I would suggest considering a scheme that samples the important subset of all possible combinations instead of applying a brute force approach. As one of your replies suggested, you are doing minimization. There are numerical techniques to do this such as simulated annealing, monte carlo sampling as well as traditional minimization algorithms. You might want to look into whether one is appropriate in your case.

查看更多
唯我独甜
6楼-- · 2020-04-21 02:08

Are you saying that you have a table with 140 cells and each value can be 1 or 0 and you'd like to generate every possible output? If so, you would have 2^140 possible combinations...which is quite a large number.

查看更多
登录 后发表回答