# Pulp Python: How to formulate a time-based variable for shipping demurrage

I am working on a shipping optimisation problem that aims to minimise demurrage charges as a result of low/insufficient inventory.

I have daily vessel requirement (sales) data in the format sales[vessel_start_date][vessel][product_required_tonnes]. I also have the daily stock inventory level in the form port_inventory[date][product_availability_tonnes]

• Demurrage is defined as: (total_days_in_port - laycan_days) * per_diem_charge. It is incurred when there is insufficient material at the port to load the vessel and the vessel's laycan period (vessel_start_date plus 10 days) has expired.
• Each vessel takes two days to load.

I would be very grateful if anyone could help me formulate this expression in pulp, or give me a steer. Many thanks.

I have created a model that I believe addresses your objective of minimizing demurrage charges. It utilizes indicator variables for each day and vessel showing when each vessel is finally loaded. The factors for the variables in the objective function are to be calculated based on the demurrage formula you provided.

The first thing to do with this model is to ensure that you have the port inventory working correctly, then add the demurrage calculation for the objective. The formulation I have provided does not deal with the two day loading period. You'll want to fill in details on how inventory increases at the port.

vessels = ['a', 'b']
sales_days = list(range(20))
sales = {1: {"a": 10}, 3: {"b": 10}}
vessel_qty = {"a": 10, "b": 10}
per_diem_charge = 100
laycan_days = 10
initial_port_inventory = 2
daily_inventory_increase = 1
model = pulp.LpProblem("PortDemurrage")
day_port_inventory = pulp.LpVariable.dict("port_inventory", sales_days,
lowBound=0, cat="Integer")
# set initial inventory
model += day_port_inventory == initial_port_inventory
vessel_start_dates = {vessel: day for day in sales for vessel in sales[day]}
# denote the vessel sales by day the vessel is finally loaded
vessel_sales = pulp.LpVariable.dicts("vessel_sales", [f"{vessel}_{day}" for
vessel in vessels for day in sales_days[vessel_start_dates[vessel]:]],
lowBound=0, cat="Integer")
for day in sales_days[1:]:
day_sales = 0
for vessel in vessels:
if vessel_start_dates[vessel] <= day:
day_sales += vessel_sales[f"{vessel}_{day}"]
model += day_port_inventory[day] == day_port_inventory[day - 1] +
daily_inventory_increase - day_sales

for vessel in vessels:
vessel_start_date = vessel_start_dates[vessel]
possible_days = sales_days[vessel_start_date:]
model += sum(vessel_sales[f"{vessel}_{day}"] for day in possible_days) ==
vessel_qty[vessel]

obj = 0
final_day_vars = []
for vessel in vessels:
demurrage_day_start = vessel_start_dates[vessel]+laycan_days+1
vessel_start_date = vessel_start_dates[vessel]
for day in sales_days[vessel_start_date:]:
vessel_day_var = pulp.LpVariable(f"{vessel}_sales_day_{day}",
lowBound=0, upBound=1, cat="Integer")
final_day_vars.append(vessel_day_var)
if day >= demurrage_day_start:
obj += vessel_day_var * (per_diem_charge * (day -
vessel_start_dates[vessel] - laycan_days))
model += vessel_sales[f"{vessel}_{day}"] <= vessel_day_var *
vessel_qty[vessel]
model += sum(final_day_vars) == len(vessels)
model += obj, "minimize demurrage charge"


Solving this model produces a solution where vessel a is finally loaded on day 8 and vessel b is finally loaded on day 18. The objective value of this solution is 500, since vessel b needs to wait 15 days after arrival.

• Thank you very much for the comprehensive response - very much appreciated!
– cmp
Jun 13, 2020 at 21:39
• could you please me clarify what 'vessel_qty[vessel]' is?
– cmp
Jun 16, 2020 at 13:59
• It is the load requirement quantity by vessel. Jun 17, 2020 at 17:43
• Thank you for clarifying, much appreciated. I dont suppose you would perhaps be able to help me with my other question? or.stackexchange.com/questions/4394/…
– cmp
Jun 17, 2020 at 18:44
• I added a portion of the mathematical formulation in that question. I believe that the pulp implementation here addresses both questions. Jun 17, 2020 at 22:56
{"ft_lang_label":"__label__en","ft_lang_prob":0.77899295,"math_prob":0.864184,"size":2708,"snap":"2023-40-2023-50","text_gpt3_token_len":699,"char_repetition_ratio":0.19045858,"word_repetition_ratio":0.0057971017,"special_character_ratio":0.2688331,"punctuation_ratio":0.11893204,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96847105,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T00:34:37Z\",\"WARC-Record-ID\":\"<urn:uuid:a96a83e7-d10e-4988-ae01-11c373814031>\",\"Content-Length\":\"135407\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1cdffb3a-a8c6-4825-8e28-55206ae5101e>\",\"WARC-Concurrent-To\":\"<urn:uuid:67b6aed0-35c9-4e8a-8f54-9f612ccb93aa>\",\"WARC-IP-Address\":\"104.18.11.86\",\"WARC-Target-URI\":\"https://or.stackexchange.com/questions/4365/pulp-python-how-to-formulate-a-time-based-variable-for-shipping-demurrage\",\"WARC-Payload-Digest\":\"sha1:ZQTQNFYZNQ3A4NRA2I35FWN6HFO2DR5S\",\"WARC-Block-Digest\":\"sha1:NND3HXY33H4DNLQGVIQ2RREF6S5JL5TB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506320.28_warc_CC-MAIN-20230922002008-20230922032008-00225.warc.gz\"}"}