0

So I'm trying to create this IP that has an optimal solution where all the variables are integers, and are all in multiples of a number, like 3. ( so the variables in the solution would have to be either 0,3,6,9,12,etc.)

I code in R, and it's pretty easy to set the constraint that the solution must be in integers (all.int = TRUE), but I am unsure of how to have it in a multiple of a number. What changes do I have to make within the Ax <= b formulation? Your help would be greatly appreciated! As of right now, I am fairly lost on how to actually do that

josliber
  • 43,891
  • 12
  • 98
  • 133
Timothy
  • 49
  • 1
  • 4

2 Answers2

2

To do this, you can define some integer variable x and then define y = 3*x. Now y is integer and a multiple of 3.

For instance, consider a trivial IP that finds the maximum multiple of 3 that is less than or equal to 10 (of course, the main motivation here is to embed this within a more complicated integer program). You could do this with:

library(lpSolve)
mod <- lp(direction = "max",
          objective.in = c(0, 1),  # (x, y)
          const.mat = rbind(c(3, -1),  # 3x - y = 0
                            c(0, 1)),  # y <= 10
          const.dir = c("=", "<="),
          const.rhs = c(0, 10),
          all.int = TRUE)
mod$solution[2]
# [1] 9
josliber
  • 43,891
  • 12
  • 98
  • 133
  • Wow thanks! That was really helpful! So I have to add a new variable [so adding a new column in the const.mat, in this case the 'y'] to lock in the solution 'x' to be a multiple? – Timothy Jul 30 '15 at 16:26
  • @Timothy we define `y = 3x` so `y` is the variable that is a multiple of 3. – josliber Jul 30 '15 at 16:30
-2

As I understand it, your criterion is that the mod 3 of the answer is 0. If so, what about mod(intResult,3) == 0?

Since I don't write in your language, the above may not be valid R, but I think you will get the idea, since the above is valid C, assuming that mod is the name of a function that returns intResult modulo 3.

David A. Gray
  • 1,039
  • 12
  • 19
  • The OP has specified that this is an integer programming problem, so all constraints must be linear in the variables. The modulo function is not a linear function of its input, so it cannot be used here. – josliber Jul 30 '15 at 16:16
  • I just read the OP's question again, and nowhere does it mention a requirement that the function must be linear. Since modulo returns an integer, it should be fair game. Let's see what the OP has to say for himself. – David A. Gray Jul 31 '15 at 22:07
  • The title begins with "Integer programs" and the question begins with "So I'm trying to create this IP" and the question is tagged with "integer-programming", so it's pretty clear that this is an integer programming question. As you can read from [the wiki article](https://en.wikipedia.org/wiki/Integer_programming), all constraints in integer programs must be linear in the decision variables. – josliber Jul 31 '15 at 23:57
  • Mod is an integer operation. Get a life. – David A. Gray Aug 01 '15 at 01:48
  • 2
    I didn't say "integer operation", I said "linear function of the decision variables." Integer programs can include constraints like "3x <= 1" or "x-2y >= 3" because functions "3x" and "x-2y" are linear functions of the variables; e.g. as x takes values 1, 2, 3, 4, 5, function "3x" takes values 3, 6, 9, 12, 15 (linear change). "x mod 3" does not have this property, taking values 1, 2, 0, 1, 2 (non-linear) for inputs 1, 2, 3, 4, 5. Linear constraints are a basic property of IPs. Since it seems you are unfamiliar with IPs, I would encourage you to take a less combative tone in your comments. – josliber Aug 01 '15 at 14:58