# [R] Optimization in R similar to MS Excel Solver

Richard James Richard.J.Cooper at uea.ac.uk
Fri Oct 19 16:25:19 CEST 2012

```Dear Colleagues,
I am attempting to develop an optimization routine for a river suspended
sediment mixing model. I have Calcium and Magnesium concentrations (%) for
sediments from 4 potential source areas (Topsoil, Channel Banks, Roads,
Drains) and I want to work out, based on the suspended sediment calcium and
magnesium concentrations, what are the optimal contributions from each
source area to river suspended sediments. The dataset is as follows:

Topsoil         Channel Bank
Ca(%)                         0.03                0.6
0.2                  0.35
Mg(%)                        0.0073           0.0102
0.0141           0.012
Contribution           0.25                0.25
0.25                0.25

and

Ca in river(%)         0.33
Mg in river (%)      0.0114

I want to optimize the contribution values (currently set at 0.25) by
minimizing the sum of squares of the relative errors of the mixing model,
calculated as follows:

SSRE =( (x1-((a1*c1)+(a2*c2)+(a3*c3)+(a4*c4))/x1)^2) +
(x2-((b1*c1)+(b2*c2)+(b3*c3)+(b4*c4))/x2)^2)

Where:
x1 = calcium in river;
x2 = magnesium in river;
a1:a4 = Ca in topsoil, channel banks, roads, drains
b1:b4 = Mg in topsoil, channel banks, roads, drains
c1:4 = Contribution to be optimized

I can generate a solution very quickly using the MS Excel Solver function,
however later I want to be able to run a Monte-Carlo simulation on to
generate confidence intervals based on variance in the source area
concentrations – hence my desire to use R to develop the mixing model.

So far I have been using the ‘optim’ function, however I’m confused as to
what form the ‘par’ and ‘fn’ arguments should take from my data above. I am
also unsure of how to write the model constraints – i.e. total contribution
should sum to 1, and all values must be non-negative.

Any help, advise, or suggestions to this problem would be very much
appreciated.

--
View this message in context: http://r.789695.n4.nabble.com/Optimization-in-R-similar-to-MS-Excel-Solver-tp4646759.html
Sent from the R help mailing list archive at Nabble.com.

```