spopt


Namespopt JSON
Version 0.6.0 PyPI version JSON
download
home_page
SummarySpatial Optimization in PySAL
upload_time2023-12-10 20:27:24
maintainerspopt contributors
docs_urlNone
author
requires_python>=3.10
licenseBSD 3-Clause
keywords spatial optimization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
<p align="center">
<img src="docs/_static/images/pysal_banner.svg" width="370" height="200" />
</p>

# `spopt`: Spatial Optimization

#### Regionalization, facility location, and transportation-oriented modeling

![tag](https://img.shields.io/github/v/release/pysal/spopt?include_prereleases&sort=semver)
[![Continuous Integration](https://github.com/pysal/spopt/actions/workflows/testing.yml/badge.svg)](https://github.com/pysal/spopt/actions/workflows/testing.yml)
[![codecov](https://codecov.io/gh/pysal/spopt/branch/main/graph/badge.svg)](https://codecov.io/gh/pysal/spopt)
[![Documentation](https://img.shields.io/static/v1.svg?label=docs&message=current&color=9cf)](http://pysal.org/spopt/)
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![status](https://joss.theoj.org/papers/1413cf2c0cf3c561386949f2e1208563/status.svg)](https://joss.theoj.org/papers/1413cf2c0cf3c561386949f2e1208563)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4444156.svg)](https://doi.org/10.5281/zenodo.4444156)

Spopt is an open-source Python library for solving optimization problems with spatial data. Originating from the `region` module in [PySAL (Python Spatial Analysis Library)](http://pysal.org), it is under active development for the inclusion of newly proposed models and methods for regionalization, facility location, and transportation-oriented solutions. 

### Regionalization

```python
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");
```
<p align="center">
<img src="docs/_static/images/maxp.svg" height="350" />
</p>

### Locate
```python
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)
```
*see [notebook](https://github.com/pysal/spopt/blob/main/notebooks/mclp.ipynb) for plotting code*
<p align="center">
<img src="docs/_static/images/mclp.png" height="350" />
</p>

## Examples
More examples can be found in the [Tutorials](https://pysal.org/spopt/tutorials.html) section of the documentation.
- [Max-p-regions problem](https://pysal.org/spopt/notebooks/maxp.html)
- [Skater](https://pysal.org/spopt/notebooks/skater.html)
- [Region K means](https://pysal.org/spopt/notebooks/reg-k-means.html)
- [Facility Location Real World Problem](https://pysal.org/spopt/notebooks/facloc-real-world.html)

All examples can be run interactively by launching this repository as a [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/pysal/spopt/main).

## Requirements
- [scipy](http://scipy.github.io/devdocs/)
- [numpy](https://numpy.org/devdocs/)
- [pandas](https://pandas.pydata.org/docs/)
- [networkx](https://networkx.org/)
- [libpysal](https://pysal.org/libpysal/)
- [scikit-learn](https://scikit-learn.org/stable/)
- [geopandas](https://geopandas.org/)
- [pulp](https://coin-or.github.io/pulp/)
- [shapely](https://shapely.readthedocs.io/en/stable/)
- [spaghetti](https://github.com/pysal/spaghetti)

## Installation
spopt is available on the [Python Package Index](https://pypi.org/). Therefore, you can either install directly with pip from the command line:
```
$ pip install -U spopt
```
or download the source distribution (.tar.gz) and decompress it to your selected destination. Open a command shell and navigate to the decompressed folder. Type:
```
$ pip install .
```
You may also install the latest stable spopt via conda-forge channel by running:
```
$ conda install --channel conda-forge spopt
```

## Related packages

* Region
* Locate
  * [`allagash`](https://github.com/apulverizer/allagash)
  * [`maximum-coverage-location`](https://github.com/cyang-kth/maximum-coverage-location)
  * [`p-center`](https://github.com/antoniomedrano/p-center)
  * [`pyspatialopt`](https://github.com/apulverizer/pyspatialopt)

## Contribute

PySAL-spopt is under active development and contributors are welcome.

If you have any suggestions, feature requests, or bug reports, please open new [issues](https://github.com/pysal/spopt/issues) on GitHub. To submit patches, please review [PySAL's documentation for developers](https://pysal.org/docs/devs/), the PySAL [development guidelines](https://github.com/pysal/pysal/wiki), the `spopt` [contributing guidelines](https://github.com/pysal/spopt/blob/main/.github/CONTRIBUTING.md) before  opening a [pull request](https://github.com/pysal/spopt/pulls). Once your changes get merged, you’ll automatically be added to the [Contributors List](https://github.com/pysal/spopt/graphs/contributors).

## Support
If you are having trouble, please [create an issue](https://github.com/pysal/spopt/issues), [start a discussion](https://github.com/pysal/spopt/discussions), or talk to us in the [gitter room](https://gitter.im/pysal/spopt).

## Code of Conduct

As a PySAL-federated project, `spopt` follows the [Code of Conduct](https://github.com/pysal/governance/blob/main/conduct/code_of_conduct.rst) under the [PySAL governance model](https://github.com/pysal/governance).

## License

The project is licensed under the [BSD 3-Clause license](https://github.com/pysal/spopt/blob/main/LICENSE.txt).

## Citation

If you use PySAL-spopt in a scientific publication, we would appreciate using the following citations:

```
@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},
}
```

## Funding

This project is/was partially funded through:

[<img align="middle" src="docs/_static/images/nsf_logo.png" width="75">](https://www.nsf.gov/index.jsp) National Science Foundation Award #1831615: [RIDIR: Scalable Geospatial Analytics for Social Science Research](https://www.nsf.gov/awardsearch/showAward?AWD_ID=1831615)

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "spopt",
    "maintainer": "spopt contributors",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "spatial optimization",
    "author": "",
    "author_email": "\"James D. Gaboardi\" <jgaboardi@gmail.com>, Xin Feng <xin.feng@ucr.edu>",
    "download_url": "https://files.pythonhosted.org/packages/b8/d1/e90d010c09c7d1a32e8ccb2e80108c55605d226bfdacf42061c62e90500c/spopt-0.6.0.tar.gz",
    "platform": null,
    "description": "\n<p align=\"center\">\n<img src=\"docs/_static/images/pysal_banner.svg\" width=\"370\" height=\"200\" />\n</p>\n\n# `spopt`: Spatial Optimization\n\n#### Regionalization, facility location, and transportation-oriented modeling\n\n![tag](https://img.shields.io/github/v/release/pysal/spopt?include_prereleases&sort=semver)\n[![Continuous Integration](https://github.com/pysal/spopt/actions/workflows/testing.yml/badge.svg)](https://github.com/pysal/spopt/actions/workflows/testing.yml)\n[![codecov](https://codecov.io/gh/pysal/spopt/branch/main/graph/badge.svg)](https://codecov.io/gh/pysal/spopt)\n[![Documentation](https://img.shields.io/static/v1.svg?label=docs&message=current&color=9cf)](http://pysal.org/spopt/)\n[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![status](https://joss.theoj.org/papers/1413cf2c0cf3c561386949f2e1208563/status.svg)](https://joss.theoj.org/papers/1413cf2c0cf3c561386949f2e1208563)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4444156.svg)](https://doi.org/10.5281/zenodo.4444156)\n\nSpopt is an open-source Python library for solving optimization problems with spatial data. Originating from the `region` module in [PySAL (Python Spatial Analysis Library)](http://pysal.org), it is under active development for the inclusion of newly proposed models and methods for regionalization, facility location, and transportation-oriented solutions. \n\n### Regionalization\n\n```python\nimport spopt, libpysal, geopandas, numpy\nmexico = geopandas.read_file(libpysal.examples.get_path(\"mexicojoin.shp\"))\nmexico[\"count\"] = 1\nattrs = [f\"PCGDP{year}\" for year in range(1950, 2010, 10)]\nw = libpysal.weights.Queen.from_dataframe(mexico)\nmexico[\"count\"], threshold_name, threshold, top_n = 1, \"count\", 4, 2\nnumpy.random.seed(123456)\nmodel = spopt.region.MaxPHeuristic(mexico, w, attrs, threshold_name, threshold, top_n)\nmodel.solve()\nmexico[\"maxp_new\"] = model.labels_\nmexico.plot(column=\"maxp_new\", categorical=True, figsize=(12,8), ec=\"w\");\n```\n<p align=\"center\">\n<img src=\"docs/_static/images/maxp.svg\" height=\"350\" />\n</p>\n\n### Locate\n```python\nfrom spopt.locate import MCLP\nfrom spopt.locate.util import simulated_geo_points\nimport numpy, geopandas, pulp, spaghetti\n\nsolver = pulp.PULP_CBC_CMD(msg=False, warmStart=True)\nlattice = spaghetti.regular_lattice((0, 0, 10, 10), 9, exterior=True)\nntw = spaghetti.Network(in_data=lattice)\nstreet = spaghetti.element_as_gdf(ntw, arcs=True)\nstreet_buffered = geopandas.GeoDataFrame(\n    geopandas.GeoSeries(street[\"geometry\"].buffer(0.5).unary_union),\n    crs=street.crs,\n    columns=[\"geometry\"],\n)\nclient_points = simulated_geo_points(street_buffered, needed=100, seed=5)\nntw.snapobservations(client_points, \"clients\", attribute=True)\nclients_snapped = spaghetti.element_as_gdf(\n    ntw, pp_name=\"clients\", snapped=True\n)\nfacility_points = simulated_geo_points(street_buffered, needed=10, seed=6)\nntw.snapobservations(facility_points, \"facilities\", attribute=True)\nfacilities_snapped = spaghetti.element_as_gdf(\n    ntw, pp_name=\"facilities\", snapped=True\n)\ncost_matrix = ntw.allneighbordistances(\n    sourcepattern=ntw.pointpatterns[\"clients\"],\n    destpattern=ntw.pointpatterns[\"facilities\"],\n)\nnumpy.random.seed(0)\nai = numpy.random.randint(1, 12, 100)\nmclp_from_cost_matrix = MCLP.from_cost_matrix(cost_matrix, ai, 4, p_facilities=4)\nmclp_from_cost_matrix = mclp_from_cost_matrix.solve(solver)\n```\n*see [notebook](https://github.com/pysal/spopt/blob/main/notebooks/mclp.ipynb) for plotting code*\n<p align=\"center\">\n<img src=\"docs/_static/images/mclp.png\" height=\"350\" />\n</p>\n\n## Examples\nMore examples can be found in the [Tutorials](https://pysal.org/spopt/tutorials.html) section of the documentation.\n- [Max-p-regions problem](https://pysal.org/spopt/notebooks/maxp.html)\n- [Skater](https://pysal.org/spopt/notebooks/skater.html)\n- [Region K means](https://pysal.org/spopt/notebooks/reg-k-means.html)\n- [Facility Location Real World Problem](https://pysal.org/spopt/notebooks/facloc-real-world.html)\n\nAll examples can be run interactively by launching this repository as a [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/pysal/spopt/main).\n\n## Requirements\n- [scipy](http://scipy.github.io/devdocs/)\n- [numpy](https://numpy.org/devdocs/)\n- [pandas](https://pandas.pydata.org/docs/)\n- [networkx](https://networkx.org/)\n- [libpysal](https://pysal.org/libpysal/)\n- [scikit-learn](https://scikit-learn.org/stable/)\n- [geopandas](https://geopandas.org/)\n- [pulp](https://coin-or.github.io/pulp/)\n- [shapely](https://shapely.readthedocs.io/en/stable/)\n- [spaghetti](https://github.com/pysal/spaghetti)\n\n## Installation\nspopt is available on the [Python Package Index](https://pypi.org/). Therefore, you can either install directly with pip from the command line:\n```\n$ pip install -U spopt\n```\nor download the source distribution (.tar.gz) and decompress it to your selected destination. Open a command shell and navigate to the decompressed folder. Type:\n```\n$ pip install .\n```\nYou may also install the latest stable spopt via conda-forge channel by running:\n```\n$ conda install --channel conda-forge spopt\n```\n\n## Related packages\n\n* Region\n* Locate\n  * [`allagash`](https://github.com/apulverizer/allagash)\n  * [`maximum-coverage-location`](https://github.com/cyang-kth/maximum-coverage-location)\n  * [`p-center`](https://github.com/antoniomedrano/p-center)\n  * [`pyspatialopt`](https://github.com/apulverizer/pyspatialopt)\n\n## Contribute\n\nPySAL-spopt is under active development and contributors are welcome.\n\nIf you have any suggestions, feature requests, or bug reports, please open new [issues](https://github.com/pysal/spopt/issues) on GitHub. To submit patches, please review [PySAL's documentation for developers](https://pysal.org/docs/devs/), the PySAL [development guidelines](https://github.com/pysal/pysal/wiki), the `spopt` [contributing guidelines](https://github.com/pysal/spopt/blob/main/.github/CONTRIBUTING.md) before  opening a [pull request](https://github.com/pysal/spopt/pulls). Once your changes get merged, you\u2019ll automatically be added to the [Contributors List](https://github.com/pysal/spopt/graphs/contributors).\n\n## Support\nIf you are having trouble, please [create an issue](https://github.com/pysal/spopt/issues), [start a discussion](https://github.com/pysal/spopt/discussions), or talk to us in the [gitter room](https://gitter.im/pysal/spopt).\n\n## Code of Conduct\n\nAs a PySAL-federated project, `spopt` follows the [Code of Conduct](https://github.com/pysal/governance/blob/main/conduct/code_of_conduct.rst) under the [PySAL governance model](https://github.com/pysal/governance).\n\n## License\n\nThe project is licensed under the [BSD 3-Clause license](https://github.com/pysal/spopt/blob/main/LICENSE.txt).\n\n## Citation\n\nIf you use PySAL-spopt in a scientific publication, we would appreciate using the following citations:\n\n```\n@misc{spopt2021,\n    author    = {Feng, Xin, and Gaboardi, James D. and Knaap, Elijah and\n                Rey, Sergio J. and Wei, Ran},\n    month     = {jan},\n    year      = {2021},\n    title     = {pysal/spopt},\n    url       = {https://github.com/pysal/spopt},\n    doi       = {10.5281/zenodo.4444156},\n    keywords  = {python,regionalization,spatial-optimization,location-modeling}\n}\n\n@article{spopt2022,\n    author    = {Feng, Xin and Barcelos, Germano and Gaboardi, James D. and\n                Knaap, Elijah and Wei, Ran and Wolf, Levi J. and\n                Zhao, Qunshan and Rey, Sergio J.},\n    year      = {2022},\n    title     = {spopt: a python package for solving spatial optimization problems in PySAL},\n    journal   = {Journal of Open Source Software},\n    publisher = {The Open Journal},\n    volume    = {7},\n    number    = {74},\n    pages     = {3330},\n    url       = {https://doi.org/10.21105/joss.03330},\n    doi       = {10.21105/joss.03330},\n}\n```\n\n## Funding\n\nThis project is/was partially funded through:\n\n[<img align=\"middle\" src=\"docs/_static/images/nsf_logo.png\" width=\"75\">](https://www.nsf.gov/index.jsp) National Science Foundation Award #1831615: [RIDIR: Scalable Geospatial Analytics for Social Science Research](https://www.nsf.gov/awardsearch/showAward?AWD_ID=1831615)\n",
    "bugtrack_url": null,
    "license": "BSD 3-Clause",
    "summary": "Spatial Optimization in PySAL",
    "version": "0.6.0",
    "project_urls": {
        "Home": "https://pysal.org/spopt/",
        "Repository": "https://github.com/pysal/spopt"
    },
    "split_keywords": [
        "spatial",
        "optimization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cdc183f786133d476c2f406ce196c598dba394aec76c1338a18800300ba1f7ad",
                "md5": "2a9d804affc540ba053c825124471d79",
                "sha256": "f14442dc4f1990e3727e87aa44a589e1f69dafc05610824392e3be6f4d1dbefd"
            },
            "downloads": -1,
            "filename": "spopt-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2a9d804affc540ba053c825124471d79",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 244542,
            "upload_time": "2023-12-10T20:27:22",
            "upload_time_iso_8601": "2023-12-10T20:27:22.123581Z",
            "url": "https://files.pythonhosted.org/packages/cd/c1/83f786133d476c2f406ce196c598dba394aec76c1338a18800300ba1f7ad/spopt-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b8d1e90d010c09c7d1a32e8ccb2e80108c55605d226bfdacf42061c62e90500c",
                "md5": "34674bc972458fb70d38af5b8d560f33",
                "sha256": "1edf615a8ab1bbae3881c742083e20f18d24724947e84bf2dd12ec38510bcf3a"
            },
            "downloads": -1,
            "filename": "spopt-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "34674bc972458fb70d38af5b8d560f33",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 30545521,
            "upload_time": "2023-12-10T20:27:24",
            "upload_time_iso_8601": "2023-12-10T20:27:24.154442Z",
            "url": "https://files.pythonhosted.org/packages/b8/d1/e90d010c09c7d1a32e8ccb2e80108c55605d226bfdacf42061c62e90500c/spopt-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-10 20:27:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pysal",
    "github_project": "spopt",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "spopt"
}
        
Elapsed time: 0.18870s