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

Map integer to binary

回答済み

コメント

3件のコメント

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

    Hi,

    It is possible with two auxiliary variables and a couple of equality constraints. First, we have to model the modulo operation

    \[\begin{align}
    N &= n + 3 k\\
    N &\in \mathbb{Z}_{\geq 0}, n \in \{0,1,2\}, k \in \mathbb{Z}_{\geq 0}
    \end{align}\]

    Integer variable \(n\) gives us the result of the modulo operation. Next we need to model the \(\texttt{if}\)-relationships.

    \[\begin{align}
    2 n_0 &+ 3 n_1 + 4 n_2 = n+2 \\
    &n_0, n_1, n_2 \in \{0,1\}
    \end{align}\]

    The above equality constraint states that, when \(n=0\), then only \(n_0=1\), if \(n=1\), then only \(n_1=1\), and if \(n=2\), then only \(n_2=1\).

    Please note that the above is just one of many possible formulations and is not guaranteed to be the best one.

    Best regards,
    Jaromił

    0
  • jielin yang
    • Gurobi-versary
    • First Question
    • First Comment

    Thank you Jaromił! This solution really inspires me!

    0

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