Initializing help system before first use

Collecting all solutions with the MIP solution pool


Type: Power generation
Rating: 3 (intermediate)
Description: We take the power generation problem stored in hpw15.mat which seeks to optimise the operating pattern of a group of electricity generators. We solve the problem collecting all solutions found during the MIP search. The optimal solution's objective and solution values are printed to screen.
File(s): mipsolpool.c
Data file(s): hpw15.mat


mipsolpool.c
/***********************************************************************
   Xpress Optimizer Examples
   =========================

   file mipsolpool.c
   `````````````````
   Generate all solutions with the MIP solution pool

   We take the power generation problem stored in hpw15.mat which seeks to
   optimise the operating pattern of a group of electricity generators. We
   solve the problem collecting all solutions found during the MIP search.
   The optimal solution's objective and solution values are printed to
   screen.

   (c) 2017 Fair Isaac Corporation
***********************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include "xprs.h"                     /* Optimizer header file */

void errormsg(const char *sSubName,int nLineNo,int nErrCode);


void main(void) {
  int nReturn;
  XPRSprob prob;
  XPRSmipsolpool msp;
  int i, nSols, nCols, iSolutionId, iSolutionIdStatus;
  double dObj, dSol;
  const char *sProblem = "hpw15";

  nReturn = XPRSinit("");
  if (nReturn!=0 && nReturn!=16) {
    char errmsgbuf[2048];
    XPRSgetlicerrmsg(errmsgbuf,2048);
    printf("We were unable to license the Xpress-optimizer: %s\n", errmsgbuf);
    exit(nReturn);
  }

  nReturn = XPRS_msp_create(&msp);
  if (nReturn!=0) errormsg("XPRS_msp_create",__LINE__,nReturn);

  nReturn = XPRScreateprob(&prob);
  if (nReturn!=0) errormsg("XPRScreateprob",__LINE__,nReturn);

  nReturn = XPRS_msp_probattach(msp, prob);
  if (nReturn!=0) errormsg("XPRS_msp_probattach",__LINE__,nReturn);

  nReturn = XPRSreadprob(prob, sProblem, "");
  if (nReturn!=0) errormsg("XPRSreadprob",__LINE__,nReturn);

  nReturn = XPRSmipoptimize(prob, "");
  if (nReturn!=0) errormsg("XPRSmipoptimize",__LINE__,nReturn);

  nReturn = XPRS_msp_getintattrib(msp, XPRS_MSP_SOLUTIONS, &nSols);
  if (nReturn!=0) errormsg("XPRS_msp_getintattrib",__LINE__,nReturn);

  if(nSols) {

    nReturn = XPRS_msp_getdblattribprobextreme(msp, prob, 0, &iSolutionId, XPRS_MSP_SOLPRB_OBJ, &dObj);
    if (nReturn!=0) errormsg("XPRS_msp_getdblattribprobextreme",__LINE__,nReturn);

    printf("Optimal Solution ID: %i\n", iSolutionId);
    printf("Optimal Objective  : %12.5f\n", dObj);

    nReturn = XPRS_msp_getintattribsol(msp, iSolutionId, &iSolutionIdStatus, XPRS_MSP_SOL_COLS, &nCols);
    if (nReturn!=0) errormsg("XPRS_msp_getintattribsol",__LINE__,nReturn);

    for(i = 0; i < nCols; i++) {
      nReturn = XPRS_msp_getsol(msp, iSolutionId, &iSolutionIdStatus, &dSol, i, i, NULL);
      if (nReturn!=0) errormsg("XPRS_msp_getsol",__LINE__,nReturn);
      printf("%3i = %12.5f\n", i, dSol);
    }

  }

  XPRSdestroyprob(prob);

  XPRS_msp_destroy(msp);
  XPRSfree();
}


/**********************************************************************************\
* Name:         errormsg                                                           *
* Purpose:      Display error information about failed subroutines.                *
* Arguments:    const char *sSubName   Subroutine name                             *
*               int nLineNo            Line number                                 *
*               int nErrCode           Error code                                  *
* Return Value: None                                                               *
\**********************************************************************************/
void errormsg(const char *sSubName,int nLineNo,int nErrCode)
{
   int nErrNo;   /* Optimizer error number */

   /* Print error message */
   printf("The subroutine %s has failed on line %d\n",sSubName,nLineNo);

   /* Append the error code, if it exists */
   if (nErrCode!=-1) printf("with error code %d\n",nErrCode);

   /* Free memory, close files and exit */
   XPRSfree();
   exit(nErrCode);
}


© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.