"""

biogeme.optimization
====================

Examples of use of several functions.

This is designed for programmers who need examples of use of the
functions of the module. The examples are designed to illustrate the
syntax. They do not correspond to any meaningful model.

Michel Bierlaire
Sun Jun 29 2025, 18:10:12
"""

from IPython.core.display_functions import display

import biogeme.biogeme_logging as blog
from biogeme.biogeme import BIOGEME
from biogeme.data.swissmetro import (
    CAR_AV_SP,
    CAR_CO_SCALED,
    CAR_TT_SCALED,
    CHOICE,
    SM_AV,
    SM_COST_SCALED,
    SM_TT_SCALED,
    TRAIN_AV_SP,
    TRAIN_COST_SCALED,
    TRAIN_TT_SCALED,
    read_data,
)
from biogeme.expressions import Beta
from biogeme.models import loglogit
from biogeme.results_processing import get_pandas_estimated_parameters
from biogeme.version import get_text

# %%
# Version of Biogeme.
print(get_text())

# %%
# The logger sets the verbosity of Biogeme. By default, Biogeme is quite silent and generates only warnings.
# To have more information about what it happening behind the scene, the level should be set to `blog.INFO`.
logger = blog.get_screen_logger(level=blog.INFO)

# %%
# Read the data file
the_data = read_data()
# %%
# Parameters to be estimated: alternative specific constants
asc_car = Beta('asc_car', 0, None, None, 0)
asc_train = Beta('asc_train', 0, None, None, 0)

# %%
# The constant associated with Swissmetro is normalized to zero. It does not need to be defined at all.
# Here, we illustrate the fact that setting the last argument of the `Beta` function to 1 fixes the parameter
# to its default value (here, 0).
asc_sm = Beta('asc_sm', 0, None, None, 1)

# %%
# Coefficients of the attributes
b_time = Beta('b_time', 0, None, None, 0)
b_cost = Beta('b_cost', 0, None, None, 0)


# %%
# Definition of the utility functions.
v_train = asc_train + b_time * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED
v_sm = asc_sm + b_time * SM_TT_SCALED + b_cost * SM_COST_SCALED
v_car = asc_car + b_time * CAR_TT_SCALED + b_cost * CAR_CO_SCALED

# %%
# Associate utility functions with the numbering of alternatives.
v = {1: v_train, 2: v_sm, 3: v_car}

# %%
# Associate the availability conditions with the alternatives.
av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}

# %%
# Definition of the model.
# This is the contribution of each observation to the log likelihood function.
log_probability = loglogit(v, av, CHOICE)


# %%
# **scipy**: this is the optimization algorithm from scipy.
my_biogeme_scipy = BIOGEME(
    the_data,
    log_probability,
    save_iterations=False,
    generate_html=False,
    generate_yaml=False,
    optimization_algorithm='scipy',
)
my_biogeme_scipy.model_name = 'simple_example_scipy'
print(my_biogeme_scipy)
results_scipy = my_biogeme_scipy.estimate(
    starting_values={'asc_train': 0, 'b_time': 0, 'b_cost': 0, 'asc_car': 0}
)
pandas_parameters_scipy = get_pandas_estimated_parameters(
    estimation_results=results_scipy
)
display(pandas_parameters_scipy)
# %%
# Here are the messages generated by the optimization algorithm
for k, v in results_scipy.optimization_messages.items():
    print(f'{k}:\t{v}')


# %%
# **Newton with trust region**
my_biogeme_tr_newton = BIOGEME(
    the_data,
    log_probability,
    save_iterations=False,
    generate_html=False,
    generate_yaml=False,
    optimization_algorithm='TR-newton',
)
my_biogeme_tr_newton.model_name = 'simple_example_tr_newton'
print(my_biogeme_tr_newton)
results_tr_newton = my_biogeme_tr_newton.estimate(
    starting_values={'asc_train': 0, 'b_time': 0, 'b_cost': 0, 'asc_car': 0}
)
pandas_parameters_tr_newton = get_pandas_estimated_parameters(
    estimation_results=results_tr_newton
)
display(pandas_parameters_tr_newton)
# %%
# Here are the messages generated by the optimization algorithm
for k, v in results_tr_newton.optimization_messages.items():
    print(f'{k}:\t{v}')

# %%
# **Newton/BFGS with trust region for simple bounds**

# %%
# This is the default algorithm used by Biogeme. It is the
# implementation of the algorithm proposed by `Conn et al. (1988)
# <https://www.ams.org/journals/mcom/1988-50-182/S0025-5718-1988-0929544-3/S0025-5718-1988-0929544-3.pdf>`_.
my_biogeme_simple_bounds = BIOGEME(
    the_data,
    log_probability,
    save_iterations=False,
    generate_html=False,
    generate_yaml=False,
    optimization_algorithm='automatic',
)
my_biogeme_simple_bounds.model_name = 'simple_example_simple_bounds'
print(my_biogeme_simple_bounds)
results_simple_bounds = my_biogeme_simple_bounds.estimate(
    starting_values={'asc_train': 0, 'b_time': 0, 'b_cost': 0, 'asc_car': 0}
)
pandas_parameters_simple_bounds = get_pandas_estimated_parameters(
    estimation_results=results_simple_bounds
)
display(pandas_parameters_simple_bounds)
# %%
# Here are the messages generated by the optimization algorithm
for k, v in results_simple_bounds.optimization_messages.items():
    print(f'{k}:\t{v}')

# %%
# When the second derivatives are too computationally expensive to
# calculate, it is possible to avoid calculating them at each
# successful iteration. The parameter `second_derivatives` allows to
# control that.

# %%
my_biogeme_simple_bounds_no_hessian = BIOGEME(
    the_data,
    log_probability,
    save_iterations=False,
    generate_html=False,
    generate_yaml=False,
    optimization_algorithm='simple_bounds',
    second_derivatives=0,
)
my_biogeme_simple_bounds_no_hessian.model_name = (
    'simple_example_simple_bounds_no_hessian'
)
print(my_biogeme_simple_bounds_no_hessian)
results_simple_bounds_no_hessian = my_biogeme_simple_bounds_no_hessian.estimate(
    starting_values={'asc_train': 0, 'b_time': 0, 'b_cost': 0, 'asc_car': 0}
)
pandas_parameters_simple_bounds_no_hessian = get_pandas_estimated_parameters(
    estimation_results=results_simple_bounds_no_hessian
)
display(pandas_parameters_simple_bounds_no_hessian)
# %%
# Here are the messages generated by the optimization algorithm
for k, v in results_simple_bounds_no_hessian.optimization_messages.items():
    print(f'{k}:\t{v}')
