I'm tried to implementation of the "Maximum product SINR -based power allocation" method.
first of all, I changed to the maxprod_sinr Matlab function converted to the python language.
But the converted code is not working, also occurred the error as below:
I Implemented the 'maxprod_SINR' function as follow
# Variable for the K x L BS power allocation matrix
rho_p = cp.Variable((self.K, self.N))
# Variable for the K x L SINR matrix
sinr = cp.Variable((self.K, self.N))
# Create constraints.
constraints = []
# Form objective
obj = cp.Maximize(cp.sum(sinr))
for j in range(self.N):
for k in range(self.K):
# SINR constraints of UE k in cell j
if signal[k,j] > 0:
# sinr[k,j]*(cp.sum(cp.sum(rho_p.T # interference[:,:,k,j])) + 1) <= (rho_p[k,j]*signal[k,j]);
constraints.append(sinr[k,j]*(cp.sum(cp.sum(rho_p.T # interf[:,:,k,j])) + 1) <= (rho_p[k,j]*signal[k,j]))
constraints.append(rho_p[k,j] >= 0)
# This applies if UE k in cell J is inactive
else:
constraints.append(sinr[k,j] == 1)
constraints.append(rho_p[k,j] >= 0)
constraints.append(cp.sum(rho_p[:,j]) <= self.Pmax)
# Maximzing the power indirectly by prod(sinr) the power constraints
prob = cp.Problem(obj, constraints)
prob.solve()
# The problem was not solved by CVX, for some reason, and we then consider equal power allocation
if prob.status == 'infeasible':
feasible = False
rhoSolution = (self.Pmax / self.K) * np.ones((self.K, self.N))
# The problem was solved by CVX
else:
feasible = True
rhoSolution = rho_p
# Converting variable() to ndarray()
rhoSolution = np.array(rhoSolution.value)
rhoSolution = rhoSolution.copy()
return feasible, rhoSolution
Related
currently running into a problem solving this.
The objective of the exercise given is to find a polynom of certian degree (the degree is given) from a dataset of points (that can be noist) and to best fit it using least sqaure method.
I don't understand the steps that lead to solving the linear equations?
what are the steps or should anyone provide such a python program that lead to the matrix that I put as an argument in my decomposition program?
Note:I have a python program for cubic splines ,LU decomposition/Guassian decomposition.
Thanks.
I tried to apply guassin / LU decomposition straight away on the dataset but I understand there are more steps to the solution...
I donwt understand how cubic splines add to the mix either..
Edit:
guassian elimintaion :
import numpy as np
import math
def swapRows(v,i,j):
if len(v.shape) == 1:
v[i],v[j] = v[j],v[i]
else:
v[[i,j],:] = v[[j,i],:]
def swapCols(v,i,j):
v[:,[i,j]] = v[:,[j,i]]
def gaussPivot(a,b,tol=1.0e-12):
n = len(b)
# Set up scale factors
s = np.zeros(n)
for i in range(n):
s[i] = max(np.abs(a[i,:]))
for k in range(0,n-1):
# Row interchange, if needed
p = np.argmax(np.abs(a[k:n,k])/s[k:n]) + k
if abs(a[p,k]) < tol: error.err('Matrix is singular')
if p != k:
swapRows(b,k,p)
swapRows(s,k,p)
swapRows(a,k,p)
# Elimination
for i in range(k+1,n):
if a[i,k] != 0.0:
lam = a[i,k]/a[k,k]
a[i,k+1:n] = a[i,k+1:n] - lam*a[k,k+1:n]
b[i] = b[i] - lam*b[k]
if abs(a[n-1,n-1]) < tol: error.err('Matrix is singular')
# Back substitution
b[n-1] = b[n-1]/a[n-1,n-1]
for k in range(n-2,-1,-1):
b[k] = (b[k] - np.dot(a[k,k+1:n],b[k+1:n]))/a[k,k]
return b
def polyFit(xData,yData,m):
a = np.zeros((m+1,m+1))
b = np.zeros(m+1)
s = np.zeros(2*m+1)
for i in range(len(xData)):
temp = yData[i]
for j in range(m+1):
b[j] = b[j] + temp
temp = temp*xData[i]
temp = 1.0
for j in range(2*m+1):
s[j] = s[j] + temp
temp = temp*xData[i]
for i in range(m+1):
for j in range(m+1):
a[i,j] = s[i+j]
return gaussPivot(a,b)
degree = 10 # can be any degree
polyFit(xData,yData,degree)
I was under the impression the code above gets a dataset of points and a degree. The output should be coeefients of a polynom that fits those points but I have a grader that was provided by my proffesor , and after checking the grading the polynom that returns has a lrage error.
After that I tried the following LU decomposition instead:
import numpy as np
def swapRows(v,i,j):
if len(v.shape) == 1:
v[i],v[j] = v[j],v[i]
else:
v[[i,j],:] = v[[j,i],:]
def swapCols(v,i,j):
v[:,[i,j]] = v[:,[j,i]]
def LUdecomp(a,tol=1.0e-9):
n = len(a)
seq = np.array(range(n))
# Set up scale factors
s = np.zeros((n))
for i in range(n):
s[i] = max(abs(a[i,:]))
for k in range(0,n-1):
# Row interchange, if needed
p = np.argmax(np.abs(a[k:n,k])/s[k:n]) + k
if abs(a[p,k]) < tol: error.err('Matrix is singular')
if p != k:
swapRows(s,k,p)
swapRows(a,k,p)
swapRows(seq,k,p)
# Elimination
for i in range(k+1,n):
if a[i,k] != 0.0:
lam = a[i,k]/a[k,k]
a[i,k+1:n] = a[i,k+1:n] - lam*a[k,k+1:n]
a[i,k] = lam
return a,seq
def LUsolve(a,b,seq):
n = len(a)
# Rearrange constant vector; store it in [x]
x = b.copy()
for i in range(n):
x[i] = b[seq[i]]
# Solution
for k in range(1,n):
x[k] = x[k] - np.dot(a[k,0:k],x[0:k])
x[n-1] = x[n-1]/a[n-1,n-1]
for k in range(n-2,-1,-1):
x[k] = (x[k] - np.dot(a[k,k+1:n],x[k+1:n]))/a[k,k]
return x
the results were a bit better but nowhere near what it should be
Edit 2:
I tried the chebyshev method suggested in the comments and came up with:
import numpy as np
def chebyshev_transform(x, n):
"""
Transforms x-coordinates to Chebyshev coordinates
"""
return np.cos(n * np.arccos(x))
def chebyshev_design_matrix(x, n):
"""
Constructs the Chebyshev design matrix
"""
x_cheb = chebyshev_transform(x, n)
T = np.zeros((len(x), n+1))
T[:,0] = 1
T[:,1] = x_cheb
for i in range(2, n+1):
T[:,i] = 2 * x_cheb * T[:,i-1] - T[:,i-2]
return T
degree =10
f = lambda x: np.cos(X)
xdata = np.linspace(-1,1,num=100)
ydata = np.array([f(i) for i in xdata])
M = chebyshev_design_matrix(xdata,degree)
D_x ,D_y = np.linalg.qr(M)
D_x, seq = LUdecomp(D_x)
A = LUsolve(D_x,D_y,seq)
I can't use linalg.qr in my program , it was just for checking how it works.In addition , I didn't get the 'slow way' of the formula that were in the comment.
The program cant get an x point that is not between -1 and 1 , is there any way around it , any normalizition?
Thanks a lot.
Hints:
You are probably asked for an unsophisticated method. If the degree of the polynomial remains low, you can use the straightforward approach below. For the sake of the explanation, I'll use a cubic model.
Assume that you want to fit your data to this polynomial, by observing that it seems to follow a cubic behavior:
ax³ + bx² + cx + d ~ y
[All x and y should be understood with an index i which is omitted for notational convenience.]
If there are more than four data points, you get an overdetermined system of equations, usually with no solution. The trick is to consider the error on the individual equations, e = ax³ + bx² + cx + d - y, and to minimize the total error. As the error is a signed number, negative errors would make minimization impossible. Instead, we minimize the sum of squared errors. (The sum of absolute errors is another option but it unfortunately leads to a much harder problem.)
Min(a, b, c, d) Σ(ax³ + bx² + cx + d - y)²
As the unknown parameters are unconstrained, it suffices to look for a stationary point, i.e. cancel the gradient of the total error. By differentiation on the unknowns a, b, c and d, we obtain
2Σ(ax³x³ + bx²x³ + cxx³ + dx³ - yx³) = 0
2Σ(ax³x² + bx²x² + cxx² + dx² - yx²) = 0
2Σ(ax³x + bx²x + cxx + dx - yx ) = 0
2Σ(ax³ + bx² + cx + d - y ) = 0
As you can recognize, this is a square linear system of equations.
I am currently trying to solve a Mixed Integer Non Linear Problem with Gekko using its Branch&Bound implementation coupled with its warm-start method to speed up and improve the convergence process compared to vanilla branch& bound.
The algorithm finds a solution after a short amount of time. Nevertheless, it hurts the constraint, which I might have defined wrongly: I have a gekko array-variable X and need another gekko array-variable "indices_open" that saves every index of x where x == 1. This "indices_open" goes into another self-defined function which is expecting "indices_open" as an numpy array and does not accept a list or gekko-array of gekko-intermediate variables. The self-defined function returns a numpy array. This final array shall be used in m.Equations and I therefore cast it to a gekko variable array.
Needless to say, something went wrong and the current solution hurts the inequality constraint, while the equality constraint is met. While analyzing the result, I came to the conclusion that "indices_open" seems not to have updated in each iteration.
In the following my try so far:
m = GEKKO()
m.options.SOLVER = 1 # APOPT is an MINLP solver
# optional solver settings with APOPT
m.solver_options = ['minlp_maximum_iterations 500', \
# minlp iterations with integer solution
'minlp_max_iter_with_int_sol 10', \
# treat minlp as nlp
'minlp_as_nlp 0', \
# nlp sub-problem max iterations
'nlp_maximum_iterations 50', \
# 1 = depth first, 2 = breadth first
'minlp_branch_method 1', \
# maximum deviation from whole number
'minlp_integer_tol 0.05', \
# covergence tolerance
'minlp_gap_tol 0.01']
#Declare x
x = m.Array(m.Var,(65),lb=0,ub=1,integer=True)
for i, xi in enumerate(x[0:65]):
xi.value = np.random.choice(np.arange(0, 2), 1, p=[0.4, 0.6])[0]
#constr
m = ineq_constraint_new(x, m)
m = eq_constraint_new(x, m)
#target
m = objective(x,m)
#STArt
start_time = time.time()
#m.solve(disp=False)
m.solve()
print('Objective: ' + x)
print('Objective: ' + str(m.options.objfcnval))
# save x
m.x = [x[j].value[0] for j in range(65)]
def eq_constraint_new(x, m):
mask = np.isin(list_unique, specific_value)
indices_fixed = np.nonzero(mask)[0]
m.Equations([x[j] == 1 for j in indices_fixed])
return m
def ineq_constraint_new(x, m):
indices_open = [j for j in range(65) if x[j].value == 1]
# DOes not work
#indices_open_banks = [m.Intermediate(j) for j in range(65) if x[j].value == 1]
array_perc, _, _,_ = self_defined_f(indices_open, some_value)
#convert to gekko variables
gekko_vec_perc_upper_bound = m.Array(m.Var, (65))
for i, xi in enumerate(gekko_vec_perc_upper_bound[0:65]):
xi.value = some_array[i]
gekko_arr_perc = m.Array(m.Var, (65))
for i, xi in enumerate(gekko_arr_perc[0:65]):
xi.value = arr_perc[i]
diff = gekko_vec_perc_upper_bound - gekko_arr_perc
m.Equations([diff[j] >= 0 for j in range(65)])
return m
def objective(x,m):
indices_open = [j for j in range(65) if x[j].value == 1]
_, arr_2, arr_3, arr_4 = self_defined_f(indices_open,some_value )
# intermediates for objective
res_dist = [None] * self.ds.n_banks
res_wand = [None] * self.ds.n_banks
res_wand_er = [None] * self.ds.n_banks
x_closed = np.array([1]*len(x)) - x
for j in range(self.ds.n_banks):
res_dist[j] = m.Intermediate(arr_2[j] * some_factor )
res_wand[j] = m.Intermediate(arr_3[j] * some_factor)
res_wand_er[j] = m.Intermediate(arr_4[j] * some_factor)
res_sach = some_factor * (some_vector * x_closed)
# Will be added together
m.Minimize(sum(res_dist))
m.Minimize(sum(res_wand))
m.Minimize(sum(res_wand_er))
m.Maximize(sum(res_sach))
return m
There is an undefined function _, arr_2, arr_3, arr_4 = self_defined_f(indices_open,some_value ) that prevents the code from running. From a quick scan of the code, an expression like:
indices_open = [j for j in range(65) if x[j].value == 1]
is not allowed because gekko requires that the equations are all defined before the m.solve() command. A callback to a function in Python is not allowed. Instead, binary variables should be used to turn something On or Off in the optimization problem. This can be an equation such as binary variable b:
b = m.Var(lb=0,ub=1,integer=True)
m.Equation(x*(1-b)<=0)
m.Equation(x*b>=0)
This makes the value of b equal to 0 if x is less than zero and b equal to 1 if x if greater than zero. There is a tutorial on if3() functions in the APMonitor documentation that may also be useful.
I am trying to implement LIP in Gurobi but somehow the constraints related to single edge into the node and single edge out of the node is being violated. The following are the equations (I am not copying the equations exactly interms of the summations limits so its (i,j) 0 - N for now, however the constraint should not be violated regardless )
So the bottom equation simply states that there should be one edge coming in and leaving the vertex or node. However in the following code I added this constraint but somehow it is getting violated in the result.
I have quite exhausted my head trying to figure out what might be the issue
import gurobipy as grb
import math
n = 4
set_I = range(0, n)
set_J = range(0, n)
Distance = 50000000
def distance(points, i, j):
dx = points[i][0] - points[j][0]
dy = points[i][1] - points[j][1]
return math.sqrt(dx*dx + dy*dy)
random.seed(1)
points = []
for i in range(n):
points.append((random.randint(0,100),random.randint(0,100)))
opt_model = grb.Model(name="MILP Model")
x_vars = {}
for i in range(n):
for j in range(n):
x_vars[i,j] = opt_model.addVar(vtype=grb.GRB.BINARY,
name='e'+str(i)+'_'+str(j))
# <= Constraint (Distance)
for i in range(n):
opt_model.addConstr(grb.quicksum(x_vars[i,j]*distance(points, i, j) for j in range(n)) <= Distance)
x_vars[i,i].ub = 0
# <= Constraint (coming in to node and going out should be 1 each)
for i in range(n):
opt_model.addConstr(grb.quicksum(x_vars[i,j] for j in range(n)) <= 1)
opt_model.update()
# <= objective is to maximize
objective = grb.quicksum(x_vars[i,j]
for i in set_I
for j in set_J)
opt_model.ModelSense = grb.GRB.MAXIMIZE
opt_model.setObjective(objective)
opt_model.update()
opt_model.optimize()
solution = opt_model.getAttr('x', x_vars )
print solution
import pandas as pd
opt_df = pd.DataFrame.from_dict(x_vars, orient="index",
columns = ["variable_object"])
opt_df.index = pd.MultiIndex.from_tuples(opt_df.index,
names=["column_i", "column_j"])
opt_df.reset_index(inplace=True)
# Gurobi
opt_df["solution_value"] = opt_df["variable_object"].apply(lambda item: item.X)
print opt_df
It seems like you did not add the constraint of equal
according to your code, it should be something like
for k in range(1, n-1):
opt_model.addConstr(grb.quicksum(x_vars[i,k] for i in range(n-1))
== grb.quicksum(x_vars[k,j] for j in range(1, n)))
and actually, your objective function should be like the following code according to your equation
objective = grb.quicksum(x_vars[i,j]
for i in range(1, n-1)
for j in range(1, n)
I am tring to implement a facility location optimization model in Gurobi (Python interface). I have some difficulties to translate the model. The mathmatical model is shown below:
where dloc,floc are the (x,y) coordinates of the demand (customer) and facility (warehouse) locations. The dloc quantities are constants (i.e.50), opposed to floc which are decision variables: these are calculated by the solver. also, x,y coordinates are float numbers between 0 and 100.
One of the key issue is I dont know how to add the facility variable, the number of which can be any between 0 and n.
my codes so far:
from gurobipy import *
import numpy as np
import math
def distance(a, b):
dx = a[0] - b[0]
dy = a[1] - b[1]
return math.sqrt(dx ** 2 + dy ** 2)
customer = np.random.uniform(0,100,[50,2])
print(customer)
m = Model()
n = m.addVar(lb=0.0, ub=GRB.INFINITY,vtype=GRB.INTEGER) #number of candidate facilities
facility={}
for j in range(n):
facility[j] = m.addVar(vtype=GRB.BINARY, name="facility%d" % j) #certainly this is not correct, as an error is reported as 'Var' object cannot be interpreted as an integer
floc = ?
So I have tried another way by manually set a fixed number of candidate facility as an interim workaround:
from gurobipy import *
import numpy as np
import math
customer = np.random.uniform(0,100,[50,2])
print(customer)
m = Model()
###Variable
dc={}
x={}
y={}
assign={}
for j in range(10):
dc[j] = m.addVar(lb=0,ub=1,vtype=GRB.BINARY, name="DC%d" % j)
x[j]= m.addVar(lb=0, ub=100, vtype=GRB.CONTINUOUS, name="x%d")
y[j] = m.addVar(lb=0, ub=100, vtype=GRB.CONTINUOUS, name="y%d")
for i in range(len(customer)):
for j in range(len(dc)):
assign[(i,j)] = m.addVar(lb=0,ub=1,vtype=GRB.BINARY, name="Cu%d from DC%d" % (i,j))
###Constraint
for i in range(len(customer)):
for j in range(len(dc)):
m.addConstr(((customer[i][0] - x[j])*(customer[i][0] - x[j]) +\
(customer[i][1] - y[j])*(customer[i][1] - y[j])) <= 40*40 + 100*100*(1-assign[(i,j)]))
for i in range(len(customer)):
m.addConstr(quicksum(assign[(i,j)] for j in range(len(dc))) == 1)
for i in range(len(customer)):
for j in range(len(dc)):
m.addConstr(assign[(i, j)] <= dc[j])
n=0
for j in dc:
n=n+dc[j]
m.setObjective(n,GRB.MINIMIZE)
m.optimize()
print('\nOptimal Solution is: %g' % m.objVal)
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
Anyone could demonstrate the translation of the model in Gurobi would be great help.
I see no problem in your definition for n. Nonetheless I rewrote your code to make it less verbose and easier to understand. First we create the given sets and constants:
from gurobipy import Model, GRB, quicksum
import numpy as np
m = Model()
demo_coords = np.random.uniform(0, 100, size=(50, 2)) # Just for demonstration
# Sets and Constants
demand = [f"i{k}" for k in range(1, 51)]
facilities = [ f"facility{k}" for k in range(1, 11) ]
dloc = {fac : demo_coords[i] for i, fac in enumerate(demand)}
maxdist = 40
M = 10e6
Note that dloc is a dictionary such that dloc[i] will give you the coordinates
for demand point i. Then dloc[i][0] is the x-coordinate and dloc[i][1] the
y-coordinate.
Now we can create the variables and store them in a gurobi tubledict:
# Variables
floc = m.addVars(facilities, 2, name="floc")
isopen = m.addVars(facilities, vtype=GRB.BINARY, name="isopen")
assign = m.addVars(demand, facilities, vtype=GRB.BINARY, name="assign")
n = m.addVar(vtype=GRB.INTEGER, name="n")
m.update()
Using m.addConstrs(), the constraints can be written as
# Constraints
m.addConstrs(((dloc[i][0] - floc[j, 0]) * (dloc[i][0] - floc[j, 0]) \
+ (dloc[i][1] - floc[j, 1])*(dloc[i][1] - floc[j, 1]) \
<= maxdist**2 + M * (1 - assign[i, j]) \
for i in demand for j in facilities), name="distance")
m.addConstrs((quicksum(assign[i, j] for j in facilities) == 1\
for i in demand), name="assignDemand")
m.addConstrs((assign[i, j] <= isopen[j] for i in demand for j in facilities),\
name="closed")
m.addConstr(n == quicksum(isopen[j] for j in facilities), name="numFacilities")
# zip is needed to iterate over all pairs of consecutive facilites
m.addConstrs((isopen[j] >= isopen[jp1] \
for j, jp1 in zip(facilities, facilities[1:])), name="order")
Note that while it isn't a problem to write floc[j, 0] in the constraint for the distance, you can't write dloc[i, 0] since dloc is a python dictionary and floc is a tupledict.
Setting the objective function und calling m.optimize()
# Objective
m.setObjective(n, sense=GRB.MINIMIZE)
m.optimize()
if m.status == GRB.OPTIMAL:
print(f"Optimal Solution is: {m.objVal}")
print("--------------")
for var in m.getVars():
print(var.varName, var.X)
gives me the optimal solution n = 3.
I am trying to solve the following problem via a Finite Difference Approximation in Python using NumPy:
$u_t = k \, u_{xx}$, on $0 < x < L$ and $t > 0$;
$u(0,t) = u(L,t) = 0$;
$u(x,0) = f(x)$.
I take $u(x,0) = f(x) = x^2$ for my problem.
Programming is not my forte so I need help with the implementation of my code. Here is my code (I'm sorry it is a bit messy, but not too bad I hope):
## This program is to implement a Finite Difference method approximation
## to solve the Heat Equation, u_t = k * u_xx,
## in 1D w/out sources & on a finite interval 0 < x < L. The PDE
## is subject to B.C: u(0,t) = u(L,t) = 0,
## and the I.C: u(x,0) = f(x).
import numpy as np
import matplotlib.pyplot as plt
# definition of initial condition function
def f(x):
return x^2
# parameters
L = 1
T = 10
N = 10
M = 100
s = 0.25
# uniform mesh
x_init = 0
x_end = L
dx = float(x_end - x_init) / N
#x = np.zeros(N+1)
x = np.arange(x_init, x_end, dx)
x[0] = x_init
# time discretization
t_init = 0
t_end = T
dt = float(t_end - t_init) / M
#t = np.zeros(M+1)
t = np.arange(t_init, t_end, dt)
t[0] = t_init
# Boundary Conditions
for m in xrange(0, M):
t[m] = m * dt
# Initial Conditions
for j in xrange(0, N):
x[j] = j * dx
# definition of solution to u_t = k * u_xx
u = np.zeros((N+1, M+1)) # NxM array to store values of the solution
# finite difference scheme
for j in xrange(0, N-1):
u[j][0] = x**2 #initial condition
for m in xrange(0, M):
for j in xrange(1, N-1):
if j == 1:
u[j-1][m] = 0 # Boundary condition
else:
u[j][m+1] = u[j][m] + s * ( u[j+1][m] - #FDM scheme
2 * u[j][m] + u[j-1][m] )
else:
if j == N-1:
u[j+1][m] = 0 # Boundary Condition
print u, t, x
#plt.plot(t, u)
#plt.show()
So the first issue I am having is I am trying to create an array/matrix to store values for the solution. I wanted it to be an NxM matrix, but in my code I made the matrix (N+1)x(M+1) because I kept getting an error that the index was going out of bounds. Anyways how can I make such a matrix using numpy.array so as not to needlessly take up memory by creating a (N+1)x(M+1) matrix filled with zeros?
Second, how can I "access" such an array? The real solution u(x,t) is approximated by u(x[j], t[m]) were j is the jth spatial value, and m is the mth time value. The finite difference scheme is given by:
u(x[j],t[m+1]) = u(x[j],t[m]) + s * ( u(x[j+1],t[m]) - 2 * u(x[j],t[m]) + u(x[j-1],t[m]) )
(See here for the formulation)
I want to be able to implement the Initial Condition u(x[j],t[0]) = x**2 for all values of j = 0,...,N-1. I also need to implement Boundary Conditions u(x[0],t[m]) = 0 = u(x[N],t[m]) for all values of t = 0,...,M. Is the nested loop I created the best way to do this? Originally I tried implementing the I.C. and B.C. under two different for loops which I used to calculate values of the matrices x and t (in my code I still have comments placed where I tried to do this)
I think I am just not using the right notation but I cannot find anywhere in the documentation for NumPy how to "call" such an array so at to iterate through each value in the proposed scheme. Can anyone shed some light on what I am doing wrong?
Any help is very greatly appreciated. This is not homework but rather to understand how to program FDM for Heat Equation because later I will use similar methods to solve the Black-Scholes PDE.
EDIT: So when I run my code on line 60 (the last "else" that I use) I get an error that says invalid syntax, and on line 51 (u[j][0] = x**2 #initial condition) I get an error that reads "setting an array element with a sequence." What does that mean?