Nothing
#!/usr/bin/env Rscript
# b07everything_assisted. Use native assisted specification search.
#
# The complete 432-specification model from everything_spec.py is written in
# this script. The assisted search, multi-objective evaluation, validity rule,
# Pareto checkpoint, and final re-estimation all stay inside native Biogeme.
library(rbiogeme)
# prepare_swissmetro_example() is defined in ../swissmetro/example_utils.R.
# It supplies paths and output setup only; it does not define this model.
script_path <- commandArgs(trailingOnly = FALSE)
script_path <- sub("^--file=", "", script_path[startsWith(script_path, "--file=")][[1L]])
source(file.path(dirname(normalizePath(script_path)), "..", "swissmetro", "example_utils.R"))
build_b07everything_model <- function(database) {
# Define COMMUTERS natively, exactly as in everything_spec.py.
database <- biogeme_database_define_variable(
database,
"COMMUTERS",
variable("PURPOSE") == 1
)
segmentation_ga <- biogeme_database_segmentation(
database,
"GA",
c(`0` = "noGA", `1` = "GA"),
reference = "noGA"
)
segmentation_luggage <- biogeme_database_segmentation(
database,
"LUGGAGE",
c(`0` = "no_lugg", `1` = "one_lugg", `3` = "several_lugg"),
reference = "no_lugg"
)
segmentation_first <- biogeme_database_segmentation(
database,
"FIRST",
c(`0` = "2nd_class", `1` = "1st_class"),
reference = "2nd_class"
)
segmentation_purpose <- biogeme_database_segmentation(
database,
"COMMUTERS",
c(`0` = "non_commuters", `1` = "commuters"),
reference = "non_commuters"
)
# Parameter names, starts, and bounds are unchanged from native Biogeme.
asc_car <- biogeme_beta("asc_car", start = 0)
asc_train <- biogeme_beta("asc_train", start = 0)
b_time <- biogeme_beta("b_time", start = 0)
b_cost <- biogeme_beta("b_cost", start = 0)
lambda_travel_time <- biogeme_beta(
"lambda_travel_time",
start = 1,
lower = -10,
upper = 10
)
square_tt_coef <- biogeme_beta("square_tt_coef", start = 0)
cube_tt_coef <- biogeme_beta("cube_tt_coef", start = 0)
power_series <- function(the_variable) {
the_variable + square_tt_coef * the_variable^2 +
cube_tt_coef * the_variable * the_variable^3
}
time_controller <- catalog_controller(
"train_tt_catalog",
c("linear", "boxcox", "power")
)
train_tt_catalog <- catalog(
"train_tt_catalog",
list(
linear = variable("TRAIN_TT_SCALED"),
boxcox = boxcox(variable("TRAIN_TT_SCALED"), lambda_travel_time),
power = power_series(variable("TRAIN_TT_SCALED"))
),
controller = time_controller
)
sm_tt_catalog <- catalog(
"sm_tt_catalog",
list(
linear = variable("SM_TT_SCALED"),
boxcox = boxcox(variable("SM_TT_SCALED"), lambda_travel_time),
power = power_series(variable("SM_TT_SCALED"))
),
controller = time_controller
)
car_tt_catalog <- catalog(
"car_tt_catalog",
list(
linear = variable("CAR_TT_SCALED"),
boxcox = boxcox(variable("CAR_TT_SCALED"), lambda_travel_time),
power = power_series(variable("CAR_TT_SCALED"))
),
controller = time_controller
)
asc_catalogs <- segmentation_catalogs(
generic_name = "asc",
beta_parameters = list(asc_train, asc_car),
potential_segmentations = list(segmentation_ga, segmentation_luggage),
maximum_number = 2
)
b_time_catalogs <- generic_alt_specific_catalogs(
generic_name = "b_time",
beta_parameters = list(b_time),
alternatives = c("train", "swissmetro", "car"),
potential_segmentations = list(segmentation_first, segmentation_purpose),
maximum_number = 1
)[[1L]]
b_cost_catalogs <- generic_alt_specific_catalogs(
generic_name = "b_cost",
beta_parameters = list(b_cost),
alternatives = c("train", "swissmetro", "car")
)[[1L]]
utilities <- list(
`1` = asc_catalogs[[1L]] +
b_time_catalogs$train * train_tt_catalog +
b_cost_catalogs$train * variable("TRAIN_COST_SCALED"),
`2` = b_time_catalogs$swissmetro * sm_tt_catalog +
b_cost_catalogs$swissmetro * variable("SM_COST_SCALED"),
`3` = asc_catalogs[[2L]] +
b_time_catalogs$car * car_tt_catalog +
b_cost_catalogs$car * variable("CAR_CO_SCALED")
)
availability <- list(
`1` = variable("TRAIN_AV_SP"),
`2` = variable("SM_AV"),
`3` = variable("CAR_AV_SP")
)
choice <- variable("CHOICE")
logit <- logit_log_probability(utilities, availability, choice)
mu_existing <- biogeme_beta("mu_existing", start = 1, lower = 1, upper = 10)
nested_existing <- nested_log_probability(
utilities,
availability,
nested_nests(
c(1, 2, 3),
list(nested_nest(mu_existing, c(1, 3), name = "Existing"))
),
choice
)
mu_public <- biogeme_beta("mu_public", start = 1, lower = 1, upper = 10)
nested_public <- nested_log_probability(
utilities,
availability,
nested_nests(
c(1, 2, 3),
list(nested_nest(mu_public, c(1, 2), name = "Public"))
),
choice
)
model_catalog <- catalog(
"model_catalog",
list(
logit = logit,
`nested existing` = nested_existing,
`nested public` = nested_public
)
)
biogeme_model(
database = database,
formula = model_catalog,
control = biogeme_control(
output_directory = prepared$output,
generate_html = FALSE,
generate_yaml = FALSE
)
)
}
prepared <- prepare_swissmetro_example(
commandArgs(trailingOnly = TRUE),
default_model = "b07everything_assisted"
)
# Native read_data() removes only CHOICE == 0 for these assisted examples.
database <- swissmetro_data(prepared$data, filter_purpose = FALSE)
model <- build_b07everything_model(database)
# This is the native validity predicate from plot_b07everything_assisted.py:
# it rejects non-negative parameters whose names contain uppercase TIME/COST.
# The bridge creates that rule natively; no R callback runs in estimation.
pareto_file <- file.path(prepared$output, "b07everything_assisted.pareto")
fit <- assisted_specification(
model,
objectives = "loglikelihood_dimension",
pareto_file_name = pareto_file,
model_name = "b07everything",
control = model$control,
force = TRUE,
validity = "negative_time_cost"
)
cat("A total of ", length(fit$results), " models have been generated.\n", sep = "")
print(fit$summary)
for (name in names(fit$description)) {
if (!identical(name, unname(fit$description[[name]]))) {
cat(name, "\t", fit$description[[name]], "\n", sep = "")
}
}
cat("Pareto file: ", fit$pareto_file_name, "\n", sep = "")
for (message in fit$pareto_statistics) cat(message, "\n", sep = "")
invisible(fit)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.