def compare_elementwise(jac, J, con_ids, var_ids): def index_of(n): return int(n[1:]) print(jac.shape) for i, cidx in enumerate(imap(index_of, con_ids)): for j, vidx in enumerate(imap(index_of, var_ids)): assert_close(i, j, cidx, vidx, jac, J)
def get_reachable_node_set(g, sources): # Reverse the edges, and run a DFS from each source, remove duplicate nodes. with nx_reversed(g): # function, when invoked with a node n as an argument, it returns a # generator of reachable nodes from n reachable_from = partial(dfs_preorder_nodes, g) return set(chain.from_iterable(imap(reachable_from, sources)))
def parse_nl_header(header): assert len(header) == 10, 'See expected format above in comment' header = list(imap(to_ints, header[1:])) # <- discarding the first line! n_vars, n_cons, n_objs = header[0][:3] nzeros = header[6][0] if any(header[5]): warning('The model has discrete variables but we treat them as continuous!') return Header(n_vars=n_vars, n_cons=n_cons, n_nzeros=nzeros, n_objs=n_objs)
def get_def_var_names(probname): lines = get_lines(nl_fname(probname)) def to_index_name(line): if line[0] == 'V': # "Vi j k #name" -> i as int index = int(line[1:].split(None, 1)[0]) # "Vi j k #name" -> name name = line.rsplit('#', 1)[1].strip() # Needs: option nl_comments 1; return index, name return None return OrderedDict(i_name for i_name in imap(to_index_name, lines) if i_name)
def to_ints(line): # '1 2' -> [1, 2] entries = line.split() return list(imap(int, entries))
def remove_comments(lines): # 'v1 #x[6]' -> 'v1' return list(imap(lambda l: l.rsplit('#', 1)[0].rstrip(), lines))
def parse_blockids(segment): return [(i, j) for i, j in imap(to_ints, segment)]
def parse_linear_part(segment): indices, coeffs = [ ], [ ] for i, c in imap(lambda l: l.split(), segment): indices.append(int(i)) # make the indices integers coeffs.append(c) # but keep the coefficients as strings return indices, coeffs
def k_segment(_header, segment, allsegs): allsegs.con_jacobian.extend(imap(int, segment))
def x_segment(_header, segment, allsegs): allsegs.initial_guess.update(imap(to_index_value, segment))
def b_segment(_header, segment, allsegs): allsegs.var_bnds.extend(imap(to_range, segment))
def r_segment(_header, segment, allsegs): cons = allsegs.cons for i, rng in enumerate(imap(to_range, segment)): nl_part, lin_part, _None = cons[i] cons[i] = (nl_part, lin_part, rng)
def add_numbers(g, args, n_counter): # replace n-1.23 with n42 where 42 is a unique id, else just return arg return tuple(imap(partial(add_if_num, g, n_counter), args))