set up a linear program, problem-凯发k8网页登录

main content

set up a linear program, problem-based

convert problem to solver form

this example shows how to convert a linear problem from mathematical form into optimization toolbox™ solver syntax using the problem-based approach.

the variables and expressions in the problem represent a model of operating a chemical plant, from an example in edgar and himmelblau [1]. two associated videos describe the problem.

  • presents the problem in pictorial form, showing how to generate the mathematical expressions of the model description.

  • describes how to convert these mathematical expressions into optimization toolbox solver syntax. this video shows how to solve the problem, and how to interpret the results.

this example, which closely follows the part 2 video, focuses on transforming the problem to solver syntax.

model description

the part 1 video suggests the following approach for converting a problem into mathematical form:

  1. get an overall idea of the problem.

  2. identify the goal (maximizing or minimizing something).

  3. identify (name) the variables.

  4. identify the constraints.

  5. determine which variables you can control.

  6. specify all quantities in mathematical notation.

  7. check the model for completeness and correctness.

for the meaning of the variables in this section, see the part 1 video.

the optimization problem is to minimize the objective function, subject to all the other expressions as constraints.

the objective function is:

0.002614 hps 0.0239 pp 0.009825 ep.

the constraints are:

2500p16250
i1192,000
c62,000
i1 - he1132,000
i1 = le1 he1 c
1359.8 i1 = 1267.8 he1 1251.4 le1 192 c 3413 p1
3000p29000
i2244,000
le2142,000
i2 = le2 he2
1359.8 i2 = 1267.8 he2 1251.4 le2 3413 p2
hps = i1 i2 bf1
hps = c mps lps
lps = le1 le2 bf2
mps = he1 he2 bf1 - bf2
p1 p2 pp24,550
ep pp12,000
mps271,536
lps100,623
all variables are positive.

first solution method: create optimization variable for each problem variable

the first solution method involves creating an optimization variable for each problem variable. as you create the variables, include their bounds.

p1 = optimvar('p1','lowerbound',2500,'upperbound',6250);
p2 = optimvar('p2','lowerbound',3000,'upperbound',9000);
i1 = optimvar('i1','lowerbound',0,'upperbound',192000);
i2 = optimvar('i2','lowerbound',0,'upperbound',244000);
c = optimvar('c','lowerbound',0,'upperbound',62000);
le1 = optimvar('le1','lowerbound',0);
le2 = optimvar('le2','lowerbound',0,'upperbound',142000);
he1 = optimvar('he1','lowerbound',0);
he2 = optimvar('he2','lowerbound',0);
hps = optimvar('hps','lowerbound',0);
mps = optimvar('mps','lowerbound',271536);
lps = optimvar('lps','lowerbound',100623);
bf1 = optimvar('bf1','lowerbound',0);
bf2 = optimvar('bf2','lowerbound',0);
ep = optimvar('ep','lowerbound',0);
pp = optimvar('pp','lowerbound',0);

create problem and objective

create an optimization problem container. include the objective function in the problem.

linprob = optimproblem('objective',0.002614*hps   0.0239*pp   0.009825*ep);

create and include linear constraints

the problem expressions contain three linear inequalities:

i1 - he1132,000
ep pp12,000
p1 p2 pp24,550
(1)

create these inequality constraints and include them in the problem.

linprob.constraints.cons1 = i1 - he1 <= 132000;
linprob.constraints.cons2 = ep   pp >= 12000;
linprob.constraints.cons3 = p1   p2   pp >= 24550;

the problem has eight linear equalities:

i2 = le2 he2
lps = le1 le2 bf2
hps = i1 i2 bf1
hps = c mps lps
i1 = le1 he1 c
mps = he1 he2 bf1 - bf2
1359.8 i1 = 1267.8 he1 1251.4 le1 192 c 3413 p1
1359.8 i2 = 1267.8 he2 1251.4 le2 3413 p2.
(2)

include these constraints as well.

linprob.constraints.econs1 = le2   he2 == i2;
linprob.constraints.econs2 = le1   le2   bf2 == lps;
linprob.constraints.econs3 = i1   i2   bf1 == hps;
linprob.constraints.econs4 = c   mps   lps == hps;
linprob.constraints.econs5 = le1   he1   c == i1;
linprob.constraints.econs6 = he1   he2   bf1 == bf2   mps;
linprob.constraints.econs7 = 1267.8*he1   1251.4*le1   192*c   3413*p1 == 1359.8*i1;
linprob.constraints.econs8 = 1267.8*he2   1251.4*le2   3413*p2 == 1359.8*i2;

solve problem

the problem formulation is complete. solve the problem using solve.

linsol = solve(linprob);
optimal solution found.

examine solution

evaluate the objective function. (you can also obtain this value by calling [linsol,fval] = solve(linprob).)

evaluate(linprob.objective,linsol)
ans =
   1.2703e 03

the lowest-cost method of operating the plant costs $1,207.30.

examine the solution variable values.

tbl = struct2table(linsol)
tbl =
  1×16 table
    bf1    bf2      c         ep         he1           he2           hps            i1           i2       le1       le2           lps           mps         p1       p2       pp  
    ___    ___    ______    ______    __________    __________    __________    __________    ________    ___    __________    __________    __________    ____    ______    _____
    0      0      8169.7    760.71    1.2816e 05    1.4338e 05    3.8033e 05    1.3633e 05    2.44e 05    0      1.0062e 05    1.0062e 05    2.7154e 05    6250    7060.7    11239

this table is too wide to view its contents easily. stack the variables to arrange them vertically.

vars = {'p1','p2','i1','i2','c','le1','le2','he1','he2',...
'hps','mps','lps','bf1','bf2','ep','pp'};
outputvars = stack(tbl,vars,'newdatavariablename','amt','indexvariablename','var')
outputvars =
  16×2 table
    var       amt    
    ___    __________
    p1          6250
    p2        7060.7
    i1    1.3633e 05
    i2      2.44e 05
    c        8169.7
    le1             0
    le2    1.0062e 05
    he1    1.2816e 05
    he2    1.4338e 05
    hps    3.8033e 05
    mps    2.7154e 05
    lps    1.0062e 05
    bf1             0
    bf2             0
    ep        760.71
    pp         11239
  • bf1, bf2, and le1 are 0, their lower bounds.

  • i2 is 244,000, its upper bound.

  • the nonzero components of the objective function (cost) are

    • hps380,328.74

    • pp11,239.29

    • ep760.71

the part 2 video interprets these characteristics in terms of the original problem.

second solution method: create one optimization variable and indices

alternatively, you can solve the problem using just one optimization variable that has indices with the names of the problem variables. this method enables you to give a lower bound of zero to all problem variables at once.

vars = {'p1','p2','i1','i2','c','le1','le2','he1','he2',...
    'hps','mps','lps','bf1','bf2','ep','pp'};
x = optimvar('x',vars,'lowerbound',0);

set variable bounds

include the bounds on the variables using dot notation.

x('p1').lowerbound = 2500;
x('p2').lowerbound = 3000;
x('mps').lowerbound = 271536;
x('lps').lowerbound = 100623;
x('p1').upperbound = 6250;
x('p2').upperbound = 9000;
x('i1').upperbound = 192000;
x('i2').upperbound = 244000;
x('c').upperbound = 62000;
x('le2').upperbound = 142000;

create problem, linear constraints, and solution

the remainder of the problem setup is similar to the setup using separate variables. the difference is that, instead of addressing a variable by its name, such as p1, you address it using its index, x('p1').

create the problem object, include the linear constraints, and solve the problem.

linprob = optimproblem('objective',0.002614*x('hps')   0.0239*x('pp')   0.009825*x('ep'));
linprob.constraints.cons1 = x('i1') - x('he1') <= 132000;
linprob.constraints.cons2 = x('ep')   x('pp') >= 12000;
linprob.constraints.cons3 = x('p1')   x('p2')   x('pp') >= 24550;
linprob.constraints.econs1 = x('le2')   x('he2') == x('i2');
linprob.constraints.econs2 = x('le1')   x('le2')   x('bf2') == x('lps');
linprob.constraints.econs3 = x('i1')   x('i2')   x('bf1') == x('hps');
linprob.constraints.econs4 = x('c')   x('mps')   x('lps') == x('hps');
linprob.constraints.econs5 = x('le1')   x('he1')   x('c') == x('i1');
linprob.constraints.econs6 = x('he1')   x('he2')   x('bf1') == x('bf2')   x('mps');
linprob.constraints.econs7 = 1267.8*x('he1')   1251.4*x('le1')   192*x('c')   3413*x('p1') == 1359.8*x('i1');
linprob.constraints.econs8 = 1267.8*x('he2')   1251.4*x('le2')   3413*x('p2') == 1359.8*x('i2');
[linsol,fval] = solve(linprob);
optimal solution found.

examine indexed solution

examine the solution as a vertical table.

tbl = table(vars',linsol.x')
tbl =
  16×2 table
    var1        var2   
    _____    __________
    'p1'           6250
    'p2'         7060.7
    'i1'     1.3633e 05
    'i2'       2.44e 05
    'c'          8169.7
    'le1'             0
    'le2'    1.0062e 05
    'he1'    1.2816e 05
    'he2'    1.4338e 05
    'hps'    3.8033e 05
    'mps'    2.7154e 05
    'lps'    1.0062e 05
    'bf1'             0
    'bf2'             0
    'ep'         760.71
    'pp'          11239

bibliography

[1] edgar, thomas f., and david m. himmelblau. optimization of chemical processes. new york: mcgraw-hill, 1987.

related topics

网站地图