Is it possible to extract the QuadraticModel variable value during the simulation?

I need to add the following constraint: m<=SOC(t)<= M . The SOC(t) function is defined as SOC(t) = SOC(t-1) + a(t)*p(t), where p(t) is the integer variable defined as : p = [Integer('p_'+str(t)) for t in range(3)], and a(t)=1 if p(t)>0, else a(t)= -1 if p(t)<=0. I'm having trouble in defining the a(t) function since p(t) is a 'QuadraticModel' type but 0 is an integer. When I define a(t) in the following way:
def a(t) :
if p_B[t] > 0 :
return 1
else:
return -1
It gives error message : TypeError: '>' not supported between instances of 'QuadraticModel' and 'int'

Is there a way to successfully define a(t) in SOC(t)?

Below is the sample code: ------------------------------------------------->

import dimod
from dimod import ConstrainedQuadraticModel, Binary, Integer, SampleSet
from dwave.system import LeapHybridCQMSampler
import numpy as np
import hybrid
from hybrid.reference.kerberos import KerberosSampler


#----------------------define variables----------------------
#there are 3 variables regarding p_B
p_B = [Integer('p_B_'+str(t),lower_bound=-800, upper_bound=800) for t in range(3)]

#----------------------Instantiate the CQM model----------------------
cqm = ConstrainedQuadraticModel()
#----------------------define the SOC(t)----------------------
def a(t) :
if p_B[t] > 0 :
return1
else:
return -1

SOC=[]
SOC.append(0.3)#the initial value of SOC[0] = 0.3

for i in range(3):
SOC.append(SOC[i] + a(i)*p_B[t]) #the definition of SOC is: SOC[t] =
#----------------------define objective----------------------
P_B = dimod.quicksum((p_B[t]-t) for t in range(3))
cqm.set_objective(P_B)

#----------------------define constraints----------------------
for t in range(3):
cqm.add_constraint( 0.1 <= SOC[t]<= 0.9)
#----------------------start sampling----------------------
sampler = LeapHybridCQMSampler()
sampleset = sampler.sample_cqm(cqm)
best_feasible = sampleset.first.sample
print('This is solution: ', best_feasible)






0

Comments

2 comments
  • Sorry, there is a typo in the definition of SOC function, where 

    for i in range(3):
    SOC.append(SOC[i] + a(i)*p_B[t])
     
    should be corrected as 
    for i in range(3):
    SOC.append(SOC[i] + a(i)*p_B[i]) 
     
    0
    Comment actions Permalink
  • Hello,

    What does p_B[t] > 0 mean here in this case?

    p_B[t] is a QuadraticModel, meaning that it is more than a single value. 
    It could be a whole set of values, such as points on a curve or surface.

    Is this equation meant to see if it is a non-trivial solution (i.e. the case where all biases are 0)?

    If it's possible to better understand this question then maybe the solution to the original question will become clearer?

    Please let us know if this is helpful.

    0
    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post