Skip to content

Optimizing

5 messages · Sam Asin, Bert Gunter, R. Michael Weylandt +1 more

#
Sam:

1. Homework? R has a no homework policy.

2. But in any case, check out the Optimization task view on CRAN. You
should be able to find something there that meets your needs.

Of course, if something is "a little over your head," that's not an
excuse, but rather an admission that you have to do some "homework" on
your own.

Cheers,
Bert
On Wed, Nov 14, 2012 at 5:23 PM, Sam Asin <asin.sam at gmail.com> wrote:

  
    
1 day later
#
On Thu, Nov 15, 2012 at 7:49 PM, Sam Asin <asin.sam at gmail.com> wrote:
Real data sets don't usually have people named A,B,C with wages 3,4,5. ;-)

To your question at hand, it's close to a classic problem in
combinatorial optimization known as the knapsack problem, but there
are some small differences. That's a difficult (in a technical sense)
problem but well-studied so there are lots of good "almost solutions."
I'd look into that and see if you can transform it to fit that
framework, for which there is almost surely a CRAN-tested
implementation available.
#
On Wed, Nov 14, 2012 at 8:23 PM, Sam Asin <asin.sam at gmail.com> wrote:
This can be formulated as an integer programming problem. Note that
the proposed solution in your post is infeasible as it violates the
wage constraint.

library(lpSolve)

people <- c("A", "B", "C", "D", "E", "F", "G", "H", "I")
type<- c(1, 1, 1, 1, 2, 2, 3, 3, 3)
value<-c(25.20, 24, 38, 20, 14, 20, 31, 11, 8)
wage<- c(4, 3.8, 5.1, 3.5, 2.4, 3, 6, 2.4, 2)

con.mat <- rbind(type == 1, type == 2, type == 3, wage)
con.dir <- c("==", "==", "==", "<=")
con.rhs <- c(2, 2, 2, 20)
binary.vec <- seq_along(people)
out <- lp("max", value, con.mat, con.dir, con.rhs, binary.vec = binary.vec)
out$solution #  1 0 1 0 1 1 0 1 1
people[out$solution == 1] # "A" "C" "E" "F" "H" "I"
out # 116.2

# note: proposed solution in post violates wage constraint
proposed.soln <- c("C", "D", "E", "F", "G", "I")
crossprod(wage, people %in% proposed.soln) # 22


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com