pyoptsolver package

Submodules

pyoptsolver.pyipopt module

pyoptsolver.pyoptsolver module

class pyoptsolver.pyoptsolver.OptConfig(backend='ipopt', **kw)[source]

Bases: object

This class is used to keep track of various optimization configurations.

It provides a unified interface for interacting with those solvers. Some custom options are also supported, but might not work depending on the backend.

ipopt_options = set(['exact_hessian', 'fd_jacobian', 'linear_solver', 'print_freq'])
scipy_kws = set(['tol'])
scipy_option_kws = set(['barrier_tol', 'disp', 'factorization_method', 'grad', 'gtol', 'initial_barrier_parameter', 'initial_barrier_tolerance', 'initial_constr_penalty', 'initial_tr_radius', 'xtol'])
shared_options = set(['deriv_check', 'fea_tol', 'major_iter', 'opt_tol', 'print_level'])
snopt_options = set(['iter_limit', 'minor_iter', 'print_file'])
class pyoptsolver.pyoptsolver.OptSolver(problem, config)[source]

Bases: object

solve_guess(guess)[source]
solve_rand()[source]
class pyoptsolver.pyoptsolver.TrustConstrSolver(problem, option)[source]

Bases: object

A wrapper that builds on a already declared problem.

solve_guess(guess)[source]
solve_rand()[source]

pyoptsolver.pysnopt module

Module contents

class pyoptsolver.IpoptConfig

Bases: pybind11_builtins.pybind11_object

A class that specifies configuration to ipopt solver.

This class has the default constructor.

Attributes:
print_level (int): level for printing = 4 max_iter (int): maximum iteration = 1000 linear_solver (string): linear solver = “mumps”; hessian_approximation (string): Hessian approach = “limited-memory” jacobian_appximation (string): Jacobian approach = “exact”; derivative_test (string): if test derivative = “no”; tol (float): convergence tolerance = 1e-6; constr_vio_tol (float): tolerance on constraint = 1e-6; max_cpu_time (float): computation time = 1e6;
add_float_option(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: unicode, arg1: float) → None

Add a float option to configurations.

Args:
option (str): the option to configure value (float): the float value for configuration
add_int_option(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: unicode, arg1: int) → None

Add an integer option to configurations.

Args:
option (str): the option to configure value (int): the integer value for configuration
add_string_option(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: unicode, arg1: unicode) → None

Add a string option to configurations.

Args:
option (str): the option to configure value (str): the string value for configuration
constr_vio_tol
derivative_test
enable_deriv_check(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, lvl: int=0L) → None

Set up the derivative check param.

Args:
level (int): the derivative check level
enable_exact_hessian(self: pyoptsolver.pyoptsolvercpp.IpoptConfig) → None

Enable usage of exact Hessian.

enable_fd_jacobian(self: pyoptsolver.pyoptsolvercpp.IpoptConfig) → None

Enable usage of finite-difference approximation of Jacobian.

hessian_approximation
jacobian_approximation
linear_solver
max_cpu_time
max_iter
print_frequency_iter
print_level
set_fea_tol(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: float) → None

Set up the feasibility tolerance.

Args:
tol (float): the tolerance
set_linear_solver(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: unicode) → None

Set the linear solver.

Args:
solver (str): the selected linear solver.
set_major_iter(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: int) → None

Set up the major iteration limit.

Args:
iter (int): limit for major iteration
set_opt_tol(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: float) → None

Set up the optimization tolerance.

Args:
tol (float): the tolerance
set_print_freq(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: int) → None

Set up the print frequency.

Args:
freq (int): the print frequency
set_print_level(self: pyoptsolver.pyoptsolvercpp.IpoptConfig, arg0: int) → int

Set up the print level.

Args:
level (int): the print level
tol
class pyoptsolver.IpoptSolver

Bases: pybind11_builtins.pybind11_object

The solver class.

solve_guess(self: pyoptsolver.pyoptsolvercpp.IpoptSolver, arg0: numpy.ndarray[float64[m, 1]]) → pyoptsolver.pyoptsolvercpp.OptResult
solve_rand(self: pyoptsolver.pyoptsolvercpp.IpoptSolver) → pyoptsolver.pyoptsolvercpp.OptResult
class pyoptsolver.SnoptConfig

Bases: pybind11_builtins.pybind11_object

A class that specifies configuration to snopt solver, keep for backward compatibility.

This class has the default constructor.

Attributes:
name (str=”Toy”): the name for the problem. print_file (str): the filename for printing results. print_level (int=0): the print level for major iteration; 0 disable iterations; 1 enables iterations. minor_print_level (int=0): print level for minor iteration. verify_level (int=0): level for gradient verification; 0 disable; 3 full verify. major_iter_limit (int=0): major iteration limit minor_iter_limit (int=0): minor iteration limit iter_limit (int=0): total iteration limit opt_tol (float=1e-6): tolerance for optimality fea_tol (float=1e-6): tolerance for feasibility int_options (list of (string, int) tuple): integer options float_options (list of (string, float) tuple): float options string_options (list of (string, string) tuple): string options
addFloatOption(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: unicode, arg1: float) → None
addIntOption(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: unicode, arg1: int) → None
addStringOption(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: unicode) → None
add_float_option(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: unicode, arg1: float) → None

Add a float option to configurations.

Args:
option (str): the option to configure value (float): the float value for configuration
add_int_option(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: unicode, arg1: int) → None

Add an integer option to configurations.

Args:
option (str): the option to configure value (int): the integer value for configuration
add_string_option(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: unicode) → None

Add a string option to configurations.

Args:
option (str): the option to configure
enable_deriv_check(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, lvl: int=3L) → None

Set up the derivative check param.

Args:
level (int): the derivative check level
feaTol
fea_tol
floatOptions
float_options
intOptions
int_options
iterLimit
iter_limit
majorIterLimit
major_iter_limit
minorIterLimit
minorPrintLevel
minor_iter_limit
minor_print_level
name
optTol
opt_tol
printFile
printLevel
print_file
print_level
set_fea_tol(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: float) → None

Set up the feasibility tolerance.

Args:
tol (float): the tolerance
set_iter_limit(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: int) → None

Set up the total iteration limit.

Args:
iter (int): limit for total iteration
set_major_iter(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: int) → None

Set up the major iteration limit.

Args:
iter (int): limit for major iteration
set_minor_iter(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: int) → None

Set up the minor iteration limit.

Args:
iter (int): limit for minor iteration
set_opt_tol(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: float) → None

Set up the optimization tolerance.

Args:
tol (float): the tolerance
set_print_level(self: pyoptsolver.pyoptsolvercpp.SnoptConfig, arg0: int) → int

Set up the print level.

Args:
level (int): the print level
stringOptions
string_options
verifyLevel
verify_level
class pyoptsolver.SnoptSolver

Bases: pybind11_builtins.pybind11_object

The snopt solver class that is constructed by a SnoptProblem object and a SnoptConfig object.

Args:
prob (SnoptProblem): the problem object. cfg (SnoptConfig): the configuration.
fEval(*args, **kwargs)

Overloaded function.

  1. fEval(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: numpy.ndarray[float64[m, 1], flags.writeable]) -> numpy.ndarray[float64[m, 1]]
  2. fEval(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: numpy.ndarray[float64[m, 1], flags.writeable]) -> numpy.ndarray[float64[m, 1]]
getInfo(self: pyoptsolver.pyoptsolvercpp.SnoptSolver) → int
get_info(self: pyoptsolver.pyoptsolvercpp.SnoptSolver) → int

Get the inform variable from the solver. It indicates solving status.

Returns:
int: the info.

Given an initial, use search style to control how solver proceed.

Args:
guess (ndarray): an initial guess iter (int): initial iteration time step (int): iteration time for later search step_num (int): how many more step iterations do we play. abstol (float): absolute cost tolerance, exit if improvement is smaller than it reltol (float): relative cost tolerance, exit if improvement is smaller than reltol * last cost
Returns:
SnoptResult: a SnoptResult object costs: ndarray, recorded costs during search times: ndarray, recorded used times during search
setFeaTol(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: float) → None
setFloatOption(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: unicode, arg1: float) → None
setIntOption(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: unicode, arg1: int) → None
setIntWorkspace(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: int) → None
setMajorIter(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: int) → None
setOptTol(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: float) → None
setPrintFile(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: unicode) → None
setRealWorkspace(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: int) → None
set_int_workspace(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: int) → None

Set the integer workspace size, this is necessary sometimes.

Args:
size (int): the desired integer workspace size.
set_real_workspace(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: int) → None

Set the real workspace size, this is necessary sometimes.

Args:
size (int): the desired real workspace size.
solveGuess(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: numpy.ndarray[float64[m, 1]]) → pyoptsolver.pyoptsolvercpp.OptResult
solveRand(self: pyoptsolver.pyoptsolvercpp.SnoptSolver) → pyoptsolver.pyoptsolvercpp.OptResult
solve_guess(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: numpy.ndarray[float64[m, 1]]) → pyoptsolver.pyoptsolvercpp.OptResult

Solve the problem using a user-specified guess.

Args:
x (ndarray): an initial guess.
Returns:
SnoptResult: a SnoptResult object.
solve_more(self: pyoptsolver.pyoptsolvercpp.SnoptSolver, arg0: int) → pyoptsolver.pyoptsolvercpp.OptResult

Use hot restart and continue a few major iterations. It can be used when SNOPT is solely used for backtracking.

Args:
iter (int): desired iteration number
Returns:
SnoptResult: a SnoptResult object
solve_rand(self: pyoptsolver.pyoptsolvercpp.SnoptSolver) → pyoptsolver.pyoptsolvercpp.OptResult

Solve the problem using a random guess generated by the solver itself.

Returns:
SnoptResult: a SnoptResult object.
class pyoptsolver.OptProblem

Bases: pybind11_builtins.pybind11_object

A class sub-classed by users to define a custom optimization problem.

Currentmodule:
__constraint__(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]], arg1: numpy.ndarray[float64[m, 1], flags.writeable]) → int

This class defines an optimization problem to optimize \(y(0)\) where \(y=f(x)+Ax\) subject to constraints \(lb \le y \le ub\) and \(xlb \le x \le xub\).

Attributes:
nx (int): number of variables to be optimized. The same with FunBase. nf (int): number of constraints. The same with FunBase. nG (int=0): nnz of the Jacobian matrix. The same with FunBase. grad (bool=False): if the function __callg__ has been implemented so we enable gradient. lb (ndarray): lower bound for f. It is readonly, use get_lb() to get a reference. ub (ndarray): upper bound for f. It is readonly, use get_ub() to get a reference. xlb (ndarray): lower bound for x. It is readonly, use get_xlb() to get a reference. xub (ndarray): upper bound for x. It is readonly, use get_xub() to get a reference. Aval (ndarray): the value ndarray for triplet A, use get_aval() to get a reference. Arow (ndarray): integer ndarray of rows for triplet A, use get_arow() to get a reference. Acol (ndarray): integer ndarray of columns for triplet A, use get_acol() to get a reference.
Note:
This class has 2 constructors. Refer to FunBase for details. The first entry of lb and ub should be -np.inf and np.inf, respectively. For linear function part, you have to set function return 0. Use ipopt_style() and snopt_style() to toggle function evaluation mode.
Acol
Arow
Aval
batch_set_lb(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]], arg1: int) → None

Set lb in batch mode by specifying an ndarray and starting index.

This value is not necessary since you can get a reference by get_lb and directly operate on that. We keep it here for backward compatibility. Other similar functions are not documented. Please refer to this.

Args:
lb (ndarray): a segment of lower bound to be set ind0 (int): the starting index for this segment.
batch_set_ub(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]], arg1: int) → None
batch_set_xlb(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]], arg1: int) → None
batch_set_xub(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]], arg1: int) → None
check_overlap(self: pyoptsolver.pyoptsolvercpp.OptProblem) → None

Check if user provided linear and nonlinear Jacobian has overlap.

detect_prob_size(*args, **kwargs)

Overloaded function.

  1. detect_prob_size(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: int, arg1: int) -> None

    Automatically detect nf and nG.

    This function requires __callf__ or __callg__ return non-trivial values.

    Args:

    nf (int): an estimated size for \(y\), it has to be larger. nG (int): an estimated size for Jacobian, it has to be larger. If set to 0, __callf__ is used and has to be defined. Otherwise __callg__ has to be defined.

  2. detect_prob_size(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]], arg1: int, arg2: int) -> None

    Automatically detect nf and nG.

    This function requires __callf__ or __callg__ return non-trivial values.

    Args:

    x( ndarray): the guess being used. This will change nf too so please make sure its size is correct nf (int): an estimated size for \(y\), it has to be larger. nG (int): an estimated size for Jacobian, it has to be larger. If set to 0, __callf__ is used and has to be defined. Otherwise __callg__ has to be defined.

enable_timer(self: pyoptsolver.pyoptsolvercpp.OptProblem) → None

Turn on internal timer. By doing so, user is able to call get_timer function.

eval_constr(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → numpy.ndarray[float64[m, 1]]
eval_cost(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → float
eval_f(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → numpy.ndarray[float64[m, 1]]

Evaluate __callf__ once.

This serves for debugging purpose.

Args:
x (ndarray): a candidate solution
Returns:
y (ndarray): the evaluated function.
eval_g(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → Tuple[numpy.ndarray[float64[m, 1]], numpy.ndarray[float64[m, 1]], numpy.ndarray[int64[m, 1]], numpy.ndarray[int64[m, 1]]]

Evaluate __callf__ once.

This serves for debugging purpose.

Args:
x (ndarray): a candidate solution
Returns:
y (ndarray): the evaluated function. G (ndarray): the value part of the returned Jacobian triplet. row (ndarray): the row part of the returned Jacobian triplet. col (ndarray): the column part of the returned Jacobian triplet.
eval_gradient(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → numpy.ndarray[float64[m, 1]]
eval_jacobian(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → Tuple[numpy.ndarray[float64[m, 1]], numpy.ndarray[int64[m, 1]], numpy.ndarray[int64[m, 1]]]
get_acol(self: pyoptsolver.pyoptsolvercpp.OptProblem) → numpy.ndarray[int32[m, 1], flags.writeable]
get_arow(self: pyoptsolver.pyoptsolvercpp.OptProblem) → numpy.ndarray[int32[m, 1], flags.writeable]
get_aval(self: pyoptsolver.pyoptsolvercpp.OptProblem) → numpy.ndarray[float64[m, 1], flags.writeable]
get_lb(self: pyoptsolver.pyoptsolvercpp.OptProblem) → numpy.ndarray[float64[m, 1], flags.writeable]
get_timer(self: pyoptsolver.pyoptsolvercpp.OptProblem) → Tuple[numpy.ndarray[float64[m, 1]], numpy.ndarray[float64[m, 1]], numpy.ndarray[float64[m, 1]]]

Return a tuple of time, cost, constr

get_ub(self: pyoptsolver.pyoptsolvercpp.OptProblem) → numpy.ndarray[float64[m, 1], flags.writeable]
get_xlb(self: pyoptsolver.pyoptsolvercpp.OptProblem) → numpy.ndarray[float64[m, 1], flags.writeable]
get_xub(self: pyoptsolver.pyoptsolvercpp.OptProblem) → numpy.ndarray[float64[m, 1], flags.writeable]
grad
ipopt_style(self: pyoptsolver.pyoptsolvercpp.OptProblem) → None
ipstyle
lb
nG
nH
nf
nx
random_gen_x(self: pyoptsolver.pyoptsolvercpp.OptProblem) → numpy.ndarray[float64[m, 1]]

Randomly generate an initial guess of x within lb and ub bounds.

Returns:
ndarray: the generated initial guess.
set_a_by_matrix(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, n], flags.c_contiguous]) → None

Set triplet matrix A by a dense row major matrix, i.e. 2d ndarray of size (m, n).

Args:
mat (ndarray): the 2d float A matrix in row major.
set_a_by_triplet(*args, **kwargs)

Overloaded function.

  1. set_a_by_triplet(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]], arg1: numpy.ndarray[int32[m, 1]], arg2: numpy.ndarray[int32[m, 1]]) -> None

    Set triplet matrix A by the value, row, column triplet pairs.

    Args:

    val (ndarray): the value ndarray row (ndarray): the integer row ndarray col (ndarray): thet integer column ndarray

  2. set_a_by_triplet(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]], arg1: numpy.ndarray[int64[m, 1]], arg2: numpy.ndarray[int64[m, 1]]) -> None

    Set triplet matrix A by the value, row, column triplet pairs.

    Args:

    val (ndarray): the value ndarray row (ndarray): the integer row ndarray col (ndarray): thet integer column ndarray

set_debug_verbose(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: bool) → None

Turn on or off verbosity.

set_lb(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → None
set_ub(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → None
set_xlb(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → None
set_xub(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: numpy.ndarray[float64[m, 1]]) → None
snopt_style(self: pyoptsolver.pyoptsolvercpp.OptProblem) → None
ub
update_nf(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: int) → None

Update nf and reallocate space for lb and ub, if necessary.

Args:
nf (int): the desired nf
update_ng(self: pyoptsolver.pyoptsolvercpp.OptProblem, arg0: int) → None

Update nnz value, equivalently to directly writing to nG.

Args:
ng (int): the desired nG
xlb
xub
class pyoptsolver.OptResult

Bases: pybind11_builtins.pybind11_object

A class that contains the solution returned by either one.

This class has the default constructor.

Attributes:
flag (int): the return flag by SNOPT.solve() obj (float): the cost function sol (ndarray): a copy of the solution fval (ndarray): a copy of F lmd (ndarray): a copy of Lagrangian multipliers xmul (ndarray): a copy of multipliers associated with bounds on x
flag
fval
get_fval(self: pyoptsolver.pyoptsolvercpp.OptResult) → numpy.ndarray[float64[m, 1], flags.writeable]

Return a reference to the function evaluation without copying.

get_lambda(self: pyoptsolver.pyoptsolvercpp.OptResult) → numpy.ndarray[float64[m, 1], flags.writeable]

Return a reference to the Lagrangian multiplier without copying.

get_sol(self: pyoptsolver.pyoptsolvercpp.OptResult) → numpy.ndarray[float64[m, 1], flags.writeable]

Return a reference to the solution without copying.

get_xmul(self: pyoptsolver.pyoptsolvercpp.OptResult) → numpy.ndarray[float64[m, 1], flags.writeable]

Return a reference to the Lagrangian multiplier on bounds without copying.

lmd
obj
sol
xmul
class pyoptsolver.OptSolver(problem, config)[source]

Bases: object

solve_guess(guess)[source]
solve_rand()[source]
class pyoptsolver.OptConfig(backend='ipopt', **kw)[source]

Bases: object

This class is used to keep track of various optimization configurations.

It provides a unified interface for interacting with those solvers. Some custom options are also supported, but might not work depending on the backend.

ipopt_options = set(['exact_hessian', 'fd_jacobian', 'linear_solver', 'print_freq'])
scipy_kws = set(['tol'])
scipy_option_kws = set(['barrier_tol', 'disp', 'factorization_method', 'grad', 'gtol', 'initial_barrier_parameter', 'initial_barrier_tolerance', 'initial_constr_penalty', 'initial_tr_radius', 'xtol'])
shared_options = set(['deriv_check', 'fea_tol', 'major_iter', 'opt_tol', 'print_level'])
snopt_options = set(['iter_limit', 'minor_iter', 'print_file'])