MIP using R
回答済みHi, I was solving an MIP using R Gurobi, this was my original code and it can solve it. Then I changed the inputs from Xi being (0,1) to Zi being(-1,0,1), I added a quadratic constraint Sum of Zi^<=m and tried to solve the MIP again, and it always gave me the infeasible solution, even when I set the quadratic constraint to be redundant, could you please take a look and tell me if I'm using something wrong here, Thank you! Attached is the main code for both parts.
Original was:
n <- nrow(W)
nedge <- rowSums(W)
nedge.total <- sum(nedge)
delta <- qnorm(alpha)*sqrt(sum(nedge^2))
model <- list()
params <- list(MIPGap=0.01, TimeLimit=300)
A <- matrix(1, 1, n)
A <- rbind(A, nedge)
A <- rbind(A, nedge)
model$A <- A
model$rhs <- c(ifelse(n%%2,(n+1)/2, n/2), nedge.total/2+delta/2, nedge.total/2-delta/2)
model$sense <- c("=","<=",">=")# set up quadratic objective
model$Q <- W*obj.sign
model$obj <- -rowSums(W)*obj.signmodel$vtype <- rep('B', n)
result <- gurobi(model, params)
And then this is the updated code:
n <- nrow(W)
nedge <- rowSums(W)
nedge.total <- sum(nedge)
delta <- qnorm(alpha)*sqrt(sum(nedge^2))
model <- list()
A <- matrix(1, 1, n)
A <- rbind(A, nedge)
A <- rbind(A, -nedge)
model$A <- A
model$rhs <- c(ifelse(n%%2,(n+1)/2, n/2), nedge.total/2 + delta/2, nedge.total/2 - delta/2)
model$sense <- c("=", "<=", ">=")
model$Q <- W * obj.sign
model$obj <- -rowSums(W) * obj.sign
# Set the variable types and bounds for Zi variables
model$vtype <- rep('I', n)
model$lb <- rep(-1, n)
model$ub <- rep(1, n)
Qc <- sparseMatrix(i = 1:n, j = 1:n, x = 1) # Diagonal elements set to 1 for Zi^2
# Define the quadratic constraint for the sum of Zi^2 being less than m
model$quadcon <- list(list(Qc = Qc, sense = "<", rhs = m))
params <- list(MIPGap = 0.01, TimeLimit = 300, BarConvTol = 1e-8, BarQCPConvTol = 1e-8)
result <- gurobi(model, params)
print(result)
-
Please refer to the Knowledge Base article How do I determine why my model is infeasible? It is probably best to compute an IIS. In R you can compute an IIS for your infeasible model via the gurobi_iis() function.
It might also make sense to use the gurobi_write() function to write your model to a human-readable LP file and inspect this file by hand to try to find a mistake in the formulation.
Best regards,
Jaromił0
サインインしてコメントを残してください。
コメント
1件のコメント