0

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

3
  • You're current output and desired output look very similar, are you wanting to only append the c#:'s to the beginning of the output? Commented Mar 15, 2017 at 1:34
  • I forgot to mention that I want those c1: and so on also displayed as outputs, and as you can see c7-c9 has three subscripts of 9 digits 001001001 and so on, and my code is only producing two subscripts of 6 digits. Commented Mar 15, 2017 at 1:36
  • You should clarify what your intent is. To me it seems you are better off using either a proper modeling language (AMPL, ZIMPL, etc.) or a standard format (CPLEX LP, MPS, etc.) to describe your problem so that you are able to also solve it later on. There are also Python modules available for that purpose: github.com/SCIP-Interfaces/PySCIPOpt Commented Mar 15, 2017 at 22:12

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! a lot. I really appreciate your help. Perfect answer.
0

Here is a working version of your code, you had most of the right stuff there you just missed a few steps here and there, but here you go:

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 = 0 # set it to zero for start not 1

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

for v1 in range(var_1-1): # or just put '1' if you want to but something has to be there
    for v2 in range(var_2):
        for v3 in range(var_3):
            count += 1
            print("c"+str(count)+": ",end='')
            print("x"+digits[v1]+digits[v2]+digits[v3]+" + "+"x" +digits[v1]+digits[v2]+digits[v3]+" <= 123") # I put the digits[v1] in

You just needed to implement your count variable, after that in the second section you needed to put in the for v1 in range(var_1-1): so that it would actually get the first 001, although you could have just manually threw that in there but I don't know what you need this for so, yeah.

Comments

0

This is the direct way to get what you want, but without knowing why you want it that way, there may be another way to go about it:

var_1 = 2
var_2 = 1
var_3 = 3

count = 1

for v1 in range(var_1):
    for v2 in range(var_2):
        for v3 in range(var_3):
            print('c{0}: x{1:03}{2:03}{3:03} - 20 z{1:03}{2:03}{3:03} <= 0'.format(count,v1+1,v2+1,v3+1))
            count += 1

for v3 in range(var_3):
    print('c{0}: x001001{1:03} + x002001{1:03} <= 123'.format(count,v3+1))
    count += 1

On Python 3.6, it is even simpler with f-strings:

var_1 = 2
var_2 = 1
var_3 = 3

count = 1

for v1 in range(1,var_1+1):
    for v2 in range(1,var_2+1):
        for v3 in range(1,var_3+1):
            print(f'c{count}: x{v1:03}{v2:03}{v3:03} - 20 z{v1:03}{v2:03}{v3:03} <= 0')
            count += 1

for v3 in range(1,var_3+1):
    print(f'c{count}: x001001{v3:03} + x002001{v3:03} <= 123')
    count += 1

See Format Specification Mini-Language.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.