Transportation problem: Difference between revisions
Content added Content deleted
Line 2,495: | Line 2,495: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Code taken from [https://github.com/dylanomics/transportation_problem here] using [https://jump.dev/JuMP.jl/stable/ JuMP]. |
|||
<lang julia>using |
<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]; |
||
⚫ | |||
⚫ | |||
# construct model |
|||
⚫ | |||
model = Model(Ipopt.Optimizer) |
|||
⚫ | |||
@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 |
|||
solution vector of quantities = [20.000000008747048, 0.0, 4.9999996490783145, 0.0, 30.000000007494098, 5.0000003509216855] |
|||
minimum total cost = 179.99999927567436</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |