Last active
July 30, 2017 10:50
-
-
Save yogabonito/b2be3014db41a934032ffa0f1ea9e1f6 to your computer and use it in GitHub Desktop.
Demo of AZP variants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# AZP-BasicTabu demo" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The basic tabu version of the AZP as described on p. 432 in Openshaw & Rao (1995) is lacking a stopping condition. That's why the implementation in [commit ee3f08f44](https://github.com/yogabonito/region/commit/ee3f08f441f7367204fc2d1d391e7e3bedacdc80) doesn't terminate. Therefore we left the cells in this notebook unexecuted." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from region.azp import *" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"from matplotlib import pyplot as plt\n", | |
"import pandas as pd\n", | |
"import geopandas as gpd\n", | |
"from shapely.geometry import Polygon" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Example 1: 3x3 lattice" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Inputs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"geometry = [\n", | |
" Polygon([(x, y),\n", | |
" (x, y+1),\n", | |
" (x+1, y+1),\n", | |
" (x+1, y)]) for y in range(3) for x in range(3)\n", | |
"]\n", | |
"\n", | |
"areas_gdf = gpd.GeoDataFrame(\n", | |
" {\"values\": [726.7, 623.6, 487.3,\n", | |
" 200.4, 245.0, 481.0,\n", | |
" 170.9, 225.9, 226.9]},\n", | |
" geometry=geometry)\n", | |
"areas_gdf.plot(column=\"values\")\n", | |
"plt.gca().invert_yaxis()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Clustering" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### without initial solution" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"azpbt = AZPBasicTabu(n_regions=2, random_state=2, tabu_length=20)\n", | |
"regions_dict_bt_wo = azpbt.fit(areas=areas_gdf,\n", | |
" data=[\"values\"],\n", | |
" contiguity=\"rook\")\n", | |
"areas_gdf[\"region\"] = pd.Series(regions_dict_bt_wo)\n", | |
"areas_gdf.plot(column=\"region\", cmap='tab20c')\n", | |
"plt.gca().invert_yaxis()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Example 2: Islands" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"islands_geometry = [\n", | |
" Polygon([(x, y),\n", | |
" (x, y+1),\n", | |
" (x+1, y+1),\n", | |
" (x+1, y)]) for y in range(3) for x in range(3)\n", | |
"] + [\n", | |
" Polygon([(x, y),\n", | |
" (x, y+1),\n", | |
" (x+1, y+1),\n", | |
" (x+1, y)]) for y in range(4,6) for x in range(5,8)\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"islands_gdf = gpd.GeoDataFrame(\n", | |
" {\"values\": [726.7, 623.6, 487.3,\n", | |
" 200.4, 245.0, 481.0,\n", | |
" 170.9, 225.9, 226.9] +\n", | |
" [100, 120, 190,\n", | |
" 175, 185, 210]},\n", | |
" geometry=islands_geometry)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"islands_gdf.plot(column=\"values\")\n", | |
"plt.gca().invert_yaxis()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Clustering" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### without initial solution" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"azpbt = AZPBasicTabu(n_regions=3, tabu_length=3, random_state=0)\n", | |
"islands_dict_bt_wo = azpbt.fit(areas=islands_gdf,\n", | |
" data=[\"values\"],\n", | |
" contiguity=\"rook\")\n", | |
"islands_gdf[\"region\"] = pd.Series(islands_dict_bt_wo)\n", | |
"islands_gdf.plot(column=\"region\", cmap='tab20c')\n", | |
"plt.gca().invert_yaxis()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Example 3: A tricky toy example" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Inputs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"squares = [\n", | |
" Polygon([(x, 0),\n", | |
" (x, 1),\n", | |
" (x+1, 1),\n", | |
" (x+1, 0)]) for x in range(3)\n", | |
"]\n", | |
"values = [0, 1, 0]\n", | |
"squares_gdf = gpd.GeoDataFrame({\"values\": values},\n", | |
" geometry=squares)\n", | |
"squares_gdf.plot(column=\"values\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Clustering" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"azpbt = AZPSimulatedAnnealing(n_regions=2, init_temperature=1, max_iterations=3, random_state=0)\n", | |
"regions_dict_squares_bt = azpbt.fit(areas=squares_gdf,\n", | |
" data=[\"values\"],\n", | |
" contiguity=\"rook\",\n", | |
" cooling_factor=0.85)\n", | |
"squares_gdf[\"region\"] = pd.Series(regions_dict_squares_bt)\n", | |
"squares_gdf.plot(column=\"region\", cmap='tab20c')\n", | |
"plt.gca().invert_yaxis()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment