/* nag_ip_mps_read (h02buc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 *
 */

#include <nag.h>
#include <stdio.h>
#include <string.h>
#include <nag_stdlib.h>
#include <nage04.h>
#include <nagh02.h>

int main(void)
{
  const char *optionsfile = "h02buce.opt";
  Integer exit_status = 0;
  Nag_Boolean *intvar;
  Integer m, n;
  Nag_H02_Opt options;
  double *a, *bl, *bu, *c, objf, *x;
  Nag_Comm comm;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_ip_mps_read (h02buc) Example Program Results\n");

  /* Initialize options structure and read option settings. */
  /* nag_ip_init (h02xxc).
   * Initialize option structure to null values
   */
  nag_ip_init(&options);
  /* nag_ip_read (h02xyc).
   * Read optional parameter values from a file
   */
  fflush(stdout);
  nag_ip_read("h02buc", optionsfile, &options, (Nag_Boolean) Nag_TRUE,
              "stdout", &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ip_read (h02xyc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Read MPSX data */
  /* nag_ip_mps_read (h02buc), see above. */
  nag_ip_mps_read(0, Nag_TRUE, &n, &m, &a, &bl, &bu, &intvar, &c, &x,
                  &options, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ip_mps_read (h02buc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Solve IP problem defined by data */
  options.list = Nag_TRUE;
  options.print_level = Nag_Soln;
  /* nag_ip_bb (h02bbc).
   * Solves integer programming problems using a branch and
   * bound method
   */
  nag_ip_bb(n, m, a, n, bl, bu, intvar, c, (double *) 0, 0, NULLFN,
            x, &objf, &options, &comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ip_bb (h02bbc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Free memory allocated by nag_ip_mps_read (h02buc) */
  /* nag_ip_mps_free (h02bvc), see above. */
  nag_ip_mps_free(&a, &bl, &bu, &intvar, &c, &x);

  /* Free options memory */
  /* nag_ip_free (h02xzc).
   * Free NAG allocated memory from option structures
   */
  nag_ip_free(&options, "all", &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ip_free (h02xzc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

END:

  return exit_status;
}