Posts

Showing posts from April, 2022

Minimum Distance to f(x) with Scipy Optimize

Image
  Shortest Distance Between f(x) and a Point ¶ In this notebook, I show a simple demonstration of using Scipy Optimize to find shortest distance from a given point $(𝑥_p,𝑦_p)$ to a curve: 𝑦 = 𝑓(𝑥). I will need to import a few Python libraries for this. In [1]: import numpy as np import scipy.optimize as optimize import matplotlib.pyplot as plt Scipy Optimize has an impressive array of functions to support common optimization problems. For this problem, I will use scipy.minimize to find the minimum distance. minimize requires an objective function and an initial guess for $x_{opt}$. f(x) is the "curve" we want to find the shortest distance to. This can be any continuous function of x. In [2]: def f ( x ): return np . cos ( x ) # + np.sin(x) def dist ( x ): """ Returns the Euclidean distance from (xp, yp) to (x, f(x)) """ y = f ( x ) dist ...

Tramp Steamer Problem using Python

Image
Here's a Jupyter notebook I made a while back with a solution to the classic optimization problem of finding the most profitable route for a cargo ship. Sequential search and Binary search are implemented to solve a simple example. Source on Github Tramp Steamer Problem ¶ Al Duke 3/11/2022 Figure 1. Tramp Steamer Imagine you own a cargo ship that transports goods between ports for profit. There are several ports and each voyage between ports results in profit p and takes time t. The port-port pairs are determined by goods available at port A and desired at port B. Not all ports are connected and connections are one-way. You want to find the routes that generate the most profit in the shortest time. We can address this problem using Graph Theory. For this problem, we have a directed, weighted graph (digraph) which includes vertices V (ports) and edges E (connections) which have profits p and traversal times t. Example 1 below shows a simple graph. Figure 2...