Question: Formulate a linear programming problem to solve the transportation problem. You are given as input: Graph with vertices and edges . Each undirected edge (
Formulate a linear programming problem to solve the transportation problem. You are given as input:
Graph with vertices
and edges
Each undirected edge
can be regarded as two edges
and
in opposite directions, if it is easier for your solution.
A cost :
associated with each edge such that
and is the cost of transporting one unit of oil along the edge.
A map :
mapping each vertex
to net supply
note that if
then it is considered a demand
We suggest following steps on pencilpaper to formulate the problem.
a identify all the decision variables,
b write down the constraints, and
c write down the objective function.
Complete the python function calculateOptimalPlan that takes inputs:
n: number of vertices which are numbered
;
edgelist: a list of undirected edges
between vertices wherein
is the cost of flow along the edge;
supplies: a list of size
where suppliesj is the supply or demand if negative at the jth vertex.
Your function must return a dictionary that maps edges ij to the flow along the edge in the direction
All flows must be nonnegative.
If you specify a flow from
to
then your dictionary must have the key ji mapped to the nonnegative flow from
to
If an edge is not present in the dictionary, we will take the flow along it to be zero.
from pulp import
def calculateOptimalPlann edgelist, supplies, debugFalse:
assert n
assert all i n and j n and i j and c for ijc in edgelist
assert lensupplies n
# TODO: Formulate the LP for optimal transportation plan and return the solution as a dictionary
# from edges ij to flow from i to j
# If an edge is not present in the dictionary, we will take its flow to be zero.
# your code here
The tests should pass:
def testsolutionn edgelist, supplies, solutionmap, expectedcost:
cost
outflows n
inflows n
for ijc in edgelist:
if ij in solutionmap:
flow solutionmapij
cost c flow
assert flow f'flow on edge ij is negative flow
outflowsi flow
inflowsj flow
elif ji in solutionmap:
flow solutionmapji
cost c flow
assert flow f'flow on edge ji in negative flow
outflowsj flow
inflowsi flow
for i s in enumeratesupplies:
if s :
assert outflowsi inflowsi s f'Vertex i constraint violated: total outflow outflowsi inflow inflowsi supply s
else:
assert absinflowsioutflowsi sEf'Vertexi constraint violated: inflow inflowsi outflowoutflowsi demand s
if expectedcost None:
assert absexpectedcost costE f'Expected cost: expectedcost your algorithm returned: cost
printTest Passed!
n
edgelist
supplies
solmap calculateOptimalPlann edgelist, supplies, debugTrue
testsolutionn edgelist, supplies, solmap,
n
edgelist
supplies
solmap calculateOptimalPlann edgelist, supplies, debugTrue
testsolutionn edgelist, supplies, solmap,
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
