Transportation problem: Difference between revisions

Content added Content deleted
Line 2,495: Line 2,495:


=={{header|Julia}}==
=={{header|Julia}}==
Using [https://github.com/JuliaOpt/MathProgBase.jl MathProgBase.jl].
Code taken from [https://github.com/dylanomics/transportation_problem here] using [https://jump.dev/JuMP.jl/stable/ JuMP].
<lang julia>using MathProgBase, Clp
<lang julia>using JuMP, Ipopt


# cost vector
c = [3, 5, 7, 3, 2, 5]
c = [3, 5, 7, 3, 2, 5];
N = size(c,1);


# constraints Ax (<,>,=) b
A = [1 1 1 0 0 0
A = [1 1 1 0 0 0
0 0 0 1 1 1
0 0 0 1 1 1
1 0 0 1 0 0
1 0 0 1 0 0
0 1 0 0 1 0
0 1 0 0 1 0
0 0 1 0 0 1]
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}}
{{out}}
<pre>
<pre>sol.status = :Optimal
sol.sol = [20.0, 0.0, 5.0, 0.0, 30.0, 5.0]
solution vector of quantities = [20.000000008747048, 0.0, 4.9999996490783145, 0.0, 30.000000007494098, 5.0000003509216855]
sol.objval = 180.0</pre>
minimum total cost = 179.99999927567436</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==