## Using the C interfaceFor the most flexible and best performing solver, use C directly. Once you have generated code online, do the following. Download and extract the ‘cvxgen.zip’ archive for your problem. This will create a subdirectory called `cvxgen/`.CVXGEN automatically creates random example data, (which may not be a feasible, unbounded problem instance). Use this to test compilation and basic operation, by typing `make`, then`./testsolver`. Check several iterations occur.Integrate the CVXGEN solver using `testsolver.c`, or the following example.
## Complete exampleThis is a complete C file that uses a CVXGEN solver. It is similar
to the // Load and instantiate global namespaces with relevant data and working space. #include "solver.h" Vars vars; Params params; Workspace work; Settings settings; void load_data(Params params) { // In this function, load all problem instance data. params.A[i] = ...; ... } void use_solution(Vars vars) { // In this function, use the optimization result. ... = vars.x[i]; } int main(int argc, char **argv) { set_defaults(); // Set basic algorithm parameters. setup_indexing(); for (;;) { // Main control loop. load_data(params); // Solve our problem at high speed! num_iters = solve(); // Recommended: check work.converged == 1. use_solution(vars) } } Now we will look at the parts of this example in more detail. ## Allocating memoryYou need to allocate space for four structures, all defined in Vars vars; Params params; Workspace work; Settings settings; In a future revision, these may be passed between functions to allow more flexibility. Contact me if you have thoughts about this. ## Compilers and librariesWe strongly recommend you use the latest possible version of CVXGEN uses the ```
#define ZERO_LIBRARY_MODE
``` to ## Setting parameters and retrieving variablesAll data in CVXGEN are stored in flat arrays, in column-major form
with zero-based indices. (Thus, entries 0–3, say, are the first column of a
matrix with 4 rows.) In particular, for a
parameter matrix ,
entry is stored in // Set all entries of a 5x3 matrix A. for (int i = 0; i < 5; i++) for (int j = 0; i < 3; i++) params.A[i+j*5] = ...; // Set the scalar parameter lambda. Note the 0 array index. params.lambda[0] = ...; // Variables are retrieved in the same way, from the vars structure. for (int i = 0; i < n; i++) printf(" %9.4f\n", vars.x[i]); ## Parameter checkingFor performance and embedding reasons, no checks will be performed on parameters of sizes, shapes or attributes. CVXGEN may do something useful if you violate your parameter specifications, but it's unlikely, and definitely not recommended or guaranteed. ## Indexed parameters or variablesIn your CVXGEN problem specification, you may have specified indexed parameters or variables, such as parameters
y[i] (3), i=1..3 end You can provide data for these parameters, in C, in two different ways. With the first, an underscore separates the index and the variable name. With the second, we use two indices to index into the parameter, and then into the parameter's data array. The latter is more convenient for use with loops. // Assign initial parameters, option 1. params.y_1[j] = ...; // Assign initial parameters, option 2. Equivalent. setup_indexing(); // call this once, at set up, before using option 2. params.y[1][j] = ...; ## Modifying solver settingsIf you want to modify solver behavior, you can change the settings. See more information about the settings. settings.verbose = 0; // disable output of solver progress. settings.max_iters = 10; // reduce the maximum iteration count, from 25. settings.eps = 0.1; // reduce the required objective tolerance, from 1e-6. settings.eps = 1e-2; // reduce the required residual tolerances, from 1e-4. // Solve with these settings. solve(); |