8.8. MATLAB API

This section provides MindOpt MATLAB reference. Contents of the MATLAB are the following:

8.8.3. Examples

Diet problem

There are many different kinds of food, each of which can provide various nutrients. How should we make decisions regarding food intake to minimize the amount spent on purchasing food while ensuring that the daily nutritional requirements of the human body are met?

Sets

  • Food set \(F\)

  • Nutrient set \(N\)

Parameters

  • The content of nutrient \(i \in N\) in one unit of food \(j\) is \(a_{ij}\) .

  • The cost of obtaining one unit of food \(j \in F\) is \(c_j\) .

Decision Variables

  • \(x_j\) is the amount of food \(j \in F\) that a person consumes daily.

Objective Function

  • Minimize the total procurement cost of food: \(\text{minimize}~ \sum_{j \in F} c_{j} x_j\) .

Constraints

  • The nutritional requirement for each nutrient \(i \in N\) must lie between the minimum value \(n_{\min,i}\) and the maximum value \(n_{\max,i}\) : \(n_{\min,i} \le \sum_{j \in F} a_{ij} x_j \le n_{\max,i}, ~~\forall i \in N\) .


% Initialize data
 demand_ub = [inf, 375, inf, inf, inf, inf, inf, 75];
 demand_lb = [2000, 350, 55, 100, 100, 100, 100, -inf];
 nutrition_name = {'Calories', 'Carbohydrates', 'Protein', 'VitA', 'VitC', 'Calcium', 'Iron', 'Volume'};
 food_name = {'Cheeseburger', 'HamSandwich', 'Hamburger', 'FishSandwich', 'ChickenSandwich', 'Fries', 'SausageBiscuit', 'LowfatMilk', 'OrangeJuice'};
 food_price = [1.84, 2.19, 1.84, 1.44, 2.29, 0.77, 1.29, 0.60, 0.72];
 req_value = [510.0, 370.0, 500.0, 370.0, 400.0, 220.0, 345.0, 110.0, 80.0;
             34.0,  35.0,  42.0,  38.0,  42.0,  26.0,  27.0,  12.0,  20.0;
             28.0,  24.0,  25.0,  14.0,  31.0,  3.0,   15.0,  9.0,   1.0;
             15.0,  15.0,  6.0,   2.0,   8.0,   0.0,   4.0,   10.0,  2.0;
             6.0,   10.0,  2.0,   0.0,   15.0,  15.0,  0.0,   120.0, 4.0;
             30.0,  20.0,  25.0,  15.0,  15.0,  0.0,   20.0,  30.0,  2.0;
             20.0,  20.0,  20.0,  10.0,  8.0,   2.0,   15.0,  0.0,   2.0;
             4.0,   7.5,   3.5,   5.0,   7.3,   2.6,   4.1,   8.0,   12.0; ];

% build model
 model.A = sparse(req_value);
 model.obj = food_price;
 model.rhs = demand_ub;
 model.lhsorsense = demand_lb;
 model.modelsense = 'Min';
 model.varnames = food_name;
 model.constrnames = nutrition_name;

% optimize
 result = mindopt(model);

% print result
 if strcmp(result.Status,'OPTIMAL')
     fprintf('The total cost is %.3f\n', result.ObjVal);
     for v=1:length(food_name)
         fprintf('You should buy %.3f unit of %s\n', result.X(v), food_name{v});
     end
 else
     fprintf('The optimize status is %s', result.Status);
 end

Facility problem

Currently, there are two shopping malls, and the locations of the malls have already been determined. There are several alternative locations for constructing warehouses, whose coordinates and construction costs are known. We assume that the transportation cost from the warehouses to the shopping malls is independent of the quantity of goods but is related to the distance between them. Please find the minimum cost scheme for warehouse construction and transportation.

Sets

  • Alternative Warehouses \(F\)

  • Shopping Malls \(M\)

Parameters

  • The transportation cost from warehouse \(i \in F\) to shopping mall \(j \in M\) is \(a_{ij}\) .

  • The cost for constructing warehouse \(i \in F\) is \(c_i\) .

  • The demand for goods at shopping mall \(j \in M\) is \(d_j\) .

Decision Variables

  • \(x_i\) indicates whether a warehouse is constructed at alternative location \(i \in F\) . It can take a value of 0 or 1, where 0 means not to build and 1 means to build.

  • \(y_{ij}\) represents the quantity of goods transported from warehouse \(i \in F\) to shopping mall \(j \in M\) .

Objective Function

Minimize the combined cost of warehouse construction and goods transportation:

\(\text{minimize}~ \sum_{i\in F} c_{i}x_i + \sum_{i\in F,j \in M} a_{ij}y_{ij}\)

Constraints

\(\sum_{i\in F} y_{ij} = d_{j}, ~~ \forall j\in M\)

\(x_i d_j - \sum_{k\in M} y_{ik} = 0, ~~ \forall i \in F, j \in M\)


% There are two shopping malls with fixed locations at (0, 1.7) and (1.4, 2.9), requiring 100 and 200 units of goods weight.
market_location = [0.0, 1.7 ; 1.4, 2.9];
market_demand = [100, 200];
market_num = length(market_demand);

% Optional location and construction cost of warehouses
facility_location = [
    0, 1;
    0, 2;
    1, 0;
    1, 1;
    1, 2;
    2, 0;
    2, 1;
    2, 2
];
facility_num = length(facility_location);
transport_fee_per_m = 1.23;
n = market_num * facility_num;

% build expense
facility_expense = [3.0, 1.0, 1.5, 1.3, 1.8, 1.6, 1.1, 1.9];

% transport expense
transport_expense = [zeros(1, facility_num * market_num)];
for i = 1:facility_num
    for j = 1:market_num
        transport_expense((i - 1) * market_num + j) = norm(market_location(j, :) - facility_location(i, :)) * transport_fee_per_m;
    end
end

% build model
model.A = sparse(A);
model.obj = [facility_expense,zeros(1,market_num * facility_num);];
model.rhs = [market_demand, zeros(1, facility_num)];
model.lhsorsense = ['==',repmat('=',1, facility_num)];
model.modelsense = 'Min';
model.vtype = [repmat('B',1, facility_num),repmat('C',1, facility_num*2)];
res = mindopt(model);

% print result
if strcmp(res.Status,'OPTIMAL')
    fprintf('The total cost is %.3f\n', res.ObjVal);
    for v = 1:facility_num
        if res.X(v) == 1
            fprintf('The No. %d warehouse should be built.\n', v);
        end
    end
else
    fprintf('The optimize status is %s', res.Status);
end

WorkForce problem

In a week, the number of workers needed by the factory varies each day. It is currently known how much each worker earns per day and the dates on which they can attend work. The goal is to calculate how to schedule the workers in order to meet the factory’s operational requirements while minimizing wage costs.

Sets:

  • Days of the week \(D = 1, 2, 3, \ldots, 7\)

  • Number of workers needed by the factory \(N\)

  • Workers \(S\)

Parameters:

  • The number of workers needed by the factory on day \(i \in D\) is \(n_i\) .

  • The daily wage of worker \(j \in S\) is \(s_j\) .

  • A sequence indicating the available working times for workers, totaling \(T\) pairs of (worker, day) denoted as \((d_i, t_i)\) , where \(d_i \in S\) and \(t_i \in D\) , for \(i = 1, 2, 3, \ldots, T\) .

Objective Function:

Minimize the total wages paid: \(\text{minimize}~ \sum_{i=1}^{T} x_i s_{d_i}\)

Decision Variables:

  • \(x_i (i = 1, 2, 3, \ldots, T)\) indicates whether the worker in the available working time sequence shows up on that day. Its values must be 0 or 1, where 0 indicates the worker does not attend, and 1 indicates the worker does attend.

Constraints:

\(\sum_{d_i=r} x_i = n_{r}, ~~\forall r\in D\)


 % Number of required workers for each day
  day_name = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'};
  workers_per_day = [3, 1, 4, 2, 1, 3, 3];

 % Daily wage of each worker
  workers_name = {'Xiaoming', 'Huahua', 'HongHong', 'Dahua', 'Lihua', 'Niuniu', 'Gouzi'};
  workers_pay = [13, 10, 11, 8, 9, 14, 14];

% Available days for each worker
  day_id = [2; 3; 5; 7; 1; 2; 5; 6; 3; 4; 5; 7; 2; 3; 5; 6; 1; 2; 3; 4; 5; 7; 1; 2; 3; 6; 1; 2; 3; 5; 6; 7;];
  worker_id = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7];

  obj = zeros(1,length(day_id));
  for i = 1:length(obj)
      obj(i) = workers_pay(worker_id(i));
  end

  A = spconvert([day_id,(1:length(obj))',ones(1,length(obj))']);

 % build model
  model.A = A;
  model.obj = obj;
  model.modelsense = 'Min';
  model.vtype = repmat('B', 1, length(obj));
  model.lhsorsense = workers_per_day;
  model.rhs = workers_per_day;
  res = mindopt(model);

 % print result
  if strcmp(res.Status,'OPTIMAL')
      fprintf("The total cost is %f \n", res.ObjVal);
      for i = 1:length(res.X)
          if res.X(i) == 1
              name = workers_name{worker_id(i)};
              day = day_name{day_id(i)};
              fprintf('%s should work at %s\n', name, day);
          end
      end
  else
      fprintf('The optimize status is %s', res.Status);
  end