# MixedIntegerLinearProgram strange behavior

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 =

MixedIntegerLinearProgram(maximization=False,

solver = "GLPK")

w = p.new_variable(integer=True,

nonnegative=True,name="w")

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(w[0] - w[1] - w[2] >= 0)

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

p.show()

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()

ans

followed by

val=p.get_values(w)

val

and finally

val0=p.get_values(w[2])

val0

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

edit retag close merge delete

Could you please put each command in it own line ?

( 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.

( 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.

( 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, p.show() 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).

( 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.

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

Sort by » oldest newest most voted

Hello, @Cyrille. I have written three pieces of code for MILPs. The first one displays the problem description as Sage itself does with p.show(), 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!

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.

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

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

( 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 milp_latex.py, 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 milp_latex.py 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)

( 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.

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

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

more