DQM Example

Hello all

I am looking for an example of DQM with linear (no quadratic) biases and the discrete variables being taken from an array of float numbers.

In my problem, I’d like to have my objective function in the linear form f(Z)=CZ where C and Z are arrays with float variables

 C= array([-0.0845, -0.2936, -0.6903, -1.041 , -0.3494,  0.    ,  0.3494,   1.041 ,  0.6903,  0.2936])

Z can take any value from 10 cases listed in:

valid_z= array([0.  , 0.4958104 , 0.47840023, 0.43096018, 0.35200113,  0.31345015, 0.31345015, 0.28250982, 0.24629329, 0.23308393]) 

I wrote this program:

from dimod import Integer, DiscreteQuadraticModel, ExactDQMSolver

n=10

dqm = DiscreteQuadraticModel()

for i in range(n):   

   _ = dqm.add_variable(n, label='z' + str(i))

   for j in range(n):       

      dqm.set_linear_case('z'+str(i), j, c[i]) 

The above program can set up the variable arrays with linear biases associated to each case, but the valid values for the cases are actually not assigned. I do not know how to tell DQM that my z values are restricted to the members of an array (valid_z above). Please could you guide me? thanks a lot!

0

Comments

3 comments
  • Hello,

    I am having a little trouble understanding exactly what you would like to do.
    Could you please provide a little clarification?

    It sounds like you want to have the Z variables (z0, z1, z2,..) be discrete values from the set z_valid.

    With the code example you have above, each variable can only be one of the values in the c array.
    This could be an indexing issue, as you have c[i]. If you were to set this to c[j], then each of the z variables could be one of the values in the c array.

    Example:

    >>>dqm.get_linear("z0")
    array([0.        , 0.4958104 , 0.47840023, 0.43096018, 0.35200113,
           0.31345015, 0.31345015, 0.28250982, 0.24629329, 0.23308393])

    This does not take into account valid_z in any way. I am not sure how that fits in just, yet but hopefully you can provide a little clarification.

    With the above example, calling dqm.getlinear() on any of the z variables will return the same array, as these are the discrete values that z0-z10 can be selected from, based on the loop in the code example you provided.

    Please let us know if this helps to resolve your problem. 
    I look forward to receiving further details.

    For future code examples, please be sure to provide a complete example that can be copied and pasted directly into a Python interpreter. This will help make the process a bit quicker and easier.

    0
    Comment actions Permalink
  • Hello David, thanks for looking into my query.

    Sorry things were not clear enough. You may ignore my codes now, as I know this is not what it should be. I just try to clarify my goal here.

    I have a linear function to minimise: f=c1z1+c2z2+...c10z10

    Each z_i can only be chosen from the set valid_z written above. The size of valid_z set is eventually 10 but could be any other size.

    I am looking to know how this can be coded in DQM. A sample source code will be highly appreciated.

    Thanks again.

    0
    Comment actions Permalink
  • Hello,

    Just so I am understanding correctly, let me state the problem.
    You have a problem:
    f=c1*z1+c2z2+...c10z10
    Where c values are constants and z values are DQM variables with n cases each.
    You would like to multiply the constant c by the value of the z case. Correct?

    The values of the z cases are essentially just labels, and would not take part in the calculation at all.
    They would evaluate to 1 or 0. The z variable has n cases and acts as a one-hot constraint.
    This means that one of the cases will evaluate to 1 and the rest to 0.

    In order to accomplish the behaviour you are interested in, you would need to precalculate the values of c1*z1_case1, c1*z1_case2... up to c10*z10_case10.
    Then you would set the linear values for each of the cases for each of the variables.

    It would look something like:

    #Create the dqm
    dqm = dimod.DiscreteQuadraticModel()
    z=["z1","z2","z3"]
    #Set the constants
    c = [1,2,3]
    #Set the valid cases for z
    valid_z = [1,2,3,4,5,6,7,8,9,10]
    #Add the variables z_1 through z_n with number of valid cases being 10
    for i in range(len(z)):
       dqm.add_variable(len(valid_z), label=z[i])
    #Compute the linear terms for all of the cases for all of the variables
    for i in range(len(c)):
       z_biases[i] = c[i]*valid_z
    #Set the linear biases for all cases of all variables
    for i in range(len(c)):
     dqm.set_linear(z[i], z_biases[i])

    Alternatively you can set the linear case terms one-by-one:

    dqm.set_linear_case(z1, "case_1", c1*valid_z["case_1"])

    Hopefully this is helpful.
    Please let us know if you have any questions.

    0
    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post