def partition_extension_pgl(pgl_file,q,num_blocks,modify_pa=True,use_ILP=False): # Since it is PGL one can add at most 2 new symbols: num_new_sym=2 # New symbols: new_symbols=range(q+1,q+1+num_new_sym) # # Number of symbols: num_symbols=q+1 # Number of parts: num_parts=num_blocks # Number of lines from the PA (asumming in order): end=(num_parts+2)*num_symbols # The +2 is for the freebies pgl_pa=parse_pa(pgl_file,end) # Get the pgl blocks: blocks=get_pgl_blocks(pgl_pa,q,num_parts+2) # Run parallel pe len_out,out,P,Q=parallel_pe_blocks(blocks,num_blocks,num_new_sym,modify_pa=True,use_ILP=use_ILP) print "pa_size:",len_out return len_out,out,P,Q
def main(argv): parser = argparse.ArgumentParser(description='Crete and ILP model for the partitioning problem') parser.add_argument("--in_pa_file", required=False, help='The Permutation Array File') parser.add_argument("--sought_hd", type=int, required=False, help='The Permutation Array File') parser.add_argument("--num_blocks", type=int, required=False, help='The number of blocks to look for') parser.add_argument("--out_pa_file", required=False, help='The filename to save the resulting permutation Array') parser.add_argument("--out_partition_file", required=False, help='The filename to save the patition for permutation Array ') parser.add_argument("--time_limit", required=False, help='Stop solver after these many seconds') parser.add_argument("--use_ceiling_function", action='store_true', required=False, help='When computing the number of blocks, use ceiling instead of floor') parser.add_argument("--in_block_file", required=False, help='The Permutation Array File') parser.add_argument("--representatives", required=False, help='A file containing a list of coset representatives of the group to be extended') # Get the arguments: args = parser.parse_args() # Get the pa from file if args.in_pa_file: # pa=parse_pa(args.in_pa_file) # print pa # Do partition and extension pa_str,p_q_str,pa_len=partition_extension(args.in_pa_file,sought_hd=args.sought_hd,num_blocks=args.num_blocks,ceil=args.use_ceiling_function) print p_q_str # exit() # Save the extended PA if args.out_pa_file: # Save to file write_file(args.out_pa_file,pa_str) # # Save the partition: if args.out_partition_file: # Save to file write_file(args.out_partition_file,p_q_str) if args.out_pa_file is None: print pa_len # Do PE on blocks instead with coset representatives: elif args.in_block_file and args.representatives and args.num_blocks: # Retrieve representatives: reps=parse_repsentatives(args.representatives) # Get the block from file pa=parse_pa(args.in_block_file) # Get cosets: cosets=create_cosets(pa,reps,args.num_blocks) pa_str,p_q_str,pa_len=partition_extension_blocks(cosets) print pa_len
def random_walk(pa_file, d, add_symbol, append_file_name, jump_prob, existing_representatives): if jump_prob == None: jump_prob = 0.7 # Retrieve the Permutation Array pa = parse_pa(pa_file) # print pa if add_symbol: pa = add_symbol_fun(pa) # Number of symbols: num_symbols = len(pa[0]) # Get a random permutation: p = p_not_in_pa(pa, num_symbols) keep_searching = True # Keep track of p_i: p_i = [range(num_symbols)] if existing_representatives is not None: p_i = existing_representatives while keep_searching: # The first pernutation to be found: ckc = qcheck_i(p_i, p, pa) if ckc >= d: print 'ckc', ckc p_i.append(p) print p if append_file_name is not None: append_file(append_file_name, pa2str([p]) + '\n') # Compute a random draw from the uniform distribution draw = random() # Reject: if draw <= jump_prob: p = p_not_in_pa(pa, num_symbols) else: # print 'trans~' for x in range(randint(1, num_symbols / 2)): p = rand_transpositon(p) while p in pa: # print 'trans+' # p=rand_transpositon(p) for x in range(randint(1, num_symbols / 2)): p = rand_transpositon(p) return p_i
def prepare(agl_file, pgl_file, p, q): num_cosets = min(p - 1, q * (q - 1)) # Get this number of AGL cosets: end = (num_cosets) * p # The +1 is for the freeby agl_pa = parse_pa(agl_file, end) agl_blocks = [] for i in xrange(num_cosets): agl_blocks.append(agl_pa[i * p:(i * p + p)]) # Parse all pgl and find blocks pgl_pa = parse_pa(pgl_file, end) # Get the pgl blocks: pgl_blocks = get_pgl_blocks(pgl_pa, q, num_cosets) # print len(pgl_blocks),num_cosets # exit() return agl_blocks, pgl_blocks
def main(argv): parser = argparse.ArgumentParser( description='Crete and ILP model for the partitioning problem') parser.add_argument("--in_pa_file", required=False, help='The Permutation Array File') parser.add_argument( "--sought_hd", type=int, required=False, help='Desired hamming distance for the resulting permutation Array') parser.add_argument( "--out_pa_file", required=False, help='The filename to save the resulting permutation Array') parser.add_argument("--add_symbol", action='store_true', required=False, help='Increase the number of symbols by one') parser.add_argument( "--out_append_perms", required=False, help='A filename to keep track of newly found permutations') parser.add_argument( "--jump_prob", required=False, type=float, help= 'The random probability to change the current permutation and find a new one' ) parser.add_argument( "--existing_representatives_list", required=False, help='Start the search including this list of cosets representatives') print 'Aqui mero' # Get the arguments: args = parser.parse_args() # The append file name: if args.out_append_perms: append_file_name = args.out_append_perms else: append_file_name = None # Parse the coset representatives: existing_representatives = None if args.existing_representatives_list: if os.path.isfile(args.existing_representatives_list): existing_representatives = parse_pa( args.existing_representatives_list) # Get the pa from file elif args.in_pa_file: # Do partition and extension perms = random_walk(args.in_pa_file, d=args.sought_hd, add_symbol=args.add_symbol, append_file_name=append_file_name, jump_prob=args.jump_prob, existing_representatives=existing_representatives) print perms
def main(): # Parse function arguments parser = argparse.ArgumentParser( description='Find controversial comments and save them as seeds') parser.add_argument("--in_pa_file", required=False, help='The Permutation Array File') parser.add_argument("--sought_hd", type=int, required=False, help='The Permutation Array File') parser.add_argument( "--out_pa_file", required=False, help='The filename to save the resulting permutation Array') parser.add_argument( "--out_partition_file", required=False, help='The filename to save the patition for permutation Array ') # Do a list of PAs and compute new bouns parser.add_argument("--in_pa_folder", required=False, help='Folder to find PAs') parser.add_argument('-l', '--l_num_symbols', nargs='+', type=int, help='List of mols to partition and extend') parser.add_argument("--out_extended_pa", required=False, help='Folder to store newly created PAs') parser.add_argument("--out_extended_partitions", required=False, help='Folder to store newly created PAs\' partitions') parser.add_argument( "--max_iter", type=int, required=False, help='Maximum number of random partitions made by the algorithm') parser.add_argument("--in_original_bounds", required=False, help='Original Bounds') parser.add_argument("--out_new_bounds_file", required=False, help='A filename to store the new bounds, if any') parser.add_argument("--num_blocks", type=int, required=False, help='The number of blocks to look for') parser.add_argument("--in_block_file", required=False, help='The Permutation Array File') parser.add_argument( "--representatives", required=False, help= 'A file containing a list of coset representatives of the group to be extended' ) parser.add_argument("--add_symbol", action='store_true', required=False, help='Increase the number of symbols by one') parser.add_argument("--q", type=int, required=False, help='The prime [power] in PGL(1,q)') parser.add_argument("--in_pgl_file", required=False, help='The Permutation Array File from PGL') parser.add_argument( "--use_pgl_blocks", action='store_true', required=False, help='Compute sequential parition and extension on blocks from PGL(2,q)' ) args = parser.parse_args() max_iter = None if args.l_num_symbols and args.in_pa_folder: # Process this list pa_len_dic = list_partition_extension( args.l_num_symbols, in_pa_folder=args.in_pa_folder, out_extended_pa=args.out_extended_pa, out_extended_partitions=args.out_extended_partitions, max_iter=args.max_iter) print pa_len_dic # Compute new bounds and save them to file if any: if args.in_original_bounds and args.out_new_bounds_file and pa_len_dic is not None: # Retrieve the original bounds: orig_bounds = parse_original_bounds(args.in_original_bounds) # Check for new bounds and create a string to save to file new_bounds_str = check_new_bounds(orig_bounds, pa_len_dic) # Save to file: if new_bounds_str is not None: # print args.out_new_bounds_file write_file(args.out_new_bounds_file, new_bounds_str) print 'Done!' # Get the pa from file elif args.in_pa_file: pa = parse_pa(args.in_pa_file) # print pa # Do partition and extension pa_str, p_q_str, pa_len = partition_extension( pa, sought_hd=args.sought_hd, num_blocks=args.num_blocks) # Save the extended PA if args.out_pa_file: # Save to file write_file(args.out_pa_file, pa_str) # # Save the partition: if args.out_partition_file: # Save to file write_file(args.out_partition_file, p_q_str) if args.out_pa_file is None: print pa_len # Do PE on blocks instead with coset representatives: elif args.in_block_file and args.representatives and args.num_blocks: # Retrieve representatives: reps = parse_repsentatives(args.representatives) # Get the block from file pa = parse_pa(args.in_block_file) if args.add_symbol: pa = add_symbol_fun(pa) # Get cosets: cosets = create_cosets(pa, reps, args.num_blocks) pa_str, p_q_str, pa_len = partition_extension_blocks(cosets) # Save the extended PA if args.out_pa_file: # Save to file write_file(args.out_pa_file, pa_str) # # Save the partition: if args.out_partition_file: # Save to file write_file(args.out_partition_file, p_q_str) print pa_len
def partition_extension(pa_file, num_symbols=None, max_iter=5, num_blocks=None): # Num of partitions to make: sqr_sym = int(math.floor(math.sqrt(num_symbols))) # Number of parts: num_parts = min(sqr_sym, num_blocks - 1) # print 'num_parts',num_parts # Number of lines from the PA (asumming in order): end = (num_parts + 1) * num_symbols # The +1 is for the freeby pa = parse_pa(pa_file, end) # Find blocks if num_blocks is not None: blocks = [] elem_per_block = len(pa) / (num_parts + 1) for b_idx in range((num_parts + 1)): sough_dist_block = pa[elem_per_block * b_idx:elem_per_block * b_idx + elem_per_block] blocks.append(sough_dist_block) # New symbol: new_symbol = len(blocks[0]) # Block matrices: mat_blocks = [] # Make the blocks matrices: for x in range(num_parts): mat_blocks.append(np.array(blocks[x])) # Search for the best parition: best_p = None best_q = None best_pa = None best_pa_len = 0 # Create the partition shell P = [[] for x in range(num_parts)] Q = [[] for x in range(num_parts)] # Num elements in each default partition: def_elemnts = int(math.floor(new_symbol / float(num_parts))) # Check if the number of elements is an integer or not is_int = (new_symbol / float(num_parts)).is_integer() # Covered permutations: pa = [] # For when num of symbols is not integer: q_start = 0 q_add = 0 # Add the default part: for x in range(num_parts): P[x].append(x) if True and not is_int: if x % 2 == 0: Q[x] += range(q_start, q_start + (def_elemnts) + 1) q_start = Q[x][-1] + 1 else: Q[x] += range(q_start, q_start + (def_elemnts)) q_start = Q[x][-1] + 1 # print q_start else: Q[x] += range((def_elemnts) * x, (def_elemnts) * x + (def_elemnts)) # Keep track of the number of symbols in the partition Q q_add += len(Q[x]) mat_blocks[x], cov_perm = check_coverage(mat_blocks[x], P[x][0], Q[x]) pa += cov_perm # Make sure tha all symbols are included in the Q partitioning if q_add < new_symbol: # get the missing symbols: Q[0] += range(q_add, new_symbol) # Go over each of the remaining columns (symbols): for x in range(num_parts, new_symbol): # Test which block gets more coverage: tmp_mats = [] tmp_cov_perm = [] len_coverage = [] # Check each block for y in range(len(mat_blocks)): tmp_mat, tmp_perms = check_coverage(mat_blocks[y], x, Q[y]) tmp_mats.append(tmp_mat) tmp_cov_perm.append(tmp_perms) len_coverage.append(len(tmp_perms)) # Get the max block index: mat_idx = len_coverage.index(max(len_coverage)) # Assign this position to P_mat_idx: P[mat_idx].append(x) # Delete the permutations in M once we found the position and values: mat_blocks[mat_idx] = tmp_mats[mat_idx] # Add symbol: ext_perms = add_symbol(tmp_cov_perm[mat_idx], x, new_symbol) # Add the selected block permutations to the pa: pa += ext_perms # Early termination if len(pa) == new_symbol * num_parts: break # Add the freeby block: ext_perms = add_symbol(blocks[-1], new_symbol - 1, new_symbol) pa += ext_perms # Create the partition as a string pa_str = pprint_pa(pa) # Create the p partition and q partition as strings: p_q_str = 'P:' + str(P) + '\n\n' p_q_str += 'Q:' + str(Q) + '\n' # The size of the pa: pa_len = len(pa) return pa_str, p_q_str, pa_len
def main(argv): parser = argparse.ArgumentParser(description='Crete and ILP model for the partitioning problem') parser.add_argument("--in_pa_file", required=False, help='The Permutation Array File, a group') parser.add_argument("--sought_hd", type=int, required=False, help='The Permutation Array File') parser.add_argument("--num_blocks", type=int, required=False, help='The number of blocks to look for') parser.add_argument("--out_pa_file", required=False, help='The filename to save the resulting permutation Array') parser.add_argument("--out_partition_file", required=False, help='The filename to save the patition for permutation Array ') parser.add_argument("--in_partition", required=False, help='A file containing the partion system to be used') parser.add_argument("--representatives", required=False, help='A file containing a list of coset representatives of the group to be extended') parser.add_argument("--num_new_sym", type=int, required=False, help='The number of new symbols to extend to') parser.add_argument("--use_ILP", action='store_true', required=False, help='Use the ILP PE method instead of the greedy algorithm') parser.add_argument("--out_dir", required=False, help='A filename to keep track of current new bounds') parser.add_argument("--q", type=int, required=False, help='The prime [power] in PGL(1,q)') parser.add_argument("--in_pgl_file", required=False, help='The Permutation Array File from PGL') # Get the arguments: args = parser.parse_args() P=None;Q=None; if args.in_partition: P,Q=parse_partition(args.in_partition) # Retrieve coset representatives if args.representatives: reps=parse_repsentatives(args.representatives) else: reps=None # Do partition and extension on the given cosets of group: if args.in_pa_file and reps: # Try this: because we want only a part of it nd_tuple=get_nd(args.in_pa_file) if nd_tuple is not None: n,d=nd_tuple # Get the group, all of it! pa=parse_pa(args.in_pa_file)[:] # Do partition and extension on coset representatives: pa_pe_size,ext_pa,P,Q=partition_extension2(pa,args.num_new_sym,args.num_blocks,reps,P,Q,modify_pa=True) # Do PE on PGL elif args.in_pgl_file and args.q and args.num_blocks: len_out,out,P,Q=partition_extension_pgl(args.in_pgl_file,args.q,args.num_blocks,use_ILP=args.use_ILP) # If a directory is given to save the resulting files: if args.out_dir: pa_fname=join(args.out_dir,str(n+args.num_new_sym)+'_'+str(d+args.num_new_sym)+'.txt') part_fname=join(args.out_dir,str(n+args.num_new_sym)+'_'+str(d+args.num_new_sym)+'_partition.txt') pa_str=pa2str(ext_pa) # Save to file print pa_fname write_file(pa_fname,pa_str) print part_fname # Create the p partition and q partition as strings: p_q_str='P:'+str(P)+'\n' p_q_str+='Q:'+str(Q)+'\n' write_file(part_fname,p_q_str) # Save the extended PA if args.out_pa_file: pa_str=pa2str(ext_pa) # Save to file write_file(args.out_pa_file,pa_str) # # Save the partition: if args.out_partition_file: # Create the p partition and q partition as strings: p_q_str='P:'+str(P)+'\n' p_q_str+='Q:'+str(Q)+'\n' # Save to file write_file(args.out_partition_file,p_q_str)
def main(): # Parse function arguments parser = argparse.ArgumentParser( description= 'Generate q-1 blocks of size q+1 from PGL(2,q) where q is a prime (power)' ) parser.add_argument("--in_pgl_file", required=False, help='The Permutation Array File from PGL') parser.add_argument("--q", type=int, required=False, help='The prime in PGL(1,q)') parser.add_argument("--pwr", type=int, required=False, help='Power to wich q is exponentiated') parser.add_argument("--out_blocks_file", required=False, help='Filename to save the resulting PGL blocks') parser.add_argument("--no_block_separator", dest='use_block_separator', action='store_false', required=False, help='Do NOT include a line of "###" between blocks') parser.set_defaults(use_block_separator=True) parser.set_defaults(pwr=1) args = parser.parse_args() pgl_blocks = None out_str = [] if args.q: q = args.q**args.pwr # # # Parralle partition and extension: if args.in_pgl_file is None and q is not None: print 'Using java group generator' if not compile_java(): print 'Error compiling the group generator. Make sure that GroupGeneration.java is located in the same directory' exit() pgl_pa = get_pgl_java(args.q, args.pwr) if pgl_pa is None: print 'Error generating the group, preferably provide a file with it' exit() elif pgl_pa == -1: print 'Error: q is not a prime or q is a prime power. If q is a prime power, include the exponent i.e. --pwr 3' exit() # Compute the number of blocks to generate num_cosets = q * ((q) - 1) # Get the pgl blocks: pgl_blocks = get_pgl_blocks(pgl_pa, q, num_cosets) elif q: if not isfile(args.in_pgl_file): print '"' + args.in_pgl_file + '"', 'not found' exit() pgl_pa = parse_pa(args.in_pgl_file) num_cosets = q * ((q) - 1) # Get the pgl blocks: pgl_blocks = get_pgl_blocks(pgl_pa, q, num_cosets) # Generate output: if pgl_blocks is not None: for block in pgl_blocks: out_str.append(pa2str(block)) # Separate blocks: if args.use_block_separator: out_str.append('###') if args.use_block_separator: out_str.pop() # Save the extended PA if args.out_blocks_file and pgl_blocks is not None: # Save to file write_file(args.out_blocks_file, '\n'.join(out_str)) # print elif out_str: print '\n'.join(out_str) else: print 'No File processed or blocks were generated!'
def prepare(pa_file,num_symbols=0,num_blocks=0,ceil=False): # Num of partitions to make: if ceil: sqr_sym=int(math.ceil(math.sqrt(num_symbols))) else: sqr_sym=int(math.floor(math.sqrt(num_symbols))) # Number of parts: num_parts=min(sqr_sym,num_blocks-1) # Number of lines from the PA (asumming in order): end=(num_parts+1)*num_symbols # The +1 is for the freeby pa=parse_pa(pa_file,end) elem_per_block=len(pa)/(num_parts+1) print elem_per_block # AGL blocks: if len(pa)%float(num_symbols)==0: print 'AGL blocks' elem_per_block=num_symbols print 'elem_per_block',elem_per_block # exit() # Find blocks if num_blocks is not None: blocks=[] for b_idx in range((num_parts+1)): sough_dist_block=pa[elem_per_block*b_idx:elem_per_block*b_idx+elem_per_block] # print len(sough_dist_block) # print 'b_idx',b_idx,hd_pairwise(sough_dist_block) blocks.append(sough_dist_block) # New symbol: new_symbol=len(blocks[0][0]) # The freeby block: freeby=blocks[-1] # Get only the blocks that will actually be used blocks=blocks[0:num_parts] # For when num of symbols is not integer: q_start=0 q_add=0 # Num of permutations covered with the arbitrary selection: start_coverage=0 # Create the partition shell P=[[] for x in range(num_parts)] Q=[[] for x in range(num_parts)] # Num elements in each default partition: def_elemnts=int(math.floor(new_symbol/float(num_parts))) # Check if the number of elements is an integer or not is_int=(new_symbol/float(num_parts)).is_integer() # Add the default part: for x in range(num_parts): # P[x].append(x) if True and not is_int: if x%2==0: Q[x]+=range(q_start,q_start+(def_elemnts)+1) q_start=Q[x][-1]+1 else: Q[x]+=range(q_start,q_start+(def_elemnts)) q_start=Q[x][-1]+1 # print q_start else: Q[x]+=range((def_elemnts)*x,(def_elemnts)*x+(def_elemnts)) # Keep track of the number of symbols in the partition Q q_add+=len(Q[x]) print P,Q return blocks,freeby,Q,P,new_symbol