%% Convert Problem to Structure
% 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
beq = problem.beq

% Examine the bounds.

problem.lb
problem.ub

% Solve the problem by calling intlinprog.

x = intlinprog(problem)

% Copyright 2012 The MathWorks, Inc.