Jump to content

Transportation problem: Difference between revisions

Line 2,495:
 
=={{header|Julia}}==
UsingCode taken from [https://github.com/JuliaOptdylanomics/MathProgBase.jltransportation_problem MathProgBasehere] using [https://jump.dev/JuMP.jl/stable/ JuMP].
<lang julia>using MathProgBaseJuMP, ClpIpopt
 
# cost vector
c = [3, 5, 7, 3, 2, 5];
N = size(c,1);
 
# constraints Ax (<,>,=) b
A = [1 1 1 0 0 0
0 0 0 1 1 1
1 0 0 1 0 0
0 1 0 0 1 0
0 0 1 0 0 1];
b = [ 25, 35, 20, 30, 10];
s = ['<', '<', '=', '=', '='];
 
# construct model
b = [ 25, 35, 20, 30, 10]
model = Model(Ipopt.Optimizer)
s = ['<', '<', '=', '=', '=']
@variable(model, x[i=1:N] >= 0, base_name="traded quantities")
cost_fn = @expression(model, c'*x) # cost function
@constraint(model, C1, A[1:2,:]*x .<= b[1:2]) # inequality constraints
@constraint(model, C2, A[3:5,:]*x .== b[3:5]) # equality constraints
@objective(model, Min, cost_fn) # objective function
 
# solve model
sol = linprog(c, A, b, s, ClpSolver())
status = JuMP.optimize!(model);
@show sol.status
xstar = value.(x);
@show sol.sol
println("solution vector of quantities = ", xstar)
@show sol.objval</lang>
println("minimum total cost = ", JuMP.objective_value(model))
 
# recover Lagrange multipliers for post-optimality
λ = [JuMP.dual(C1[1]),JuMP.dual(C1[2])]
μ = [JuMP.dual(C2[1]),JuMP.dual(C2[2]),JuMP.dual(C2[3])]
{{out}}
<pre>
<pre>sol.status = :Optimal
sol.solsolution vector of quantities = [20.0000000008747048, 0.0, 54.09999996490783145, 0.0, 30.0000000007494098, 5.00000003509216855]
sol.objvalminimum total cost = 180179.099999927567436</pre>
 
=={{header|Kotlin}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.