remove pyomo dependency in cluster network, use scip as OS solver

This commit is contained in:
Fabian 2024-01-29 12:08:56 +01:00
parent 06c853d7f9
commit 81e7c4eb67
2 changed files with 16 additions and 28 deletions

View File

@ -36,7 +36,7 @@ dependencies:
- networkx - networkx
- scipy - scipy
- shapely>=2.0 - shapely>=2.0
- pyomo - scipopt
- matplotlib - matplotlib
- proj - proj
- fiona - fiona
@ -47,7 +47,6 @@ dependencies:
- tabula-py - tabula-py
- pyxlsb - pyxlsb
- graphviz - graphviz
- ipopt
# Keep in conda environment when calling ipython # Keep in conda environment when calling ipython
- ipython - ipython

View File

@ -126,10 +126,10 @@ import warnings
from functools import reduce from functools import reduce
import geopandas as gpd import geopandas as gpd
import linopy
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import pyomo.environ as po
import pypsa import pypsa
import seaborn as sns import seaborn as sns
from _helpers import configure_logging, update_p_nom_max from _helpers import configure_logging, update_p_nom_max
@ -214,7 +214,7 @@ def get_feature_for_hac(n, buses_i=None, feature=None):
return feature_data return feature_data
def distribute_clusters(n, n_clusters, focus_weights=None, solver_name="cbc"): def distribute_clusters(n, n_clusters, focus_weights=None, solver_name="scip"):
""" """
Determine the number of clusters per country. Determine the number of clusters per country.
""" """
@ -254,31 +254,20 @@ def distribute_clusters(n, n_clusters, focus_weights=None, solver_name="cbc"):
L.sum(), 1.0, rtol=1e-3 L.sum(), 1.0, rtol=1e-3
), f"Country weights L must sum up to 1.0 when distributing clusters. Is {L.sum()}." ), f"Country weights L must sum up to 1.0 when distributing clusters. Is {L.sum()}."
m = po.ConcreteModel() m = linopy.Model()
clusters = m.add_variables(
def n_bounds(model, *n_id): lower=1, upper=N, coords=[L.index], name="n", integer=True
return (1, N[n_id])
m.n = po.Var(list(L.index), bounds=n_bounds, domain=po.Integers)
m.tot = po.Constraint(expr=(po.summation(m.n) == n_clusters))
m.objective = po.Objective(
expr=sum((m.n[i] - L.loc[i] * n_clusters) ** 2 for i in L.index),
sense=po.minimize,
) )
m.add_constraints(clusters.sum() == n_clusters, name="tot")
opt = po.SolverFactory(solver_name) m.objective = (
if solver_name == "appsi_highs" or not opt.has_capability("quadratic_objective"): clusters * clusters - 2 * clusters * L * n_clusters
logger.warning( ) # + (L * n_clusters) ** 2 (constant)
f"The configured solver `{solver_name}` does not support quadratic objectives. Falling back to `ipopt`." if solver_name == "gurobi":
) logging.getLogger("gurobipy").propagate = False
opt = po.SolverFactory("ipopt") else:
solver_name = "scip"
results = opt.solve(m) m.solve(solver_name=solver_name)
assert ( return m.solution["n"].to_series().astype(int)
results["Solver"][0]["Status"] == "ok"
), f"Solver returned non-optimally: {results}"
return pd.Series(m.n.get_values(), index=L.index).round().astype(int)
def busmap_for_n_clusters( def busmap_for_n_clusters(