[R-SIG-Finance] Mixed integer programming

Eric Berger er|cjberger @end|ng |rom gm@||@com
Tue Feb 19 15:01:44 CET 2019


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 using enricoschumann.net>
wrote:

> >>>>> "Hannu" == Hannu Kahra <hkahra using gmail.com> writes:
>
>     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'.)
>
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enricoschumann.net
>
> _______________________________________________
> R-SIG-Finance using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list