Commit e875713b authored by Aurore Sallard's avatar Aurore Sallard
Browse files

sec. loc. assignemnt done

parent cc942f0f
Pipeline #116298 failed with stage
import pandas as pd
import numpy as np
import data.spatial.utils as spatial_utils
import geopandas as gpd
def configure(context):
context.stage("synthesis.population.destinations")
context.config("use_detailed_activities")
def execute(context):
det_activities = context.config("use_detailed_activities")
df = pd.DataFrame(context.stage("synthesis.population.destinations"), copy=True)
if not det_activities:
return df
df["open_0-3"] = 0
df["open_3-6"] = 0
df["open_6-9"] = 0
df["open_9-12"] = 0
df["open_12-15"] = 0
df["open_15-18"] = 0
df["open_18-21"] = 0
df["open_21-24"] = 0
df.loc[:, "always_open"] = df["noga"].str.startswith("94") | (df["noga"] == "932900") | df["noga"].str.startswith("931") | (df["noga"] == "861001") | (df["noga"] == "861002")
df.loc[:, "office_hours"] = (df["noga"] == "791100") | (df["noga"] == "791200") | (df["noga"] == "841100") | (df["noga"] == "842400") | (df["noga"] == "842301") | (df["noga"] == "843000") | (df["noga"] == "649201") | (df["noga"] == "651100") | (df["noga"] == "681000") | (df["noga"] == "682001") | (df["noga"] == "682002") | (df["noga"] == "683100") | (df["noga"] == "683200") | (df["noga"] == "691001") | (df["noga"] == "692000") | df["noga"].str.startswith("6419") | df["noga"].str.startswith("6512") | (df["noga"] == "772200")
df.loc[:, "office++"] = df["noga"].str.startswith("472") | (df["offers_shop"] & np.logical_not(df["offers_grocery"])) | df["noga"].str.startswith("8690") | df["noga"].str.startswith("95") | df["noga"].str.startswith("33") | (df["noga"] == "750000") | (df["noga"] == "960101") | (df["noga"] == "960102") | (df["noga"] == "960201") | (df["noga"] == "960202") | (df["noga"] == "531000") | (df["noga"] == "532000") | (df["noga"] == "452001") | (df["noga"] == "452002") | (df["noga"] == "855300")
df.loc[:, "office+-"] = (df["noga"] == "771100") | (df["noga"] == "772100") | (df["noga"] == "772900")
df.loc[:, "office-++"] = (df["noga"] == "862100") | (df["noga"] == "862200") | (df["noga"] == "862300")
df.loc[:, "always-"] = df["noga"].str.startswith("55") | (df["noga"].str.startswith("56") & df["noga"] != "563002") | df["noga"].str.startswith("473")
df.loc[:, "extended_night"] = (df["noga"] == "932100") | df["noga"].str.startswith("471") | (df["noga"] == "960401") | (df["noga"] == "960402")
df.loc[:, "markets"] = (df["noga"] == "478100")
df.loc[:, "theaters"] = df["noga"].str.startswith("90")
df.loc[:, "museums"] = df["noga"].str.startswith("91")
df.loc[:, "cinemas"] = (df["noga"] == "591400")
df.loc[:, "casinos"] = (df["noga"] == "920000")
df.loc[:, "night"] = (df["noga"] == "563002")
df.loc[:, "culture_sport_hobby"] = (df["noga"] == "855200") | (df["noga"] == "855100")
df.loc[df["always_open"] | df["casinos"]| df["night"], "open_0-3"] = 1
df.loc[df["always-"] | df["theaters"], "open_0-3"] = 0.5
df.loc[df["always_open"]| df["night"], "open_3-6"] = 1
df.loc[df["always-"] | df["casinos"] | df["markets"], "open_3-6"] = 0.5
df.loc[df["always_open"]| df["markets"], "open_6-9"] = 1
df.loc[df["always-"] | df["museums"] | df["extended_night"] | df["office++"] | df["office+-"] | df["office-++"] , "open_6-9"] = 0.5
df.loc[df["always_open"]| df["markets"] | df["office_hours"] | df["always-"] | df["office++"] | df["office+-"] | df["office-++"]| df["extended_night"] | df["culture_sport_hobby"] | df["museums"], "open_9-12"] = 1
df.loc[df["theaters"] | df["cinemas"] , "open_9-12"] = 0.5
df.loc[df["always_open"]| df["always-"] | df["office_hours"] | df["office++"] | df["office+-"]| df["office-++"]| df["extended_night"] | df["culture_sport_hobby"] | df["museums"] | df["cinemas"], "open_12-15"] = 1
df.loc[df["theaters"] | df["casinos"] | df["markets"] , "open_12-15"] = 0.5
df.loc[df["always_open"]| df["always-"] | df["office_hours"] | df["office++"] | df["office+-"]| df["office-++"]| df["extended_night"] | df["culture_sport_hobby"] | df["museums"] | df["cinemas"], "open_15-18"] = 1
df.loc[df["theaters"] | df["casinos"], "open_15-18"] = 0.5
df.loc[df["always_open"]| df["always-"] | df["office-++"] | df["extended_night"] | df["culture_sport_hobby"] | df["cinemas"]|df["theaters"]| df["casinos"] , "open_18-21"] = 1
df.loc[df["office++"] | df["museums"], "open_18-21"] = 0.5
df.loc[df["always_open"]|df["theaters"]|df["cinemas"]|df["casinos"]| df["night"], "open_21-24"] = 1
df.loc[df["always-"] | df["museums"] | df["extended_night"], "open_21-24"] = 0.5
del df["always_open"]
del df["office_hours"]
del df["office++"]
del df["office+-"]
del df["office-++"]
del df["always-"]
del df["extended_night"]
del df["markets"]
del df["theaters"]
del df["museums"]
del df["cinemas"]
del df["casinos"]
del df["culture_sport_hobby"]
df.loc[df["offers_outdoor"], "open_0-3"] = 1
df.loc[df["offers_outdoor"], "open_3-6"] = 1
df.loc[df["offers_outdoor"], "open_6-9"] = 1
df.loc[df["offers_outdoor"], "open_9-12"] = 1
df.loc[df["offers_outdoor"], "open_12-15"] = 1
df.loc[df["offers_outdoor"], "open_15-18"] = 1
df.loc[df["offers_outdoor"], "open_18-21"] = 1
df.loc[df["offers_outdoor"], "open_21-24"] = 1
return df
......@@ -24,7 +24,7 @@ def execute(context):
age_bounds = [(-np.inf, 6), (6, 12), (12, 15), (15, 19), (19, np.inf)]
education_types = ["kindergarten", "primary", "secondary1", "secondary2", "tertiary"]
query_sizes = (5, 5, 10, 10, 20)
query_sizes = (3, 3, 5, 5, 5)
for (lower_bound, upper_bound), type_educ, query_size in zip(age_bounds, education_types, query_sizes):
f_persons = (df_persons["age"] > lower_bound) & (df_persons["age"] <= upper_bound)
......
......@@ -194,8 +194,12 @@ def compute_weight(purpose, duration, capacity):
return max(2**5*0.98*(x-0.5)**3*(y-0.5)**3+0.5,0)
def compute_all_weights(purpose, duration, capacities):
return [compute_weight(purpose, duration, c) for c in capacities]
def compute_all_weights(purpose, duration, capacities, openings):
L = [compute_weight(purpose, duration, c) for c in capacities]
if np.all(openings) == 0:
return L
L = [o*w for (o,w) in list(zip(openings, L))]
return L
class CustomDistanceSampler(rda.FeasibleDistanceSampler):
......@@ -237,10 +241,27 @@ class CustomDiscretizationSolver(rda.DiscretizationSolver):
for location, purpose, act_dur, start_time in zip(locations, problem["purposes"], problem["activity_duration"], problem["activity_start_time"]):
if purpose in ["grocery", "other(S)", "culture", "gastronomy", "religion", "other(L)", "services"]:
self.query_size = 5
self.query_size = 3
distances, indices = self.indices[purpose].query(location.reshape(1, -1), self.query_size, return_distance=True)
candidates_nboemployees = self.data[purpose]["number_employees"][indices][0]
weights = compute_all_weights(purpose, act_dur, candidates_nboemployees)
start = (start_time // 3600) % 24
if start < 3:
openings = self.data[purpose]["open03"][indices][0]
elif start >= 3 and start < 6:
openings = self.data[purpose]["open36"][indices][0]
elif start >= 6 and start < 9:
openings = self.data[purpose]["open69"][indices][0]
elif start >= 9 and start < 12:
openings = self.data[purpose]["open912"][indices][0]
elif start >= 12 and start < 15:
openings = self.data[purpose]["open1215"][indices][0]
elif start >= 15 and start < 18:
openings = self.data[purpose]["open1518"][indices][0]
elif start >= 18 and start < 21:
openings = self.data[purpose]["open1821"][indices][0]
elif start >= 21 and start < 24:
openings = self.data[purpose]["open2124"][indices][0]
weights = compute_all_weights(purpose, act_dur, candidates_nboemployees, openings)
weights = weights / np.sum(weights)
selector = np.random.choice(self.query_size, p=weights)
......
......@@ -17,7 +17,7 @@ def configure(context):
context.stage("synthesis.population.spatial.primary.locations")
context.stage("synthesis.population.spatial.secondary.distance_distributions")
context.stage("synthesis.population.destinations")
context.stage("synthesis.population.destinations_schedule")
context.config("random_seed")
context.config("threads")
......@@ -43,7 +43,7 @@ def prepare_locations(context):
def prepare_destinations(context):
df_destinations = context.stage("synthesis.population.destinations")
df_destinations = context.stage("synthesis.population.destinations_schedule")
M = np.max(df_destinations["destination_id"].values.tolist()) + 1
det_activities = context.config("use_detailed_activities")
......@@ -69,6 +69,14 @@ def prepare_destinations(context):
df_home.loc[:, "destination_x"] = df_home["geometry"].apply(lambda x: x.x).values
df_home.loc[:, "destination_y"] = df_home["geometry"].apply(lambda x: x.y).values
df_home.loc[:, "number_employees"] = 1
df_home.loc[:, "open_0-3"] = 1
df_home.loc[:, "open_3-6"] = 1
df_home.loc[:, "open_6-9"] = 1
df_home.loc[:, "open_9-12"] = 1
df_home.loc[:, "open_12-15"] = 1
df_home.loc[:, "open_15-18"] = 1
df_home.loc[:, "open_18-21"] = 1
df_home.loc[:, "open_21-24"] = 1
df_home = pd.DataFrame(df_home)
df_destinations = pd.concat([df_destinations, df_home])
......@@ -79,16 +87,32 @@ def prepare_destinations(context):
df_destinations["offers_services"] = df_destinations['offers_services'].fillna(False)
df_destinations["number_employees"] = df_destinations['number_employees'].fillna(1)
open03 = df_destinations["open_0-3"].values
open36 = df_destinations["open_3-6"].values
open69 = df_destinations["open_6-9"].values
open912 = df_destinations["open_9-12"].values
open1215 = df_destinations["open_12-15"].values
open1518 = df_destinations["open_15-18"].values
open1821 = df_destinations["open_18-21"].values
open2124 = df_destinations["open_21-24"].values
output_path = context.config("output_path")
#df_destinations.to_csv("%s/destinations_sec_activities.csv" % output_path, index = None)
for purpose in ("grocery", "other(S)", "culture", "gastronomy", "religion", "sport", "other(L)", "other", "visits", "volunteer", "outdoor", "services"):
f = df_destinations["offers_%s" % purpose].values
#print(list(set(f)))
data[purpose] = dict(
identifiers=identifiers[f],
locations=locations[f],
number_employees = nb_employees[f]
number_employees = nb_employees[f],
open03 = open03[f],
open36 = open36[f],
open69 = open69[f],
open912 = open912[f],
open1215 = open1215[f],
open1518 = open1518[f],
open1821 = open1821[f],
open2124 = open2124[f]
)
print(purpose, len(identifiers[f]))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment