pyslsqp.postprocessing

pyslsqp.postprocessing.print_file_contents(...)

Print the contents of the saved file.

pyslsqp.postprocessing.load_results(filepath)

Load the results of optimization from the saved file as a dictionary.

pyslsqp.postprocessing.load_attributes(filepath)

Load the attributes of optimization from the saved file as a dictionary.

pyslsqp.postprocessing.load_variables(...[, ...])

Load specified variable iterates between itr_start and itr_end from the saved file.

pyslsqp.postprocessing.visualize(...[, ...])

Visualize different scalar variables using the saved data in a file.

pyslsqp.postprocessing.print_dict_as_table(data)

Print any input dictionary as a table.

pyslsqp.postprocessing.print_file_contents(filepath)[source]

Print the contents of the saved file.

Parameters
filepathstr

Path to the saved file.

Examples

>>> import numpy as np
>>> from pyslsqp import optimize
>>> obj = lambda x: np.sum(x**2)
>>> grad = lambda x: 2*x
>>> xl = 0.0
>>> xu = np.array([1, 1])
>>> x0 = np.array([0.5, 0.5])
>>> results = optimize(x0, obj=obj, grad=grad, xl=xl, xu=xu, save_itr='major', save_vars=['objective', 'optimality', 'x'])  
No constraints defined. Running an unconstrained optimization problem...
Optimization terminated successfully    (Exit mode 0)
            Final objective value                : 0.000000e+00
            Final optimality                     : 0.000000e+00
            Final feasibility                    : 0.000000e+00
            Number of major iterations           : 2
            Number of function evaluations       : 2
            Number of derivative evaluations     : 2
            Average Derivative evaluation time   : ... s per evaluation
            Average Function evaluation time     : ... s per evaluation
            Total Function evaluation time       : ... s [ ...%]
            Total Derivative evaluation time     : ... s [ ...%]
            Optimizer time                       : ... s [ ...%]
            Processing time                      : ... s [ ...%]
            Visualization time                   : ... s [  0.00%]
            Total optimization time              : ... s [100.00%]
            Summary saved to                     : slsqp_summary.out
            Iteration data saved to              : slsqp_recorder.hdf5
>>> from pyslsqp.postprocessing import print_file_contents
>>> print_file_contents('slsqp_recorder.hdf5')  
Available data in the file:
---------------------------
     Attributes of optimization  : ['acc', 'con_scaler', 'finite_diff_abs_step', 'finite_diff_rel_step', 
     'hot_start', 'iprint', 'keep_plot_open', 'load_filename', 'm', 'maxiter', 'meq', 'n', 'obj_scaler', 
     'save_figname', 'save_filename', 'save_itr', 'save_vars', 'summary_filename', 'visualize', 'visualize_vars', 
     'warm_start', 'x0', 'x_scaler', 'xl', 'xu']
     Saved variable iterates     : ['ismajor', 'iter', 'majiter', 'objective', 'optimality', 'x']
     Results of Optimization     : ['constraints', 'feasibility', 'fev_time', 'gev_time', 'gradient', 'jacobian', 
     'message', 'multipliers', 'nfev', 'ngev', 'num_majiter', 'objective', 'optimality', 'optimizer_time', 'processing_time', 
     'save_filename', 'status', 'success', 'summary_filename', 'total_time', 'visualization_time', 'x']
pyslsqp.postprocessing.load_results(filepath)[source]

Load the results of optimization from the saved file as a dictionary.

Parameters
filepathstr

Path to the saved file.

Returns
out_datadict

Dictionary with optimization results.

Examples

>>> import numpy as np
>>> from pyslsqp import optimize
>>> obj = lambda x: np.sum(x**2)
>>> grad = lambda x: 2*x
>>> xl = 0.0
>>> xu = np.array([1, 1])
>>> x0 = np.array([0.5, 0.5])
>>> results = optimize(x0, obj=obj, grad=grad, xl=xl, xu=xu, save_itr='major', save_vars=['objective', 'optimality', 'x'])  
No constraints defined. Running an unconstrained optimization problem...
Optimization terminated successfully    (Exit mode 0)
            Final objective value                : 0.000000e+00
            Final optimality                     : 0.000000e+00
            Final feasibility                    : 0.000000e+00
            Number of major iterations           : 2
            Number of function evaluations       : 2
            Number of derivative evaluations     : 2
            Average Derivative evaluation time   : ... s per evaluation
            Average Function evaluation time     : ... s per evaluation
            Total Function evaluation time       : ... s [ ...%]
            Total Derivative evaluation time     : ... s [ ...%]
            Optimizer time                       : ... s [ ...%]
            Processing time                      : ... s [ ...%]
            Visualization time                   : ... s [  0.00%]
            Total optimization time              : ... s [100.00%]
            Summary saved to                     : slsqp_summary.out
            Iteration data saved to              : slsqp_recorder.hdf5
>>> from pyslsqp.postprocessing import load_results
>>> load_results('slsqp_recorder.hdf5')  
{'constraints': array([], dtype=float64), 'feasibility': 0.0, 'fev_time': ..., 'gev_time': ..., 'gradient': array([0., 0.]), 
'jacobian': array([], shape=(0, 2), dtype=float64), 'message': 'Optimization terminated successfully', 
'multipliers': array([], dtype=float64), 'nfev': 2, 'ngev': 2, 'num_majiter': 2, 'objective': 0.0, 'optimality': 0.0, 
'optimizer_time': ..., 'processing_time': ..., 'save_filename': 'slsqp_recorder.hdf5', 'status': 0, 'success': True, 
'summary_filename': 'slsqp_summary.out', 'total_time': ..., 'visualization_time': 0.0, 'x': array([0., 0.])}
pyslsqp.postprocessing.load_attributes(filepath)[source]

Load the attributes of optimization from the saved file as a dictionary.

Parameters
filepathstr

Path to the saved file.

Returns
out_datadict

Dictionary with optimization attributes.

Examples

>>> import numpy as np
>>> from pyslsqp import optimize
>>> obj = lambda x: np.sum(x**2)
>>> grad = lambda x: 2*x
>>> xl = 0.0
>>> xu = np.array([1, 1])
>>> x0 = np.array([0.5, 0.5])
>>> results = optimize(x0, obj=obj, grad=grad, xl=xl, xu=xu, save_itr='major', save_vars=['objective', 'optimality', 'x'])  
No constraints defined. Running an unconstrained optimization problem...
Optimization terminated successfully    (Exit mode 0)
            Final objective value                : 0.000000e+00
            Final optimality                     : 0.000000e+00
            Final feasibility                    : 0.000000e+00
            Number of major iterations           : 2
            Number of function evaluations       : 2
            Number of derivative evaluations     : 2
            Average Derivative evaluation time   : ... s per evaluation
            Average Function evaluation time     : ... s per evaluation
            Total Function evaluation time       : ... s [ ...%]
            Total Derivative evaluation time     : ... s [ ...%]
            Optimizer time                       : ... s [ ...%]
            Processing time                      : ... s [ ...%]
            Visualization time                   : ... s [  0.00%]
            Total optimization time              : ... s [100.00%]
            Summary saved to                     : slsqp_summary.out
            Iteration data saved to              : slsqp_recorder.hdf5
>>> from pyslsqp.postprocessing import load_attributes
>>> load_attributes('slsqp_recorder.hdf5')  
{'acc': 1e-06, 'con_scaler': 1.0, 'finite_diff_abs_step': 'None (undefined)', 'finite_diff_rel_step': 1.4901161193847656e-08, 
'hot_start': False, 'iprint': 1, 'keep_plot_open': False, 'load_filename': 'None (undefined)', 'm': 0, 'maxiter': 100, 'meq': 0, 'n': 2, 'obj_scaler': 1.0, 
'save_figname': 'slsqp_plot.pdf', 'save_filename': 'slsqp_recorder.hdf5', 'save_itr': 'major', 'save_vars': ['objective', 'optimality', 'x'], 
'summary_filename': 'slsqp_summary.out', 'visualize': False, 'visualize_vars': ['objective', 'optimality', 'feasibility'], 'warm_start': False, 
'x0': array([0.5, 0.5]), 'x_scaler': 1.0, 'xl': 0.0, 'xu': array([1, 1])}
pyslsqp.postprocessing.load_variables(filepath, vars, itr_start=0, itr_end=-1, major_only=False)[source]

Load specified variable iterates between itr_start and itr_end from the saved file. Returns a dictionary with the variable names as keys and list of variable iterates as values. Note the variables at itr_start and itr_end are included in the output.

Parameters
filepathstr

Path to the saved file.

varsstr or list

Variable names to load from the saved file.

itr_startint, default=0

Starting iteration to load the variables from. Negative indices are allowed with -1 representing the last iteration and -2 representing the second last iteration and so on.

itr_endint, default=-1

Ending iteration to load the variables from. Negative indices are allowed with -1 representing the last iteration and -2 representing the second last iteration and so on.

major_onlybool, default=False

If True, only major iterations are loaded. If False, all iterations are loaded irrespective of major or line search iterations.

Returns
out_datadict

Dictionary with variable names as keys and list of variable iterates as values.

Examples

>>> import numpy as np
>>> from pyslsqp import optimize
>>> obj = lambda x: np.sum(x**2)
>>> grad = lambda x: 2*x
>>> xl = 0.0
>>> xu = np.array([1, 1])
>>> x0 = np.array([0.5, 0.5])
>>> results = optimize(x0, obj=obj, grad=grad, xl=xl, xu=xu, save_itr='major', save_vars=['objective', 'optimality', 'x'])  
No constraints defined. Running an unconstrained optimization problem...
Optimization terminated successfully    (Exit mode 0)
            Final objective value                : 0.000000e+00
            Final optimality                     : 0.000000e+00
            Final feasibility                    : 0.000000e+00
            Number of major iterations           : 2
            Number of function evaluations       : 2
            Number of derivative evaluations     : 2
            Average Derivative evaluation time   : ... s per evaluation
            Average Function evaluation time     : ... s per evaluation
            Total Function evaluation time       : ... s [ ...%]
            Total Derivative evaluation time     : ... s [ ...%]
            Optimizer time                       : ... s [ ...%]
            Processing time                      : ... s [ ...%]
            Visualization time                   : ... s [  0.00%]
            Total optimization time              : ... s [100.00%]
            Summary saved to                     : slsqp_summary.out
            Iteration data saved to              : slsqp_recorder.hdf5
>>> from pyslsqp.postprocessing import load_variables
>>> load_variables('slsqp_recorder.hdf5', ['objective', 'optimality', 'x[0]'], itr_start=0, itr_end=-1, major_only=True)
{'objective': [0.5, 0.0, 0.0], 'optimality': [99.0, 0.0, 0.0], 'x[0]': [0.5, 0.0, 0.0]}
pyslsqp.postprocessing.visualize(savefilename, visualize_vars, itr_start=0, itr_end=-1, major_only=False, save_figname=None)[source]

Visualize different scalar variables using the saved data in a file.

The variables to visualize should be a list of strings, where each string is the name of a variable to visualize. The variables can be any of the following:

  • ‘objective’ : the objective function value

  • ‘optimality’ : the optimality condition

  • ‘feasibility’ : the feasibility condition

  • ‘x[i]’ : the ith variable value

  • ‘constraints[i]’ : the ith constraint value

  • ‘jacobian[i,j]’ : the (i,j) element of the Jacobian matrix

  • ‘gradient[i]’ : the ith gradient value

  • ‘multipliers[i]’ : the ith Lagrange multiplier value

Creates a plot with the specified variables on the y-axis and the iteration number on the x-axis. The plots are stacked vertically in the order they are specified in the list.

Parameters
savefilenamestr

Path to the saved file.

visualize_varsstr or list of str

List of variables to visualize.

itr_startint, default=0

Starting iteration to visualize. Negative indices are allowed with -1 representing the last iteration and -2 representing the second last iteration and so on.

itr_endint, default=-1

Ending iteration to visualize. Negative indices are allowed with -1 representing the last iteration and -2 representing the second last iteration and so on.

major_onlybool, default=False

If True, only major iterations are visualized. If False, all iterations are visualized irrespective of major or line search iterations.

save_fignamestr, default=None

Path to save the figure. If None, the figure is not saved.

Examples

>>> import numpy as np
>>> from pyslsqp import optimize
>>> obj = lambda x: np.sum(x**2)
>>> grad = lambda x: 2*x
>>> xl = 0.0
>>> xu = np.array([1, 1])
>>> x0 = np.array([0.5, 0.5])
>>> results = optimize(x0, obj=obj, grad=grad, xl=xl, xu=xu, save_itr='major', save_vars=['objective', 'optimality', 'x'])  
No constraints defined. Running an unconstrained optimization problem...
Optimization terminated successfully    (Exit mode 0)
            Final objective value                : 0.000000e+00
            Final optimality                     : 0.000000e+00
            Final feasibility                    : 0.000000e+00
            Number of major iterations           : 2
            Number of function evaluations       : 2
            Number of derivative evaluations     : 2
            Average Derivative evaluation time   : ... s per evaluation
            Average Function evaluation time     : ... s per evaluation
            Total Function evaluation time       : ... s [ ...%]
            Total Derivative evaluation time     : ... s [ ...%]
            Optimizer time                       : ... s [ ...%]
            Processing time                      : ... s [ ...%]
            Visualization time                   : ... s [  0.00%]
            Total optimization time              : ... s [100.00%]
            Summary saved to                     : slsqp_summary.out
            Iteration data saved to              : slsqp_recorder.hdf5
>>> from pyslsqp.postprocessing import visualize
>>> visualize('slsqp_recorder.hdf5', ['objective', 'optimality', 'x[0]', 'x[1]'], major_only=True)
pyslsqp.postprocessing.print_dict_as_table(data)[source]

Print any input dictionary as a table.

Parameters
datadict

Dictionary to print as a table.

Examples

>>> data = {'a': 0, 'b': "string", 'c': ['a', 'b', 'c']}
>>> print_dict_as_table(data)
--------------------------------------------------
        a                        : 0
        b                        : string
        c                        : ['a', 'b', 'c']
--------------------------------------------------