[R] Problem with minimization that I failed to understand

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Fri Mar 28 21:18:52 CET 2025


À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 using 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 using 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 using 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 using solution)
#> [1] 21 21
apply(g1 using 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 using 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 using solution)
#> [1] 41 21
apply(g2 using 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



More information about the R-help mailing list