def runExtend(N, k, t=3, l=2, verbose=1, initsols=None, nums=[], algorithm=None): """ Run extension algorithm and return arrays Args: N (int): number of rows k (int): number of columns to extend to t (int): strength of the arrays l (int): factors of the designs initsols (None or list): list of arrays to extend, None to start with root Returns: list: list of generated designs Example: >>> import oapackage >>> designs = oapackage.oahelper.runExtend(16, 5, 3, verbose=0) """ if verbose: print('runExtend: N=%d, k=%d, t=%d' % (N, k, t)) if isinstance(l, list): ll = l else: ll = [l] ll = ll + [ll[-1]] * (k - len(ll)) s = oalib.intVector(ll) adata = oalib.arraydata_t(s, N, t, k) al = oalib.array_link(adata.N, adata.strength, 1) al.create_root(adata) if initsols is None: sols0 = oalib.arraylist_t() sols0.append(al) tstart = t else: sols0 = initsols tstart = sols0[0].n_columns oaoptions = oalib.OAextend() if algorithm is None: oaoptions.setAlgorithmAuto(adata) else: oaoptions.setAlgorithm(algorithm, adata) solsx = sols0 for ii in range(tstart, k): solsx = oalib.arraylist_t() oalib.extend_arraylist(sols0, adata, oaoptions, ii, solsx) if verbose >= 2: print(' ii %d: %d' % (ii, solsx.size())) sols0 = solsx nums.append(solsx.size()) sys.stdout.flush() return solsx
def runExtend(N, k, t=3, l=2, verbose=1, initsols=None, nums=[], algorithm=None): """ Run extension algorithm and return arrays Arguments --------- N : integer number of rows k: integer number of columns t: integer strength of the arrays >>> r = runExtend(16, 5, 3, verbose=0) """ if verbose: print('runExtend: N=%d, k=%d, t=%d' % (N, k, t)) if isinstance(l, list): # types.ListType): ll = l else: ll = [l] ll = ll + [ll[-1]] * (k - len(ll)) s = oalib.intVector(ll) adata = oalib.arraydata_t(s, N, t, k) al = oalib.array_link(adata.N, adata.strength, 1) al.create_root(adata) if initsols is None: sols0 = oalib.arraylist_t() sols0.append(al) tstart = t else: sols0 = initsols tstart = sols0[0].n_columns oaoptions = oalib.OAextend() if algorithm is None: oaoptions.setAlgorithmAuto(adata) else: oaoptions.setAlgorithm(algorithm, adata) solsx = sols0 for ii in range(tstart, k): solsx = oalib.arraylist_t() oalib.extend_arraylist(sols0, adata, oaoptions, ii, solsx) if verbose >= 2: print(' ii %d: %d' % (ii, solsx.size())) sols0 = solsx nums.append(solsx.size()) sys.stdout.flush() return solsx
def extendSingleArray(A, adata, t=3, verbose=1): """ Extend a single array """ oaoptions = oalib.OAextend() sols0 = oalib.arraylist_t() solsx = oalib.arraylist_t() sols0.push_back(A) #N = A.n_rows k = A.n_columns n = oalib.extend_arraylist(sols0, adata, oaoptions, k, solsx) sys.stdout.flush() return solsx
def extendSingleArray(A, adata, t=3, verbose=1): """ Extend a single orthogonal array """ oaoptions = oalib.OAextend() oaoptions.setAlgorithmAuto(adata) sols0 = oalib.arraylist_t() solsx = oalib.arraylist_t() sols0.push_back(A) k = A.n_columns n = oalib.extend_arraylist(sols0, adata, oaoptions, k, solsx) assert(n >= len(solsx)) sys.stdout.flush() return solsx
def extendSingleArray(A, adata, t=3, verbose=1): """ Extend a single orthogonal array """ oaoptions = oalib.OAextend() oaoptions.setAlgorithmAuto(adata) sols0 = oalib.arraylist_t() solsx = oalib.arraylist_t() sols0.push_back(A) k = A.n_columns n = oalib.extend_arraylist(sols0, adata, oaoptions, k, solsx) assert (n >= len(solsx)) sys.stdout.flush() return solsx
def selectArraysInFile(infile, outfile, idx, afmode=oalib.ATEXT, verbose=1, cache=1): """ Select arrays in a file by indices Args: infile (str): file with designs outfile (str): output file with designs inx (list of indices) Returns: None """ if not checkFiles(outfile, cache=cache): gidxint = oalib.intVector([int(x) for x in idx]) sols = oalib.arraylist_t() oalib.selectArrays(infile, gidxint, sols, 0) af = oalib.arrayfile_t(infile, 1) _ = oalib.writearrayfile(outfile, sols, afmode, af.nrows, af.ncols) if verbose >= 2: print('selectArrays: write array file %s in mode %d' % (outfile, afmode)) else: if verbose >= 2: print('output file %s already exists' % outfile)
def joinArrayLists(ww): """ Concetenate a list of array lists into a single list """ ll = oalib.arraylist_t() for w in ww: for al in w: ll.push_back(al) return ll
def selectArrays(infile, outfile, idx, afmode=oalib.ATEXT, verbose=1, cache=1): """ Select arrays in a file by indices Input: - infile (string) - outfile (string) - inx (list of indices) Output: - None """ if not checkFiles(outfile, cache=cache): gidxint = oalib.intVector([int(x) for x in idx]) # print(' xx select: %s' % str(gidxint)) # print(' xx select: %s' % str([v for v in gidxint])) sols = oalib.arraylist_t() oalib.selectArrays(infile, gidxint, sols, 0) af = oalib.arrayfile_t(infile, 1) af.nrows r = oalib.writearrayfile(outfile, sols, afmode, af.nrows, af.ncols) if verbose >= 2: print('selectArrays: write array file %s in mode %d' % (outfile, afmode)) else: if verbose >= 2: print('output file %s already exists' % outfile)
def selectParetoArrays(allarrays, pp): """ Select arrays using a Pareto object """ paretoarrays = oalib.arraylist_t() paretoidx = np.array(pp.allindices()) ww = oalib.longVector(tuple(paretoidx.tolist())) oalib.selectArrays(allarrays, ww, paretoarrays) return paretoarrays
def arraystats(A, verbose=1): """ Return statistics of an array """ Af = A.transpose().flatten() al = oalib.array_link(A.shape[0], A.shape[1], 0) alist = oalib.arraylist_t(1) alist[0] = al # al.array ia = oalib.intArray.frompointer(al.array) for x in range(0, A.size): ia[x] = int(Af[x]) # al.showarray() jresults = oalib.analyseArrays(alist, 0) js = jresults[0] vals = pointer2np(js.vals, js.nc) jvals = getposjvals(A, 3, verbose=0) jv = np.zeros(len(jvals)) c = Counter(abs(vals)) for ii, xx in enumerate(jvals): if c.has_key(xx): jv[ii] = c.get(xx) if verbose: print('Possible j-values: %s' % jvals, end="") print(' values: %s' % jv.astype(int)) N = A.shape[0] Ak = (1 / N ^ 2) * sum(vals**2) print('Ak: %s' % Ak) return Ak
def selectParetoArrays(array_list, pareto_object): """ Select arrays using a Pareto object Args: array_list (list): list of arrays pareto (object): oapackage Pareto object Returns: list: list with all Pareto optimal designs """ paretoarrays = oalib.arraylist_t() paretoidx = np.array(pareto_object.allindices()) ww = oalib.longVector(tuple(paretoidx.tolist())) oalib.selectArrays(array_list, ww, paretoarrays) return paretoarrays
def selectJ(sols0, jj=5, jresults=None, verbose=1): """ Select only arrays with J-characteristics non-zero """ if jresults is None: jresults = oalib.analyseArrays(sols0, verbose, jj) solseo = oalib.arraylist_t() v = [] for ii, js in enumerate(jresults): # js.show() # if not np.array(js.vals)[0]==0: # solseo.append(sols0[ii]) v.append(js.vals[0]) si = [i for (i, j) in sorted(enumerate(v), key=operator.itemgetter(1))] for jj in si: if v[jj] > 0: solseo.append(sols0[jj]) if verbose: print('selectJ: kept %d/%d solutions' % (solseo.size(), sols0.size())) return solseo
def selectJ(sols0, jj=5, jresults=None, verbose=1): """ Select only arrays with J-characteristics non-zero We asssume the designs are in even-odd ordering (i.e. only check the J value of first columns) """ if jresults is None: jresults = oalib.analyseArrays(sols0, verbose, jj) solseo = oalib.arraylist_t() v = [] for ii, js in enumerate(jresults): v.append(js.values[0]) si = [i for (i, j) in sorted(enumerate(v), key=operator.itemgetter(1))] for jj in si: if v[jj] > 0: solseo.append(sols0[jj]) if verbose: print('selectJ: kept %d/%d solutions' % (solseo.size(), len(sols0))) return solseo