How to find k solutions of an optimization with CVXR

113 Views Asked by At

I'm trying to find multiple solutions (TXs[1],TXs[2],TXs[3],TXs[4],TXs[5],TZs) that respect the following conditions:

# Variables :
TXs <- Variable(5)
TZs <- Variable(1)


# Objectif :
obj = abs(TXs[1] + TXs[2] + TXs[3] + TXs[4] + TXs[5] + TZs - 100)

# Conditions :
abs(TXs[1] - 2) <=1
abs(TXs[2] - 55) <= 2
abs(TXs[3] - 25) <= 0.5
abs(TXs[4] - 8) <= 1
abs(TXs[5] - 7) <= 1
abs(TZs[1] - 1.5) <= 1

cor(TXs[1], TXs[2]) = 0.77
cor(TXs[3], TXs[2]) = 0.85
cor(TXs[4], TXs[2]) = 0.88
cor(TXs[5], TXs[2]) = 0.99
cor(TZs, TXs[2]) = 0.4

abs(TXs[1] + TXs[2] + TXs[3] + TXs[4] + TXs[5] + TZs[1] - 100) <=  0.001)

I've written the following code that tries to find k solutions but it fails as I always get the same result:

library(CVXR)

# k solutions
k <- 10
solutions <- matrix(NA, nrow = k, ncol = 6)


# Variables
TXs <- Variable(5)
TZs <- Variable(1)

# Objectif
obj = abs(TXs[1] + TXs[2] + TXs[3] + TXs[4] + TXs[5] + TZs - 100)

for (i in 1:k) {
  print(i)
  
  # Problem
  prob = Problem(Minimize(obj),
                 list(abs(TXs[1] - 2) <= 1,  ((TXs[1] - 2)/ 1) == (0.77 * (TXs[2] - 55)/ 2), 
                      abs(TXs[3] - 25) <= 2,  ((TXs[3] - 25)/ 2) == (0.85 * (TXs[2] - 55)/ 2),
                      abs(TXs[4] - 8) <= 0.5,  ((TXs[4] - 8)/ 0.5) == (0.88 * (TXs[2] - 55)/ 2),
                      abs(TXs[5] - 7) <= 1,  ((TXs[5] - 7)/ 1) == (0.99 * (TXs[2] - 55)/ 2),
                      abs(TZs[1] - 1.5) <= 1.2,  ((TZs - 1.5)/ 1.2) == (0.4 * (TXs[2] - 55)/ 2),
                      abs(TXs[1] + TXs[2] + TXs[3] + TXs[4] + TXs[5] + TZs[1] - 100) <=  0.001))
  
  result = solve(prob, verbose = TRUE )
  solutions[i,] <- c(result$getValue(TXs[1]),
                     result$getValue(TXs[2]),#TXs[2],
                     result$getValue(TXs[3]),
                     result$getValue(TXs[4]),
                     result$getValue(TXs[5]),
                     result$getValue(TZs[1]))
}

solutions = as.data.frame(solutions)
colnames(solutions) = c("TXs[1]","TXs[2]","TXs[3]","TXs[4]","TXs[5]","TZs" )
solutions$Somme = rowSums(solutions)

Is there a way to modify my code to get multiple solutions? I am also open to other alternatives to "CVXR".

0

There are 0 best solutions below