简体   繁体   中英

Transform Solver Excel program in R

I want to maximize a one variable (sum(DeplaQ1$somme_vrai)) with unknow parameters (alpha,VDT,constVPC,constVPP,a,constTC,const2RM,constTaxi) and I want to determine this parametres with a one constraint x1 <= 10000

I do not find this solution. triplaQ1 is a database with time and number of trip per transport mod.I try to use Optimum or LpSolve but it does not work.**

** My program :

fr <- function(alpha,VDT,constVPC,constVPP,a,constTC,const2RM,constTaxi) {
  triplaQ1$u_VPC <- - (alpha * alpha + 0.001)*(triplaQ1$time_VPC*VDT + 0.15*triplaQ1$dist_VPC + constVPC)

  triplaQ1$u_VPP <- - (alpha * alpha +0.001) * (triplaQ1$time_VPP * VDT) + constVPP

  triplaQ1$u_TC <- - (alpha * alpha +0.001) * ((triplaQ1$time_TC/a)*VDT+constTC)

  triplaQ1$u_mp <- - (alpha * alpha +0.001) * (triplaQ1$time_MP * VDT)

  triplaQ1$u_2RM <- - (alpha * alpha +0.001) * (triplaQ1$time_2RM * VDT + const2RM)

  triplaQ1$u_Taxi <- - (alpha * alpha + 0.001) * (triplaQ1$time_Taxi * VDT + 0.3 * triplaQ1$dist_Taxi + constTaxi)

  triplaQ1$simuleVPC <- triplaQ1$trip * ((exp(triplaQ1$u_VPC)) / (exp(triplaQ1$u_VPC) + exp(triplaQ1$u_VPP) + exp(triplaQ1$u_TC) + exp(triplaQ1$u_mp) + exp(triplaQ1$u_2RM) + exp(triplaQ1$u_Taxi)))

  triplaQ1$simuleVPP <- triplaQ1$trip * ((exp(triplaQ1$u_VPP)) / (exp(triplaQ1$u_VPC) + exp(triplaQ1$u_VPP) + exp(triplaQ1$u_TC) + exp(triplaQ1$u_mp) + exp(triplaQ1$u_2RM) + exp(triplaQ1$u_Taxi)))

  triplaQ1$simuleTC <- triplaQ1$trip * ((exp(triplaQ1$u_TC)) / (exp(triplaQ1$u_VPC) + exp(triplaQ1$u_VPP) + exp(triplaQ1$u_TC) + exp(triplaQ1$u_mp) + exp(triplaQ1$u_2RM) + exp(triplaQ1$u_Taxi)))

  triplaQ1$simuleMAP <- triplaQ1$trip * ((exp(triplaQ1$u_mp)) / (exp(triplaQ1$u_VPC) + exp(triplaQ1$u_VPP) + exp(triplaQ1$u_TC) + exp(triplaQ1$u_mp) + exp(triplaQ1$u_2RM) + exp(triplaQ1$u_Taxi)))

  triplaQ1$simule2RM <- triplaQ1$trip * ((exp(triplaQ1$u_2RM)) / (exp(triplaQ1$u_VPC) + exp(triplaQ1$u_VPP) + exp(triplaQ1$u_TC) + exp(triplaQ1$u_mp) + exp(triplaQ1$u_2RM) + exp(triplaQ1$u_Taxi)))

  triplaQ1$simuleTaxi <- triplaQ1$trip * ((exp(triplaQ1$u_Taxi)) / (exp(triplaQ1$u_VPC) + exp(triplaQ1$u_VPP) + exp(triplaQ1$u_TC) + exp(triplaQ1$u_mp) + exp(triplaQ1$u_2RM) + exp(triplaQ1$u_Taxi)))

  triplaQ1$somme_simule <- triplaQ1$simuleVPC + triplaQ1$simuleVPP + triplaQ1$simuleTC + triplaQ1$simuleMAP + triplaQ1$simule2RM  + triplaQ1$simuleTaxi

  triplaQ1$shsimuleVPC <- ifelse(triplaQ1$trip > 0.001, triplaQ1$simuleVPC/triplaQ1$trip, 0)
  triplaQ1$shsimuleVPP <- ifelse(triplaQ1$trip > 0.001, triplaQ1$simuleVPP/triplaQ1$trip, 0)
  triplaQ1$shsimuleTC <- ifelse(triplaQ1$trip > 0.001, triplaQ1$simuleTC/triplaQ1$trip, 0)
  triplaQ1$shsimuleMAP <- ifelse(triplaQ1$trip > 0.001, triplaQ1$simuleMAP/triplaQ1$trip, 0)
  triplaQ1$shsimule2RM <- ifelse(triplaQ1$trip > 0.001, triplaQ1$simule2RM/triplaQ1$trip, 0)
  triplaQ1$shsimuleTaxi <- ifelse(triplaQ1$trip > 0.001, triplaQ1$simuleTaxi/triplaQ1$trip, 0)

  triplaQ1$shsommesimule <- triplaQ1$shsimuleVPC + triplaQ1$shsimuleVPP + triplaQ1$shsimuleTC + triplaQ1$shsimuleMAP + triplaQ1$shsimule2RM + triplaQ1$shsimuleTaxi

  triplaQ1$VraiVPC <- ifelse(triplaQ1$trip > 0,triplaQ1$Tij_vpc * log(triplaQ1$simuleVPC/triplaQ1$trip),0)
  triplaQ1$VraiVPP <- ifelse(triplaQ1$trip > 0,triplaQ1$Tij._vpp * log(triplaQ1$simuleVPP/triplaQ1$trip),0)
  triplaQ1$VraiTC <- ifelse(triplaQ1$trip > 0,triplaQ1$Tij._tc * log(triplaQ1$simuleTC/triplaQ1$trip),0)
  triplaQ1$VraiMAP <- ifelse(triplaQ1$trip > 0,triplaQ1$Tij._mp * log(triplaQ1$simuleMAP/triplaQ1$trip),0)
  triplaQ1$Vrai2RM <- ifelse(triplaQ1$trip > 0,triplaQ1$Tij._2RM * log(triplaQ1$simule2RM/triplaQ1$trip),0)
  triplaQ1$VraiTaxi <- ifelse(triplaQ1$trip > 0,triplaQ1$Tij_Taxi * log(triplaQ1$simuleTaxi/triplaQ1$trip),0)

  triplaQ1$somme_vrai <- triplaQ1$VraiVPC + triplaQ1$VraiVPP + triplaQ1$VraiTC + triplaQ1$VraiMAP + triplaQ1$Vrai2RM + triplaQ1$VraiTaxi

  return(sum(triplaQ1$somme_vrai))

}

x1 <- sqrt((sum(DeplaQ1$dep_VPC) - sum(DeplaQ1$simuleVPC))^2 + (sum(DeplaQ1$dep_VPP) - sum(DeplaQ1$simuleVPP))^2 + (sum(DeplaQ1$dep_TC) - sum(DeplaQ1$simuleTC))^2 + (sum(DeplaQ1$dep_2RM) - sum(DeplaQ1$simule2RM))^2 + (sum(DeplaQ1$dep_MP) - sum(DeplaQ1$simuleMAP))^2 + (sum(DeplaQ1$dep_Taxi) - sum(DeplaQ1$simuleTaxi))^2)

thank you in advance

first translate the EXCEL formula you want to find a target value to to a r-function. I did this for a very simple formula x^2+y^2 here, this is my_fun in the example. Then define a function that gives the squared difference (or any other norm you want to use) between target value and function value for all inputs. Then just put everything into the optim function.

my_fun <- function(x){
  x[1]^2 + x[2]^2
}

target <- 2

optim_fun <- function(x){
  sum((my_fun(x)-target)^2)
}

res <- optim(c(0,0), optim_fun)

res

To deal with constraints you could introduce a penalty term. Some algorithms in optim also support constraints. There are a lot of more specialised optimisation libraries for R if the very basic optim function does not fit your need.

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