Confusion about the network flow example in Python tutorial
回答済みI am new to using Gurobi to solve some optimization problems (especially graph-related problems). I took a quick look at your Python tutorial (https://www.gurobi.com/wp-content/plugins/hd_documentations/documentation/9.1/quickstart_linux.pdf).
In the network.py example, it is different from previous two examples (i.e. mip1.py, matrix1.py) as I could not find syntax like
m.setObjective(obj @ x, GRB.MAXIMIZE)
Additionally, the network.py skips try...except... syntax used in previous two examples.
I am wondering
- Why the m.setObjective() could be ignored? As network problem is not a feasibility problem (it is minimizing the cost of transportation rather than find feasible decision variables), is the Gurobi minimizing something implicitly?
- Is try...except... syntax preferred or not? It seems to me the practice of network.py (i.e. using something like if m.status == GRB.OPTIMAL) is better as it causes less hassle about indentation.
Thank you in advance!
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Yui,
Why the m.setObjective() could be ignored? As network problem is not a feasibility problem (it is minimizing the cost of transportation rather than find feasible decision variables), is the Gurobi minimizing something implicitly?
In the netflow.py example, the coefficients of the variables in the objective function are set in the call to addVars(). The \(\texttt{obj}\) argument sets the corresponding coefficients in the objective function. The objective function is minimized by default.
Is try...except... syntax preferred or not? It seems to me the practice of network.py (i.e. using something like if m.status == GRB.OPTIMAL) is better as it causes less hassle about indentation.
It depends on your application. If you rather have a small script where you expect that no error occurs due to its simplicity, then omitting the \(\texttt{try - except}\) syntax is fine. However, it is often better to be double safe and just use the \(\texttt{try - except}\) syntax. You can always build the \(\texttt{try - except}\) block around one outsourced function in order to avoid the indentation issue.
Note that
if (m.status == GRB.OPTIMAL)
does not save you from possible other errors which may occur during the execution of your code.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント