Initializing help system before first use

Burglar - Use of index sets, formulating logical constraints


Type: Knapsack problem
Rating: 2 (easy-medium)
Description: Several versions of a simple knapsack problem:
  • xbburg: standard formlation
  • xbburgi: shows how to index an array of variables by an index set
  • xbburgl: adds several indicator constraints to state logical conditions
File(s): xbburg.java, xbburgi.java, xbburgl.java


xbburg.java
/********************************************************
  Xpress-BCL Java Example Problems
  ================================

  file xbburg.java
  ````````````````
  Burglar problem, binary variable formulation.

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

import com.dashoptimization.*;

public class xbburg
{
 static final int NItems = 8;        /* Number of items */

/****DATA****/
/* Item:                           1   2   3   4   5   6   7   8 */
 static final double[] VALUE  =  {15,100, 90, 60, 40, 15, 10,  1};
                                     /* Value of items */
 static final double[] WEIGHT =  { 2, 20, 20, 30, 40, 30, 60, 10};
                                     /* Weight of items */
 static final double WTMAX = 102;    /* Max weight allowed for haul */

 public static void main(String[] args)
 { 
  XPRB bcl;
  XPRBvar[] x;  
  XPRBexpr lobj, kn;  
  int i;
  XPRBprob p;
  
  bcl = new XPRB();                  /* Initialize BCL */
  p = bcl.newProb("Burglar");        /* Create a new problem in BCL */
 
/****VARIABLES****/
  x = new XPRBvar[NItems];           /* 1 if we take item i; 0 otherwise */
  for(i=0;i<NItems;i++)  x[i]=p.newVar("x", XPRB.BV);
 
/****OBJECTIVE****/
  lobj = new XPRBexpr();
  for(i=0;i<NItems;i++)  lobj.add(x[i].mul(VALUE[i])); 
  p.setObj(lobj);                    /* Set objective: maximize total value */ 

/****CONSTRAINTS****/
  kn = new XPRBexpr();
  for(i=0;i<NItems;i++)  kn.add(x[i].mul(WEIGHT[i]));  
  p.newCtr("WtMax", kn.lEql(WTMAX) );   /* Weight restriction */

/****SOLVING + OUTPUT****/
  p.setSense(XPRB.MAXIM);            /* Choose the sense of the optimization */
  p.mipOptimize("");                 /* Solve the MIP-problem */
  System.out.println("Objective: " + p.getObjVal());  /* Get objective value */

  for(i=0;i<NItems;i++)              /* Print out the solution */
   System.out.print(x[i].getName()+ ":" + x[i].getSol() + " ");
  System.out.println();
 }
} 


xbburgi.java
/********************************************************
  Xpress-BCL Java Example Problems
  ================================

  file xbburgi.java
  `````````````````
  Burglar problem. 
  Binary variable formulation with index sets.

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

import com.dashoptimization.*;

public class xbburgi
{
/****DATA****/
/* Item:                          ca  ne  va  pi  tv  vi  ch  br */
 static final double[] VALUE  =  {15,100, 90, 60, 40, 15, 10,  1};
                                     /* Value of items */
 static final double[] WEIGHT =  { 2, 20, 20, 30, 40, 30, 60, 10};
                                     /* Weight of items */
 static final double WTMAX = 102;    /* Max weight allowed for haul */

 static final String[] ITEMNAMES = {"camera", "necklace", "vase", "picture",
   "tv", "video", "chest", "brick"};

 static int NItems;                  /* Number of items */

 public static void main(String[] args)
 { 
  XPRB bcl;
  XPRBvar[] x;
  XPRBindexSet ITEMS;                /* Set of items */
  int i;
  XPRBexpr lobj, kn;  
  XPRBprob p;
  
  bcl = new XPRB();                  /* Initialize BCL */
  p = bcl.newProb("Burglari");       /* Create a new problem in BCL */
 
/****INDICES****/
  ITEMS=p.newIndexSet("Items",ITEMNAMES.length);   /* Create the index set */
  for(i=0;i<ITEMNAMES.length;i++)  ITEMS.addElement(ITEMNAMES[i]);
 
  NItems=ITEMS.getSize();            /* Get the size of the index set */
 
/****VARIABLES****/
  x = new XPRBvar[NItems];
  for(i=0;i<NItems;i++)
   x[i] = p.newVar("x_"+ITEMS.getIndexName(i), XPRB.BV);    
                                     /* 1 if we take item i; 0 otherwise */
 
/****OBJECTIVE****/
  lobj = new XPRBexpr();
  for(i=0;i<NItems;i++)  lobj.add(x[i].mul(VALUE[i])); 
  p.setObj(lobj);                    /* Set objective: maximize total value */ 

/****CONSTRAINTS****/
  kn = new XPRBexpr();
  for(i=0;i<NItems;i++)  kn.add(x[i].mul(WEIGHT[i]));  
  p.newCtr("WtMax", kn.lEql(WTMAX) );   /* Weight restriction */
 
/****SOLVING + OUTPUT****/
  p.setSense(XPRB.MAXIM);            /* Choose the sense of the optimization */
  p.mipOptimize("");                 /* Solve the MIP-problem */
  System.out.println("Objective: " + p.getObjVal());  /* Get objective value */

  for(i=0;i<NItems;i++)              /* Print out the chosen items */
   if(x[i].getSol()>0) 
    System.out.println(ITEMS.getIndexName(i) + ": " + x[i].getSol());
 }
} 


xbburgl.java
/********************************************************
  Xpress-BCL Java Example Problems
  ================================

  file xbburgl.java
  `````````````````
  Burglar problem. 
  Binary variable formulation with index sets.
  -- Formulating logical conditions 
     with indicator constraints --

  (c) 2009 Fair Isaac Corporation
      author: S.Heipcke, June 2009, rev. Mar. 2011
********************************************************/

import com.dashoptimization.*;

public class xbburgl
{
/****DATA****/
/* Item:                          ca  ne  va  pi  tv  vi  ch  br */
 static final double[] VALUE  =  {15,100, 90, 60, 40, 15, 10,  1};
                                     /* Value of items */
 static final double[] WEIGHT =  { 2, 20, 20, 30, 40, 30, 60, 10};
                                     /* Weight of items */
 static final double WTMAX = 102;    /* Max weight allowed for haul */

 static final String[] ITEMNAMES = {"camera", "necklace", "vase", "picture",
   "tv", "video", "chest", "brick"};

 static int NItems;                  /* Number of items */

 public static void main(String[] args)
 { 
  XPRB bcl;
  XPRBvar[] x;
  XPRBindexSet ITEMS;                /* Set of items */
  int i;
  XPRBexpr lobj, kn;  
  XPRBctr Log3a,Log3b;
  XPRBprob p;
  
  bcl = new XPRB();                  /* Initialize BCL */
  p = bcl.newProb("BurglarL");       /* Create a new problem in BCL */
 
/****INDICES****/
  ITEMS=p.newIndexSet("Items",ITEMNAMES.length);   /* Create the index set */
  for(i=0;i<ITEMNAMES.length;i++)  ITEMS.addElement(ITEMNAMES[i]);
 
  NItems=ITEMS.getSize();            /* Get the size of the index set */
 
/****VARIABLES****/
  x = new XPRBvar[NItems];
  for(i=0;i<NItems;i++)
   x[i] = p.newVar("x_"+ITEMS.getIndexName(i), XPRB.BV);    
                                     /* 1 if we take item i; 0 otherwise */
 
/****OBJECTIVE****/
  lobj = new XPRBexpr();
  for(i=0;i<NItems;i++)  lobj.add(x[i].mul(VALUE[i])); 
  p.setObj(lobj);                    /* Set objective: maximize total value */ 

/****CONSTRAINTS****/
  kn = new XPRBexpr();
  for(i=0;i<NItems;i++)  kn.add(x[i].mul(WEIGHT[i]));  
  p.newCtr("WtMax", kn.lEql(WTMAX) );   /* Weight restriction */

/** Logic constraint:
 ** Either take "vase" and "picture" or "tv" and "video" (but not both pairs). */

/* Values within each pair are the same */
 p.newCtr("Log1", x[ITEMS.getIndex("vase")].eql(x[ITEMS.getIndex("picture")]));
                                            /* x["vase"] == x["picture"] */
 p.newCtr("Log2", x[ITEMS.getIndex("tv")].eql(x[ITEMS.getIndex("video")]));
                                                /* x["tv"] == x["video"] */

/* Choose exactly one pair */
 Log3a = p.newCtr("Log3a",
   x[ITEMS.getIndex("tv")].add(x[ITEMS.getIndex("video")]).lEql(0));
                                              /* x["tv"]+x["video"] <= 0 */
 Log3b = p.newCtr("Log3b",
   x[ITEMS.getIndex("tv")].add(x[ITEMS.getIndex("video")]).gEql(2) ); 
                                              /* x["tv"]+x["video"] >= 2 */

/* Turn the 2 constraints into indicator constraints */
 Log3a.setIndicator(1, x[ITEMS.getIndex("vase")]);
                                  /* x["vase"]=1 -> x["tv"]+x["video"]=0 */
 Log3b.setIndicator(-1, x[ITEMS.getIndex("vase")]);
                                  /* x["vase"]=0 -> x["tv"]+x["video"]=2 */

/* Alternative MIP formulation (instead of Log3a and Log3b) */
/*
 p.newCtr("Log3", 
   x[ITEMS.getIndex("tv")].add(x[ITEMS.getIndex("vase")]).eql(1) );
*/                                            /* x["tv"] = 1 - x["vase"] */
 

/****SOLVING + OUTPUT****/
  p.setSense(XPRB.MAXIM);            /* Choose the sense of the optimization */
  p.mipOptimize("");                 /* Solve the MIP-problem */
  System.out.println("Objective: " + p.getObjVal());  /* Get objective value */

  for(i=0;i<NItems;i++)              /* Print out the chosen items */
   if(x[i].getSol()>0) 
    System.out.println(ITEMS.getIndexName(i) + ": " + x[i].getSol());
 }
} 


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