%% Solve MILP Using Problem-Based Approach
% This example shows how to set up a problem using the problem-based approach and then solve it using the solver-based approach. The problem is
% \mathop {\min }\limits_x \left( { - 3{x_1} - 2{x_2} - {x_3}} \right)
% {\rm{\ subject\ to\ }}\left\{ {\begin{array}{*{20}{l}}
% {{x_3}{\rm{\ binary}}}\\
% {{x_1},{x_2} \ge 0}\\
% {{x_1} + {x_2} + {x_3} \le 7}\\
% {4{x_1} + 2{x_2} + {x_3} = 12}
% \end{array}} \right.
% Create an OptimizationProblem object named prob to represent this problem. To specify a binary variable, create an optimization variable with integer type, a lower bound of 0, and an upper bound of 1.

x = optimvar('x',2,'LowerBound',0);
xb = optimvar('xb','LowerBound',0,'UpperBound',1,'Type','integer');
prob = optimproblem('Objective',-3*x(1)-2*x(2)-xb);
cons1 = sum(x) + xb <= 7;
cons2 = 4*x(1) + 2*x(2) + xb == 12;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;

% Convert the problem object to a problem structure.

problem = prob2struct(prob);

% Solve the resulting problem structure.

[sol,fval,exitflag,output] = intlinprog(problem)

% Both sol(1) and sol(3) are binary-valued. Which value corresponds to the binary optimization variable xb?

prob.Variables

% The variable xb appears last in the Variables display, so xb corresponds to sol(3) = 1. See Algorithms.
% Copyright 2018 The MathWorks, Inc.