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

Delivery - Data input from file; infeasibility analysis

Description
A simple supply and demand network example showing data input from file and the use of "views": incremental definition of arrays of variables. Also uses constraint templates with the arrays of variables.

A second version of this model (file xbdlvriis) has modified data making the problem infeasible. This example shows how to analyze infeasibility with the help of IIS (irreducible infeasible sets), it retrieves the IIS and prints out their contents.

It is possible to retrieve more detailed information on the IIS, such as isolation rows or bounds, using Xpress Optimizer functions (file xbdlvriis2iso) or to use the infeasibility repair functionality of the Optimizer (file xbdlvriis2rep) with models defined in BCL.


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

Data Files





xbdelvr.cs

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

  file xbdelvr.cs
  ````````````````
  Transportation problem.

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

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


namespace Examples
{
    public class TestIntroDelivery
    {
        const int NSupp = 10;                  /* Number of suppliers */
        const int NCust = 7;                   /* Number of customers */
        const int MaxArcs = 100;               /* Max. num. of non-zero cost values */

        //Define XPRBDATAPATH to wherever you have placed the data folder; here we expect it to be same directory as compiled example.
        static string XPRBDATAPATH = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName + "/Data";
        static string VANFILE = XPRBDATAPATH + "/delivery/ifvan.dat";   /* Van data file */
        static string COSTFILE = XPRBDATAPATH + "/delivery/cost.dat";    /* Cost data file */

        /****DATA****/
        /* Supplier:      London  Luton  B'ham Bristl  Derby Stckpt   York */
        double[] SUPPLY = {140.0, 600.0,  50.0,  10.0, 400.0, 200.0,  20.0,
        /* Supplier: Derby  Soton Scnthp */
        90.0,  30.0,  12.0};
        /* Customer:      London Livpol Doncst   York   Hull Manchr Shffld */
        double[] DEMAND = {123.3,  56.4,  17.1, 192.8, 310.0,  47.0,  86.0};

        double[,] COST = new double[NSupp,NCust];        /* Cost per supplier-customer pair */

        double[,] IFVAN = new double[NSupp,NCust];       /* Non-zero if route uses vans instead
        of lorries */
        double VANCAP=40.0;               /* Capacity on routes that use vans */

        XPRBprob p = new XPRBprob("Delivery");           /* Initialize a new problem in BCL */

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

        void modDelivery()
        {
            XPRBexpr lobj, lc;
            int s,c;
            XPRBvar[,] x = new XPRBvar[NSupp,NCust];

            /****VARIABLES****/
            for(s=0;s<NSupp;s++)
            for(c=0; c<NCust; c++)
                x[s,c] = p.newVar("x_s" + s);

            /****OBJECTIVE****/
        lobj = new XPRBexpr();
            for(s=0;s<NSupp;s++)             /* Objective: Minimize total cost */
                for(c=0; c<NCust; c++)
                lobj += COST[s,c]*x[s,c];
            p.setObj(p.newCtr("OBJ", lobj)); /* Set objective function */

            /****CONSTRAINTS****/
            for(c=0; c<NCust; c++)           /* Satisfy demand of each customer */
            {
                lc = new XPRBexpr(0);
                for(s=0;s<NSupp;s++)  lc += x[s,c];
                p.newCtr("Demand", lc >= DEMAND[c]);
            }

            for(s=0;s<NSupp;s++)             /* Keep within supply at each supplier*/
            {
                lc = new XPRBexpr(0);
                for(c=0; c<NCust; c++)  lc+= x[s,c];
                p.newCtr("Supply", lc <= SUPPLY[s]);
            }

            /****BOUNDS****/
            for(s=0;s<NSupp;s++)
                for(c=0; c<NCust; c++)
                    if(IFVAN[s,c]!=0) x[s,c].setUB(VANCAP);

            /****SOLVING + OUTPUT****/
            p.exportProb(BCLconstant.XPRB_MPS,"delivery");   /* Write out an MPS file */

            p.setSense(BCLconstant.XPRB_MINIM);           /* Set objective sense to minimization */
            p.lpOptimize();                      /* Solve the LP-problem */
            System.Console.WriteLine("Objective: " + p.getObjVal());  /* Get objective value */

            for(s=0;s<NSupp;s++)              /* Print out the solution values */
                for(c=0; c<NCust; c++)
                    System.Console.Write(x[s,c].getName() + ":" + x[s,c].getSol() + " ");
            System.Console.WriteLine();
        }

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

        /**** Read data from files ****/
        void readData()
        {
            int s,c;
            double[] outdata;
            FileStream file;
            StreamReader fileStreamIn;

            /* Initialize data tables to 0: in the van data file some entries that are
            * zero are simply left out, but the function XPRBreadarrline only initializes
            * those elements to 0 that have been read, e.g. a data line ",,," results
            * in the first 4 elements of the array to be set to zero, even if the
            * maximum number of elements to be read (=last parameter of XPRBreadarrline)
            * has a much larger value */
            for(s=0;s<NSupp;s++)
                for(c=0; c<NCust; c++)
                {
                    COST[s,c] = 0;
                    IFVAN[s,c] = 0;
                }

            /* Read the demand data file */
            file = new FileStream(COSTFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            for (s = 0; s < NSupp; s++)
            {
                p.XPRBreadarrline(fileStreamIn, 99, "{g}, ", out outdata, NCust);
                for (int i = 0; i < NCust; i++)
                    COST[s, i] = outdata[i];
            }
            fileStreamIn.Close();
            file.Close();

            /* Read the van data file */
            file = new FileStream(VANFILE, FileMode.Open, FileAccess.Read);
            fileStreamIn = new StreamReader(file);
            for (s = 0; s < NSupp; s++)
            {
                p.XPRBreadarrline(fileStreamIn, 99, "{g}, ", out outdata, NCust);
                for (int i = 0; i < NCust; i++)
                    IFVAN[s, i] = outdata[i];
            }
            fileStreamIn.Close();
            file.Close();

        }

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

        public static void Main()
        {
            XPRB.init();

            TestIntroDelivery TestInstance = new TestIntroDelivery();

            TestInstance.readData();            /* Data input from file */
            TestInstance.modDelivery();         /* Problem formulation and solution */

            return;
        }

    }

}
Back to examples browserPrevious exampleNext example