Optimazation with DEoptim

Hey,

i have a problem with my R code maybe you can help me.

I have written an function which depend on the two variables P and alpha.

Now i want to know the "optimal" value for my two variables with DEoptim.

I know how to use the function if i want to optimize one variable. Is it also possible to do with two variables?

P should go from 0 - 100
and alpha should go from 1 - 2

Thank you very much for your help.

this is the code so far:

library(DEoptim)

outDEoptim = DEoptim(fkt.Palpha,
lower = c(),
upper = c(),
DEoptim.control(NP=20,
itermax = 20))

I think what you are asking is in the package documentation.

library(DEoptim)
Rosenbrock <- function(x){
  x1 <- x[1]
  x2 <- x[2]
  100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
## DEoptim searches for minima of the objective function between
## lower and upper bounds on each parameter to be optimized. Therefore
## in the call to DEoptim we specify vectors that comprise the
## lower and upper bounds; these vectors are the same length as the
## parameter vector.
lower <- c(0  ,1)
upper <- c(100,2)
## run DEoptim and set a seed first for replicability
set.seed(1234)

opt_result <- DEoptim(Rosenbrock, lower, upper)

note this likely suggests that you need to alter your function so that it takes parameters in the way that the Rosenbrock example function does)

1 Like

Thank you for your help. I guess it helped a little bit but now i have the next problem:

"Error in FUN(newX[, i], ...) :
Argument "alpha" is missing, with no default"

The function before uses P and alpha but the result is a single number:
P = 50
alpha = 1,5
return value = 6300 (for example)

Could this be a problem when i try to use the code like this?

outDEoptim = DEoptim(fkt.Palpha,
lower = c(0, 1),
upper = c(100, 2),
DEoptim.control(NP=20,
itermax = 20))

I don't think its possible to help you without seeing your custom function.

1 Like

This is the function:

fkt.Pal = function(P, alpha) {

#Allg. Berechnung
x.Re = numeric(length(S.1)

for (i in 1:length(S.1)) {
x.Re[i] = min(max(S.1[i] - P, 0), L)
}
pi.S = mean(S.1)(1+load.S)
pi.Re = mean(x.Re)
(1+load.Re)

A.0 = (EK.0 + pi.S) -pi.Re
alpha.reich = 1 - alpha

A.1 = A.0exp(alphar.arm + alpha.reich*r.reich)

RBC.1 = A.1 - S.1 + x.Re

SP.1 = numeric(length(RBC.1))

for (i in 1:length(RBC.1)) {
if(RBC.1[i] < 0) {SP.1[i] = 1}
else{SP.1[i] = 0}
}

SP.1 = mean(SP.1)

RBC.SP = mean(RBC.1)/SP.1

return(c(RBC.SP))
}

Thank you for your help :slight_smile:

fkt.Pal = function(x) {
P <- x[[1]]
alpha <- x[[2]]
1 Like

Thank you again :slight_smile:

How do i use the function then? Because if im trying to use it like this:

"fkt.Pal(100,3)"

i get an error: "Error in fkt.Pal(100, 3) : unused argument (3)"

fkt.Pal(c(100,3))
1 Like

Sir,

you deserve a medal. :heart_eyes:

Now its working. Again thank you very much :slight_smile:

1 Like

This are the results from DEoptim:

outDEoptim #Ausgabe der Ergebnisse
$optim
$optim$bestmem
par1 par2
161.9478944 0.8274849

$optim$bestval
[1] -6528.56

$optim$nfeval
[1] 42

$optim$iter
[1] 20

$member
$member$lower
par1 par2
0 0

$member$upper
par1 par2
500 1

$member$bestmemit
par1 par2
1 121.9615 0.2909649
2 121.9615 0.2909649
3 121.9615 0.2909649
4 121.9615 0.2088695
5 145.6629 0.3848632
6 146.3210 0.4495587
7 152.0692 0.5725310
8 153.2743 0.5282415
9 164.8084 0.7949481
10 161.9114 0.7856480
11 161.9114 0.7856480
12 161.9114 0.7856480
13 161.9114 0.7856480
14 161.9114 0.7856480
15 161.9114 0.7856480
16 161.9114 0.7856480
17 161.9114 0.7856480
18 161.9479 0.8274849
19 161.9479 0.8274849
20 161.9479 0.8274849

$member$bestvalit
[1] -5536.246 -5536.246 -5536.246 -5583.002 -6261.338 -6283.910 -6417.670 -6417.865 -6507.891 -6526.310
[11] -6526.310 -6526.310 -6526.310 -6526.310 -6526.310 -6526.310 -6526.310 -6528.560 -6528.560 -6528.560

$member$pop
[,1] [,2]
[1,] 163.4554 0.7856786
[2,] 166.3820 0.9109723
[3,] 163.9783 0.8731127
[4,] 160.1948 0.7224186
[5,] 164.5761 0.8409223
[6,] 161.1247 0.8042221
[7,] 161.9114 0.7736972
[8,] 162.7878 0.7729358
[9,] 160.8854 0.7853638
[10,] 161.3228 0.8003717
[11,] 161.9479 0.8274849
[12,] 162.6249 0.8535452
[13,] 159.8767 0.7202999
[14,] 161.9325 0.7497633
[15,] 159.0639 0.7289032
[16,] 162.8852 0.7631842
[17,] 160.8101 0.7899345
[18,] 162.3477 0.8513563
[19,] 158.4636 0.7464541
[20,] 162.7054 0.8006427

$member$storepop
list()

Is there a possibility to plot the results so i can see how $bestval changes with changes of variable one and two?

assuming you still have the version of the function with the parameters P and Alpha you can do this

 #decide which points to plot
 params_df <- expand.grid(P=0:100,
              Alpha=seq(1,2,.1))
  
  #get function values at the points
  params_df$fval <- fkt.Pal(params_df$P,params_df$Alpha)
  
# plot them
  library(plotly)
  plot_ly(data=params_df,
         x=~P,
         y=~Alpha,
         z=~fval,
         color = ~fval,
         type="scatter3d",
         text = ~paste0("P ",P ,"\n",
                       "Alpha ",Alpha ,"\n",
                       "fval ",fval ,"\n"),
         hoverinfo='text')

i get an error with this lines:

error in fkt.Pal(params_df$P, params_df$Alpha) :
unused argument (params_df$Alpha)

do you know where's the problem? i already checked the spelling but there are no mistakes.

Thanks :slight_smile:

Did you remember to go back to this?

Hi Major,

could you please show me the code and the whole DeOptim command?

have the same problem and no idea, because still doesnt work.

Thanks a lot!!

btw thats my command that fails:

  result_optimization = -RBC_fct_mean/SP_1_fct# negativ => bei Minimierung durch Optimierer(Kostenfkt)
  # word Ergebnis maximiert
  
  return(result_optimization) 
  
}

#result_PAL = fkt.Pal(100,0.3)

lower = c(0,0)
upper = c(500,1)


#set.seed(1234)
library(DEoptim)

outDEoptim = DEoptim(fkt.Pal,
                     lower = lower, upper=upper,
                     DEoptim.control(NP = 20, itermax = 20))

Ok habs hinbekommen :DD

einfach nur die optimierungsfunktion als x angeben und dann mit x[1] = P und x[2] = alpha in die formeln einsetzen

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.