modopt.postprocessing
Print and return the contents of the record file. |
|
|
Load the results of optimization from the record as a dictionary. |
|
Load the attributes of optimization from the record as a dictionary. |
|
Load specified variable iterates from the record file. |
|
Visualize different scalar variables using the saved data from the record file. |
- modopt.postprocessing.print_record_contents(filepath, suppress_print=False)[source]
Print and return the contents of the record file.
- Parameters
- filepathstr
Path to the record file.
- suppress_printbool, default=False
If False, print the contents of the record file. Otherwise, return the contents as a tuple.
- Returns
- attributeslist
List of attributes of optimization.
- opt_varslist
List of recorded optimizer variables.
- callback_varslist
List of recorded callback variables.
- resultslist
List of results of optimization.
Examples
>>> import numpy as np >>> import modopt as mo >>> obj = lambda x: np.sum(x**2) >>> grad = lambda x: 2*x >>> con = lambda x: np.array([x[0] + x[1], x[0] - x[1]]) >>> jac = lambda x: np.array([[1, 1], [1, -1]]) >>> xl = np.array([1.0, -np.inf]) >>> x0 = np.array([500., 50.]) >>> cl = 1.0 >>> cu = np.array([1., np.inf]) >>> problem = mo.ProblemLite(x0, obj=obj, grad=grad, con=con, jac=jac, xl=xl, cl=cl, cu=cu) >>> optimizer = mo.SLSQP(problem, recording=True) >>> results = optimizer.solve() >>> from modopt.postprocessing import print_record_contents >>> print_record_contents(results['out_dir']+'/record.hdf5') Available data in the record: ----------------------------- - Attributes of optimization : ['c_lower', 'c_scaler', 'c_upper', 'constrained', 'hot_start_from', 'modopt_output_files', 'nc', 'nx', 'o_scaler', 'problem_name', 'readable_outputs', 'recording', 'solver_name', 'solver_options-callback', 'solver_options-disp', 'solver_options-ftol', 'solver_options-maxiter', 'timestamp', 'visualize', 'x0', 'x_lower', 'x_scaler', 'x_upper'] - Recorded optimizer variables : ['x'] - Recorded callback variables : ['con', 'grad', 'jac', 'obj', 'x'] - Results of optimization : ['con_evals', 'fun', 'grad_evals', 'hess_evals', 'jac', 'jac_evals', 'message',... 'nfev', 'nit', 'njev', 'obj_evals', 'out_dir', 'reused_callbacks', 'status', 'success', 'total_callbacks', 'x'] ([...], [...], [...])
- modopt.postprocessing.load_results(filepath)[source]
Load the results of optimization from the record as a dictionary.
- Parameters
- filepathstr
Path to the record file.
- Returns
- out_datadict
Dictionary with optimization results.
Examples
>>> import numpy as np >>> import modopt as mo >>> obj = lambda x: np.sum(x**2) >>> grad = lambda x: 2*x >>> con = lambda x: np.array([x[0] + x[1], x[0] - x[1]]) >>> jac = lambda x: np.array([[1, 1], [1, -1]]) >>> xl = np.array([1.0, -np.inf]) >>> x0 = np.array([500., 50.]) >>> cl = 1.0 >>> cu = np.array([1., np.inf]) >>> problem = mo.ProblemLite(x0, obj=obj, grad=grad, con=con, jac=jac, xl=xl, cl=cl, cu=cu) >>> optimizer = mo.SLSQP(problem, recording=True) >>> results = optimizer.solve() >>> from modopt.postprocessing import load_results >>> load_results(results['out_dir']+'/record.hdf5') {'con_evals': 3, 'fun': 1.0..., 'grad_evals': 2, 'hess_evals': 0, 'jac': array([...2.0...e+00, ...]), 'jac_evals': 2, 'message': 'Optimization terminated successfully',... 'nfev': 2, 'nit': 2, 'njev': 2, 'obj_evals': 2, 'out_dir': '...', 'reused_callbacks': 0, 'status': 0, 'success': True, 'total_callbacks': 9, 'x': array([...1.0..., ...])}
- modopt.postprocessing.load_attributes(filepath)[source]
Load the attributes of optimization from the record as a dictionary.
- Parameters
- filepathstr
Path to the record file.
- Returns
- out_datadict
Dictionary with optimization attributes.
Examples
>>> import numpy as np >>> import modopt as mo >>> obj = lambda x: np.sum(x**2) >>> grad = lambda x: 2*x >>> con = lambda x: np.array([x[0] + x[1], x[0] - x[1]]) >>> jac = lambda x: np.array([[1, 1], [1, -1]]) >>> xl = np.array([1.0, -np.inf]) >>> x0 = np.array([500., 50.]) >>> cl = 1.0 >>> cu = np.array([1., np.inf]) >>> problem = mo.ProblemLite(x0, obj=obj, grad=grad, con=con, jac=jac, xl=xl, cl=cl, cu=cu) >>> optimizer = mo.SLSQP(problem, recording=True) >>> results = optimizer.solve() >>> from modopt.postprocessing import load_attributes >>> load_attributes(results['out_dir']+'/record.hdf5') {'c_lower': array([1., 1.]), 'c_scaler': array([1., 1.]), 'c_upper': array([ 1., inf]), 'constrained': True, 'hot_start_from': 'None', 'modopt_output_files': ['directory: ...', 'modopt_results.out', 'record.hdf5'], 'nc': 2, 'nx': 2, 'o_scaler': array([1.]), 'problem_name': 'unnamed_problem', 'readable_outputs': [], 'recording': 'True', 'solver_name': 'scipy-slsqp', 'solver_options-callback': 'None', 'solver_options-disp': False, 'solver_options-ftol': 1e-06, 'solver_options-maxiter': 100, 'timestamp': '...', 'visualize': [], 'x0': array([500., 50.]), 'x_lower': array([ 1., -inf]), 'x_scaler': array([1., 1.]), 'x_upper': array([inf, inf])}
- modopt.postprocessing.load_variables(filepath, vars, callback_context=False)[source]
Load specified variable iterates from the record file. Returns a dictionary with the variable names as keys and lists of variable iterates as values. Note that the keys for callback variables will be prefixed with ‘callback_’ as opposed to optimizer variables that will have its key as the specified variable name.
- Parameters
- filepathstr
Path to the record file.
- varsstr or list
Variable names to load from the record file. If only specific scalar variables are needed from an array, use the format ‘var_name[idx]’. For example, ‘x[0]’ will load the iterates for the first element of the array ‘x’, and ‘jac[i,j]’ will load the iterates for the (i,j)-th element of the array ‘jac’.
- callback_contextbool, default=False
If True, load the callback index and inputs for each callback variable in
vars, in addition to the callback variable iterates. The context is stored as a separate list in the output dictionary with its key formatted as the variable name prefixed by ‘callback_context_’. Each context in the list is a dictionary with the keys ‘callback_index’, and the name of the input variable(s) used in the callback (e.g. ‘x’, ‘lag_mult’, etc.).
- Returns
- out_datadict
Dictionary with variable names as keys and lists of variable iterates as values. Keys for callback variables is prefixed with ‘callback_’ and keys for callback variable context is prefixed with ‘callback_context_’.
Examples
>>> import numpy as np >>> import modopt as mo >>> obj = lambda x: np.sum(x**2) >>> grad = lambda x: 2*x >>> con = lambda x: np.array([x[0] + x[1], x[0] - x[1]]) >>> jac = lambda x: np.array([[1, 1], [1, -1]]) >>> xl = np.array([1.0, -np.inf]) >>> x0 = np.array([500., 50.]) >>> cl = 1.0 >>> cu = np.array([1., np.inf]) >>> problem = mo.ProblemLite(x0, obj=obj, grad=grad, con=con, jac=jac, xl=xl, cl=cl, cu=cu) >>> optimizer = mo.SLSQP(problem, recording=True) >>> results = optimizer.solve() >>> from modopt.postprocessing import load_variables >>> load_variables(results['out_dir']+'/record.hdf5', ['x[0]', 'obj', 'con[1]', 'grad', 'jac[0,1]']) {'x[0]': [500.0, 1.0..., 1.0...], 'callback_x[0]': [500.0, 500.0, 500.0, 500.0, 500.0, 1.0..., 1.0..., 1.0..., 1.0...], 'callback_obj': [252500.0, 1.0...], 'callback_con[1]': [450.0, 450.0, ...], 'callback_grad': [array([1000., 100.]), array([...2.00000...e+00, ...])], 'callback_jac[0,1]': [1.0, 1.0]} >>> load_variables(results['out_dir']+'/record.hdf5', ['x[0]', 'obj', 'con[1]', 'grad', 'jac[0,1]'], callback_context=True) {'x[0]': [500.0, 1.0..., 1.0...], 'callback_x[0]': [500.0, 500.0, 500.0, 500.0, 500.0, 1.0..., 1.0..., 1.0..., 1.0...], 'callback_context_x[0]': [{'callback_index': 0, 'x': array([500., 50.])}, {'callback_index': 1, 'x': array([500., 50.])}, ..., {'callback_index': 8, 'x': array([...1.000...e+00, ...])}], 'callback_obj': [252500.0, 1.0...], 'callback_context_obj': [{'callback_index': 1, 'x': array([500., 50.])}, {'callback_index': 5, 'x': array([...1.000...e+00, ...])}], 'callback_con[1]': [450.0, 450.0, ...], 'callback_context_con[1]': [{'callback_index': 0, 'x': array([500., 50.])}, {'callback_index': ..., 'x': array([500., 50.])}, {'callback_index': 6, 'x': array([...1.000...e+00, ...])}], 'callback_grad': [array([1000., 100.]), array([...2.00000...e+00, ...])], 'callback_context_grad': [{'callback_index': 2, 'x': array([500., 50.])}, {'callback_index': 7, 'x': array([...1.000...e+00, ...])}], 'callback_jac[0,1]': [1.0, 1.0], 'callback_context_jac[0,1]': [{'callback_index': ..., 'x': array([500., 50.])}, {'callback_index': 8, 'x': array([...1.000...e+00, ...])}]}
- modopt.postprocessing.visualize(filepath, vars, save_figname=None)[source]
Visualize different scalar variables using the saved data from the record file.
The variables to visualize should be a list of strings, where each string is the name of a variable to visualize. Some examples for the variables are as follows (availability depends on the optimizer used):
‘obj’ : the objective function value
‘opt’ : the optimality measure
‘feas’ : the feasibility measure
‘x[i]’ : the i-th variable value
‘con[i]’ : the i-th constraint value
‘jac[i,j]’ : the (i,j)-th element of the Jacobian matrix
‘grad[i]’ : the i-th gradient value
‘lmult[i]’ : the i-th 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
- filepathstr
Path to the record file.
- varsstr or list of str
List of variables to visualize.
- save_fignamestr, default=None
Path to save the figure. If None, the figure will not be saved.
Examples
>>> import numpy as np >>> import modopt as mo >>> obj = lambda x: np.sum(x**2) >>> grad = lambda x: 2*x >>> con = lambda x: np.array([x[0] + x[1], x[0] - x[1]]) >>> jac = lambda x: np.array([[1, 1], [1, -1]]) >>> xl = np.array([1.0, -np.inf]) >>> x0 = np.array([500., 50.]) >>> cl = 1.0 >>> cu = np.array([1., np.inf]) >>> problem = mo.ProblemLite(x0, obj=obj, grad=grad, con=con, jac=jac, xl=xl, cl=cl, cu=cu) >>> optimizer = mo.SLSQP(problem, recording=True) >>> results = optimizer.solve() >>> from modopt.postprocessing import visualize >>> visualize(results['out_dir']+'/record.hdf5', ['x[0]', 'obj', 'con[1]', 'grad[0]', 'jac[0,1]'])