Piecewise linear approximation for log2 objective term
回答済みHello Everyone,
Currently, I am stuck with this problem where I need to solve Shannon capacity formulation with all given parameters. But, as Gurobi cannot solve log_2 terms directly, therefore I used piecewise general constraint. Now, I need to put the distance term in the denominator for different transmitters and receivers and approximate the log term for transmitting power in the numerator. By doing this, there is a mismatch between the x and y approximation, i.e., their terms are not equal in number.
Could you please guide me on how to resolve this issue?
If you have any questions, kindly let me know.
Thanks
import gurobipy as gp
from gurobipy import GRB
import math
import numpy as np
m = gp.Model()
f = 2
e = 0.1
c = 0.5
a = 5
b = 50
dx1 = np.zeros(a)
dy1 = np.zeros(a)
dz1 = 20* np.ones(a)
for i in range(a):
dx1[i] = np.random.uniform(0, 250)
dy1[i] = np.random.uniform(0, 250)
dx2 = np.zeros(b)
dy2 = np.zeros(b)
dz2 = np.ones(b)
for j in range(b):
dx2[j] = np.random.uniform(0, 1000)
dy2[j] = np.random.uniform(0, 1000)
# variables
x = m.addVars(a, b, vtype=GRB.CONTINUOUS, name="x")
y = m.addVars(a, b, vtype=GRB.CONTINUOUS, name="y")
# create x-points and y-points for approximation of y = log2(1+(cx) / ( (d1-d2) * (e+f) )
xs = [0.01*i for i in range(201)]
ys = [np.log2(1 + (c*p) / ( ((dx1[i]-dx2[j])**2 + (dy1[i]-dy2[j])**2 + (dz1[i]-dz2[j])**2 ) * (e+f) ) ) for p in xs for i in range(a) for j in range(b)]
# add piecewise-linear constraint
for i in range(a):
for j in range(b):
m.addGenConstrPWL(x[i, j], y[i, j], xs, ys, 'pwl')
# add objective function
m.setObjective(y.sum(), sense=GRB.MAXIMIZE)
m.update()
m.Params.OutputFlag = 0
m.Params.InfUnbdInfo = 1
m.Params.DualReductions = 0
m.optimize()
obj = m.getObjective()
print("########## The objective value is: ", obj.getValue(), "##############" )
v = m.getVars()
for i in range(a):
for j in range(b):
print(v[i, j].x)
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi,
you are constructing one extremely long list \(\texttt{ys}\). However, you want to make this a list of lists dependent on the \(\texttt{a}\) and \(\texttt{b}\) indices. One way to achieve this would be
ys = []
for i in range(a):
for j in range(b):
ys.append([np.log2(1 + (c*p) / ( ((dx1[i]-dx2[j])**2 + (dy1[i]-dy2[j])**2 + (dz1[i]-dz2[j])**2 ) * (e+f) ) ) for p in xs])
# add piecewise-linear constraint
for i in range(a):
for j in range(b):
m.addGenConstrPWL(x[i, j], y[i, j], xs, ys[i*b+j], 'pwl')Moreover, please note that the getVars returns a single indexed list. Thus, you can access the variable values via
for v in m.getVars():
print('%s %g' % (v.varName, v.x))Best regards,
Jaromił0 -
Thank you Jaromil Najman
The code is working now.
0
投稿コメントは受け付けていません。
コメント
3件のコメント