import random
import math
import cvxpy as cp
import numpy as np
U = 10 # int(input("Enter number of users : "))
K = 600 # int(input("Enter number of content in database : "))
# ncache = int(input("Enter number of cache : "))
# cache_size=int(input("Enter cache size : "))
sigma = 1 # size of content currently assumed to be 1 gb for all content
lamda = 0.12 # currncy unit per gb requested content currently taken as 10/gb
Nu = 10 # No. of content recommended to the user
dis = 0.05
R_ui = np.empty((U, K), dtype=float) # R_ui is revenue per content
# Probability of user following the recommendations
alpha_u = np.random.uniform(0.6, 1, U)
y0 = np.random.randint(2, size=(U, K)) # Input Variable
# MOVING TO FORMULA 1
U0_cp = 0 # U0_cp is initial utility of Content Provider
for i in range(U):
for j in range(K):
U0_cp = U0_cp + (alpha_u[i]*y0[i][j]*(R_ui[i][j]-sigma*lamda)/Nu)
# MOVING TO FORMULA 2
C_j = 10 # Number Of Caches
X = np.empty((K, C_j), dtype=float)
for i in range(K):
for j in range(C_j-1):
X[i][j] = random.randint(0, 1)
X[i][9] = 1
# X=np.random.randint(2,size=(K,C_j)) # Cache Input Variable
C_u = 3 # Subset of caches user has access to
C_cost = np.random.uniform(0, 0.002, C_j)
C_cost[9] = 0.05 # Cost of retrieval from a cache
C_cost.round(4)
C_cm = np.empty((U, C_u), dtype=float) # User cache mapping
flag = True
temp = 0
for i in range(U):
for j in range(0, C_u-1):
flag = True
while flag:
temp = np.random.choice(C_cost)
if temp in C_cm[i]:
continue
else:
C_cm[i][j] = temp
flag = False
C_cm[i][2] = 0.05
C_cm[i].sort()
C_cm.round(4)
K_ui = np.empty((U, K), dtype=float) # Cost retrieval matrix
temp = 0
temp1 = 1
temp2 = 0
for i in range(U):
for j in range(K):
for k in range(C_u):
temp2 = sigma * C_cm[i][k] * X[j][list(C_cost).index(C_cm[i][k])]
for l in range(0, list(C_cost).index(C_cm[i][k])-1):
temp1 = temp1*(1-X[j][l])
temp = temp+temp2*temp1
K_ui[i][j] = temp
U0_cdn = 0 # Initial Utility of CDN
for i in range(U):
for j in range(K):
U0_cdn = U0_cdn + (alpha_u[i] * y0[i][j]*(sigma*lamda-K_ui[i][j])/Nu)
# Formula 3
y_ui = cp.Variable()
Y = np.empty((U, K), dtype=float)
for i in range(U):
for j in range(K):
U0_cp = (alpha_u[i]*y0[i][j]*(R_ui[i][j]-sigma*lamda)/Nu)
U0_cdn = (alpha_u[i]*y0[i][j]*(sigma*lamda-K_ui[i][j])/Nu)
formula = cp.Maximize(cp.log((alpha_u[i]*y_ui*(R_ui[i][j]-sigma*lamda*(1+dis*(y0[i][j-1])))/Nu)-(alpha_u[i]*y0[i][j]*(R_ui[i][j]-sigma*lamda)/Nu))+cp.log(
(alpha_u[i]*y_ui*(sigma*lamda*(1+dis*(y0[i][j]-1))-K_ui[i][j])/Nu)-(alpha_u[i]*y0[i][j]*(sigma*lamda-K_ui[i][j])/Nu)))
constraint = [(alpha_u[i]*y_ui*(R_ui[i][j]-sigma*lamda*(1+dis*(y0[i][j-1])))/Nu)-(alpha_u[i]*y0[i][j]*(R_ui[i][j]-sigma*lamda)/Nu) >= 0,
(alpha_u[i]*y_ui*(sigma*lamda*(1+dis*(y0[i][j]-1))-K_ui[i][j])/Nu)-(alpha_u[i]*y0[i][j]*(sigma*lamda-K_ui[i][j])/Nu) >= 0, y_ui <= 1,y_ui >= 0]
objective = cp.Problem(formula, constraint)
Y[i][j] = objective.solve(verbose=True)
print(Y)
Stuck trying to implement the following collaboration problem using CVXPY. kindly share the possible implementation approach.
I was trying to maximize the matrix Y in the collaboration problem but I do not know how to maximize a matrix using CVXPY so I implemented CVXPY on each value of the matrix but it is not working.
I need help for this code in finding the nearest neighbor distance between point locations. I think the problem is that instead of adding 'nearestdistance' to 'Sumdistance' only if it is smaller than the previous 'newdistance' it adds the nearest distance no matter what. Though I might be wrong. Code shown below:
q = loadPoints(f)
n = 1416
Sumdistance = 0
for i in q:
iID = int(i.getID())
x1 = float(i.getX())
y1 = float(i.getY())
nearestdistance = 999999999999999999999999999
for j in range(0, n):
if j != iID:
jID = (q[j].getID())
x2 = float(q[j].getX())
y2 = float(q[j].getY())
dx = x1 - x2
dy = y1 - y2
newdistance = math.sqrt(math.pow(dx,2) + (math.pow(dy,2)))
if newdistance < nearestdistance:
nearestdistance = newdistance
else nearestdistance =
Sumdistance = Sumdistance + nearestdistance
area = 10000000000
Do = Sumdistance/n
De = 0.5/(math.sqrt(n/area))
ANN = Do/De
print(ANN)
Thank you!
You are correct - you should always add nearestdistance to Sumdistance after iterating the neighbors.
for i in q:
......
for j in range(0, n):
if j != iID:
........
if newdistance < nearestdistance:
nearestdistance = newdistance
Sumdistance += nearestdistance
Note that the final sum will include reverse neighbors, ie a -> b and b -> a
import numpy as np;
import math;
import random;
from scipy.optimize import minimize;
def matrixmult (A, B):
rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])
Z = [[0 for row in range(rows_B)] for col in range(cols_A)]
for i in range(cols_A):
for j in range(rows_A):
#for k in range(cols_A):
Z[i][j] += A[i][j] * B[i][j]
return Z
def constraint1(x):
A=x
rows_X = cols_X = len(x)
ad = np.ones((len(x),1)) #makes a 7x1 array of ones
ad1 = x.sum(axis=1) # makes 7x1 array, each element is sum of each rows
ad2 = np.matrix(ad1)
for i in range(len(x)):
ad[i] = ad[i] - ad2[i] # sum of each row in a binary matrix must be 1 to indicate there is only one entrance or exit for each node
#for j in range(cols_X):
#ad = ad - ad1[i]
return ad
def constraint2(x):
rows_X = cols_X = len(x)
ad3 = np.ones((1,len(x)))
ad4 = x.sum(axis=0)
ad5 = np.matrix(ad4)
for i in range(len(x)):
ad3[i] = ad3[i] - ad5[i]
#for j in range(cols_X):
#ad = ad - ad1[i]
return ad3
def total(C):
C = np.array([[np.nan,3,5,np.nan,np.nan,np.nan,3],[3,np.nan,3,7,np.nan,np.nan,11],[5,3,np.nan,3,np.nan,np.nan,np.nan],[np.nan,7,3,np.nan,3,9,11],[np.nan,np.nan,np.nan,3,np.nan,3,np.nan],[np.nan,np.nan,np.nan,9,3,np.nan,3],[3,11,np.nan,11,np.nan,3,np.nan]])
X = [[0 for row in range(len(C))] for col in range(len(C[0]))]
for i in range(len(C[0])):
for j in range(len(C)):
if math.isnan(C[i][j]) == False :
X[i][j] += random.randint(0,1)
else :
X[i][j]==np.nan
CX = matrixmult (C, X)
cx = np.array(CX)
x = np.matrix(X)
print(x.sum(axis=1))
print(x.sum(axis=0))
print(x)
print(cx)
tot = 0
for i in range(len(cx[0])):
for j in range(len(cx)):
if math.isnan(cx[i][j]) == False :
#print (i,j)
tot += cx[i][j]
#for i in range(len(cx[0])):
#for j in range(len(cx)):
#if math.isnan(cx[i][j]) == False :
#print (i,j)
return tot
C = np.array([[np.nan,3,5,np.nan,np.nan,np.nan,3],[3,np.nan,3,7,np.nan,np.nan,11],[5,3,np.nan,3,np.nan,np.nan,np.nan],[np.nan,7,3,np.nan,3,9,11],[np.nan,np.nan,np.nan,3,np.nan,3,np.nan],[np.nan,np.nan,np.nan,9,3,np.nan,3],[3,11,np.nan,11,np.nan,3,np.nan]])
con1 = {'type' : 'eq', 'fun' : constraint1}
con2 = {'type' : 'eq', 'fun' : constraint2}
cons = [con1,con2]
path = minimize(total, 12,method='SLSQP', jac=None, bounds=None, tol=None, callback=None, constraints = cons)
print(path)
I need to implement traveling salesman problem with linear programming. My intention to use python optimization tools. Its my first program in python and optimization programs.
Since there are two constraints forces traveling salesman to visit(enter and leave) every node once, I wanted to create binary selection 'x' matrix with the same dimensions of cost matrix. Since there is one entrance every column of the selection matrix will sum to 1 and the same for each exit.
I have problems with the usage of scipy.optimize.minimize method. I am not able to send selection matrix to the constraint functions. I will appreciate if anybody helps, thanks in advance.. (sub-tour elimination constraints are not implemented yet)
from cvxpy import *
import numpy as np
import math;
import random;
n = 7
#X = Bool(n , n)
#Y = Bool(n , 1)
#C = np.random.randint(1,5,(n,n))
C = np.array([[np.nan,3,5,np.nan,np.nan,np.nan,3],[3,np.nan,3,7,np.nan,np.nan,11],[5,3,np.nan,3,np.nan,np.nan,np.nan],[np.nan,7,3,np.nan,3,9,11],[np.nan,np.nan,np.nan,3,np.nan,3,np.nan],[np.nan,np.nan,np.nan,9,3,np.nan,3],[3,11,np.nan,11,np.nan,3,np.nan]])
#X = [[0 for row in range(len(C))] for col in range(len(C[0]))]
X = np.zeros((n,n))
for i in range(n):
for j in range(n):
if math.isnan(C[i][j]) == False :
X[i][j] += random.randint(0,1)
else :
X[i][j]== np.nan
#x = np.array(X, dtype = np.float64)
P = C*X
nodes = []
tot = 0
for i in range(n):
for j in range(n):
if math.isnan(P[i][j]) == False :
tot += P[i][j]
if(P[i][j] >0):
print (i,j)
nodes.append((i,j))
print(nodes)
print(len(nodes))
objective = Minimize(tot)
constraints = []
constraints.append( sum_entries( X, axis=0 ) == 1 )
constraints.append( sum_entries( X, axis=1 ) == 1 )
#constraints.append( sum_entries(Y) == C )
prob = Problem(objective, constraints)
prob.solve(solver=GLPK_MI)
print (prob.value)
print(tot)
print(C)
print(X)
print(P)
#print(objective)
Now i have an edited optimization code using cvxpy packet. But it could not minimize the objective. I could not find more examples on cvxpy MILP examples. If you have any suggestion this will be nice. thanks
I am working on a program that can convolve two lists and I need to do it from scratch. I have the correct output from my for loop, however I can't return it because the appended output will only save the last iteration of the for loop. I've tried enumerate, append, extend, and several others, I just don't know python enough to get a list or ideally an array of ints from my for loop.
import numpy as np
#def conv395(x,h):
#Function to convolve x input signal and h impulse response
h = [1,2,-1,1]
x = [1,1,2,1,2,2,1,1]
M = len(h)-1
L = len(x)
Ly = M+L
def ConV(x,h,n):
y = (h[m]*x[(n-1)-m])
return (y)
for n in range(0,Ly+1):
mx = n-L+1
if mx < 1:
n = n+1
#print("SPACE")
Y1 = 0
if n <= M+1:
Y = []
for m in range(n):
#print(n)
y = ConV(x,h,n)
Y1 = y + Y1
Y.append(Y1)
print Y,
OUTPUT
[1] [3] [3] [5]
HOWEVER
Y type=list size=1 Value=[5]
I need the Value to match the output in a list or array.
Any help would be greatly appreciated,thanks!
something like this maybe?
h = [1,2,-1,1]
x = [1,1,2,1,2,2,1,1]
M = len(h)-1
L = len(x)
Ly = M+L
LOL = []
def ConV(x,h,n):
y = (h[m]*x[(n-1)-m])
return (y)
for n in range(0,Ly+1):
mx = n-L+1
if mx < 1:
n = n+1
#print("SPACE")
Y1 = 0
if n <= M+1:
Y = []
for m in range(n):
#print(n)
y = ConV(x,h,n)
Y1 = y + Y1
Y.append(Y1)
LOL.append(Y)
print Y,
print LOL
I want to find pattern in some spectra.
Spectrum image
Pattern should look like 2 in gray circles on picture, all data looks similarly. Light blue line is the original data, dotted dark blue line - average over 6 points. I was trying to do window with some size and scan data and check whether the y-flux value drops/rise below 60 ish % but that seems to find other regions and the one that I want, or only this I don't want.
The width of pattern is not always the same in spectra that I have. There is a picture of spectrum with pattern black dashed line but my program didn't found it.
not found picture
I tried changing size of window but it doesn't help. Can I use some pattern recognition algorithm to find this patterns? Could somebody point me in some direction? Or explain in easy way since I'm kinda lost in this, please?
That's my code:
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import ascii
import glob
def reading(file_name):
data = ascii.read(file_name)
lam = data['col0'][1:-1]
#data offset *10**17 + 5
flux = data['col1'][1:-1]*10**17 + 5
return lam, flux
def percentChange(startPoint,currentPoint):
return abs(((currentPoint-startPoint)/startPoint))*100.00
def window(data, size):
n = len(data)
out = []
wind = data[0 : size]
i = size
while i + size/2 < n:
wind = data[i - size/2 : i + size/2]
tmp = percentChange(wind[0], wind[-1])
if tmp > 50.:
out.append([tmp, i - size/2, i + size/2])
i = i + size
return out
def window2(data, size):
n = len(data)
out = []
wind = data[0 : size]
i = size
while i + size/2 < n:
wind = data[i - size/2 : i + size/2]
tmp = percentChange(wind[0], wind[len(wind)/2])
if tmp > 50.:
out.append([tmp, i - size/2, i + size/2])
i = i + size
return out
def plotting(lamb, flux):
plt.rcParams['font.family'] = 'freeserif'
plt.rcParams['font.size'] = 12
plt.rcParams['axes.labelsize'] = 15
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
plt.rcParams['legend.fontsize'] = 12
plt.rcParams['figure.titlesize'] = 12
plt.rcParams['xtick.minor.visible'] = True
plt.rcParams['ytick.minor.visible'] = True
plt.plot(lamb, flux)
plt.xlabel("wavelenght [A]")
plt.ylabel("flux [erg/cm^2/s/A]")
def averaging(lamb, flux, param):
end = 1480
bin_flux_1 = [np.mean(flux[i : i + param]) for i in range(0, end, param)]
bin_lam_1 = [np.mean(lamb[i : i + param]) for i in range(0, end, param)]
return bin_lam_1, bin_flux_1
def main():
param = 6
stack = 6
for name in glob.glob('TRAIN/*.dat'):
print name
lamb, flux = reading(name)
lamb_a, flux_a = averaging(lamb, flux, param)
plotting(lamb, flux)
plotting(lamb_a, flux_a)
change = window(flux_a, stack)
change2 = window2(flux_a, stack)
minim = flux_a.index(min(flux_a))
for i in range(len(change)):
plt.axvline(lamb_a[change[i][1]], color='r', linestyle='--',linewidth=1)
plt.axvline(lamb_a[change[i][2]], color='r', linestyle='--',linewidth=1)
for i in range(len(change2)):
plt.axvline(lamb_a[change2[i][1]], color='y', linestyle='-',linewidth=1)
plt.axvline(lamb_a[change2[i][2]], color='y', linestyle='-',linewidth=1)
plt.axvline(lamb_a[minim], color='k', linestyle='--',linewidth=1)
plt.show()
if __name__ == "__main__":
main()
You can do it by using Knuth–Morris–Pratt algorithm in linear O(n + m) time complexity where n and m are the lengths of text and pattern.
KMP algorithm is basically a pattern matching algorithm (finding the starting position of a needle in haystack) which works on character string.
def kmp_matcher(t, d):
n=len(t)
m=len(d)
pi = compute_prefix_function(d)
q = 0
i = 0
while i < n:
if d[q]==t[i]:
q=q+1
i = i + 1
else:
if q != 0:
q = pi[q-1]
else:
i = i + 1
if q == m:
print "pattern occurs with shift "+str(i-q)
q = pi[q-1]
def compute_prefix_function(p):
m=len(p)
pi =range(m)
k=1
l = 0
while k < m:
if p[k] <= p[l]:
l = l + 1
pi[k] = l
k = k + 1
else:
if l != 0:
l = pi[l-1]
else:
pi[k] = 0
k = k + 1
return pi
t = 'brownfoxlazydog'
p = 'lazy'
kmp_matcher(t, p)