I have a problem which is to find the location and the plant which offer the least costly options for different firms. Therefore, the list is given as follows:
[[{4: [14.038, 28.423, 43.483, 58.00699999999999]},
{3: [31.645999999999997, 78.993, 110.59100000000001]},
{1: [30.967000000000002, 68.55000000000001, 100.32900000000001]}],
[{4: [27.651999999999997, 59.993, 93.76999999999998, 122.67799999999998]},
{3: [34.604, 80.786, 116.641]},
{2: [59.28399999999999, 136.2, 206.18, 277.543, 340.03800000000007]},
{1: [32.953, 77.665, 123.44800000000001]}],
[{4: [15.295000000000002, 30.656, 47.892999999999994, 63.731999999999985]},
{3: [29.290999999999997, 74.506, 110.141]}],
[{4: [36.424, 67.84299999999999, 99.04999999999998, 134.88299999999998]},
{3: [39.557, 75.643, 111.09500000000001]}]]
For instance, the first firm would be :
[{4: [14.038, 28.423, 43.483, 58.00699999999999]},
{3: [31.645999999999997, 78.993, 110.59100000000001]},
{1: [30.967000000000002, 68.55000000000001, 100.32900000000001]}]
and its least costly option would be: Location 4, at plant 0 and a cost of 14.038.
I would like to be able to find the correct code that would return that specific solution. Furthermore, I would like to know if there is a way to return the second best solution. In the case of the first firm, we would have: Location 4, at plant 1 and a cost of 28.423.
Thanks!
A possible solution is to sort all the costs from each firm, location and plant and take the less costly k
, where k
is the number of options for (firm, location, plant) you are looking for:
k = 2
best_k = sorted(
[(c, firm_id, location_id, plant_id)
for firm_id, plant_res in enumerate(costs)
for location_res in plant_res
for location_id, location_costs in location_res.items()
for plant_id, c in enumerate(location_costs)
]
)[:k]
where costs
is the list in your question.
Here best_k
:
[(14.038, 0, 4, 0), (15.295000000000002, 2, 4, 0), (27.651999999999997, 1, 4, 0)]
So, the less costly option is 14.038, provided by firm 0 at location 4, plant 0. The second option is 15.295, provided by firm 2 at location 4, plant 0. And, in the end, the first option is 27.652 provided by firm 1 at location 4, plant 0.