Jorge I Velez
2012-Nov-28 03:48 UTC
[R] Help setting optimization problem to include more constraints
Dear R-helpers,
I am struggling with an optimization problem at the moment and decided to
write the list looking for some help. I will use a very small example to
explain what I would like to. Thanks in advance for your help.
We would like to distribute resources from 4 warehouses to 3 destinations.
The costs associated are as follows:
Destination>From 1 2 3 Total
1 1 3 4 300
2 3 2 3 200
3 2 2 1 200
4 1 3 1 200
Total 350 250 300 900
Thus, shipping one unit from warehouse 1 to destination point 3 costs $4.
Let X_{ij} be the number of units to be shipped from warehouse i to
destinaton j (i = 1, 2, 3, 4; j= 1, 2, 3). If c_{ij} is the cost of
shipping one unit from warehouse i to destination j, the formulation in R
would be as follows:
require(lpSolve)
f <- matrix(c(1, 3, 4, 3, 2, 3, 2, 2, 1, 1, 3, 1), ncol = 3, byrow = TRUE)
row.rhs <- c(300, 200, 200, 200)
col.rhs <- c(350, 250, 300)
row.signs <- rep("==", length(row.rhs))
col.signs <- rep("==", length(col.rhs))
lp.transport(f, "min", row.signs, row.rhs, col.signs, col.rhs)
D <- lp.transport(f, "min", row.signs, row.rhs, col.signs, col.rhs)
D$solution
# [,1] [,2] [,3]
#[1,] 300 0 0
#[2,] 0 200 0
#[3,] 0 50 150
#[4,] 50 0 150
Thus, we will ship 300 units from point 1 to destination 1; 200 from point
2 to destination 2 and so on, and the cost of this distribution plan is
$1150. However, I would like to add the following two constraints:
# 1. weighted sums by column
# w is a vector of known constants, i.e., w = c(1.2, .9, .7, 2.3)
# r is also known, i.e., r = 4
w1*x11 + w2*x21 + w3*x21 + w4*x41 == r # col 1
w1*x12 + w2*x22 + w3*x32 + w4*x42 == r # col 2
w1*x13 + w2*x23 + w3*x33 + w4*x43 == r # col 3
# 2. By column, the number of X's greater than zero should be two or
greater. In this small example, this condition is satisfied, but I would
like to make sure that it is also satisfied in my problem.
# 3. Using lp.transport(), the function to be minimized is linear, i.e., Z
= c11*x11 + c12*x12 + ... + c42*x42 + c43*x43. However, in my case, I am
interested in minimizing the standard deviation of g = c(l1, l2, l3), with
l1 = w1*x11 + w2*x21 + w3*x21 + w4*x41
l2 = w1*x12 + w2*x22 + w3*x32 + w4*x42
l3 = w1*x13 + w2*x23 + w3*x33 + w4*x43
and w as previously described.
I can easily include constraint #1, but not #2 neither changing the
function to be optimized, and would very much appreciate any insights on
how to do it. Thank you once again.
Regards,
Jorge.-
[[alternative HTML version deleted]]
Berend Hasselman
2012-Nov-28 10:16 UTC
[R] Help setting optimization problem to include more constraints
On 28-11-2012, at 04:48, Jorge I Velez wrote:> Dear R-helpers, > > I am struggling with an optimization problem at the moment and decided to > write the list looking for some help. I will use a very small example to > explain what I would like to. Thanks in advance for your help. > > We would like to distribute resources from 4 warehouses to 3 destinations. > The costs associated are as follows: > > Destination > From 1 2 3 Total > 1 1 3 4 300 > 2 3 2 3 200 > 3 2 2 1 200 > 4 1 3 1 200 > Total 350 250 300 900 > > Thus, shipping one unit from warehouse 1 to destination point 3 costs $4. > > Let X_{ij} be the number of units to be shipped from warehouse i to > destinaton j (i = 1, 2, 3, 4; j= 1, 2, 3). If c_{ij} is the cost of > shipping one unit from warehouse i to destination j, the formulation in R > would be as follows: > > require(lpSolve) > f <- matrix(c(1, 3, 4, 3, 2, 3, 2, 2, 1, 1, 3, 1), ncol = 3, byrow = TRUE) > row.rhs <- c(300, 200, 200, 200) > col.rhs <- c(350, 250, 300) > row.signs <- rep("==", length(row.rhs)) > col.signs <- rep("==", length(col.rhs)) > lp.transport(f, "min", row.signs, row.rhs, col.signs, col.rhs) > D <- lp.transport(f, "min", row.signs, row.rhs, col.signs, col.rhs) > D$solution > # [,1] [,2] [,3] > #[1,] 300 0 0 > #[2,] 0 200 0 > #[3,] 0 50 150 > #[4,] 50 0 150 > > Thus, we will ship 300 units from point 1 to destination 1; 200 from point > 2 to destination 2 and so on, and the cost of this distribution plan is > $1150. However, I would like to add the following two constraints: > > # 1. weighted sums by column > # w is a vector of known constants, i.e., w = c(1.2, .9, .7, 2.3) > # r is also known, i.e., r = 4 > w1*x11 + w2*x21 + w3*x21 + w4*x41 == r # col 1Shouldn't w3*x21 be w3 * x31?> w1*x12 + w2*x22 + w3*x32 + w4*x42 == r # col 2 > w1*x13 + w2*x23 + w3*x33 + w4*x43 == r # col 3 > > # 2. By column, the number of X's greater than zero should be two or > greater. In this small example, this condition is satisfied, but I would > like to make sure that it is also satisfied in my problem. > > # 3. Using lp.transport(), the function to be minimized is linear, i.e., Z > = c11*x11 + c12*x12 + ... + c42*x42 + c43*x43. However, in my case, I am > interested in minimizing the standard deviation of g = c(l1, l2, l3), with > > l1 = w1*x11 + w2*x21 + w3*x21 + w4*x41 > l2 = w1*x12 + w2*x22 + w3*x32 + w4*x42 > l3 = w1*x13 + w2*x23 + w3*x33 + w4*x43 > > and w as previously described.From constraint #1 and the definition3 in #3 one may conclude: l1==r l2==r l3==r implying that the standard deviation of g is 0. Always. Take your pick for x values. I don't get it. Berend