Skip to content

Mixed integer programming

5 messages · Hannu Kahra, Enrico Schumann, Eric Berger

#
I have tried to replicate Example 5.1 in Luenberger: Investment Science.

Here is the model
model <- MIPModel() %>%
  add_variable(x[i], i = 1:7, type = "binary") %>%
  set_objective(200*x[1]+30*x[2]+200*x[3]+60*x[4]+50*x[5]+100*x[6]+50*x[7])
%>%

add_constraint(100*x[1]+20*x[2]+150*x[3]+50*x[4]+50*x[5]+150*x[6]+150*x[7]
<= 500)
solve_model(with_ROI("glpk",verbose = TRUE))

I get the following error

Error in UseMethod("solve_model") :
  no applicable method for 'solve_model' applied to an object of class
"function"

 modelMixed integer linear optimization problem
Variables:
  Continuous: 0
  Integer: 0
  Binary: 7
Model sense: maximize
Constraints: 1

What is wrong? Thank you in advance.

-Hannu
#
I forgot to mention that I am using the ompr package. An example is given
here

https://blog.revolutionanalytics.com/2016/12/mixed-integer-programming-in-r-with-the-ompr-package.html

-Hannu
On Tue, Feb 19, 2019 at 2:10 PM Hannu Kahra <hkahra at gmail.com> wrote:

            

  
  
#
Hannu> I have tried to replicate Example 5.1 in Luenberger: Investment Science.
    Hannu> Here is the model
    Hannu> model <- MIPModel() %>%
    Hannu>   add_variable(x[i], i = 1:7, type = "binary") %>%
    Hannu>   set_objective(200*x[1]+30*x[2]+200*x[3]+60*x[4]+50*x[5]+100*x[6]+50*x[7])
    Hannu> %>%

    Hannu> add_constraint(100*x[1]+20*x[2]+150*x[3]+50*x[4]+50*x[5]+150*x[6]+150*x[7]
    Hannu> <= 500)
    Hannu> solve_model(with_ROI("glpk",verbose = TRUE))

    Hannu> I get the following error

    Hannu> Error in UseMethod("solve_model") :
    Hannu>   no applicable method for 'solve_model' applied to an object of class
    Hannu> "function"

    Hannu>  modelMixed integer linear optimization problem
    Hannu> Variables:
    Hannu>   Continuous: 0
    Hannu>   Integer: 0
    Hannu>   Binary: 7
    Hannu> Model sense: maximize
    Hannu> Constraints: 1

    Hannu> What is wrong? Thank you in advance.

    Hannu> -Hannu

If you want people to help you, you need to provide a
reproducible example. Also, please do not post in HTML,
as code examples get scrambled and become unreadable.

(If I had to venture a guess, I'd think you've
 forgotten one of those `%>%` before 'solve_model'.)
#
Hi Hannu,
I figured out the problem. The following code works for me. Note that I
generally find it helpful to write function calls with the name of the
package they come from, as in ompr::MIPModel() rather than just MIPModel().
This is partly style and partly to avoid issues when functions get masked
by other functions of the same name.

Here is my code:

library(magrittr)
library(ompr)
library(ROI)
library(ROI.plugin.glpk)
library(ompr.roi)

mymodel <- ompr::MIPModel() %>%
  add_variable(x[i], i = 1:7, type = "binary") %>%
  set_objective(200*x[1]+30*x[2]+200*x[3]+60*x[4]+50*x[5]+100*x[6]+50*x[7])
%>%

add_constraint(100*x[1]+20*x[2]+150*x[3]+50*x[4]+50*x[5]+150*x[6]+150*x[7]
<= 500)

z <- ompr::solve_model(mymodel, ompr.roi::with_ROI(solver="glpk"))

z
# Status: optimal
# Objective value: 610

summary(z)
#                                   Length  Class              Mode
#model                              3      optimization_model list
#objective_value               1      -none-             numeric
#status                              1      -none-             character
#solution                           7      -none-             numeric
#solution_column_duals   1      -none-             function
#solution_row_duals         1      -none-             function

z$solution

# x[1] x[2] x[3] x[4] x[5] x[6] x[7]
#   1    0    1    1    1    1    0

HTH,
Eric


On Tue, Feb 19, 2019 at 3:46 PM Enrico Schumann <es at enricoschumann.net>
wrote:

  
  
#
Hi Eric,

thank you very much.

Best regards,
Hannu
On Tue, Feb 19, 2019 at 4:02 PM Eric Berger <ericjberger at gmail.com> wrote: