Skip to main content

Unable to use the setAttr() method in gurobipy

Answered

Comments

4 comments

  • Virgil Stokes
    Gurobi-versary
    First Comment
    First Question

    Note, when I selected code and then did a copy an paste of the actual code, the indented spaces were ignored :-( Please look a the link https://github.com/fzsun/cutstock-gurobi

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi,

    Line \(\texttt{m.setAttr('VType', x.values(), 'I'*len(x))}\) actually expands to \(\texttt{m.setAttr('VType', x.values(), 'IIIIIIIIIIIII')}\), since \(\texttt{len(x)}\) equals 13 resulting in the error you see.

    You can achieve setting the type of all \(x\) variables to integer by executing \(\texttt{m.setAttr('VType', x.values(), 'I')}\) or
    \(\texttt{m.setAttr('VType', x.values(), ['I']*len(x))}\).
    Please note that in the first option, all variables types are going to be set to integer as exactly one character \(\texttt{'I'}\) is used for all variables \(x\). In the second option a list \(\texttt{['I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I']}\) is constructed and passed to the \(\texttt{setAttr()}\) function, setting the type of each of the \(x\) variables individually to integer.

    Best regards,
    Jaromił

    1
  • Virgil Stokes
    Gurobi-versary
    First Comment
    First Question

    Very good Jaromil,

    Thank you for your prompt reply to my question. Indeed this solves the problem with setAttr() and gives an answer to the 1D cutting stock problem. However, when I examine the results obtained, I am a little botherd by the trim loss (the demands for the widths of 18 and 34 are exceeded by 2 and 1 respectfully). I would be interested in your thoughts on the results obtained, Jaromil. Perhaps the formulation of the problem by Fangzhou Sun could be improved.

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Virgil,

    The exceeded amounts arise because the optimizer tries to reduce the waste of the roll. E.g., it would suffice to use

    1 * [0, 0, 3, 0, 0] 

    instead of

    1 * [0, 0, 5, 0, 0]

    but the cutstock waste would be larger, namely 44 instead of 10. Thus, the solution with an exceed of 2 for the demand of 18 width is "better" in terms of "waste reduction". 

    It would also be possible to slightly variate the patterns but, the optimal solution of 38 rolls could not be lowered anyway.

    Best regards,
    Jaromił

    0

Please sign in to leave a comment.