How to pass nonlinear objective functions into ROI package in R?

945 views Asked by At

I'm trying to tackle a nonlinear optimization problem where the objective functions are non-linear and constraints are linear. I read a bit on the ROI package in R and I decided to use the same. However, I am facing a problem while solving the optimization problem.

I am essentially trying to minimize the area under a supply-demand curve. The equation for the supply and demand curves are defined in the code:

Objective function: minimize (Integral of supply curve + integral of demand curve),
subject to constraints q greater than or equal to 34155 (stored in a variable called ICR),
q greater than or equal to 0
and q less than or equal to 40000.

I have tried to run this through the ROI package in RStudio and I keep getting an error telling me that there is no solver to be found.

    library(tidyverse)
    library(ROI)    
    library(rSymPy)    
    library(mosaicCalc)

    # Initializing parameters for demand curve
    A1 <- 6190735.2198302800    
    B1 <- -1222739.9618776600    
    C1 <- 103427.9556133250    
    D1 <- -4857.0627045073    
    E1 <- 136.7660814828    


    # Initializing parameters for Supply Curve
    S1 <- -1.152    
    S2 <- 0.002    
    S3 <- a-9.037e-09    
    S4 <- 2.082e-13    
    S5 <- -1.64e-18    

    ICR <- 34155    

    demand_curve_integral <- antiD(A1 + B1*q + C1*(q^2)+ D1*(q^3) + E1*(q^4) ~q)    
    supply_curve_integral <- antiD(S1 + S2*(q) + S3*(q^2) + S4*(q^3) + S5*(q^4)~q)    

    # Setting up the objective function
    obj_func <- function(q){   (18.081*demand_curve_integral(q))+supply_curve_integral(q)}    

    # Setting up the optimization Problem

   lp <- OP(objective = F_objective(obj_func, n=1L),     
     constraints=L_constraint(L=matrix(c(1, 1, 1), nrow=3),     
                              dir=c(">=", ">=", "<="),     
                              rhs=c(ICR, 0, 40000, 1))),     
     maximum = FALSE)    

    sol <- ROI_solve(lp)

This is the error that I keep getting in RStudio:

    Error in ROI_solve(lp) : no solver found for this signature:    
        objective: F    
        constraints: L    
        bounds: V    
        cones: X    
        maximum: FALSE    
        C: TRUE    
        I: FALSE    
        B: FALSE   

What should I do to rectify this error?

2

There are 2 answers

0
SteveM On

I haven't run NLP using ROI. But you have to install an ROI solver plug-in and then load the library in your code. The current solver plug-ins are:

library(ROI.plugin.glpk)
library(ROI.plugin.lpsolve)
library(ROI.plugin.neos)
library(ROI.plugin.symphony)
library(ROI.plugin.cplex)

Neos provides access to NLP solvers but I don't know how to pass solver parameters via an ROI plug-in function call.

https://neos-guide.org/content/nonlinear-programming

0
Florian On

In general you could use ROI.plugin.alabama or ROI.plugin.nloptr for this optimization problem.

But I looked at the problem and this raised several questions.

  1. a is not defined in the code.
  2. You state that q has length 1 and add 3 linear constraints the constraints say
    q >= 34155, q >= 0, q <= 40000 or q <= 1
    I am not entirely sure since the length of rhs is 4 but L and dir suggest there are only 3 linear constraints.

How should the constraint look like?

34155 <= q <= 40000?
Then you could specify the constraint as bounds and use ROI.plugin.optimx or since you have a one dimensional optimization problem just use optimize from the stats package https://stat.ethz.ch/R-manual/R-devel/library/stats/html/optimize.html.