def getFromPermutation(max, num): """ Returns elements of a permutation, because random.permutation is too low size -- size to elements num -- numbers to select """ code = """ #line 85 "cutils.py" int *arange = (int *) malloc(sizeof(int)*max); int i, value; for (i = 0; i < max; i++) { arange[i] = i; } for (i = 0; i < num; i++) { value = int(RAND1(i) % (num-i+1)); RESULTS1(i) = arange[value]; arange[value] = arange[num-i-1]; } free(arange); """ assert num <= max rand = random.randreal(0, max, size=num) results = np.array(np.zeros(num, dtype=np.int8)) weave.inline(code, ["rand", "max", "num", "results"]) return results
def crossBLX(mother, parent, domain, alpha): """ crossover operator BLX-alpha mother -- mother (first individual) parent -- parent (second individual) domain -- domain to check alpha -- parameter alpha Returns the new children following the expression children = random(x-alpha*dif, y+alpha*dif), where dif=abs(x,y) and x=lower(mother,parents), y=upper(mother,parents) >>> import numpy as np >>> low=-5 >>> upper = 5 >>> dim=30 >>> sol = np.array([1,2,3,2,1]) >>> crossBLX(sol,sol,[low,upper],0) array([ 1., 2., 3., 2., 1.]) """ code = """ #line 128 "cutils.py" double diff,I,A,B; double x, y; int i; for (i = 0; i < Nm[0]; i++) { if (M1(i) < P1(i) ) { x = M1(i); y = P1(i); } else { y = M1(i); x = P1(i); } I = alpha*(y-x); A = x-I; B = y+I; if (A < lower) A = lower; if (B > upper) B = upper; C1(i) = A+R1(i)*(B-A); } """ dim = mother.size m = mother p = parent r = random.randreal(0, 1, dim) c = np.array(np.zeros(dim)) [lower, upper] = domain weave.inline(code, ["m", "p", "r", "alpha", "lower", "upper", "c"]) return c
def getParentByNAM(motherId, values, popsize, tsize=3): """ Get the parent using the NAM selection Parent is selected by competition between tsize random individuals (the individual which best/lower fitness is selected) Return the parent more distant from motherId """ code = """ #line 186 "cutils.py" int *arange = (int *) malloc(sizeof(int)*max); int *parents = (int *) malloc(sizeof(int)*num); int i, j, posi, limit,dim; int reference = (int) ref; double dist, value, maxdist; maxdist = -1; // Init the permutation for (i = 0; i < max; i++) { arange[i] = i; } arange[reference] = arange[max-1]; max-=1; // Get the random individuals for (i = 0; i < num; i++) { limit = (max-i-1); posi = (int) rint(RAND1(i)*limit); parents[i] = arange[posi]; arange[posi] = arange[limit]; } free(arange); dim = Nd[1]; // Obtains the most distant parents for (i = 0; i < num; i++) { posi = parents[i]; dist = 0; for (j = 0; j < dim; j++) { value = D2(posi, j) - D2(reference, j); dist += value*value; } dist = sqrt(dist); if (dist > maxdist || maxdist < 0) { maxdist = dist; BEST1(0) = posi; } } free(parents); """ rand = random.randreal(0.0, 1.0, tsize) max = popsize num = tsize d = values (psize, dim) = values.shape ref = motherId best = np.array([0]) weave.inline(code, ["ref", "rand", "max", "num", "d", "best"]) return best[0]