Main 2023 test
View or edit on GitHub
This page is synchronized from trase/models/diet_trase/main_2023_test.ipynb. Last modified on 2025-12-13 00:30 CET by Trase Admin.
Please view or edit the original file there; changes should be reflected here after a midnight build (CET time),
or manually triggering it with a GitHub action (link).
Documentation
Testing stuff to see if it works
import sys
from datetime import datetime
import numpy as np
import pandas as pd
# import math
from math import ceil
from tqdm import tqdm
from trase.tools.sps import *
from trase.tools.aws import upload_pandas_df_to_s3
from trase.tools.matching.fair_allocation import proportional_sourcing, direct_sourcing
from trase.tools.sei_pcs.supply_chain import split_flows_using_proportions
from trase.tools.sei_pcs.supply_chain import SupplyChain
from definition import years as model_years, version
from constants import HS6_TO_PRODUCT
# from trase.tools.sei_pcs.pandas_lp import TraseGlpkCommand
pd.set_option("display.float_format", "{:,.2f}".format)
HS6_TO_PRODUCT
/opt/tljh/user/lib/python3.8/trase/tools/sei_pcs/pandas_utilities.py:17: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
from pandas.util.testing import assert_frame_equal
{'090111': 'green',
'090112': 'green',
'090121': 'processed',
'090122': 'processed',
'090190': 'processed',
'210111': 'processed',
'210112': 'processed'}
# Run supply chain model
# for year in range(2008, 2021 + 1):
for year in [2020]:
print("-" * 40)
print("Running {} {} {}...".format("Indonesia", "Coffee", year))
# sc = SupplyChain('cocoa_coffee_global/Indonesia/coffee', year=year, bucket="trase-storage")
sc = SupplyChain("cocoa_coffee_global/Indonesia/coffee", year=year)
# Set up Dataset
sc.preparation()
sc.load()
----------------------------------------
Running Indonesia Coffee 2020...
Extracting data from source ...[0m [97m took 0.1 seconds[0m
[97mSkipping re-process of Production[0m
Extracting data from source ...[0m [97m took 0.1 seconds[0m
[97mSkipping re-process of Flows[0m
Extracting data from source ...[0m [97m took 0.1 seconds[0m
[97mSkipping re-process of Jurisdictions[0m
Extracting data from source ...[0m [97m took 0.1 seconds[0m
[97mSkipping re-process of DistanceMatrixJurisdictionToPort[0m
Extracting data from source ...[0m [97m took 0.1 seconds[0m
[97mSkipping re-process of DistanceMatrixJurisdictionToJurisdiction[0m
Extracting data from source ...[0m [97m took 0.1 seconds[0m
[97mSkipping re-process of Assets[0m
Extracting data from source ...[0m [97m took 0.0 seconds[0m
[97mSkipping re-process of Population[0m
Production
# Jurisdiction production
production = sc.get("production").set_index("geocode.geocode")["value"]
production.index = production.index.rename("geocode")
# convert to series
production = production.squeeze()
# # Get total annual production
total_production = sum(production)
# # print("production (sample)")
# # print(production.sample(3))
# # print("total production")
print(total_production)
# production.sample(10)
production
753900.0
geocode
ID-11 73,400.00
ID-12 75,000.00
ID-13 12,300.00
ID-14 2,400.00
ID-15 18,700.00
ID-16 191,200.00
ID-17 62,700.00
ID-18 118,100.00
ID-19 0.00
ID-21 0.00
ID-31 0.00
ID-32 22,400.00
ID-33 24,900.00
ID-34 500.00
ID-35 48,500.00
ID-36 2,200.00
ID-51 15,300.00
ID-52 5,900.00
ID-53 24,200.00
ID-61 3,700.00
ID-62 400.00
ID-63 1,300.00
ID-64 200.00
ID-65 200.00
ID-71 3,700.00
ID-72 2,600.00
ID-73 33,700.00
ID-74 2,800.00
ID-75 100.00
ID-76 4,300.00
ID-81 400.00
ID-82 0.00
ID-91 0.00
ID-94 2,800.00
Name: value, dtype: float64
Flows
# Call in flows
flows_df = sc.get("flows")
# THIS NEEDS TO BE DONE ON RAW EQUIVALENT VALUES EVENTUALLY
total_exports = sum(sc.get("flows")["mass_tonnes"])
demand_export = flows_df
demand_export = demand_export.set_index(["port_of_export_label.trase_id"])[
"mass_tonnes"
]
demand_export.index = demand_export.index.rename("trase_id")
demand_export = demand_export.groupby(level=["trase_id"]).sum()
# print("total exports")
print(total_exports)
# # print("flows_df (sample)")
# flows_df.sample(10)
print(demand_export)
533128.0693800185
trase_id
ID-PORT-0007 106.62
ID-PORT-0010 70,967.80
ID-PORT-0032 0.14
ID-PORT-0036 0.00
ID-PORT-0047 3.18
ID-PORT-0056 1,097.76
ID-PORT-0069 4.21
ID-PORT-0078 233,240.64
ID-PORT-0114 8,504.43
ID-PORT-0118 62.12
ID-PORT-0121 108.00
ID-PORT-0130 2.74
ID-PORT-0131 18.00
ID-PORT-0136 74,572.14
ID-PORT-0137 144,112.05
ID-PORT-0167 247.95
ID-PORT-0168 0.22
ID-PORT-0172 0.10
ID-PORT-0174 79.43
ID-PORT-0175 0.55
Name: mass_tonnes, dtype: float64
Domestic demand
total_domestic_demand = total_production - total_exports
population = sc.get("populations")
population_prop = population.set_index("geocode")["proportions"]
domestic_demand = total_domestic_demand * population_prop
print(total_domestic_demand)
print(domestic_demand)
220771.9306199815
geocode
ID-11 4,329.31
ID-12 12,100.53
ID-13 4,515.60
ID-14 5,295.18
ID-15 2,906.89
ID-16 6,930.39
ID-17 1,649.22
ID-18 7,346.36
ID-19 1,196.51
ID-21 1,745.05
ID-31 8,549.91
ID-32 39,551.99
ID-33 29,646.42
ID-34 3,011.60
ID-35 32,942.78
ID-36 9,808.38
ID-51 3,534.52
ID-52 4,381.99
ID-53 4,376.07
ID-61 4,436.19
ID-62 2,194.40
ID-63 3,347.99
ID-64 3,089.98
ID-65 582.64
ID-71 2,129.07
ID-72 2,454.58
ID-73 7,385.75
ID-74 2,162.86
ID-75 954.82
ID-76 1,167.69
ID-81 1,506.40
ID-82 1,056.17
ID-91 947.30
ID-94 3,537.41
Name: proportions, dtype: float64
Costs
costs_jur2port = sc.get("distance_matrix_jur_port")
# costs_jur2port = costs_jur2port.set_index(["origin", "destination"])["distance"]
costs_jur2jur = sc.get("distance_matrix_jur_jur")
# costs_jur2jur = costs_jur2jur.set_index(["origin", "destination"])["distance"]
costs = costs_jur2port.append(costs_jur2jur)
costs = costs.set_index(["origin", "destination"])["distance"]
# costs_jur2port.sample(10)
# costs_jur2jur.sample(10)
costs.sample(10)
/tmp/ipykernel_20120/3927901774.py:7: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
costs = costs_jur2port.append(costs_jur2jur)
origin destination
ID-51 ID-PORT-0069 41.79
ID-31 ID-PORT-0026 99.63
ID-82 ID-17 2,834.37
ID-62 ID-PORT-0013 654.82
ID-11 ID-PORT-0134 2,272.75
ID-76 ID-PORT-0060 1,648.59
ID-52 ID-19 1,391.47
ID-73 ID-32 1,435.85
ID-76 ID-PORT-0036 1,737.32
ID-64 ID-PORT-0008 436.87
Name: distance, dtype: float64
LP
supply = production
demand = demand_export.append(domestic_demand)
costs = costs
print(supply)
print(demand)
print(costs)
geocode
ID-11 73,400.00
ID-12 75,000.00
ID-13 12,300.00
ID-14 2,400.00
ID-15 18,700.00
ID-16 191,200.00
ID-17 62,700.00
ID-18 118,100.00
ID-19 0.00
ID-21 0.00
ID-31 0.00
ID-32 22,400.00
ID-33 24,900.00
ID-34 500.00
ID-35 48,500.00
ID-36 2,200.00
ID-51 15,300.00
ID-52 5,900.00
ID-53 24,200.00
ID-61 3,700.00
ID-62 400.00
ID-63 1,300.00
ID-64 200.00
ID-65 200.00
ID-71 3,700.00
ID-72 2,600.00
ID-73 33,700.00
ID-74 2,800.00
ID-75 100.00
ID-76 4,300.00
ID-81 400.00
ID-82 0.00
ID-91 0.00
ID-94 2,800.00
Name: value, dtype: float64
ID-PORT-0007 106.62
ID-PORT-0010 70,967.80
ID-PORT-0032 0.14
ID-PORT-0036 0.00
ID-PORT-0047 3.18
ID-PORT-0056 1,097.76
ID-PORT-0069 4.21
ID-PORT-0078 233,240.64
ID-PORT-0114 8,504.43
ID-PORT-0118 62.12
ID-PORT-0121 108.00
ID-PORT-0130 2.74
ID-PORT-0131 18.00
ID-PORT-0136 74,572.14
ID-PORT-0137 144,112.05
ID-PORT-0167 247.95
ID-PORT-0168 0.22
ID-PORT-0172 0.10
ID-PORT-0174 79.43
ID-PORT-0175 0.55
ID-11 4,329.31
ID-12 12,100.53
ID-13 4,515.60
ID-14 5,295.18
ID-15 2,906.89
ID-16 6,930.39
ID-17 1,649.22
ID-18 7,346.36
ID-19 1,196.51
ID-21 1,745.05
ID-31 8,549.91
ID-32 39,551.99
ID-33 29,646.42
ID-34 3,011.60
ID-35 32,942.78
ID-36 9,808.38
ID-51 3,534.52
ID-52 4,381.99
ID-53 4,376.07
ID-61 4,436.19
ID-62 2,194.40
ID-63 3,347.99
ID-64 3,089.98
ID-65 582.64
ID-71 2,129.07
ID-72 2,454.58
ID-73 7,385.75
ID-74 2,162.86
ID-75 954.82
ID-76 1,167.69
ID-81 1,506.40
ID-82 1,056.17
ID-91 947.30
ID-94 3,537.41
dtype: float64
origin destination
ID-11 ID-PORT-0001 1,713.68
ID-51 ID-PORT-0001 792.45
ID-19 ID-PORT-0001 457.58
ID-36 ID-PORT-0001 647.52
ID-17 ID-PORT-0001 928.15
...
ID-34 ID-71 1,862.00
ID-13 1,353.45
ID-16 866.41
ID-12 1,686.33
ID-34 0.00
Name: distance, Length: 6698, dtype: float64
/tmp/ipykernel_20120/3464272357.py:2: FutureWarning: The series.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
demand = demand_export.append(domestic_demand)
solve_transportation_problem(
supply=supply,
demand=demand,
costs=costs,
on_missing="warn",
)
Welcome to the CBC MILP Solver
Version: 2.10.3
Build Date: Dec 15 2019
command line - /opt/tljh/user/lib/python3.8/site-packages/pulp/solverdir/cbc/linux/64/cbc /tmp/d3bf6b18a3f7480fbeda484e14d53fa1-pulp.mps timeMode elapsed branch printingOptions all solution /tmp/d3bf6b18a3f7480fbeda484e14d53fa1-pulp.sol (default strategy 1)
At line 2 NAME MODEL
At line 3 ROWS
At line 93 COLUMNS
At line 5568 RHS
At line 5657 BOUNDS
At line 5658 ENDATA
Problem MODEL has 88 rows, 1836 columns and 3672 elements
Coin0008I MODEL read with 0 errors
Option for timeMode changed from cpu to elapsed
Presolve 83 (-5) rows, 1566 (-270) columns and 3132 (-540) elements
0 Obj 0 Primal inf 753900 (54)
47 Obj 2.334096e+08 Primal inf 63350.954 (43)
99 Obj 2.6982603e+08 Primal inf 4791.2683 (29)
131 Obj 2.709022e+08
Optimal - objective value 2.709022e+08
After Postsolve, objective 2.709022e+08, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 270902201.7 - 131 iterations time 0.002, Presolve 0.00
Option for printingOptions changed from normal to all
Total time (CPU seconds): 0.01 (Wallclock seconds): 0.01
(geocode
ID-11 ID-11 4,329.31
ID-PORT-0010 69,070.58
ID-12 ID-PORT-0010 1,897.21
ID-11 ID-PORT-0172 0.10
ID-12 ID-12 12,100.53
...
ID-72 ID-72 2,454.58
ID-73 ID-73 7,385.75
ID-PORT-0056 1,097.76
ID-74 ID-74 2,162.86
ID-76 ID-76 1,167.69
Length: 82, dtype: float64,
geocode
ID-11 0.00
ID-12 0.00
ID-13 0.00
ID-14 0.00
ID-15 0.00
ID-16 -0.00
ID-17 0.00
ID-18 0.00
ID-19 0.00
ID-21 0.00
ID-31 0.00
ID-32 0.00
ID-33 0.00
ID-34 0.00
ID-35 0.00
ID-36 0.00
ID-51 0.00
ID-52 0.00
ID-53 -0.00
ID-61 0.00
ID-62 0.00
ID-63 0.00
ID-64 0.00
ID-65 0.00
ID-71 -0.00
ID-72 0.00
ID-73 0.00
ID-74 0.00
ID-75 0.00
ID-76 -0.00
ID-81 0.00
ID-82 0.00
ID-91 0.00
ID-94 0.00
Name: value, dtype: float64)