Convert optimization problem to solver form
problem = prob2struct(prob)
Convert an optimization problem object to a problem structure.
Input the basic MILP problem from Mixed-Integer Linear Programming Basics: Problem-Based.
ingots = optimvar('ingots',4,1,'Type','integer','LowerBound',0,'UpperBound',1); alloys = optimvar('alloys',4,1,'LowerBound',0); weightIngots = [5,3,4,6]; costIngots = weightIngots.*[350,330,310,280]; costAlloys = [500,450,400,100]; cost = costIngots*ingots + costAlloys*alloys; steelprob = optimproblem; steelprob.Objective = cost; totalweight = weightIngots*ingots + sum(alloys); carbonIngots = [5,4,5,3]/100; molybIngots = [3,3,4,4,]/100; carbonAlloys = [8,7,6,3]/100; molybAlloys = [6,7,8,9]/100; totalCarbon = (weightIngots.*carbonIngots)*ingots + carbonAlloys*alloys; totalMolyb = (weightIngots.*molybIngots)*ingots + molybAlloys*alloys; steelprob.Constraints.conswt = totalweight == 25; steelprob.Constraints.conscarb = totalCarbon == 1.25; steelprob.Constraints.consmolyb = totalMolyb == 1.25;
Convert the problem to an
intlinprog problem structure.
problem = prob2struct(steelprob);
Examine the resulting linear equality constraint matrix and vector.
Aeq = problem.Aeq
Aeq = (1,1) 1.0000 (2,1) 0.0800 (3,1) 0.0600 (1,2) 1.0000 (2,2) 0.0700 (3,2) 0.0700 (1,3) 1.0000 (2,3) 0.0600 (3,3) 0.0800 (1,4) 1.0000 (2,4) 0.0300 (3,4) 0.0900 (1,5) 5.0000 (2,5) 0.2500 (3,5) 0.1500 (1,6) 3.0000 (2,6) 0.1200 (3,6) 0.0900 (1,7) 4.0000 (2,7) 0.2000 (3,7) 0.1600 (1,8) 6.0000 (2,8) 0.1800 (3,8) 0.2400
beq = problem.beq
beq = 3×1 25.0000 1.2500 1.2500
Examine the bounds.
ans = 8×1 0 0 0 0 0 0 0 0
ans = 8×1 Inf Inf Inf Inf 1 1 1 1
Solve the problem by calling
x = intlinprog(problem)
LP: Optimal objective value is 8125.600000. Cut Generation: Applied 3 mir cuts. Lower bound is 8495.000000. Relative gap is 0.00%. Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
x = 8×1 7.2500 0 0.2500 3.5000 1.0000 1.0000 0 1.0000
problem— Problem structure
intlinprogproblem structure |
linprogproblem structure |
quadprogproblem structure |
If the objective and constraint functions are linear and at least
one problem variable has type
problem is an
If the objective constraint functions are linear and no problem
variable has type
problem is a
If the constraints are linear and the objective function is a
constant plus a sum of squares of linear expressions, then
problem is an
If the constraints are linear and the objective is a general
quadratic expression, then
problem is a
quadprog problem structure.
The problem structure includes an additional field,
that represents an additive constant for the objective function. If you
solve the problem structure using the specified solver, the returned
objective function value does not include the
If you solve
prob using the
solve function, the returned objective function value
If the ObjectiveSense of
problem uses the
negative of the objective function in
prob. This is
because solvers minimize. To maximize, they minimize the negative of the
original objective function. In this case, the reported optimal function
value is the negative of the value in the original problem. See Maximizing an Objective. You cannot use
lsqlin for a maximization problem.
The basis for the problem structure is an implicit ordering of all problem variables
into a single vector. The order of the problem variables is the same as the order of the
Variables property in
For example, suppose that the problem variables are in this order:
x — a 3-by-2-by-4 array
y — a 3-by-2 array
Then the implicit variable order is as if the problem variable is
The first 24 elements of
vars are equivalent to
x(:), and the next six elements are equivalent to
y(:), for a total of 30 elements. The lower and upper bounds
correspond to this variable ordering, and in this example, each linear constraint matrix
has 30 columns.