FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Collecting all solutions with the MIP solution pool

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.

mipsolpool.zip[download all files]

Source Files

Data Files





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 = "../data/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 = XPRSminim(prob, "g");
  if (nReturn!=0) errormsg("XPRSminim",__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);
}


Back to examples browserPrevious exampleNext example