/* nag_opt_handle_solve_socp_ipm (e04ptc) Example Program.
*
* Copyright 2023 Numerical Algorithms Group.
*
* Mark 29.3, 2023.
*/
#include <nag.h>
#include <stdio.h>
int main(void) {
Integer nclin, nvar, nnza, nnzu, nnzuc, exit_status, i, x_idx;
Integer icone, ncones, nvar_cone, idgroup = 0;
Integer idlc;
Integer verbose_output;
Integer *irowa = 0, *icola = 0;
Integer *vidx_cone = 0;
double *c = 0, *a = 0, *bla = 0, *bua = 0, *xl = 0, *xu = 0, *x = 0, *u = 0,
*uc = 0;
double rinfo[100], stats[100];
char cone_type[10] = "";
void *handle = 0;
/* Nag Types */
Nag_Comm comm;
NagError fail;
exit_status = 0;
printf("nag_opt_handle_solve_socp_ipm (e04ptc) Example Program Results\n\n");
fflush(stdout);
/* Read the data file and allocate memory */
scanf(" %*[^\n]"); /* Skip heading in data file */
scanf("%" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT " %*[^\n]", &nvar, &nclin,
&nnza);
/* Allocate memory */
nnzu = 2 * nvar + 2 * nclin;
nnzuc = 0;
if (!(irowa = NAG_ALLOC(nnza, Integer)) ||
!(icola = NAG_ALLOC(nnza, Integer)) || !(c = NAG_ALLOC(nvar, double)) ||
!(a = NAG_ALLOC(nnza, double)) || !(bla = NAG_ALLOC(nclin, double)) ||
!(bua = NAG_ALLOC(nclin, double)) || !(xl = NAG_ALLOC(nvar, double)) ||
!(xu = NAG_ALLOC(nvar, double)) ||
!(vidx_cone = NAG_ALLOC(nvar, Integer))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read objective */
for (i = 0; i < nvar; i++) {
scanf("%lf", &c[i]);
}
scanf("%*[^\n]");
/* Read constraint matrix row indices */
for (i = 0; i < nnza; i++) {
scanf("%" NAG_IFMT, &irowa[i]);
}
scanf("%*[^\n]");
/* Read constraint matrix col indices */
for (i = 0; i < nnza; i++) {
scanf("%" NAG_IFMT, &icola[i]);
}
scanf("%*[^\n]");
/* Read constraint matrix values */
for (i = 0; i < nnza; i++) {
scanf("%lf", &a[i]);
}
scanf("%*[^\n]");
/* Read linear constraints lower bounds */
for (i = 0; i < nclin; i++) {
scanf("%lf ", &bla[i]);
}
scanf("%*[^\n]");
/* Read linear constraints upper bounds */
for (i = 0; i < nclin; i++) {
scanf("%lf ", &bua[i]);
}
scanf("%*[^\n]");
/* Read variables lower bounds */
for (i = 0; i < nvar; i++) {
scanf("%lf ", &xl[i]);
}
scanf("%*[^\n]");
/* Read variables upper bounds */
for (i = 0; i < nvar; i++) {
scanf("%lf ", &xu[i]);
}
scanf("%*[^\n]");
/* Create the problem handle */
/* nag_opt_handle_init (e04rac).
* Initialize an empty problem handle with NVAR variables. */
nag_opt_handle_init(&handle, nvar, NAGERR_DEFAULT);
/* nag_opt_handle_set_linobj (e04rec)
* Define a linear objective */
nag_opt_handle_set_linobj(handle, nvar, c, NAGERR_DEFAULT);
/* nag_opt_handle_set_simplebounds (e04rhc)
* Define bounds on the variables */
nag_opt_handle_set_simplebounds(handle, nvar, xl, xu, NAGERR_DEFAULT);
/* nag_opt_handle_set_linconstr (e04rjc)
* Define linear constraints */
idlc = 0;
nag_opt_handle_set_linconstr(handle, nclin, bla, bua, nnza, irowa, icola, a,
&idlc, NAGERR_DEFAULT);
/* nag_opt_handle_set_group (e04rbc)
* Define cone constraints */
/* Read number of cones */
scanf("%" NAG_IFMT " %*[^\n]", &ncones);
/* Read cone constraints */
for (icone = 0; icone < ncones; icone++) {
scanf("%" NAG_IFMT " %*[^\n]", &nvar_cone);
scanf("%9s", cone_type);
scanf("%*[^\n]");
for (i = 0; i < nvar_cone; i++) {
scanf("%" NAG_IFMT, &vidx_cone[i]);
}
scanf("%*[^\n]");
idgroup = 0;
nag_opt_handle_set_group(handle, cone_type, nvar_cone, vidx_cone, &idgroup,
NAGERR_DEFAULT);
nnzuc = nnzuc + nvar_cone;
}
/* Allocate memory for x, u and uc */
if (!(x = NAG_ALLOC(nvar, double)) || !(u = NAG_ALLOC(nnzu, double)) ||
!(uc = NAG_ALLOC(nnzuc, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
for (i = 0; i < nvar; i++) {
x[i] = 0.0;
}
/* Set this to 1 to cause nag_opt_handle_solve_socp_ipm (e04ptc)
* to produce intermediate progress output */
verbose_output = 0;
if (verbose_output) {
/* nag_opt_handle_opt_set (e04zmc) */
/* Require printing of primal and dual solutions at the end of the solve */
nag_opt_handle_opt_set(handle, "Print Solution = Yes", NAGERR_DEFAULT);
} else {
/* Turn off printing of intermediate progress output */
nag_opt_handle_opt_set(handle, "Print Level = 1", NAGERR_DEFAULT);
}
/* nag_opt_handle_solve_socp_ipm (e04ptc) */
INIT_FAIL(fail);
nag_opt_handle_solve_socp_ipm(handle, nvar, x, nnzu, u, nnzuc, uc, rinfo,
stats, NULLFN, &comm, &fail);
if (fail.code != NE_NOERROR && fail.code != NW_NOT_CONVERGED) {
printf("nag_opt_handle_solve_socp_ipm (e04ptc) failed.\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Print solution if optimal or suboptimal solution found */
printf(" Optimal X:\n");
printf(" x_idx Value\n");
for (x_idx = 0; x_idx < nvar; x_idx++) {
printf(" %5" NAG_IFMT " %12.5E\n", x_idx + 1, x[x_idx]);
}
END:
NAG_FREE(c);
NAG_FREE(irowa);
NAG_FREE(icola);
NAG_FREE(a);
NAG_FREE(bla);
NAG_FREE(bua);
NAG_FREE(xl);
NAG_FREE(xu);
NAG_FREE(x);
NAG_FREE(u);
NAG_FREE(uc);
NAG_FREE(vidx_cone);
/* nag_opt_handle_free (e04rzc).
* Destroy the problem handle and deallocate all the memory. */
if (handle)
nag_opt_handle_free(&handle, NAGERR_DEFAULT);
return exit_status;
}