# Standard Form conversion¶

Convert the following linear program into standard form:

\begin{aligned} \text{minimize}\qquad& p + q\\ \text{subject to:}\qquad& 5p - 3q = 7 \\ & 2p + q \ge 2 \\ & 1 \le q \le 4 \end{aligned}

### Original problem¶

In :
using JuMP, Clp

m = Model(solver=ClpSolver())
@variable(m, p )
@variable(m, 1 <= q <= 4 )
@constraint(m, 5p - 3q == 7 )
@constraint(m, 2p + q >= 2 )
@objective(m, Min, p + q )

status = solve(m)

println(m)
println(status)
println()
println("p = ", getvalue(p) )
println("q = ", getvalue(q) )
println("objective = ", getobjectivevalue(m) )

Min p + q
Subject to
5 p - 3 q == 7
2 p + q >= 2
p
1 <= q <= 4

Optimal

p = 2.0
q = 1.0
objective = 3.0


### Standard form¶

should look like: \begin{aligned} \text{maximize}\qquad& c^T x\\ \text{subject to:}\qquad& Ax \le b\\ & x \ge 0 \end{aligned}

In :
using JuMP, Clp

m = Model(solver=ClpSolver())
@variable(m, u >= 0 )
@variable(m, v >= 0 )
@variable(m, w >= 0 )
@constraint(m, (w+1) <= 4 )
@constraint(m, -5(u-v) + 3(w+1) <= -7 )
@constraint(m, 5(u-v) - 3(w+1) <= 7 )
@constraint(m, -2(u-v) - (w+1) <= -2 )
@objective(m, Max, -(u-v) - (w+1) )

status = solve(m)

println(m)
println(status)
println()
println("p = ", getvalue(u-v) )
println("q = ", getvalue(w+1) )
println("objective = ", -getobjectivevalue(m) )

Max -u + v - w - 1
Subject to
w <= 3
-5 u + 5 v + 3 w <= -10
5 u - 5 v - 3 w <= 10
-2 u + 2 v - w <= -1
u >= 0
v >= 0
w >= 0

Optimal

p = 2.0
q = 1.0
objective = 3.0


### Standard form (compact)¶

In :
# we defined the vector x to be: x = [u;  v;  w]
A = [0 0 1; -5 5 3; 5 -5 -3; -2 2 -1]
b = [3; -10; 10; -1]
c = [-1; 1; -1]

using JuMP, Clp

m = Model(solver=ClpSolver())
@variable(m, x[1:3] >= 0 )      # specify a vector variable
@constraint(m, A*x .<= b )      # the dot in front of <=, which indicates element-wise (vector) inequalities
@objective(m, Max, dot(c,x) )   # must use dot(c,x) or (c'*x) to return a scalar

status = solve(m)

println(m)
println(status)
println()
println("p = ", getvalue(x-x) )
println("q = ", getvalue(x+1) )
println("objective = ", -(getobjectivevalue(m)-1) )

Max -x + x - x
Subject to
x <= 3
-5 x + 5 x + 3 x <= -10
5 x - 5 x - 3 x <= 10
-2 x + 2 x - x <= -1
x[i] >= 0 for all i in {1,2,3}

Optimal

p = 2.0
q = 1.0
objective = 3.0

In [ ]: