I am working on a model to match supply and demand. A short explanation to the model: I have several “supply” points with a certain unit of resources and several “demand” points. The aim is to “discharge” the available resources from the “supply” points to the closest “demand” point, by minimizing transport distances.
In ArcGIS, I already found the closest “demand” point to each “supply” point with corresponding distance. In addition I have the amount of resources produced at the “supply” point and resources needed at the “demand” point . I stored the results in an Excel table and sorted them for distance small to large so that I can work further with them using Python.
I made a table with a simple overview, where I have 2 “supply” points and several “demand” points. The resources available at “supply” point 1 is 10 and at “supply” point 2 is 20. The demand at all “demand” points varies . The Excel table looks like this:
With that Excel table, I want to calculate how many units of resources could be supplied from each “supply” point ('D') to the closest (and 2nd closest …n closest) “demand” point('E'), until the value in the “supply” point is 0/distributed completely. I want to store the value of how much is transported from “supply” to “demand” in an additional column ('F') For that I wrote this code:
for row in range(1,sheet.max_row+1):
if sheet['D'+str(row)].value !=0 and sheet['E'+str(row)].value !=0:
for row in range(1,sheet.max_row+1):
if sheet['D'+str(row)].value > sheet['E'+str(row)].value:
sheet['F'+str(row)].value = sheet['E'+str(row)].value
sheet['D'+str(row)].value = sheet['D'+str(row)].value - sheet['E'+str(row)].value
sheet['E'+str(row)].value = 0
if sheet['D'+str(row)].value < sheet['E'+str(row)].value:
sheet['F'+str(row)].value = sheet['D'+str(row)].value
sheet['E'+str(row)].value = sheet['E'+str(row)].value -sheet['D'+str(row)].value
sheet['D'+str(row)].value = 0
if sheet['D'+str(row)].value == sheet['E'+str(row)].value:
sheet['F'+str(row)].value = sheet['E'+str(row)].value
sheet['D'+str(row)].value = 0
sheet['E'+str(row)].value = 0
The above code works and calculates all values that I want in the columns F in all rows over the length of the list.
However, there remains one problem:
The way the code is written now, it does not update the values of supply and demand properly. So it does not take into account that, if some resources are supplied from one “supply” point to a “demand” point, the amount of available resources in the “supply” cell changes for calculations thereafter.
More specifically, the “supply” point 1 only has 10 units available that it can “distribute”. In the example table below, “supply” point 1 can only supply resources to the first and second closest “demand” point, until all resources of “supply” point 1 are used up (6 units to “demand point” with DEMAND_FID = 10 and 4 units to “demand point” DEMAND_FID = 11). Afterwards, all units of “supply” point 1 (SUPPLY_R) are “gone”, the demand of “demand point” with DEMAND_FID = 10 is fulfilled and the demand of “demand point” with DEMAND_FID = 11 is reduced from 6 to 1. Therefore, the 6th row should not even be calculated, since “supply” point 1 cannot supply any additional resources to a “demand point” Therefore, I want to update the values of SUPPLY_R and DEMAND_R after one row has been calculated.
The “ideal” table with results should look like this:
How can I solve the issue of changing one value in one row, based on the previous rows within a calculation?
I have tried to update the new “supply” and “demand” values with values from a dictionary but this does not give the desired result.