.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/programmers/plot_optimization.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_programmers_plot_optimization.py: 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 .. GENERATED FROM PYTHON SOURCE LINES 15-38 .. code-block:: Python 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 .. GENERATED FROM PYTHON SOURCE LINES 39-40 Version of Biogeme. .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python print(get_text()) .. GENERATED FROM PYTHON SOURCE LINES 43-45 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`. .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Read the data file .. GENERATED FROM PYTHON SOURCE LINES 49-50 .. code-block:: Python the_data = read_data() .. GENERATED FROM PYTHON SOURCE LINES 51-52 Parameters to be estimated: alternative specific constants .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python asc_car = Beta('asc_car', 0, None, None, 0) asc_train = Beta('asc_train', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 56-59 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). .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python asc_sm = Beta('asc_sm', 0, None, None, 1) .. GENERATED FROM PYTHON SOURCE LINES 62-63 Coefficients of the attributes .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python b_time = Beta('b_time', 0, None, None, 0) b_cost = Beta('b_cost', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 68-69 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 69-73 .. code-block:: Python 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 .. GENERATED FROM PYTHON SOURCE LINES 74-75 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: Python v = {1: v_train, 2: v_sm, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 78-79 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 82-84 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: Python log_probability = loglogit(v, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 88-89 **scipy**: this is the optimization algorithm from scipy. .. GENERATED FROM PYTHON SOURCE LINES 89-106 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 107-108 Here are the messages generated by the optimization algorithm .. GENERATED FROM PYTHON SOURCE LINES 108-112 .. code-block:: Python for k, v in results_scipy.optimization_messages.items(): print(f'{k}:\t{v}') .. GENERATED FROM PYTHON SOURCE LINES 113-114 **Newton with trust region** .. GENERATED FROM PYTHON SOURCE LINES 114-131 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 132-133 Here are the messages generated by the optimization algorithm .. GENERATED FROM PYTHON SOURCE LINES 133-136 .. code-block:: Python for k, v in results_tr_newton.optimization_messages.items(): print(f'{k}:\t{v}') .. GENERATED FROM PYTHON SOURCE LINES 137-138 **Newton/BFGS with trust region for simple bounds** .. GENERATED FROM PYTHON SOURCE LINES 140-143 This is the default algorithm used by Biogeme. It is the implementation of the algorithm proposed by `Conn et al. (1988) `_. .. GENERATED FROM PYTHON SOURCE LINES 143-160 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 161-162 Here are the messages generated by the optimization algorithm .. GENERATED FROM PYTHON SOURCE LINES 162-165 .. code-block:: Python for k, v in results_simple_bounds.optimization_messages.items(): print(f'{k}:\t{v}') .. GENERATED FROM PYTHON SOURCE LINES 166-170 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. .. GENERATED FROM PYTHON SOURCE LINES 172-192 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 193-194 Here are the messages generated by the optimization algorithm .. GENERATED FROM PYTHON SOURCE LINES 194-196 .. code-block:: Python for k, v in results_simple_bounds_no_hessian.optimization_messages.items(): print(f'{k}:\t{v}') .. _sphx_glr_download_auto_examples_programmers_plot_optimization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_optimization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_optimization.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_optimization.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_