# [R] Relatively Simple Maximization Using Optim Doesnt Optimize

Skyler Saleebyan @ky|erb@@|eeby@n @end|ng |rom gm@||@com
Thu Mar 12 16:21:40 CET 2020

Thanks for the replies. Since I was seeing this glitch with CG in my 1d and
2d formulation of the problem I was trying to figure out what was going on
that led to the failure.  I'll switch to a more suitable method and keep
these considerations in mind.

On Thu, Mar 12, 2020, 9:23 AM J C Nash <profjcnash using gmail.com> wrote:

> As author of CG (at least the code that was used to build it), I can say I
> was
> never happy with that code. Rcgmin is the replacement I wrote, and I
> believe that
> could still be improved.
>
> BUT:
>   - you have a 1D optimization. Use Brent method and supply bounds.
>   - I never intended CG (or BFGS or Nelder-Mead or ...) to work for 1D
>     problems
>   - as Jeff points out, you need the gradient. I stop Rcgmin and Rvmmin if
>     user hasn't supplied one, as numerical approximations need to be very
>     good for these gradient methods
>
> JN
>
> On 2020-03-12 10:03 a.m., Jeff Newmiller wrote:
> > The help file points out that CG is "fragile" ... and I would expect
> that failing to define a gradient function will exacerbate that.
> >
> > I think you should use a different algorithm or specify a gradient
> function. You might also consider working with the more recent optimr
> package contributed by Dr Nash, author of the original optim function in R.
> >
> > On March 12, 2020 2:30:26 AM PDT, Skyler Saleebyan <
> skylerbsaleebyan using gmail.com> wrote:
> >> I am trying to familiarize myself with optim() with a relatively simple
> >> maximization.
> >>
> >> Description:
> >> L and K are two terms which are constrained to add up to a total 100000
> >> (with respective weights to each). To map this constraint I plugged K
> >> into
> >> the function (to make this as simple as possible.)
> >>
> >> Together these two feed into one nonlinear function which is the
> >> product of
> >> two monotonic (on the positive interval) functions. Then that numbers
> >> is
> >> returned in a function fed to optim, which should maximize the output
> >> by
> >> adjusting L. The whole code is:
> >>
> >> production1 <- function(L){
> >>  budget=100000
> >>  Lcost=12
> >>  Kcost=15
> >>  K=(budget-L*Lcost)/Kcost
> >>  machines=0.05*L^(2/3)*K^(1/3)
> >>  return(machines)
> >> }
> >>
> >> # production1(6000) #example of number with much higher output vs optim
> >> result
> >> S1=optim(1001,production1,method="CG",control=list(fnscale=-1))
> >> S1
> >>
> >> Output:
> >> \$par
> >> [1] 1006.536
> >>
> >> \$value
> >> [1] 90.54671
> >>
> >> \$counts
> >>     201      101
> >>
> >> \$convergence
> >> [1] 1
> >>
> >> \$message
> >> NULL
> >>
> >>
> >> For some reason this never explores the problem space and just spits
> >> out
> >> some answer close to the initial condition. What am I doing wrong?
> >>
> >> Thanks,
> >> Skyler S.
> >>
> >>      [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help