Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

solve simple quadratic optimization in R with sum constraint

$
0
0

I want to solve a very simple quadratic optimization problem in R where one of the constraints is an equality constraint related to the sum of a vector. I tried to use the quadprog package but I cannot figure out how to make it work. The help function does not provide an example where the one of the constraints equals the sum. But perhaps quadprog is not the right package to use...

My problem is the following. I would like to shrink/expand a vector of values in such a way that the sum is equal to a set value and their values are bound by 0 and a maximum value. As the results need to be as close to the initial values as possible I want to minimize the residual sum of squares. Below I present a toy example. It is easy to see that for example (5,5,6) is in the solution space but not the optimal solution.

x_start <- c(4,3,7)
tot <- 16

# Objective function that needs to be minimized
obj <- function(x){return((x-x_start)^2)}

# Subject to: 
# Sum constraint
con1 <- function(x) {return(sum(x)-tot)}

# Bounds 0 <= x <=6
x_max <- 6
x_min <- 0


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>