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.

xbsetopsjava.zip[download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
xbsetops.java[download]





xbsetops.java

/********************************************************
  Xpress-BCL Java Example Problems
  ================================

  file xbsetops.java
  ``````````````````
  Set operations.

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

import com.dashoptimization.*;

public class xbsetops {

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

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

    /**** Create the union of two index sets ****/
    static XPRBindexSet createUnion(XPRBprob p, XPRBindexSet a, XPRBindexSet b,
                                    String name) {
        int i;
        XPRBindexSet c;

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

    /**** Create the intersection of two index sets ****/
    static XPRBindexSet createInter(XPRBprob p, XPRBindexSet a, XPRBindexSet b,
                                    String name) {
        int i;
        XPRBindexSet c;

        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)  c.addElement(a.getIndexName(i));
        return c;
    }

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

    public static void main(String[] args) {
        XPRBindexSet ports,cities,both,places;
        int i;

        try (XPRBprob p = new XPRBprob("Setops")) { /* Initialize BCL and create a new problem */

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

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

Back to examples browserPrevious exampleNext example