Comments (10)
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.
Though this works with add_constraint
. I guess that could be harmonized
from ompr.
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.
how would one use this technique to set bounds if the computed indexes belong to a matrix?
from ompr.
Can you give me an example?
from ompr.
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.
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.
Thanks @shrinidhee indeed a bug. Will post a fix soon
from ompr.
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.
This seems to be fixed now with #130.
from ompr.
Related Issues (20)
- NEOS Server & Async Mode: Error: object of type 'closure' is not subsettable
- Gurobi Solver- Recent Experience? HOT 1
- Binary variable <= Binary matrix: Error in constraint$lhs - constraint$rhs : non-numeric argument to binary operator HOT 4
- Solver log not showing up anymore
- Semi continuous variable HOT 7
- Feature Request Integrated with RAMPL HOT 1
- Feature Request: GAMS presolver for ompr HOT 1
- Error in add_variable, object not found HOT 1
- Object not found HOT 3
- Release ompr 1.0.3
- Getting error "*.AbstractLinearFunction" when multiplying in objective function HOT 1
- the cannery problem - vignette enhancement
- Again, my curious interest is in pre-processing sparse data objects outside of MIPModel if possible. I created a simple sparse supply chain dataframe as an example:
- Feature Request: adding "macro" functionality to ompr HOT 1
- Sparse Matrix Constraints with multi-index
- Limiting Variable Definition By Ordered Pair HOT 9
- How write a constraint once there is a variable used as the index? HOT 2
- interactions: "Quadratic expression are not supported" HOT 3
- Request Feature: Add Vectorized semantics revisited of MILPModel to MIPModel
- add dependency matrix to constrains HOT 19
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ompr.