メインコンテンツへスキップ

How to print a variable value

回答済み

コメント

3件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff Gurobi Staff
    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?.
  • Silke Horn
    • Gurobi Staff Gurobi Staff

    Hi,

    There are a few issues in your code: I assume that the lines that add the constraints "constraint1" and "constraint2" should read "for TT in set_T" and the closing parantheses should be put after "set_T" instead. Moreover, you need to call "scm.optimize()" before you can access the variables' X attribute. Also note that your call to scm.update() is not necessary.

    With these fixes, your code should print all variable names (including ATOTAL and BTOTAL) and their values in the optimal solution. However, the values of ATOTAL and BTOTAL will likely be 0 since these variables are not used in your constraints.

    In fact, you first define ATOTAL and BTOTAL to be decision variables in your model, directly afterwards you override them to be linear expressions that you then use to build the objective function. However, you only used the Python variables (and not the model's decision variables) to build the objective. You need to add constraints if you want your decision variables ATOTAL and BTOTAL to be equal to those quicksum expressions. E. g.:

    scm.addConstr(ATOTAL == quicksum( 3 * A[TT] for TT in set_T))

    scm.addConstr(BTOTAL == quicksum( 5 * B[TT] for TT in set_T))

    0
  • Han Shih
    • Gurobi-versary
    • Conversationalist
    • Curious

    Hi Silke,

     

    Thank you for your help. I got it working to print a variable value.

     

    Robert

    0

投稿コメントは受け付けていません。