In this post we consider a simple way to optimize your crypto or currency transactions.
Let us consider the following situation. Your need local currency to cover unexpected expenses but have only crypto. You call several persons (lets they be A, B, C, D) to whom you trust and ask if they will be able to exchange your crypto on local currency. The rates and amounts of local currency available are presented in the table below. The only condition is that these rates are valid for one hour.

Times in minutes between different locations are shown below.

You want to have the maximal amount of local currency. In which order you should visit these persons?
Below is a python script to solve this problem. You may change parameters to suit your conditions. You will need to have python 3 and itertools installed on your computer. If you do not know how to do this see [1,2].
Copy this code into a file optim.py and run it with the command: python3 optim.py. Python is very sensitive to TABs, therefore make sure that all TABs in the code are preserved in the file optim.py. If during a coping process TABs were replaced on spaces you should delete the spaces and insert TABs.
def times(x:list,tm:list) -> list:
#calculate times spent on visits from a permutation x and times tm
t=[0,0,0,0,0] #times spent on visits
t[0]=tm[0][x[0]-1] #from you to the first person
for i in range(1,4):
k=x[i]-1
kp=x[i-1]
t[i]=t[i-1]+tm[kp][k]
return t
def amounts(x:list,tm_v:list,rates:list,limits:list) -> list:
#calculate amount received in 60 minutes and number of persons visited
a=0.0
for i in range(4):
k=x[i]-1
if tm_v[i]<60:
a=a+limits[k]/rates[k]
else:
break
return [a,i+1]
#main
from itertools import permutations as perm
rates=[1.2,1.3,1.4,1.5] #exchange rates
limits=[5000,3000,2000,3000] #limits of available local currency
tm=[[35,20,20,50],[0,8,19,39],[8,0,9,17],[19,9,0,14],[39,17,14,0]] #times between persons
max_a=0
count=0
for p in perm(range(1,5)):
p1=list(p)
tm_visits=times(p1,tm)
[a,i]=amounts(p1,tm_visits,rates,limits)
if a>max_a:
count=0
max_a=a
n=i
max_p=p1
if a==max_a:
count=count+1
print("optimal solution:","max_amount=",max_a,"number of visits=",n,"order=",max_p)
if count>1:
#select all other optimal solutions
print("other optimal solutions")
for p in perm(range(1,5)):
p1=list(p)
tm_visits=times(p1,tm)
[a,i]=amounts(p1,tm_visits,rates,limits)
if (max_a == a and max_p != p1):
print("max_amount=",a,"number of visits=",n,"order=",p)
As the result you will see the optimal solution.
![]()
The optimal solution: max_amount= 9902.930402930404 number of visits= 4 order= [3, 4, 2, 1]
This means that you should start from the person C (index 3) then go to the person D (index 4) then go to the person B (index 2) and finally to the person A (index 1). Your total amount received in local currency is equal to 9,902.93.
References:
[1] How to install python
https://realpython.com/installing-python/
https://www.codecademy.com/article/install-python3
[2] How to install pip3
https://www.activestate.com/resources/quick-reads/how-to-install-and-use-pip3/