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

Chgprobs - Working with multiple problems

Description
This example defines 3 very small problems, making changes to the problem definition after matrix generation and retrieving solution information. It also shows BCL warnings.


Source Files





xbexpl.cxx

/********************************************************
  Xpress-BCL C++ Example Problems
  ===============================

  file xbexpl.cxx
  ```````````````
  Working with multiple problems.

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/

#include <iostream>
#include "xprb_cpp.h"

using namespace std;
using namespace ::dashoptimization;

/********************************************************/
/* This file illustrates how to                         */
/* - do changes to the problem definition               */
/* - retrieve solution information                      */
/* - define and work with several problems              */
/*                                                      */ 
/* Define at least one of the following options. It is  */
/* possible to define all together. In this case the    */
/* last function (expl5) that shows how to switch       */
/* between problems is activated too.                   */
/********************************************************/
#define CHGCTR      /* Accessing and modifying constraints */
#define CHGVAR      /* Accessing and modifying variables   */
#define UNBOUNDED   /* Solve a small unbounded problem     */

void expl2(void);
void expl3(void);
void expl4(void);
void expl5(void);

XPRBprob p2("expl2"), p3("expl3"), *p4;    
                    /* Initialize BCL and create 2 problems */

/***********************************************************************/
int main(int argc, char **argv)
{
#ifdef CHGCTR
 expl2();
#endif
#ifdef CHGVAR
 expl3(); 
#endif
#ifdef UNBOUNDED
 expl4();  
#ifdef CHGCTR
#ifdef CHGVAR
 expl5();
#endif
#endif
#endif

 return 0;
} 
/***********************************************************************/

    /**** Expl 2: changing bounds and operations on constraints ****/
void expl2()
{
 XPRBvar x[5];
 XPRBctr ctr[4];
 XPRBexpr lobj;
 double objcof[]={2.0,1.0,1.0,1.0,0};
 int i;
        
                /* Define 5 integer variables in 0,...,100 */
 for(i=0;i<5;i++) x[i] = p2.newVar(XPRBnewname("x_%d",i),XPRB_UI,0,100);

                /* Create the constraints:
                   ctr0: x0 +10 <= x1
                   ctr1: x1     <= x3
                   ctr2: x1 + 8 <= x2 */
 ctr[0]=p2.newCtr("ctr0", x[0] +10 <= x[1]);
 ctr[1]=p2.newCtr("ctr1", x[1]     <= x[3]);
 ctr[2]=p2.newCtr("ctr2", x[1] + 8 <= x[2]);
        
 lobj=0;
 for(i=0;i<5;i++) lobj += objcof[i]*x[i]; 
 p2.setObj(p2.newCtr("OBJ",lobj)); /* Select objective function */ 
 p2.setSense(XPRB_MINIM);         /* Set objective sense to minimization */

 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;  
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 p2.exportProb(XPRB_LP,"expl2");  /* Matrix generation and output */
 p2.print();                    /* Print current problem definition */

 p2.lpOptimize("");             /* Solve the LP */
 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;  
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 cout << "Objective: " << p2.getObjVal() << endl;
 for(i=0;i<4;i++)               /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";
 cout << endl;
 
 ctr[0].setRange(-15,-5);       /* Transform constraint into range constraint */
 cout << endl << "<<<<<<<<Constraint transformed into range:>>>>>>>>" << endl;
 p2.print();                    /* Print current problem definition */
 for(i=0;i<4;i++) 
 { x[i].print(); cout << " "; } /* Print new variable bounds */
 cout << endl;
 p2.mipOptimize("");            /* Solve the MIP */
 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;  
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 cout << "Objective: " << p2.getObjVal() << endl;
 for(i=0;i<4;i++)               /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";
 cout << endl;
 
 ctr[0].setType(XPRB_L);       /* Change range constraint back to constraint */
 cout << endl << "<<<<<<<<Constraint restored to inequality:>>>>>>>>" << endl;
 p2.print();                    /* Print current problem definition */
 ctr[0].setTerm(-10);           /* Set new RHS value */
 cout << "<<<<<<<<Restore original RHS value:>>>>>>>>" << endl;
 p2.print();                    /* Print current problem definition */

 x[1].setLB(15);                /* Change the bound on a variable */
 cout << "<<<<<<<<Variable bound changed:>>>>>>>>" << endl;
 for(i=0;i<4;i++) 
 { x[i].print(); cout << " "; } /* Print new variable bounds */
 cout << endl;
 p2.mipOptimize("");            /* Solve the MIP */
 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;  
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 cout << "Objective: " << p2.getObjVal() << endl;
 for(i=0;i<4;i++)               /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";
 cout << endl;

                                /* Change constraint coefficient and RHS */
 ctr[1].setTerm(x[1],-3);       /* ctr1: x1 <= 3*x3 */
 ctr[0].addTerm(-10);           /* ctr0: x0 + 20 <= x1 */
 cout << endl << "<<<<<<<<Constraint coefficient and RHS changed:>>>>>>>>" << endl;
 for(i=0;i<3;i++) ctr[i].print();
 for(i=0;i<4;i++) 
 { x[i].print(); cout << " "; } /* Print new variable bounds */
 cout << endl;
 p2.mipOptimize("");            /* Solve the MIP */
 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;  
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 cout << "Objective: " << p2.getObjVal() << endl;
 for(i=0;i<4;i++)               /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";
 cout << endl;

                                /* Change constraint type */
 ctr[2].setType(XPRB_G);        /* ctr2: x1 + 8 >= x2 */
 cout << endl << "<<<<<<<<Constraint type changed:>>>>>>>>" << endl;
 for(i=0;i<3;i++) ctr[i].print();
 for(i=0;i<4;i++) 
 { x[i].print(); cout << " "; } /* Print variable bounds */
 cout << endl;
 p2.mipOptimize("");            /* Solve the MIP */
 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;  
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 cout << "Objective: " << p2.getObjVal() << endl;
 for(i=0;i<4;i++)               /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";
 cout << endl;

                                /* Add another constraint ctr3: x0 +37<= x2 */
 ctr[3] = p2.newCtr("ctr3", x[0]+37 <= x[2]);
 cout << endl << "<<<<<<<<Constraint added:>>>>>>>>" << endl;
 p2.print();
 for(i=0;i<4;i++) 
 { x[i].print(); cout << " "; } /* Print variable bounds */
 cout << endl;
 p2.mipOptimize("");            /* Solve the MIP */
 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;  
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 cout << "Objective: " << p2.getObjVal() << endl;
 for(i=0;i<4;i++)               /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";
 cout << endl;

                /* delete a constraint */
 p2.delCtr(ctr[2]);      
 cout << endl << "<<<<<<<<Constraint deleted:>>>>>>>>" << endl;
 p2.print();
 for(i=0;i<4;i++) 
 { x[i].print(); cout << " "; } /* Print variable bounds */
 cout << endl;
 p2.mipOptimize("");            /* Solve the MIP */
 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;  
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 cout << "Objective: " << p2.getObjVal() << endl;
 for(i=0;i<4;i++)               /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";
 cout << endl;

}

    /**** Expl 3: Knapsack problem: accessing variables ****/
void expl3()
{
 XPRBvar x[4];
 XPRBexpr le, lobj;
 XPRBctr ctr;
 double coeff[]={30.0, 32.0, 27.0, 11.0};
 double objcof[]={9.0, 15.0, 8.0, 3.0};
 int i;
         
 for(i=0;i<4;i++)                /* Define 4 binary variables */
  x[i] = p3.newVar(XPRBnewname("x_%d",i),XPRB_BV);

                                 /* Create the knapsack constraint:
                                    sum_i coeff[i]*x[i] <= 70  */
 for(i=0;i<4;i++) le += coeff[i]*x[i]; 
 ctr = p3.newCtr("sumkn", le <= 70);
 
 for(i=0;i<4;i++) lobj += objcof[i]*x[i]; 
 p3.setObj(p3.newCtr("OBJ",lobj));    /* Set objective function */ 

/* p3.print(); */                /* Uncomment to print the problem */ 
 p3.exportProb(XPRB_MPS,"expl3");  /* Matrix output in MPS format */
 p3.setSense(XPRB_MAXIM);        /* Change to maximization */
 p3.mipOptimize("");             /* Solve the MIP */
 cout << "Objective: " << p3.getObjVal() << endl;   /* Get objective value */
 for(i=0;i<4;i++)                /* Print the solution */
  cout << x[i].getName() << ": " << x[i].getSol() << " (rc:" << x[i].getRCost() << ")" << endl;
 cout << "Dual: " << ctr.getDual() << ", slack:" << ctr.getSlack() << endl;    
                                 /* Print dual & slack values */

 cout << endl << "<<<<<<<<Variable type changed from BV to UI>>>>>>>>" << endl;
 x[1].setType(XPRB_UI);         /* Change variable type */
 cout << x[1].getName() << ": bounds: " << x[1].getLB() << " " << x[1].getUB();
 cout << ", type: " << x[1].getType() << ", index: " << x[1].getColNum() << endl;
 p3.mipOptimize("");            /* Re-solve MIP */
 cout << "Objective: " << p3.getObjVal() << endl;   /* Get objective value */        

 cout << endl << "<<<<<<<<Variable bound changed: no matrix regeneration>>>>>>>>" << endl;
 x[1].setUB(3);                  /* Change variable bound */
 cout << x[1].getName() << ": bounds: " << x[1].getLB() << " " << x[1].getUB();
 cout << ", type: " << x[1].getType() << ", index: " << x[1].getColNum() << endl;
 p3.mipOptimize("");            /* Re-solve MIP */
 cout << "Objective: " << p3.getObjVal() << endl;   /* Get objective value */
 for(i=0;i<4;i++)               /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";

 cout << endl << endl << "<<<<<<<<Variable type changed from UI to PI>>>>>>>>" << endl;
 x[1].setType(XPRB_PI);         /* Change variable type */
 x[1].setLim(2);                 /* Set the integer limit for 
                                    the partial integer variable */
 x[1].print(); cout << endl;     /* Print current variable definition */
 p3.mipOptimize("");            /* Re-solve MIP */
 cout << "Objective: " << p3.getObjVal() << endl;   /* Get objective value */
 for(i=0;i<4;i++)                /* Print the solution */
  cout << x[i].getName() << ": " << x[i].getSol() << " (rc:" << x[i].getRCost() << ")" << endl;
 cout << "Dual: " << ctr.getDual() << ", slack:" << ctr.getSlack() << endl;    
                                 /* Print dual & slack values */
}   

    /****Expl 4: a small unbounded problem ****/
void expl4()
{
 XPRBvar x[2];
 int i;

 p4=new XPRBprob("expl4");       /* Create a new problem */
 
                                 /* Define 2 variables in [0,PLUSINFINITY] */
 for(i=0;i<2;i++) x[i]=p4->newVar(XPRBnewname("x_%d",i));

                                 /* Create the constraints:
                                    ctr0: 4*x0 + x1 >= 4
                                    ctr1: x0 + x1   >= 3
                                    ctr2: x0 + 2*x1 >= 4 */
 p4->newCtr("c1", 4*x[0] + x[1] >= 4);
 p4->newCtr("c2", x[0] +   x[1] >= 3);
 p4->newCtr("c3", x[0] + 2*x[1] >= 4);

 p4->setObj(p4->newCtr("OBJ", x[0]+x[1]));  /* Define and set obj. function */ 

/* Try out the effect of solving without presolve:
 XPRSsetintcontrol(XPRBgetXPRSprob(), XPRS_PRESOLVE, 0);
 XPRSsetintcontrol(XPRBgetXPRSprob(), XPRS_MIPPRESOLVE, 0); 
*/
 p4->setSense(XPRB_MAXIM);       /* Change to maximization */
 p4->lpOptimize("");             /* Solve the LP */
 cout << "Problem status: " << p4->getProbStat() << "  LP status: " ;
 cout << p4->getLPStat() << "  MIP status: " << p4->getMIPStat() << endl;
 cout << "Objective: " << p4->getObjVal() << endl;   /* Get objective value */
 for(i=0;i<2;i++)                /* Print solution values */
  cout << x[i].getName() << ":" << x[i].getSol() << " ";
 cout << endl;
}

    /***Expl5: Working with different problems****/
void expl5()
{
 int i;
 
 cout << endl << "<<<<<<<<Re-solve problem " << p2.getName() << ">>>>>>>>" << endl;
 p2.mipOptimize("");             /* Solve the MIP */
 cout << "Problem status: " << p2.getProbStat() << "  LP status: " ;
 cout << p2.getLPStat() << "  MIP status: " << p2.getMIPStat() << endl;
 cout << "Objective: " << p2.getObjVal() << endl;  /* Get objective value */
 for(i=0;i<4;i++)                /* Print solution values */
  cout << "x_" << i << ":" << p2.getVarByName(XPRBnewname("x_%d",i)).getSol() << " ";
  
 cout << endl << endl << "<<<<<<<<Delete problem " << p4->getName() << ">>>>>>>>" << endl;
 p4->print();                    /* Print the problem def. */
 delete p4;                      /* Delete the problem: this is only possible
                                    because it has been defined as a pointer, 
                                    and not as an object (case of p2 and p3)*/

 cout << "<<<<<<<<Re-solve problem " <<p3.getName() << " and print it>>>>>>>>" << endl;
 p3.print();                     /* Print the problem def. */
 p3.mipOptimize("");             /* Solve the MIP */
 cout << "Problem status: " << p3.getProbStat() << "  LP status: " ;  
 cout << p3.getLPStat() << "  MIP status: " << p3.getMIPStat() << endl;
 cout << "Objective: " << p3.getObjVal() << endl;  /* Get objective value */
 for(i=0;i<4;i++)                /* Print solution values */
  cout << "x_" << i << ":" << p3.getVarByName(XPRBnewname("x_%d",i)).getSol() << " "; 
 cout << endl;
}

Back to examples browserPrevious exampleNext example