I’m trying to solve a problem which is Minimize log2(1 + 1/x). 1<=x<=3
Because cvxpy can’t use the log2 function, I use cp.log (1 + x) / np.log (2) instead.
This is my code.
x = cp.Variable()
obj = cp.Minimize(cp.log(1 + 1 / x) / cp.log(2))
constraints = [x >= 1, x <=3]
prob = cp.Problem(obj, constraints)
prob.solve()
error------------------------------------
The objective is not DCP. Its following subexpressions are not:
1.0 / var0
See under entropy at
for one solution to your problem.
Thank you for your reply. But Can you tell me how to use this form? I don’t understand。
First of all this is not a cvxpy forum.
Second, since you anyhow need the exponential cone to model logarithm, you can model
t\geq log(1+1/x)
as
t\geq log(1+e^u)
e^u\geq 1/x
and then minimize t.
I will leave it to you to figure out which of the cvxpy atoms from https://www.cvxpy.org/tutorial/functions/index.html will help you achieve that.
Thanks a lot for your replay, but solver told me that constraints are not DCP.
This is my code
obj = cp.Minimize(t)
constraints = [ ]
constraints.append(x <= 3)
constraints.append(x >= 1)
constraints.append(t >= cp.logistic(u))
constraints.append(cp.exp(u) >= 1 / x)
exp(var2) <= 1.0 / var0 , because the following subexpressions are not:
Resolving how to write e^u\geq 1/x in a dcp compliant way is part of the exercise left for you. You can also check https://docs.mosek.com/modeling-cookbook/expo.html#log-sum-inv
Thank you so much! You already helped me a lot. The last problem, Generally speaking,does the base number of logarithmic function affect the concavity and convexity of function? For example, log2(1 + 1/ x) is convex for x. what about loge(1 + 1/x)?
As you wrote in your first model it is just multiplication by a scalar, so no change to convexity as long as base > 1.
Thank you again and wish you a happy life!