2

I have little background in mathematics and am trying to write a multi-objective optimization function.

I have the following 3 vectors:

A = 0.4122487 0.3861316 0.3160613 0.2949684
B = 0.1407469 0.1828053 0.2088941 0.2143583
C = 0.2966363 0.1947112 0.1664350 0.1543946

My goal is the following: to find at which value X my requirements are best met: 1) minimize A, 2) maximize B and 3) maximize C.

enter image description here

Here is my code:

fun <- function(weights,A,B,C){
   fit = sum((weights[1] * A +
        weights[2] * B +
        weights[3] * C))
    return(fit)
}

# the weight of A (positive, since I want to minimise A)
Wa = 1
# the weight of B (negative, since I want to maximise B)
Wb = -0.5
# the weight of C (negative, since I want to maximise C)
Wc = -1.5
result <- optim(weights=c(Wa,Wb,Wc), fn = fun)

Here are the results:

result$par = 2.365022e+44 -1.697108e+44 -9.150244e+43
result$value = -5.343856e+44

Is my implementation correct? How do I interpret these results? My understanding is very limited, but instinctively these numbers seem out of proportion compared to the range of my initial vectors...

1 Answers1

2

Answer:

Implementation fails the following way: par[1] * A is a vector of four elements, so is par[2] * B and par[3] * [C]. Your function fun first calculates sum of three vectors and then sums elements of it.

Function optim() tries to find such parameters of vector par that return return value of your function is lowest possible. That means it maximises the sum of vector A multiplied by par[1] and minimises sums of vectors B and C multiplied by par[2] and par[3].

The answer would be quite similar when you just maximise par[1] and minimise par[2] and par[3] with no other constraints. Mathematical answer would be infinities, but it seems function optim() reaches maximum computable values which are for example 2.36 *10^44 (scientific notation).

This way it does not meet your needs and it needs to be implemented other way.

Solution:

It is hard to define what you really want to do: are A, B, C discrete vectors or they are supposed represent functions, as they are plotted? Other words your solution should use exact values from these vectors or something between? After providing such information a scheme of possible solution to that problem may be found somewhere.

[edit: added solution below]

There is a stack overflow question for discrete optimisation using optim() here. But i think it is too big tank to kill a mouse this time.

I would calculate a value of a function in Data Frame and use max() to find best choice:

# sample data:
A <- c(0.4122487, 0.3861316, 0.3160613, 0.2949684)
B <- c(0.1407469, 0.1828053, 0.2088941, 0.2143583)
C <- c(0.2966363, 0.1947112, 0.1664350, 0.1543946)
df <- data.frame(A = A, B = B, C = C)

#solution
df$fun <- df$A - df$B - df$C 
head(df)
df[df$fun == max(df$fun),]

You can also find some tutorials for using optim() at google if you just want to learn some stuff.

Community
  • 1
  • 1
cure
  • 425
  • 2
  • 12
  • Great answer! Can you please take a look at this question? https://stackoverflow.com/questions/68280857/r-x-probs-outside-0-1 – stats_noob Jul 07 '21 at 19:54