<jupyter_start><jupyter_text>This is a type of location optimization analysis, specifically finding the optimal location of facilites on a network. This analysis is the P-Median Problem implemented in **Python**: P-Median ProblemThe P-median problem finds the location of (a pre-specified number of) P facilities to minimize the average travel distance (or time) among all demand points and facilities. The P-median problem can take into account the level of demand at each point (e.g. number of people, or the number of visits).more information on GOSTNets Optimization can be found in the wiki: https://github.com/worldbank/GOST_PublicGoods/wiki/GOSTnets-Optimization<jupyter_code>import geopandas as gpd
import pandas as pd
import os, sys
# add to your system path the location of the GOSTnet.py library
sys.path.append(r'../../../GOSTNets/GOSTNets')
import GOSTnet_Optimization as gn
import importlib<jupyter_output><empty_output><jupyter_text>The P-Median Problem requires an OD matrix as an input read in OD matrix saved as csv file as dataframe<jupyter_code>pth = r'../../../../lima_optimization_output'
OD_df = pd.read_csv(os.path.join(pth, 'saved_OD.csv'),index_col=0)
OD_df[:3]
OD_df.keys()
facilities = OD_df.columns.values.tolist()
# facilities list
facilities
# pulp is a python optimization library
import pulp
#import importlib
#importlib.reload(gn)<jupyter_output><empty_output><jupyter_text>Compute the P-Median ProblemThe third argument to the optimize_facility_locations function is the number facilities to optimally locate<jupyter_code>results = gn.optimize_facility_locations(OD_df, facilities, 4, existing_facilities = None)<jupyter_output><empty_output><jupyter_text>These are the node IDs of the selected optimal facilities<jupyter_code>results
# save results in a .txt file
file = open(r'../../../../lima_optimization_output/results.txt', 'w')
for r in results:
    file.write('{}\n'.format(r))
file.close()<jupyter_output><empty_output><jupyter_text>Optional, if you would like to access the objective function value<jupyter_code>#generate pulp.LpProblem object without solving
return_problem = gn.optimize_facility_locations(OD_df, facilities, 4, existing_facilities = None, execute=False)
#solve problem
return_problem.solve()
#print out objective value
return_problem.objective.value()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0908315565031983, 'fraction_numerical': 0.0017057569296375266, 'mean_word_length': 6.174311926605505, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '242849', 'license': 'MIT', 'n_tokens_mistral': 732, 'n_tokens_neox': 699, 'n_words': 232, 'path': 'Implementations/FY20/ACC_Peru_Lima_Optimization/JupyterNotebooks/Step6 - Optimization_P-Median_Python.ipynb', 'repo_name': 'worldbank/GOST_PublicGoods', 'size': '2345'}