# [R] constrOptim with method = "L-BFGS-B"

Spencer Graves spencer.graves at pdf.com
Tue Jun 17 21:34:27 CEST 2008

```      I believe that 'optim' will not accept equality constraints.

However, you do not need the generality of 'optim' to "minimize a
quadratic function with boundary conditions and one equality
condition".  This type of problem is called "quadratic programming",
and  RSiteSearch("quadratic programming", "fun") just returned 29 hits
for me.  The first 3 cite functions in the "LowRankQP", "kernlab", and
"quadprog" packages.  I don't know if any of these will solve your
problem, but I suspect that at least one might.  If not, can you recast
the problem to remove the equality constraint?

If the above does not work for you, I suggest you try a much
simpler version, e.g., with 'mat' = a 3 x 3 array with one inequality
and one equality, as suggested in the famous book by Polya on "How to
Solve It" (http://en.wikipedia.org/wiki/How_to_Solve_It).  This has the
added advantage of giving you a simpler example to send to this list if
you can't make it work.  You are to be commended for providing a
self-contained example.  Unfortunately, your example is so large that it
is slightly intimidating.  A simpler example might elicit more (and more
useful) replies -- if it doesn't lead you to the solution, as Polya
suggested that it might.

Hope this helps,
Spencer

lhaba wrote:
> Hi,
> i need to minimize a quadratic function with boundary condidtions and one
> equality condition.
> In order to do that i converted the equality constraint into 2 inequality
> constaints and passed everything cia constrOptim, as the manual said:
> everything included in the ... will be passed to Optim that will pass it
> back to fn in case it does not need it.
>
> My code is the following:
>
> mat <- array( c(0.0001088799073581,	0.0000136029502036,	0.0000060430384243,
> 0.0000847097879033,	0.0000115053365822,	0.0000216245975292,
> 0.0000483253391811,	0.0000787580901352,	0.0000186474817658,
> 0.0000312260571354,	0.0000217594093734,	0.0000536298150897,
> 0.0000166202592455,	0.0000451975061637,	-0.0000120364862228,
> 0.0000497117714376,
> 0.0000136029502036,	0.0001537319301276,	0.0000226518408080,
> 0.0000591480002102,	0.0000797128619950,	0.0000091332643423,
> 0.0000693354260457,	0.0000825217915015,	0.0000229122227269,
> 0.0000297662414650,	0.0000334443258658,	0.0000273254534933,
> 0.0000202062301763,	0.0000026260702295,	0.0000558975248740,
> 0.0000953647537111,
> 0.0000060430384243,	0.0000226518408080,	0.0005971325756834,
> -0.0000762583321100,	-0.0000246005202071,	-0.0000300982253054,
> 0.0000299178429478,	0.0000135672602503,	0.0001735431064391,
> -0.0000133347388414,	0.0001387582890571,	0.0000964898243724,
> -0.0000149571346672,	0.0000104437939143,	0.0001246900353191,
> -0.0000171884354549,
> 0.0000847097879033,	0.0000591480002102,	-0.0000762583321100,
> 0.0004968467836203,	0.0002303499425964,	0.0000992731601466,
> 0.0002685466918035,	0.0002580180069951,	0.0000725833959653,
> 0.0000525639940758,	0.0001785049461665,	0.0001781339191317,
> 0.0000597631329497,	0.0000201160486244,	0.0002582267884874,
> 0.0002473268250781,
> 0.0000115053365822,	0.0000797128619950,	-0.0000246005202071,
> 0.0002303499425964,	0.0002945009393242,	-0.0000426583313588,
> 0.0002067711081561,	0.0002695894499975,	0.0001312519434236,
> -0.0000079156628396,	0.0001423655606105,	0.0000044733483182,
> 0.0000303832556655,	0.0000577624190434,	0.0001193435284164,
> 0.0002422477575812,
> 0.0000216245975292,	0.0000091332643423,	-0.0000300982253054,
> 0.0000992731601466,	-0.0000426583313588,	0.0001641146317929,
> 0.0000311621614693,	-0.0000147821020927,	-0.0000767394607354,
> 0.0000619936562782,	-0.0000306228761064,	0.0001495752154579,
> 0.0000389317919640,	-0.0000714551280935,	-0.0000564616194935,
> 0.0000384367900903,
> 0.0000483253391811,	0.0000693354260457,	0.0000299178429478,
> 0.0002685466918035,	0.0002067711081561,	0.0000311621614693,
> 0.0003176493360736,	0.0002575792630182,	0.0001371966488704,
> 0.0000436833885846,	0.0001442516276721,	0.0001075447728937,
> 0.0000371155448252,	0.0000475873370276,	0.0002162409964174,
> 0.0002870514043081,
> 0.0000787580901352,	0.0000825217915015,	0.0000135672602503,
> 0.0002580180069951,	0.0002695894499975,	-0.0000147821020927,
> 0.0002575792630182,	0.0006217963583393,	0.0002368375072233,
> 0.0000078625467985,	0.0002054774387807,	-0.0000066572248626,
> 0.0000485854317294,	0.0002802199677114,	0.0001676465030622,
> 0.0003028775764026,
> 0.0000186474817658,	0.0000229122227269,	0.0001735431064391,
> 0.0000725833959653,	0.0001312519434236,	-0.0000767394607354,
> 0.0001371966488704,	0.0002368375072233,	0.0004475645060339,
> -0.0000030389778729,	0.0001706183643212,	-0.0000017789896670,
> 0.0000722657436668,	0.0001664088523103,	0.0001220193496918,
> 0.0001641280878243,
> 0.0000312260571354,	0.0000297662414650,	-0.0000133347388414,
> 0.0000525639940758,	-0.0000079156628396,	0.0000619936562782,
> 0.0000436833885846,	0.0000078625467985,	-0.0000030389778729,
> 0.0000822356406019,	-0.0000226786278360,	0.0000752056105897,
> 0.0000399801889185,	-0.0000441549693477,	0.0000047887593401,
> 0.0000352165734549,
> 0.0000217594093734,	0.0000334443258658,	0.0001387582890571,
> 0.0001785049461665,	0.0001423655606105,	-0.0000306228761064,
> 0.0001442516276721,	0.0002054774387807,	0.0001706183643212,
> -0.0000226786278360,	0.0004304869804941,	0.0001566983136020,
> 0.0000332770114864,	0.0000012432094922,	0.0002491186667930,
> 0.0001285479414542,
> 0.0000536298150897,	0.0000273254534933,	0.0000964898243724,
> 0.0001781339191317,	0.0000044733483182,	0.0001495752154579,
> 0.0001075447728937,	-0.0000066572248626,	-0.0000017789896670,
> 0.0000752056105897,	0.0001566983136020,	0.0005292416268831,
> 0.0000893358436932,	-0.0001009559617338,	0.0000888461032129,
> 0.0000714719761291,
> 0.0000166202592455,	0.0000202062301763,	-0.0000149571346672,
> 0.0000597631329497,	0.0000303832556655,	0.0000389317919640,
> 0.0000371155448252,	0.0000485854317294,	0.0000722657436668,
> 0.0000399801889185,	0.0000332770114864,	0.0000893358436932,
> 0.0001844874143317,	0.0000549019705905,	-0.0000117658984941,
> 0.0000394986211508,
> 0.0000451975061637,	0.0000026260702295,	0.0000104437939143,
> 0.0000201160486244,	0.0000577624190434,	-0.0000714551280935,
> 0.0000475873370276,	0.0002802199677114,	0.0001664088523103,
> -0.0000441549693477,	0.0000012432094922,	-0.0001009559617338,
> 0.0000549019705905,	0.0006686123611712,	-0.0001115788528761,
> 0.0000151312169512,
> -0.0000120364862228,	0.0000558975248740,	0.0001246900353191,
> 0.0002582267884874,	0.0001193435284164,	-0.0000564616194935,
> 0.0002162409964174,	0.0001676465030622,	0.0001220193496918,
> 0.0000047887593401,	0.0002491186667930,	0.0000888461032129,
> -0.0000117658984941,	-0.0001115788528761,	0.0014937840813054,
> 0.0001299625832782,
> 0.0000497117714376,	0.0000953647537111,	-0.0000171884354549,
> 0.0002473268250781,	0.0002422477575812,	0.0000384367900903,
> 0.0002870514043081,	0.0003028775764026,	0.0001641280878243,
> 0.0000352165734549,	0.0001285479414542,	0.0000714719761291,
> 0.0000394986211508,	0.0000151312169512,	0.0001299625832782,
> 0.0004355778955394), c(16,16))
>
> covar  <- function(x) return (t(x) %*%mat %*% (x))
> covargr <- function(x) return ( 2*mat %*% (x))
>
> upper1 = c(1, 0, 0, 0,  0, 0, 0,0 ,0 , 0, 0, 0, 0, 0, 0, 0)
> lower1 = c(1, -0.25, -0.25, -0.25, -0.25, -0.25, -0.25, -0.25, -0.25, -0.25,
> -0.25, -0.25, -0.25, -0.25, -0.25, -0.25)
> lower[1] = 1
> init = upper1- 1/15.0
> init[1] = 1
>
>  optim( init,  covar, covargr, method = "L-BFGS-B", lower = lower1, upper =
> upper1)
> u1 <- c(0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 0,
> -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)
> u2<- array(u1, c(16, 2))
> u2[,2] <- -u2[,2]
>
> u1<- t(u1)
> c1<- array(c(-1, -1),c(2,1))
>  constrOptim (init, covar, covargr, t(u2), c1, mu = 1e-054,  method =
> "L-BFGS-B", lower = lower1, upper = upper1, outer.iterations = 100,
> outer.eps = 1e-05)
>
>
> The problem I face is an error message telling me that the method "L-BFGS-B"
> needs finite values of fn...
> while everything works well in the optim case without the inequalities.
>
> Does anybody have any clue about what might have gone wrong?
>
> THank you for your help
>
>
> Georges
>
>

```