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

  111. public class ILPObjective
  112. {
  113.     private double[] _c = null;

  114.     /**
  115.      * ILPObjective Constructor
  116.      *
  117.      * @param c The Objective Coefficient Array
  118.      *
  119.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  120.      */

  121.     public ILPObjective (
  122.         final double[] c)
  123.         throws java.lang.Exception
  124.     {
  125.         if (null == (_c = c))
  126.         {
  127.             throw new java.lang.Exception (
  128.                 "ILPObjective Constructor => Invalid Inputs"
  129.             );
  130.         }

  131.         int dimension = _c.length;

  132.         if (0 == dimension)
  133.         {
  134.             throw new java.lang.Exception (
  135.                 "ILPObjective Constructor => Invalid Inputs"
  136.             );
  137.         }

  138.         for (int dimensionIndex = 0;
  139.             dimensionIndex < dimension;
  140.             ++dimensionIndex)
  141.         {
  142.             if (!org.drip.numerical.common.NumberUtil.IsValid (
  143.                 _c[dimensionIndex]
  144.             ))
  145.             {
  146.                 throw new java.lang.Exception (
  147.                     "ILPObjective Constructor => Invalid Inputs"
  148.                 );
  149.             }
  150.         }
  151.     }

  152.     /**
  153.      * Retrieve "c"
  154.      *
  155.      * @return c
  156.      */

  157.     public double[] c()
  158.     {
  159.         return _c;
  160.     }

  161.     /**
  162.      * Retrieve the Variate Dimension
  163.      *
  164.      * @return Variate Dimension
  165.      */

  166.     public int dimension()
  167.     {
  168.         return _c.length;
  169.     }

  170.     /**
  171.      * Validate the Variate Input
  172.      *
  173.      * @param variateArray The Input Variate Array
  174.      *
  175.      * @return TRUE - The Input Variate successfully Validated
  176.      */

  177.     public boolean validate (
  178.         final int[] variateArray)
  179.     {
  180.         if (null == variateArray)
  181.         {
  182.             return false;
  183.         }

  184.         int dimension = _c.length;

  185.         if (dimension != variateArray.length)
  186.         {
  187.             return false;
  188.         }

  189.         for (int dimensionIndex = 0;
  190.             dimensionIndex < dimension;
  191.             ++dimensionIndex)
  192.         {
  193.             if (0 >= variateArray[dimensionIndex])
  194.             {
  195.                 return false;
  196.             }
  197.         }

  198.         return true;
  199.     }

  200.     /**
  201.      * Evaluate the Objective Function at the specified Variate Array
  202.      *
  203.      * @param variateArray The Input Variate Array
  204.      *
  205.      * @return The Objective Function at the specified Variate Array
  206.      *
  207.      * @throws java.lang.Exception Thrown if the Evaluation cannot be done
  208.      */

  209.     public double evaluate (
  210.         final int[] variateArray)
  211.         throws java.lang.Exception
  212.     {
  213.         if (!validate (
  214.             variateArray
  215.         ))
  216.         {
  217.             throw new java.lang.Exception (
  218.                 "ILPObjective::evaluate => Variate Array not Valid"
  219.             );
  220.         }

  221.         double value = 0.;
  222.         int dimension = _c.length;

  223.         for (int dimensionIndex = 0;
  224.             dimensionIndex < dimension;
  225.             ++dimensionIndex)
  226.         {
  227.             value += _c[dimensionIndex] * variateArray[dimensionIndex];
  228.         }

  229.         return value;
  230.     }
  231. }