coin-or / python-mip

Python-MIP: collection of Python tools for the modeling and solution of Mixed-Integer Linear programs
Eclipse Public License 2.0
531 stars 92 forks source link

Visual Code Python debugging tool crashes when trying to access "Model" object attributes before optimization #26

Open otavio-silva opened 4 years ago

otavio-silva commented 4 years ago

Consider the following code to model the maximum independent set problem to solve the maximum clique using MIP and NetworkX:

import mip
import networkx

n = 2 ** 3
g = networkx.binomial_tree(n)
networkx.add_star(g, [i for i in range(n)])
g1 = networkx.complement(g)

model = mip.Model("Independent Set")
x = [model.add_var(var_type=mip.BINARY) for _ in range(len(g1.nodes))]
model.objective = mip.maximize(mip.xsum(x[i] for i in range(len(g1.nodes))))
for (i, j) in g1.edges:
    model += x[i] + x[j] <= 1
model.optimize()
selected = [i for i in range(len(g1.nodes)) if x[i].x >= 0.99]
print(selected)
g2 = g.subgraph(selected)

If I try to access the model variables in the watch section, it gives he following message on terminal:

Information not available, model was not optimized yet.

And then, the debugger dies. I believe this is not what should happen, some help with this?

h-g-s commented 4 years ago

Hello Otávio !

Currently we throw an exception when an specific property of a variable (the solution value x) is accessed, since the debugger is probably trying to access all properties we have this problem...

I think that the behaviour could be changed to just return None as solution value when no optimization is performed. I'll include this change in the next release.

Cheers,

Haroldo

--

Haroldo Gambini Santos Computing Department Universidade Federal de Ouro Preto - UFOP email: haroldo@ufop.edu.br Haroldo.GambiniSantos@cs.kuleuven.be home/research page: www.decom.ufop.br/haroldo

It has long been an axiom of mine that the little things are infinitely the most important. -- Sir Arthur Conan Doyle, "A Case of Identity"

On Wed, 20 Nov 2019, Otávio Augusto Silva wrote:

Consider the following code to model the maximum independent set problem to solve the maximum clique using MIP and NetworkX:

import mip import networkx

n = 2 ** 3 g = networkx.binomial_tree(n) networkx.add_star(g, [i for i in range(n)]) g1 = networkx.complement(g)

model = mip.Model("Independent Set") x = [model.add_var(vartype=mip.BINARY) for in range(len(g1.nodes))] model.objective = mip.maximize(mip.xsum(x[i] for i in range(len(g1.nodes)))) for (i, j) in g1.edges: model += x[i] + x[j] <= 1 model.optimize() selected = [i for i in range(len(g1.nodes)) if x[i].x >= 0.99] print(selected) g2 = g.subgraph(selected)

If I try to access the model variables in the watch section, it gives he following message on terminal:

Information not available, model was not optimized yet.

And then, the debugger dies. I believe this is not what should happen, some help with this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, orunsubscribe.[AB4VZOQHN473EXM6FJ7JUZTQUXXB5A5CNFSM4JP4AJ72YY3PNVWWK3TUL52HS4D FUVEXG43VMWVGG33NNVSW45C7NFSM4H27DLLQ.gif]

otavio-silva commented 4 years ago

Thanks for the feedback, glad to see this issue being addressed!

h-g-s commented 4 years ago

should be fixed in 1.6, could you please check ?

otavio-silva commented 4 years ago

I updated MIP and checked yesterday and the same behaviour continues

h-g-s commented 4 years ago

Strange, which attribute triggered the exception ?

Em dom, 1 de dez de 2019 12:19, Otávio Augusto Silva < notifications@github.com> escreveu:

I updated MIP and checked yesterday and the same behaviour continues

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/coin-or/python-mip/issues/26?email_source=notifications&email_token=AB4VZOQK2ZYDTE23K6WU2NDQWPIZFA5CNFSM4JP4AJ72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFRMDPQ#issuecomment-560120254, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4VZOQ3JXGR3ED2KOIFJITQWPIZFANCNFSM4JP4AJ7Q .

otavio-silva commented 4 years ago

I'm not sure, Visual Code shows my all attributes of the object, but I can't see any because the debugger dies before anything shows up. Not sure if relevant, but the same thing happens on Jupyter notebooks, if I use the tab key to show the attributes of the object, the kernel also dies.

h-g-s commented 4 years ago

The object that you tried to inspect in the debugger is a variable ? Right now (1.6) no exception is thrown while querying variable contents, so I don't know where to look

otavio-silva commented 4 years ago

It is, in my example code it's the model variable, before running the model.optimize() line.

h-g-s commented 4 years ago

Hi @otavio-silva , could you provide your code (or better, an small example) where it happens so that I can check if this is solved ?

otavio-silva commented 4 years ago

@h-g-s here's an example modelling the 0-1 Knapsack problem:

import mip

p = [825594, 1677009, 1676628, 1523970, 943972, 97426, 69666, 1296457, 1679693, 1902996, 1844992, 1049289, 1252836, 1319836, 953277, 2067538, 675367, 853655, 1826027, 65731, 901489, 577243, 466257, 369261]
w = [382745, 799601, 909247, 729069, 467902, 44328, 34610, 698150, 823460, 903959, 853665, 551830, 610856, 670702, 488960, 951111, 323046, 446298, 931161, 31385, 496951, 264724, 224916, 169684]
c = 6404180
n = len(w)

model = mip.Model("Knapsack")
x = [model.add_var(var_type=mip.BINARY) for _ in range(n)]
model.objective = mip.maximize(mip.xsum(p[i] * x[i] for i in range(n)))
model += mip.xsum(w[i] * x[i] for i in range(n)) <= c
model.optimize()
selected = [i for i in range(n) if x[i].x >= 0.99]
print(selected)

If I try to inspect the model variable before calling model.optimize(), the debugger dies. I'm using Visual Studio code.

tuliotoffolo commented 4 years ago

Hi @otavio-silva, I am trying to reproduce the error, however everything works fine for me. Can you provide me your python and Visual Studio Code versions?

screenshot

otavio-silva commented 4 years ago

Sorry for the delay, here's the info:

Python-MIP package version 1.6.4
Python 3.7.5 (default, Oct 31 2019, 15:18:51) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Visual Studio Code 1.41.0
9579eda04fdb3a9bba2750f15193e5fafe16b959
x64
h-g-s commented 4 years ago

Hi @otavio-silva , just released 1.6.5. There was a case when dual information was being queried when it was not available that was fixed. Maybe this is the cause of your crash. Could you test it ?

otavio-silva commented 4 years ago

I just tested it @h-g-s, and it still gives the same error, sadly.

h-g-s commented 4 years ago

hi @otavio-silva , just released 1.6.6 with a fix that may be related to this problem: querying the objective function without optimizing was crashing. Now it returns None if no solution is available. Could you please check again ?

otavio-silva commented 4 years ago

I just tested it with the new version, there's still something causing crashes.