backtracking in dijkstra algorithm in python - python-3.x

i have been given a matrix space of 400,400 with few obstacles inside . I have eight set of actions namely up, down, left right with edge cost of 1 and 4 diagonals with edge cost of 1.42. i took initial node as (5,5) and final node as (195,295). I'm able to reach the goal using Dijkstra's algorithm quite quickly but am not able to backtrack and have been stuck for a long time. Can someone please help me. Here's my code
import numpy as np
import matplotlib.pyplot as plt
import time
import math
blank_list = []
for entries in range(160000):
entries = math.inf
blank_list.append(entries)
initial_matrix = np.array(blank_list, dtype = object).reshape(400, 400)
rows = initial_matrix.shape[0]
cols = initial_matrix.shape[1]
flag = False
# Obstacles
for h in range(0, rows):
for k in range(0, cols):
if (k-225)**2 + (h-50)**2 <= 625:
initial_matrix[h][k] = flag
for h in range(0, rows):
for k in range(0, cols):
if h<= 13*k -140 and h<= 185 and h<= -(7/5)*k +290 and h<= (6/5)*k +30 and h<= -(6/5)*k +210 and h<= k + 100:
initial_matrix[h][k] = flag
for h in range(0, rows):
for k in range(0, cols):
if ((k-150)**2)/1600 + ((h-100)**2)/400 <= 1:
initial_matrix[h][k] = flag
# move subfunctions
def move_right(i,j, map_):
if not map_[i,j+1]:
return None
if j == 300:
return None
else:
cost = initial_matrix[i][j] + 1
j = j + 1
return (i,j) , cost
def move_left(i,j,map_):
if not map_[i,j-1]:
return None
if j == 0:
return None
else:
cost = initial_matrix[i][j] + 1
j = j - 1
return (i,j) , cost
def move_up(i,j,map_):
if not map_[i-1,j]:
return None
if i == 0:
return None
else:
cost = initial_matrix[i][j] + 1
i = i - 1
return (i,j) , cost
def move_down(i,j,map_):
if not map_[i+1,j]:
return None
if i == 200:
return None
else:
cost = initial_matrix[i][j] + 1
i = i + 1
return (i,j) , cost
def move_upright(i,j,map_):
if not map_[i-1,j+1]:
return None
if j == 300 or i == 0:
return None
else:
cost = 1.42 + initial_matrix[i][j]
j = j + 1
i = i - 1
return (i,j) , cost
def move_downright(i,j,map_):
if not map_[i+1,j+1]:
return None
if j == 300 or i == 200:
return None
else:
cost = 1.42 + initial_matrix[i][j]
j = j + 1
i = i + 1
return (i,j) , cost
def move_upleft(i,j,map_):
if map_[i-1,j-1]:
return None
if j == 0 or i == 0:
return None
else:
cost = 1.42 + initial_matrix[i][j]
j = j - 1
i = i - 1
return (i,j) , cost
def move_downleft(i,j,map_):
if not map_[i+1,j-1]:
return None
if j == 0 or i == 200:
return None
else:
cost = 1.42 + initial_matrix[i][j]
j = j - 1
i = i + 1
return (i,j) , cost
def get_neighbours(i,j):
neighbours_cost = []
index = []
action_up = move_up(i,j,initial_matrix)
if action_up is not None:
neighbours_cost.append(action_up[1])
index.append(action_up[0])
action_down = move_down(i,j,initial_matrix)
if action_down is not None:
neighbours_cost.append(action_down[1])
index.append(action_down[0])
action_left = move_left(i,j,initial_matrix)
if action_left is not None:
neighbours_cost.append(action_left[1])
index.append(action_left[0])
action_right = move_right(i,j,initial_matrix)
if action_right is not None:
neighbours_cost.append(action_right[1])
index.append(action_right[0])
action_upright = move_upright(i,j,initial_matrix)
if action_upright is not None:
neighbours_cost.append(action_upright[1])
index.append(action_upright[0])
action_downright = move_downright(i,j,initial_matrix)
if action_downright is not None:
neighbours_cost.append(action_downright[1])
index.append(action_downright[0])
action_upleft = move_upleft(i,j,initial_matrix)
if action_upleft is not None:
neighbours_cost.append(action_upleft[1])
index.append(action_upleft[0])
action_downleft = move_downleft(i,j,initial_matrix)
if action_downleft is not None:
neighbours_cost.append(action_downleft[1])
index.append(action_downleft[0])
return neighbours_cost, index
'''def get_current_value(i,j):
current_value = initial_matrix[i][j]
return current_value'''
'''def locate_value_index(mat):
i,j = np.where(mat == node)
i = int(i)
j = int(j)
return (i,j)'''
def sort_list1(list_a,list_b):
list_b = [i[1] for i in sorted(zip(list_a, list_b))]
def sort_list2(list_a):
list_a.sort()
goal = (195,295)
ind = (5,5)
initial_matrix[ind[0]][ind[1]] = 0
node_cost = 0
explore_queue = []
index_queue = []
index_queue.append(ind)
explore_queue.append(node_cost)
visited = set()
breakwhile = False
start_time = time.clock()
while len(index_queue) != 0 and not breakwhile:
node = index_queue[0]
visited.add(node)
#for i in range(len(index_queue)):
index_queue.pop(0)
explore_queue.pop(0)
#print(visited)
#val = get_current_value(ind[0],ind[1])
pair = get_neighbours(node[0],node[1])
neighbours_cost = pair[0]
index = pair[1]
#print(index)
#print(neighbours_cost)
#######
if node == goal:
print('goal reached')
breakwhile = True
#######
for i in range(len(index)):
if index[i] not in visited:
old_cost = initial_matrix[index[i][0]][index[i][1]]
if neighbours_cost[i] < old_cost:
initial_matrix[index[i][0]][index[i][1]] = neighbours_cost[i]
if old_cost != math.inf:
ind_node = index_queue.index((index[i][0], index[i][1]))
index_queue.pop(ind_node)
explore_queue.pop(ind_node)
index_queue.append(index[i])
explore_queue.append(initial_matrix[index[i][0]][index[i][1]])
sort_list1(explore_queue,index_queue)
sort_list2(explore_queue)
end_time = time.clock()
print(end_time - start_time)

I figured out the solution. it is to create dictionary with all the index values and iterating them over a while loop

Related

Getting invalid syntax error on line 26 if xi_distance > d_max:

import copy
import math
class obj_kMeans:
def __init__(self,init_centroid,k):
self.k = k
self.clusters = None
self.centroids = np.empty((self.k,2))
self.initial_centroid = init_centroid
def distance(self,x,y):
return np.linalg.norm(y-x, ord=2)
def init_centroids(self, x):
centroids = self.centroids
centroids[0] = self.initial_centroid
for i in range(1, self.k):
d_max = 0
d_max_cen = centroids[0]
for x_i in x:
if x_i in centroids:
continue
xi_distance = sum([self.distance(x_i, prev) for prev in centroids]
if xi_distance > d_max:
d_max = xi_distance
d_max_cen = x_i
centroids[i] = d_max_cen
return centroids
def cost_function(self,clusters):
squared_errors = list()
for i in range(self.k):
points = clusters[i][1]
centroid = clusters[i][0]
errors = [self.distance(centroid,point)**2 for point in points]
total_error = sum(errors)
squared_errors.append(total_error)
return sum(squared_errors)
def cluster(self,x):
centroids = self.init_centroids(x)
clusters = {}
for i in range(self.k):
cluster_list = (centroids[i],list())
clusters[i] = cluster_list
itr = 0
while True:
for point in x:
dist_min = self.distance(centroids[0],point)
num = 0
cls_point = 0
for centroid in centroids:
d = self.distance(centroid,point)
if d < dist_min:
cls_point = num
dist_min = d
num +=1
clusters[cls_point][1].append(point)
self.clusters = copy.deepcopy(clusters)
centroids = list()
converges = True
for i in range(self.k):
dp = clusters[i][1]
dp.append(clusters[i][0])
centroid_p = np.mean(np.array(dp), axis=0)
if not ((centroid_p == clusters[i][0]).all()):
converges = False
centroids.append(centroid_p)
cluster_list = (centroid_p,list())
clusters[i] = cluster_list
itr +=1
if converges:
break
self.centroids = centroids
kMeans_implement1 = obj_kMeans(k=k1, init_centroid = i_point1)
kMeans_implement2 = obj_kMeans(k=k2, init_centroid = i_point2)
kMeans_implement1.cluster(data)
kMeans_implement2.cluster(data)
cost_val1 = kMeans_implement1.cost_function(kMeans_implement1.clusters)
cost_val2 = kMeans_implement2.cost_function(kMeans_implement2.clusters)
print("centroid 1", kMeans_implement1.centroids)
print("centroid 2", kMeans_implement2.centroids)
print("cost 1", cost_val1)
print("cost 2", cost_val2)

Scipy optimize.minimize with multi- parameters

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import linalg, optimize
%matplotlib inline
Data load
data = pd.read_csv("D:/Stat/TimeSeries/KRW_month_0617_1.csv",index_col="Date") / 100
para = open("D:/Stat/TimeSeries/KRW_month_0617_1.txt").readlines()[0:2]
data.index = pd.to_datetime(data.index)
Parameters
cond = []
params = []
time = []
for i in para:
j = i.split()
for k in j:
cond.append(k)
cond = cond[1:]
for i in range(len(cond)):
cond[i] = round(float(cond[i]),4)
params = cond[0:23]
time = cond[23:]
maturity = np.array(time[1:])
timegap = 1/cond[23]
Functions We need
def Paramcheck(Params, checkStationary = 1):
result = 0
Kappa = np.array([[params[20],0,0], [0,params[21],0], [0,0,params[22]]])
Sigma = np.array([[params[1],0,0], [params[2],params[3],0], [params[4],params[5],params[6]]])
State = np.array([params[7], params[8], params[9]])
Lambda = params[0]
SigmaEps = np.identity(10)
for i in range(10):
SigmaEps[i][i] = params[i+10]
for i in range(len(Sigma)):
if Sigma[i][i] < 0:
result = 1
for j in SigmaEps:
if np.any(SigmaEps) < 0:
result = 1
if Lambda < 0.05 or Lambda > 2:
result = 1
elif State[0] < 0:
result = 1
elif Kappa[0][0] < 0:
result = 1
if result == 0 and checkStationary > 0:
if max(np.linalg.eigvals(-Kappa).real) > 0:
result = 2
return result
def CheckDet(x):
if x == np.inf or x == np.nan:
result = 1
elif x < 0:
result = 2
elif abs(x) < 10**-250:
result = 3
else:
result = 0
return result
def NS_factor(lambda_val, maturity):
col1 = np.ones(len(maturity))
col2 = (1 - np.exp(-lambda_val*maturity))/(lambda_val*maturity)
col3 = col2 - np.exp(-lambda_val*maturity)
factor = np.array([col1,col2,col3]).transpose()
return factor
def DNS_Kalman_filter(Params, *args):
N = Paramcheck(Params)
if N == 0:
Kappa = np.array([[params[20],0,0], [0,params[21],0], [0,0,params[22]]])
Sigma = np.array([[params[1],0,0], [params[2],params[3],0],
[params[4],params[5],params[6]]])
State = np.array([params[7], params[8], params[9]])
Lambda = params[0]
SigmaEps = np.identity(10)
for i in range(10):
SigmaEps[i][i] = params[i+10]
Obs_Yield = args[0]
Obs_Date = args[1]
Timegap = args[2]
Obs_Mty = args[3]
Finalstate = args[4]
Mty_length = len(Obs_Mty)
B = NS_factor(lambda_val = Lambda,maturity = Obs_Mty)
H_large = SigmaEps **2
N_obs = len(Obs_Date)
LLH_vec = np.zeros(N_obs)
phi1 = linalg.expm(-Kappa*Timegap)
phi0 = (np.identity(3)-phi1) # State
Eigenvalues = np.linalg.eig(Kappa)[0]
Eigen_vec = np.linalg.eig(Kappa)[1]
Eigen_vec_inv = np.linalg.inv(Eigen_vec)
S = Eigen_vec_inv # Sigma # Sigma.transpose() # Eigen_vec_inv.transpose()
Atilde = np.dot(Sigma[0], Sigma[0])
Btilde = np.dot(Sigma[1], Sigma[1])
Ctilde = np.dot(Sigma[2], Sigma[2])
Dtilde = np.dot(Sigma[0], Sigma[1])
Etilde = np.dot(Sigma[0], Sigma[2])
Ftilde = np.dot(Sigma[1], Sigma[2])
res1= Atilde* Obs_Mty* Obs_Mty/6
res2= Btilde*(1/(2*Lambda**2) - (1-np.exp(-Lambda*Obs_Mty))/(Lambda**3*Obs_Mty) + (1-
np.exp(-2*Lambda*Obs_Mty))/(4*Lambda**3*Obs_Mty))
res3= Ctilde*(1/(2*Lambda**2) + np.exp(-Lambda*Obs_Mty)/(Lambda**2)-
Obs_Mty*np.exp(-2*Lambda*Obs_Mty)/(4*Lambda) -
3*np.exp(-2*Lambda*Obs_Mty)/(4*Lambda**2) - 2*(1-np.exp(-
Lambda*Obs_Mty))/(Lambda**3*Obs_Mty) + 5*(1-
np.exp(-2*Lambda*Obs_Mty))/(8*Lambda**3*Obs_Mty))
res4= Dtilde*(Obs_Mty/(2*Lambda) + np.exp(-Lambda*Obs_Mty)/(Lambda**2) - (1-np.exp(-
Lambda*Obs_Mty))/(Lambda**3*Obs_Mty))
res5= Etilde*(3*np.exp(-Lambda*Obs_Mty)/(Lambda**2) + Obs_Mty/(2*Lambda)+Obs_Mty*np.exp(-
Lambda*Obs_Mty)/(Lambda) - 3*(1-np.exp(-Lambda*Obs_Mty))/(Lambda**3*Obs_Mty))
res6= Ftilde*(1/(Lambda**2) + np.exp(-Lambda*Obs_Mty)/(Lambda**2) -
np.exp(-2*Lambda*Obs_Mty)/(2*Lambda**2) - 3*(1-np.exp(-
Lambda*Obs_Mty))/(Lambda**3*Obs_Mty) + 3*(1-
np.exp(-2*Lambda*Obs_Mty))/(4*Lambda**3*Obs_Mty))
val = res1 + res2 + res3 + res4 + res5 + res6
V_mat = np.zeros([3,3])
V_lim = np.zeros([3,3])
for i in range(3):
for j in range(3):
V_mat[i][j] = S[i][j]*(1-np.exp(-(Eigenvalues[i] +
Eigenvalues[j])*Timegap))/(Eigenvalues[i] + Eigenvalues[j])
V_lim[i][j] = S[i][j]/(Eigenvalues[i] + Eigenvalues[j])
Q = (Eigen_vec # V_mat # Eigen_vec.transpose()).real
Sigma_lim = (Eigen_vec # V_lim # Eigen_vec.transpose()).real
for i in range(N_obs):
y = Obs_Yield[i]
xhat = phi0 + phi1 # State
y_implied = B # xhat
v = y - y_implied + val
Sigmahat = phi1 # Sigma_lim # phi1.transpose() + Q
F = B # Sigmahat # B.transpose() + H_large
detF = np.linalg.det(F)
if CheckDet(detF) > 0:
N = 3
break
Finv = np.linalg.inv(F)
State = xhat + Sigmahat # B.transpose() # Finv # v
Sigma_lim = Sigmahat - Sigmahat # B.transpose() # Finv # B # Sigmahat
LLH_vec[i] = np.log(detF) + v.transpose() # Finv # v
if N == 0:
if Finalstate:
yDate = Obs_Date[-1]
result = np.array([yDate,State])
else:
result = 0.5 * (sum(LLH_vec) + Mty_length*N_obs*np.log(2*np.pi))
else:
result = 7000000
return result
I made a code that does Arbitrage Free Nelson-Siegel model. Data is return rates of bond (1Y,1.5Y, ... ,20Y). I wanna optimize that function with scipy optimize.minimize function with fixed *args.
Suppose that Initial parmas are verified that it's close to optimized params from empirical experiments using Dynamic Nelson-Siegel Model.
LLC_new = 0
while True:
LLC_old = LLC_new
OPT = optimize.minimize(x0=params,fun=DNS_Kalman_filter, args=
(data.values,data.index,timegap,maturity,0))
params = OPT.x
LLC_new = round(OPT.fun,5)
print("Current LLC: %0.5f" %LLC_new)
if LLC_old == LLC_new:
OPT_para = params
FinalState = DNS_Kalman_filter(params,data.values,data.index,timegap,maturity,True)
break
Result is
Current LLC: -7613.70146
Current LLC: -7613.70146
LLC(log-likelihood value) isn't maximized. It's not a result I desire using Optimizer.
Is there any solution for that?
In R, there is optim() function works as similar as scipy.optimize.minimize() which works really well. I also have a R code for that very similar to this Python code.

How to fix with KeyError: q[minKey]? Run time error on python

I'm currently doing my school assignment which needs to be submitted to Automarker to test the code.
The question I'm stuck has to pass 5 different tasks and each task contains different input which is not given.
The only input is given is the sample input which shows below.
I keep getting the result of "KeyError: del q[min_Key]" and the status show "RunTimeError".
I don't understand how does it occurs?
And how to fix this error correctly?
from math import sqrt
import math
def getP(numV, ver, e):
d = -1
q = {0:0.0}
while bool(q):
minNum = 9999.0
min_Key = len(q) - 1
for k in q.keys():
if minNum > q[k]:
minNum = q[k]
min_Key = k
if numV-1 == min_Key:
d = minNum
break
del q[min_Key]
for v in e[min_Key].keys():
if v not in q:
q[v] = minNum+e[min_Key][v]
else:
if q[v] > minNum+e[min_Key][v]:
q[v] = minNum+e[min_Key][v]
return (d)
while True:
try:
user_input = input().split(',')
size = len(user_input)
n = user_input[0]
ver = {}
numV = 0
for i in range(1, size):
if i % 2 == 1:
ver[numV] = (float(user_input[i]), float(user_input[i+1]))
numV = numV + 1
e = {}
for u in ver.keys():
e[u] = {}
for v in ver.keys():
if u != v:
dist1 = (ver[u][1] - ver[v][1])*(ver[u][1] - ver[v][1])
dist2 = (ver[u][0] - ver[v][0])*(ver[u][0] - ver[v][0])
dist = dist1 + dist2
if dist <= 10000.0:
dist = sqrt(dist)
e[u][v] = dist
d = getP(numV, ver, e)
if d == -1:
print(d)
else:
print('{0:.2f}'.format(d))
except EOFError:
break
Sample Input: 100,0,0,0,100,100,100
Sample Output: 200.00
TIA!

How to get the best feature or gens from "random_search(n,dim)" using python code?

I have get the feature or gens from random_search(n,dim), but if i re-run the program, the result always change or different from the result befor.
I want the feature selection (gens) have the same result, although i want to re-run the program/code.
I cant get the same result if i re-run the code or program
' python
Code BCS for feature selection
def BCS(Eval_Func,m_i=200,n=24,minf=0,dim=None,prog=False,alpha=0.1,beta=1.5,param=0.25):
estimate=Eval_Func().evaluate
if dim==None:
dim=Eval_Func().check_dimentions(dim)
pa=param
#flag=dr
gens=random_search(n,dim)
fit=[float("-inf") if minf == 0 else float("inf") for _ in range(n)]
pos=[0 for _ in range(n)]
g_pos=[0]*dim
g_val=float("-inf") if minf == 0 else float("inf")
gens_dict={tuple([0]*dim):float("-inf") if minf == 0 else float("inf")}
if prog:
miter=tqdm(range(m_i))
else:
miter=range(m_i)
for it in miter:
for i,g in enumerate(gens):
if tuple(g) in gens_dict:
score=gens_dict[tuple(g)]
else:
score=estimate(g)
gens_dict[tuple(g)]=score
if score > fit[i] if minf==0 else score < fit[i]:
fit[i]=score
pos[i]=g
maxfit,maxind=max(fit),fit.index(max(fit))
minfit,minind=min(fit),fit.index(min(fit))
if minf==0:
if maxfit > g_val:
g_val=dc(maxfit)
g_pos=dc(gens[maxind])
else:
if minfit < g_val:
g_val=dc(minfit)
g_pos=dc(gens[minind])
if pa < random.uniform(0,1):
if minf==0:
gens[minind]=[0 if 0.5>random.uniform(0,1) else 1 for _ in range(dim)]#rand_gen()
fit[minind]=float("-inf") if minf == 0 else float("inf")
else:
gens[maxind]=[0 if 0.5>random.uniform(0,1) else 1 for _ in range(dim)]#rand_gen()
fit[maxind]=float("-inf") if minf == 0 else float("inf")
for g in gens:
for d in range(dim):
x=levy_flight(beta,g_pos[d],g[d],alpha)
if random.uniform(0,1) < sigmoid(x):
g[d]=1
else:
g[d]=0
return g_val,g_pos,g_pos.count(1)
Code form Class Evaluate
class Evaluate:
def __init__(self):
self.train_l = tr_y
self.train_d = tr_x
self.test_l = te_y
self.test_d=te_x
self.K = 2
def evaluate(self,gen):
mask=np.array(gen) > 0
al_data=np.array([al[mask] for al in self.train_d])
#al_test_data=np.array([al[mask] for al in self.test_d])
kf = ms.KFold(n_splits=self.K,random_state=42)
s = 0
for tr_ix,te_ix in kf.split(al_data):
s+= svm.SVR(kernel = 'rbf', gamma='scale', C=1.0, epsilon=0.2).fit(al_data[tr_ix],self.train_l[tr_ix]).score(al_data[te_ix],self.train_l[te_ix])#.predict(al_test_data)
s/=self.K
return s#np.count_nonzero(self.test_l==res)/len(self.test_l)
def check_dimentions(self,dim):
if dim==None:
return len(self.train_d[0])
else:
return dim
Code For Test_score
def test_score(gen,tr_x,tr_y,te_x,te_y):
mask=np.array(gen) == 1
al_data=np.array(tr_x[:,mask])
al_test_data=np.array(te_x[:,mask])
return np.mean(np.abs(([svm.SVR(kernel = 'rbf', gamma='scale', C=1.0, epsilon=0.2).fit(al_data,tr_y).score(al_test_data,te_y) for i in range(4)])))*100
Code for call the Program BCS and Class Evaluate
def fitureSelection(data):
result = []
for i in range(0,2):
X = data.loc[data['label']== i].iloc[:,0:93]
y = data.loc[data['label']== i].iloc[:,95]
tr_x, te_x, tr_y, te_y = train_test_split(X,y,test_size=0.2)
sc_X = StandardScaler()
tr_x = sc_X.fit_transform(tr_x)
te_x = sc_X.fit_transform(te_x)
s,g,l = BCS(Eval_Func=Evaluate,n=20,m_i=200)
result.append("".join(map(str,g)))
return result
Code for call the def fitureSelection(data)
hasil = fitureSelection(Hasilcolend)
The result
['000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000',
'000000000000100000010000000000000000000100000000000000001100000000000000000001010001000000000']
'

Simple python loop doesn't work with many inner loops

I am struggling with a very simple loop that perfectly works if run "standalone", but doesn't work anymore if I use it as an outer loop for many other instructions (which also perfectly work if run standalone for only 1 iteration).
The simple outer loop is a
for i in range(0,somevalue):
do some inner instructions
Here is the full code, which perfectly works if I put a range of dimension 1, whilst it never ends if I put even a simple range of dimension 2:
import numpy as np
import numpy.ma as ma
import random
import matplotlib.pyplot as plt
i = int
x = np.zeros(1440)
class_x = np.zeros(1440)
w1 = np.array([0,6*60])
w2 = np.array([20*60,23*60])
x[w1[0]:(w1[1])] = np.full(np.diff(w1),0.001)
x[w2[0]:(w2[1])] = np.full(np.diff(w2),0.001)
x_masked = np.zeros_like(ma.masked_not_equal(x,0.001))
c = 10
func_time = 300
max_free_spot = int
i = 0
for i in range(0,1):
tot_time = 0
switch_count = 0
switch_ons = []
while
tot_time <= func_time:
switch_on = random.choice([random.randint(w1[0],(w1[1]-c)),random.randint(w2[0],(w2[1]-c))])
if x[switch_on] == 0.001:
if switch_on in range(w1[0],w1[1]):
if np.any(x[switch_on:w1[1]]!=0.001):
next_switch = [switch_on + k[0] for k in np.where(x[switch_on:]!=0.001)]
if (next_switch[0] - switch_on) >= c and max_free_spot >= c:
upper_limit = min((next_switch[0]-switch_on),min(func_time,w1[1]-switch_on))
elif (next_switch[0] - switch_on) < c and max_free_spot >= c:
continue
else: upper_limit = next_switch[0]-switch_on
else:
upper_limit = min(func_time,w1[1]-switch_on) #max random length of cycle
if upper_limit >= c:
indexes = np.arange(switch_on,switch_on+(random.randint(c,upper_limit)))
else:
indexes = np.arange(switch_on,switch_on+upper_limit)
else:
if np.any(x[switch_on:w2[1]]!=0.001):
next_switch = [switch_on + k[0] for k in np.where(x[switch_on:]!=0.001)]
if (next_switch[0] - switch_on) >= c:
upper_limit = min((next_switch[0]-switch_on),min(func_time,w2[1]-switch_on))
elif (next_switch[0] - switch_on) < c and max_free_spot >= c:
continue
else: upper_limit = next_switch[0]-switch_on
else:
upper_limit = min(func_time,w2[1]-switch_on)
if upper_limit >= c:
indexes = np.arange(switch_on,switch_on+(random.randint(c,upper_limit)))
else:
indexes = np.arange(switch_on,switch_on+upper_limit)
tot_time = tot_time + indexes.size
switch_ons.append(switch_on)
if tot_time > func_time:
indexes_adj = indexes[:-(tot_time-func_time)]
coincidence = random.randint(1,5)
np.put(x_masked,indexes_adj,(2*coincidence),mode='clip')
np.put(x,indexes_adj,(2*coincidence))
x_masked = np.zeros_like(ma.masked_greater_equal(x_masked,0.001))
tot_time = (tot_time - indexes.size) + indexes_adj.size
switch_count = switch_count + 1
break
else:
coincidence = random.randint(1,5)
np.put(x_masked,indexes,(2*coincidence),mode='clip')
np.put(x,indexes,(2*coincidence))
x_masked = np.zeros_like(ma.masked_greater_equal(x_masked,0.001))
tot_time = tot_time
switch_count = switch_count + 1
free_spots = []
for j in ma.notmasked_contiguous(x_masked):
free_spots.append(j.stop-j.start)
max_free_spot = max(free_spots)
class_x = class_x + x
plt.plot(class_x)
Any help is really really appreciated

Resources