R Optimization, setting constraint - r

I have been solving the optimization problem in R with 2 variables using lpSolveAPI. "
one of the constraint is standarddeviation(ax1,bx2)=1.24.
I am unable to input this constraint, it throws an error message
"The length of xt must be equal to the number of decision variables in
lprec"
Could you please suggest the suitable package in R where I would be able to input the above constraint?

Hi I am able to solve the constraint with solnp algorithm in Rsolnp package . I tried other algorithm in nloptr but solnp throws better solution

Related

Error performing sensitivity analysis on binary integer programming model using LpSolveAPI package in R

I have a basic binary linear programming model with twenty-five constraints and 416 variables. I create the model and solve for an optimal solution, using the LpSolveAPI package, with no problem. However, when I try to run the package's sensitivity analysis functions I receive this error:
'''Error in get.sensitivity.obj(ff.lp) : OPTIMAL solution'''
If the function is run before the model is solved then this error is given:
'''Error in get.sensitivity.obj(ff.lp) : Model has not been optimized'''
There is no reason the solution being optimal should prevent sensitivity analysis. I don't understand what this error is telling me and I can't find the source code of the function to see what conditions are producing this error message.
Can anyone explain why I am getting this error and how to fix it, or otherwise perform sensitivity analysis on a model built with LpSolveAPI package.

What is the best package/algorithm to solve a large optimization problem in R?

The problem has an exponential objective and exponential equality constraint function.Both my objective and constraint function are differentiable.
I would want to solve it using lagrange method and also want the function to output lagrange multiplier. I am currently exploring aug_lag in nloptr and solnp in rsolnp.
First question:
Would you suggest these packages or i should explore any other package?
Second question:
I know that rsolnp and nloptr performs local optimisation majorly but if i want to do global optimisation on a similar problem, can you suggest any package / algorithm?

Specify solver in CVXR

I am new to the package CVXR. I am using it to do the convex optimization within each iteration of EM algorithms. Everything is fine at first but after 38 iterations, I have an error:
Error in valuesById(object, results_dict, sym_data, solver) :
Solver failed. Try another.
I am not sure why the solver works fine at first but then fails to work later. I looked up the manual about how to change the solver but could not find the answer. I am also curious about whether we can specify learning step size in CVXR. Really appreciate any help
The list of installed solvers in CVXR you can get with
installed_solvers()
In my case that is:
# "ECOS" "ECOS_BB" "SCS"
You can change the one that is used just using argument solver, e.g. to change from the default ECOS to SCS:
result <- solve(prob, solver="SCS")
I think the developers are planning to support other solvers in the future, e.g. gurobi...

Configuring SOCP solver in 'R'

I am trying to use the Rsocp package in R to solve a linear optimization problem with quadratic constraints. Much like in R - fPortfolio - Error in eqsumW[2, -1] : subscript out of bounds
More specifically I am attempting to maximize an expected return given a target risk parameter and portfolio/position limits.
install.packages("Rsocp", repos="http://R-Forge.R-project.org")
install.packages("fPortfolio")
require(fPortfolio)
require(Rsocp)
I run
lppData=100*LPP2005.RET[,1:6]
maxRetSpec=portfolioSpec()
setTargetRisk(maxRetSpec)=0.07
groupConstraints <- c("minsumW[1:6]=-.75",
"maxsumW[1:6]=1.75")
boxConstraints <- c("minW[1:6]=-1",
"maxW[1:6]=1")
bgConstraints <- c(groupConstraints, boxConstraints)
setSolver(maxRetSpec)="solveRsocp"
efficientPortfolio(data=lppData, spec=maxRetSpec, constraints=bgConstraints)
and get the following error...
Error in eqsumW[2, -1] : subscript out of bounds
It is my understand that Rsocp is a second order cone solver designed specifically for this purpose. Having gone through several different stackexchange forums it seems there are several people who have encountered this problem with unsatisfactory solutions. I was wondering if anyone has had success using the Rsocp solver that could give me a hand working through this error? Or alternatively can someone point me towards an 'R' solver that can handle this type of optimization problem?

Optimization in R with arbitrary constraints

I have done it in Excel but need to run a proper simulation in R.
I need to minimize function F(x) (x is a vector) while having constraints that sum(x)=1, all values in x are [0,1] and another function G(x) > G_0.
I have tried it with optim and constrOptim. None of them give you this option.
The problem you are referring to is (presumably) a non-linear optimization with non-linear constraints. This is one of the most general optimization problems.
The package I have used for these purposes is called nloptr: see here. From my experience, it is both versatile and fast. You can specify both equality and inequality constaints by setting eval_g_eq and eval_g_ineq, correspondingly. If the jacobians are known explicitly (can be derived analytically), specify them for faster convergence; otherwise, a numerical approximation is used.
Use this list as a general reference to optimization problems.
Write the set of equations using the Lagrange multiplier, then solve using the R command nlm.
You can do this in the OpenMx Package (currently host at the site listed below. Aiming for 2.0 relase on cran this year)
It is a general purpose package mostly used for Structural Equation Modelling, but handling nonlinear constraints.
FOr your case, make an mxModel() with your algebras expressed in mxAlgebras() and the constraints in mxConstraints()
When you mxRun() the model, the algebras will be solved within the constraints, if possible.
http://openmx.psyc.virginia.edu/

Resources