1

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".

AnonX
  • 167
  • 8
  • Maybe you need to add constraints to remove solutions you already know, like https://stackoverflow.com/a/37779437/216064 – Karsten W. Feb 18 '23 at 22:00
  • Gonna take a look at how to implement it in my code. Thanks. – AnonX Feb 19 '23 at 09:48
  • Couldn't find how to append my previous solutions to my solver... Do you have any idea how to add them? – AnonX Feb 19 '23 at 12:43
  • Asking for different solution for a problem with just continuous variables is not a meaningful question. (We can perturb the solution by an infinitesimal small number, and we have a new solution). May be you want to get a few different bases. That can be done, but is not easy. See e.g. https://yetanothermathprogrammingconsultant.blogspot.com/2016/01/finding-all-optimal-lp-solutions.html – Erwin Kalvelagen Feb 20 '23 at 16:40

0 Answers0