C
Uno's C interface: how to use Uno from C
Uno's C interface allows you to solve an optimization model described by callback functions.
The file Uno_C_API.cpp is compiled as part of libuno (static or shared) via make, and the header Uno_C_API.h is installed via make install.
An example is available in the file example_hs015.c.
Start by including the Uno header:
Building an optimization model
Building an optimization model is incremental and starts with the information about the variables:
void* model = uno_create_model(problem_type, number_variables,
variables_lower_bounds, variables_upper_bounds, base_indexing);
The following optional elements can be added or set to the model separately: - lower bounds for the variables:
- upper bounds for the variables: - a lower bound for a given variable: - an upper bound for a given variable: - the objective function (and its gradient). It is 0 otherwise; - constraint functions (and their Jacobian);uno_set_constraints(model, number_constraints, constraint_functions,
constraints_lower_bounds, constraints_upper_bounds, number_jacobian_nonzeros,
jacobian_row_indices, jacobian_column_indices, jacobian);
uno_set_lagrangian_hessian(model, number_hessian_nonzeros, hessian_triangular_part,
hessian_row_indices, hessian_column_indices, lagrangian_hessian);
UNO_MULTIPLIER_NEGATIVE);
- user data of an arbitrary type (void*);
- an initial primal point;
- an initial dual point.
Each of these functions returns an integer that is 0 upon success and positive upon failure.
The memory for the model is allocated by the C interface and must be freed by a call to the function:
Creating an instance of the Uno solver
Create an instance of the Uno solver with a simple function call.
The memory for the solver is allocated by the C interface and must be freed by a call to the function:
Passing options to the Uno solver
Options can be passed to the Uno solver:
uno_set_solver_integer_option(solver, "max_iterations", 1000);
uno_set_solver_double_option(solver, "primal_tolerance", 1.0e-6);
uno_set_solver_bool_option(solver, "print_solution", true);
uno_set_solver_string_option(solver, "hessian_model", "exact");
Loading options from a file (overwrites existing options):
Getting typed value of an option from the Uno solver:
uno_int uno_get_solver_integer_option(solver, "max_iterations");
size_t uno_get_solver_unsigned_integer_option(solver, "max_iterations");
double uno_set_solver_double_option(solver, "primal_tolerance");
bool uno_get_solver_bool_option(solver, "print_solution");
const char* uno_get_solver_string_option(solver, "hessian_model");
Setting a preset has Uno mimic an existing solver:
Setting solver callbacks
Setting the user callbacks to the Uno solver:
uno_set_solver_callbacks(solver, notify_acceptable_iterate_callback, user_termination_callback, user_data);
Setting the logger stream callback:
and reset the logger stream to the standard output:
Solving the model
The model can then be solved by Uno:
Inspecting the result
A set of functions allows you to inspect the result of the optimization:
- the optimization status (UNO_SUCCESS, UNO_ITERATION_LIMIT, UNO_TIME_LIMIT, UNO_EVALUATION_ERROR, UNO_ALGORITHMIC_ERROR):
UNO_NOT_OPTIMAL, UNO_FEASIBLE_KKT_POINT, UNO_FEASIBLE_FJ_POINT, UNO_INFEASIBLE_STATIONARY_POINT, UNO_FEASIBLE_SMALL_STEP, UNO_INFEASIBLE_SMALL_STEP, UNO_UNBOUNDED):
- the objective value of the solution:
- the primal solution:
- the dual solution associated with the general constraints:
- the dual solution associated with the lower bounds:
- the dual solution associated with the upper bounds:
- the primal feasibility measure at the solution:
- the stationarity measure at the solution:
- the complementarity measure at the solution: