Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9013709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:20:12+00:00 2026-06-16T03:20:12+00:00

I have been trying to solve a constrained optimization problem in R using constrOptim()

  • 0

I have been trying to solve a constrained optimization problem in R using constrOptim() (my first time) but am struggling to set up the constraints for my problem.

The problem is pretty straight forward and i can set up the function ok but am a bit at a loss about passing the constraints in.

e.g. problem i’ve defined is (am going to start with N fixed at 1000 say so i just want to solve for X ultimately i’d like to choose both N and X that max profit):

so i can set up the function as:

fun <- function(x, N, a, c, s) {   ## a profit function
    x1 <- x[1]
    x2 <- x[2]
    x3 <- x[3]    
    a1 <- a[1]
    a2 <- a[2]
    a3 <- a[3]
    c1 <- c[1]
    c2 <- c[2]
    c3 <- c[3]
    s1 <- s[1]
    s2 <- s[2]
    s3 <- s[3]  
    ((N*x1*a1*s1)-(N*x1*c1))+((N*x2*a2*s2)-(N*x2*c2))+((N*x3*a3*s3)-(N*x3*c3))
}

The constraints i need to implement are that:

x1>=0.03
x1<=0.7
x2>=0.03
x2<=0.7
x3>=0.03
x2<=0.7
x1+x2+x3=1

The X here represents buckets into which i need to optimally allocate N, so x1=pecent of N to place in bucket 1 etc. with each bucket having at least 3% but no more than 70%.

Any help much appreciated…

e.g. here is an example i used to test the function does what i want:

    fun <- function(x, N, a, c, s) {   ## profit function
        x1 <- x[1]
        x2 <- x[2]
        x3 <- x[3]    
        a1 <- a[1]
        a2 <- a[2]
        a3 <- a[3]
        c1 <- c[1]
        c2 <- c[2]
        c3 <- c[3]
        s1 <- s[1]
        s2 <- s[2]
        s3 <- s[3]  
        ((N*x1*a1*s1)-(N*x1*c1))+((N*x2*a2*s2)-(N*x2*c2))+((N*x3*a3*s3)-(N*x3*c3))
    };

    x <-matrix(c(0.5,0.25,0.25));

    a <-matrix(c(0.2,0.15,0.1));

    s <-matrix(c(100,75,50));

    c <-matrix(c(10,8,7));

    N <- 1000;

fun(x,N,a,c,s);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:20:14+00:00Added an answer on June 16, 2026 at 3:20 am

    You can use The lpSolveAPI package.

    ## problem constants
    a <- c(0.2, 0.15, 0.1)
    s <- c(100, 75, 50)
    c <- c(10, 8, 7)
    N <- 1000
    
    
    ## Problem formulation
    # x1          >= 0.03
    # x1          <= 0.7
    #     x2      >= 0.03
    #     x2      <= 0.7
    #          x3 >= 0.03
    # x1 +x2 + x3  = 1
    #N*(c1- a1*s1)* x1 + (a2*s2 - c2)* x2 + (a3*s3-  c3)* x3
    
    library(lpSolveAPI)
    my.lp <- make.lp(6, 3)
    

    The best way to build a model in lp solve is columnwise;

    #constraints by columns
    set.column(my.lp, 1, c(1, 1, 0, 0, 1, 1))
    set.column(my.lp, 2, c(0, 0, 1, 1, 0, 1))
    set.column(my.lp, 3, c(0, 0, 0, 0, 1, 1))
    #the objective function ,since we need to max I set negtive max(f) = -min(f)
    set.objfn (my.lp, -N*c(c[1]- a[1]*s[1], a[2]*s[2] - c[2],a[3]*s[3]-  c[3]))
    set.rhs(my.lp, c(rep(c(0.03,0.7),2),0.03,1))
    #constraint types 
    set.constr.type(my.lp, c(rep(c(">=","<="), 2),">=","="))
    

    take a look at my model

    my.lp
    Model name: 
    
     Model name: 
                 C1     C2     C3          
    Minimize  10000  -3250   2000          
    R1            1      0      0  >=  0.03
    R2            1      0      0  <=   0.7
    R3            0      1      0  >=  0.03
    R4            0      1      0  <=   0.7
    R5            1      0      1  >=  0.03
    R6            1      1      1   =     1
    Kind        Std    Std    Std          
    Type       Real   Real   Real          
    Upper       Inf    Inf    Inf          
    Lower         0      0      0      
     solve(my.lp)
    
    [1] 0    ## sucess :)
    
    get.objective(my.lp)
    [1] -1435
    get.constraints(my.lp)
    [1] 0.70 0.70 0.03 0.03 0.97 1.00
    ## the decisions variables
    get.variables(my.lp)
    [1] 0.03 0.70 0.27
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to solve this problem for a while, but couldn't with
I have been trying to solve this style problem for the whole day, but
I have been trying to solve a problem involving thread communication using wait() and
For some time I have been trying to solve fairly common problem consisting of
I have little problem that I have been trying to solve for some time.
I have been trying to solve this problem all day but haven't got any
I have been trying to find ways to solve the problem. Firebug said syntax
I have an interesting problem here I've been trying to solve for the last
I have been trying to solve some crackmes (from http://crackmes.de ) using WinDbg. Most
I have been trying to solve this problem for three days now, it's really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.