MATLAB has a new way to express optimization problems. Until now they used a matrix-based API (this is now called: "Solver Based Optimization"). In 2017b they added "Problem Based Optimization" which is kind of a algebraic modeling language (like AMPL or GAMS) inside Matlab. Let's revisit a simple problem related to warehousing [3].
We use the following indices:
This implementation uses the old matrix based API. The code is somewhat removed from the original problem as we need to use quite some non-trivial code to deal with the details in populating the solver matrices. This is still a very simple model: for more complex models this amount of code and its complexity will only increase. See [1] for more details.
This implementation is using the new Matlab Problem Based Optimization language features [2]. In my opinion this is a big improvement. When reading this code we at least recognize the original problem. In addition we see the code is more compact.
For completeness, here is the GAMS representation [3]:
It is instructive to see the differences in approaches. Here are some highlights:
There is a notable difference in number of different language constructs we use. In Matlab we use:
Mathematical Model
We use the following indices:
- \(p\): products
- \(f\): factories
- \(w\): warehouses
- \(s\): stores
- \(x_{p,f,w} \ge 0\): shipments of product \(p\) from factory \(f\) to warehouse \(w\),
- \(y_{s,w} \in \{0,1\}\): links each store \(s\) to a single warehouse \(w\).
- \(pcost_{f,p}\): unit production cost
- \(tcost_{p}\): unit transportation cost
- \(dist_{f,w}, dist_{s,w}\): distances
- \(pcap_{f,p}\): factory production capacities
- \(wcap_{w}\): warehouse capacity
- \(d_{s,p}\): demand for product \(p\) at store \(s\)
- \(turn_{p}\): product turnover rate
| MIP Model |
|---|
| \[\large{\begin{align} \min&\sum_{p,f,w} (pcost_{f,p}+tcost_p\cdot dist_{f,w})\cdot x_{p,f,w}+\sum_{s,w,p} d_{s,p}\cdot tcost_p \cdot dist_{s,w} \cdot y_{s,w}\\ &\sum_w x_{p,f,w} \le pcap_{f,p} \>\>\forall f,p \>\> \text{(production capacity)}\\ &\sum_f x_{p,f,w} = \sum_s d_{s,p}\cdot y_{s,w} \>\>\forall p,w \>\>\text{(demand)}\\ &\sum_{p,s} \frac{d_{s,p}}{turn_{p}} y_{s,w} \le wcap_{w}\>\>\forall w \>\>\text{(warehouse capacity)}\\ &\sum_w y_{s,w} = 1\>\>\forall s \>\>\text{(one warehouse for a store)}\\ &x_{p,f,w} \ge 0 \\ &y_{s,w} \in \{0,1\} \end{align}}\] |
Matlab: Solver Based Implementation
1 | %----------------------------------------------------- |
Matlab: Problem Based Implementation
This implementation is using the new Matlab Problem Based Optimization language features [2]. In my opinion this is a big improvement. When reading this code we at least recognize the original problem. In addition we see the code is more compact.
1 | %----------------------------------------------------- |
GAMS Implementation
For completeness, here is the GAMS representation [3]:
1 | sets |
It is instructive to see the differences in approaches. Here are some highlights:
| Expression | Matlab (Model Based) | GAMS |
|---|---|---|
| \[\sum_{p,f,w} pcost_{f,p} \cdot x_{p,f,w}\] | sum(sum(sum(x,3).*(pcost'),2),1) | sum((p,f,w), pcost(f,p)*x(p,f,w)) |
| \[\sum_{p,f,w} tcost_p \cdot distfw_{f,w} \cdot x_{p,f,w}\] | objfun2 = 0; | sum((p,f,w), tcost(p)*distfw(f,w)*x(p,f,w)) |
| \[\sum_w x_{p,f,w} \le pcap_{f,p}\] | sum(x,3) <= pcap' | sum(w,x(p,f,w)) =l= pcap(f,p) |
| \[\sum_f x_{p,f,w} = \sum_s d_{s,p}\cdot y_{s,w}\] | squeeze(sum(x,2)) == d'*y | sum(f,x(p,f,w)) =e= sum(s, d(s,p)*y(s,w)) |
| \[\sum_{p,s} \frac{d_{s,p}}{turn_{p}} y_{s,w} \le wcap_{w}\] | sum(diag(1./turn)*(d'*y),1) <= wcap' | sum((p,s),(d(s,p)/turn(p))*y(s,w)) =l= wcap(w) |
| \[\sum_w y_{s,w} = 1\] | sum(y,2) == ones(S,1) | sum(w,y(s,w)) =e= 1 |
There is a notable difference in number of different language constructs we use. In Matlab we use:
- Different versions of sum
- A loop
- Diag, transpose ('), element wise division (./)
- Squeeze
In GAMS we basically only use sum.
References
- Factory, Warehouse, Sales Allocation Model: Solver-Based, http://www.mathworks.com/help/optim/ug/factory-warehouse-sales-allocation-model.html
- Factory, Warehouse, Sales Allocation Model: Model-Based, http://www.mathworks.com/help/optim/examples/factory-warehouse-sales-allocation-model.html
- Matlab vs GAMS: Integer Programming, http://yetanothermathprogrammingconsultant.blogspot.com/2016/10/matlab-vs-gams-integer-programming.html