def get_ext_one_ports_i(n, per_carrier=True): "Get a pd.Multiindex for all extendable branches in the network." check_carriers(n) comps = n.one_port_components & set(nominal_attrs) if per_carrier: return pd.MultiIndex.from_frame( pd.concat( n.df(c).loc[get_extendable_i(n, c), ['bus', 'carrier']] for c in comps)) return pd.Index( sum(([(c, i) for i in get_extendable_i(n, c)] for c in comps), []))
def objective_constant(n): nom_attr = nominal_attrs.items() constant = 0 for c, attr in nom_attr: ext_i = get_extendable_i(n, c) constant += n.df(c)[attr][ext_i] @ n.df(c).capital_cost[ext_i] return constant
def define_mga_constraint(n, sns, epsilon=None, with_fix=None): """Build constraint defining near-optimal feasible space Parameters ---------- n : pypsa.Network sns : Series|list-like snapshots epsilon : float, optional Allowed added cost compared to least-cost solution, by default None with_fix : bool, optional Calculation of allowed cost penalty should include cost of non-extendable components, by default None """ if epsilon is None: epsilon = float(snakemake.wildcards.epsilon) if with_fix is None: with_fix = snakemake.config.get("include_non_extendable", True) expr = [] # operation for c, attr in lookup.query("marginal_cost").index: cost = (get_as_dense(n, c, "marginal_cost", sns).loc[:, lambda ds: (ds != 0).all()].mul( n.snapshot_weightings[sns], axis=0)) if cost.empty: continue expr.append( linexpr((cost, get_var(n, c, attr).loc[sns, cost.columns])).stack()) # investment for c, attr in nominal_attrs.items(): cost = n.df(c)["capital_cost"][get_extendable_i(n, c)] if cost.empty: continue expr.append(linexpr((cost, get_var(n, c, attr)[cost.index]))) lhs = pd.concat(expr).sum() if with_fix: ext_const = objective_constant(n, ext=True, nonext=False) nonext_const = objective_constant(n, ext=False, nonext=True) rhs = (1 + epsilon) * (n.objective + ext_const + nonext_const) - nonext_const else: ext_const = objective_constant(n) rhs = (1 + epsilon) * (n.objective + ext_const) define_constraints(n, lhs, "<=", rhs, "GlobalConstraint", "mu_epsilon")
def objective_constant(n, ext=True, nonext=True): """[summary] """ if not (ext or nonext): return 0.0 constant = 0.0 for c, attr in nominal_attrs.items(): i = pd.Index([]) if ext: i = i.append(get_extendable_i(n, c)) if nonext: i = i.append(get_non_extendable_i(n, c)) constant += n.df(c)[attr][i] @ n.df(c).capital_cost[i] return constant
def define_mga_constraint(n, sns): epsilon = float(snakemake.wildcards.epsilon) expr = [] # operation for c, attr in lookup.query("marginal_cost").index: cost = (get_as_dense(n, c, "marginal_cost", sns).loc[:, lambda ds: (ds != 0).all()].mul( n.snapshot_weightings[sns], axis=0)) if cost.empty: continue expr.append( linexpr((cost, get_var(n, c, attr).loc[sns, cost.columns])).stack()) # investment for c, attr in nominal_attrs.items(): cost = n.df(c)["capital_cost"][get_extendable_i(n, c)] if cost.empty: continue expr.append(linexpr((cost, get_var(n, c, attr)[cost.index]))) lhs = pd.concat(expr).sum() if snakemake.config["include_non_extendable"]: ext_const = objective_constant(n, ext=True, nonext=False) nonext_const = objective_constant(n, ext=False, nonext=True) rhs = (1 + epsilon) * (n.objective + ext_const + nonext_const) - nonext_const else: ext_const = objective_constant(n) rhs = (1 + epsilon) * (n.objective + ext_const) define_constraints(n, lhs, "<=", rhs, "GlobalConstraint", "mu_epsilon")
def get_ext_branches_i(n, branch_components=None): "Get a pd.Multiindex for all extendable branches in the network." branch_components = check_branch_comps(branch_components, n) return pd.Index( sum(([(c, i) for i in get_extendable_i(n, c)] for c in branch_components), []))