Code Monkey home page Code Monkey logo

Comments (10)

dirkschumacher avatar dirkschumacher commented on June 19, 2024

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

from ompr.

dirkschumacher avatar dirkschumacher commented on June 19, 2024

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

from ompr.

dirkschumacher avatar dirkschumacher commented on June 19, 2024
  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)

from ompr.

rickyars avatar rickyars commented on June 19, 2024

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

from ompr.

dirkschumacher avatar dirkschumacher commented on June 19, 2024

Can you give me an example?

from ompr.

rickyars avatar rickyars commented on June 19, 2024

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)

from ompr.

 avatar commented on June 19, 2024

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?

from ompr.

dirkschumacher avatar dirkschumacher commented on June 19, 2024

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

from ompr.

ShuchitaShukla avatar ShuchitaShukla commented on June 19, 2024

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.

from ompr.

dirkschumacher avatar dirkschumacher commented on June 19, 2024

This seems to be fixed now with #130.

from ompr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.