Created on Fri Feb 18 11:22:33 2022 @author: mesa

In [13]:
import numpy as np
from supplychainmodelhelper import graphoperations as go
import pandas as pd

#help(go.calibration)

# creating the graph
prod = ['eggs']
act = ['Producer','Consumer']
loc = [
        'Portugal',
        'Spain',
        'France',
        'Italy',
        'Germany',
        'Montenegro',
        'Marocco'
        ]
     
myNW = go.createGraph(
        listOfActors=act,
        listOfLocations=loc,
        listOfProducts=prod)
    
    # Creating Distance matrix
myData = {
    loc[0]: [1, 625, 1737, 2509, 2781, 3351,1100],
    loc[1]: [625, 1, 1277, 1965, 2320, 2804,1116],
    loc[2]: [1737, 1277, 1, 1420, 1054, 2038,2247],
    loc[3]: [2509, 1965, 1420, 1, 1505, 740,3019],
    loc[4]: [2781, 2320, 1054, 1505, 1, 1636,3297],
    loc[5]: [3351, 2804, 2038, 740, 1636, 1,3883],
    loc[6]: [1100,1116,2247,3019,3297,3883,1]
}
    
myDF = pd.DataFrame(myData,index=loc)
    
    # supply and demand
    
sendingProd = [[12588339,44688368,48464470,33153139,24304433,2300,9204611]]#eggs
   
receivingProd = [[19101284,91333314,48818138,957046,12195878,0,0]]#eggs
    

    
myTDs = []
allFlows = np.ndarray((7,7,1,5))
    
    # creating list of node IDs of participants
for run,p in enumerate(prod):
    print("product ", p)
    print("------------------------")
    senderIDs = go.getListOfNodeIDs(
        myNW,
        actors=['Producer'],
        products=[p]
    )
    receiverIDs = go.getListOfNodeIDs(
        myNW,
        actors=['Consumer'],
        products=[p]
    )
    
    # creating edgeIDs for adding 'distance' to graph
    allcombIDs1, allcombIDs2 = go.getAllCombinations(
        senderIDs,
        receiverIDs,
        order='1st'
    )
    myEdges4Graph = go.getEdgeID(myNW, allcombIDs1, allcombIDs2)
    didItWork3 = go.addAttr2Edges(
        myNW,
        myEdges4Graph,
        myDF.values.flatten(),
        attr='distance'
    )
    
    go.addAttr2ExistingNodes(myNW,senderIDs,'output',sendingProd[run])
    go.addAttr2ExistingNodes(myNW,receiverIDs,'input',receivingProd[run])
    
    TD = go.calcPotTransportDistances(
            myNW,
            listOfSenderIDs=senderIDs,
            listOfReceiverIDs=receiverIDs
        )
    # get the transport distances
    myTDs.append(TD)
    print("TD ", p, ":", TD)
    
    # Run the gravity model with given transport distance and return the
    # flow each myTDs[run] is a list of 5 transport distances
    # allFlows is input for the calibration algorithm
    for it, td in enumerate(myTDs[run]):
        allFlows[
            0:len(senderIDs),
            0:len(receiverIDs),
            run,
            it
        ] = go.hymanModel(
                myNW,
                listOfSenderIDs=senderIDs,
                listOfReceiverIDs=receiverIDs,
                transportDistance=td,
                tolerance=0.01
            )
        eggsflow= go.hymanModel(
            myNW,
            listOfSenderIDs=senderIDs,
            listOfReceiverIDs=receiverIDs,
            transportDistance=td,
            tolerance=0.01
        )
    
   # determining beta
 #   myBeta = 0.1
 #   flow = go.furnessModel(
 #       myNW,
#        senderIDs,
 #       receiverIDs,
 #       myBeta,
 #       'distance',
 #       'exp'
 #   )
product  eggs
------------------------
ipfn converged: convergence_rate not updating or below rate_tolerance
ipfn converged: convergence_rate not updating or below rate_tolerance
ipfn converged: convergence_rate not updating or below rate_tolerance
ipfn converged: convergence_rate not updating or below rate_tolerance
ipfn converged: convergence_rate not updating or below rate_tolerance
TD  eggs : [200.0, 400.0, 599.0, 798.0, 998.0]
On the  1. iteration: tolerance is down to 387.6656
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  2. iteration: tolerance is down to 115.4034
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  3. iteration: tolerance is down to 117.8567
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  4. iteration: tolerance is down to 199.0000
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  5. iteration: tolerance is down to 199.0000
ipfn converged: convergence_rate not updating or below rate_tolerance
ipfn converged: convergence_rate not updating or below rate_tolerance
On the last iteration ( 2.): tolerance is down to 115.4034
On the  1. iteration: tolerance is down to 387.6656
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  2. iteration: tolerance is down to 115.4034
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  3. iteration: tolerance is down to 117.8567
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  4. iteration: tolerance is down to 199.0000
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  5. iteration: tolerance is down to 199.0000
ipfn converged: convergence_rate not updating or below rate_tolerance
ipfn converged: convergence_rate not updating or below rate_tolerance
On the last iteration ( 2.): tolerance is down to 115.4034
On the  1. iteration: tolerance is down to 224.0946
On the  2. iteration: tolerance is down to 188.4168
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  3. iteration: tolerance is down to 310.2875
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  4. iteration: tolerance is down to 306.4942
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  5. iteration: tolerance is down to 399.0000
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  6. iteration: tolerance is down to 399.0000
ipfn converged: convergence_rate not updating or below rate_tolerance
ipfn converged: convergence_rate not updating or below rate_tolerance
On the last iteration ( 2.): tolerance is down to 188.4168
On the  1. iteration: tolerance is down to 224.0946
On the  2. iteration: tolerance is down to 188.4168
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  3. iteration: tolerance is down to 310.2875
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  4. iteration: tolerance is down to 306.4942
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  5. iteration: tolerance is down to 399.0000
ipfn converged: convergence_rate not updating or below rate_tolerance
On the  6. iteration: tolerance is down to 399.0000
ipfn converged: convergence_rate not updating or below rate_tolerance
ipfn converged: convergence_rate not updating or below rate_tolerance
On the last iteration ( 2.): tolerance is down to 188.4168
On the  1. iteration: tolerance is down to 103.2061
On the  2. iteration: tolerance is down to 49.4617
On the  3. iteration: tolerance is down to 25.0537
On the  4. iteration: tolerance is down to 10.2688
On the  5. iteration: tolerance is down to 3.6909
On the  6. iteration: tolerance is down to 0.9199
On the  7. iteration: tolerance is down to 0.1178
/home/NeubertK/anaconda3/envs/p39/lib/python3.9/site-packages/ipfn/ipfn.py:146: RuntimeWarning: invalid value encountered in double_scalars
  if abs(m_ijk / ori_ijk - 1) > max_conv:
/home/NeubertK/anaconda3/envs/p39/lib/python3.9/site-packages/supplychainmodelhelper/graphoperations.py:1920: RuntimeWarning: divide by zero encountered in double_scalars
  (
/home/NeubertK/anaconda3/envs/p39/lib/python3.9/site-packages/supplychainmodelhelper/graphoperations.py:1628: RuntimeWarning: invalid value encountered in double_scalars
  WeightDist = np.sum(flow * dist) / (np.sum(flow))
On the  8. iteration: tolerance is down to 0.0044
On the last iteration ( 8.): tolerance is down to 0.0044
On the  1. iteration: tolerance is down to 103.2061
On the  2. iteration: tolerance is down to 49.4617
On the  3. iteration: tolerance is down to 25.0537
On the  4. iteration: tolerance is down to 10.2688
On the  5. iteration: tolerance is down to 3.6909
On the  6. iteration: tolerance is down to 0.9199
On the  7. iteration: tolerance is down to 0.1178
On the  8. iteration: tolerance is down to 0.0044
On the last iteration ( 8.): tolerance is down to 0.0044
On the  1. iteration: tolerance is down to 0.2847
On the  2. iteration: tolerance is down to 0.0004
On the last iteration ( 2.): tolerance is down to 0.0004
On the  1. iteration: tolerance is down to 0.2847
On the  2. iteration: tolerance is down to 0.0004
On the last iteration ( 2.): tolerance is down to 0.0004
On the  1. iteration: tolerance is down to 108.9248
On the  2. iteration: tolerance is down to 29.0070
On the  3. iteration: tolerance is down to 3.1320
On the  4. iteration: tolerance is down to 0.0612
On the  5. iteration: tolerance is down to 0.0001
On the last iteration ( 5.): tolerance is down to 0.0001
On the  1. iteration: tolerance is down to 108.9248
On the  2. iteration: tolerance is down to 29.0070
On the  3. iteration: tolerance is down to 3.1320
On the  4. iteration: tolerance is down to 0.0612
On the  5. iteration: tolerance is down to 0.0001
On the last iteration ( 5.): tolerance is down to 0.0001

creating calibration measure that halfway might make sense

In [14]:
allFoodTransported = sum(sum(sendingProd,[]))
dummy = np.random.rand(48)*allFoodTransported/80+            allFoodTransported/(len(sum(sendingProd,[]))+1)
myCalMat = np.append(
        dummy,
        allFoodTransported-np.sum(dummy)).reshape((len(senderIDs),
        len(receiverIDs))
)
    
print(go.calibration(allFlows,myCalMat))
exiting with relative difference to calibration matrix: 11.574211743055177
[4]
In [ ]: