Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with computed indexes of variables in set_bounds #87

Closed
dirkschumacher opened this issue Oct 14, 2016 · 10 comments
Closed

Bug with computed indexes of variables in set_bounds #87

dirkschumacher opened this issue Oct 14, 2016 · 10 comments
Assignees

Comments

@dirkschumacher
Copy link
Owner

set_bounds(u[e(k)], ub = 0, k = 1:m)

@dirkschumacher dirkschumacher added this to the CRAN 1.0 milestone Oct 14, 2016
@dirkschumacher dirkschumacher self-assigned this Oct 14, 2016
@dirkschumacher
Copy link
Owner Author

Hello past me, is this really a bug? Isn't that equal to set_bounds(u[k], ub = 0, k = e(1:m))?

@dirkschumacher
Copy link
Owner Author

Though this works with add_constraint. I guess that could be harmonized

@dirkschumacher dirkschumacher removed this from the CRAN 1.0 milestone Oct 14, 2016
@dirkschumacher
Copy link
Owner Author

  a <- identity
  m <- MILPModel() %>%
    add_variable(x[i], i = 1:3) %>%
    set_bounds(x[a(i)], i = 1:3, lb = 1)
  expect_equal(c(1, 1, 1), m@variables[[1]]@lb)

@rickyars
Copy link

rickyars commented Feb 7, 2017

how would one use this technique to set bounds if the computed indexes belong to a matrix?

@dirkschumacher
Copy link
Owner Author

Can you give me an example?

@rickyars
Copy link

rickyars commented Feb 7, 2017

Let's use your student assignment example and extend it. I want to create a matrix that says "student i cannot take course j". So for these (i,j) pairs, I want to set the upper-bound to zero. So how do I do this? I think this can be done by creating a matrix and adding a constraint.

Build the matrix:

blocked <- data.frame(
  crossing(i = 1:n, j = 1:m) %>%
    mutate(blocked = 1) %>%
    spread(j, blocked) %>%
    select(-i)
)
# student 10 cannot take course 4
blocked[10, 4] <- 0

# student 25 cannot take course 1 or 3
blocked[25, 1] <- 0
blocked[25, 3] <- 0

Build the model with additional constraint:

model <- MIPModel() %>%
  
  # 1 iff student i is assigned to course m
  add_variable(x[i, j], i = 1:n, j = 1:m, type = "binary") %>%
  
  # maximize the preferences
  set_objective(sum_expr(weight(i, j) * x[i, j], i = 1:n, j = 1:m)) %>%
  
  # we cannot exceed the capacity of a course
  add_constraint(sum_expr(x[i, j], i = 1:n) <= capacity[j], j = 1:m) %>% 
  
  # each student needs to be assigned to one course
  add_constraint(sum_expr(x[i, j], j = 1:m) == 1, i = 1:n) %>%

  # NEW: some students are blocked from certain courses
  add_constraint(x[i, j] <= blocked[i, j], i = 1:n, j = 1)

But it might be nice (and maybe in some instances preferable) to do this by setting the bounds on the decision variables. I'm not sure what this would look like though. E.g.,:

  # NEW: some students are blocked from certain courses
  set_bounds(x[i, j], ub = blocked[i, j])

Or maybe (with the 0/1 flipped in blocked[i,j]):

  # some students are blocked from certain courses
  set_bounds(x[blocked[i, j]], ub = 0, i = 1:n, j = 1:m)

@ghost
Copy link

ghost commented Mar 11, 2017

Hey great work on this package! Looking forward to its release on CRAN.

Here's a related bug (I believe so) that I found today -

MIPModel() %>%
  add_variable(x[a, b], a=1:2, b=1:5, type = "integer", lb = 0, ub = 5) %>%
  set_objective(sum_expr(x[a, b], a=1:2, b=1:5), "max") %>%
  # add_constraint(x[i, i] == 0, i = 1:2) %>%
  set_bounds(x[i, i], i=1:2, ub = 0) %>%
  solve_model(with_ROI(solver = "glpk")) %>%
  get_solution(x[a, b])

   variable a b value
1         x 1 1     0
2         x 1 2     5
3         x 1 3     5
4         x 1 4     0
5         x 1 5     5
6         x 2 1     5
7         x 2 2     5
8         x 2 3     5
9         x 2 4     5
10        x 2 5     5

The expected output is x[1, 1] = 0 and x[2, 2] = 0. However I get x[1, 1] = 0 and x[1, 4] = 0.

I can get the correct results with add_constraint i.e. below code works fine

MIPModel() %>%
  add_variable(x[a, b], a=1:2, b=1:5, type = "integer", lb = 0, ub = 5) %>%
  set_objective(sum_expr(x[a, b], a=1:2, b=1:5), "max") %>%
  add_constraint(x[i, i] == 0, i = 1:2) %>%
  # set_bounds(x[i, i], i=1:2, ub = 0) %>%
  solve_model(with_ROI(solver = "glpk")) %>%
  get_solution(x[a, b])

   variable a b value
1         x 1 1     0
2         x 1 2     5
3         x 1 3     5
4         x 1 4     5
5         x 1 5     5
6         x 2 1     5
7         x 2 2     0
8         x 2 3     5
9         x 2 4     5
10        x 2 5     5

Any thoughts?

@dirkschumacher
Copy link
Owner Author

Thanks @shrinidhee indeed a bug. Will post a fix soon

@ShuchitaShukla
Copy link

hi @rickyars @dirkschumacher @shrinidhee

Does the below workf for you?

we cannot exceed the capacity of a course

add_constraint(sum_expr(x[i, j], i = 1:n) <= capacity[j], j = 1:m) %>%
Im not able to add the below line in my code:
model <- MIPModel() %>%
add_variable(Qtysold[i],i=1:n,type = "integer" ,lb=0 ) %>%
add_variable(Total_mat[i,j],i=1:n,j=1:68, type = "continuous", lb = 0) %>%
set_objective(sum_expr(Qtysold[i]*Revenueperunit[i],i=1:n), "max")%>%
add_constraint(Qtysold[i] <= qty_req[i], i = 1:n)%>%
add_constraint(Qtysold[i]*Raw_mat[i,j]==Total_mat[i,j],i=1:n,j=1:68)%>%
add_constraint(sum_expr(Total_mat[i,j],i=1:n)<=Inv_Stock[j],j=1:68)

The last line gives an error:Error in check_for_unknown_vars_impl(model, the_ast) :
The expression contains a variable that is not part of the model.

@dirkschumacher
Copy link
Owner Author

This seems to be fixed now with #130.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants