I need to solve a problem. I have 5 devices. They all have 4 kind of I/O types. And there is a target input/output combination. At first step, I want to find all combinations among the devices so that the total I/O number of selected devices are all equal or greater than the target values. Let me explain:
# Devices=[numberof_AI,numberof_AO,numberof_BI,numberof_BO,price]
Device1=[8,8,4,4,200]
Device1=[16,0,16,0,250]
Device1=[8,0,4,4,300]
Device1=[16,8,4,4,300]
Device1=[8,8,2,2,150]
Target=[24,12,16,8]
There are constraints as well. In combinations, max. number of devices can be 5 at most.
At the second step, among the combinations found, I will pick the cheapest one.
Actually, I managed to solve this problem with for loops in Python. I works like a charm. But it takes too much time even though I use cython.
What other options can I benefit from for this kind of problem?
You can use a linear programming package like PuLP. (note this also requires you to install an LP library like GLPK).
Here's how you would use it to solve the example you gave:
Running this is extremely fast, and I get that n1 = 2 and n2 = 1 and the others are 0.
Just check all combinations. As you have just 5 devices, that makes (at most)
6^5=7776
possibilities (since each of the five positions might be unused you have to use6
). Then for each possibility, you check whether it fulfils your criteria. I don't see why this should take so much time.The following script takes not a second on my machine to compute the stuff.
Requires Python 2.7.
One could also solve this problem using the Python Constraint module by Gustavo Niemeyer.
This produces the following output:
Thus, the optimal solution is 2 x Device1, 1 x Device2, 0 x Device3, 0 x Device4, 0 x Device5.
(Note that the variables are named using zero-based indexing. Device1 corresponds to 0, Device2 corresponds to 1, and so on.)