Created on Fri Feb 18 11:22:33 2022 @author: mesa
from supplychainmodelhelper import graphoperations as go
from supplychainmodelhelper.drawer import Drawer
from supplychainmodelhelper import mds
import pandas as pd
import numpy as np
import glob, os, subprocess
#help(go.hymanModel)
#help(go.calibration)
prod = ['eggs','milk']
act = ['Producer','Consumer']
loc = [
'Portugal',
'Spain',
'France',
'Italy',
'Germany',
'Montenegro',
'Marocco'
]
myNW = go.createGraph(
listOfActors=act,
listOfLocations=loc,
listOfProducts=prod)
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)
sendingProd = [[12588339,44688368,48464470,33153139,24304433,2300,9204611],#eggs
[75752927,121886945,490834291,3933048,497278336,138,0]]#milk
receivingProd = [[19101284,91333314,48818138,957046,12195878,0,0],#eggs
[70146856,121623784,95268102,761020054,104160029,14602,37452258]]#milk
print('send eggs:', sum(sendingProd[0]))
print('received eggs:',sum(receivingProd[0]))
print('send milk:', sum(sendingProd[1]))
print('received milk:',sum(receivingProd[1]))
myTDs = []
allFlows = np.ndarray((7,7,2,5))
for run,p in enumerate(prod):
print('run:',run)
print('p:', p)
# creating list of node IDs of participants
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(
list1=senderIDs,
list2=receiverIDs,
order='1st'
)
myEdges4Graph = go.getEdgeID(
SCGraph=myNW,
outgoingNodes=allcombIDs1,
incomingNodes=allcombIDs2
)
didItWork3 = go.addAttr2Edges(
SCGraph=myNW,
listOfEdgeIDs=myEdges4Graph,
listOfContent=myDF.values.flatten(),
attr='distance'
)
go.addAttr2ExistingNodes(
SCGraph=myNW,
listOfNodeIDs=senderIDs,
nameOfAttr='output',
listOfAttr=sendingProd[run]
)
go.addAttr2ExistingNodes(
SCGraph=myNW,
listOfNodeIDs=receiverIDs,
nameOfAttr='input',
listOfAttr=receivingProd[run]
)
# get the transport distances
myTDs.append(
go.calcPotTransportDistances(
SCGraph=myNW,
listOfSenderIDs=senderIDs,
listOfReceiverIDs=receiverIDs,
nrOfValues=5
)
)
# 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]):
hmod = go.hymanModel(
myNW,
listOfSenderIDs=senderIDs,
listOfReceiverIDs=receiverIDs,
transportDistance=td,
tolerance=0.01
)
print(hmod)
allFlows[
0:len(senderIDs),
0:len(receiverIDs),
run,
it
] = hmod
ipfn/ipfn.py:146: RuntimeWarning: invalid value encountered in double_scalars
if abs(m_ijk / ori_ijk - 1) > max_conv:
supplychainmodelhelper/graphoperations.py:1920: RuntimeWarning: divide by zero encountered in double_scalars
(
supplychainmodelhelper/graphoperations.py:1628: RuntimeWarning: invalid value encountered in double_scalars
WeightDist = np.sum(flow * dist) / (np.sum(flow))
sendingProd = [[12588339,44688368,48464470,33153139,24304433,2300,9204611],#eggs
[75752927,121886945,490834291,3933048,497278336,137,1]]#milk
receivingProd = [[19101284,91333314,48818138,957046,12195876,1,1],#eggs
[70146856,121623784,95268102,761020054,104160029,14602,37452258]]#milk
myTDs = []
allFlows = np.ndarray((7,7,2,5))
for run,p in enumerate(prod):
print('run:',run)
print('p:', p)
# creating list of node IDs of participants
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(
list1=senderIDs,
list2=receiverIDs,
order='1st'
)
myEdges4Graph = go.getEdgeID(
SCGraph=myNW,
outgoingNodes=allcombIDs1,
incomingNodes=allcombIDs2
)
didItWork3 = go.addAttr2Edges(
SCGraph=myNW,
listOfEdgeIDs=myEdges4Graph,
listOfContent=myDF.values.flatten(),
attr='distance'
)
go.addAttr2ExistingNodes(
SCGraph=myNW,
listOfNodeIDs=senderIDs,
nameOfAttr='output',
listOfAttr=sendingProd[run]
)
go.addAttr2ExistingNodes(
SCGraph=myNW,
listOfNodeIDs=receiverIDs,
nameOfAttr='input',
listOfAttr=receivingProd[run]
)
# get the transport distances
myTDs.append(
go.calcPotTransportDistances(
SCGraph=myNW,
listOfSenderIDs=senderIDs,
listOfReceiverIDs=receiverIDs,
nrOfValues=5
)
)
# 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]):
hmod = go.hymanModel(
myNW,
listOfSenderIDs=senderIDs,
listOfReceiverIDs=receiverIDs,
transportDistance=td,
tolerance=0.01
)
print(hmod)
allFlows[
0:len(senderIDs),
0:len(receiverIDs),
run,
it
] = hmod
--> are zeros not allowed?
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))
# creating the graph
prod = ['milk','beer','schnaps']
act = ['producer','consumer','warehouse','store']
loc = ['BER','SXF','TXL']
myNW = go.createGraph(
listOfActors=act,
listOfLocations=loc,
listOfProducts=prod)
# Creating Distance matrix
myData={loc[0]:[1,2,50],loc[1]:[2,1,49],loc[2]:[50,49,1]}
myDF = pd.DataFrame(myData,index=loc)
# supply and demand
sendingProd = [[10,50,40],[11,45,44],[15,55,30]]
receivingProd = [[30,30,40],[40,30,30],[10,70,20]]
myTDs = []
allFlows = np.ndarray((3,3,3,5))
# creating list of node IDs of participants
for run,p in enumerate(prod):
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])
# get the transport distances
myTDs.append(
go.calcPotTransportDistances(
myNW,
listOfSenderIDs=senderIDs,
listOfReceiverIDs=receiverIDs
)
)
# 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]):
hm = go.hymanModel(
myNW,
listOfSenderIDs=senderIDs,
listOfReceiverIDs=receiverIDs,
transportDistance=td,
tolerance=0.01
)
print(hm)
allFlows[
0:len(senderIDs),
0:len(receiverIDs),
run,
it
] = hm
#creating calibration measure that halfway might make sense
allFoodTransported = sum(sum(sendingProd,[]))
dummy = np.random.rand(8)*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))
/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))