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

Setops - Index sets

Description
A example showing the use of index sets and in particular, creating the union and intersection of index sets.

xbsetopscpp.zip[download all files]

Source Files





xbsetops.cxx

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

  file xbsetops.cxx
  `````````````````
  Set operations.

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

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

using namespace std;
using namespace ::dashoptimization;

/* Define arrays with index names: note that the restriction 
   to 8 characters does not apply for BCL                    */
char *city_names[]={"rome", "bristol", "london", "paris", "liverpool"};
char *port_names[]={"plymouth", "bristol", "glasgow", "london", "calais", "liverpool"};

/***********************************************************************/

    /**** Create the union of two index sets ****/     
void createUnion(XPRBprob& p, const XPRBindexSet& a, const XPRBindexSet& b, 
  XPRBindexSet& c, char *name)
{
 int i;
 
 c=p.newIndexSet(name,a.getSize()+b.getSize());
 for(i=0;i<a.getSize();i++)  c += a[i];
 for(i=0;i<b.getSize();i++)  c += b[i];
}

    /**** Create the intersection of two index sets ****/
void createInter(XPRBprob& p, const XPRBindexSet& a, const XPRBindexSet& b, 
  XPRBindexSet& c, char *name)
{
 int i;
 
 c=p.newIndexSet(name,a.getSize()<b.getSize()?a.getSize():b.getSize());
 for(i=0;i<a.getSize();i++)
  if(b[a[i]]>=0)  c += a[i];
}

/***********************************************************************/

int main(int argc, char **argv)
{
 XPRBindexSet ports,cities,both,places;
 int i;
 XPRBprob p("Setops");              /* Initialize BCL */

        /* Create sets "cities" and "ports" and add the indices */
 cities=p.newIndexSet("cities",5);
 for(i=0;i<5;i++)  cities+=city_names[i];
 ports=p.newIndexSet("ports",6);
 for(i=0;i<6;i++)  ports+=port_names[i];

        /* Create the union of "cities" and "ports" and print it */
 createUnion(p,cities,ports,places,"places");
 places.print();
        /* Create the intersection of "cities" and "ports", print it */
 createInter(p,cities,ports,both,"both sets");
 both.print();
 
 return 0;
}

Back to examples browserPrevious exampleNext example