Question

I have this code:

split_at = q[:,3].searchsorted([1,random.randrange(LB,UB-I)])
D = numpy.split(q, split_at) 
T=D[1]
TF=D[2]
T2=copy(TF)
T2[:,3]=T2[:,3]+I
u=random.sample(T[:],1)
v=random.sample(T2[:],1)
u=array(u)
v=array(v)
d=v[0,0]-u[0,0]+T[-1,3]

I want that ifd<=1000 :

x=numpy.where(v==T2)[0][0]
y=numpy.where(u==T)[0][0]
l=np.copy(T[y])
T[y],T2[x]=T2[x],T[y]
T2[x],l=l,T2[x]
E=np.copy(T)
E2=np.copy(T2)
E[:,3]=np.cumsum(E[:,0])
E2[:,3]=np.cumsum(E2[:,0])+I
f2=sum(E[:,1]*E[:,3])+sum(E2[:,1]*E2[:,3])

And than that if d>1000 I want ot recalculate the first part of code and verify if d<=1000 and this over and over again until the condition is met.

Was it helpful?

Solution

What about starting with:

d = 1000000
while d > 1000:
    split_at = q[:,3].searchsorted([1,random.randrange(LB,UB-I)])
    D = numpy.split(q, split_at) 
    T=D[1]
    TF=D[2]
    T2=copy(TF)
    T2[:,3]=T2[:,3]+I
    u=random.sample(T[:],1)
    v=random.sample(T2[:],1)
    u=array(u)
    v=array(v)
    d=v[0,0]-u[0,0]+T[-1,3]
x=numpy.where(v==T2)[0][0]
y=numpy.where(u==T)[0][0]
l=np.copy(T[y])
T[y],T2[x]=T2[x],T[y]
T2[x],l=l,T2[x]
E=np.copy(T)
E2=np.copy(T2)
E[:,3]=np.cumsum(E[:,0])
E2[:,3]=np.cumsum(E2[:,0])+I
f2=sum(E[:,1]*E[:,3])+sum(E2[:,1]*E2[:,3])

OTHER TIPS

Why not arranging the code into functions like:

def func1(): # add your arguments to param list
   split_at = q[:,3].searchsorted([1,random.randrange(LB,UB-I)])
   # some code 
   d=v[0,0]-u[0,0]+T[-1,3]
   return d,T,T2

def func2(T,T2):
   x=numpy.where(v==T2)[0][0]
   # more code       

and just do something like:

d = 10000
while  d > 1000:
   d,T,T2 = func1()
else:
   func2(T,T2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top