def convex_cardinality_relaxed(f, epsilon=<NUM_LIT>):
def convex_cardinality_wrapper(*args, **kwargs):<EOL><INDENT>def dict_result(r):<EOL><INDENT>if isinstance(r, tuple):<EOL><INDENT>return dict(r[<NUM_LIT:0>])<EOL><DEDENT>return dict(r)<EOL><DEDENT>full_result = f(*args, **kwargs)<EOL>result = dict_result(full_result)<EOL>def update_weight(value):<EOL><INDENT>return <NUM_LIT:1>/(epsilon + abs(value))<EOL><DEDENT>while True:<EOL><INDENT>weights = {identifier: update_weight(value)<EOL>for identifier, value in iteritems(result)}<EOL>kwargs['<STR_LIT>'] = weights<EOL>last_result = result<EOL>full_result = f(*args, **kwargs)<EOL>result = dict_result(full_result)<EOL>delta = math.sqrt(sum(pow(value - last_result[identifier], <NUM_LIT:2>)<EOL>for identifier, value in iteritems(result)))<EOL>if delta < epsilon:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if isinstance(full_result, tuple):<EOL><INDENT>return (iteritems(result),) + full_result[<NUM_LIT:1>:]<EOL><DEDENT>return iteritems(result)<EOL><DEDENT>return convex_cardinality_wrapper<EOL>
Transform L1-norm optimization function into cardinality optimization.

    The given function must optimize a convex problem with
    a weighted L1-norm as the objective. The transformed function
    will apply the iterated weighted L1 heuristic to approximately
    optimize the cardinality of the solution. This method is
    described by S. Boyd, "L1-norm norm methods for convex cardinality
    problems." Lecture Notes for EE364b, Stanford University, 2007.
    Available online at www.stanford.edu/class/ee364b/.

    The given function must take an optional keyword parameter weights
    (dictionary), and the weights must be set to one if not specified.
    The function must return the non-weighted solution as an iterator
    over (identifier, value)-tuples, either directly or as the first
    element of a tuple.