Initializing help system before first use

Setops - Index sets


Type: Programming
Rating: 2 (easy-medium)
Description: A example showing the use of index sets and in particular, creating the union and intersection of index sets.
File(s): xbsetops.java


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();
        }
    }
}

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