跳转到主要内容

PySAL中的空间优化

项目描述

spopt: 空间优化

区域化、设施定位和以交通为导向的建模

tag Continuous Integration codecov Documentation License Ruff status DOI Discord

Spopt是一个开源的Python库,用于解决空间数据的优化问题。起源于PySAL (Python空间分析库)中的region模块,它正在积极开发,以包含区域化、设施定位和以交通为导向的解决方案的新提出的模型和方法。

区域化

import spopt, libpysal, geopandas, numpy
mexico = geopandas.read_file(libpysal.examples.get_path("mexicojoin.shp"))
mexico["count"] = 1
attrs = [f"PCGDP{year}" for year in range(1950, 2010, 10)]
w = libpysal.weights.Queen.from_dataframe(mexico)
mexico["count"], threshold_name, threshold, top_n = 1, "count", 4, 2
numpy.random.seed(123456)
model = spopt.region.MaxPHeuristic(mexico, w, attrs, threshold_name, threshold, top_n)
model.solve()
mexico["maxp_new"] = model.labels_
mexico.plot(column="maxp_new", categorical=True, figsize=(12,8), ec="w");

定位

from spopt.locate import MCLP
from spopt.locate.util import simulated_geo_points
import numpy, geopandas, pulp, spaghetti

solver = pulp.PULP_CBC_CMD(msg=False, warmStart=True)
lattice = spaghetti.regular_lattice((0, 0, 10, 10), 9, exterior=True)
ntw = spaghetti.Network(in_data=lattice)
street = spaghetti.element_as_gdf(ntw, arcs=True)
street_buffered = geopandas.GeoDataFrame(
    geopandas.GeoSeries(street["geometry"].buffer(0.5).unary_union),
    crs=street.crs,
    columns=["geometry"],
)
client_points = simulated_geo_points(street_buffered, needed=100, seed=5)
ntw.snapobservations(client_points, "clients", attribute=True)
clients_snapped = spaghetti.element_as_gdf(
    ntw, pp_name="clients", snapped=True
)
facility_points = simulated_geo_points(street_buffered, needed=10, seed=6)
ntw.snapobservations(facility_points, "facilities", attribute=True)
facilities_snapped = spaghetti.element_as_gdf(
    ntw, pp_name="facilities", snapped=True
)
cost_matrix = ntw.allneighbordistances(
    sourcepattern=ntw.pointpatterns["clients"],
    destpattern=ntw.pointpatterns["facilities"],
)
numpy.random.seed(0)
ai = numpy.random.randint(1, 12, 100)
mclp_from_cost_matrix = MCLP.from_cost_matrix(cost_matrix, ai, 4, p_facilities=4)
mclp_from_cost_matrix = mclp_from_cost_matrix.solve(solver)

查看笔记本中的绘图代码

示例

更多示例可以在文档的教程部分找到。

所有示例都可以通过将此存储库作为Binder启动来交互式运行。

要求

安装

spopt可在Python包索引上找到。因此,您可以直接通过命令行使用pip安装

$ pip install -U spopt

或者下载源代码包(.tar.gz)并将其解压缩到您选择的目录。打开命令行工具并导航到解压缩的文件夹。输入

$ pip install .

您还可以通过运行

$ conda install --channel conda-forge spopt

相关包

贡献

PySAL-spopt处于积极开发中,欢迎贡献者。

如果您有任何建议、功能请求或错误报告,请在新GitHub问题中提出。要提交补丁,请在PySAL开发者文档、PySAL 开发指南spopt 贡献指南中查看,然后提交pull request。一旦您的更改被合并,您将自动被添加到贡献者列表中。

支持

如果您遇到问题,请创建问题开始讨论或在我们PySAL的Discord频道中与我们交谈。

行为准则

作为PySAL联盟项目,spopt遵循行为准则,该准则位于PySAL治理模型下。

许可证

本项目采用BSD 3-Clause许可

引用

如果您在科学出版物中使用PySAL-spopt,我们将感谢您使用以下引用

@misc{spopt2021,
    author    = {Feng, Xin, and Gaboardi, James D. and Knaap, Elijah and
                Rey, Sergio J. and Wei, Ran},
    month     = {jan},
    year      = {2021},
    title     = {pysal/spopt},
    url       = {https://github.com/pysal/spopt},
    doi       = {10.5281/zenodo.4444156},
    keywords  = {python,regionalization,spatial-optimization,location-modeling}
}

@article{spopt2022,
    author    = {Feng, Xin and Barcelos, Germano and Gaboardi, James D. and
                Knaap, Elijah and Wei, Ran and Wolf, Levi J. and
                Zhao, Qunshan and Rey, Sergio J.},
    year      = {2022},
    title     = {spopt: a python package for solving spatial optimization problems in PySAL},
    journal   = {Journal of Open Source Software},
    publisher = {The Open Journal},
    volume    = {7},
    number    = {74},
    pages     = {3330},
    url       = {https://doi.org/10.21105/joss.03330},
    doi       = {10.21105/joss.03330},
}

资金

本项目部分资金通过以下方式获得

国家科学基金会奖号#1831615: RIDIR: Scalable Geospatial Analytics for Social Science Research

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码包

spopt-0.6.1.tar.gz (30.5 MB 查看哈希值)

上传时间 源代码

构建发行版

spopt-0.6.1-py3-none-any.whl (243.1 kB 查看哈希值)

上传时间 Python 3

由以下支持