Ask Your Question

MixedIntegerLinearProgram strange behavior

asked 2019-09-30 22:13:18 +0200

Cyrille gravatar image

updated 2019-10-01 12:54:21 +0200

This is a reedition of my initial question

I find missleading the following comportment (I have add name="w") according the suggestion of dsejas

%display latex p =


solver = "GLPK")

w = p.new_variable(integer=True,


p.add_constraint(w[0] + w[1] + w[2] - 14*w[3] == 1)

p.add_constraint(w[1] + 2w[2] - 8w[3] == 0)

p.add_constraint(2w[2] - 3w[3] == 0)

p.add_constraint(w[0] - w[1] - w[2] >= 0)

p.add_constraint(w[3] >= 1)

p.set_objective(w[2]+ 5*w[3])

but look at what is shown. The $x_i$'s are in the display. Then I have typpeset :

b = p.get_backend()

b.solver_parameter("simplex_or_intopt", "simplex_only")

b.solver_parameter("verbosity_simplex", "GLP_MSG_ALL")

ans = p.solve()


followed by



and finally



Look at w[2]'s value. It is not an integer value.

edit retag flag offensive close merge delete


Could you please put each command in it own line ?

tmonteil gravatar imagetmonteil ( 2019-10-01 01:27:21 +0200 )edit

I agree with tmontiel. Your code should be formatted. Also, are you using implicit multiplication? Your code seems to be missing some asterisks.

Concerning the $w$ vs $x$ problem, I can suggest you to use

w = p.new_variable(integer=True, nonegative=True, name="w")

That should allow you to have $w$ as variable instead of $x$.

As for the LaTeX output, I am afraid the show() method outputs a pre-formated string, so you cannot display it as LaTeX, as far as I know. However, I can suggest you to define your own function to take care of this. Depending on your skills, it should be easy for you. If it's not, please, let me know, so I can check if I can develop a small piece of code for you.

dsejas gravatar imagedsejas ( 2019-10-01 06:14:36 +0200 )edit

dsejas I am just a beginner in Sagemath. I will appreciate if you develop the small piece of code as you just sugggest. Thanks a lot.

Cyrille gravatar imageCyrille ( 2019-10-01 08:34:31 +0200 )edit

Hello, @Cyrille. OK, I will try to write a subroutine to output LaTeX code; it may take me some time. However, I would like to know: Do you want the problem or the solution output as LaTeX? Or do want both?

On the other hand, is showing $x$ as variable because of the way this command formats its output. However, you may have noticed that there are $w$'s in the description of the problem, by using the name='w' option, as I suggested.

Concerning the non-integer values, I am not completely sure how the solver_parameter method you used works, but you seem to have established wrong parameters there. If I remove the two lines with that method, I get integer solutions. Check my following comment (I run out of allowed characters).

dsejas gravatar imagedsejas ( 2019-10-01 18:51:31 +0200 )edit

You can check the code I modified from your MILP problem here. I hope I got it right.

dsejas gravatar imagedsejas ( 2019-10-01 18:52:45 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-10-03 04:38:51 +0200

dsejas gravatar image

Hello, @Cyrille. I have written three pieces of code for MILPs. The first one displays the problem description as Sage itself does with, where p is a MILP, but it is shown in LaTeX format. You can check it here. Sorry I was too verbose with comments. You can delete them safely.

The second piece of code displays the description of the problem on a more mathematical notation. The code is here. I didn't use comments because it is essentially the same code, but with different output.

The third piece os code produces the solution to the problem in LaTeX format. You can find it here.

Finally, here you have an example using the problem of your original question. Uncomment the last three instructions alternatively to see the results.

Hopefully, this works correctly in all cases. If not, please let me know.

I hope this helps!

edit flag offensive delete link more


By the way, sorry for not using the Sage Cell Server for the code. Unfortunately, it generates so long permalinks, that they cannot be pasted here correctly. That's hwy I used Paste Debian.

dsejas gravatar imagedsejas ( 2019-10-03 04:40:05 +0200 )edit

Sorry fo my incompetence but how did I load your code in a sage notebook

Cyrille gravatar imageCyrille ( 2019-10-03 09:14:39 +0200 )edit

Hello, @Cyrille! Actually, that's a good question. I am not that experienced with Sage Notebooks. One way would be to copy the code to the Notebook itself. Another way would be to put all the code in a file, let's say, copy the file to the same directory where you will run the Sage Notebook, and write from milp_latex import * in the notebook itself.

If you use the file, be sure to use only one definition of the function _str_coeff in the code I sent you. It is defined twice, one in the first piece of code, and once in the second.

As I tell you, I have not too much experience with Sage Notebooks, because I prefer to work on a terminal. There could be other ways to use the code there, but I don't know them. May I suggest you to ...(more)

dsejas gravatar imagedsejas ( 2019-10-03 20:49:41 +0200 )edit

dsejas I have tested your code with the exemple. It works perfectly. I will experiment it on many problem then I will give you my return on experiment. I would appreciate to be able to thank you in my text. For this I need your true name. I can understand that it is not in the policy of this site to reveal this and the adresses of the people. I am professor in the University of Orleans (France) and I am thr lone Cyrille as economist. So you can easily find my email address.

Cyrille gravatar imageCyrille ( 2019-10-04 03:48:59 +0200 )edit

answered 2019-10-01 01:26:52 +0200

tmonteil gravatar image

To use p.get_values, you should first do p.solve() first. However, in your case, there is no solution:

sage: p.solve()
MIPSolverException: GLPK: Problem has no feasible solution
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-09-30 22:13:18 +0200

Seen: 187 times

Last updated: Oct 03 '19