Related
How can I include a job precedence vector within this code?
In the Prec list, I set which job should go before another from the indexes, but how can I do the evaluation of this condition so that I can rearrange the allocation of jobs?
import numpy as np
M = 5
Pj = np.array([99,184,80,180,51,69,129,152,168,171])
Prec = [[9,2],[2,8],[4,2]]
J = np.array(range(len(Pj)))
#Organización de los índices de trabajos por duración
Pj_SI = np.argsort(Pj)
Pj_SA = Pj[Pj_SI]
J_Order = J[Pj_SI]
# print(Pj_SI)
# print(Pj_SA)
print(np.argsort(J_Order))
#SPT HEURISTIC
M_j = {}
M_pj = {}
M_Cj = {}
for m in range(M):
M_j[m] = []
M_pj[m] = []
M_Cj[m] = []
for i, Pj in enumerate(Pj_SA):
M_pj[(i)%M].append(Pj)
M_j[(i)%M].append(J_Order[i])
if i<M:
M_Cj[(i)%M].append(Pj)
else:
M_Cj[(i)%M].append(M_Cj[(i)%M][len(M_Cj[(i)%M])-1]+Pj)
print("Processing Time in Machines", M_pj)
print("Assignment Order", M_j)
Cmax = []
for m in range(len(M_Cj)):
Cmax.append(np.sum(M_pj[m]))
Makespan = max(Cmax)
print('Machines Load: ', Cmax)
print('Makespan SPT:', Makespan)
I wrote a MPC with Python and it worked before. After a long time I want to use it again but I got this Error
f0 passed has more than 1 dimension.
But I didn't change anything on my code. It is some kind of strange.
Here is my code:
import numpy as np
import numpy.linalg as npl
import matplotlib.pyplot as plt
from scipy.optimize import minimize
def mpcAugment(Am, Bm, Cm ):
"Function for Augmented Model"
nx, nu = Bm.shape
ny = Cm.shape[0]
A = np.zeros((nx+ny,nx+ny))
A[0:nx,0:nx] = Am
A[nx:nx+ny,0:nx] = Cm#Am
A[nx:nx+ny,nx:nx+ny] = np.eye(ny)
B = np.zeros((nx+ny,nu))
B[0:nx,:nu] = Bm
B[nx:nx+ny,:nu] = Cm#Bm
C = np.zeros((ny,nx+ny))
C[:ny,nx:nx+ny] = np.eye(ny)
return A, B, C
'Define Parameters'
k = 0.4
AICB = 153.8
mcp = 8.8e4
vamb1 = 30
vamb2 = 45
a = -k*AICB/mcp
b = -1/mcp
Ts = 20
VICBref = -5.0
Am = np.array([[1+Ts*a]])
Bm = np.array([[Ts*b]])
Gm = np.array([[-Ts*a]])
Cm = np.array([[1]])
A, B, C = mpcAugment(Am,Bm,Cm)
A, G, C = mpcAugment(Am,Gm,Cm)
nx, nu = B.shape
ny = C.shape[0]
nd = G.shape[1]
Np = 20
Nu = 5
F = np.zeros((Np*ny,nx))
PHI = np.zeros((Np*ny,Nu*nu))
PHIw = np.zeros((Np*ny,Np*nd))
for i in range(0,Np):
Ai = npl.matrix_power(A, i+1)
F[i*ny:(i+1)*ny,:] = C#Ai
for j in range(0, Nu):
if j <= i:
Aij = np.linalg.matrix_power(A, i-j)
PHI[i*ny:(i+1)*ny, j*nu:(j+1)*nu] = C#Aij#B
for j in range(0, Np):
if j <= i:
Aij = np.linalg.matrix_power(A, i-j)
PHIw[i*ny:(i+1)*ny, j*nd:(j+1)*nd] = C#Aij#G
umax = 3100
umin = 0
Q = np.eye(Np*ny)
R = 1e-2*np.eye(Nu*nu)
Rs = VICBref*np.ones((Np*ny,1))
Ainq = np.zeros((2*Nu*nu,Nu*nu))
binq = np.zeros((2*Nu*nu,1))
cinq = np.zeros((2*Nu*nu,1))
for i in range(0,Nu):
binq[i*nu:(i+1)*nu] = umax
binq[(i+Nu)*nu:(Nu+i+1)*nu] = 1
cinq[i*nu:(i+1)*nu] = 1
cinq[(i+Nu)*nu:(Nu+i+1)*nu] = -1
for j in range(0,i+1):
Ainq[i*nu:(i+1)*nu,j*nu:(j+1)*nu] = np.eye(nu)
Ainq[(i+Nu)*nu:(Nu+i+1)*nu,j*nu:(j+1)*nu] = np.eye(nu)
u0 = 0
def objective(du):
dU = np.array(du).reshape((len(du),1))
Y = F#x + PHI#dU + PHIw#w
return np.transpose((Rs-Y))#(Rs-Y)+np.transpose(dU)#R#(dU)
def constraint1(du):
dU = np.array(du).reshape((len(du),1))
return (binq - Ainq#dU - cinq*u0)[0]
#print(objective([1,1,1]))
ulim = (umin, umax)
bnds = np.kron(np.ones((Nu,1)),ulim)
#print(bnds)
Um = np.ones((nu*Nu,1))
Tsim = 5e4
time = np.arange(0,Tsim,Ts)
Nt = len(time)
xm = np.zeros((Nt,1))
um = np.zeros((Nt,nu))
ym = np.zeros((Nt,ny))
xm[0] = 0
ym[0] = Cm.dot(xm[0])
w = np.zeros((Np*nd,1))
print('Am = ',Am)
print('Bm = ',Bm)
print('Cm = ',Cm)
x = np.zeros((nx,1))
x[1] = xm[0]
vamb = vamb1
Vamb = np.zeros((Nt,1))
Ns = int(np.floor(Nt/2))
Vamb[0:Ns] = vamb1*np.ones((Ns,1))
Vamb[Ns:Nt] = vamb2*np.ones((Nt-Ns,1))
Vref = VICBref*np.ones((Nt,1))
con = {'type':'ineq','fun':constraint1}
for i in range(0,Nt-1):
sol = minimize(objective, Um, method = 'SLSQP',constraints = con)
if sol.success == False:
print('Error Cant solve problem')
exit()
Um = sol.x
um[i+1] = um[i] + Um[0]
u0 = um[i+1]
xm[i+1] = Am.dot(xm[i])+Bm.dot(um[i+1])+Gm.dot(Vamb[i])
ym[i+1] = Cm.dot(xm[i+1])
for j in range(0,Np):
if i+j < Nt:
Rs[j] = Vref[i+j]
w[j] = Vamb[i+j]-Vamb[i+j-1]
else:
Rs[j] = Vref[Nt-1]
w[j] = 0
x[0] = xm[i+1] - xm[i]
x[1] = xm[i+1]
print('Q = ',um[i+1],' , VICB = ',xm[i+1], ' vamb = ', Vamb[i])
hour = 60*60
plt.figure()
plt.subplot(2,1,1)
plt.plot(time/hour,ym)
plt.plot(time/hour,Vref,'--')
plt.xlabel('time(hours)')
plt.xlim([0, Tsim/hour])
plt.subplot(2,1,2)
plt.plot(time/hour,um)
plt.xlim([0, Tsim/hour])
plt.show()
It about a controller, which control the temperature of a cool box.
Is that possible that anything changed in main simply code?
I think the problem is now in minimizations part.
I reinstalled all of my libraries and it worked
I am doing some Udemy AI courses and came across with one that "teaches" a bidimensional cheetah how to walk. I was doing the exercises on my computer, but it takes too much time. I decided to use Google Cloud to run the code and see the results some hours after. Nevertheless, when I run the code I get the following error " WARN: Tried to pass
invalid video frame, marking as broken: Your frame has data type int64, but we require uint8 (i.e. RGB values from 0-255)".
After the code is executed, I see into the folder and I don't see any videos (just the meta info).
Some more info (if it helps):
I have a 1 CPU (4g), SSD Ubuntu 16.04 LTS
I have not tried anything yet to solve it because I don´t know what to try. Im looking for solutions on the web, but nothing I could try.
This is the code
import os
import numpy as np
import gym
from gym import wrappers
import pybullet_envs
class Hp():
def __init__(self):
self.nb_steps = 1000
self.episode_lenght = 1000
self.learning_rate = 0.02
self.nb_directions = 32
self.nb_best_directions = 32
assert self.nb_best_directions <= self.nb_directions
self.noise = 0.03
self.seed = 1
self.env_name = 'HalfCheetahBulletEnv-v0'
class Normalizer():
def __init__(self, nb_inputs):
self.n = np.zeros(nb_inputs)
self.mean = np.zeros(nb_inputs)
self.mean_diff = np.zeros(nb_inputs)
self.var = np.zeros(nb_inputs)
def observe(self, x):
self.n += 1.
last_mean = self.mean.copy()
self.mean += (x - self.mean) / self.n
#abajo es el online numerator update
self.mean_diff += (x - last_mean) * (x - self.mean)
#abajo online computation de la varianza
self.var = (self.mean_diff / self.n).clip(min = 1e-2)
def normalize(self, inputs):
obs_mean = self.mean
obs_std = np.sqrt(self.var)
return (inputs - obs_mean) / obs_std
class Policy():
def __init__(self, input_size, output_size):
self.theta = np.zeros((output_size, input_size))
def evaluate(self, input, delta = None, direction = None):
if direction is None:
return self.theta.dot(input)
elif direction == 'positive':
return (self.theta + hp.noise * delta).dot(input)
else:
return (self.theta - hp.noise * delta).dot(input)
def sample_deltas(self):
return [np.random.randn(*self.theta.shape) for _ in range(hp.nb_directions)]
def update (self, rollouts, sigma_r):
step = np.zeros(self.theta.shape)
for r_pos, r_neg, d in rollouts:
step += (r_pos - r_neg) * d
self.theta += hp.learning_rate / (hp.nb_best_directions * sigma_r) * step
def explore(env, normalizer, policy, direction = None, delta = None):
state = env.reset()
done = False
num_plays = 0.
#abajo puede ser promedio de las rewards
sum_rewards = 0
while not done and num_plays < hp.episode_lenght:
normalizer.observe(state)
state = normalizer.normalize(state)
action = policy.evaluate(state, delta, direction)
state, reward, done, _ = env.step(action)
reward = max(min(reward, 1), -1)
#abajo sería poner un promedio
sum_rewards += reward
num_plays += 1
return sum_rewards
def train (env, policy, normalizer, hp):
for step in range(hp.nb_steps):
#iniciar las perturbaciones deltas y los rewards positivos/negativos
deltas = policy.sample_deltas()
positive_rewards = [0] * hp.nb_directions
negative_rewards = [0] * hp.nb_directions
#sacar las rewards en la dirección positiva
for k in range(hp.nb_directions):
positive_rewards[k] = explore(env, normalizer, policy, direction = 'positive', delta = deltas[k])
#sacar las rewards en dirección negativo
for k in range(hp.nb_directions):
negative_rewards[k] = explore(env, normalizer, policy, direction = 'negative', delta = deltas[k])
#sacar todas las rewards para sacar la desvest
all_rewards = np.array(positive_rewards + negative_rewards)
sigma_r = all_rewards.std()
#acomodar los rollauts por el max (r_pos, r_neg) y seleccionar la mejor dirección
scores = {k:max(r_pos, r_neg) for k, (r_pos, r_neg) in enumerate(zip(positive_rewards, negative_rewards))}
order = sorted(scores.keys(), key = lambda x:scores[x])[:hp.nb_best_directions]
rollouts = [(positive_rewards[k], negative_rewards[k], deltas[k]) for k in order]
#actualizar policy
policy.update (rollouts, sigma_r)
#poner el final reward del policy luego del update
reward_evaluation = explore (env, normalizer, policy)
print('Paso: ', step, 'Lejania: ', reward_evaluation)
def mkdir(base, name):
path = os.path.join(base, name)
if not os.path.exists(path):
os.makedirs(path)
return path
work_dir = mkdir('exp', 'brs')
monitor_dir = mkdir(work_dir, 'monitor')
hp = Hp()
np.random.seed(hp.seed)
env = gym.make(hp.env_name)
env = wrappers.Monitor(env, monitor_dir, force = True)
nb_inputs = env.observation_space.shape[0]
nb_outputs = env.action_space.shape[0]
policy = Policy(nb_inputs, nb_outputs)
normalizer = Normalizer(nb_inputs)
train(env, policy, normalizer, hp)
In the end, I think it was either a thing of an old version of ffmpeg or another compatibility issue (this is my first try with linux and I could not update ffmpeg properly). I changed my virtual environment from Ubunto 16.04 to Debian. It worked perfectly.
I'm using a double for loop, to calculate a value. This for loop gets the i element of a vector and the i+1 element of the same vector, then it does some calculation. But when the second iteration of the second for loop starts, I get the error 'int' object has no attribute 'triu_indices'
I have three matrixes, and some functions. Also I use a double for (I don't think this is the pythonic way to do that, however I'm learning)
I have this:
import numpy as np
#The three matrixes
flowMatrixSymNoCeros = np.array([[0,4,6,2,4,4],
[4,0,4,2,2,8],
[6,4,0,2,2,6],
[2,2,2,0,6,2],
[4,2,2,6,0,10],
[4,8,6,2,10,0]])
flowMatrixSymCeros = np.array([[0,0,6,2,4,0],
[0,0,4,2,2,8],
[6,4,0,2,2,6],
[2,2,2,0,6,2],
[4,2,2,6,0,0],
[0,8,6,2,0,0]])
closenessRatingSymNoceros = np.array([[0,5,3,2,6,4],
[5,0,5,2,6,2],
[3,5,0,1,2,1],
[2,2,1,0,2,2],
[6,6,2,2,0,6],
[4,2,1,2,6,0]])
matrices = np.array([flowMatrixSymNoCeros,
flowMatrixSymCeros,
closenessRatingSymNoceros])
def normalMatrixesAsym(matrices):
matrixes = np.copy(matrices)
matrixes = np.absolute(matrixes)
normalMatrixes = []
for matriz in matrixes:
s = np.sum(matriz)
normalMatrixes.append(matriz / s)
return np.asarray(normalMatrixes)
def sdwm(symetria, normalMatrix):
SD= 0
normalizedMatrix = np.copy(normalMatrix)
m = normalizedMatrix.shape[0]
sd = lambda num,den : (num/den)**(1/2)
# maskUpper = np.mask_indices(m, np.triu, 1)
# maskLower = np.mask_indices(m, np.tril, -1)
upper = normalizedMatrix[np.triu_indices(m,1)]
lower = normalizedMatrix[np.tril_indices(m,-1)]
upperAbs = np.abs(upper)
if(symetria):
media = np.absolute(np.mean(upper))
num = np.sum((upperAbs - np.mean(media))**2)
den = ((m * (m-1))/2)-1
SD = sd(num,den) #calculo del SD
else:
# lower = np.tril(normalizedMatrix,-1)
matrixNoDiag = np.append(upper,lower)
matrixNoDiagAbs = np.abs(matrixNoDiag)
mean = np.absolute(np.mean(matrixNoDiag))
num = np.sum((matrixNoDiagAbs - mean)**2)
den = (m*(m-1))-1 #calcula el denominador
SD = sd(num,den) #calcula el SD
return SD
def calcularCriticalValues(funcion, symetria, normalMatrixes):
normalMatrices = np.copy(normalMatrixes)
criticalValules = []
for normalMatriz in normalMatrices:
criticalValules.append(funcion(symetria,normalMatriz))
return np.asarray(criticalValules)
normalizedMatrices = normalMatrixesAsym(matrices)
SD = calcularCriticalValues(nm.sdwm,False,normalizedMatrices)
m=len(matrices)
R= np.zeros((m,m))
n = len(normalizedMatrices[0])
for i,matrix in enumerate(normalizedMatrices):
upperI = matrix[np.triu_indices(n,1)]
lowerI = matrix[np.tril_indices(n,-1)]
matrixNoDiagI = np.append(upperI,lowerI)
meanI = np.absolute(np.mean(matrixNoDiagI))
matrixNoDiagIAbs = np.abs(matrixNoDiagI)
for j in range(i+1,m):
matrixJ =normalizedMatrices[j]
upperJ = matrixJ[np.triu_indices(n,1)] #the problem is here
lowerJ = matrixJ[np.tril_indices(n,-1)]
matrixNoDiagJ = np.append(upperJ,lowerJ)
meanJ = np.absolute(np.mean(matrixNoDiagJ))
matrixNoDiagJAbs = np.abs(matrixNoDiagJ)
num = np.sum((matrixNoDiagIAbs - meanI)*(matrixNoDiagJAbs -
meanJ))
np = (n*(n-1))-1 #n''
den = np*SD[i]*SD[j]
r = num/den
R[i][j] = r
print(R)
What I expect is a matrix named R, with the calculations that the algorithm do.
This is
>>>R
>>>[[0. 0.456510 0.987845]
[0. 0.156457 0.987845]
[0. 0. 0. ]]
The error I get is:
AttributeError Traceback (most recent call last)
in ()
204 # print(i,j)
205 matrixJ =normalizedMatrices[j]
--> 206 upperJ = matrixJ[np.triu_indices(n,1)] # Obtiene elementos diagonal superior
207 lowerJ = matrixJ[np.tril_indices(n,-1)] # Obtiene elementos diagonal superior
208 matrixNoDiagJ = np.append(upperJ,lowerJ)
AttributeError: 'int' object has no attribute 'triu_indices'
The problem is that you use np as a variable in that loop: np = (n*(n-1))-1 #n''. You are assigning it to an int value, which is shadowing the imported np. You need to rename that variable.
This program works for the value of the variable "n" set to 4, as it's the case in the following code:
from __future__ import division
from numpy import zeros
import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import linalg
import math
def getA(kappa):
matrix = zeros((n, n), float)
for i in range(n):
for j in range(n):
matrix[i][j] = 2*math.cos((2*math.pi/n)*(abs(j-i))*kappa)
return matrix
def getF(csi, a):
csiInv = linalg.inv(csi)
valueF = np.dot(csiInv, a)
valueF = np.dot(valueF, csiInv)
valueF = np.dot(valueF, a)
traceF = valueF.trace()
return 0.5 * traceF
def getG(csi, f, a):
csiInv = linalg.inv(csi)
valueG = np.dot(csiInv, a)
valueG = np.dot(valueG, csiInv)
valueG = valueG / (2 * f)
return valueG
def getE(g, k):
#m = 10 ^ -6
#kinv = linalg.inv(k + np.eye(k.shape[1])*m)
kinv = linalg.inv(k)
#kinv = linalg.pinv(k)
ktrans = k.transpose()
#ktransinv = linalg.pinv(ktrans)
#ktransinv = linalg.inv(ktrans + np.eye(ktrans.shape[1])*m)
ktransinv = linalg.inv(ktrans)
e = np.dot(ktransinv,g)
e = np.dot(e, kinv)
return e
def getW(k, a, e):
ktrans = k.transpose()
w = np.dot(k, a)
w = np.dot(w, ktrans)
w = np.dot(w, e)
valuew = w.trace()
return valuew
def getV(csi, e, e2, k):
v = np.dot(csi, k)
v = np.dot(v, e)
v = np.dot(v, k)
v = np.dot(v, csi)
v = np.dot(v, k)
v = np.dot(v, e2)
v = np.dot(v, k)
traceV = v.trace()
return traceV
handle_2 = open("test.txt", "w")
n = 4
power_spectrum_k = np.zeros(n, float)
for i in range(n):
power = math.exp(-(2*math.pi*i/n)*(2*math.pi*i/n))
power_spectrum_k[i] = power
# ora posso chiamare l'anti-trasformata
inverse_transform = np.fft.ifft(power_spectrum_k)
print 'inverse_transform:', inverse_transform
CSI = zeros((n, n))
for i in range(n):
for j in range(n):
CSI[i][j] = inverse_transform[abs(i-j)]
betaArray = zeros(n, float)
WabArray = zeros((6, n), float)
correlation = zeros((6, 6), float)
list = [1, 2, 3, 4, 5, 6]
K = zeros((n, n), float)
for i in range(n):
for j in range(i+1):
i_shifted = i + 2
j_shifted = j + 1
print "###############################"
print i_shifted
print j
component1 = ((3.0*70.0*70.0*0.3)/(2.0*300000.0*300000.0))
component2 = ((j_shifted*(i_shifted-j_shifted))/(i_shifted))
component3 = (1.0+(70.0/300000.0)*j_shifted)
print component1
print component2
print component3
K[i][j] = component1*component2*component3
#print 'DetK:', np.linalg.det(K)
print 'K:\n', K
counter = 0
for alpha in list:
counter2 = 0
Aa = getA(alpha)
Faa = getF(CSI, Aa)
Ga = getG(CSI, Faa, Aa)
Ea = getE(Ga, K)
#print 'Ea:', Ea
V_alphaalpha = getV(CSI, Ea, Ea, K)
for beta in xrange(n):
Ab = getA(beta + 1)
#print "Calling getW with K=", K, "\n Ab=", Ab, "\nEa=", Ea
W_ab = getW(K, Ab, Ea)
#print "\nGot W_ab=", W_ab
betaArray[beta] = beta + 1
WabArray[counter][beta] = W_ab
output_string = " {0} {1} \n".format(str(beta + 1), str(W_ab))
handle_2.write(output_string)
Fbb = getF(CSI, Ab)
Gb = getG(CSI, Fbb, Ab)
Eb = getE(Gb, K)
#print "Beta array"
#print betaArray
#print "Wab array"
#print WabArray
for gamma in list:
Ac = getA(gamma)
Fcc = getF(CSI, Ac)
Gc = getG(CSI, Fcc, Ac)
Ec = getE(Gc, K)
V_alphagamma = getV(CSI, Ea, Ec, K)
V_gammagamma = getV(CSI, Ec, Ec, K)
C_alphagamma = V_alphagamma/(math.sqrt(V_alphaalpha * V_gammagamma))
correlation[counter][counter2] = C_alphagamma
counter2 = counter2 + 1
counter = counter + 1
print 'correlation:\n', correlation
WabArray_all = []
betaArray_all = []
for b in range(0, len(WabArray), 1):
for n in betaArray:
betaArray_all.append(n)
for n in WabArray[b]:
WabArray_all.append(n)
Now, as soon as I get n = 5 and any other value bigger than 4, I receive the error:
line 148, in <module>
C_alphagamma = V_alphagamma/(math.sqrt(V_alphaalpha * V_gammagamma))
ValueError: math domain error
which I interpret as a math error due to the fact that I performing the square root of a negative value. Nevertheless, I cannot understand where exactly is the error, in the sense that I cannot understand why changing from 4 to, say, 5 makes the difference. Has anyone any idea of what is going wrong?
math.sqrt is unable to calculate the negative square root
>>> math.sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
If this is valid complex math, import sqrt from cmath instead:
>>> cmath.sqrt(-1)
1j
It is hard to say where the problem is without understanding the algorythm. You will have to do more investigations. IMHO, you should ty to dump the values to better understand the problem but only when thing goes wrong to avoid too much output. Something like :
try:
C_alphagamma = V_alphagamma/(math.sqrt(V_alphaalpha * V_gammagamma))
except ValueError as e:
print alpha, Aa, Faa, Ga, Ea, V_alphaalpha
print gamma, Ac, Fcc, Gc, Ec, V_alphagamma, V_gammagamma
raise e
This is only an idea and other values might be more pertinent