Duncan Murdoch
2025-Mar-28 09:04 UTC
[R] Problem with minimization that I failed to understand
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.
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]]
Seemingly Similar 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