NAG Library Manual, Mark 30.3
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_mip_handle_solve_milp (h02bkc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.3, 2024.
 */

#include <nag.h>
#include <stdio.h>

int main(void) {

  Integer nclin, nvar, nnza, nnzc, exit_status, i;
  Integer idlc, nvar_int;
  Integer *irowa = 0, *icola = 0;
  Integer *vidx_int = 0;
  double *cvec = 0, *a = 0, *bla = 0, *bua = 0, *xl = 0, *xu = 0, *x = 0;
  double rinfo[100], stats[100];
  char int_type[10] = "";
  void *handle = 0;
  /* Nag Types */
  Nag_Comm comm;

  exit_status = 0;

  printf("nag_mip_handle_solve_milp (h02bkc) 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 " %" NAG_IFMT " %" NAG_IFMT " %*[^\n]",
        &nclin, &nvar, &nnza, &nnzc, &nvar_int);
  /* Allocate memory */
  if (!(irowa = NAG_ALLOC(nnza, Integer)) ||
      !(icola = NAG_ALLOC(nnza, Integer)) ||
      !(cvec = NAG_ALLOC(nnzc, 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)) ||
      !(x = NAG_ALLOC(nvar, double)) || 
      !(vidx_int = NAG_ALLOC(nvar_int, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < nvar; i++) {
    x[i] = 0.0;
  }

  /* Read objective */
  for (i = 0; i < nnzc; i++) {
    scanf("%lf", &cvec[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]");
    scanf("%9s", int_type);
    scanf("%*[^\n]");
    for (i = 0; i < nvar_int; i++) {
      scanf("%" NAG_IFMT, &vidx_int[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, cvec, 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_property (e04rcc)
   * Define integer variables */
  nag_opt_handle_set_property(handle, int_type, nvar_int, vidx_int, NAGERR_DEFAULT);

  /* nag_opt_handle_opt_set (e04zmc)
   * Set objective sense to maximize */
  nag_opt_handle_opt_set(handle, "Task = Maximize", NAGERR_DEFAULT);
  /* Turn off option printing */
  nag_opt_handle_opt_set(handle, "Print Options = No",
                         NAGERR_DEFAULT);
  /* Require printing of the solution at the end of the solve */
  nag_opt_handle_opt_set(handle, "Print Solution = Yes", NAGERR_DEFAULT);
  /* Turn off printing of intermediate progress output */
  nag_opt_handle_opt_set(handle, "Print Level = 1", NAGERR_DEFAULT);

  /* nag_mip_handle_solve_milp (h02bkc)
   * Call MILP solver */
  nag_mip_handle_solve_milp(handle, nvar, x, rinfo, stats, NULLFN,
                              &comm, NAGERR_DEFAULT);

END:
  NAG_FREE(cvec);
  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(vidx_int);
  /* 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;
}