Skip to main content

LInear Optimization Networks - shortest path algorithms

Project description

build codecov License: GPL v3

LInear Optimization Networks (LION)

             ,%%%%%%%%,
           ,%%/\%%%%/\%%
          ,%%%\c "" J/%%%
 %.       %%%%/ o  o \%%%
 `%%.     %%%%    _  |%%%
  `%%     `%%%%(__Y__)%%'
  //       ;%%%%`\-/%%%'
 ((       /  `%%%%%%%'
  \\    .'          |
   \\  /       \  | |
    \\/         ) | |
     \         /_ | |__
     (___________)))))))

This package implements variants of shortest path algorithms to compute minimal angle shortest paths and k diverse shortest paths through a cost / resistance array.

Possible applications include

  • Computer vision tasks
  • Route finding in robotics
  • Infrastructure planning

Installation

Simple install with pip:

pip install lion-sp

The library itself has few dependencies (see setup.py).

  • numba (for fast compiled algorithms)
  • numpy
  • scipy

Installation from git:

Create a virtual environment and activate it:

python3 -m venv env
source env/bin/activate

Install the repository in editable mode:

git clone https://github.com/NinaWie/lion
cd lion
pip install -e .

Instructions

All main functions are available in algorithms. Usage is examplified in test_api.

Input:

  • instance: 2D numpy array of real float or integer values which are the costs
  • configuration dictionary with
    • start_inds: start coordinates, e.g. [0,0]
    • dest_inds: target coordinates, e.g. [20,20]
    • angle_weight: Float between 0 and 1, 0: angles don't matter, 1: only angles are minimized, costs are not taken into account
    • forbidden_val: value indicating that a cell is forbidden / outside of the project region (can be int, np.nan, np.inf ... )
    • point_dist_min: minimum cell distance of neighboring points (default 3)
    • point_dist_max: minimum cell distance of neighboring points (default 5)
    • angle_weight: how important is the angle (default 0)
    • edge_weight: importantance of costs between points compared to the cost at the points (default=0, i.e. only the points count)
    • max_direction_deviation: maximum deviation in angle from the straight connection from start to end (default: pi/2)
    • max_angle: maximum angle of adacent edges (default: pi, i.e. no restriction)
    • angle_cost_function: 'linear' and 'discrete' are implemented. See code
    • memory_limit: default is 1 trillion, if the number of edges is higher, an iterative approximation procedure (''pipeline'') is used
    • pipeline: List of decreasing positive integers, ending with 1 The pipeline in an iterative approach defines the downsampling factors for each step. By default, it is set automatically based on the memory limit. It can however be set manually as well, e.g. [3,1] means downsample by factor of 3, compute optimal path, reduce region of interest to a corridor around optimal path (corridor width is computed automatically based on the memory_limit) then downsample by factor of 1 (aka full resolution). There is no support for setting the corridor width manually because it does not make sense to make it smaller than it could be
    • between_points_allowed: If True, then forbidden areas can still be between points (only relevant for point routing) If False, then it is not allowed that a forbidden area is between two points of the path
    • diversity_threshold:
      • FOR KSP.ksp: Minimum diversity of next path from previous paths in cell distance. E.g. if thresh = 200, each path will be at least 200 cells away at one point from each other path. If None, it is set by default to 1/20 of the instance size
      • FOR KSP.min_set_intersection: maximum intersection of the new path with all previous points Must be between 0 and 1. E.g. if 0.2, then at most 20% of cells are shared between one path and the other paths

Example usage

np.random.seed(0)
instance = np.random.rand(100,100)
# place some forbidden regions
instance[instance>0.9] = np.inf
# define config dictionary
cfg = {
  "start_inds": [0,2],
  "dest_inds": [96, 93],
  "angle_weight": 0.5,
  "forbidden_val": np.inf
}

# Compute the least cost route:
opt_route = optimal_route(instance, cfg)
# >>> optimal_route.shape
# (120, 2) --> x and y coordinates of 120 cells on the route

# compute k diverse routes:
multiple_routes = ksp_routes(instance, cfg, 5)
# >>> len(multiple_routes)
# 5
# >>> np.all(np.array(multiple_routes[0]) == np.array(opt_route)))
# True --> 1st route is the optimal one, other routes are diverse alternatives 

# define minimum and maximum distance between points
cfg["point_dist_min"] = 5
cfg["point_dist_max"] = 7

opt_points = optimal_points(instance, cfg)
# >>> len(opt_points)
# 26 --> only 26 because they can be more than 1 apart
# >>> np.linalg.norm(np.array(opt_points[0]) - np.array(opt_points[1]))
# 5.83095189 --> Euclidean distance between cells is between 5 and 7

multiple_points = ksp_points(instance, cfg, 5)
# >>> len(multiple_points)
# 5

If you use this code in a publication, please cite the following paper:

@article{wiedemann2021optimization,
  title={An Optimization Framework for Power Infrastructure Planning},
  author={Wiedemann, Nina and Adjiashvili, David},
  journal={arXiv preprint arXiv:2101.03388},
  year={2021}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lion-sp-0.1.1.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lion_sp-0.1.1-py3-none-any.whl (48.6 kB view details)

Uploaded Python 3

File details

Details for the file lion-sp-0.1.1.tar.gz.

File metadata

  • Download URL: lion-sp-0.1.1.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6

File hashes

Hashes for lion-sp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b8055fcdcb41559d1c7917f0cf7f826d6e2f54bba0af2a8c265b608f8459f2a5
MD5 07dec7141cfca0c9ce5f391eaae0ead7
BLAKE2b-256 0614cd46fb841d9eb88dd905c41af7ac5c02e5c1179386e238ed1a4a51d73a58

See more details on using hashes here.

File details

Details for the file lion_sp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: lion_sp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 48.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6

File hashes

Hashes for lion_sp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5d98d8371c4ebfdbb31f9a11c7f0ee2faade87f6473cb26b8341260da4c8fa35
MD5 c9788c1e514af83acaffa7d4324d53c5
BLAKE2b-256 0870e63010beeb39544dc65691ce7564d04a7f55d36cd6cf74888a37c509248e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page