Function applied to variable for more flexible domain
AnsweredOptimizing a problem f(x), where 0.0 <= x <=1 .0
Is it possible to achieve below when solving x?
when x < threshold, x== 0
when x >= threshold, x== x

You can define x as a semicontinuous variable. A semicontinuous variable has the property that it takes a value of 0 or a value between the specified lower and upper bounds. With the Python API, you can define your x variable as follows:
x = model.addVar( lb = threshold, vtype = "S", name ="x")
0 
Thank you. I'll try it out
0 
Have checked that it would work as expected. Would it be possible to mix it with the continuous variable? For example, set x1x5 to be semicontinuous and the rest to be just continuous
0 
Yes, a model can have both semicontinuous and continuous variables.
0 
Sorry I meant mixing semicontinuous and continuous for a variable with length n
That sounds like just using different ub/lb for each x_i. Could you confirm?
For example,
n = 10
threshold = 0.2
lb = np.zeros(n)
lb[0] = threshold
x = model.addVar(n, lb=lb , ub=1.0, vtype="S", name="x")But if want to pin my x_i (ub and lb are the same), can it still by any chance bring it to 0?
0 
One approach to do this would be to set the "vtype" attribute for the variables according to the types you want. In the example below, x[0], x[1], and x[2] will be semicontinuous variables with lower bounds of 1, and x[3] and x[4] will be continuous variables with the default lower bound of zero.
lb = [1,1,2,0,0]
x = m.addVars(5, lb=lb, vtype=["S" for _ in range(3)] + ["C" for _ in range(2)], name="x")I hope this helps.
Best regards,
Simran0 
That's what I'm looking for. Thank you!
0
Please sign in to leave a comment.
Comments
7 comments