Exemplo n.º 1
0
def cooperative_tradeoff(community, min_growth, fraction, fluxes, pfba):
    """Find the best tradeoff between community and individual growth."""
    with community as com:
        check_modification(community)
        min_growth = _format_min_growth(min_growth, community.species)
        _apply_min_growth(community, min_growth)

        com.objective = 1000.0 * com.variables.community_objective
        min_growth = optimize_with_retry(
            com, message="could not get community growth rate.") / 1000.0

        if not isinstance(fraction, Sized):
            fraction = [fraction]
        else:
            fraction = np.sort(fraction)[::-1]

        # Add needed variables etc.
        regularize_l2_norm(com, 0.0)
        results = []
        for fr in fraction:
            com.variables.community_objective.lb = fr * min_growth
            com.variables.community_objective.ub = min_growth
            sol = solve(community, fluxes=fluxes, pfba=pfba)
            if sol.status != OPTIMAL:
                sol = crossover(com, sol, fluxes=fluxes, pfba=pfba)
            results.append((fr, sol))
        if len(results) == 1:
            return results[0][1]
        return pd.DataFrame.from_records(results,
                                         columns=["tradeoff", "solution"])
Exemplo n.º 2
0
def cooperative_tradeoff(community, min_growth, fraction, fluxes, pfba):
    """Find the best tradeoff between community and individual growth."""
    with community as com:
        solver = interface_to_str(community.problem)
        check_modification(community)
        min_growth = _format_min_growth(min_growth, community.taxa)
        _apply_min_growth(community, min_growth)

        com.objective = com.scale * com.variables.community_objective
        min_growth = (optimize_with_retry(
            com, message="could not get community growth rate.") / com.scale)
        if not isinstance(fraction, Sized):
            fraction = [fraction]
        else:
            fraction = np.sort(fraction)[::-1]

        # Add needed variables etc.
        regularize_l2_norm(com, 0.0)
        results = []
        for fr in fraction:
            com.variables.community_objective.lb = fr * min_growth
            com.variables.community_objective.ub = min_growth
            sol = solve(community, fluxes=fluxes, pfba=pfba)
            # OSQP is better with QPs then LPs
            # so it won't get better with the crossover
            if (sol.status != OPTIMAL and solver != "osqp"):
                sol = crossover(com, sol, fluxes=fluxes, pfba=pfba)
            results.append((fr, sol))
        if len(results) == 1:
            return results[0][1]
        return pd.DataFrame.from_records(results,
                                         columns=["tradeoff", "solution"])
Exemplo n.º 3
0
def test_crossover(community):
    sol = community.cooperative_tradeoff(fraction=1.0)
    gcs = sol.members.growth_rate.dropna()
    sol = ms.crossover(community, sol)
    for i, g in enumerate(gcs):
        sol.members.growth_rate[i] == approx(g)