Daniel Lobo
2025-Mar-28 13:59 UTC
[R] Problem with minimization that I failed to understand
Hi Duncan, Thanks for your comment, I agree with that. But, how it can be justified that an Optimizer gives a result which is inferior to the starting value? At most, resulting value can remain at the same level, isnt it? On Fri, 28 Mar 2025 at 14:34, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> I haven't run your code, but since Kendall correlation is based on > ranks, your Fn is probably locally constant with jumps when the ranks > change. That's a really hard kind of function to maximize, and the > algorithm used by fmincon is not appropriate to do it. > > Sorry, but I don't know if there is an R function that can do > constrained discrete maximization. > > Duncan Murdoch > > On 2025-03-27 2:35 p.m., Daniel Lobo wrote: > > Hi, > > > > I have below minimization problem > > > > > > MyDat = structure(list(c(50L, 0L, 0L, 50L, 75L, 100L, 50L, 0L, 50L, 0L, > > 25L, 50L, 50L, 75L, 75L, 75L, 0L, 75L, 75L, 75L, 0L, 25L, 75L, > > 75L, 0L, 75L, 100L, 0L, 25L, 100L), c(75L, 0L, 0L, 50L, 100L, > > 50L, 75L, 75L, 100L, 25L, 0L, 25L, 100L, 0L, 50L, 0L, 25L, 25L, > > 100L, 75L, 0L, 0L, 0L, 50L, 0L, 75L, 75L, 0L, 50L, 25L), c(50L, > > 0L, 0L, 0L, 100L, 25L, 0L, 0L, 25L, 50L, 0L, 25L, 75L, 50L, 100L, > > 50L, 0L, 75L, 25L, 50L, 0L, 0L, 25L, 0L, 50L, 100L, 100L, 0L, > > 75L, 50L), c(25L, 0L, 0L, 75L, 75L, 25L, 50L, 50L, 100L, 25L, > > 0L, 100L, 50L, 25L, 100L, 25L, 25L, 100L, 50L, 100L, 0L, 0L, > > 100L, 50L, 0L, 50L, 75L, 0L, 50L, 25L), c(50L, 0L, 0L, 75L, 75L, > > 75L, 25L, 25L, 0L, 100L, 0L, 25L, 25L, 75L, 100L, 0L, 25L, 0L, > > 75L, 25L, 25L, 25L, 75L, 25L, 0L, 75L, 100L, 0L, 100L, 100L), > > c(50L, 0L, 0L, 50L, 100L, 25L, 25L, 25L, 50L, 50L, 0L, 50L, > > 75L, 0L, 100L, 50L, 25L, 100L, 50L, 75L, 0L, 0L, 50L, 25L, > > 0L, 100L, 100L, 0L, 75L, 50L), c(50L, 0L, 0L, 50L, 75L, 25L, > > 75L, 50L, 100L, 25L, 0L, 75L, 25L, 0L, 50L, 0L, 50L, 75L, > > 100L, 75L, 0L, 0L, 100L, 0L, 0L, 50L, 75L, 0L, 100L, 100L > > ), c(25L, 75L, 50L, 25L, 75L, 50L, 100L, 75L, 100L, 25L, > > 0L, 75L, 25L, 50L, 25L, 25L, 75L, 75L, 100L, 75L, 75L, 100L, > > 75L, 25L, 0L, 75L, 75L, 0L, 75L, 100L), c(55L, 30L, 20L, > > 30L, 45L, 30L, 30L, 30L, 70L, 30L, 10L, 45L, 45L, 45L, 45L, > > 30L, 30L, 55L, 45L, 45L, 30L, 30L, 30L, NA, 30L, 55L, 45L, > > 20L, 45L, 70L), c(85L, 40L, 40L, 40L, 55L, 40L, 20L, 30L, > > 30L, 30L, 20L, 30L, 70L, 40L, 85L, 55L, 30L, 40L, 30L, 55L, > > 20L, 30L, 55L, 0L, 40L, 55L, 70L, 40L, 85L, 70L), c(45L, > > 45L, 0L, 45L, 45L, 45L, 0L, 0L, 100L, 45L, 0L, 100L, 45L, > > 45L, 100L, 45L, 45L, 100L, 45L, 45L, 45L, 45L, 25L, 45L, > > 0L, 100L, 45L, 0L, 45L, 45L), c(55L, 45L, 45L, 45L, 55L, > > 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 55L, 45L, > > 55L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 45L, 45L, 45L, > > 45L), c(100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, > > 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, > > 100L, 100L, 100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, > > 100L), c(100L, 25L, 25L, 0L, 100L, 60L, 0L, 0L, 25L, 60L, > > 0L, 60L, 100L, 60L, 100L, 100L, 25L, 100L, 60L, 100L, 100L, > > 60L, 100L, 60L, 100L, 100L, 100L, 100L, 60L, 60L), c(0L, > > 0L, 50L, 50L, 100L, 100L, 0L, 0L, 100L, 100L, 0L, 100L, 100L, > > 0L, 100L, 100L, 0L, 100L, 100L, 100L, 100L, 100L, 100L, 0L, > > 100L, 100L, 100L, 100L, 100L, 100L), c(40L, 100L, 40L, 100L, > > 100L, 40L, 100L, 100L, 100L, 40L, 100L, 100L, 100L, 100L, > > 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, > > 100L, 100L, 100L, 0L, 100L, 100L), c(100L, 100L, 100L, 100L, > > 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, > > 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, -10L, > > 100L, 100L, 100L, -10L, 100L, 100L), c(70L, 0L, 25L, 0L, > > 100L, 25L, 0L, 0L, 0L, 45L, 0L, 25L, 100L, 100L, 100L, 100L, > > 0L, 70L, 0L, 100L, 45L, 45L, 0L, 0L, 100L, 100L, 100L, 0L, > > 100L, 100L), c(55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, > > 55L, 55L, 55L, 55L, 55L, 55L, 55L, 20L, 55L, 20L, 55L, 20L, > > 20L, 100L, 55L, 55L, 55L, 55L, 0L, 55L, 55L), c(65L, 65L, > > 100L, 65L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, > > 100L, 100L, 100L, 100L, 65L, 100L, 100L, 100L, 65L, 100L, > > 0L, 65L, 100L, 100L, 100L, 100L, 100L, 100L), c(85L, 85L, > > 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 56L, 85L, > > 100L, 85L, 85L, 85L, 0L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, > > 85L, 28L, 56L, 56L)), row.names = c(NA, -30L), class = "data.frame") > > > > Fn = function(Wts) return(-Kendall::Kendall(1:Nobs, > > rank(-as.vector(as.matrix(MyDat) %*% matrix(Wts, nc = 1)[, 1, drop > > T])))$tau[1]) > > q1 = pracma::fmincon(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, > > 0.09, 0.08, 0.02, 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, > 0.02), > > fn = Fn, > > A = matrix(c(rep(0, 20), -1), nrow = 1), b = -2.05/100, Aeq > > matrix(c(rep(1, 20), 1), nrow = 1), beq = 1, > > lb = rep(0.01, 21), > > tol = 1e-16, maxfeval = 10000000, maxiter = 5000000) > > > > > > However with above code, I got sub-optimal value in terms of minimization > > of the objective function: > > > > q1$value > > #0.1632184 > > Fn(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, 0.09, 0.08, 0.02, > > 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, 0.02)) > > #0.1586207 > > > > Could you please help me to understand what went wrong with my code and > how > > to correct that? > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]
Every time I give a seminar on optimization (most recently in Feb at Univ Cote d'Azur -- thank you Yassine for the welcome!) I point out Algorithms CONVERGE Programs TERMINATE If you race a Maserati (fmincon?) on a dirt bike course, you'll likely get stuck on the first mud mound, which could be higher than the start. You might to better with a Nelder-Mead type minimizer, setting the objective very large when constraints violated, but ensuring starting polytope is all-feasible. Not guaranteed, but NM methods are surprisingly robust. My experience is that they get "near" a local min quite quickly, then take forever to decide they're done. And if you have quite large flat areas, polytope may collapse. I'm suggesting this as I don't have knowledge of a discrete optimizer in R. JN On 2025-03-28 09:59, Daniel Lobo wrote:> Hi Duncan, > > Thanks for your comment, I agree with that. > > But, how it can be justified that an Optimizer gives a result which is > inferior to the starting value? At most, resulting value can remain at the > same level, isnt it? > > On Fri, 28 Mar 2025 at 14:34, Duncan Murdoch <murdoch.duncan at gmail.com> > wrote: > >> I haven't run your code, but since Kendall correlation is based on >> ranks, your Fn is probably locally constant with jumps when the ranks >> change. That's a really hard kind of function to maximize, and the >> algorithm used by fmincon is not appropriate to do it. >> >> Sorry, but I don't know if there is an R function that can do >> constrained discrete maximization. >> >> Duncan Murdoch >> >> On 2025-03-27 2:35 p.m., Daniel Lobo wrote: >>> Hi, >>> >>> I have below minimization problem >>> >>> >>> MyDat = structure(list(c(50L, 0L, 0L, 50L, 75L, 100L, 50L, 0L, 50L, 0L, >>> 25L, 50L, 50L, 75L, 75L, 75L, 0L, 75L, 75L, 75L, 0L, 25L, 75L, >>> 75L, 0L, 75L, 100L, 0L, 25L, 100L), c(75L, 0L, 0L, 50L, 100L, >>> 50L, 75L, 75L, 100L, 25L, 0L, 25L, 100L, 0L, 50L, 0L, 25L, 25L, >>> 100L, 75L, 0L, 0L, 0L, 50L, 0L, 75L, 75L, 0L, 50L, 25L), c(50L, >>> 0L, 0L, 0L, 100L, 25L, 0L, 0L, 25L, 50L, 0L, 25L, 75L, 50L, 100L, >>> 50L, 0L, 75L, 25L, 50L, 0L, 0L, 25L, 0L, 50L, 100L, 100L, 0L, >>> 75L, 50L), c(25L, 0L, 0L, 75L, 75L, 25L, 50L, 50L, 100L, 25L, >>> 0L, 100L, 50L, 25L, 100L, 25L, 25L, 100L, 50L, 100L, 0L, 0L, >>> 100L, 50L, 0L, 50L, 75L, 0L, 50L, 25L), c(50L, 0L, 0L, 75L, 75L, >>> 75L, 25L, 25L, 0L, 100L, 0L, 25L, 25L, 75L, 100L, 0L, 25L, 0L, >>> 75L, 25L, 25L, 25L, 75L, 25L, 0L, 75L, 100L, 0L, 100L, 100L), >>> c(50L, 0L, 0L, 50L, 100L, 25L, 25L, 25L, 50L, 50L, 0L, 50L, >>> 75L, 0L, 100L, 50L, 25L, 100L, 50L, 75L, 0L, 0L, 50L, 25L, >>> 0L, 100L, 100L, 0L, 75L, 50L), c(50L, 0L, 0L, 50L, 75L, 25L, >>> 75L, 50L, 100L, 25L, 0L, 75L, 25L, 0L, 50L, 0L, 50L, 75L, >>> 100L, 75L, 0L, 0L, 100L, 0L, 0L, 50L, 75L, 0L, 100L, 100L >>> ), c(25L, 75L, 50L, 25L, 75L, 50L, 100L, 75L, 100L, 25L, >>> 0L, 75L, 25L, 50L, 25L, 25L, 75L, 75L, 100L, 75L, 75L, 100L, >>> 75L, 25L, 0L, 75L, 75L, 0L, 75L, 100L), c(55L, 30L, 20L, >>> 30L, 45L, 30L, 30L, 30L, 70L, 30L, 10L, 45L, 45L, 45L, 45L, >>> 30L, 30L, 55L, 45L, 45L, 30L, 30L, 30L, NA, 30L, 55L, 45L, >>> 20L, 45L, 70L), c(85L, 40L, 40L, 40L, 55L, 40L, 20L, 30L, >>> 30L, 30L, 20L, 30L, 70L, 40L, 85L, 55L, 30L, 40L, 30L, 55L, >>> 20L, 30L, 55L, 0L, 40L, 55L, 70L, 40L, 85L, 70L), c(45L, >>> 45L, 0L, 45L, 45L, 45L, 0L, 0L, 100L, 45L, 0L, 100L, 45L, >>> 45L, 100L, 45L, 45L, 100L, 45L, 45L, 45L, 45L, 25L, 45L, >>> 0L, 100L, 45L, 0L, 45L, 45L), c(55L, 45L, 45L, 45L, 55L, >>> 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 55L, 45L, >>> 55L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 45L, 45L, 45L, >>> 45L), c(100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, >>> 100L), c(100L, 25L, 25L, 0L, 100L, 60L, 0L, 0L, 25L, 60L, >>> 0L, 60L, 100L, 60L, 100L, 100L, 25L, 100L, 60L, 100L, 100L, >>> 60L, 100L, 60L, 100L, 100L, 100L, 100L, 60L, 60L), c(0L, >>> 0L, 50L, 50L, 100L, 100L, 0L, 0L, 100L, 100L, 0L, 100L, 100L, >>> 0L, 100L, 100L, 0L, 100L, 100L, 100L, 100L, 100L, 100L, 0L, >>> 100L, 100L, 100L, 100L, 100L, 100L), c(40L, 100L, 40L, 100L, >>> 100L, 40L, 100L, 100L, 100L, 40L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 0L, 100L, 100L), c(100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, -10L, >>> 100L, 100L, 100L, -10L, 100L, 100L), c(70L, 0L, 25L, 0L, >>> 100L, 25L, 0L, 0L, 0L, 45L, 0L, 25L, 100L, 100L, 100L, 100L, >>> 0L, 70L, 0L, 100L, 45L, 45L, 0L, 0L, 100L, 100L, 100L, 0L, >>> 100L, 100L), c(55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, >>> 55L, 55L, 55L, 55L, 55L, 55L, 55L, 20L, 55L, 20L, 55L, 20L, >>> 20L, 100L, 55L, 55L, 55L, 55L, 0L, 55L, 55L), c(65L, 65L, >>> 100L, 65L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 65L, 100L, 100L, 100L, 65L, 100L, >>> 0L, 65L, 100L, 100L, 100L, 100L, 100L, 100L), c(85L, 85L, >>> 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 56L, 85L, >>> 100L, 85L, 85L, 85L, 0L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, >>> 85L, 28L, 56L, 56L)), row.names = c(NA, -30L), class = "data.frame") >>> >>> Fn = function(Wts) return(-Kendall::Kendall(1:Nobs, >>> rank(-as.vector(as.matrix(MyDat) %*% matrix(Wts, nc = 1)[, 1, drop >>> T])))$tau[1]) >>> q1 = pracma::fmincon(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, >>> 0.09, 0.08, 0.02, 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, >> 0.02), >>> fn = Fn, >>> A = matrix(c(rep(0, 20), -1), nrow = 1), b = -2.05/100, Aeq >>> matrix(c(rep(1, 20), 1), nrow = 1), beq = 1, >>> lb = rep(0.01, 21), >>> tol = 1e-16, maxfeval = 10000000, maxiter = 5000000) >>> >>> >>> However with above code, I got sub-optimal value in terms of minimization >>> of the objective function: >>> >>> q1$value >>> #0.1632184 >>> Fn(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, 0.09, 0.08, 0.02, >>> 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, 0.02)) >>> #0.1586207 >>> >>> Could you please help me to understand what went wrong with my code and >> how >>> to correct that? >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >> https://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Duncan Murdoch
2025-Mar-28 16:08 UTC
[R] Problem with minimization that I failed to understand
That's a question for the maintainer of the package you used. Duncan Murdoch On 2025-03-28 9:59 a.m., Daniel Lobo wrote:> Hi Duncan, > > Thanks for?your comment, I agree with that. > > But, how it can be justified that an Optimizer gives a result which is > inferior to the starting value? At most, resulting value can remain at > the same level, isnt it? > > On Fri, 28 Mar 2025 at 14:34, Duncan Murdoch <murdoch.duncan at gmail.com > <mailto:murdoch.duncan at gmail.com>> wrote: > > I haven't run your code, but since Kendall correlation is based on > ranks, your Fn is probably locally constant with jumps when the ranks > change.? That's a really hard kind of function to maximize, and the > algorithm used by fmincon is not appropriate to do it. > > Sorry, but I don't know if there is an R function that can do > constrained discrete maximization. > > Duncan Murdoch > > On 2025-03-27 2:35 p.m., Daniel Lobo wrote: > > Hi, > > > > I have below minimization problem > > > > > > MyDat = structure(list(c(50L, 0L, 0L, 50L, 75L, 100L, 50L, 0L, > 50L, 0L, > > 25L, 50L, 50L, 75L, 75L, 75L, 0L, 75L, 75L, 75L, 0L, 25L, 75L, > > 75L, 0L, 75L, 100L, 0L, 25L, 100L), c(75L, 0L, 0L, 50L, 100L, > > 50L, 75L, 75L, 100L, 25L, 0L, 25L, 100L, 0L, 50L, 0L, 25L, 25L, > > 100L, 75L, 0L, 0L, 0L, 50L, 0L, 75L, 75L, 0L, 50L, 25L), c(50L, > > 0L, 0L, 0L, 100L, 25L, 0L, 0L, 25L, 50L, 0L, 25L, 75L, 50L, 100L, > > 50L, 0L, 75L, 25L, 50L, 0L, 0L, 25L, 0L, 50L, 100L, 100L, 0L, > > 75L, 50L), c(25L, 0L, 0L, 75L, 75L, 25L, 50L, 50L, 100L, 25L, > > 0L, 100L, 50L, 25L, 100L, 25L, 25L, 100L, 50L, 100L, 0L, 0L, > > 100L, 50L, 0L, 50L, 75L, 0L, 50L, 25L), c(50L, 0L, 0L, 75L, 75L, > > 75L, 25L, 25L, 0L, 100L, 0L, 25L, 25L, 75L, 100L, 0L, 25L, 0L, > > 75L, 25L, 25L, 25L, 75L, 25L, 0L, 75L, 100L, 0L, 100L, 100L), > >? ? ? c(50L, 0L, 0L, 50L, 100L, 25L, 25L, 25L, 50L, 50L, 0L, 50L, > >? ? ? 75L, 0L, 100L, 50L, 25L, 100L, 50L, 75L, 0L, 0L, 50L, 25L, > >? ? ? 0L, 100L, 100L, 0L, 75L, 50L), c(50L, 0L, 0L, 50L, 75L, 25L, > >? ? ? 75L, 50L, 100L, 25L, 0L, 75L, 25L, 0L, 50L, 0L, 50L, 75L, > >? ? ? 100L, 75L, 0L, 0L, 100L, 0L, 0L, 50L, 75L, 0L, 100L, 100L > >? ? ? ), c(25L, 75L, 50L, 25L, 75L, 50L, 100L, 75L, 100L, 25L, > >? ? ? 0L, 75L, 25L, 50L, 25L, 25L, 75L, 75L, 100L, 75L, 75L, 100L, > >? ? ? 75L, 25L, 0L, 75L, 75L, 0L, 75L, 100L), c(55L, 30L, 20L, > >? ? ? 30L, 45L, 30L, 30L, 30L, 70L, 30L, 10L, 45L, 45L, 45L, 45L, > >? ? ? 30L, 30L, 55L, 45L, 45L, 30L, 30L, 30L, NA, 30L, 55L, 45L, > >? ? ? 20L, 45L, 70L), c(85L, 40L, 40L, 40L, 55L, 40L, 20L, 30L, > >? ? ? 30L, 30L, 20L, 30L, 70L, 40L, 85L, 55L, 30L, 40L, 30L, 55L, > >? ? ? 20L, 30L, 55L, 0L, 40L, 55L, 70L, 40L, 85L, 70L), c(45L, > >? ? ? 45L, 0L, 45L, 45L, 45L, 0L, 0L, 100L, 45L, 0L, 100L, 45L, > >? ? ? 45L, 100L, 45L, 45L, 100L, 45L, 45L, 45L, 45L, 25L, 45L, > >? ? ? 0L, 100L, 45L, 0L, 45L, 45L), c(55L, 45L, 45L, 45L, 55L, > >? ? ? 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 55L, 45L, > >? ? ? 55L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 45L, 45L, 45L, > >? ? ? 45L), c(100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, > >? ? ? 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, > >? ? ? 100L, 100L, 100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, > >? ? ? 100L), c(100L, 25L, 25L, 0L, 100L, 60L, 0L, 0L, 25L, 60L, > >? ? ? 0L, 60L, 100L, 60L, 100L, 100L, 25L, 100L, 60L, 100L, 100L, > >? ? ? 60L, 100L, 60L, 100L, 100L, 100L, 100L, 60L, 60L), c(0L, > >? ? ? 0L, 50L, 50L, 100L, 100L, 0L, 0L, 100L, 100L, 0L, 100L, 100L, > >? ? ? 0L, 100L, 100L, 0L, 100L, 100L, 100L, 100L, 100L, 100L, 0L, > >? ? ? 100L, 100L, 100L, 100L, 100L, 100L), c(40L, 100L, 40L, 100L, > >? ? ? 100L, 40L, 100L, 100L, 100L, 40L, 100L, 100L, 100L, 100L, > >? ? ? 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, > >? ? ? 100L, 100L, 100L, 0L, 100L, 100L), c(100L, 100L, 100L, 100L, > >? ? ? 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, > >? ? ? 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, -10L, > >? ? ? 100L, 100L, 100L, -10L, 100L, 100L), c(70L, 0L, 25L, 0L, > >? ? ? 100L, 25L, 0L, 0L, 0L, 45L, 0L, 25L, 100L, 100L, 100L, 100L, > >? ? ? 0L, 70L, 0L, 100L, 45L, 45L, 0L, 0L, 100L, 100L, 100L, 0L, > >? ? ? 100L, 100L), c(55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, > >? ? ? 55L, 55L, 55L, 55L, 55L, 55L, 55L, 20L, 55L, 20L, 55L, 20L, > >? ? ? 20L, 100L, 55L, 55L, 55L, 55L, 0L, 55L, 55L), c(65L, 65L, > >? ? ? 100L, 65L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, > >? ? ? 100L, 100L, 100L, 100L, 65L, 100L, 100L, 100L, 65L, 100L, > >? ? ? 0L, 65L, 100L, 100L, 100L, 100L, 100L, 100L), c(85L, 85L, > >? ? ? 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 56L, 85L, > >? ? ? 100L, 85L, 85L, 85L, 0L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, > >? ? ? 85L, 28L, 56L, 56L)), row.names = c(NA, -30L), class > "data.frame") > > > > Fn = function(Wts) return(-Kendall::Kendall(1:Nobs, > > rank(-as.vector(as.matrix(MyDat) %*% matrix(Wts, nc = 1)[, 1, drop > > T])))$tau[1]) > > q1 = pracma::fmincon(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, > 0.04, > > 0.09, 0.08, 0.02, 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, > 0.02, 0.02), > > fn = Fn, > >? ? ? ? ? ? A = matrix(c(rep(0, 20), -1), nrow = 1), b > -2.05/100, Aeq > > matrix(c(rep(1, 20), 1), nrow = 1), beq = 1, > >? ? ? ? ? ? lb = rep(0.01, 21), > >? ? ? ? ? ? tol = 1e-16, maxfeval = 10000000, maxiter = 5000000) > > > > > > However with above code, I got sub-optimal value in terms of > minimization > > of the objective function: > > > > q1$value > > #0.1632184 > > Fn(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, 0.09, 0.08, > 0.02, > > 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, 0.02)) > > #0.1586207 > > > > Could you please help me to understand what went wrong with my > code and how > > to correct that? > > > >? ? ? ?[[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org <mailto:R-help at r-project.org> mailing list > -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > <https://stat.ethz.ch/mailman/listinfo/r-help> > > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > <https://www.R-project.org/posting-guide.html> > > and provide commented, minimal, self-contained, reproducible code. >
Rui Barradas
2025-Mar-28 20:18 UTC
[R] Problem with minimization that I failed to understand
?s 13:59 de 28/03/2025, Daniel Lobo escreveu:> Hi Duncan, > > Thanks for your comment, I agree with that. > > But, how it can be justified that an Optimizer gives a result which is > inferior to the starting value? At most, resulting value can remain at the > same level, isnt it? > > On Fri, 28 Mar 2025 at 14:34, Duncan Murdoch <murdoch.duncan at gmail.com> > wrote: > >> I haven't run your code, but since Kendall correlation is based on >> ranks, your Fn is probably locally constant with jumps when the ranks >> change. That's a really hard kind of function to maximize, and the >> algorithm used by fmincon is not appropriate to do it. >> >> Sorry, but I don't know if there is an R function that can do >> constrained discrete maximization. >> >> Duncan Murdoch >> >> On 2025-03-27 2:35 p.m., Daniel Lobo wrote: >>> Hi, >>> >>> I have below minimization problem >>> >>> >>> MyDat = structure(list(c(50L, 0L, 0L, 50L, 75L, 100L, 50L, 0L, 50L, 0L, >>> 25L, 50L, 50L, 75L, 75L, 75L, 0L, 75L, 75L, 75L, 0L, 25L, 75L, >>> 75L, 0L, 75L, 100L, 0L, 25L, 100L), c(75L, 0L, 0L, 50L, 100L, >>> 50L, 75L, 75L, 100L, 25L, 0L, 25L, 100L, 0L, 50L, 0L, 25L, 25L, >>> 100L, 75L, 0L, 0L, 0L, 50L, 0L, 75L, 75L, 0L, 50L, 25L), c(50L, >>> 0L, 0L, 0L, 100L, 25L, 0L, 0L, 25L, 50L, 0L, 25L, 75L, 50L, 100L, >>> 50L, 0L, 75L, 25L, 50L, 0L, 0L, 25L, 0L, 50L, 100L, 100L, 0L, >>> 75L, 50L), c(25L, 0L, 0L, 75L, 75L, 25L, 50L, 50L, 100L, 25L, >>> 0L, 100L, 50L, 25L, 100L, 25L, 25L, 100L, 50L, 100L, 0L, 0L, >>> 100L, 50L, 0L, 50L, 75L, 0L, 50L, 25L), c(50L, 0L, 0L, 75L, 75L, >>> 75L, 25L, 25L, 0L, 100L, 0L, 25L, 25L, 75L, 100L, 0L, 25L, 0L, >>> 75L, 25L, 25L, 25L, 75L, 25L, 0L, 75L, 100L, 0L, 100L, 100L), >>> c(50L, 0L, 0L, 50L, 100L, 25L, 25L, 25L, 50L, 50L, 0L, 50L, >>> 75L, 0L, 100L, 50L, 25L, 100L, 50L, 75L, 0L, 0L, 50L, 25L, >>> 0L, 100L, 100L, 0L, 75L, 50L), c(50L, 0L, 0L, 50L, 75L, 25L, >>> 75L, 50L, 100L, 25L, 0L, 75L, 25L, 0L, 50L, 0L, 50L, 75L, >>> 100L, 75L, 0L, 0L, 100L, 0L, 0L, 50L, 75L, 0L, 100L, 100L >>> ), c(25L, 75L, 50L, 25L, 75L, 50L, 100L, 75L, 100L, 25L, >>> 0L, 75L, 25L, 50L, 25L, 25L, 75L, 75L, 100L, 75L, 75L, 100L, >>> 75L, 25L, 0L, 75L, 75L, 0L, 75L, 100L), c(55L, 30L, 20L, >>> 30L, 45L, 30L, 30L, 30L, 70L, 30L, 10L, 45L, 45L, 45L, 45L, >>> 30L, 30L, 55L, 45L, 45L, 30L, 30L, 30L, NA, 30L, 55L, 45L, >>> 20L, 45L, 70L), c(85L, 40L, 40L, 40L, 55L, 40L, 20L, 30L, >>> 30L, 30L, 20L, 30L, 70L, 40L, 85L, 55L, 30L, 40L, 30L, 55L, >>> 20L, 30L, 55L, 0L, 40L, 55L, 70L, 40L, 85L, 70L), c(45L, >>> 45L, 0L, 45L, 45L, 45L, 0L, 0L, 100L, 45L, 0L, 100L, 45L, >>> 45L, 100L, 45L, 45L, 100L, 45L, 45L, 45L, 45L, 25L, 45L, >>> 0L, 100L, 45L, 0L, 45L, 45L), c(55L, 45L, 45L, 45L, 55L, >>> 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 55L, 45L, >>> 55L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 45L, 45L, 45L, >>> 45L), c(100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, >>> 100L), c(100L, 25L, 25L, 0L, 100L, 60L, 0L, 0L, 25L, 60L, >>> 0L, 60L, 100L, 60L, 100L, 100L, 25L, 100L, 60L, 100L, 100L, >>> 60L, 100L, 60L, 100L, 100L, 100L, 100L, 60L, 60L), c(0L, >>> 0L, 50L, 50L, 100L, 100L, 0L, 0L, 100L, 100L, 0L, 100L, 100L, >>> 0L, 100L, 100L, 0L, 100L, 100L, 100L, 100L, 100L, 100L, 0L, >>> 100L, 100L, 100L, 100L, 100L, 100L), c(40L, 100L, 40L, 100L, >>> 100L, 40L, 100L, 100L, 100L, 40L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 0L, 100L, 100L), c(100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, -10L, >>> 100L, 100L, 100L, -10L, 100L, 100L), c(70L, 0L, 25L, 0L, >>> 100L, 25L, 0L, 0L, 0L, 45L, 0L, 25L, 100L, 100L, 100L, 100L, >>> 0L, 70L, 0L, 100L, 45L, 45L, 0L, 0L, 100L, 100L, 100L, 0L, >>> 100L, 100L), c(55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, >>> 55L, 55L, 55L, 55L, 55L, 55L, 55L, 20L, 55L, 20L, 55L, 20L, >>> 20L, 100L, 55L, 55L, 55L, 55L, 0L, 55L, 55L), c(65L, 65L, >>> 100L, 65L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, >>> 100L, 100L, 100L, 100L, 65L, 100L, 100L, 100L, 65L, 100L, >>> 0L, 65L, 100L, 100L, 100L, 100L, 100L, 100L), c(85L, 85L, >>> 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 56L, 85L, >>> 100L, 85L, 85L, 85L, 0L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, >>> 85L, 28L, 56L, 56L)), row.names = c(NA, -30L), class = "data.frame") >>> >>> Fn = function(Wts) return(-Kendall::Kendall(1:Nobs, >>> rank(-as.vector(as.matrix(MyDat) %*% matrix(Wts, nc = 1)[, 1, drop >>> T])))$tau[1]) >>> q1 = pracma::fmincon(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, >>> 0.09, 0.08, 0.02, 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, >> 0.02), >>> fn = Fn, >>> A = matrix(c(rep(0, 20), -1), nrow = 1), b = -2.05/100, Aeq >>> matrix(c(rep(1, 20), 1), nrow = 1), beq = 1, >>> lb = rep(0.01, 21), >>> tol = 1e-16, maxfeval = 10000000, maxiter = 5000000) >>> >>> >>> However with above code, I got sub-optimal value in terms of minimization >>> of the objective function: >>> >>> q1$value >>> #0.1632184 >>> Fn(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, 0.09, 0.08, 0.02, >>> 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, 0.02)) >>> #0.1586207 >>> >>> Could you please help me to understand what went wrong with my code and >> how >>> to correct that? >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >> https://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, I don't know if this is relevant a package GA - genetic algorithms - gets solutions above the starting value. Fn <- function(Wts) return( -Kendall::Kendall( 1:Nobs, rank(-as.vector(as.matrix(MyDat) %*% matrix(Wts, nc = 1)[, 1, drop = T])) )$tau[1] ) Nobs <- nrow(MyDat) StartingValue <- c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, 0.09, 0.08, 0.02, 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, 0.02) library(GA) #> Loading required package: foreach #> Loading required package: iterators #> Package 'GA' version 3.2.4 #> Type 'citation("GA")' for citing this R package in publications. #> #> Attaching package: 'GA' #> The following object is masked from 'package:utils': #> #> de set.seed(2025) g1 <- ga( type = "real-valued", fitness = \(x) Fn(x), lower = rep(0.01, 21), upper = rep(1, 21L), maxiter = 100L ) dim(g1 at solution) #> [1] 21 21 apply(g1 at solution, 1L, Fn) #> [1] 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 #> [8] 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 #> [15] 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 0.2735632 Fn(StartingValue) #> [1] 0.1586207 suggestions <- g1 at solution g2 <- ga( type = "real-valued", fitness = function(x) Fn(x), lower = rep(0.01, 21), upper = rep(1, 21L), suggestions = suggestions, maxiter = 100L ) dim(g2 at solution) #> [1] 41 21 apply(g2 at solution, 1L, Fn) #> [1] 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 #> [8] 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 #> [15] 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 #> [22] 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 #> [29] 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 #> [36] 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 0.2873563 Fn(StartingValue) #> [1] 0.1586207 Hoep this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Possibly Parallel Threads
- Problem with minimization that I failed to understand
- Problem with minimization that I failed to understand
- Problem with minimization that I failed to understand
- Problem with minimization that I failed to understand
- Problem with minimization that I failed to understand