Search code examples
pythonmixed-integer-programming

Mixed Integer Program in Python


I have a MIP Problem that I need to output in Python. See the following attempt so far. I just need few suggestions that would point me in the right direction.

digits = [("00"+str(x))[-3:] for x in range(1, 10)]

var_1 = 2
var_2 = 1
var_3 = 3

LHS = [5,6,7]          
RHS = [100,200,300]
count = 1

for v1 in range(var_1):
    for v2 in range(var_2):
        for v3 in range(var_3):
            print("x"+digits[v1]+digits[v2]+digits[v3]+" - 20 z"+digits[v1]+digits[v2]+digits[v3]+" <= 0")

for v2 in range(var_2):
    for v3 in range(var_3):
        print("x"+digits[v2]+digits[v3]+" + "+"x" +digits[v2]+digits[v3]+" <= 123")

CURRENT OUTPUT:

x001001001 - 20 z001001001 <= 0
x001001002 - 20 z001001002 <= 0
x001001003 - 20 z001001003 <= 0
x002001001 - 20 z002001001 <= 0
x002001002 - 20 z002001002 <= 0
x002001003 - 20 z002001003 <= 0
x001001 + x001001 <= 123
x001002 + x001002 <= 123
x001003 + x001003 <= 123

My code is not producing what I want. Here is the output I want it to produce.

c1: x001001001 - 20 z001001001 <= 0
c2: x001001002 - 20 z001001002 <= 0
c3: x001001003 - 20 z001001003 <= 0
c4: x002001001 - 20 z002001001 <= 0
c5: x002001002 - 20 z002001002 <= 0
c6: x002001003 - 20 z002001003 <= 0
c7: x001001001 + x002001001 <= 123
c8: x001001002 + x002001002 <= 123
c9: x001001003 + x002001003 <= 123

Any help would be appreciated. Any modules that I need to use to make it simpler to code would be helpful as well. There are many more lines of codes that I have with different combinations of order of those digits within, but if I can get decent knowledge then I should be able to do it.

I also want to be able to print this output to a text file. What would be the easiest way to do so? Any suggestions? Thanks


Solution

  • DD1, you are on the right track here. All you need here is not that difficult. Use the code below as a starting point. This is a fix to your code. It gives the output you need. Other responses are also helpful.

    for v1 in range(var_1):
        for v2 in range(var_2):
            for v3 in range(var_3):
                print("c"+str(count)+":", end=" ")
                print("x"+digits[v1]+digits[v2]+digits[v3]+" - 20 z"+digits[v1]+digits[v2]+digits[v3]+" <= 0")
                count += 1
    
    for v2 in range(var_2):
        for v3 in range(var_3):
            print("c"+str(count)+":", end=" ")
            print(" + ".join("x"+d+digits[v2]+digits[v3] for d in digits[:var_1]), end = " ")
            print("<= 123")
            count += 1
    

    I suggest that you look at the three topics: join method, list comprehension, and list slicing. These are good for the type of code you are looking to have.

    For saving your code as text file. You can use this:

    import sys
    
    orig_stdout = sys.stdout
    a = open('filename.txt', 'w')  # w for write
    sys.stdout = a
    
    #put your code here
    
    sys.stdout = orig_stdout
    file.close()
    

    If you like, post any updates of your code if this helps.