Iterating Swap Algorithm Python
Question: I have an algorithm. I want that last solution of the algorithm if respect
certains conditions become the first solution. In my case I have this:

  1. First PArt

Split the multidimensional array q in 2 parts

    
    
    split_at = q[:,3].searchsorted([1,random.randrange(LB,UB-I)])
    D = numpy.split(q, split_at) 
    

Change and rename the splitted matrix:

    
    
    S=B[1]
    SF=B[2]
    S2=copy(SF)
    S2[:,3]=S2[:,3]+I
    

Define a function f:

    
    
    f=sum(S[:,1]*S[:,3])+sum(S2[:,1]*S2[:,3])
    

This first part is an obligated passage.

  1. Second Passage

Then I split again the array in 2 parts:

    
    
    split_at = q[:,3].searchsorted([1,random.randrange(LB,UB-I)]) 
    D = numpy.split(q, split_at) 
    

I rename and change parts of the matrix(like in the first passage:

    
    
    T=D[1]
    TF=D[2]
    T2=copy(TF)
    T2[:,3]=T2[:,3]+I
    
    u=random.sample(T[:],1) #I random select an array from T
    v=random.sample(T2[:],1) #random select an array from T2
    u=array(u)
    v=array(v)
    

Here is my first problem: I want to continue the algorithm only if
v[0,0]-u[0,0]+T[-1,3]<=UB, if not I want to repeat Second Passage until the
condition is verified.

Now I swap 1 random array from T with another from T2:

    
    
    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]
    

I modified and recalculate some in the matrix:

    
    
    E=np.copy(T)
    E2=np.copy(T2)
    E[:,3]=np.cumsum(E[:,0])
    E2[:,3]=np.cumsum(E2[:,0])+I
    

Define f2:

    
    
    f2=sum(E[:,1]*E[:,3])+sum(E2[:,1]*E2[:,3])
    

Here my second and last problem. I need to iterate this algorithm. If f-f2<0
my new starting solution has to be E and E2 and my new f has to be f2 and
iterate excluding last choice the algorithm (recalcultaing a new f and f2).
Thank you for the patience. I'm a noob :D

EDIT: I have an example here(this part goes before the code I have written on
top)

    
    
    import numpy as np
    import random
    
    p=[ 29,  85, 147,  98,  89,  83,  49,   7,  48,  88, 106,  97,   2,
           107,  33, 144, 123,  84,  25,  42,  17,  82, 125, 103,  31, 110,
            34, 100,  36,  46,  63,  18, 132,  10,  26, 119, 133,  15, 138,
           113, 108,  81, 118, 116, 114, 130, 134,  86, 143, 126, 104,  52,
           102,   8,  90,  11,  87,  37,  68,  75,  69,  56,  40,  70,  35,
            71, 109,   5, 131, 121,  73,  38, 149,  20, 142,  91,  24,  53,
            57,  39,  80,  79,  94, 136, 111,  78,  43,  92, 135,  65, 140,
           148, 115,  61, 137,  50,  77,  30,   3,  93]
    w=[106,  71, 141, 134,  14,  53,  57, 128, 119,   6,   4,   2, 140,
            63,  51, 126,  35,  21, 125,   7, 109,  82,  95, 129,  67, 115,
           112,  31, 114,  42,  91,  46, 108,  60,  97, 142,  85, 149,  28,
            58,  52,  41,  22,  83,  86,   9, 120,  30, 136,  49,  84,  38,
            70, 127,   1,  99,  55,  77, 144, 105, 145, 132,  45,  61,  81,
            10,  36,  80,  90,  62,  32,  68, 117,  64,  24, 104, 131,  15,
            47, 102, 100,  16,  89,   3, 147,  48, 148,  59, 143,  98,  88,
           118, 121,  18,  19,  11,  69,  65, 123,  93]
    p=array(p,'double')
    w=array(w,'double')
    r=p/w
    LB=12 
    UB=155 
    I=9 
    j=p,w,r
    j=transpose(j)
    k=j[j[:,2].argsort()]
    c=np.cumsum(k[:,0])
    q=k[:,0],k[:,1],k[:,2],c
    q=transpose(q)
    o=sum(q[:,1]*q[:,3])
    split_at = q[:,3].searchsorted([1,UB-I]) 
    B = numpy.split(q, split_at) 
    S=B[1]
    SF=B[2]
    S2=copy(SF)
    S2[:,3]=S2[:,3]+I
    f=sum(S[:,1]*S[:,3])+sum(S2[:,1]*S2[:,3])
    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)
    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])
    

I tried:

    
    
    def DivideRandom(T,T2):
        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
    Divide(T,T2)
    def SelectJob(u,v):
        u=random.sample(T[:],1)
        v=random.sample(T2[:],1)
        u=array(u)
        v=array(v)
    SelectJob(u,v)    
    d=v[0,0]-u[0,0]+T[-1,3]
    def Swap(u,v):
        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])
    while True:
         if d<=UB
            Swap(u,v)
            if d>UB
               DivideRandom(T,T2)
               SelectJob(u,v)
               if d<UB:
                  break
    


Answer: You can iterate indefinitely using `while True`, then stop whenever your
conditions are met using `break`:

    
    
    count = 0
    while True:
        count += 1
        if count == 10:
            break
    

So for your second example you can try:

    
    
    while True:
        ...
        if f - f2 < 0:
            # use new variables
            f, E = f2, E2
        else:
            break
    

Your first problem is similar; loop, test, reset the appropriate variables.

