ILPConstraint.java

  1. package org.drip.linearprogram.canonical;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  *
  8.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  9.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  10.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  11.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  12.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  13.  *      and computational support.
  14.  *  
  15.  *      https://lakshmidrip.github.io/DROP/
  16.  *  
  17.  *  DROP is composed of three modules:
  18.  *  
  19.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  20.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  21.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  22.  *
  23.  *  DROP Product Core implements libraries for the following:
  24.  *  - Fixed Income Analytics
  25.  *  - Loan Analytics
  26.  *  - Transaction Cost Analytics
  27.  *
  28.  *  DROP Portfolio Core implements libraries for the following:
  29.  *  - Asset Allocation Analytics
  30.  *  - Asset Liability Management Analytics
  31.  *  - Capital Estimation Analytics
  32.  *  - Exposure Analytics
  33.  *  - Margin Analytics
  34.  *  - XVA Analytics
  35.  *
  36.  *  DROP Computational Core implements libraries for the following:
  37.  *  - Algorithm Support
  38.  *  - Computation Support
  39.  *  - Function Analysis
  40.  *  - Model Validation
  41.  *  - Numerical Analysis
  42.  *  - Numerical Optimizer
  43.  *  - Spline Builder
  44.  *  - Statistical Learning
  45.  *
  46.  *  Documentation for DROP is Spread Over:
  47.  *
  48.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  49.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  50.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  51.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  52.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  53.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  54.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  55.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  56.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  57.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  58.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  59.  *
  60.  *  Licensed under the Apache License, Version 2.0 (the "License");
  61.  *      you may not use this file except in compliance with the License.
  62.  *  
  63.  *  You may obtain a copy of the License at
  64.  *      http://www.apache.org/licenses/LICENSE-2.0
  65.  *  
  66.  *  Unless required by applicable law or agreed to in writing, software
  67.  *      distributed under the License is distributed on an "AS IS" BASIS,
  68.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  69.  *  
  70.  *  See the License for the specific language governing permissions and
  71.  *      limitations under the License.
  72.  */

  73. /**
  74.  * <i>ILPConstraint</i> holds the Constraint Matrix LHS and Constraint Array RHS for an Integer Linear
  75.  *  Program Ax lte B, where A is Z<sup>m x n</sup>, B is Z<sup>m</sup>, and x is Z<sub>+</sub><sup>n</sup>.
  76.  *  The References are:
  77.  *
  78.  * <br><br>
  79.  *  <ul>
  80.  *      <li>
  81.  *          Burdet, C. A., and E. L. Johnson (1977): A Sub-additive Approach to Solve Linear Integer Programs
  82.  *              <i>Annals of Discrete Mathematics</i> <b>1</b> 117-143
  83.  *      </li>
  84.  *      <li>
  85.  *          Chvatal, V. (1973): Edmonds Polytopes in a Hierarchy of Combinatorial Problems <i>Discrete
  86.  *              Mathematics</i> <b>4 (4)</b> 305-337
  87.  *      </li>
  88.  *      <li>
  89.  *          Gomory, R. E. (1958): Outline of an Algorithm for Integer Solutions to Linear Programs
  90.  *              <i>Bulletin of the American Mathematical Society</i> <b>64 (5)</b> 275-278
  91.  *      </li>
  92.  *      <li>
  93.  *          Kelley, J. E. (1960): The Cutting Plane Method for Solving Convex Problems <i>Journal for the
  94.  *              Society of the Industrial and Applied Mathematics</i> <b>8 (4)</b> 703-712
  95.  *      </li>
  96.  *      <li>
  97.  *          Letchford, A. N. and A. Lodi (2002): Strengthening Chvatal-Gomory Cuts and Gomory Fractional Cuts
  98.  *              <i>Operations Research Letters</i> <b>30 (2)</b> 74-82
  99.  *      </li>
  100.  *  </ul>
  101.  *
  102.  *  <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AssetAllocationAnalyticsLibrary.md">Asset Allocation Analytics</a></li>
  106.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/linearprogram/README.md">Linear Programming Solution Suite</a></li>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/linearprogram/canonical/README.md">Linear Programming Framework Canonical Elements</a></li>
  108.  *  </ul>
  109.  *
  110.  * @author Lakshmi Krishnamurthy
  111.  */

  112. public class ILPConstraint
  113. {
  114.     private int[] _bArray = null;
  115.     private int[][] _aGrid = null;

  116.     /**
  117.      * ILPConstraint Constructor
  118.      *
  119.      * @param aGrid "A" Constraint Grid
  120.      * @param bArray "b" Constraint Array
  121.      *
  122.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  123.      */

  124.     public ILPConstraint (
  125.         final int[][] aGrid,
  126.         final int[] bArray)
  127.         throws java.lang.Exception
  128.     {
  129.         if (null == (_aGrid = aGrid) ||
  130.             null == (_bArray = bArray)
  131.         )
  132.         {
  133.             throw new java.lang.Exception (
  134.                 "ILPConstraint Constructor => Invalid Inputs"
  135.             );
  136.         }

  137.         int dimension = -1;
  138.         int constraintCount = _bArray.length;

  139.         if (0 == constraintCount ||
  140.             _aGrid.length != constraintCount)
  141.         {
  142.             throw new java.lang.Exception (
  143.                 "ILPConstraint Constructor => Invalid Inputs"
  144.             );
  145.         }

  146.         for (int constraintIndex = 0;
  147.             constraintIndex < constraintCount;
  148.             ++constraintIndex)
  149.         {
  150.             if (null == _aGrid[constraintIndex])
  151.             {
  152.                 throw new java.lang.Exception (
  153.                     "ILPConstraint Constructor => Invalid Inputs"
  154.                 );
  155.             }

  156.             if (-1 == dimension)
  157.             {
  158.                 if (0 == (dimension = _aGrid[constraintIndex].length))
  159.                 {
  160.                     throw new java.lang.Exception (
  161.                         "ILPConstraint Constructor => Invalid Inputs"
  162.                     );
  163.                 }
  164.             }
  165.             else
  166.             {
  167.                 if (dimension != _aGrid[constraintIndex].length)
  168.                 {
  169.                     throw new java.lang.Exception (
  170.                         "ILPConstraint Constructor => Invalid Inputs"
  171.                     );
  172.                 }
  173.             }
  174.         }
  175.     }

  176.     /**
  177.      * Retrieve "A" Grid
  178.      *
  179.      * @return A Grid
  180.      */

  181.     public int[][] aGrid()
  182.     {
  183.         return _aGrid;
  184.     }

  185.     /**
  186.      * Retrieve "b" Array
  187.      *
  188.      * @return b Array
  189.      */

  190.     public int[] bArray()
  191.     {
  192.         return _bArray;
  193.     }

  194.     /**
  195.      * Retrieve the Constraint Count
  196.      *
  197.      * @return Constraint Count
  198.      */

  199.     public int constraintCount()
  200.     {
  201.         return _bArray.length;
  202.     }

  203.     /**
  204.      * Retrieve the Variate Dimension
  205.      *
  206.      * @return Variate Dimension
  207.      */

  208.     public int dimension()
  209.     {
  210.         return _aGrid[0].length;
  211.     }

  212.     /**
  213.      * Validate the Variate Input
  214.      *
  215.      * @param variateArray The Input Variate Array
  216.      *
  217.      * @return TRUE - The Input Variate successfully Validated
  218.      */

  219.     public boolean validate (
  220.         final int[] variateArray)
  221.     {
  222.         if (null == variateArray)
  223.         {
  224.             return false;
  225.         }

  226.         int dimension = _aGrid[0].length;

  227.         if (dimension != variateArray.length)
  228.         {
  229.             return false;
  230.         }

  231.         for (int dimensionIndex = 0;
  232.             dimensionIndex < dimension;
  233.             ++dimensionIndex)
  234.         {
  235.             if (0 >= variateArray[dimensionIndex])
  236.             {
  237.                 return false;
  238.             }
  239.         }

  240.         return true;
  241.     }

  242.     /**
  243.      * Verify if the Variate Array satisfies the Constraint
  244.      *
  245.      * @param variateArray The Input Variate Array
  246.      *
  247.      * @return TRUE - The Variate Array satisfies the Constraint
  248.      *
  249.      * @throws java.lang.Exception Thrown if the Verification cannot be done
  250.      */

  251.     public boolean verify (
  252.         final int[] variateArray)
  253.         throws java.lang.Exception
  254.     {
  255.         if (!validate (
  256.             variateArray
  257.         ))
  258.         {
  259.             throw new java.lang.Exception (
  260.                 "ILPConstraint::verify => Variate Array not Valid"
  261.             );
  262.         }

  263.         int dimension = _aGrid[0].length;
  264.         int constraintCount = _bArray.length;

  265.         for (int constraintIndex = 0;
  266.             constraintIndex < constraintCount;
  267.             ++constraintIndex)
  268.         {
  269.             int constraintLHS = 0;

  270.             for (int dimensionIndex = 0;
  271.                 dimensionIndex < dimension;
  272.                 ++dimensionIndex)
  273.             {
  274.                 constraintLHS += _aGrid[constraintIndex][dimensionIndex] * variateArray[dimensionIndex];
  275.             }

  276.             if (constraintLHS > _bArray[constraintIndex])
  277.             {
  278.                 return false;
  279.             }
  280.         }

  281.         return true;
  282.     }

  283.     /**
  284.      * Generate a Chvatal-Gomory Cut
  285.      *
  286.      * @param lambdaArray The Lambda Array
  287.      *
  288.      * @return The Chvatal-Gomory Cut
  289.      */

  290.     public org.drip.linearprogram.cuttingplane.ChvatalGomoryCut chvatalGomoryCut (
  291.         final double[] lambdaArray)
  292.     {
  293.         try
  294.         {
  295.             return new org.drip.linearprogram.cuttingplane.ChvatalGomoryCut (
  296.                 _aGrid,
  297.                 _bArray,
  298.                 lambdaArray
  299.             );
  300.         }
  301.         catch (java.lang.Exception e)
  302.         {
  303.             e.printStackTrace();
  304.         }

  305.         return null;
  306.     }

  307.     /**
  308.      * Generate a Strengthened Chvatal-Gomory Cut
  309.      *
  310.      * @param lambdaArray The Lambda Array
  311.      * @param t Strengthening Integer
  312.      *
  313.      * @return The Chvatal-Gomory Cut
  314.      */

  315.     public org.drip.linearprogram.cuttingplane.StrengthenedChvatalGomoryCut strengthenedChvatalGomoryCut (
  316.         final double[] lambdaArray,
  317.         final int t)
  318.     {
  319.         if (null == lambdaArray)
  320.         {
  321.             return null;
  322.         }

  323.         double lambdaB = 0.;
  324.         int constraintCount = _bArray.length;

  325.         if (lambdaArray.length != constraintCount)
  326.         {
  327.             return null;
  328.         }

  329.         for (int constraintIndex = 0;
  330.             constraintIndex < constraintCount;
  331.             ++constraintIndex)
  332.         {
  333.             lambdaB = lambdaB + lambdaArray[constraintIndex] * _bArray[constraintIndex];
  334.         }

  335.         try
  336.         {
  337.             double fractionalLambdaB = org.drip.numerical.common.NumberUtil.Fractional (
  338.                 lambdaB
  339.             );

  340.             if (0.5 >= fractionalLambdaB)
  341.             {
  342.                 return null;
  343.             }

  344.             double tFractionalLambdaB = org.drip.numerical.common.NumberUtil.Fractional (
  345.                 t * fractionalLambdaB
  346.             );

  347.             return 0.5 <= tFractionalLambdaB && tFractionalLambdaB < 1. ? null :
  348.                 new org.drip.linearprogram.cuttingplane.StrengthenedChvatalGomoryCut (
  349.                     _aGrid,
  350.                     _bArray,
  351.                     lambdaArray,
  352.                     t
  353.                 );
  354.         }
  355.         catch (java.lang.Exception e)
  356.         {
  357.             e.printStackTrace();
  358.         }

  359.         return null;
  360.     }

  361.     /**
  362.      * Generate a Burdet-Johnson Cut
  363.      *
  364.      * @param lambdaArray The Lambda Array
  365.      *
  366.      * @return The Burdet-Johnson Cut
  367.      */

  368.     public org.drip.linearprogram.cuttingplane.BurdetJohnsonCut burdetJohnsonCut (
  369.         final double[] lambdaArray)
  370.     {
  371.         try
  372.         {
  373.             return new org.drip.linearprogram.cuttingplane.BurdetJohnsonCut (
  374.                 _aGrid,
  375.                 _bArray,
  376.                 lambdaArray
  377.             );
  378.         }
  379.         catch (java.lang.Exception e)
  380.         {
  381.             e.printStackTrace();
  382.         }

  383.         return null;
  384.     }

  385.     /**
  386.      * Generate a Strengthened Burdet-Johnson Cut
  387.      *
  388.      * @param lambdaArray The Lambda Array
  389.      * @param r1ToR1Increasing R<sup>1</sup> To R<sup>1</sup> Increasing Function
  390.      *
  391.      * @return The Strengthened Burdet-Johnson Cut
  392.      */

  393.     public org.drip.linearprogram.cuttingplane.StrengthenedBurdetJohnsonCut strengthenedBurdetJohnsonCut (
  394.         final double[] lambdaArray,
  395.         final org.drip.function.definition.R1ToR1 r1ToR1Increasing)
  396.     {
  397.         try
  398.         {
  399.             return new org.drip.linearprogram.cuttingplane.StrengthenedBurdetJohnsonCut (
  400.                 _aGrid,
  401.                 _bArray,
  402.                 lambdaArray,
  403.                 r1ToR1Increasing
  404.             );
  405.         }
  406.         catch (java.lang.Exception e)
  407.         {
  408.             e.printStackTrace();
  409.         }

  410.         return null;
  411.     }
  412. }