TopographyMap.java

  1. package org.drip.sample.graph;

  2. import java.util.Map;

  3. import org.drip.numerical.common.FormatUtil;
  4. import org.drip.service.env.EnvManager;
  5. import org.drip.spaces.graph.Edge;
  6. import org.drip.spaces.graph.Topography;
  7. import org.drip.spaces.graph.Vertex;

  8. /*
  9.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  10.  */

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

  70. /**
  71.  * <i>TopographyMap</i> illustrates the Construction of Topographical Map. The References are:
  72.  *  
  73.  * <br><br>
  74.  *  <ul>
  75.  *      <li>
  76.  *          Wikipedia (2018a): Graph (Abstract Data Type)
  77.  *              https://en.wikipedia.org/wiki/Graph_(abstract_data_type)
  78.  *      </li>
  79.  *      <li>
  80.  *          Wikipedia (2018b): Graph Theory https://en.wikipedia.org/wiki/Graph_theory
  81.  *      </li>
  82.  *      <li>
  83.  *          Wikipedia (2018c): Graph (Discrete Mathematics)
  84.  *              https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)
  85.  *      </li>
  86.  *      <li>
  87.  *          Wikipedia (2018d): Dijkstra's Algorithm https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
  88.  *      </li>
  89.  *      <li>
  90.  *          Wikipedia (2018e): Bellman-Ford Algorithm
  91.  *              https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
  92.  *      </li>
  93.  *  </ul>
  94.  * <br><br>
  95.  *  <ul>
  96.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalCore.md">Numerical Core Module</a></li>
  97.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/AlgorithmSupportLibrary.md">Algorithm Support Library</a></li>
  98.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  99.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/graph/README.md">Graph Builder and Navigator</a></li>
  100.  *  </ul>
  101.  * <br><br>
  102.  *
  103.  * @author Lakshmi Krishnamurthy
  104.  */

  105. public class TopographyMap
  106. {

  107.     private static final Topography SetTopography()
  108.         throws Exception
  109.     {
  110.         String[] vertexArray = new String[]
  111.         {
  112.             "Delhi     ",
  113.             "Bombay    ",
  114.             "Madras    ",
  115.             "Calcutta  ",
  116.             "Bangalore ",
  117.             "Hyderabad ",
  118.             "Cochin    ",
  119.             "Pune      ",
  120.             "Ahmedabad ",
  121.             "Jaipur    "
  122.         };

  123.         Topography topography = new Topography();

  124.         for (String vertexName : vertexArray)
  125.         {
  126.             topography.addVertex (vertexName);
  127.         }

  128.         topography.addEdge (
  129.             new Edge (
  130.                 vertexArray[0], // Delhi
  131.                 vertexArray[1], // Bombay
  132.                 1388.
  133.             )
  134.         );

  135.         topography.addEdge (
  136.             new Edge (
  137.                 vertexArray[0], // Delhi
  138.                 vertexArray[2], // Madras
  139.                 2191.
  140.             )
  141.         );

  142.         topography.addEdge (
  143.             new Edge (
  144.                 vertexArray[1], // Bombay
  145.                 vertexArray[2], // Madras
  146.                 1279.
  147.             )
  148.         );

  149.         topography.addEdge (
  150.             new Edge (
  151.                 vertexArray[0], // Delhi
  152.                 vertexArray[3], // Calcutta
  153.                 1341.
  154.             )
  155.         );

  156.         topography.addEdge (
  157.             new Edge (
  158.                 vertexArray[1], // Bombay
  159.                 vertexArray[3], // Calcutta
  160.                 1968.
  161.             )
  162.         );

  163.         topography.addEdge (
  164.             new Edge (
  165.                 vertexArray[2], // Madras
  166.                 vertexArray[3], // Calcutta
  167.                 1663.
  168.             )
  169.         );

  170.         topography.addEdge (
  171.             new Edge (
  172.                 vertexArray[2], // Madras
  173.                 vertexArray[4], // Bangalore
  174.                 361.
  175.             )
  176.         );

  177.         topography.addEdge (
  178.             new Edge (
  179.                 vertexArray[2], // Madras
  180.                 vertexArray[5], // Hyderabad
  181.                 784.
  182.             )
  183.         );

  184.         topography.addEdge (
  185.             new Edge (
  186.                 vertexArray[2], // Madras
  187.                 vertexArray[6], // Cochin
  188.                 697.
  189.             )
  190.         );

  191.         topography.addEdge (
  192.             new Edge (
  193.                 vertexArray[1], // Bombay
  194.                 vertexArray[7], // Pune
  195.                 192.
  196.             )
  197.         );

  198.         topography.addEdge (
  199.             new Edge (
  200.                 vertexArray[1], // Bombay
  201.                 vertexArray[8], // Ahmedabad
  202.                 492.
  203.             )
  204.         );

  205.         topography.addEdge (
  206.             new Edge (
  207.                 vertexArray[0], // Delhi
  208.                 vertexArray[9], // Jaipur
  209.                 308.
  210.             )
  211.         );

  212.         return topography;
  213.     }

  214.     private static final void DisplayVertexes (
  215.         final Topography topography,
  216.         final String vertexName)
  217.         throws Exception
  218.     {
  219.         Vertex vertex = topography.vertex (vertexName);

  220.         Map<String, Double> egressMap = vertex.egressMap();

  221.         System.out.println();

  222.         System.out.println ("\t||----------------------------------||");

  223.         for (Map.Entry<String, Double> egressEntry : egressMap.entrySet())
  224.         {
  225.             System.out.println (
  226.                 "\t|| " + vertexName + " => " +
  227.                 egressEntry.getKey() + " = " +
  228.                 FormatUtil.FormatDouble (egressEntry.getValue(), 4, 0, 1.) + " ||"
  229.             );
  230.         }

  231.         System.out.println ("\t||----------------------------------||");
  232.     }

  233.     public static void main (
  234.         final String[] inputArray)
  235.         throws Exception
  236.     {
  237.         EnvManager.InitEnv ("");

  238.         Topography topography = SetTopography();

  239.         for (String vertexName : topography.vertexNameSet())
  240.         {
  241.             DisplayVertexes (
  242.                 topography,
  243.                 vertexName
  244.             );
  245.         }

  246.         EnvManager.TerminateEnv();
  247.     }
  248. }