Initializing help system before first use

Folio - Advanced modeling and solving tasks


Type: Portfolio optimization
Rating: 2 (easy-medium)
Description: Working with multiple MIP solutions:
  • foliomatenumsol.c,foliomatenumsol.java - using the MIP solution enumerator
  • foliomatsolpool.c,foliomatsolpool.java - using the MIP solution pool
File(s): foliomatenumsol.c, foliomatenumsol.java, foliomatsolpool.c, foliomatsolpool.java
Data file(s): folio10_7.lp

foliomatenumsol.c
/********************************************************
  Xpress Optimizer Example Problems
  =================================

  file foliomatenumsol.c
  ``````````````````````
  Using the MIP solution enumerator with a MIP problem
  input from a matrix file.

  (c) 2009 Fair Isaac Corporation
      author: D.Nielsen, S.Heipcke, July 2009, rev. Jul. 2014
********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "xprs.h"
#include "xprs_mse_defaulthandler.h"

int main(int argc, char **argv)
{
 XPRSprob prob;
 XPRSmipsolpool msp;
 XPRSmipsolenum mse;
 int i, s, nSols, solID, nCols, solStatus, presolveOps, maxSols;
 double objval, sol;

 XPRSinit(NULL);                         /* Initialize Xpress Optimizer */
 XPRScreateprob(&prob);                  /* Create a new problem */
                                
 XPRSreadprob(prob, "folio10_7.lp", "");    /* Read the problem matrix */

 /* Get the number of columns in the problem */
 XPRSgetintattrib(prob, XPRS_ORIGINALCOLS, &nCols);

 /* Create a mip solution pool to store the solutions */
 XPRS_msp_create(&msp);

 /* Create a mip solution enumerator to run the search */
 XPRS_mse_create(&mse);

 /* Disable heuristics to avoid duplicate solutions being found and stored */
 XPRSsetintcontrol(prob, XPRS_HEURSTRATEGY, 0);

 /* Disable presolve operations that attempt to improve the efficiency by 
    cutting off MIP solutions from the feasible region that are either:-
    1) Degenerate (i.e., that have equivalent representations with 
       different feasible values of the variables) or
    2) Dominated (i.e., that can be deduced to be worse than solutions 
       contained in the remaining feasible region). 
 */
 XPRSgetintcontrol(prob, XPRS_PRESOLVEOPS, &presolveOps);  
 presolveOps &= ~(1 << 5);     /* Disable duplicate column removal  */
 presolveOps &= ~(1 << 3);     /* Disable dual reduction operations */
 XPRSsetintcontrol(prob, XPRS_PRESOLVEOPS, presolveOps);  
 XPRSgetintcontrol(prob, XPRS_MIPPRESOLVE, &presolveOps);  
 presolveOps &= ~(1 << 4);     /* Disable dual reductions */
 XPRSsetintcontrol(prob, XPRS_MIPPRESOLVE, presolveOps);  
 XPRSsetintcontrol(prob, XPRS_SYMMETRY, 0);  /* Disable symmetry detection */

 /* Run the enumeration */
 maxSols = 10;
 XPRS_mse_maxim(mse, prob, msp, XPRS_mse_defaulthandler, NULL, &maxSols);

 /* Get the number of solutions found */
 XPRS_mse_getintattrib(mse, XPRS_MSE_SOLUTIONS, &nSols);

 /* Print out the solutions found */
 for(i=1; i<=nSols; i++)
 {
  XPRS_mse_getsollist(mse, XPRS_MSE_METRIC_MIPOBJECT, i, i, &solID, 
                      NULL, NULL);
  XPRS_mse_getsolmetric(mse, solID, &solStatus, XPRS_MSE_METRIC_MIPOBJECT,
                        &objval);
  printf("--------\nSolution %d:  Objective: %g\n", i, objval);
  for(s=0; s<nCols ;s++) {
   XPRS_msp_getsol(msp, solID, &solStatus, &sol, s, s, NULL);
   if(sol!=0) printf("%3d: %g\n", s+1, sol);
  }
 }

 XPRS_mse_destroy(mse);
 XPRS_msp_destroy(msp);

 XPRSdestroyprob(prob);                  /* Delete the problem */ 
 XPRSfree();                             /* Terminate Xpress */
  
 return 0;
} 

foliomatenumsol.java
/********************************************************
  Xpress Optimizer Example Problems
  =================================

  file foliomatenumsol.java
  `````````````````````````
  Using the MIP solution enumerator with a MIP problem
  input from a matrix file.

  (c) 2009 Fair Isaac Corporation
      author: D.Nielsen, S.Heipcke, July 2009, rev. Jan. 2013
********************************************************/

import com.dashoptimization.*;

class foliomatenumsol
{
 public static void main(String args[])
 {
  try {
    XPRS.init ();
  } catch (Exception e) {
    System.out.println("Failed to initialize");
    return;
  }

  try {
   IntHolder maxSols = new IntHolder();
   int i, s, nSols, nCols, presolveOps;
   int[] solID = new int[1];
   double[] sol = new double[1];
   IntHolder solStatus = new IntHolder();
   DoubleHolder objval = new DoubleHolder();

   XPRSmipsolpool msp = new XPRSmipsolpool();
   XPRSmipsolenum mse = new XPRSmipsolenum();
   XPRSprob prob = new XPRSprob();

   /* Use the default handler for the solutions found when we run mse */
   XPRSdefaultMipSolEnumHandler handler = new XPRSdefaultMipSolEnumHandler();

   /* Read in the matrix file */
   prob.readProb("folio10_7.lp", "");

   /* Get the number of columns in the problem */
   nCols = prob.getIntAttrib(XPRS.ORIGINALCOLS);

   /* Disable heuristics to avoid duplicate solutions being found and stored */
   prob.setIntControl(XPRS.HEURSTRATEGY, 0);

   /* Disable presolve operations that attempt to improve the efficiency by 
      cutting off MIP solutions from the feasible region that are either:-
       1) Degenerate (i.e., that have equivalent representations with 
          different feasible values of the variables) or
       2) Dominated (i.e., that can be deduced to be worse than solutions 
          contained in the remaining feasible region). 
   */
   presolveOps = prob.getIntControl(XPRS.PRESOLVEOPS);  
   presolveOps = ~(1 << 5);     /* Disable duplicate column removal  */
   presolveOps = ~(1 << 3);     /* Disable dual reduction operations */
   prob.setIntControl(XPRS.PRESOLVEOPS, presolveOps);  
   presolveOps = prob.getIntControl(XPRS.MIPPRESOLVE);  
   presolveOps = ~(1 << 4);     /* Disable dual reductions  */
   prob.setIntControl(XPRS.MIPPRESOLVE, presolveOps);  
   prob.setIntControl(XPRS.SYMMETRY, 0);   /* Disable symmetry detection */

   /* Run the enumeration */
   maxSols.value = 10;
   mse.maxim(prob, msp, handler, maxSols);

   /* Get the number of solutions found */
   nSols = mse.getIntAttrib(XPRS.MSE_SOLUTIONS);

   /* Print out the solutions found */
   for(i=1; i<=nSols; i++)
   {
    mse.getSolList(XPRS.MSE_METRIC_MIPOBJECT, i, i, solID, null, null);
    mse.getSolMetric(solID[0], solStatus, XPRS.MSE_METRIC_MIPOBJECT, objval);
    System.out.println("--------\nSolution " + i + ":  Objective: " + objval);
    for(s=0; s<nCols ;s++) {
     msp.getSol(solID[0], solStatus, sol, s, s, null);
     if(sol[0]!=0) System.out.println((s+1) + ": "+ sol[0]);
    }
   }
  } catch(XPRSprobException xpe) {
   xpe.printStackTrace();
  }

  XPRS.free();
 }
}

foliomatsolpool.c
/********************************************************
  Xpress Optimizer Example Problems
  =================================

  file foliomatsolpool.c
  ``````````````````````
  Using the MIP solution pool with a MIP problem
  input from a matrix file.

  (c) 2009 Fair Isaac Corporation
      author: D.Nielsen, S.Heipcke, July 2009, rev. June 2010
********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "xprs.h"

int main(int argc, char **argv)
{
 XPRSprob prob;
 XPRSmipsolpool msp;
 int s, nSols, solID, nCols, solStatus;
 double objval, sol;

 XPRSinit(NULL);                         /* Initialize Xpress Optimizer */
 XPRScreateprob(&prob);                  /* Create a new problem */
                                
 XPRSreadprob(prob, "folio10_7.lp", "");    /* Read the problem matrix */

 /* Create a mip solution pool to store the solutions */
 XPRS_msp_create(&msp);

 /* Attach the mip solution pool to the problem so it will 
    automatically receive the solutions as they are found */
 XPRS_msp_probattach(msp, prob);

 /* Solve the problem */
 XPRSchgobjsense(prob, XPRS_OBJ_MAXIMIZE);  /* Set sense to maximization */
 XPRSmipoptimize(prob, "");                 /* Solve the problem */

 /* Get the number of solutions found */
 XPRS_msp_getintattrib(msp, XPRS_MSP_SOLUTIONS, &nSols);

 if(nSols)
 {
  printf("Number of solutions: %d\n", nSols);

  /* Get the best objective value of the solutions found */
  XPRS_msp_getdblattribprobextreme(msp, prob, 1, &solID,
                                   XPRS_MSP_SOLPRB_OBJ, &objval);

  printf("Optimal solution ID: %d\n", solID);
  printf("Optimal objective  : %g\n", objval);

  /* Get the number of columns in the solutions */
  XPRS_msp_getintattribsol(msp, solID, &solStatus,
                           XPRS_MSP_SOL_COLS, &nCols);

  /* Print out the solution values of the best solution */
  for(s=0; s<nCols; s++) {
   XPRS_msp_getsol(msp, solID, &solStatus, &sol, s, s, NULL);
   if(sol!=0) printf("%3d: %g\n", s+1, sol);
  }
 }

 XPRSdestroyprob(prob);                  /* Delete the problem */
 XPRS_msp_destroy(msp);
 XPRSfree();                             /* Terminate Xpress */
  
 return 0;
} 

foliomatsolpool.java
/********************************************************
  Xpress Optimizer Example Problems
  =================================

  file foliomatsolpool.java
  `````````````````````````
  Using the MIP solution pool with a MIP problem
  input from a matrix file.

  (c) 2009 Fair Isaac Corporation
      author: D.Nielsen, S.Heipcke, July 2009, rev. June 2010
********************************************************/

import com.dashoptimization.*;

class foliomatsolpool
{
 public static void main(String args[])
 {
  try {
    XPRS.init ();
  } catch (Exception e) {
    System.out.println("Failed to initialize");
    return;
  }

  try {
   int s, nSols, nCols;
   DoubleHolder objval = new DoubleHolder();
   double[] sol = new double[1];
   IntHolder solID = new IntHolder();
   IntHolder solStatus = new IntHolder();

   XPRSprob prob = new XPRSprob();
   XPRSmipsolpool msp = new XPRSmipsolpool();

   /* Read in the matrix file */
   prob.readProb("folio10_7.lp", "");

   /* Attach the mip solution pool to the problem so it will 
      automatically receive the solutions as they are found */
   msp.probAttach(prob);

   /* Solve the problem */
   prob.chgObjSense(XPRSenumerations.ObjSense.MAXIMIZE); 
   prob.mipOptimize();

   /* Get the number of solutions found */
   nSols = msp.getIntAttrib(XPRS.MSP_SOLUTIONS);

   if(nSols > 0)
   {
    System.out.println("Number of solutions: " + nSols);

    /* Get the best objective value of the solutions found */
    msp.getDblAttribProbExtreme(prob, 1, solID, XPRS.MSP_SOLPRB_OBJ, objval);

    System.out.println("Optimal solution ID: " + solID.value);
    System.out.println("Optimal objective  : " + objval.value);

    /* Get the number of columns in the solutions */
    nCols = msp.getIntAttribSol(solID.value, solStatus, XPRS.MSP_SOL_COLS);

    /* Print out the solution values of the best solution */
    for(s=0; s<nCols ;s++) {
     msp.getSol(solID.value, solStatus, sol, s, s, null);
     if(sol[0]!=0) System.out.println((s+1) + ": "+ sol[0]);
    }
   }
  } catch(XPRSprobException xpe) {
   xpe.printStackTrace();
  }

  XPRS.free();
 }
}


© 2001-2019 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.