简体   繁体   中英

Optimization with the package ‘DEoptimR’ in R code

I've been trying to make an optimization in an objective fuction to find values that will help me be able to do a new ranking which is similar to an expected ranking that I have. It is about a company ranking, and I'm comparing the position of the company in my ranking and in the expected ranking.

I'm using the Evaluation.NDCG from the package 'StatRank'. It says how similar the two rankings are, in a scale from 0 to 1.

To find the values, I'm using JDEoptim from the package 'DEoptimR'.

I have a function called 'otimizar'. The code below is just a restriction, a rule, the sums of some values must be 1, and the sum of some values must be -1.

library(DEoptimR)

restricao <- function(pesos) {
  nota_diarias                   <- pesos[1]
  nota_contratos                 <- pesos[2]
  nota_contratos_at              <- pesos[2]
  nota_contratos_usuario         <- pesos[3]
  nota_contratos_usuario_at      <- pesos[3]
  nota_carros_rua                <- pesos[4]
  nota_carros_rua_at             <- pesos[4]
  nota_receita_media             <- pesos[5]
  nota_receita_media_at          <- pesos[5]
  nota_receita_total             <- pesos[6]
  nota_receita_total_at          <- pesos[6]
  nota_rpd                       <- pesos[7]
  nota_rpd_at                    <- pesos[7]
  nota_meses_relacionamento      <- pesos[8]
  nota_meses_relacionamento_at   <- pesos[8]
  nota_dias_ultimo_contrato      <- pesos[9]
  nota_dias_ultimo_contrato_at   <- pesos[9]
  nota_status                    <- pesos[10]
        

  c(nota_diarias+nota_contratos+nota_contratos_at+nota_contratos_usuario+nota_contratos_usuario_at+      
  nota_carros_rua+nota_carros_rua_at+nota_receita_media+nota_receita_media_at - 1,
 nota_receita_total+nota_receita_total_at+nota_rpd+nota_rpd_at+nota_meses_relacionamento+
  nota_meses_relacionamento_at+nota_dias_ultimo_contrato+nota_dias_ultimo_contrato_at+nota_status - (-1)) }


set.seed(1234)

lower <- c( 0 , 0 ,0 , 0, 0,  0 ,0 , 0, 0, 0,0, 0,0, 0,0,-1,-1, 0 , -1,  0, 0, 0, 0, 0, 0, -1, -1,-1,-1,-1,-1,-1)
upper <- c( 1 , 1 ,1 , 1, 1,  1 ,1 , 1, 1, 1,1, 1,1, 1,1, 0, 0, 1 ,  0 , 1, 1, 1, 1, 1, 1,  0 , 0, 0, 0, 0, 0, 0)

otimzacao=JDEoptim(lower, upper,fn = otimizar, tol = 1e-3,
                   constr = restricao,trace = TRUE, NP = 10, maxiter = 10)

The return of 'Otimizar' is the Evaluation.NDCG value from the package 'StatRank'. I got 0.30 now, and I want to get it near to 1, but the optimization is not working, and it stops in 0.38. And it also doesnt respect the restriction.

Could you guys help me, please?

You have to use 'meq = 2' because you have two equalities as constraints.

You can also try 'eps = 1e-3' instead of the default 'eps = 1e-5'.

Better yet, you can use the two equalities to explicitly calculate the value for two decision variables, thereby reducing the problem to a bound constrained one as suggested in the documentation.

Also, it seems that you want to maximize 'otimizar', therefore you have to change the sign of its return value because 'JDEoptim' tries to find a minimum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM