dwave-examples / nurse-scheduling

A demo of a nurse scheduling model
Apache License 2.0
45 stars 38 forks source link

Trouble reproducing the exact qubo equation #14

Open gYunTian opened 3 years ago

gYunTian commented 3 years ago

I refer to both the hard shift and soft nurse constraint.

In doing my algebra, I wasn't able to reproduce the exact equation found in the documentations for both constraints. I think I may have gotten rusty at my high school math but please look at the following and enlighten me:

Mine: Expanding the following equation and removing the constant:

    lagrange_hard_shift * sum_d ( effort * sum_n q_i(n,d) - workforce ) ** 2

gives me the following:

    lagrange_hard_shift * effort ** 2 * sum_d sum_m sum_n q_i(n,d) q_j(m, d)   (Similar)
    - 2 * lagrange_hard_shift * sum_d sum_n q_i(n,d) * effort * workforce   (Different)

Documentation: Notice the (Different) parts - above vs the bottom one:

    lagrange_hard_shift * (effort ** 2 - 2 effort * workforce) * sum_d sum_n q_i(n,d)   (Different)
    + lagrange_hard_shift * effort ** 2 * sum_d sum_m sum_n q_i(n,d) q_j(m, d)    (Similar)

There is an additional effort **2 among other things. Can someone please kindly explain the differences? Thanks

hhtong commented 3 years ago

Hi @gYunTian,

Here's a response from @amahmudwave and @pierrelouisp:

When expanding:

lagrange_hard_shift * sum_d ( effort * sum_n q_i(n,d) - workforce ) ** 2

We get:

lagrange_hard_shift * effort ** 2 * sum_d sum_m sum_n q_i(n,d) q_j(m, d) - 2 * lagrange_hard_shift * sum_d sum_n q_i(n,d) * effort * workforce   (Your answer)
  +  lagrange_hard_shift * (effort ** 2 * sum_d sum_n q_i(n,d))

This is because

(sum_n q_i(n,d))** 2 == sum_n q_i(n,d)**2  + sum_m sum_n q_i(n,d) q_j(m, d) == sum_n q_i(n,d)  + sum_m sum_n q_i(n,d) q_j(m, d)

When we take the summation of binary variables and square them, we get the sum of the different variables multiplied with each other, plus the sum of the square of individual variables. ie. (a + b)**2 == (a**2 + b**2 + 2ab).

The sum of the square of individual variables is equal to the sum of the original binary variables, since 0 2 = 0 and 1 2 = 1.

Hope this helps!