[R] suboptimal lp solutions
Peter Dalgaard
P.Dalgaard at biostat.ku.dk
Wed Oct 22 17:08:28 CEST 2008
Selwyn McCracken wrote:
> Hi list,
>
> I want to find the total maximum resources I can spend given a set
> allocation proportion and some simple budget constraints.
>
> However, I get suboptimal results via lp and friends (i.e. lpSolve and
> simplex in the linprog and boot) .
>
> For example:
>
> library(lpSolve)
>
> proportions = c( 0.46, 0.28, 0.26)
> constraints = c( 352, 75, 171)
>
> lp(objective.in = proportions,
> const.mat = diag(c(1,1,1)),
> const.rhs=constraints,
> const.dir=rep('<=',3),
> direction='max'
> )
>
> ## > Success: the objective function is 227.38
> 227.38 * proportions #[1] 104.5948 63.6664 59.1188 (OK all less than
> constraints, however...)
>
> 267 * proportions ## [1] 122.82 74.76 69.42 (... a better result is
> possible)
>
> ## This example goes over the budget constraints
>
> proportions2 = c( 0.47, 0.28, 0.25)
> constraints2 = c(12, 7, 99)
>
> lp(objective.in = proportions2,
> const.mat = diag(c(1,1,1)),
> const.rhs=constraints2,
> const.dir=rep('<=',3),
> direction='max'
> )
>
> ## > Success: the objective function is 32.35
> 32.35 * proportions2 ##[1] 15.2045 9.0580 8.0875 1st and second values are
> higher than the constraints allow!!!
>
> Any help gratefully received.
>
>
I'm not sure what you're trying to do but you're not doing it right...
> proportions = c( 0.46, 0.28, 0.26)
> constraints = c( 352, 75, 171)
>
> lp(objective.in = proportions,
+ const.mat = diag(c(1,1,1)),
+ const.rhs=constraints,
+ const.dir=rep('<=',3),
+ direction='max'
+ )
Success: the objective function is 227.38
> v <- .Last.value
> v$solution
[1] 352 75 171
> sum(v$solution*proportions)
[1] 227.38
... which is fine: The coefficients of the objective function are all
positive and there are separate constraints of each component, so just
take the max allowed of each.
However, something tells me that that wasn't the problem you set out to
solve....
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list