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.

xbsetopscs.zip[download all files]

Source Files





xbsetops.cs

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

  file xbsetops.cs
  ````````````````
  Set operations.

  (c) 2008 Fair Isaac Corporation
      authors: S.Heipcke, D.Brett.
********************************************************/

using System;
using System.Text;
using System.IO;
using BCL;


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

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

        /**** Create the union of two index sets ****/     
        public void createUnion(XPRBprob p, XPRBindexSet a, XPRBindexSet b, 
        out XPRBindexSet c, string name)
        {
            int i, dummy;

            c=p.newIndexSet(name,a.getSize()+b.getSize());
            for(i=0;i<a.getSize();i++)  dummy = c + a.getIndexName(i);
            for(i=0;i<b.getSize();i++)  dummy = c + b.getIndexName(i);
        }

        /**** Create the intersection of two index sets ****/
        public void createInter(XPRBprob p, XPRBindexSet a, XPRBindexSet b, 
        out XPRBindexSet c, string name)
        {
            int i, dummy;

            c=p.newIndexSet(name,a.getSize()<b.getSize()?a.getSize():b.getSize());
            for(i=0;i<a.getSize();i++)
                if (b.getIndex(a.getIndexName(i)) >= 0) dummy = c + a.getIndexName(i);
        }

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

        public static void Main()
        {
            XPRB.init();
            XPRBindexSet ports,cities,both,places;
            int i, dummy;
            XPRBprob p = new XPRBprob("Setops");              /* Initialize BCL */
            TestSetOps TestInstance = new TestSetOps();

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

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

            return;
        }
    }
}
Back to examples browserPrevious exampleNext example