0

I am using R and have run a Gurobi optimization using model$vtype <- "B" where all elements of result$x are numeric and binary

For some reason one of my result variables that supposedly equals 1, does not equal 1 when I test for it. See the following from the console:

Browse[2]> result$x
  [1] 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
 [90] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[179] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
Browse[2]> result$x[15]
[1] 1
Browse[2]> result$x[15]==1
[1] FALSE

All the 8 other elements of result$x that equal 1 successfully equal TRUE when I test whether they equal 1, it's only the 15th element that is causing an issue.

Any ideas why this is occurring? I'm using a binary variable type, so I don't get how 1 != 1

Other possibly relevant info:

Browse[2]> is.numeric(result$x[15])
[1] TRUE
Browse[2]> typeof(result$x[15])
[1] "double"

Browse[2]> which(result$x==1)
[1]  10  33  48  81 127 133 151 198

Further info requested:

Browse[2]> typeof(result$x)
[1] "double"

When I cast it to integer, the 15th element for some reason becomes 0 instead of 1.. can anyone explain why this is happening and how I should properly adjust it? See below:

as.integer(result$x)
  [1] 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
 [90] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[179] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

Thanks

Tim M
  • 1
  • 1
  • What's type of `result$x`? Please use `integer`. – MKR Apr 07 '18 at 19:40
  • I edited above to show that casting to `as.integer(result$x)` converts the 15th element from `1` to `0` for some strange reason. – Tim M Apr 07 '18 at 20:18
  • @TimM Type `as.integer(0.99)` in your console, you will notice that the number becomes `0`. It is likely the 15th number is a number very close to 1. – www Apr 07 '18 at 20:42
  • @TimM Use `print(result$x[15], digits = 20)` to find out. – www Apr 07 '18 at 20:43

0 Answers0