def random_sparse(strategy, prob, obj_reaction, flux_threshold):
essential = set()<EOL>deleted = set()<EOL>for entity, deleted_reactions in strategy.iter_tests():<EOL><INDENT>if obj_reaction in deleted_reactions:<EOL><INDENT>logger.info(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(entity))<EOL>essential.add(entity)<EOL>continue<EOL><DEDENT>if len(deleted_reactions) == <NUM_LIT:0>:<EOL><INDENT>logger.info(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(entity))<EOL>deleted.add(entity)<EOL>strategy.delete(entity, deleted_reactions)<EOL>continue<EOL><DEDENT>logger.info('<STR_LIT>'.format(<EOL>'<STR_LIT:U+002CU+0020>'.join(deleted_reactions)))<EOL>constr = []<EOL>for r in deleted_reactions:<EOL><INDENT>flux_var = prob.get_flux_var(r)<EOL>c, = prob.prob.add_linear_constraints(flux_var == <NUM_LIT:0>)<EOL>constr.append(c)<EOL><DEDENT>logger.info('<STR_LIT>'.format(<EOL>'<STR_LIT:U+002CU+0020>'.join(deleted_reactions)))<EOL>try:<EOL><INDENT>prob.maximize(obj_reaction)<EOL><DEDENT>except fluxanalysis.FluxBalanceError:<EOL><INDENT>logger.info(<EOL>'<STR_LIT>'.format(<EOL>entity))<EOL>for c in constr:<EOL><INDENT>c.delete()<EOL><DEDENT>essential.add(entity)<EOL>continue<EOL><DEDENT>logger.debug('<STR_LIT>'.format(<EOL>obj_reaction, prob.get_flux(obj_reaction)))<EOL>if prob.get_flux(obj_reaction) < flux_threshold:<EOL><INDENT>for c in constr:<EOL><INDENT>c.delete()<EOL><DEDENT>essential.add(entity)<EOL>logger.info('<STR_LIT>'.format(<EOL>entity))<EOL><DEDENT>else:<EOL><INDENT>deleted.add(entity)<EOL>strategy.delete(entity, deleted_reactions)<EOL>logger.info('<STR_LIT>'.format(entity))<EOL><DEDENT><DEDENT>return essential, deleted<EOL>
Find a random minimal network of model reactions.

    Given a reaction to optimize and a threshold, delete entities randomly
    until the flux of the reaction to optimize falls under the threshold.
    Keep deleting until no more entities can be deleted. It works
    with two strategies: deleting reactions or deleting genes (reactions
    related to certain genes).

    Args:
        strategy: :class:`.ReactionDeletionStrategy` or
            :class:`.GeneDeletionStrategy`.
        prob: :class:`psamm.fluxanalysis.FluxBalanceProblem`.
        obj_reaction: objective reactions to optimize.
        flux_threshold: threshold of max reaction flux.